blob: 5c33d65c055f37be80e0044a4c9ea2549bc7ee8a [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
Chris Lattnerdf986172009-01-02 07:01:27 +0000252 const std::string &AsmSoFar = M->getModuleInlineAsm();
253 if (AsmSoFar.empty())
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000254 M->setModuleInlineAsm(AsmStr);
Chris Lattnerdf986172009-01-02 07:01:27 +0000255 else
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000256 M->setModuleInlineAsm(AsmSoFar+"\n"+AsmStr);
Chris Lattnerdf986172009-01-02 07:01:27 +0000257 return false;
258}
259
260/// toplevelentity
261/// ::= 'target' 'triple' '=' STRINGCONSTANT
262/// ::= 'target' 'datalayout' '=' STRINGCONSTANT
263bool LLParser::ParseTargetDefinition() {
264 assert(Lex.getKind() == lltok::kw_target);
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000265 std::string Str;
Chris Lattnerdf986172009-01-02 07:01:27 +0000266 switch (Lex.Lex()) {
267 default: return TokError("unknown target property");
268 case lltok::kw_triple:
269 Lex.Lex();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000270 if (ParseToken(lltok::equal, "expected '=' after target triple") ||
271 ParseStringConstant(Str))
Chris Lattnerdf986172009-01-02 07:01:27 +0000272 return true;
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000273 M->setTargetTriple(Str);
Chris Lattnerdf986172009-01-02 07:01:27 +0000274 return false;
275 case lltok::kw_datalayout:
276 Lex.Lex();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000277 if (ParseToken(lltok::equal, "expected '=' after target datalayout") ||
278 ParseStringConstant(Str))
Chris Lattnerdf986172009-01-02 07:01:27 +0000279 return true;
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000280 M->setDataLayout(Str);
Chris Lattnerdf986172009-01-02 07:01:27 +0000281 return false;
282 }
283}
284
285/// toplevelentity
286/// ::= 'deplibs' '=' '[' ']'
287/// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']'
288bool LLParser::ParseDepLibs() {
289 assert(Lex.getKind() == lltok::kw_deplibs);
Chris Lattnerdf986172009-01-02 07:01:27 +0000290 Lex.Lex();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000291 if (ParseToken(lltok::equal, "expected '=' after deplibs") ||
292 ParseToken(lltok::lsquare, "expected '=' after deplibs"))
293 return true;
294
295 if (EatIfPresent(lltok::rsquare))
296 return false;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000297
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000298 std::string Str;
299 if (ParseStringConstant(Str)) return true;
300 M->addLibrary(Str);
301
302 while (EatIfPresent(lltok::comma)) {
303 if (ParseStringConstant(Str)) return true;
304 M->addLibrary(Str);
305 }
306
307 return ParseToken(lltok::rsquare, "expected ']' at end of list");
Chris Lattnerdf986172009-01-02 07:01:27 +0000308}
309
Dan Gohman3845e502009-08-12 23:32:33 +0000310/// ParseUnnamedType:
Chris Lattnerdf986172009-01-02 07:01:27 +0000311/// ::= 'type' type
Dan Gohman3845e502009-08-12 23:32:33 +0000312/// ::= LocalVarID '=' 'type' type
Chris Lattnerdf986172009-01-02 07:01:27 +0000313bool LLParser::ParseUnnamedType() {
Dan Gohman3845e502009-08-12 23:32:33 +0000314 unsigned TypeID = NumberedTypes.size();
315
316 // Handle the LocalVarID form.
317 if (Lex.getKind() == lltok::LocalVarID) {
318 if (Lex.getUIntVal() != TypeID)
319 return Error(Lex.getLoc(), "type expected to be numbered '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000320 Twine(TypeID) + "'");
Dan Gohman3845e502009-08-12 23:32:33 +0000321 Lex.Lex(); // eat LocalVarID;
322
323 if (ParseToken(lltok::equal, "expected '=' after name"))
324 return true;
325 }
326
Chris Lattnerdf986172009-01-02 07:01:27 +0000327 LocTy TypeLoc = Lex.getLoc();
Chris Lattnerf7240de2010-04-10 18:01:25 +0000328 if (ParseToken(lltok::kw_type, "expected 'type' after '='")) return true;
Chris Lattnerdf986172009-01-02 07:01:27 +0000329
Owen Anderson1d0be152009-08-13 21:58:54 +0000330 PATypeHolder Ty(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +0000331 if (ParseType(Ty)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000332
Chris Lattnerdf986172009-01-02 07:01:27 +0000333 // See if this type was previously referenced.
334 std::map<unsigned, std::pair<PATypeHolder, LocTy> >::iterator
335 FI = ForwardRefTypeIDs.find(TypeID);
336 if (FI != ForwardRefTypeIDs.end()) {
Chris Lattnerc38daba2009-01-05 18:19:46 +0000337 if (FI->second.first.get() == Ty)
338 return Error(TypeLoc, "self referential type is invalid");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000339
Chris Lattnerdf986172009-01-02 07:01:27 +0000340 cast<DerivedType>(FI->second.first.get())->refineAbstractTypeTo(Ty);
341 Ty = FI->second.first.get();
342 ForwardRefTypeIDs.erase(FI);
343 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000344
Chris Lattnerdf986172009-01-02 07:01:27 +0000345 NumberedTypes.push_back(Ty);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000346
Chris Lattnerdf986172009-01-02 07:01:27 +0000347 return false;
348}
349
350/// toplevelentity
351/// ::= LocalVar '=' 'type' type
352bool LLParser::ParseNamedType() {
353 std::string Name = Lex.getStrVal();
354 LocTy NameLoc = Lex.getLoc();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000355 Lex.Lex(); // eat LocalVar.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000356
Owen Anderson1d0be152009-08-13 21:58:54 +0000357 PATypeHolder Ty(Type::getVoidTy(Context));
Daniel Dunbara279bc32009-09-20 02:20:51 +0000358
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000359 if (ParseToken(lltok::equal, "expected '=' after name") ||
360 ParseToken(lltok::kw_type, "expected 'type' after name") ||
361 ParseType(Ty))
362 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000363
Chris Lattnerdf986172009-01-02 07:01:27 +0000364 // Set the type name, checking for conflicts as we do so.
365 bool AlreadyExists = M->addTypeName(Name, Ty);
366 if (!AlreadyExists) return false;
367
368 // See if this type is a forward reference. We need to eagerly resolve
369 // types to allow recursive type redefinitions below.
370 std::map<std::string, std::pair<PATypeHolder, LocTy> >::iterator
371 FI = ForwardRefTypes.find(Name);
372 if (FI != ForwardRefTypes.end()) {
Chris Lattnerc38daba2009-01-05 18:19:46 +0000373 if (FI->second.first.get() == Ty)
374 return Error(NameLoc, "self referential type is invalid");
375
Chris Lattnerdf986172009-01-02 07:01:27 +0000376 cast<DerivedType>(FI->second.first.get())->refineAbstractTypeTo(Ty);
377 Ty = FI->second.first.get();
378 ForwardRefTypes.erase(FI);
379 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000380
Chris Lattnerdf986172009-01-02 07:01:27 +0000381 // Inserting a name that is already defined, get the existing name.
382 const Type *Existing = M->getTypeByName(Name);
383 assert(Existing && "Conflict but no matching type?!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000384
Chris Lattnerdf986172009-01-02 07:01:27 +0000385 // Otherwise, this is an attempt to redefine a type. That's okay if
386 // the redefinition is identical to the original.
387 // FIXME: REMOVE REDEFINITIONS IN LLVM 3.0
388 if (Existing == Ty) return false;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000389
Chris Lattnerdf986172009-01-02 07:01:27 +0000390 // Any other kind of (non-equivalent) redefinition is an error.
391 return Error(NameLoc, "redefinition of type named '" + Name + "' of type '" +
392 Ty->getDescription() + "'");
393}
394
395
396/// toplevelentity
397/// ::= 'declare' FunctionHeader
398bool LLParser::ParseDeclare() {
399 assert(Lex.getKind() == lltok::kw_declare);
400 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000401
Chris Lattnerdf986172009-01-02 07:01:27 +0000402 Function *F;
403 return ParseFunctionHeader(F, false);
404}
405
406/// toplevelentity
407/// ::= 'define' FunctionHeader '{' ...
408bool LLParser::ParseDefine() {
409 assert(Lex.getKind() == lltok::kw_define);
410 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000411
Chris Lattnerdf986172009-01-02 07:01:27 +0000412 Function *F;
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000413 return ParseFunctionHeader(F, true) ||
414 ParseFunctionBody(*F);
Chris Lattnerdf986172009-01-02 07:01:27 +0000415}
416
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000417/// ParseGlobalType
418/// ::= 'constant'
419/// ::= 'global'
Chris Lattnerdf986172009-01-02 07:01:27 +0000420bool LLParser::ParseGlobalType(bool &IsConstant) {
421 if (Lex.getKind() == lltok::kw_constant)
422 IsConstant = true;
423 else if (Lex.getKind() == lltok::kw_global)
424 IsConstant = false;
Duncan Sands35b51072009-02-10 16:24:55 +0000425 else {
426 IsConstant = false;
Chris Lattnerdf986172009-01-02 07:01:27 +0000427 return TokError("expected 'global' or 'constant'");
Duncan Sands35b51072009-02-10 16:24:55 +0000428 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000429 Lex.Lex();
430 return false;
431}
432
Dan Gohman3845e502009-08-12 23:32:33 +0000433/// ParseUnnamedGlobal:
434/// OptionalVisibility ALIAS ...
435/// OptionalLinkage OptionalVisibility ... -> global variable
436/// GlobalID '=' OptionalVisibility ALIAS ...
437/// GlobalID '=' OptionalLinkage OptionalVisibility ... -> global variable
438bool LLParser::ParseUnnamedGlobal() {
439 unsigned VarID = NumberedVals.size();
440 std::string Name;
441 LocTy NameLoc = Lex.getLoc();
442
443 // Handle the GlobalID form.
444 if (Lex.getKind() == lltok::GlobalID) {
445 if (Lex.getUIntVal() != VarID)
446 return Error(Lex.getLoc(), "variable expected to be numbered '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000447 Twine(VarID) + "'");
Dan Gohman3845e502009-08-12 23:32:33 +0000448 Lex.Lex(); // eat GlobalID;
449
450 if (ParseToken(lltok::equal, "expected '=' after name"))
451 return true;
452 }
453
454 bool HasLinkage;
455 unsigned Linkage, Visibility;
456 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
457 ParseOptionalVisibility(Visibility))
458 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000459
Dan Gohman3845e502009-08-12 23:32:33 +0000460 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
461 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility);
462 return ParseAlias(Name, NameLoc, Visibility);
463}
464
Chris Lattnerdf986172009-01-02 07:01:27 +0000465/// ParseNamedGlobal:
466/// GlobalVar '=' OptionalVisibility ALIAS ...
467/// GlobalVar '=' OptionalLinkage OptionalVisibility ... -> global variable
468bool LLParser::ParseNamedGlobal() {
469 assert(Lex.getKind() == lltok::GlobalVar);
470 LocTy NameLoc = Lex.getLoc();
471 std::string Name = Lex.getStrVal();
472 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000473
Chris Lattnerdf986172009-01-02 07:01:27 +0000474 bool HasLinkage;
475 unsigned Linkage, Visibility;
476 if (ParseToken(lltok::equal, "expected '=' in global variable") ||
477 ParseOptionalLinkage(Linkage, HasLinkage) ||
478 ParseOptionalVisibility(Visibility))
479 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000480
Chris Lattnerdf986172009-01-02 07:01:27 +0000481 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
482 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility);
483 return ParseAlias(Name, NameLoc, Visibility);
484}
485
Devang Patel256be962009-07-20 19:00:08 +0000486// MDString:
487// ::= '!' STRINGCONSTANT
Chris Lattner442ffa12009-12-29 21:53:55 +0000488bool LLParser::ParseMDString(MDString *&Result) {
Devang Patel256be962009-07-20 19:00:08 +0000489 std::string Str;
490 if (ParseStringConstant(Str)) return true;
Chris Lattner442ffa12009-12-29 21:53:55 +0000491 Result = MDString::get(Context, Str);
Devang Patel256be962009-07-20 19:00:08 +0000492 return false;
493}
494
495// MDNode:
496// ::= '!' MDNodeNumber
Chris Lattner449c3102010-04-01 05:14:45 +0000497//
498/// This version of ParseMDNodeID returns the slot number and null in the case
499/// of a forward reference.
500bool LLParser::ParseMDNodeID(MDNode *&Result, unsigned &SlotNo) {
501 // !{ ..., !42, ... }
502 if (ParseUInt32(SlotNo)) return true;
503
504 // Check existing MDNode.
505 if (SlotNo < NumberedMetadata.size() && NumberedMetadata[SlotNo] != 0)
506 Result = NumberedMetadata[SlotNo];
507 else
508 Result = 0;
509 return false;
510}
511
Chris Lattner4a72efc2009-12-30 04:15:23 +0000512bool LLParser::ParseMDNodeID(MDNode *&Result) {
Devang Patel256be962009-07-20 19:00:08 +0000513 // !{ ..., !42, ... }
514 unsigned MID = 0;
Chris Lattner449c3102010-04-01 05:14:45 +0000515 if (ParseMDNodeID(Result, MID)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000516
Chris Lattner449c3102010-04-01 05:14:45 +0000517 // If not a forward reference, just return it now.
518 if (Result) return false;
Devang Patel256be962009-07-20 19:00:08 +0000519
Chris Lattner449c3102010-04-01 05:14:45 +0000520 // Otherwise, create MDNode forward reference.
Dan Gohman489b29b2010-08-20 22:02:26 +0000521 MDNode *FwdNode = MDNode::getTemporary(Context, 0, 0);
Devang Patel256be962009-07-20 19:00:08 +0000522 ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc());
Chris Lattner0834e6a2009-12-30 04:51:58 +0000523
524 if (NumberedMetadata.size() <= MID)
525 NumberedMetadata.resize(MID+1);
526 NumberedMetadata[MID] = FwdNode;
Chris Lattner442ffa12009-12-29 21:53:55 +0000527 Result = FwdNode;
Devang Patel256be962009-07-20 19:00:08 +0000528 return false;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000529}
Devang Patel256be962009-07-20 19:00:08 +0000530
Chris Lattner84d03b12009-12-29 22:35:39 +0000531/// ParseNamedMetadata:
Devang Pateleff2ab62009-07-29 00:34:02 +0000532/// !foo = !{ !1, !2 }
533bool LLParser::ParseNamedMetadata() {
Chris Lattner1d928312009-12-30 05:02:06 +0000534 assert(Lex.getKind() == lltok::MetadataVar);
Devang Pateleff2ab62009-07-29 00:34:02 +0000535 std::string Name = Lex.getStrVal();
Chris Lattner1d928312009-12-30 05:02:06 +0000536 Lex.Lex();
Devang Pateleff2ab62009-07-29 00:34:02 +0000537
Chris Lattner84d03b12009-12-29 22:35:39 +0000538 if (ParseToken(lltok::equal, "expected '=' here") ||
Chris Lattnere434d272009-12-30 04:56:59 +0000539 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattner84d03b12009-12-29 22:35:39 +0000540 ParseToken(lltok::lbrace, "Expected '{' here"))
Devang Pateleff2ab62009-07-29 00:34:02 +0000541 return true;
542
Dan Gohman17aa92c2010-07-21 23:38:33 +0000543 NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
Dan Gohman9dc8ae12010-07-13 19:42:44 +0000544 if (Lex.getKind() != lltok::rbrace)
545 do {
Dan Gohman9dc8ae12010-07-13 19:42:44 +0000546 if (ParseToken(lltok::exclaim, "Expected '!' here"))
547 return true;
Chris Lattner442ffa12009-12-29 21:53:55 +0000548
Dan Gohman9dc8ae12010-07-13 19:42:44 +0000549 MDNode *N = 0;
550 if (ParseMDNodeID(N)) return true;
Dan Gohman17aa92c2010-07-21 23:38:33 +0000551 NMD->addOperand(N);
Dan Gohman9dc8ae12010-07-13 19:42:44 +0000552 } while (EatIfPresent(lltok::comma));
Devang Pateleff2ab62009-07-29 00:34:02 +0000553
554 if (ParseToken(lltok::rbrace, "expected end of metadata node"))
555 return true;
556
Devang Pateleff2ab62009-07-29 00:34:02 +0000557 return false;
558}
559
Devang Patel923078c2009-07-01 19:21:12 +0000560/// ParseStandaloneMetadata:
Daniel Dunbara279bc32009-09-20 02:20:51 +0000561/// !42 = !{...}
Devang Patel923078c2009-07-01 19:21:12 +0000562bool LLParser::ParseStandaloneMetadata() {
Chris Lattnere434d272009-12-30 04:56:59 +0000563 assert(Lex.getKind() == lltok::exclaim);
Devang Patel923078c2009-07-01 19:21:12 +0000564 Lex.Lex();
565 unsigned MetadataID = 0;
Devang Patel923078c2009-07-01 19:21:12 +0000566
567 LocTy TyLoc;
Owen Anderson1d0be152009-08-13 21:58:54 +0000568 PATypeHolder Ty(Type::getVoidTy(Context));
Devang Patel104cf9e2009-07-23 01:07:34 +0000569 SmallVector<Value *, 16> Elts;
Chris Lattner3f5132a2009-12-29 22:40:21 +0000570 if (ParseUInt32(MetadataID) ||
571 ParseToken(lltok::equal, "expected '=' here") ||
572 ParseType(Ty, TyLoc) ||
Chris Lattnere434d272009-12-30 04:56:59 +0000573 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattner3f5132a2009-12-29 22:40:21 +0000574 ParseToken(lltok::lbrace, "Expected '{' here") ||
Victor Hernandez24e64df2010-01-10 07:14:18 +0000575 ParseMDNodeVector(Elts, NULL) ||
Chris Lattner3f5132a2009-12-29 22:40:21 +0000576 ParseToken(lltok::rbrace, "expected end of metadata node"))
Devang Patel104cf9e2009-07-23 01:07:34 +0000577 return true;
578
Owen Anderson647e3012009-07-31 21:35:40 +0000579 MDNode *Init = MDNode::get(Context, Elts.data(), Elts.size());
Chris Lattner0834e6a2009-12-30 04:51:58 +0000580
581 // See if this was forward referenced, if so, handle it.
Chris Lattnere80250e2009-12-29 21:43:58 +0000582 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> >::iterator
Devang Patel1c7eea62009-07-08 19:23:54 +0000583 FI = ForwardRefMDNodes.find(MetadataID);
584 if (FI != ForwardRefMDNodes.end()) {
Dan Gohman489b29b2010-08-20 22:02:26 +0000585 MDNode *Temp = FI->second.first;
586 Temp->replaceAllUsesWith(Init);
587 MDNode::deleteTemporary(Temp);
Devang Patel1c7eea62009-07-08 19:23:54 +0000588 ForwardRefMDNodes.erase(FI);
Chris Lattner0834e6a2009-12-30 04:51:58 +0000589
590 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
591 } else {
592 if (MetadataID >= NumberedMetadata.size())
593 NumberedMetadata.resize(MetadataID+1);
594
595 if (NumberedMetadata[MetadataID] != 0)
596 return TokError("Metadata id is already used");
597 NumberedMetadata[MetadataID] = Init;
Devang Patel1c7eea62009-07-08 19:23:54 +0000598 }
599
Devang Patel923078c2009-07-01 19:21:12 +0000600 return false;
601}
602
Chris Lattnerdf986172009-01-02 07:01:27 +0000603/// ParseAlias:
604/// ::= GlobalVar '=' OptionalVisibility 'alias' OptionalLinkage Aliasee
605/// Aliasee
Chris Lattner040f7582009-04-25 21:26:00 +0000606/// ::= TypeAndValue
607/// ::= 'bitcast' '(' TypeAndValue 'to' Type ')'
Dan Gohmandd8004d2009-07-27 21:53:46 +0000608/// ::= 'getelementptr' 'inbounds'? '(' ... ')'
Chris Lattnerdf986172009-01-02 07:01:27 +0000609///
610/// Everything through visibility has already been parsed.
611///
612bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc,
613 unsigned Visibility) {
614 assert(Lex.getKind() == lltok::kw_alias);
615 Lex.Lex();
616 unsigned Linkage;
617 LocTy LinkageLoc = Lex.getLoc();
618 if (ParseOptionalLinkage(Linkage))
619 return true;
620
621 if (Linkage != GlobalValue::ExternalLinkage &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000622 Linkage != GlobalValue::WeakAnyLinkage &&
623 Linkage != GlobalValue::WeakODRLinkage &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000624 Linkage != GlobalValue::InternalLinkage &&
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000625 Linkage != GlobalValue::PrivateLinkage &&
Bill Wendling5e721d72010-07-01 21:55:59 +0000626 Linkage != GlobalValue::LinkerPrivateLinkage &&
Bill Wendling55ae5152010-08-20 22:05:50 +0000627 Linkage != GlobalValue::LinkerPrivateWeakLinkage &&
628 Linkage != GlobalValue::LinkerPrivateWeakDefAutoLinkage)
Chris Lattnerdf986172009-01-02 07:01:27 +0000629 return Error(LinkageLoc, "invalid linkage type for alias");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000630
Chris Lattnerdf986172009-01-02 07:01:27 +0000631 Constant *Aliasee;
632 LocTy AliaseeLoc = Lex.getLoc();
Chris Lattner040f7582009-04-25 21:26:00 +0000633 if (Lex.getKind() != lltok::kw_bitcast &&
634 Lex.getKind() != lltok::kw_getelementptr) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000635 if (ParseGlobalTypeAndValue(Aliasee)) return true;
636 } else {
637 // The bitcast dest type is not present, it is implied by the dest type.
638 ValID ID;
639 if (ParseValID(ID)) return true;
640 if (ID.Kind != ValID::t_Constant)
641 return Error(AliaseeLoc, "invalid aliasee");
642 Aliasee = ID.ConstantVal;
643 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000644
Duncan Sands1df98592010-02-16 11:11:14 +0000645 if (!Aliasee->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +0000646 return Error(AliaseeLoc, "alias must have pointer type");
647
648 // Okay, create the alias but do not insert it into the module yet.
649 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(),
650 (GlobalValue::LinkageTypes)Linkage, Name,
651 Aliasee);
652 GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000653
Chris Lattnerdf986172009-01-02 07:01:27 +0000654 // See if this value already exists in the symbol table. If so, it is either
655 // a redefinition or a definition of a forward reference.
Chris Lattner1d871c52009-10-25 23:22:50 +0000656 if (GlobalValue *Val = M->getNamedValue(Name)) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000657 // See if this was a redefinition. If so, there is no entry in
658 // ForwardRefVals.
659 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
660 I = ForwardRefVals.find(Name);
661 if (I == ForwardRefVals.end())
662 return Error(NameLoc, "redefinition of global named '@" + Name + "'");
663
664 // Otherwise, this was a definition of forward ref. Verify that types
665 // agree.
666 if (Val->getType() != GA->getType())
667 return Error(NameLoc,
668 "forward reference and definition of alias have different types");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000669
Chris Lattnerdf986172009-01-02 07:01:27 +0000670 // If they agree, just RAUW the old value with the alias and remove the
671 // forward ref info.
672 Val->replaceAllUsesWith(GA);
673 Val->eraseFromParent();
674 ForwardRefVals.erase(I);
675 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000676
Chris Lattnerdf986172009-01-02 07:01:27 +0000677 // Insert into the module, we know its name won't collide now.
678 M->getAliasList().push_back(GA);
Benjamin Krameraf812352010-10-16 11:28:23 +0000679 assert(GA->getName() == Name && "Should not be a name conflict!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000680
Chris Lattnerdf986172009-01-02 07:01:27 +0000681 return false;
682}
683
684/// ParseGlobal
685/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalThreadLocal
Rafael Espindolabea46262011-01-08 16:42:36 +0000686/// OptionalAddrSpace OptionalUnNammedAddr GlobalType Type Const
Chris Lattnerdf986172009-01-02 07:01:27 +0000687/// ::= OptionalLinkage OptionalVisibility OptionalThreadLocal
Rafael Espindolabea46262011-01-08 16:42:36 +0000688/// OptionalAddrSpace OptionalUnNammedAddr GlobalType Type Const
Chris Lattnerdf986172009-01-02 07:01:27 +0000689///
690/// Everything through visibility has been parsed already.
691///
692bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
693 unsigned Linkage, bool HasLinkage,
694 unsigned Visibility) {
695 unsigned AddrSpace;
Rafael Espindolabea46262011-01-08 16:42:36 +0000696 bool ThreadLocal, IsConstant, UnnamedAddr;
Chris Lattnerdf986172009-01-02 07:01:27 +0000697 LocTy TyLoc;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000698
Owen Anderson1d0be152009-08-13 21:58:54 +0000699 PATypeHolder Ty(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +0000700 if (ParseOptionalToken(lltok::kw_thread_local, ThreadLocal) ||
701 ParseOptionalAddrSpace(AddrSpace) ||
Rafael Espindolabea46262011-01-08 16:42:36 +0000702 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr) ||
Chris Lattnerdf986172009-01-02 07:01:27 +0000703 ParseGlobalType(IsConstant) ||
704 ParseType(Ty, TyLoc))
705 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000706
Chris Lattnerdf986172009-01-02 07:01:27 +0000707 // If the linkage is specified and is external, then no initializer is
708 // present.
709 Constant *Init = 0;
710 if (!HasLinkage || (Linkage != GlobalValue::DLLImportLinkage &&
Duncan Sands5f4ee1f2009-03-11 08:08:06 +0000711 Linkage != GlobalValue::ExternalWeakLinkage &&
Chris Lattnerdf986172009-01-02 07:01:27 +0000712 Linkage != GlobalValue::ExternalLinkage)) {
713 if (ParseGlobalValue(Ty, Init))
714 return true;
715 }
716
Duncan Sands1df98592010-02-16 11:11:14 +0000717 if (Ty->isFunctionTy() || Ty->isLabelTy())
Chris Lattner4a2f1122009-02-08 20:00:15 +0000718 return Error(TyLoc, "invalid type for global variable");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000719
Chris Lattnerdf986172009-01-02 07:01:27 +0000720 GlobalVariable *GV = 0;
721
722 // See if the global was forward referenced, if so, use the global.
Chris Lattner91dad872009-02-02 07:24:28 +0000723 if (!Name.empty()) {
Chris Lattner1d871c52009-10-25 23:22:50 +0000724 if (GlobalValue *GVal = M->getNamedValue(Name)) {
725 if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal))
726 return Error(NameLoc, "redefinition of global '@" + Name + "'");
727 GV = cast<GlobalVariable>(GVal);
728 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000729 } else {
730 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
731 I = ForwardRefValIDs.find(NumberedVals.size());
732 if (I != ForwardRefValIDs.end()) {
733 GV = cast<GlobalVariable>(I->second.first);
734 ForwardRefValIDs.erase(I);
735 }
736 }
737
738 if (GV == 0) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000739 GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, 0,
Owen Andersone9b11b42009-07-08 19:03:57 +0000740 Name, 0, false, AddrSpace);
Chris Lattnerdf986172009-01-02 07:01:27 +0000741 } else {
742 if (GV->getType()->getElementType() != Ty)
743 return Error(TyLoc,
744 "forward reference and definition of global have different types");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000745
Chris Lattnerdf986172009-01-02 07:01:27 +0000746 // Move the forward-reference to the correct spot in the module.
747 M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
748 }
749
750 if (Name.empty())
751 NumberedVals.push_back(GV);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000752
Chris Lattnerdf986172009-01-02 07:01:27 +0000753 // Set the parsed properties on the global.
754 if (Init)
755 GV->setInitializer(Init);
756 GV->setConstant(IsConstant);
757 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
758 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
759 GV->setThreadLocal(ThreadLocal);
Rafael Espindolabea46262011-01-08 16:42:36 +0000760 GV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000761
Chris Lattnerdf986172009-01-02 07:01:27 +0000762 // Parse attributes on the global.
763 while (Lex.getKind() == lltok::comma) {
764 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000765
Chris Lattnerdf986172009-01-02 07:01:27 +0000766 if (Lex.getKind() == lltok::kw_section) {
767 Lex.Lex();
768 GV->setSection(Lex.getStrVal());
769 if (ParseToken(lltok::StringConstant, "expected global section string"))
770 return true;
771 } else if (Lex.getKind() == lltok::kw_align) {
772 unsigned Alignment;
773 if (ParseOptionalAlignment(Alignment)) return true;
774 GV->setAlignment(Alignment);
775 } else {
776 TokError("unknown global variable property!");
777 }
778 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000779
Chris Lattnerdf986172009-01-02 07:01:27 +0000780 return false;
781}
782
783
784//===----------------------------------------------------------------------===//
785// GlobalValue Reference/Resolution Routines.
786//===----------------------------------------------------------------------===//
787
788/// GetGlobalVal - Get a value with the specified name or ID, creating a
789/// forward reference record if needed. This can return null if the value
790/// exists but does not have the right type.
791GlobalValue *LLParser::GetGlobalVal(const std::string &Name, const Type *Ty,
792 LocTy Loc) {
793 const PointerType *PTy = dyn_cast<PointerType>(Ty);
794 if (PTy == 0) {
795 Error(Loc, "global variable reference must have pointer type");
796 return 0;
797 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000798
Chris Lattnerdf986172009-01-02 07:01:27 +0000799 // Look this name up in the normal function symbol table.
800 GlobalValue *Val =
801 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
Daniel Dunbara279bc32009-09-20 02:20:51 +0000802
Chris Lattnerdf986172009-01-02 07:01:27 +0000803 // If this is a forward reference for the value, see if we already created a
804 // forward ref record.
805 if (Val == 0) {
806 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
807 I = ForwardRefVals.find(Name);
808 if (I != ForwardRefVals.end())
809 Val = I->second.first;
810 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000811
Chris Lattnerdf986172009-01-02 07:01:27 +0000812 // If we have the value in the symbol table or fwd-ref table, return it.
813 if (Val) {
814 if (Val->getType() == Ty) return Val;
815 Error(Loc, "'@" + Name + "' defined with type '" +
816 Val->getType()->getDescription() + "'");
817 return 0;
818 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000819
Chris Lattnerdf986172009-01-02 07:01:27 +0000820 // Otherwise, create a new forward reference for this value and remember it.
821 GlobalValue *FwdVal;
Chris Lattner1e407c32009-01-08 19:05:36 +0000822 if (const FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType())) {
823 // Function types can return opaque but functions can't.
Duncan Sands47c51882010-02-16 14:50:09 +0000824 if (FT->getReturnType()->isOpaqueTy()) {
Chris Lattner1e407c32009-01-08 19:05:36 +0000825 Error(Loc, "function may not return opaque type");
826 return 0;
827 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000828
Duncan Sands5f4ee1f2009-03-11 08:08:06 +0000829 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
Chris Lattner1e407c32009-01-08 19:05:36 +0000830 } else {
Owen Andersone9b11b42009-07-08 19:03:57 +0000831 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
832 GlobalValue::ExternalWeakLinkage, 0, Name);
Chris Lattner1e407c32009-01-08 19:05:36 +0000833 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000834
Chris Lattnerdf986172009-01-02 07:01:27 +0000835 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
836 return FwdVal;
837}
838
839GlobalValue *LLParser::GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc) {
840 const PointerType *PTy = dyn_cast<PointerType>(Ty);
841 if (PTy == 0) {
842 Error(Loc, "global variable reference must have pointer type");
843 return 0;
844 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000845
Chris Lattnerdf986172009-01-02 07:01:27 +0000846 GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000847
Chris Lattnerdf986172009-01-02 07:01:27 +0000848 // If this is a forward reference for the value, see if we already created a
849 // forward ref record.
850 if (Val == 0) {
851 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
852 I = ForwardRefValIDs.find(ID);
853 if (I != ForwardRefValIDs.end())
854 Val = I->second.first;
855 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000856
Chris Lattnerdf986172009-01-02 07:01:27 +0000857 // If we have the value in the symbol table or fwd-ref table, return it.
858 if (Val) {
859 if (Val->getType() == Ty) return Val;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000860 Error(Loc, "'@" + Twine(ID) + "' defined with type '" +
Chris Lattnerdf986172009-01-02 07:01:27 +0000861 Val->getType()->getDescription() + "'");
862 return 0;
863 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000864
Chris Lattnerdf986172009-01-02 07:01:27 +0000865 // Otherwise, create a new forward reference for this value and remember it.
866 GlobalValue *FwdVal;
Chris Lattner830703b2009-01-05 18:27:50 +0000867 if (const FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType())) {
868 // Function types can return opaque but functions can't.
Duncan Sands47c51882010-02-16 14:50:09 +0000869 if (FT->getReturnType()->isOpaqueTy()) {
Chris Lattner0d8484f2009-01-05 18:56:52 +0000870 Error(Loc, "function may not return opaque type");
Chris Lattner830703b2009-01-05 18:27:50 +0000871 return 0;
872 }
Duncan Sands5f4ee1f2009-03-11 08:08:06 +0000873 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M);
Chris Lattner830703b2009-01-05 18:27:50 +0000874 } else {
Owen Andersone9b11b42009-07-08 19:03:57 +0000875 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
876 GlobalValue::ExternalWeakLinkage, 0, "");
Chris Lattner830703b2009-01-05 18:27:50 +0000877 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000878
Chris Lattnerdf986172009-01-02 07:01:27 +0000879 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
880 return FwdVal;
881}
882
883
884//===----------------------------------------------------------------------===//
885// Helper Routines.
886//===----------------------------------------------------------------------===//
887
888/// ParseToken - If the current token has the specified kind, eat it and return
889/// success. Otherwise, emit the specified error and return failure.
890bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
891 if (Lex.getKind() != T)
892 return TokError(ErrMsg);
893 Lex.Lex();
894 return false;
895}
896
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000897/// ParseStringConstant
898/// ::= StringConstant
899bool LLParser::ParseStringConstant(std::string &Result) {
900 if (Lex.getKind() != lltok::StringConstant)
901 return TokError("expected string constant");
902 Result = Lex.getStrVal();
903 Lex.Lex();
904 return false;
905}
906
907/// ParseUInt32
908/// ::= uint32
909bool LLParser::ParseUInt32(unsigned &Val) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000910 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
911 return TokError("expected integer");
912 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
913 if (Val64 != unsigned(Val64))
914 return TokError("expected 32-bit integer (too large)");
915 Val = Val64;
916 Lex.Lex();
917 return false;
918}
919
920
921/// ParseOptionalAddrSpace
922/// := /*empty*/
923/// := 'addrspace' '(' uint32 ')'
924bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
925 AddrSpace = 0;
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000926 if (!EatIfPresent(lltok::kw_addrspace))
Chris Lattnerdf986172009-01-02 07:01:27 +0000927 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +0000928 return ParseToken(lltok::lparen, "expected '(' in address space") ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000929 ParseUInt32(AddrSpace) ||
Chris Lattnerdf986172009-01-02 07:01:27 +0000930 ParseToken(lltok::rparen, "expected ')' in address space");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000931}
Chris Lattnerdf986172009-01-02 07:01:27 +0000932
933/// ParseOptionalAttrs - Parse a potentially empty attribute list. AttrKind
934/// indicates what kind of attribute list this is: 0: function arg, 1: result,
935/// 2: function attr.
Chris Lattnerad9ad7c2009-03-25 06:36:36 +0000936/// 3: function arg after value: FIXME: REMOVE IN LLVM 3.0
Chris Lattnerdf986172009-01-02 07:01:27 +0000937bool LLParser::ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind) {
938 Attrs = Attribute::None;
939 LocTy AttrLoc = Lex.getLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000940
Chris Lattnerdf986172009-01-02 07:01:27 +0000941 while (1) {
942 switch (Lex.getKind()) {
943 case lltok::kw_sext:
944 case lltok::kw_zext:
Chris Lattnerad9ad7c2009-03-25 06:36:36 +0000945 // Treat these as signext/zeroext if they occur in the argument list after
946 // the value, as in "call i8 @foo(i8 10 sext)". If they occur before the
947 // value, as in "call i8 @foo(i8 sext (" then it is part of a constant
948 // expr.
Chris Lattnerdf986172009-01-02 07:01:27 +0000949 // FIXME: REMOVE THIS IN LLVM 3.0
Chris Lattnerad9ad7c2009-03-25 06:36:36 +0000950 if (AttrKind == 3) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000951 if (Lex.getKind() == lltok::kw_sext)
952 Attrs |= Attribute::SExt;
953 else
954 Attrs |= Attribute::ZExt;
955 break;
956 }
957 // FALL THROUGH.
958 default: // End of attributes.
959 if (AttrKind != 2 && (Attrs & Attribute::FunctionOnly))
960 return Error(AttrLoc, "invalid use of function-only attribute");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000961
Chris Lattnerad9ad7c2009-03-25 06:36:36 +0000962 if (AttrKind != 0 && AttrKind != 3 && (Attrs & Attribute::ParameterOnly))
Chris Lattnerdf986172009-01-02 07:01:27 +0000963 return Error(AttrLoc, "invalid use of parameter-only attribute");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000964
Chris Lattnerdf986172009-01-02 07:01:27 +0000965 return false;
Devang Patel578efa92009-06-05 21:57:13 +0000966 case lltok::kw_zeroext: Attrs |= Attribute::ZExt; break;
967 case lltok::kw_signext: Attrs |= Attribute::SExt; break;
968 case lltok::kw_inreg: Attrs |= Attribute::InReg; break;
969 case lltok::kw_sret: Attrs |= Attribute::StructRet; break;
970 case lltok::kw_noalias: Attrs |= Attribute::NoAlias; break;
971 case lltok::kw_nocapture: Attrs |= Attribute::NoCapture; break;
972 case lltok::kw_byval: Attrs |= Attribute::ByVal; break;
973 case lltok::kw_nest: Attrs |= Attribute::Nest; break;
Chris Lattnerdf986172009-01-02 07:01:27 +0000974
Devang Patel578efa92009-06-05 21:57:13 +0000975 case lltok::kw_noreturn: Attrs |= Attribute::NoReturn; break;
976 case lltok::kw_nounwind: Attrs |= Attribute::NoUnwind; break;
977 case lltok::kw_noinline: Attrs |= Attribute::NoInline; break;
978 case lltok::kw_readnone: Attrs |= Attribute::ReadNone; break;
979 case lltok::kw_readonly: Attrs |= Attribute::ReadOnly; break;
Jakob Stoklund Olesen570a4a52010-02-06 01:16:28 +0000980 case lltok::kw_inlinehint: Attrs |= Attribute::InlineHint; break;
Devang Patel578efa92009-06-05 21:57:13 +0000981 case lltok::kw_alwaysinline: Attrs |= Attribute::AlwaysInline; break;
982 case lltok::kw_optsize: Attrs |= Attribute::OptimizeForSize; break;
983 case lltok::kw_ssp: Attrs |= Attribute::StackProtect; break;
984 case lltok::kw_sspreq: Attrs |= Attribute::StackProtectReq; break;
985 case lltok::kw_noredzone: Attrs |= Attribute::NoRedZone; break;
986 case lltok::kw_noimplicitfloat: Attrs |= Attribute::NoImplicitFloat; break;
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000987 case lltok::kw_naked: Attrs |= Attribute::Naked; break;
Charles Davis970bfcc2010-10-25 15:37:09 +0000988 case lltok::kw_hotpatch: Attrs |= Attribute::Hotpatch; 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
Owen Andersonaf7ec972009-07-28 21:19:26 +00002080 ID.ConstantVal = ConstantVector::get(Elts.data(), Elts.size());
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:
2287 case lltok::kw_frem: {
Dan Gohman59858cf2009-07-27 16:11:46 +00002288 bool NUW = false;
2289 bool NSW = false;
2290 bool Exact = false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002291 unsigned Opc = Lex.getUIntVal();
2292 Constant *Val0, *Val1;
2293 Lex.Lex();
Dan Gohman59858cf2009-07-27 16:11:46 +00002294 LocTy ModifierLoc = Lex.getLoc();
2295 if (Opc == Instruction::Add ||
2296 Opc == Instruction::Sub ||
2297 Opc == Instruction::Mul) {
2298 if (EatIfPresent(lltok::kw_nuw))
2299 NUW = true;
2300 if (EatIfPresent(lltok::kw_nsw)) {
2301 NSW = true;
2302 if (EatIfPresent(lltok::kw_nuw))
2303 NUW = true;
2304 }
2305 } else if (Opc == Instruction::SDiv) {
2306 if (EatIfPresent(lltok::kw_exact))
2307 Exact = true;
2308 }
Chris Lattnerdf986172009-01-02 07:01:27 +00002309 if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
2310 ParseGlobalTypeAndValue(Val0) ||
2311 ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
2312 ParseGlobalTypeAndValue(Val1) ||
2313 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
2314 return true;
2315 if (Val0->getType() != Val1->getType())
2316 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002317 if (!Val0->getType()->isIntOrIntVectorTy()) {
Dan Gohman59858cf2009-07-27 16:11:46 +00002318 if (NUW)
2319 return Error(ModifierLoc, "nuw only applies to integer operations");
2320 if (NSW)
2321 return Error(ModifierLoc, "nsw only applies to integer operations");
2322 }
Dan Gohman1eaac532010-05-03 22:44:19 +00002323 // Check that the type is valid for the operator.
2324 switch (Opc) {
2325 case Instruction::Add:
2326 case Instruction::Sub:
2327 case Instruction::Mul:
2328 case Instruction::UDiv:
2329 case Instruction::SDiv:
2330 case Instruction::URem:
2331 case Instruction::SRem:
2332 if (!Val0->getType()->isIntOrIntVectorTy())
2333 return Error(ID.Loc, "constexpr requires integer operands");
2334 break;
2335 case Instruction::FAdd:
2336 case Instruction::FSub:
2337 case Instruction::FMul:
2338 case Instruction::FDiv:
2339 case Instruction::FRem:
2340 if (!Val0->getType()->isFPOrFPVectorTy())
2341 return Error(ID.Loc, "constexpr requires fp operands");
2342 break;
2343 default: llvm_unreachable("Unknown binary operator!");
2344 }
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002345 unsigned Flags = 0;
2346 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2347 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
2348 if (Exact) Flags |= SDivOperator::IsExact;
2349 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
Dan Gohman59858cf2009-07-27 16:11:46 +00002350 ID.ConstantVal = C;
Chris Lattnerdf986172009-01-02 07:01:27 +00002351 ID.Kind = ValID::t_Constant;
2352 return false;
2353 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002354
Chris Lattnerdf986172009-01-02 07:01:27 +00002355 // Logical Operations
2356 case lltok::kw_shl:
2357 case lltok::kw_lshr:
2358 case lltok::kw_ashr:
2359 case lltok::kw_and:
2360 case lltok::kw_or:
2361 case lltok::kw_xor: {
2362 unsigned Opc = Lex.getUIntVal();
2363 Constant *Val0, *Val1;
2364 Lex.Lex();
2365 if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
2366 ParseGlobalTypeAndValue(Val0) ||
2367 ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
2368 ParseGlobalTypeAndValue(Val1) ||
2369 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
2370 return true;
2371 if (Val0->getType() != Val1->getType())
2372 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002373 if (!Val0->getType()->isIntOrIntVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002374 return Error(ID.Loc,
2375 "constexpr requires integer or integer vector operands");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002376 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
Chris Lattnerdf986172009-01-02 07:01:27 +00002377 ID.Kind = ValID::t_Constant;
2378 return false;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002379 }
2380
Chris Lattnerdf986172009-01-02 07:01:27 +00002381 case lltok::kw_getelementptr:
2382 case lltok::kw_shufflevector:
2383 case lltok::kw_insertelement:
2384 case lltok::kw_extractelement:
2385 case lltok::kw_select: {
2386 unsigned Opc = Lex.getUIntVal();
2387 SmallVector<Constant*, 16> Elts;
Dan Gohmandd8004d2009-07-27 21:53:46 +00002388 bool InBounds = false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002389 Lex.Lex();
Dan Gohmandd8004d2009-07-27 21:53:46 +00002390 if (Opc == Instruction::GetElementPtr)
Dan Gohmandcb40a32009-07-29 15:58:36 +00002391 InBounds = EatIfPresent(lltok::kw_inbounds);
Chris Lattnerdf986172009-01-02 07:01:27 +00002392 if (ParseToken(lltok::lparen, "expected '(' in constantexpr") ||
2393 ParseGlobalValueVector(Elts) ||
2394 ParseToken(lltok::rparen, "expected ')' in constantexpr"))
2395 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002396
Chris Lattnerdf986172009-01-02 07:01:27 +00002397 if (Opc == Instruction::GetElementPtr) {
Duncan Sands1df98592010-02-16 11:11:14 +00002398 if (Elts.size() == 0 || !Elts[0]->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002399 return Error(ID.Loc, "getelementptr requires pointer operand");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002400
Chris Lattnerdf986172009-01-02 07:01:27 +00002401 if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(),
Eli Friedman4e9bac32009-07-24 21:56:17 +00002402 (Value**)(Elts.data() + 1),
2403 Elts.size() - 1))
Chris Lattnerdf986172009-01-02 07:01:27 +00002404 return Error(ID.Loc, "invalid indices for getelementptr");
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002405 ID.ConstantVal = InBounds ?
2406 ConstantExpr::getInBoundsGetElementPtr(Elts[0],
2407 Elts.data() + 1,
2408 Elts.size() - 1) :
2409 ConstantExpr::getGetElementPtr(Elts[0],
2410 Elts.data() + 1, Elts.size() - 1);
Chris Lattnerdf986172009-01-02 07:01:27 +00002411 } else if (Opc == Instruction::Select) {
2412 if (Elts.size() != 3)
2413 return Error(ID.Loc, "expected three operands to select");
2414 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
2415 Elts[2]))
2416 return Error(ID.Loc, Reason);
Owen Andersonbaf3c402009-07-29 18:55:55 +00002417 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002418 } else if (Opc == Instruction::ShuffleVector) {
2419 if (Elts.size() != 3)
2420 return Error(ID.Loc, "expected three operands to shufflevector");
2421 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2422 return Error(ID.Loc, "invalid operands to shufflevector");
Owen Andersonfba933c2009-07-01 23:57:11 +00002423 ID.ConstantVal =
Owen Andersonbaf3c402009-07-29 18:55:55 +00002424 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002425 } else if (Opc == Instruction::ExtractElement) {
2426 if (Elts.size() != 2)
2427 return Error(ID.Loc, "expected two operands to extractelement");
2428 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
2429 return Error(ID.Loc, "invalid extractelement operands");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002430 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002431 } else {
2432 assert(Opc == Instruction::InsertElement && "Unknown opcode");
2433 if (Elts.size() != 3)
2434 return Error(ID.Loc, "expected three operands to insertelement");
2435 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2436 return Error(ID.Loc, "invalid insertelement operands");
Owen Andersonfba933c2009-07-01 23:57:11 +00002437 ID.ConstantVal =
Owen Andersonbaf3c402009-07-29 18:55:55 +00002438 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002439 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002440
Chris Lattnerdf986172009-01-02 07:01:27 +00002441 ID.Kind = ValID::t_Constant;
2442 return false;
2443 }
2444 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002445
Chris Lattnerdf986172009-01-02 07:01:27 +00002446 Lex.Lex();
2447 return false;
2448}
2449
2450/// ParseGlobalValue - Parse a global value with the specified type.
Victor Hernandez92f238d2010-01-11 22:31:58 +00002451bool LLParser::ParseGlobalValue(const Type *Ty, Constant *&C) {
2452 C = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00002453 ValID ID;
Victor Hernandez92f238d2010-01-11 22:31:58 +00002454 Value *V = NULL;
2455 bool Parsed = ParseValID(ID) ||
2456 ConvertValIDToValue(Ty, ID, V, NULL);
2457 if (V && !(C = dyn_cast<Constant>(V)))
2458 return Error(ID.Loc, "global values must be constants");
2459 return Parsed;
Chris Lattnerdf986172009-01-02 07:01:27 +00002460}
2461
Victor Hernandez92f238d2010-01-11 22:31:58 +00002462bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
2463 PATypeHolder Type(Type::getVoidTy(Context));
2464 return ParseType(Type) ||
2465 ParseGlobalValue(Type, V);
2466}
2467
2468/// ParseGlobalValueVector
2469/// ::= /*empty*/
2470/// ::= TypeAndValue (',' TypeAndValue)*
2471bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts) {
2472 // Empty list.
2473 if (Lex.getKind() == lltok::rbrace ||
2474 Lex.getKind() == lltok::rsquare ||
2475 Lex.getKind() == lltok::greater ||
2476 Lex.getKind() == lltok::rparen)
2477 return false;
2478
2479 Constant *C;
2480 if (ParseGlobalTypeAndValue(C)) return true;
2481 Elts.push_back(C);
2482
2483 while (EatIfPresent(lltok::comma)) {
2484 if (ParseGlobalTypeAndValue(C)) return true;
2485 Elts.push_back(C);
2486 }
2487
2488 return false;
2489}
2490
Dan Gohman309b3af2010-08-24 02:24:03 +00002491bool LLParser::ParseMetadataListValue(ValID &ID, PerFunctionState *PFS) {
2492 assert(Lex.getKind() == lltok::lbrace);
2493 Lex.Lex();
2494
2495 SmallVector<Value*, 16> Elts;
2496 if (ParseMDNodeVector(Elts, PFS) ||
2497 ParseToken(lltok::rbrace, "expected end of metadata node"))
2498 return true;
2499
2500 ID.MDNodeVal = MDNode::get(Context, Elts.data(), Elts.size());
2501 ID.Kind = ValID::t_MDNode;
2502 return false;
2503}
2504
Dan Gohman83448032010-07-14 18:26:50 +00002505/// ParseMetadataValue
2506/// ::= !42
2507/// ::= !{...}
2508/// ::= !"string"
2509bool LLParser::ParseMetadataValue(ValID &ID, PerFunctionState *PFS) {
2510 assert(Lex.getKind() == lltok::exclaim);
2511 Lex.Lex();
2512
2513 // MDNode:
2514 // !{ ... }
Dan Gohman309b3af2010-08-24 02:24:03 +00002515 if (Lex.getKind() == lltok::lbrace)
2516 return ParseMetadataListValue(ID, PFS);
Dan Gohman83448032010-07-14 18:26:50 +00002517
2518 // Standalone metadata reference
2519 // !42
2520 if (Lex.getKind() == lltok::APSInt) {
2521 if (ParseMDNodeID(ID.MDNodeVal)) return true;
2522 ID.Kind = ValID::t_MDNode;
2523 return false;
2524 }
2525
2526 // MDString:
2527 // ::= '!' STRINGCONSTANT
2528 if (ParseMDString(ID.MDStringVal)) return true;
2529 ID.Kind = ValID::t_MDString;
2530 return false;
2531}
2532
Victor Hernandez92f238d2010-01-11 22:31:58 +00002533
2534//===----------------------------------------------------------------------===//
2535// Function Parsing.
2536//===----------------------------------------------------------------------===//
2537
2538bool LLParser::ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
2539 PerFunctionState *PFS) {
Duncan Sands1df98592010-02-16 11:11:14 +00002540 if (Ty->isFunctionTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002541 return Error(ID.Loc, "functions are not values, refer to them as pointers");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002542
Chris Lattnerdf986172009-01-02 07:01:27 +00002543 switch (ID.Kind) {
Daniel Dunbara279bc32009-09-20 02:20:51 +00002544 default: llvm_unreachable("Unknown ValID!");
Chris Lattnerdf986172009-01-02 07:01:27 +00002545 case ValID::t_LocalID:
Victor Hernandez92f238d2010-01-11 22:31:58 +00002546 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2547 V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc);
2548 return (V == 0);
Chris Lattnerdf986172009-01-02 07:01:27 +00002549 case ValID::t_LocalName:
Victor Hernandez92f238d2010-01-11 22:31:58 +00002550 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2551 V = PFS->GetVal(ID.StrVal, Ty, ID.Loc);
2552 return (V == 0);
2553 case ValID::t_InlineAsm: {
2554 const PointerType *PTy = dyn_cast<PointerType>(Ty);
2555 const FunctionType *FTy =
2556 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
2557 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
2558 return Error(ID.Loc, "invalid type for inline asm constraint string");
2559 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1, ID.UIntVal>>1);
2560 return false;
2561 }
2562 case ValID::t_MDNode:
2563 if (!Ty->isMetadataTy())
2564 return Error(ID.Loc, "metadata value must have metadata type");
2565 V = ID.MDNodeVal;
2566 return false;
2567 case ValID::t_MDString:
2568 if (!Ty->isMetadataTy())
2569 return Error(ID.Loc, "metadata value must have metadata type");
2570 V = ID.MDStringVal;
2571 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002572 case ValID::t_GlobalName:
2573 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
2574 return V == 0;
2575 case ValID::t_GlobalID:
2576 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
2577 return V == 0;
2578 case ValID::t_APSInt:
Duncan Sands1df98592010-02-16 11:11:14 +00002579 if (!Ty->isIntegerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002580 return Error(ID.Loc, "integer constant must have integer type");
Jay Foad40f8f622010-12-07 08:25:19 +00002581 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
Owen Andersoneed707b2009-07-24 23:12:02 +00002582 V = ConstantInt::get(Context, ID.APSIntVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00002583 return false;
2584 case ValID::t_APFloat:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002585 if (!Ty->isFloatingPointTy() ||
Chris Lattnerdf986172009-01-02 07:01:27 +00002586 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
2587 return Error(ID.Loc, "floating point constant invalid for type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002588
Chris Lattnerdf986172009-01-02 07:01:27 +00002589 // The lexer has no type info, so builds all float and double FP constants
2590 // as double. Fix this here. Long double does not need this.
2591 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00002592 Ty->isFloatTy()) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002593 bool Ignored;
2594 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
2595 &Ignored);
2596 }
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002597 V = ConstantFP::get(Context, ID.APFloatVal);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002598
Chris Lattner959873d2009-01-05 18:24:23 +00002599 if (V->getType() != Ty)
2600 return Error(ID.Loc, "floating point constant does not have type '" +
2601 Ty->getDescription() + "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002602
Chris Lattnerdf986172009-01-02 07:01:27 +00002603 return false;
2604 case ValID::t_Null:
Duncan Sands1df98592010-02-16 11:11:14 +00002605 if (!Ty->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002606 return Error(ID.Loc, "null must be a pointer type");
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002607 V = ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerdf986172009-01-02 07:01:27 +00002608 return false;
2609 case ValID::t_Undef:
Chris Lattnere67c1aa2009-01-05 08:13:38 +00002610 // FIXME: LabelTy should not be a first-class type.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00002611 if ((!Ty->isFirstClassType() || Ty->isLabelTy()) &&
Duncan Sands47c51882010-02-16 14:50:09 +00002612 !Ty->isOpaqueTy())
Chris Lattnere67c1aa2009-01-05 08:13:38 +00002613 return Error(ID.Loc, "invalid type for undef constant");
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002614 V = UndefValue::get(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00002615 return false;
Chris Lattner081b5052009-01-05 07:52:51 +00002616 case ValID::t_EmptyArray:
Duncan Sands1df98592010-02-16 11:11:14 +00002617 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
Chris Lattner081b5052009-01-05 07:52:51 +00002618 return Error(ID.Loc, "invalid empty array initializer");
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002619 V = UndefValue::get(Ty);
Chris Lattner081b5052009-01-05 07:52:51 +00002620 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002621 case ValID::t_Zero:
Chris Lattnere67c1aa2009-01-05 08:13:38 +00002622 // FIXME: LabelTy should not be a first-class type.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00002623 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002624 return Error(ID.Loc, "invalid type for null constant");
Owen Andersona7235ea2009-07-31 20:28:14 +00002625 V = Constant::getNullValue(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00002626 return false;
2627 case ValID::t_Constant:
Chris Lattner61c70e92010-08-28 04:09:24 +00002628 if (ID.ConstantVal->getType() != Ty)
Chris Lattnerdf986172009-01-02 07:01:27 +00002629 return Error(ID.Loc, "constant expression type mismatch");
Chris Lattnerfdfeb692010-02-12 20:49:41 +00002630
Chris Lattnerdf986172009-01-02 07:01:27 +00002631 V = ID.ConstantVal;
2632 return false;
2633 }
2634}
Daniel Dunbara279bc32009-09-20 02:20:51 +00002635
Chris Lattnerdf986172009-01-02 07:01:27 +00002636bool LLParser::ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS) {
2637 V = 0;
2638 ValID ID;
Victor Hernandezbf170d42010-01-05 22:22:14 +00002639 return ParseValID(ID, &PFS) ||
Victor Hernandez92f238d2010-01-11 22:31:58 +00002640 ConvertValIDToValue(Ty, ID, V, &PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00002641}
2642
2643bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState &PFS) {
Owen Anderson1d0be152009-08-13 21:58:54 +00002644 PATypeHolder T(Type::getVoidTy(Context));
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002645 return ParseType(T) ||
2646 ParseValue(T, V, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00002647}
2648
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002649bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
2650 PerFunctionState &PFS) {
2651 Value *V;
2652 Loc = Lex.getLoc();
2653 if (ParseTypeAndValue(V, PFS)) return true;
2654 if (!isa<BasicBlock>(V))
2655 return Error(Loc, "expected a basic block");
2656 BB = cast<BasicBlock>(V);
2657 return false;
2658}
2659
2660
Chris Lattnerdf986172009-01-02 07:01:27 +00002661/// FunctionHeader
2662/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
Rafael Espindolabea46262011-01-08 16:42:36 +00002663/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
Chris Lattnerdf986172009-01-02 07:01:27 +00002664/// OptionalAlign OptGC
2665bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
2666 // Parse the linkage.
2667 LocTy LinkageLoc = Lex.getLoc();
2668 unsigned Linkage;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002669
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002670 unsigned Visibility, RetAttrs;
Rafael Espindolabea46262011-01-08 16:42:36 +00002671 bool UnnamedAddr;
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002672 CallingConv::ID CC;
Owen Anderson1d0be152009-08-13 21:58:54 +00002673 PATypeHolder RetType(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00002674 LocTy RetTypeLoc = Lex.getLoc();
2675 if (ParseOptionalLinkage(Linkage) ||
2676 ParseOptionalVisibility(Visibility) ||
2677 ParseOptionalCallingConv(CC) ||
2678 ParseOptionalAttrs(RetAttrs, 1) ||
Rafael Espindolabea46262011-01-08 16:42:36 +00002679 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr) ||
Chris Lattnera9a9e072009-03-09 04:49:14 +00002680 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
Chris Lattnerdf986172009-01-02 07:01:27 +00002681 return true;
2682
2683 // Verify that the linkage is ok.
2684 switch ((GlobalValue::LinkageTypes)Linkage) {
2685 case GlobalValue::ExternalLinkage:
2686 break; // always ok.
2687 case GlobalValue::DLLImportLinkage:
Duncan Sands5f4ee1f2009-03-11 08:08:06 +00002688 case GlobalValue::ExternalWeakLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002689 if (isDefine)
2690 return Error(LinkageLoc, "invalid linkage for function definition");
2691 break;
Rafael Espindolabb46f522009-01-15 20:18:42 +00002692 case GlobalValue::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +00002693 case GlobalValue::LinkerPrivateLinkage:
Bill Wendling5e721d72010-07-01 21:55:59 +00002694 case GlobalValue::LinkerPrivateWeakLinkage:
Bill Wendling55ae5152010-08-20 22:05:50 +00002695 case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002696 case GlobalValue::InternalLinkage:
Nick Lewycky55f64db2009-04-13 07:02:02 +00002697 case GlobalValue::AvailableExternallyLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +00002698 case GlobalValue::LinkOnceAnyLinkage:
2699 case GlobalValue::LinkOnceODRLinkage:
2700 case GlobalValue::WeakAnyLinkage:
2701 case GlobalValue::WeakODRLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002702 case GlobalValue::DLLExportLinkage:
2703 if (!isDefine)
2704 return Error(LinkageLoc, "invalid linkage for function declaration");
2705 break;
2706 case GlobalValue::AppendingLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +00002707 case GlobalValue::CommonLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002708 return Error(LinkageLoc, "invalid function linkage type");
2709 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002710
Chris Lattner99bb3152009-01-05 08:00:30 +00002711 if (!FunctionType::isValidReturnType(RetType) ||
Duncan Sands47c51882010-02-16 14:50:09 +00002712 RetType->isOpaqueTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002713 return Error(RetTypeLoc, "invalid function return type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002714
Chris Lattnerdf986172009-01-02 07:01:27 +00002715 LocTy NameLoc = Lex.getLoc();
Chris Lattnerf570e622009-02-18 21:48:13 +00002716
2717 std::string FunctionName;
2718 if (Lex.getKind() == lltok::GlobalVar) {
2719 FunctionName = Lex.getStrVal();
2720 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
2721 unsigned NameID = Lex.getUIntVal();
2722
2723 if (NameID != NumberedVals.size())
2724 return TokError("function expected to be numbered '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +00002725 Twine(NumberedVals.size()) + "'");
Chris Lattnerf570e622009-02-18 21:48:13 +00002726 } else {
2727 return TokError("expected function name");
2728 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002729
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002730 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +00002731
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002732 if (Lex.getKind() != lltok::lparen)
Chris Lattnerdf986172009-01-02 07:01:27 +00002733 return TokError("expected '(' in function argument list");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002734
Chris Lattnerdf986172009-01-02 07:01:27 +00002735 std::vector<ArgInfo> ArgList;
2736 bool isVarArg;
Chris Lattnerdf986172009-01-02 07:01:27 +00002737 unsigned FuncAttrs;
Chris Lattnerdf986172009-01-02 07:01:27 +00002738 std::string Section;
Chris Lattnerdf986172009-01-02 07:01:27 +00002739 unsigned Alignment;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002740 std::string GC;
2741
Chris Lattnerdfd19dd2009-01-05 18:34:07 +00002742 if (ParseArgumentList(ArgList, isVarArg, false) ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002743 ParseOptionalAttrs(FuncAttrs, 2) ||
2744 (EatIfPresent(lltok::kw_section) &&
2745 ParseStringConstant(Section)) ||
2746 ParseOptionalAlignment(Alignment) ||
2747 (EatIfPresent(lltok::kw_gc) &&
2748 ParseStringConstant(GC)))
2749 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00002750
2751 // If the alignment was parsed as an attribute, move to the alignment field.
2752 if (FuncAttrs & Attribute::Alignment) {
2753 Alignment = Attribute::getAlignmentFromAttrs(FuncAttrs);
2754 FuncAttrs &= ~Attribute::Alignment;
2755 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002756
Chris Lattnerdf986172009-01-02 07:01:27 +00002757 // Okay, if we got here, the function is syntactically valid. Convert types
2758 // and do semantic checks.
2759 std::vector<const Type*> ParamTypeList;
2760 SmallVector<AttributeWithIndex, 8> Attrs;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002761 // FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
Chris Lattnerdf986172009-01-02 07:01:27 +00002762 // attributes.
2763 unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg;
2764 if (FuncAttrs & ObsoleteFuncAttrs) {
2765 RetAttrs |= FuncAttrs & ObsoleteFuncAttrs;
2766 FuncAttrs &= ~ObsoleteFuncAttrs;
2767 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002768
Chris Lattnerdf986172009-01-02 07:01:27 +00002769 if (RetAttrs != Attribute::None)
2770 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00002771
Chris Lattnerdf986172009-01-02 07:01:27 +00002772 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
2773 ParamTypeList.push_back(ArgList[i].Type);
2774 if (ArgList[i].Attrs != Attribute::None)
2775 Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs));
2776 }
2777
2778 if (FuncAttrs != Attribute::None)
2779 Attrs.push_back(AttributeWithIndex::get(~0, FuncAttrs));
2780
2781 AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Daniel Dunbara279bc32009-09-20 02:20:51 +00002782
Benjamin Kramerf0127052010-01-05 13:12:22 +00002783 if (PAL.paramHasAttr(1, Attribute::StructRet) && !RetType->isVoidTy())
Daniel Dunbara279bc32009-09-20 02:20:51 +00002784 return Error(RetTypeLoc, "functions with 'sret' argument must return void");
2785
Owen Andersonfba933c2009-07-01 23:57:11 +00002786 const FunctionType *FT =
Owen Andersondebcb012009-07-29 22:17:13 +00002787 FunctionType::get(RetType, ParamTypeList, isVarArg);
2788 const PointerType *PFT = PointerType::getUnqual(FT);
Chris Lattnerdf986172009-01-02 07:01:27 +00002789
2790 Fn = 0;
2791 if (!FunctionName.empty()) {
2792 // If this was a definition of a forward reference, remove the definition
2793 // from the forward reference table and fill in the forward ref.
2794 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI =
2795 ForwardRefVals.find(FunctionName);
2796 if (FRVI != ForwardRefVals.end()) {
2797 Fn = M->getFunction(FunctionName);
Chris Lattnerf1cfb952010-04-20 04:49:11 +00002798 if (Fn->getType() != PFT)
2799 return Error(FRVI->second.second, "invalid forward reference to "
2800 "function '" + FunctionName + "' with wrong type!");
2801
Chris Lattnerdf986172009-01-02 07:01:27 +00002802 ForwardRefVals.erase(FRVI);
2803 } else if ((Fn = M->getFunction(FunctionName))) {
2804 // If this function already exists in the symbol table, then it is
2805 // multiply defined. We accept a few cases for old backwards compat.
2806 // FIXME: Remove this stuff for LLVM 3.0.
2807 if (Fn->getType() != PFT || Fn->getAttributes() != PAL ||
2808 (!Fn->isDeclaration() && isDefine)) {
2809 // If the redefinition has different type or different attributes,
2810 // reject it. If both have bodies, reject it.
2811 return Error(NameLoc, "invalid redefinition of function '" +
2812 FunctionName + "'");
2813 } else if (Fn->isDeclaration()) {
2814 // Make sure to strip off any argument names so we can't get conflicts.
2815 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2816 AI != AE; ++AI)
2817 AI->setName("");
2818 }
Chris Lattner1d871c52009-10-25 23:22:50 +00002819 } else if (M->getNamedValue(FunctionName)) {
2820 return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00002821 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002822
Dan Gohman41905542009-08-29 23:37:49 +00002823 } else {
Chris Lattnerdf986172009-01-02 07:01:27 +00002824 // If this is a definition of a forward referenced function, make sure the
2825 // types agree.
2826 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I
2827 = ForwardRefValIDs.find(NumberedVals.size());
2828 if (I != ForwardRefValIDs.end()) {
2829 Fn = cast<Function>(I->second.first);
2830 if (Fn->getType() != PFT)
2831 return Error(NameLoc, "type of definition and forward reference of '@" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +00002832 Twine(NumberedVals.size()) + "' disagree");
Chris Lattnerdf986172009-01-02 07:01:27 +00002833 ForwardRefValIDs.erase(I);
2834 }
2835 }
2836
2837 if (Fn == 0)
2838 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
2839 else // Move the forward-reference to the correct spot in the module.
2840 M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
2841
2842 if (FunctionName.empty())
2843 NumberedVals.push_back(Fn);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002844
Chris Lattnerdf986172009-01-02 07:01:27 +00002845 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
2846 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
2847 Fn->setCallingConv(CC);
2848 Fn->setAttributes(PAL);
Rafael Espindolabea46262011-01-08 16:42:36 +00002849 Fn->setUnnamedAddr(UnnamedAddr);
Chris Lattnerdf986172009-01-02 07:01:27 +00002850 Fn->setAlignment(Alignment);
2851 Fn->setSection(Section);
2852 if (!GC.empty()) Fn->setGC(GC.c_str());
Daniel Dunbara279bc32009-09-20 02:20:51 +00002853
Chris Lattnerdf986172009-01-02 07:01:27 +00002854 // Add all of the arguments we parsed to the function.
2855 Function::arg_iterator ArgIt = Fn->arg_begin();
2856 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
Chris Lattner5bda3792009-11-26 22:48:23 +00002857 // If we run out of arguments in the Function prototype, exit early.
2858 // FIXME: REMOVE THIS IN LLVM 3.0, this is just for the mismatch case above.
2859 if (ArgIt == Fn->arg_end()) break;
2860
Chris Lattnerdf986172009-01-02 07:01:27 +00002861 // If the argument has a name, insert it into the argument symbol table.
2862 if (ArgList[i].Name.empty()) continue;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002863
Chris Lattnerdf986172009-01-02 07:01:27 +00002864 // Set the name, if it conflicted, it will be auto-renamed.
2865 ArgIt->setName(ArgList[i].Name);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002866
Benjamin Krameraf812352010-10-16 11:28:23 +00002867 if (ArgIt->getName() != ArgList[i].Name)
Chris Lattnerdf986172009-01-02 07:01:27 +00002868 return Error(ArgList[i].Loc, "redefinition of argument '%" +
2869 ArgList[i].Name + "'");
2870 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002871
Chris Lattnerdf986172009-01-02 07:01:27 +00002872 return false;
2873}
2874
2875
2876/// ParseFunctionBody
2877/// ::= '{' BasicBlock+ '}'
2878/// ::= 'begin' BasicBlock+ 'end' // FIXME: remove in LLVM 3.0
2879///
2880bool LLParser::ParseFunctionBody(Function &Fn) {
2881 if (Lex.getKind() != lltok::lbrace && Lex.getKind() != lltok::kw_begin)
2882 return TokError("expected '{' in function body");
2883 Lex.Lex(); // eat the {.
Daniel Dunbara279bc32009-09-20 02:20:51 +00002884
Chris Lattner09d9ef42009-10-28 03:39:23 +00002885 int FunctionNumber = -1;
2886 if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
2887
2888 PerFunctionState PFS(*this, Fn, FunctionNumber);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002889
Chris Lattner2fdf8db2010-01-09 19:20:07 +00002890 // We need at least one basic block.
2891 if (Lex.getKind() == lltok::rbrace || Lex.getKind() == lltok::kw_end)
2892 return TokError("function body requires at least one basic block");
2893
Chris Lattnerdf986172009-01-02 07:01:27 +00002894 while (Lex.getKind() != lltok::rbrace && Lex.getKind() != lltok::kw_end)
2895 if (ParseBasicBlock(PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002896
Chris Lattnerdf986172009-01-02 07:01:27 +00002897 // Eat the }.
2898 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +00002899
Chris Lattnerdf986172009-01-02 07:01:27 +00002900 // Verify function is ok.
Chris Lattner09d9ef42009-10-28 03:39:23 +00002901 return PFS.FinishFunction();
Chris Lattnerdf986172009-01-02 07:01:27 +00002902}
2903
2904/// ParseBasicBlock
2905/// ::= LabelStr? Instruction*
2906bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
2907 // If this basic block starts out with a name, remember it.
2908 std::string Name;
2909 LocTy NameLoc = Lex.getLoc();
2910 if (Lex.getKind() == lltok::LabelStr) {
2911 Name = Lex.getStrVal();
2912 Lex.Lex();
2913 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002914
Chris Lattnerdf986172009-01-02 07:01:27 +00002915 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
2916 if (BB == 0) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002917
Chris Lattnerdf986172009-01-02 07:01:27 +00002918 std::string NameStr;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002919
Chris Lattnerdf986172009-01-02 07:01:27 +00002920 // Parse the instructions in this block until we get a terminator.
2921 Instruction *Inst;
Chris Lattner1340dd32009-12-30 05:48:36 +00002922 SmallVector<std::pair<unsigned, MDNode *>, 4> MetadataOnInst;
Chris Lattnerdf986172009-01-02 07:01:27 +00002923 do {
2924 // This instruction may have three possibilities for a name: a) none
2925 // specified, b) name specified "%foo =", c) number specified: "%4 =".
2926 LocTy NameLoc = Lex.getLoc();
2927 int NameID = -1;
2928 NameStr = "";
Daniel Dunbara279bc32009-09-20 02:20:51 +00002929
Chris Lattnerdf986172009-01-02 07:01:27 +00002930 if (Lex.getKind() == lltok::LocalVarID) {
2931 NameID = Lex.getUIntVal();
2932 Lex.Lex();
2933 if (ParseToken(lltok::equal, "expected '=' after instruction id"))
2934 return true;
2935 } else if (Lex.getKind() == lltok::LocalVar ||
2936 // FIXME: REMOVE IN LLVM 3.0
2937 Lex.getKind() == lltok::StringConstant) {
2938 NameStr = Lex.getStrVal();
2939 Lex.Lex();
2940 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
2941 return true;
2942 }
Devang Patelf633a062009-09-17 23:04:48 +00002943
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002944 switch (ParseInstruction(Inst, BB, PFS)) {
2945 default: assert(0 && "Unknown ParseInstruction result!");
2946 case InstError: return true;
2947 case InstNormal:
Chris Lattner4ba9d9b2010-04-07 04:08:57 +00002948 BB->getInstList().push_back(Inst);
2949
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002950 // With a normal result, we check to see if the instruction is followed by
2951 // a comma and metadata.
2952 if (EatIfPresent(lltok::comma))
Dan Gohman9d072f52010-08-24 02:05:17 +00002953 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002954 return true;
2955 break;
2956 case InstExtraComma:
Chris Lattner4ba9d9b2010-04-07 04:08:57 +00002957 BB->getInstList().push_back(Inst);
2958
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002959 // If the instruction parser ate an extra comma at the end of it, it
2960 // *must* be followed by metadata.
Dan Gohman9d072f52010-08-24 02:05:17 +00002961 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002962 return true;
2963 break;
2964 }
Devang Patelf633a062009-09-17 23:04:48 +00002965
Chris Lattnerdf986172009-01-02 07:01:27 +00002966 // Set the name on the instruction.
2967 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
2968 } while (!isa<TerminatorInst>(Inst));
Daniel Dunbara279bc32009-09-20 02:20:51 +00002969
Chris Lattnerdf986172009-01-02 07:01:27 +00002970 return false;
2971}
2972
2973//===----------------------------------------------------------------------===//
2974// Instruction Parsing.
2975//===----------------------------------------------------------------------===//
2976
2977/// ParseInstruction - Parse one of the many different instructions.
2978///
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002979int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
2980 PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002981 lltok::Kind Token = Lex.getKind();
2982 if (Token == lltok::Eof)
2983 return TokError("found end of file when expecting more instructions");
2984 LocTy Loc = Lex.getLoc();
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00002985 unsigned KeywordVal = Lex.getUIntVal();
Chris Lattnerdf986172009-01-02 07:01:27 +00002986 Lex.Lex(); // Eat the keyword.
Daniel Dunbara279bc32009-09-20 02:20:51 +00002987
Chris Lattnerdf986172009-01-02 07:01:27 +00002988 switch (Token) {
2989 default: return Error(Loc, "expected instruction opcode");
2990 // Terminator Instructions.
Owen Anderson1d0be152009-08-13 21:58:54 +00002991 case lltok::kw_unwind: Inst = new UnwindInst(Context); return false;
2992 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002993 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
2994 case lltok::kw_br: return ParseBr(Inst, PFS);
2995 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
Chris Lattnerab21db72009-10-28 00:19:10 +00002996 case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00002997 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
2998 // Binary Operators.
2999 case lltok::kw_add:
3000 case lltok::kw_sub:
Dan Gohman59858cf2009-07-27 16:11:46 +00003001 case lltok::kw_mul: {
3002 bool NUW = false;
3003 bool NSW = false;
3004 LocTy ModifierLoc = Lex.getLoc();
3005 if (EatIfPresent(lltok::kw_nuw))
3006 NUW = true;
3007 if (EatIfPresent(lltok::kw_nsw)) {
3008 NSW = true;
3009 if (EatIfPresent(lltok::kw_nuw))
3010 NUW = true;
3011 }
Dan Gohman1eaac532010-05-03 22:44:19 +00003012 bool Result = ParseArithmetic(Inst, PFS, KeywordVal, 1);
Dan Gohman59858cf2009-07-27 16:11:46 +00003013 if (!Result) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003014 if (!Inst->getType()->isIntOrIntVectorTy()) {
Dan Gohman59858cf2009-07-27 16:11:46 +00003015 if (NUW)
3016 return Error(ModifierLoc, "nuw only applies to integer operations");
3017 if (NSW)
3018 return Error(ModifierLoc, "nsw only applies to integer operations");
3019 }
3020 if (NUW)
Dan Gohmanf8dbee72009-09-07 23:54:19 +00003021 cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
Dan Gohman59858cf2009-07-27 16:11:46 +00003022 if (NSW)
Dan Gohmanf8dbee72009-09-07 23:54:19 +00003023 cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
Dan Gohman59858cf2009-07-27 16:11:46 +00003024 }
3025 return Result;
3026 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00003027 case lltok::kw_fadd:
3028 case lltok::kw_fsub:
3029 case lltok::kw_fmul: return ParseArithmetic(Inst, PFS, KeywordVal, 2);
3030
Dan Gohman59858cf2009-07-27 16:11:46 +00003031 case lltok::kw_sdiv: {
3032 bool Exact = false;
3033 if (EatIfPresent(lltok::kw_exact))
3034 Exact = true;
3035 bool Result = ParseArithmetic(Inst, PFS, KeywordVal, 1);
3036 if (!Result)
3037 if (Exact)
Dan Gohmanf8dbee72009-09-07 23:54:19 +00003038 cast<BinaryOperator>(Inst)->setIsExact(true);
Dan Gohman59858cf2009-07-27 16:11:46 +00003039 return Result;
3040 }
3041
Chris Lattnerdf986172009-01-02 07:01:27 +00003042 case lltok::kw_udiv:
Chris Lattnerdf986172009-01-02 07:01:27 +00003043 case lltok::kw_urem:
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003044 case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1);
Chris Lattnere914b592009-01-05 08:24:46 +00003045 case lltok::kw_fdiv:
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003046 case lltok::kw_frem: return ParseArithmetic(Inst, PFS, KeywordVal, 2);
Chris Lattnerdf986172009-01-02 07:01:27 +00003047 case lltok::kw_shl:
3048 case lltok::kw_lshr:
3049 case lltok::kw_ashr:
3050 case lltok::kw_and:
3051 case lltok::kw_or:
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003052 case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00003053 case lltok::kw_icmp:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003054 case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00003055 // Casts.
3056 case lltok::kw_trunc:
3057 case lltok::kw_zext:
3058 case lltok::kw_sext:
3059 case lltok::kw_fptrunc:
3060 case lltok::kw_fpext:
3061 case lltok::kw_bitcast:
3062 case lltok::kw_uitofp:
3063 case lltok::kw_sitofp:
3064 case lltok::kw_fptoui:
Daniel Dunbara279bc32009-09-20 02:20:51 +00003065 case lltok::kw_fptosi:
Chris Lattnerdf986172009-01-02 07:01:27 +00003066 case lltok::kw_inttoptr:
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003067 case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00003068 // Other.
3069 case lltok::kw_select: return ParseSelect(Inst, PFS);
Chris Lattner0088a5c2009-01-05 08:18:44 +00003070 case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00003071 case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
3072 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
3073 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
3074 case lltok::kw_phi: return ParsePHI(Inst, PFS);
3075 case lltok::kw_call: return ParseCall(Inst, PFS, false);
3076 case lltok::kw_tail: return ParseCall(Inst, PFS, true);
3077 // Memory.
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00003078 case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
3079 case lltok::kw_malloc: return ParseAlloc(Inst, PFS, BB, false);
Victor Hernandez66284e02009-10-24 04:23:03 +00003080 case lltok::kw_free: return ParseFree(Inst, PFS, BB);
Chris Lattnerdf986172009-01-02 07:01:27 +00003081 case lltok::kw_load: return ParseLoad(Inst, PFS, false);
3082 case lltok::kw_store: return ParseStore(Inst, PFS, false);
3083 case lltok::kw_volatile:
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003084 if (EatIfPresent(lltok::kw_load))
Chris Lattnerdf986172009-01-02 07:01:27 +00003085 return ParseLoad(Inst, PFS, true);
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003086 else if (EatIfPresent(lltok::kw_store))
Chris Lattnerdf986172009-01-02 07:01:27 +00003087 return ParseStore(Inst, PFS, true);
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003088 else
Chris Lattnerdf986172009-01-02 07:01:27 +00003089 return TokError("expected 'load' or 'store'");
Chris Lattnerdf986172009-01-02 07:01:27 +00003090 case lltok::kw_getresult: return ParseGetResult(Inst, PFS);
3091 case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
3092 case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS);
3093 case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS);
3094 }
3095}
3096
3097/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
3098bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003099 if (Opc == Instruction::FCmp) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003100 switch (Lex.getKind()) {
3101 default: TokError("expected fcmp predicate (e.g. 'oeq')");
3102 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
3103 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
3104 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
3105 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
3106 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
3107 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
3108 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
3109 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
3110 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
3111 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
3112 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
3113 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
3114 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
3115 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
3116 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
3117 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
3118 }
3119 } else {
3120 switch (Lex.getKind()) {
3121 default: TokError("expected icmp predicate (e.g. 'eq')");
3122 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
3123 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
3124 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
3125 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
3126 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
3127 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
3128 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
3129 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
3130 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
3131 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
3132 }
3133 }
3134 Lex.Lex();
3135 return false;
3136}
3137
3138//===----------------------------------------------------------------------===//
3139// Terminator Instructions.
3140//===----------------------------------------------------------------------===//
3141
3142/// ParseRet - Parse a return instruction.
Chris Lattner3f3a0f62009-12-29 21:25:40 +00003143/// ::= 'ret' void (',' !dbg, !1)*
3144/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
3145/// ::= 'ret' TypeAndValue (',' TypeAndValue)+ (',' !dbg, !1)*
Devang Patelf633a062009-09-17 23:04:48 +00003146/// [[obsolete: LLVM 3.0]]
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003147int LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
3148 PerFunctionState &PFS) {
Owen Anderson1d0be152009-08-13 21:58:54 +00003149 PATypeHolder Ty(Type::getVoidTy(Context));
Chris Lattnera9a9e072009-03-09 04:49:14 +00003150 if (ParseType(Ty, true /*void allowed*/)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003151
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00003152 if (Ty->isVoidTy()) {
Owen Anderson1d0be152009-08-13 21:58:54 +00003153 Inst = ReturnInst::Create(Context);
Chris Lattnerdf986172009-01-02 07:01:27 +00003154 return false;
3155 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003156
Chris Lattnerdf986172009-01-02 07:01:27 +00003157 Value *RV;
3158 if (ParseValue(Ty, RV, PFS)) return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003159
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003160 bool ExtraComma = false;
Devang Patelf633a062009-09-17 23:04:48 +00003161 if (EatIfPresent(lltok::comma)) {
Devang Patel0475c912009-09-29 00:01:14 +00003162 // Parse optional custom metadata, e.g. !dbg
Chris Lattner1d928312009-12-30 05:02:06 +00003163 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003164 ExtraComma = true;
Devang Patelf633a062009-09-17 23:04:48 +00003165 } else {
3166 // The normal case is one return value.
Chris Lattner3f3a0f62009-12-29 21:25:40 +00003167 // FIXME: LLVM 3.0 remove MRV support for 'ret i32 1, i32 2', requiring
3168 // use of 'ret {i32,i32} {i32 1, i32 2}'
Devang Patelf633a062009-09-17 23:04:48 +00003169 SmallVector<Value*, 8> RVs;
3170 RVs.push_back(RV);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003171
Devang Patelf633a062009-09-17 23:04:48 +00003172 do {
Devang Patel0475c912009-09-29 00:01:14 +00003173 // If optional custom metadata, e.g. !dbg is seen then this is the
3174 // end of MRV.
Chris Lattner1d928312009-12-30 05:02:06 +00003175 if (Lex.getKind() == lltok::MetadataVar)
Daniel Dunbara279bc32009-09-20 02:20:51 +00003176 break;
3177 if (ParseTypeAndValue(RV, PFS)) return true;
3178 RVs.push_back(RV);
Devang Patelf633a062009-09-17 23:04:48 +00003179 } while (EatIfPresent(lltok::comma));
3180
3181 RV = UndefValue::get(PFS.getFunction().getReturnType());
3182 for (unsigned i = 0, e = RVs.size(); i != e; ++i) {
Daniel Dunbara279bc32009-09-20 02:20:51 +00003183 Instruction *I = InsertValueInst::Create(RV, RVs[i], i, "mrv");
3184 BB->getInstList().push_back(I);
3185 RV = I;
Devang Patelf633a062009-09-17 23:04:48 +00003186 }
Chris Lattnerdf986172009-01-02 07:01:27 +00003187 }
3188 }
Devang Patelf633a062009-09-17 23:04:48 +00003189
Owen Anderson1d0be152009-08-13 21:58:54 +00003190 Inst = ReturnInst::Create(Context, RV);
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003191 return ExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003192}
3193
3194
3195/// ParseBr
3196/// ::= 'br' TypeAndValue
3197/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3198bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
3199 LocTy Loc, Loc2;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003200 Value *Op0;
3201 BasicBlock *Op1, *Op2;
Chris Lattnerdf986172009-01-02 07:01:27 +00003202 if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003203
Chris Lattnerdf986172009-01-02 07:01:27 +00003204 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
3205 Inst = BranchInst::Create(BB);
3206 return false;
3207 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003208
Owen Anderson1d0be152009-08-13 21:58:54 +00003209 if (Op0->getType() != Type::getInt1Ty(Context))
Chris Lattnerdf986172009-01-02 07:01:27 +00003210 return Error(Loc, "branch condition must have 'i1' type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003211
Chris Lattnerdf986172009-01-02 07:01:27 +00003212 if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003213 ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003214 ParseToken(lltok::comma, "expected ',' after true destination") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003215 ParseTypeAndBasicBlock(Op2, Loc2, PFS))
Chris Lattnerdf986172009-01-02 07:01:27 +00003216 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003217
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003218 Inst = BranchInst::Create(Op1, Op2, Op0);
Chris Lattnerdf986172009-01-02 07:01:27 +00003219 return false;
3220}
3221
3222/// ParseSwitch
3223/// Instruction
3224/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
3225/// JumpTable
3226/// ::= (TypeAndValue ',' TypeAndValue)*
3227bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
3228 LocTy CondLoc, BBLoc;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003229 Value *Cond;
3230 BasicBlock *DefaultBB;
Chris Lattnerdf986172009-01-02 07:01:27 +00003231 if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
3232 ParseToken(lltok::comma, "expected ',' after switch condition") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003233 ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003234 ParseToken(lltok::lsquare, "expected '[' with switch table"))
3235 return true;
3236
Duncan Sands1df98592010-02-16 11:11:14 +00003237 if (!Cond->getType()->isIntegerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003238 return Error(CondLoc, "switch condition must have integer type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003239
Chris Lattnerdf986172009-01-02 07:01:27 +00003240 // Parse the jump table pairs.
3241 SmallPtrSet<Value*, 32> SeenCases;
3242 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
3243 while (Lex.getKind() != lltok::rsquare) {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003244 Value *Constant;
3245 BasicBlock *DestBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003246
Chris Lattnerdf986172009-01-02 07:01:27 +00003247 if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
3248 ParseToken(lltok::comma, "expected ',' after case value") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003249 ParseTypeAndBasicBlock(DestBB, PFS))
Chris Lattnerdf986172009-01-02 07:01:27 +00003250 return true;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003251
Chris Lattnerdf986172009-01-02 07:01:27 +00003252 if (!SeenCases.insert(Constant))
3253 return Error(CondLoc, "duplicate case value in switch");
3254 if (!isa<ConstantInt>(Constant))
3255 return Error(CondLoc, "case value is not a constant integer");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003256
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003257 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
Chris Lattnerdf986172009-01-02 07:01:27 +00003258 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003259
Chris Lattnerdf986172009-01-02 07:01:27 +00003260 Lex.Lex(); // Eat the ']'.
Daniel Dunbara279bc32009-09-20 02:20:51 +00003261
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003262 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
Chris Lattnerdf986172009-01-02 07:01:27 +00003263 for (unsigned i = 0, e = Table.size(); i != e; ++i)
3264 SI->addCase(Table[i].first, Table[i].second);
3265 Inst = SI;
3266 return false;
3267}
3268
Chris Lattnerab21db72009-10-28 00:19:10 +00003269/// ParseIndirectBr
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003270/// Instruction
Chris Lattnerab21db72009-10-28 00:19:10 +00003271/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
3272bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003273 LocTy AddrLoc;
3274 Value *Address;
3275 if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
Chris Lattnerab21db72009-10-28 00:19:10 +00003276 ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
3277 ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003278 return true;
3279
Duncan Sands1df98592010-02-16 11:11:14 +00003280 if (!Address->getType()->isPointerTy())
Chris Lattnerab21db72009-10-28 00:19:10 +00003281 return Error(AddrLoc, "indirectbr address must have pointer type");
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003282
3283 // Parse the destination list.
3284 SmallVector<BasicBlock*, 16> DestList;
3285
3286 if (Lex.getKind() != lltok::rsquare) {
3287 BasicBlock *DestBB;
3288 if (ParseTypeAndBasicBlock(DestBB, PFS))
3289 return true;
3290 DestList.push_back(DestBB);
3291
3292 while (EatIfPresent(lltok::comma)) {
3293 if (ParseTypeAndBasicBlock(DestBB, PFS))
3294 return true;
3295 DestList.push_back(DestBB);
3296 }
3297 }
3298
3299 if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
3300 return true;
3301
Chris Lattnerab21db72009-10-28 00:19:10 +00003302 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003303 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
3304 IBI->addDestination(DestList[i]);
3305 Inst = IBI;
3306 return false;
3307}
3308
3309
Chris Lattnerdf986172009-01-02 07:01:27 +00003310/// ParseInvoke
3311/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
3312/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
3313bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
3314 LocTy CallLoc = Lex.getLoc();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003315 unsigned RetAttrs, FnAttrs;
3316 CallingConv::ID CC;
Owen Anderson1d0be152009-08-13 21:58:54 +00003317 PATypeHolder RetType(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00003318 LocTy RetTypeLoc;
3319 ValID CalleeID;
3320 SmallVector<ParamInfo, 16> ArgList;
3321
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003322 BasicBlock *NormalBB, *UnwindBB;
Chris Lattnerdf986172009-01-02 07:01:27 +00003323 if (ParseOptionalCallingConv(CC) ||
3324 ParseOptionalAttrs(RetAttrs, 1) ||
Chris Lattnera9a9e072009-03-09 04:49:14 +00003325 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003326 ParseValID(CalleeID) ||
3327 ParseParameterList(ArgList, PFS) ||
3328 ParseOptionalAttrs(FnAttrs, 2) ||
3329 ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003330 ParseTypeAndBasicBlock(NormalBB, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003331 ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003332 ParseTypeAndBasicBlock(UnwindBB, PFS))
Chris Lattnerdf986172009-01-02 07:01:27 +00003333 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003334
Chris Lattnerdf986172009-01-02 07:01:27 +00003335 // If RetType is a non-function pointer type, then this is the short syntax
3336 // for the call, which means that RetType is just the return type. Infer the
3337 // rest of the function argument types from the arguments that are present.
3338 const PointerType *PFTy = 0;
3339 const FunctionType *Ty = 0;
3340 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3341 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3342 // Pull out the types of all of the arguments...
3343 std::vector<const Type*> ParamTypes;
3344 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3345 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +00003346
Chris Lattnerdf986172009-01-02 07:01:27 +00003347 if (!FunctionType::isValidReturnType(RetType))
3348 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003349
Owen Andersondebcb012009-07-29 22:17:13 +00003350 Ty = FunctionType::get(RetType, ParamTypes, false);
3351 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00003352 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003353
Chris Lattnerdf986172009-01-02 07:01:27 +00003354 // Look up the callee.
3355 Value *Callee;
Victor Hernandez92f238d2010-01-11 22:31:58 +00003356 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003357
Chris Lattnerdf986172009-01-02 07:01:27 +00003358 // FIXME: In LLVM 3.0, stop accepting zext, sext and inreg as optional
3359 // function attributes.
3360 unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg;
3361 if (FnAttrs & ObsoleteFuncAttrs) {
3362 RetAttrs |= FnAttrs & ObsoleteFuncAttrs;
3363 FnAttrs &= ~ObsoleteFuncAttrs;
3364 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003365
Chris Lattnerdf986172009-01-02 07:01:27 +00003366 // Set up the Attributes for the function.
3367 SmallVector<AttributeWithIndex, 8> Attrs;
3368 if (RetAttrs != Attribute::None)
3369 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003370
Chris Lattnerdf986172009-01-02 07:01:27 +00003371 SmallVector<Value*, 8> Args;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003372
Chris Lattnerdf986172009-01-02 07:01:27 +00003373 // Loop through FunctionType's arguments and ensure they are specified
3374 // correctly. Also, gather any parameter attributes.
3375 FunctionType::param_iterator I = Ty->param_begin();
3376 FunctionType::param_iterator E = Ty->param_end();
3377 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
3378 const Type *ExpectedTy = 0;
3379 if (I != E) {
3380 ExpectedTy = *I++;
3381 } else if (!Ty->isVarArg()) {
3382 return Error(ArgList[i].Loc, "too many arguments specified");
3383 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003384
Chris Lattnerdf986172009-01-02 07:01:27 +00003385 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3386 return Error(ArgList[i].Loc, "argument is not of expected type '" +
3387 ExpectedTy->getDescription() + "'");
3388 Args.push_back(ArgList[i].V);
3389 if (ArgList[i].Attrs != Attribute::None)
3390 Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs));
3391 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003392
Chris Lattnerdf986172009-01-02 07:01:27 +00003393 if (I != E)
3394 return Error(CallLoc, "not enough parameters specified for call");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003395
Chris Lattnerdf986172009-01-02 07:01:27 +00003396 if (FnAttrs != Attribute::None)
3397 Attrs.push_back(AttributeWithIndex::get(~0, FnAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003398
Chris Lattnerdf986172009-01-02 07:01:27 +00003399 // Finish off the Attributes and check them
3400 AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Daniel Dunbara279bc32009-09-20 02:20:51 +00003401
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003402 InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB,
Chris Lattnerdf986172009-01-02 07:01:27 +00003403 Args.begin(), Args.end());
3404 II->setCallingConv(CC);
3405 II->setAttributes(PAL);
3406 Inst = II;
3407 return false;
3408}
3409
3410
3411
3412//===----------------------------------------------------------------------===//
3413// Binary Operators.
3414//===----------------------------------------------------------------------===//
3415
3416/// ParseArithmetic
Chris Lattnere914b592009-01-05 08:24:46 +00003417/// ::= ArithmeticOps TypeAndValue ',' Value
3418///
3419/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1,
3420/// then any integer operand is allowed, if it is 2, any fp operand is allowed.
Chris Lattnerdf986172009-01-02 07:01:27 +00003421bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
Chris Lattnere914b592009-01-05 08:24:46 +00003422 unsigned Opc, unsigned OperandType) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003423 LocTy Loc; Value *LHS, *RHS;
3424 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3425 ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
3426 ParseValue(LHS->getType(), RHS, PFS))
3427 return true;
3428
Chris Lattnere914b592009-01-05 08:24:46 +00003429 bool Valid;
3430 switch (OperandType) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003431 default: llvm_unreachable("Unknown operand type!");
Chris Lattnere914b592009-01-05 08:24:46 +00003432 case 0: // int or FP.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003433 Valid = LHS->getType()->isIntOrIntVectorTy() ||
3434 LHS->getType()->isFPOrFPVectorTy();
Chris Lattnere914b592009-01-05 08:24:46 +00003435 break;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003436 case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
3437 case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
Chris Lattnere914b592009-01-05 08:24:46 +00003438 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003439
Chris Lattnere914b592009-01-05 08:24:46 +00003440 if (!Valid)
3441 return Error(Loc, "invalid operand type for instruction");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003442
Chris Lattnerdf986172009-01-02 07:01:27 +00003443 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3444 return false;
3445}
3446
3447/// ParseLogical
3448/// ::= ArithmeticOps TypeAndValue ',' Value {
3449bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
3450 unsigned Opc) {
3451 LocTy Loc; Value *LHS, *RHS;
3452 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3453 ParseToken(lltok::comma, "expected ',' in logical operation") ||
3454 ParseValue(LHS->getType(), RHS, PFS))
3455 return true;
3456
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003457 if (!LHS->getType()->isIntOrIntVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003458 return Error(Loc,"instruction requires integer or integer vector operands");
3459
3460 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3461 return false;
3462}
3463
3464
3465/// ParseCompare
3466/// ::= 'icmp' IPredicates TypeAndValue ',' Value
3467/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
Chris Lattnerdf986172009-01-02 07:01:27 +00003468bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
3469 unsigned Opc) {
3470 // Parse the integer/fp comparison predicate.
3471 LocTy Loc;
3472 unsigned Pred;
3473 Value *LHS, *RHS;
3474 if (ParseCmpPredicate(Pred, Opc) ||
3475 ParseTypeAndValue(LHS, Loc, PFS) ||
3476 ParseToken(lltok::comma, "expected ',' after compare value") ||
3477 ParseValue(LHS->getType(), RHS, PFS))
3478 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003479
Chris Lattnerdf986172009-01-02 07:01:27 +00003480 if (Opc == Instruction::FCmp) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003481 if (!LHS->getType()->isFPOrFPVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003482 return Error(Loc, "fcmp requires floating point operands");
Dan Gohman1c8a23c2009-08-25 23:17:54 +00003483 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003484 } else {
3485 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003486 if (!LHS->getType()->isIntOrIntVectorTy() &&
Duncan Sands1df98592010-02-16 11:11:14 +00003487 !LHS->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003488 return Error(Loc, "icmp requires integer operands");
Dan Gohman1c8a23c2009-08-25 23:17:54 +00003489 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Chris Lattnerdf986172009-01-02 07:01:27 +00003490 }
3491 return false;
3492}
3493
3494//===----------------------------------------------------------------------===//
3495// Other Instructions.
3496//===----------------------------------------------------------------------===//
3497
3498
3499/// ParseCast
3500/// ::= CastOpc TypeAndValue 'to' Type
3501bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
3502 unsigned Opc) {
3503 LocTy Loc; Value *Op;
Owen Anderson1d0be152009-08-13 21:58:54 +00003504 PATypeHolder DestTy(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00003505 if (ParseTypeAndValue(Op, Loc, PFS) ||
3506 ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
3507 ParseType(DestTy))
3508 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003509
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003510 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
3511 CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
Chris Lattnerdf986172009-01-02 07:01:27 +00003512 return Error(Loc, "invalid cast opcode for cast from '" +
3513 Op->getType()->getDescription() + "' to '" +
3514 DestTy->getDescription() + "'");
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003515 }
Chris Lattnerdf986172009-01-02 07:01:27 +00003516 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
3517 return false;
3518}
3519
3520/// ParseSelect
3521/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3522bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
3523 LocTy Loc;
3524 Value *Op0, *Op1, *Op2;
3525 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3526 ParseToken(lltok::comma, "expected ',' after select condition") ||
3527 ParseTypeAndValue(Op1, PFS) ||
3528 ParseToken(lltok::comma, "expected ',' after select value") ||
3529 ParseTypeAndValue(Op2, PFS))
3530 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003531
Chris Lattnerdf986172009-01-02 07:01:27 +00003532 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
3533 return Error(Loc, Reason);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003534
Chris Lattnerdf986172009-01-02 07:01:27 +00003535 Inst = SelectInst::Create(Op0, Op1, Op2);
3536 return false;
3537}
3538
Chris Lattner0088a5c2009-01-05 08:18:44 +00003539/// ParseVA_Arg
3540/// ::= 'va_arg' TypeAndValue ',' Type
3541bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003542 Value *Op;
Owen Anderson1d0be152009-08-13 21:58:54 +00003543 PATypeHolder EltTy(Type::getVoidTy(Context));
Chris Lattner0088a5c2009-01-05 08:18:44 +00003544 LocTy TypeLoc;
Chris Lattnerdf986172009-01-02 07:01:27 +00003545 if (ParseTypeAndValue(Op, PFS) ||
3546 ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
Chris Lattner0088a5c2009-01-05 08:18:44 +00003547 ParseType(EltTy, TypeLoc))
Chris Lattnerdf986172009-01-02 07:01:27 +00003548 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003549
Chris Lattner0088a5c2009-01-05 08:18:44 +00003550 if (!EltTy->isFirstClassType())
3551 return Error(TypeLoc, "va_arg requires operand with first class type");
Chris Lattnerdf986172009-01-02 07:01:27 +00003552
3553 Inst = new VAArgInst(Op, EltTy);
3554 return false;
3555}
3556
3557/// ParseExtractElement
3558/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
3559bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
3560 LocTy Loc;
3561 Value *Op0, *Op1;
3562 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3563 ParseToken(lltok::comma, "expected ',' after extract value") ||
3564 ParseTypeAndValue(Op1, PFS))
3565 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003566
Chris Lattnerdf986172009-01-02 07:01:27 +00003567 if (!ExtractElementInst::isValidOperands(Op0, Op1))
3568 return Error(Loc, "invalid extractelement operands");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003569
Eric Christophera3500da2009-07-25 02:28:41 +00003570 Inst = ExtractElementInst::Create(Op0, Op1);
Chris Lattnerdf986172009-01-02 07:01:27 +00003571 return false;
3572}
3573
3574/// ParseInsertElement
3575/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3576bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
3577 LocTy Loc;
3578 Value *Op0, *Op1, *Op2;
3579 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3580 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3581 ParseTypeAndValue(Op1, PFS) ||
3582 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3583 ParseTypeAndValue(Op2, PFS))
3584 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003585
Chris Lattnerdf986172009-01-02 07:01:27 +00003586 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
Eric Christopher0aaf4e92009-07-23 01:01:32 +00003587 return Error(Loc, "invalid insertelement operands");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003588
Chris Lattnerdf986172009-01-02 07:01:27 +00003589 Inst = InsertElementInst::Create(Op0, Op1, Op2);
3590 return false;
3591}
3592
3593/// ParseShuffleVector
3594/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3595bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
3596 LocTy Loc;
3597 Value *Op0, *Op1, *Op2;
3598 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3599 ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
3600 ParseTypeAndValue(Op1, PFS) ||
3601 ParseToken(lltok::comma, "expected ',' after shuffle value") ||
3602 ParseTypeAndValue(Op2, PFS))
3603 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003604
Chris Lattnerdf986172009-01-02 07:01:27 +00003605 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
3606 return Error(Loc, "invalid extractelement operands");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003607
Chris Lattnerdf986172009-01-02 07:01:27 +00003608 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
3609 return false;
3610}
3611
3612/// ParsePHI
Chris Lattnerc6e20092009-10-18 05:27:44 +00003613/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003614int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
Owen Anderson1d0be152009-08-13 21:58:54 +00003615 PATypeHolder Ty(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00003616 Value *Op0, *Op1;
3617 LocTy TypeLoc = Lex.getLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00003618
Chris Lattnerdf986172009-01-02 07:01:27 +00003619 if (ParseType(Ty) ||
3620 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
3621 ParseValue(Ty, Op0, PFS) ||
3622 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson1d0be152009-08-13 21:58:54 +00003623 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003624 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3625 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003626
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003627 bool AteExtraComma = false;
Chris Lattnerdf986172009-01-02 07:01:27 +00003628 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
3629 while (1) {
3630 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003631
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003632 if (!EatIfPresent(lltok::comma))
Chris Lattnerdf986172009-01-02 07:01:27 +00003633 break;
3634
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003635 if (Lex.getKind() == lltok::MetadataVar) {
3636 AteExtraComma = true;
Devang Patela43d46f2009-10-16 18:45:49 +00003637 break;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003638 }
Devang Patela43d46f2009-10-16 18:45:49 +00003639
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003640 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003641 ParseValue(Ty, Op0, PFS) ||
3642 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson1d0be152009-08-13 21:58:54 +00003643 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003644 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3645 return true;
3646 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003647
Chris Lattnerdf986172009-01-02 07:01:27 +00003648 if (!Ty->isFirstClassType())
3649 return Error(TypeLoc, "phi node must have first class type");
3650
3651 PHINode *PN = PHINode::Create(Ty);
3652 PN->reserveOperandSpace(PHIVals.size());
3653 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
3654 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
3655 Inst = PN;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003656 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003657}
3658
3659/// ParseCall
3660/// ::= 'tail'? 'call' OptionalCallingConv OptionalAttrs Type Value
3661/// ParameterList OptionalAttrs
3662bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
3663 bool isTail) {
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003664 unsigned RetAttrs, FnAttrs;
3665 CallingConv::ID CC;
Owen Anderson1d0be152009-08-13 21:58:54 +00003666 PATypeHolder RetType(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00003667 LocTy RetTypeLoc;
3668 ValID CalleeID;
3669 SmallVector<ParamInfo, 16> ArgList;
3670 LocTy CallLoc = Lex.getLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00003671
Chris Lattnerdf986172009-01-02 07:01:27 +00003672 if ((isTail && ParseToken(lltok::kw_call, "expected 'tail call'")) ||
3673 ParseOptionalCallingConv(CC) ||
3674 ParseOptionalAttrs(RetAttrs, 1) ||
Chris Lattnera9a9e072009-03-09 04:49:14 +00003675 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003676 ParseValID(CalleeID) ||
3677 ParseParameterList(ArgList, PFS) ||
3678 ParseOptionalAttrs(FnAttrs, 2))
3679 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003680
Chris Lattnerdf986172009-01-02 07:01:27 +00003681 // If RetType is a non-function pointer type, then this is the short syntax
3682 // for the call, which means that RetType is just the return type. Infer the
3683 // rest of the function argument types from the arguments that are present.
3684 const PointerType *PFTy = 0;
3685 const FunctionType *Ty = 0;
3686 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3687 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3688 // Pull out the types of all of the arguments...
3689 std::vector<const Type*> ParamTypes;
Eli Friedman83b4a972010-07-24 23:06:59 +00003690 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3691 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +00003692
Chris Lattnerdf986172009-01-02 07:01:27 +00003693 if (!FunctionType::isValidReturnType(RetType))
3694 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003695
Owen Andersondebcb012009-07-29 22:17:13 +00003696 Ty = FunctionType::get(RetType, ParamTypes, false);
3697 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00003698 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003699
Chris Lattnerdf986172009-01-02 07:01:27 +00003700 // Look up the callee.
3701 Value *Callee;
Victor Hernandez92f238d2010-01-11 22:31:58 +00003702 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003703
Chris Lattnerdf986172009-01-02 07:01:27 +00003704 // FIXME: In LLVM 3.0, stop accepting zext, sext and inreg as optional
3705 // function attributes.
3706 unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg;
3707 if (FnAttrs & ObsoleteFuncAttrs) {
3708 RetAttrs |= FnAttrs & ObsoleteFuncAttrs;
3709 FnAttrs &= ~ObsoleteFuncAttrs;
3710 }
3711
3712 // Set up the Attributes for the function.
3713 SmallVector<AttributeWithIndex, 8> Attrs;
3714 if (RetAttrs != Attribute::None)
3715 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003716
Chris Lattnerdf986172009-01-02 07:01:27 +00003717 SmallVector<Value*, 8> Args;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003718
Chris Lattnerdf986172009-01-02 07:01:27 +00003719 // Loop through FunctionType's arguments and ensure they are specified
3720 // correctly. Also, gather any parameter attributes.
3721 FunctionType::param_iterator I = Ty->param_begin();
3722 FunctionType::param_iterator E = Ty->param_end();
3723 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
3724 const Type *ExpectedTy = 0;
3725 if (I != E) {
3726 ExpectedTy = *I++;
3727 } else if (!Ty->isVarArg()) {
3728 return Error(ArgList[i].Loc, "too many arguments specified");
3729 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003730
Chris Lattnerdf986172009-01-02 07:01:27 +00003731 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3732 return Error(ArgList[i].Loc, "argument is not of expected type '" +
3733 ExpectedTy->getDescription() + "'");
3734 Args.push_back(ArgList[i].V);
3735 if (ArgList[i].Attrs != Attribute::None)
3736 Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs));
3737 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003738
Chris Lattnerdf986172009-01-02 07:01:27 +00003739 if (I != E)
3740 return Error(CallLoc, "not enough parameters specified for call");
3741
3742 if (FnAttrs != Attribute::None)
3743 Attrs.push_back(AttributeWithIndex::get(~0, FnAttrs));
3744
3745 // Finish off the Attributes and check them
3746 AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Daniel Dunbara279bc32009-09-20 02:20:51 +00003747
Chris Lattnerdf986172009-01-02 07:01:27 +00003748 CallInst *CI = CallInst::Create(Callee, Args.begin(), Args.end());
3749 CI->setTailCall(isTail);
3750 CI->setCallingConv(CC);
3751 CI->setAttributes(PAL);
3752 Inst = CI;
3753 return false;
3754}
3755
3756//===----------------------------------------------------------------------===//
3757// Memory Instructions.
3758//===----------------------------------------------------------------------===//
3759
3760/// ParseAlloc
Devang Patelf633a062009-09-17 23:04:48 +00003761/// ::= 'malloc' Type (',' TypeAndValue)? (',' OptionalInfo)?
3762/// ::= 'alloca' Type (',' TypeAndValue)? (',' OptionalInfo)?
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003763int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS,
3764 BasicBlock* BB, bool isAlloca) {
Owen Anderson1d0be152009-08-13 21:58:54 +00003765 PATypeHolder Ty(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00003766 Value *Size = 0;
Chris Lattnereeb4a842009-07-02 23:08:13 +00003767 LocTy SizeLoc;
Chris Lattnerdf986172009-01-02 07:01:27 +00003768 unsigned Alignment = 0;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003769 if (ParseType(Ty)) return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003770
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003771 bool AteExtraComma = false;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003772 if (EatIfPresent(lltok::comma)) {
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003773 if (Lex.getKind() == lltok::kw_align) {
3774 if (ParseOptionalAlignment(Alignment)) return true;
3775 } else if (Lex.getKind() == lltok::MetadataVar) {
3776 AteExtraComma = true;
Devang Patelf633a062009-09-17 23:04:48 +00003777 } else {
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003778 if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
3779 ParseOptionalCommaAlign(Alignment, AteExtraComma))
3780 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003781 }
3782 }
3783
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003784 if (Size && !Size->getType()->isIntegerTy())
3785 return Error(SizeLoc, "element count must have integer type");
Chris Lattnerdf986172009-01-02 07:01:27 +00003786
Victor Hernandez68afa542009-10-21 19:11:40 +00003787 if (isAlloca) {
Owen Anderson50dead02009-07-15 23:53:25 +00003788 Inst = new AllocaInst(Ty, Size, Alignment);
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003789 return AteExtraComma ? InstExtraComma : InstNormal;
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00003790 }
Victor Hernandez68afa542009-10-21 19:11:40 +00003791
3792 // Autoupgrade old malloc instruction to malloc call.
3793 // FIXME: Remove in LLVM 3.0.
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003794 if (Size && !Size->getType()->isIntegerTy(32))
3795 return Error(SizeLoc, "element count must be i32");
Victor Hernandez68afa542009-10-21 19:11:40 +00003796 const Type *IntPtrTy = Type::getInt32Ty(Context);
Victor Hernandez9d0b7042009-11-07 00:16:28 +00003797 Constant *AllocSize = ConstantExpr::getSizeOf(Ty);
3798 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, IntPtrTy);
Victor Hernandez68afa542009-10-21 19:11:40 +00003799 if (!MallocF)
3800 // Prototype malloc as "void *(int32)".
3801 // This function is renamed as "malloc" in ValidateEndOfModule().
Victor Hernandez336ea062009-10-23 00:59:10 +00003802 MallocF = cast<Function>(
3803 M->getOrInsertFunction("", Type::getInt8PtrTy(Context), IntPtrTy, NULL));
Victor Hernandez9d0b7042009-11-07 00:16:28 +00003804 Inst = CallInst::CreateMalloc(BB, IntPtrTy, Ty, AllocSize, Size, MallocF);
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003805return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003806}
3807
3808/// ParseFree
3809/// ::= 'free' TypeAndValue
Victor Hernandez66284e02009-10-24 04:23:03 +00003810bool LLParser::ParseFree(Instruction *&Inst, PerFunctionState &PFS,
3811 BasicBlock* BB) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003812 Value *Val; LocTy Loc;
3813 if (ParseTypeAndValue(Val, Loc, PFS)) return true;
Duncan Sands1df98592010-02-16 11:11:14 +00003814 if (!Val->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003815 return Error(Loc, "operand to free must be a pointer");
Victor Hernandez66284e02009-10-24 04:23:03 +00003816 Inst = CallInst::CreateFree(Val, BB);
Chris Lattnerdf986172009-01-02 07:01:27 +00003817 return false;
3818}
3819
3820/// ParseLoad
Devang Patelf633a062009-09-17 23:04:48 +00003821/// ::= 'volatile'? 'load' TypeAndValue (',' OptionalInfo)?
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003822int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS,
3823 bool isVolatile) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003824 Value *Val; LocTy Loc;
Devang Patelf633a062009-09-17 23:04:48 +00003825 unsigned Alignment = 0;
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003826 bool AteExtraComma = false;
3827 if (ParseTypeAndValue(Val, Loc, PFS) ||
3828 ParseOptionalCommaAlign(Alignment, AteExtraComma))
3829 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003830
Duncan Sands1df98592010-02-16 11:11:14 +00003831 if (!Val->getType()->isPointerTy() ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003832 !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType())
3833 return Error(Loc, "load operand must be a pointer to a first class type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003834
Chris Lattnerdf986172009-01-02 07:01:27 +00003835 Inst = new LoadInst(Val, "", isVolatile, Alignment);
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003836 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003837}
3838
3839/// ParseStore
Dan Gohmana119de82009-06-14 23:30:43 +00003840/// ::= 'volatile'? 'store' TypeAndValue ',' TypeAndValue (',' 'align' i32)?
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003841int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS,
3842 bool isVolatile) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003843 Value *Val, *Ptr; LocTy Loc, PtrLoc;
Devang Patelf633a062009-09-17 23:04:48 +00003844 unsigned Alignment = 0;
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003845 bool AteExtraComma = false;
Chris Lattnerdf986172009-01-02 07:01:27 +00003846 if (ParseTypeAndValue(Val, Loc, PFS) ||
3847 ParseToken(lltok::comma, "expected ',' after store operand") ||
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003848 ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
3849 ParseOptionalCommaAlign(Alignment, AteExtraComma))
Chris Lattnerdf986172009-01-02 07:01:27 +00003850 return true;
Devang Patelf633a062009-09-17 23:04:48 +00003851
Duncan Sands1df98592010-02-16 11:11:14 +00003852 if (!Ptr->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003853 return Error(PtrLoc, "store operand must be a pointer");
3854 if (!Val->getType()->isFirstClassType())
3855 return Error(Loc, "store operand must be a first class value");
3856 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
3857 return Error(Loc, "stored value and pointer type do not match");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003858
Chris Lattnerdf986172009-01-02 07:01:27 +00003859 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment);
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003860 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003861}
3862
3863/// ParseGetResult
Dan Gohmana119de82009-06-14 23:30:43 +00003864/// ::= 'getresult' TypeAndValue ',' i32
Chris Lattnerdf986172009-01-02 07:01:27 +00003865/// FIXME: Remove support for getresult in LLVM 3.0
3866bool LLParser::ParseGetResult(Instruction *&Inst, PerFunctionState &PFS) {
3867 Value *Val; LocTy ValLoc, EltLoc;
3868 unsigned Element;
3869 if (ParseTypeAndValue(Val, ValLoc, PFS) ||
3870 ParseToken(lltok::comma, "expected ',' after getresult operand") ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003871 ParseUInt32(Element, EltLoc))
Chris Lattnerdf986172009-01-02 07:01:27 +00003872 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003873
Duncan Sands1df98592010-02-16 11:11:14 +00003874 if (!Val->getType()->isStructTy() && !Val->getType()->isArrayTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003875 return Error(ValLoc, "getresult inst requires an aggregate operand");
3876 if (!ExtractValueInst::getIndexedType(Val->getType(), Element))
3877 return Error(EltLoc, "invalid getresult index for value");
3878 Inst = ExtractValueInst::Create(Val, Element);
3879 return false;
3880}
3881
3882/// ParseGetElementPtr
Dan Gohmandd8004d2009-07-27 21:53:46 +00003883/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003884int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003885 Value *Ptr, *Val; LocTy Loc, EltLoc;
Dan Gohmandd8004d2009-07-27 21:53:46 +00003886
Dan Gohmandcb40a32009-07-29 15:58:36 +00003887 bool InBounds = EatIfPresent(lltok::kw_inbounds);
Dan Gohmandd8004d2009-07-27 21:53:46 +00003888
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003889 if (ParseTypeAndValue(Ptr, Loc, PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003890
Duncan Sands1df98592010-02-16 11:11:14 +00003891 if (!Ptr->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003892 return Error(Loc, "base of getelementptr must be a pointer");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003893
Chris Lattnerdf986172009-01-02 07:01:27 +00003894 SmallVector<Value*, 16> Indices;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003895 bool AteExtraComma = false;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003896 while (EatIfPresent(lltok::comma)) {
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003897 if (Lex.getKind() == lltok::MetadataVar) {
3898 AteExtraComma = true;
Devang Patel6225d642009-10-13 18:49:55 +00003899 break;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003900 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003901 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
Duncan Sands1df98592010-02-16 11:11:14 +00003902 if (!Val->getType()->isIntegerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003903 return Error(EltLoc, "getelementptr index must be an integer");
3904 Indices.push_back(Val);
3905 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003906
Chris Lattnerdf986172009-01-02 07:01:27 +00003907 if (!GetElementPtrInst::getIndexedType(Ptr->getType(),
3908 Indices.begin(), Indices.end()))
3909 return Error(Loc, "invalid getelementptr indices");
3910 Inst = GetElementPtrInst::Create(Ptr, Indices.begin(), Indices.end());
Dan Gohmandd8004d2009-07-27 21:53:46 +00003911 if (InBounds)
Dan Gohmanf8dbee72009-09-07 23:54:19 +00003912 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003913 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003914}
3915
3916/// ParseExtractValue
3917/// ::= 'extractvalue' TypeAndValue (',' uint32)+
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003918int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003919 Value *Val; LocTy Loc;
3920 SmallVector<unsigned, 4> Indices;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003921 bool AteExtraComma;
Chris Lattnerdf986172009-01-02 07:01:27 +00003922 if (ParseTypeAndValue(Val, Loc, PFS) ||
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003923 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerdf986172009-01-02 07:01:27 +00003924 return true;
3925
Chris Lattnerfdfeb692010-02-12 20:49:41 +00003926 if (!Val->getType()->isAggregateType())
3927 return Error(Loc, "extractvalue operand must be aggregate type");
Chris Lattnerdf986172009-01-02 07:01:27 +00003928
3929 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(),
3930 Indices.end()))
3931 return Error(Loc, "invalid indices for extractvalue");
3932 Inst = ExtractValueInst::Create(Val, Indices.begin(), Indices.end());
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003933 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003934}
3935
3936/// ParseInsertValue
3937/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003938int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003939 Value *Val0, *Val1; LocTy Loc0, Loc1;
3940 SmallVector<unsigned, 4> Indices;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003941 bool AteExtraComma;
Chris Lattnerdf986172009-01-02 07:01:27 +00003942 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
3943 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
3944 ParseTypeAndValue(Val1, Loc1, PFS) ||
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003945 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerdf986172009-01-02 07:01:27 +00003946 return true;
Chris Lattner628c13a2009-12-30 05:14:00 +00003947
Chris Lattnerfdfeb692010-02-12 20:49:41 +00003948 if (!Val0->getType()->isAggregateType())
3949 return Error(Loc0, "insertvalue operand must be aggregate type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003950
Chris Lattnerdf986172009-01-02 07:01:27 +00003951 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(),
3952 Indices.end()))
3953 return Error(Loc0, "invalid indices for insertvalue");
3954 Inst = InsertValueInst::Create(Val0, Val1, Indices.begin(), Indices.end());
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003955 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003956}
Nick Lewycky21cc4462009-04-04 07:22:01 +00003957
3958//===----------------------------------------------------------------------===//
3959// Embedded metadata.
3960//===----------------------------------------------------------------------===//
3961
3962/// ParseMDNodeVector
Nick Lewyckycb337992009-05-10 20:57:05 +00003963/// ::= Element (',' Element)*
3964/// Element
3965/// ::= 'null' | TypeAndValue
Victor Hernandezbf170d42010-01-05 22:22:14 +00003966bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts,
Victor Hernandez24e64df2010-01-10 07:14:18 +00003967 PerFunctionState *PFS) {
Dan Gohmanac809752010-07-13 19:33:27 +00003968 // Check for an empty list.
3969 if (Lex.getKind() == lltok::rbrace)
3970 return false;
3971
Nick Lewycky21cc4462009-04-04 07:22:01 +00003972 do {
Chris Lattnera7352392009-12-30 04:42:57 +00003973 // Null is a special case since it is typeless.
3974 if (EatIfPresent(lltok::kw_null)) {
3975 Elts.push_back(0);
3976 continue;
Nick Lewyckycb337992009-05-10 20:57:05 +00003977 }
Chris Lattnera7352392009-12-30 04:42:57 +00003978
3979 Value *V = 0;
3980 PATypeHolder Ty(Type::getVoidTy(Context));
3981 ValID ID;
Victor Hernandezbf170d42010-01-05 22:22:14 +00003982 if (ParseType(Ty) || ParseValID(ID, PFS) ||
Victor Hernandez92f238d2010-01-11 22:31:58 +00003983 ConvertValIDToValue(Ty, ID, V, PFS))
Chris Lattnera7352392009-12-30 04:42:57 +00003984 return true;
3985
Nick Lewyckycb337992009-05-10 20:57:05 +00003986 Elts.push_back(V);
Nick Lewycky21cc4462009-04-04 07:22:01 +00003987 } while (EatIfPresent(lltok::comma));
3988
3989 return false;
3990}