blob: 37151e68cb2818dabb6823b4ecadb984a0737100 [file] [log] [blame]
Chris Lattnerac161bf2009-01-02 07:01:27 +00001//===-- LLParser.cpp - Parser Class ---------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the parser class for .ll files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "LLParser.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth91065212014-03-05 10:34:14 +000016#include "llvm/IR/AutoUpgrade.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/CallingConv.h"
18#include "llvm/IR/Constants.h"
19#include "llvm/IR/DerivedTypes.h"
20#include "llvm/IR/InlineAsm.h"
21#include "llvm/IR/Instructions.h"
Manman Ren209b17c2013-09-28 00:22:27 +000022#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Module.h"
24#include "llvm/IR/Operator.h"
25#include "llvm/IR/ValueSymbolTable.h"
Torok Edwin56d06592009-07-11 20:10:48 +000026#include "llvm/Support/ErrorHandling.h"
Chris Lattnerac161bf2009-01-02 07:01:27 +000027#include "llvm/Support/raw_ostream.h"
28using namespace llvm;
29
Chris Lattner229907c2011-07-18 04:54:35 +000030static std::string getTypeString(Type *T) {
Chris Lattner0f214eb2011-06-18 21:18:23 +000031 std::string Result;
32 raw_string_ostream Tmp(Result);
33 Tmp << *T;
34 return Tmp.str();
35}
36
Chris Lattner3822f632009-01-02 08:05:26 +000037/// Run: module ::= toplevelentity*
Chris Lattnerad6f3352009-01-04 20:44:11 +000038bool LLParser::Run() {
Chris Lattner3822f632009-01-02 08:05:26 +000039 // Prime the lexer.
40 Lex.Lex();
41
Chris Lattnerad6f3352009-01-04 20:44:11 +000042 return ParseTopLevelEntities() ||
43 ValidateEndOfModule();
Chris Lattnerac161bf2009-01-02 07:01:27 +000044}
45
46/// ValidateEndOfModule - Do final validity and sanity checks at the end of the
47/// module.
48bool LLParser::ValidateEndOfModule() {
Chris Lattner8eff0152010-04-01 05:14:45 +000049 // Handle any instruction metadata forward references.
50 if (!ForwardRefInstMetadata.empty()) {
51 for (DenseMap<Instruction*, std::vector<MDRef> >::iterator
52 I = ForwardRefInstMetadata.begin(), E = ForwardRefInstMetadata.end();
53 I != E; ++I) {
54 Instruction *Inst = I->first;
55 const std::vector<MDRef> &MDList = I->second;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000056
Chris Lattner8eff0152010-04-01 05:14:45 +000057 for (unsigned i = 0, e = MDList.size(); i != e; ++i) {
58 unsigned SlotNo = MDList[i].MDSlot;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000059
Chris Lattner8eff0152010-04-01 05:14:45 +000060 if (SlotNo >= NumberedMetadata.size() || NumberedMetadata[SlotNo] == 0)
61 return Error(MDList[i].Loc, "use of undefined metadata '!" +
Benjamin Kramerc7583112010-09-27 17:42:11 +000062 Twine(SlotNo) + "'");
Chris Lattner8eff0152010-04-01 05:14:45 +000063 Inst->setMetadata(MDList[i].MDKind, NumberedMetadata[SlotNo]);
64 }
65 }
66 ForwardRefInstMetadata.clear();
67 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +000068
Manman Ren209b17c2013-09-28 00:22:27 +000069 for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
70 UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
71
Bill Wendlingb32b0412013-02-08 06:32:06 +000072 // Handle any function attribute group forward references.
73 for (std::map<Value*, std::vector<unsigned> >::iterator
74 I = ForwardRefAttrGroups.begin(), E = ForwardRefAttrGroups.end();
75 I != E; ++I) {
76 Value *V = I->first;
77 std::vector<unsigned> &Vec = I->second;
78 AttrBuilder B;
79
80 for (std::vector<unsigned>::iterator VI = Vec.begin(), VE = Vec.end();
81 VI != VE; ++VI)
82 B.merge(NumberedAttrBuilders[*VI]);
83
84 if (Function *Fn = dyn_cast<Function>(V)) {
85 AttributeSet AS = Fn->getAttributes();
86 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
87 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
88 AS.getFnAttributes());
89
90 FnAttrs.merge(B);
91
92 // If the alignment was parsed as an attribute, move to the alignment
93 // field.
94 if (FnAttrs.hasAlignmentAttr()) {
95 Fn->setAlignment(FnAttrs.getAlignment());
96 FnAttrs.removeAttribute(Attribute::Alignment);
97 }
98
99 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
100 AttributeSet::get(Context,
101 AttributeSet::FunctionIndex,
102 FnAttrs));
103 Fn->setAttributes(AS);
104 } else if (CallInst *CI = dyn_cast<CallInst>(V)) {
105 AttributeSet AS = CI->getAttributes();
106 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
107 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
108 AS.getFnAttributes());
Bill Wendling59dce372013-02-12 10:13:06 +0000109 FnAttrs.merge(B);
Bill Wendlingb32b0412013-02-08 06:32:06 +0000110 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
111 AttributeSet::get(Context,
112 AttributeSet::FunctionIndex,
113 FnAttrs));
114 CI->setAttributes(AS);
115 } else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) {
116 AttributeSet AS = II->getAttributes();
117 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
118 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
119 AS.getFnAttributes());
Bill Wendling59dce372013-02-12 10:13:06 +0000120 FnAttrs.merge(B);
Bill Wendlingb32b0412013-02-08 06:32:06 +0000121 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
122 AttributeSet::get(Context,
123 AttributeSet::FunctionIndex,
124 FnAttrs));
125 II->setAttributes(AS);
126 } else {
127 llvm_unreachable("invalid object with forward attribute group reference");
128 }
129 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000130
Chris Lattner3432c622009-10-28 03:39:23 +0000131 // If there are entries in ForwardRefBlockAddresses at this point, they are
132 // references after the function was defined. Resolve those now.
133 while (!ForwardRefBlockAddresses.empty()) {
134 // Okay, we are referencing an already-parsed function, resolve them now.
135 Function *TheFn = 0;
136 const ValID &Fn = ForwardRefBlockAddresses.begin()->first;
137 if (Fn.Kind == ValID::t_GlobalName)
138 TheFn = M->getFunction(Fn.StrVal);
139 else if (Fn.UIntVal < NumberedVals.size())
140 TheFn = dyn_cast<Function>(NumberedVals[Fn.UIntVal]);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000141
Chris Lattner3432c622009-10-28 03:39:23 +0000142 if (TheFn == 0)
143 return Error(Fn.Loc, "unknown function referenced by blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000144
Chris Lattner3432c622009-10-28 03:39:23 +0000145 // Resolve all these references.
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000146 if (ResolveForwardRefBlockAddresses(TheFn,
Chris Lattner3432c622009-10-28 03:39:23 +0000147 ForwardRefBlockAddresses.begin()->second,
148 0))
149 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000150
Chris Lattner3432c622009-10-28 03:39:23 +0000151 ForwardRefBlockAddresses.erase(ForwardRefBlockAddresses.begin());
152 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000153
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000154 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i)
155 if (NumberedTypes[i].second.isValid())
156 return Error(NumberedTypes[i].second,
157 "use of undefined type '%" + Twine(i) + "'");
158
159 for (StringMap<std::pair<Type*, LocTy> >::iterator I =
160 NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I)
161 if (I->second.second.isValid())
162 return Error(I->second.second,
163 "use of undefined type named '" + I->getKey() + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000164
Chris Lattnerac161bf2009-01-02 07:01:27 +0000165 if (!ForwardRefVals.empty())
166 return Error(ForwardRefVals.begin()->second.second,
167 "use of undefined value '@" + ForwardRefVals.begin()->first +
168 "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000169
Chris Lattnerac161bf2009-01-02 07:01:27 +0000170 if (!ForwardRefValIDs.empty())
171 return Error(ForwardRefValIDs.begin()->second.second,
172 "use of undefined value '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000173 Twine(ForwardRefValIDs.begin()->first) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000174
Devang Pateld2541152009-07-08 19:23:54 +0000175 if (!ForwardRefMDNodes.empty())
176 return Error(ForwardRefMDNodes.begin()->second.second,
177 "use of undefined metadata '!" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000178 Twine(ForwardRefMDNodes.begin()->first) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000179
Devang Pateld2541152009-07-08 19:23:54 +0000180
Chris Lattnerac161bf2009-01-02 07:01:27 +0000181 // Look for intrinsic functions and CallInst that need to be upgraded
182 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; )
183 UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000184
Manman Ren8b4306c2013-12-02 21:29:56 +0000185 UpgradeDebugInfo(*M);
186
Chris Lattnerac161bf2009-01-02 07:01:27 +0000187 return false;
188}
189
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000190bool LLParser::ResolveForwardRefBlockAddresses(Function *TheFn,
Chris Lattner3432c622009-10-28 03:39:23 +0000191 std::vector<std::pair<ValID, GlobalValue*> > &Refs,
192 PerFunctionState *PFS) {
193 // Loop over all the references, resolving them.
194 for (unsigned i = 0, e = Refs.size(); i != e; ++i) {
195 BasicBlock *Res;
Chris Lattneraa99c942009-11-01 01:27:45 +0000196 if (PFS) {
Chris Lattner3432c622009-10-28 03:39:23 +0000197 if (Refs[i].first.Kind == ValID::t_LocalName)
198 Res = PFS->GetBB(Refs[i].first.StrVal, Refs[i].first.Loc);
Chris Lattneraa99c942009-11-01 01:27:45 +0000199 else
Chris Lattner3432c622009-10-28 03:39:23 +0000200 Res = PFS->GetBB(Refs[i].first.UIntVal, Refs[i].first.Loc);
201 } else if (Refs[i].first.Kind == ValID::t_LocalID) {
202 return Error(Refs[i].first.Loc,
Chris Lattnera38a4df2009-11-02 18:28:45 +0000203 "cannot take address of numeric label after the function is defined");
Chris Lattner3432c622009-10-28 03:39:23 +0000204 } else {
205 Res = dyn_cast_or_null<BasicBlock>(
206 TheFn->getValueSymbolTable().lookup(Refs[i].first.StrVal));
207 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000208
Chris Lattneraa99c942009-11-01 01:27:45 +0000209 if (Res == 0)
Chris Lattner3432c622009-10-28 03:39:23 +0000210 return Error(Refs[i].first.Loc,
211 "referenced value is not a basic block");
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000212
Chris Lattner3432c622009-10-28 03:39:23 +0000213 // Get the BlockAddress for this and update references to use it.
214 BlockAddress *BA = BlockAddress::get(TheFn, Res);
215 Refs[i].second->replaceAllUsesWith(BA);
216 Refs[i].second->eraseFromParent();
217 }
218 return false;
219}
220
221
Chris Lattnerac161bf2009-01-02 07:01:27 +0000222//===----------------------------------------------------------------------===//
223// Top-Level Entities
224//===----------------------------------------------------------------------===//
225
226bool LLParser::ParseTopLevelEntities() {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000227 while (1) {
228 switch (Lex.getKind()) {
229 default: return TokError("expected top-level entity");
230 case lltok::Eof: return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000231 case lltok::kw_declare: if (ParseDeclare()) return true; break;
232 case lltok::kw_define: if (ParseDefine()) return true; break;
233 case lltok::kw_module: if (ParseModuleAsm()) return true; break;
234 case lltok::kw_target: if (ParseTargetDefinition()) return true; break;
Bill Wendling706d3d62012-11-28 08:41:48 +0000235 case lltok::kw_deplibs: if (ParseDepLibs()) return true; break;
Dan Gohman466876b2009-08-12 23:32:33 +0000236 case lltok::LocalVarID: if (ParseUnnamedType()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000237 case lltok::LocalVar: if (ParseNamedType()) return true; break;
Dan Gohman466876b2009-08-12 23:32:33 +0000238 case lltok::GlobalID: if (ParseUnnamedGlobal()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000239 case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break;
Chris Lattner1eed2d62009-12-30 04:56:59 +0000240 case lltok::exclaim: if (ParseStandaloneMetadata()) return true; break;
Bill Wendling63b88192013-02-06 06:52:58 +0000241 case lltok::MetadataVar:if (ParseNamedMetadata()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000242
243 // The Global variable production with no name can have many different
244 // optional leading prefixes, the production is:
Nico Rieck7157bb72014-01-14 15:22:47 +0000245 // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
246 // OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
Rafael Espindola45e6c192011-01-08 16:42:36 +0000247 // ('constant'|'global') ...
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000248 case lltok::kw_private: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000249 case lltok::kw_internal: // OptionalLinkage
250 case lltok::kw_weak: // OptionalLinkage
251 case lltok::kw_weak_odr: // OptionalLinkage
252 case lltok::kw_linkonce: // OptionalLinkage
253 case lltok::kw_linkonce_odr: // OptionalLinkage
254 case lltok::kw_appending: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000255 case lltok::kw_common: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000256 case lltok::kw_extern_weak: // OptionalLinkage
257 case lltok::kw_external: { // OptionalLinkage
Nico Rieck7157bb72014-01-14 15:22:47 +0000258 unsigned Linkage, Visibility, DLLStorageClass;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000259 if (ParseOptionalLinkage(Linkage) ||
260 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000261 ParseOptionalDLLStorageClass(DLLStorageClass) ||
262 ParseGlobal("", SMLoc(), Linkage, true, Visibility, DLLStorageClass))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000263 return true;
264 break;
265 }
266 case lltok::kw_default: // OptionalVisibility
267 case lltok::kw_hidden: // OptionalVisibility
268 case lltok::kw_protected: { // OptionalVisibility
Nico Rieck7157bb72014-01-14 15:22:47 +0000269 unsigned Visibility, DLLStorageClass;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000270 if (ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000271 ParseOptionalDLLStorageClass(DLLStorageClass) ||
272 ParseGlobal("", SMLoc(), 0, false, Visibility, DLLStorageClass))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000273 return true;
274 break;
275 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000276
Chris Lattnerac161bf2009-01-02 07:01:27 +0000277 case lltok::kw_thread_local: // OptionalThreadLocal
278 case lltok::kw_addrspace: // OptionalAddrSpace
279 case lltok::kw_constant: // GlobalType
280 case lltok::kw_global: // GlobalType
Nico Rieck7157bb72014-01-14 15:22:47 +0000281 if (ParseGlobal("", SMLoc(), 0, false, 0, 0)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000282 break;
Bill Wendlinga7c38772013-02-09 15:48:49 +0000283
284 case lltok::kw_attributes: if (ParseUnnamedAttrGrp()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000285 }
286 }
287}
288
289
290/// toplevelentity
291/// ::= 'module' 'asm' STRINGCONSTANT
292bool LLParser::ParseModuleAsm() {
293 assert(Lex.getKind() == lltok::kw_module);
294 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000295
296 std::string AsmStr;
Chris Lattner3822f632009-01-02 08:05:26 +0000297 if (ParseToken(lltok::kw_asm, "expected 'module asm'") ||
298 ParseStringConstant(AsmStr)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000299
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000300 M->appendModuleInlineAsm(AsmStr);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000301 return false;
302}
303
304/// toplevelentity
305/// ::= 'target' 'triple' '=' STRINGCONSTANT
306/// ::= 'target' 'datalayout' '=' STRINGCONSTANT
307bool LLParser::ParseTargetDefinition() {
308 assert(Lex.getKind() == lltok::kw_target);
Chris Lattner3822f632009-01-02 08:05:26 +0000309 std::string Str;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000310 switch (Lex.Lex()) {
311 default: return TokError("unknown target property");
312 case lltok::kw_triple:
313 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +0000314 if (ParseToken(lltok::equal, "expected '=' after target triple") ||
315 ParseStringConstant(Str))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000316 return true;
Chris Lattner3822f632009-01-02 08:05:26 +0000317 M->setTargetTriple(Str);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000318 return false;
319 case lltok::kw_datalayout:
320 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +0000321 if (ParseToken(lltok::equal, "expected '=' after target datalayout") ||
322 ParseStringConstant(Str))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000323 return true;
Chris Lattner3822f632009-01-02 08:05:26 +0000324 M->setDataLayout(Str);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000325 return false;
326 }
327}
328
Bill Wendling706d3d62012-11-28 08:41:48 +0000329/// toplevelentity
330/// ::= 'deplibs' '=' '[' ']'
331/// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']'
332/// FIXME: Remove in 4.0. Currently parse, but ignore.
333bool LLParser::ParseDepLibs() {
334 assert(Lex.getKind() == lltok::kw_deplibs);
335 Lex.Lex();
336 if (ParseToken(lltok::equal, "expected '=' after deplibs") ||
337 ParseToken(lltok::lsquare, "expected '=' after deplibs"))
338 return true;
339
340 if (EatIfPresent(lltok::rsquare))
341 return false;
342
343 do {
344 std::string Str;
345 if (ParseStringConstant(Str)) return true;
346 } while (EatIfPresent(lltok::comma));
347
348 return ParseToken(lltok::rsquare, "expected ']' at end of list");
349}
350
Dan Gohman466876b2009-08-12 23:32:33 +0000351/// ParseUnnamedType:
Dan Gohman466876b2009-08-12 23:32:33 +0000352/// ::= LocalVarID '=' 'type' type
Chris Lattnerac161bf2009-01-02 07:01:27 +0000353bool LLParser::ParseUnnamedType() {
Chris Lattner07037362011-06-18 23:51:31 +0000354 LocTy TypeLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000355 unsigned TypeID = Lex.getUIntVal();
Chris Lattner8936d2b2011-06-19 00:03:46 +0000356 Lex.Lex(); // eat LocalVarID;
357
358 if (ParseToken(lltok::equal, "expected '=' after name") ||
359 ParseToken(lltok::kw_type, "expected 'type' after '='"))
360 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000361
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000362 if (TypeID >= NumberedTypes.size())
363 NumberedTypes.resize(TypeID+1);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000364
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000365 Type *Result = 0;
366 if (ParseStructDefinition(TypeLoc, "",
367 NumberedTypes[TypeID], Result)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000368
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000369 if (!isa<StructType>(Result)) {
370 std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID];
371 if (Entry.first)
372 return Error(TypeLoc, "non-struct types may not be recursive");
373 Entry.first = Result;
374 Entry.second = SMLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000375 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000376
Chris Lattnerac161bf2009-01-02 07:01:27 +0000377 return false;
378}
379
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000380
Chris Lattnerac161bf2009-01-02 07:01:27 +0000381/// toplevelentity
382/// ::= LocalVar '=' 'type' type
383bool LLParser::ParseNamedType() {
384 std::string Name = Lex.getStrVal();
385 LocTy NameLoc = Lex.getLoc();
Chris Lattner3822f632009-01-02 08:05:26 +0000386 Lex.Lex(); // eat LocalVar.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000387
Chris Lattner3822f632009-01-02 08:05:26 +0000388 if (ParseToken(lltok::equal, "expected '=' after name") ||
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000389 ParseToken(lltok::kw_type, "expected 'type' after name"))
Chris Lattner3822f632009-01-02 08:05:26 +0000390 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000391
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000392 Type *Result = 0;
393 if (ParseStructDefinition(NameLoc, Name,
394 NamedTypes[Name], Result)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000395
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000396 if (!isa<StructType>(Result)) {
397 std::pair<Type*, LocTy> &Entry = NamedTypes[Name];
398 if (Entry.first)
399 return Error(NameLoc, "non-struct types may not be recursive");
400 Entry.first = Result;
401 Entry.second = SMLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000402 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000403
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000404 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000405}
406
407
408/// toplevelentity
409/// ::= 'declare' FunctionHeader
410bool LLParser::ParseDeclare() {
411 assert(Lex.getKind() == lltok::kw_declare);
412 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000413
Chris Lattnerac161bf2009-01-02 07:01:27 +0000414 Function *F;
415 return ParseFunctionHeader(F, false);
416}
417
418/// toplevelentity
419/// ::= 'define' FunctionHeader '{' ...
420bool LLParser::ParseDefine() {
421 assert(Lex.getKind() == lltok::kw_define);
422 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000423
Chris Lattnerac161bf2009-01-02 07:01:27 +0000424 Function *F;
Chris Lattner3822f632009-01-02 08:05:26 +0000425 return ParseFunctionHeader(F, true) ||
426 ParseFunctionBody(*F);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000427}
428
Chris Lattner3822f632009-01-02 08:05:26 +0000429/// ParseGlobalType
430/// ::= 'constant'
431/// ::= 'global'
Chris Lattnerac161bf2009-01-02 07:01:27 +0000432bool LLParser::ParseGlobalType(bool &IsConstant) {
433 if (Lex.getKind() == lltok::kw_constant)
434 IsConstant = true;
435 else if (Lex.getKind() == lltok::kw_global)
436 IsConstant = false;
Duncan Sandsd1de45a2009-02-10 16:24:55 +0000437 else {
438 IsConstant = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000439 return TokError("expected 'global' or 'constant'");
Duncan Sandsd1de45a2009-02-10 16:24:55 +0000440 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000441 Lex.Lex();
442 return false;
443}
444
Dan Gohman466876b2009-08-12 23:32:33 +0000445/// ParseUnnamedGlobal:
446/// OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000447/// OptionalLinkage OptionalVisibility OptionalDLLStorageClass
448/// ... -> global variable
Dan Gohman466876b2009-08-12 23:32:33 +0000449/// GlobalID '=' OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000450/// GlobalID '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
451/// ... -> global variable
Dan Gohman466876b2009-08-12 23:32:33 +0000452bool LLParser::ParseUnnamedGlobal() {
453 unsigned VarID = NumberedVals.size();
454 std::string Name;
455 LocTy NameLoc = Lex.getLoc();
456
457 // Handle the GlobalID form.
458 if (Lex.getKind() == lltok::GlobalID) {
459 if (Lex.getUIntVal() != VarID)
460 return Error(Lex.getLoc(), "variable expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000461 Twine(VarID) + "'");
Dan Gohman466876b2009-08-12 23:32:33 +0000462 Lex.Lex(); // eat GlobalID;
463
464 if (ParseToken(lltok::equal, "expected '=' after name"))
465 return true;
466 }
467
468 bool HasLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000469 unsigned Linkage, Visibility, DLLStorageClass;
Dan Gohman466876b2009-08-12 23:32:33 +0000470 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000471 ParseOptionalVisibility(Visibility) ||
472 ParseOptionalDLLStorageClass(DLLStorageClass))
Dan Gohman466876b2009-08-12 23:32:33 +0000473 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000474
Dan Gohman466876b2009-08-12 23:32:33 +0000475 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
Nico Rieck7157bb72014-01-14 15:22:47 +0000476 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
477 DLLStorageClass);
478 return ParseAlias(Name, NameLoc, Visibility, DLLStorageClass);
Dan Gohman466876b2009-08-12 23:32:33 +0000479}
480
Chris Lattnerac161bf2009-01-02 07:01:27 +0000481/// ParseNamedGlobal:
482/// GlobalVar '=' OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000483/// GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
484/// ... -> global variable
Chris Lattnerac161bf2009-01-02 07:01:27 +0000485bool LLParser::ParseNamedGlobal() {
486 assert(Lex.getKind() == lltok::GlobalVar);
487 LocTy NameLoc = Lex.getLoc();
488 std::string Name = Lex.getStrVal();
489 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000490
Chris Lattnerac161bf2009-01-02 07:01:27 +0000491 bool HasLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000492 unsigned Linkage, Visibility, DLLStorageClass;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000493 if (ParseToken(lltok::equal, "expected '=' in global variable") ||
494 ParseOptionalLinkage(Linkage, HasLinkage) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000495 ParseOptionalVisibility(Visibility) ||
496 ParseOptionalDLLStorageClass(DLLStorageClass))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000497 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000498
Chris Lattnerac161bf2009-01-02 07:01:27 +0000499 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
Nico Rieck7157bb72014-01-14 15:22:47 +0000500 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
501 DLLStorageClass);
502 return ParseAlias(Name, NameLoc, Visibility, DLLStorageClass);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000503}
504
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000505// MDString:
506// ::= '!' STRINGCONSTANT
Chris Lattner1797fc72009-12-29 21:53:55 +0000507bool LLParser::ParseMDString(MDString *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000508 std::string Str;
509 if (ParseStringConstant(Str)) return true;
Chris Lattner1797fc72009-12-29 21:53:55 +0000510 Result = MDString::get(Context, Str);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000511 return false;
512}
513
514// MDNode:
515// ::= '!' MDNodeNumber
Chris Lattner8eff0152010-04-01 05:14:45 +0000516//
517/// This version of ParseMDNodeID returns the slot number and null in the case
518/// of a forward reference.
519bool LLParser::ParseMDNodeID(MDNode *&Result, unsigned &SlotNo) {
520 // !{ ..., !42, ... }
521 if (ParseUInt32(SlotNo)) return true;
522
523 // Check existing MDNode.
524 if (SlotNo < NumberedMetadata.size() && NumberedMetadata[SlotNo] != 0)
525 Result = NumberedMetadata[SlotNo];
526 else
527 Result = 0;
528 return false;
529}
530
Chris Lattner6dac02a2009-12-30 04:15:23 +0000531bool LLParser::ParseMDNodeID(MDNode *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000532 // !{ ..., !42, ... }
533 unsigned MID = 0;
Chris Lattner8eff0152010-04-01 05:14:45 +0000534 if (ParseMDNodeID(Result, MID)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000535
Chris Lattner8eff0152010-04-01 05:14:45 +0000536 // If not a forward reference, just return it now.
537 if (Result) return false;
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000538
Chris Lattner8eff0152010-04-01 05:14:45 +0000539 // Otherwise, create MDNode forward reference.
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000540 MDNode *FwdNode = MDNode::getTemporary(Context, None);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000541 ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc());
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000542
Chris Lattnerfc58af22009-12-30 04:51:58 +0000543 if (NumberedMetadata.size() <= MID)
544 NumberedMetadata.resize(MID+1);
545 NumberedMetadata[MID] = FwdNode;
Chris Lattner1797fc72009-12-29 21:53:55 +0000546 Result = FwdNode;
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000547 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000548}
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000549
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000550/// ParseNamedMetadata:
Devang Patelbe626972009-07-29 00:34:02 +0000551/// !foo = !{ !1, !2 }
552bool LLParser::ParseNamedMetadata() {
Chris Lattnereafe4de2009-12-30 05:02:06 +0000553 assert(Lex.getKind() == lltok::MetadataVar);
Devang Patelbe626972009-07-29 00:34:02 +0000554 std::string Name = Lex.getStrVal();
Chris Lattnereafe4de2009-12-30 05:02:06 +0000555 Lex.Lex();
Devang Patelbe626972009-07-29 00:34:02 +0000556
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000557 if (ParseToken(lltok::equal, "expected '=' here") ||
Chris Lattner1eed2d62009-12-30 04:56:59 +0000558 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000559 ParseToken(lltok::lbrace, "Expected '{' here"))
Devang Patelbe626972009-07-29 00:34:02 +0000560 return true;
561
Dan Gohman2637cc12010-07-21 23:38:33 +0000562 NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000563 if (Lex.getKind() != lltok::rbrace)
564 do {
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000565 if (ParseToken(lltok::exclaim, "Expected '!' here"))
566 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000567
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000568 MDNode *N = 0;
569 if (ParseMDNodeID(N)) return true;
Dan Gohman2637cc12010-07-21 23:38:33 +0000570 NMD->addOperand(N);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000571 } while (EatIfPresent(lltok::comma));
Devang Patelbe626972009-07-29 00:34:02 +0000572
573 if (ParseToken(lltok::rbrace, "expected end of metadata node"))
574 return true;
575
Devang Patelbe626972009-07-29 00:34:02 +0000576 return false;
577}
578
Devang Patel39e64d42009-07-01 19:21:12 +0000579/// ParseStandaloneMetadata:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000580/// !42 = !{...}
Devang Patel39e64d42009-07-01 19:21:12 +0000581bool LLParser::ParseStandaloneMetadata() {
Chris Lattner1eed2d62009-12-30 04:56:59 +0000582 assert(Lex.getKind() == lltok::exclaim);
Devang Patel39e64d42009-07-01 19:21:12 +0000583 Lex.Lex();
584 unsigned MetadataID = 0;
Devang Patel39e64d42009-07-01 19:21:12 +0000585
586 LocTy TyLoc;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000587 Type *Ty = 0;
Devang Patele059ba6e2009-07-23 01:07:34 +0000588 SmallVector<Value *, 16> Elts;
Chris Lattner278bc952009-12-29 22:40:21 +0000589 if (ParseUInt32(MetadataID) ||
590 ParseToken(lltok::equal, "expected '=' here") ||
591 ParseType(Ty, TyLoc) ||
Chris Lattner1eed2d62009-12-30 04:56:59 +0000592 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattner278bc952009-12-29 22:40:21 +0000593 ParseToken(lltok::lbrace, "Expected '{' here") ||
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000594 ParseMDNodeVector(Elts, NULL) ||
Chris Lattner278bc952009-12-29 22:40:21 +0000595 ParseToken(lltok::rbrace, "expected end of metadata node"))
Devang Patele059ba6e2009-07-23 01:07:34 +0000596 return true;
597
Jay Foad5514afe2011-04-21 19:59:31 +0000598 MDNode *Init = MDNode::get(Context, Elts);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000599
Chris Lattnerfc58af22009-12-30 04:51:58 +0000600 // See if this was forward referenced, if so, handle it.
Chris Lattner218b22f2009-12-29 21:43:58 +0000601 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> >::iterator
Devang Pateld2541152009-07-08 19:23:54 +0000602 FI = ForwardRefMDNodes.find(MetadataID);
603 if (FI != ForwardRefMDNodes.end()) {
Dan Gohman16a5d982010-08-20 22:02:26 +0000604 MDNode *Temp = FI->second.first;
605 Temp->replaceAllUsesWith(Init);
606 MDNode::deleteTemporary(Temp);
Devang Pateld2541152009-07-08 19:23:54 +0000607 ForwardRefMDNodes.erase(FI);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000608
Chris Lattnerfc58af22009-12-30 04:51:58 +0000609 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
610 } else {
611 if (MetadataID >= NumberedMetadata.size())
612 NumberedMetadata.resize(MetadataID+1);
613
614 if (NumberedMetadata[MetadataID] != 0)
615 return TokError("Metadata id is already used");
616 NumberedMetadata[MetadataID] = Init;
Devang Pateld2541152009-07-08 19:23:54 +0000617 }
618
Devang Patel39e64d42009-07-01 19:21:12 +0000619 return false;
620}
621
Chris Lattnerac161bf2009-01-02 07:01:27 +0000622/// ParseAlias:
Nico Rieck7157bb72014-01-14 15:22:47 +0000623/// ::= GlobalVar '=' OptionalVisibility OptionalDLLStorageClass 'alias'
624/// OptionalLinkage Aliasee
Chris Lattnerac161bf2009-01-02 07:01:27 +0000625/// Aliasee
Chris Lattner4c73d7a2009-04-25 21:26:00 +0000626/// ::= TypeAndValue
627/// ::= 'bitcast' '(' TypeAndValue 'to' Type ')'
Dan Gohman1639c392009-07-27 21:53:46 +0000628/// ::= 'getelementptr' 'inbounds'? '(' ... ')'
Chris Lattnerac161bf2009-01-02 07:01:27 +0000629///
Nico Rieck7157bb72014-01-14 15:22:47 +0000630/// Everything through DLL storage class has already been parsed.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000631///
632bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc,
Nico Rieck7157bb72014-01-14 15:22:47 +0000633 unsigned Visibility, unsigned DLLStorageClass) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000634 assert(Lex.getKind() == lltok::kw_alias);
635 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000636 LocTy LinkageLoc = Lex.getLoc();
Rafael Espindola78527052013-10-06 15:10:43 +0000637 unsigned L;
638 if (ParseOptionalLinkage(L))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000639 return true;
640
Rafael Espindola78527052013-10-06 15:10:43 +0000641 GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L;
642
Rafael Espindolacaa43562013-10-09 16:07:32 +0000643 if(!GlobalAlias::isValidLinkage(Linkage))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000644 return Error(LinkageLoc, "invalid linkage type for alias");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000645
Chris Lattnerac161bf2009-01-02 07:01:27 +0000646 Constant *Aliasee;
647 LocTy AliaseeLoc = Lex.getLoc();
Chris Lattner4c73d7a2009-04-25 21:26:00 +0000648 if (Lex.getKind() != lltok::kw_bitcast &&
649 Lex.getKind() != lltok::kw_getelementptr) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000650 if (ParseGlobalTypeAndValue(Aliasee)) return true;
651 } else {
652 // The bitcast dest type is not present, it is implied by the dest type.
653 ValID ID;
654 if (ParseValID(ID)) return true;
655 if (ID.Kind != ValID::t_Constant)
656 return Error(AliaseeLoc, "invalid aliasee");
657 Aliasee = ID.ConstantVal;
658 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000659
Duncan Sands19d0b472010-02-16 11:11:14 +0000660 if (!Aliasee->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +0000661 return Error(AliaseeLoc, "alias must have pointer type");
662
663 // Okay, create the alias but do not insert it into the module yet.
664 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(),
665 (GlobalValue::LinkageTypes)Linkage, Name,
666 Aliasee);
667 GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000668 GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000669
Chris Lattnerac161bf2009-01-02 07:01:27 +0000670 // See if this value already exists in the symbol table. If so, it is either
671 // a redefinition or a definition of a forward reference.
Chris Lattnere38317f2009-10-25 23:22:50 +0000672 if (GlobalValue *Val = M->getNamedValue(Name)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000673 // See if this was a redefinition. If so, there is no entry in
674 // ForwardRefVals.
675 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
676 I = ForwardRefVals.find(Name);
677 if (I == ForwardRefVals.end())
678 return Error(NameLoc, "redefinition of global named '@" + Name + "'");
679
680 // Otherwise, this was a definition of forward ref. Verify that types
681 // agree.
682 if (Val->getType() != GA->getType())
683 return Error(NameLoc,
684 "forward reference and definition of alias have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000685
Chris Lattnerac161bf2009-01-02 07:01:27 +0000686 // If they agree, just RAUW the old value with the alias and remove the
687 // forward ref info.
688 Val->replaceAllUsesWith(GA);
689 Val->eraseFromParent();
690 ForwardRefVals.erase(I);
691 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000692
Chris Lattnerac161bf2009-01-02 07:01:27 +0000693 // Insert into the module, we know its name won't collide now.
694 M->getAliasList().push_back(GA);
Benjamin Kramer1dc34b42010-10-16 11:28:23 +0000695 assert(GA->getName() == Name && "Should not be a name conflict!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000696
Chris Lattnerac161bf2009-01-02 07:01:27 +0000697 return false;
698}
699
700/// ParseGlobal
Nico Rieck7157bb72014-01-14 15:22:47 +0000701/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
702/// OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000703/// OptionalExternallyInitialized GlobalType Type Const
Nico Rieck7157bb72014-01-14 15:22:47 +0000704/// ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
705/// OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000706/// OptionalExternallyInitialized GlobalType Type Const
Chris Lattnerac161bf2009-01-02 07:01:27 +0000707///
David Majnemerc4ab61c2014-03-09 06:41:58 +0000708/// Everything up to and including OptionalDLLStorageClass has been parsed
709/// already.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000710///
711bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
712 unsigned Linkage, bool HasLinkage,
Nico Rieck7157bb72014-01-14 15:22:47 +0000713 unsigned Visibility, unsigned DLLStorageClass) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000714 unsigned AddrSpace;
Shuxin Yang2e1890e2013-10-27 03:08:44 +0000715 bool IsConstant, UnnamedAddr, IsExternallyInitialized;
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000716 GlobalVariable::ThreadLocalMode TLM;
Rafael Espindola026d1522011-01-13 01:30:30 +0000717 LocTy UnnamedAddrLoc;
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000718 LocTy IsExternallyInitializedLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000719 LocTy TyLoc;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000720
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000721 Type *Ty = 0;
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000722 if (ParseOptionalThreadLocal(TLM) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000723 ParseOptionalAddrSpace(AddrSpace) ||
Rafael Espindola026d1522011-01-13 01:30:30 +0000724 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
725 &UnnamedAddrLoc) ||
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000726 ParseOptionalToken(lltok::kw_externally_initialized,
727 IsExternallyInitialized,
728 &IsExternallyInitializedLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000729 ParseGlobalType(IsConstant) ||
730 ParseType(Ty, TyLoc))
731 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000732
Chris Lattnerac161bf2009-01-02 07:01:27 +0000733 // If the linkage is specified and is external, then no initializer is
734 // present.
735 Constant *Init = 0;
Nico Rieck7157bb72014-01-14 15:22:47 +0000736 if (!HasLinkage || (Linkage != GlobalValue::ExternalWeakLinkage &&
Chris Lattnerac161bf2009-01-02 07:01:27 +0000737 Linkage != GlobalValue::ExternalLinkage)) {
738 if (ParseGlobalValue(Ty, Init))
739 return true;
740 }
741
Duncan Sands19d0b472010-02-16 11:11:14 +0000742 if (Ty->isFunctionTy() || Ty->isLabelTy())
Chris Lattnerc9e1b482009-02-08 20:00:15 +0000743 return Error(TyLoc, "invalid type for global variable");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000744
Chris Lattnerac161bf2009-01-02 07:01:27 +0000745 GlobalVariable *GV = 0;
746
747 // See if the global was forward referenced, if so, use the global.
Chris Lattner1f386b82009-02-02 07:24:28 +0000748 if (!Name.empty()) {
Chris Lattnere38317f2009-10-25 23:22:50 +0000749 if (GlobalValue *GVal = M->getNamedValue(Name)) {
750 if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal))
751 return Error(NameLoc, "redefinition of global '@" + Name + "'");
752 GV = cast<GlobalVariable>(GVal);
753 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000754 } else {
755 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
756 I = ForwardRefValIDs.find(NumberedVals.size());
757 if (I != ForwardRefValIDs.end()) {
758 GV = cast<GlobalVariable>(I->second.first);
759 ForwardRefValIDs.erase(I);
760 }
761 }
762
763 if (GV == 0) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000764 GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, 0,
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000765 Name, 0, GlobalVariable::NotThreadLocal,
766 AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000767 } else {
768 if (GV->getType()->getElementType() != Ty)
769 return Error(TyLoc,
770 "forward reference and definition of global have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000771
Chris Lattnerac161bf2009-01-02 07:01:27 +0000772 // Move the forward-reference to the correct spot in the module.
773 M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
774 }
775
776 if (Name.empty())
777 NumberedVals.push_back(GV);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000778
Chris Lattnerac161bf2009-01-02 07:01:27 +0000779 // Set the parsed properties on the global.
780 if (Init)
781 GV->setInitializer(Init);
782 GV->setConstant(IsConstant);
783 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
784 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000785 GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000786 GV->setExternallyInitialized(IsExternallyInitialized);
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000787 GV->setThreadLocalMode(TLM);
Rafael Espindola45e6c192011-01-08 16:42:36 +0000788 GV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000789
Chris Lattnerac161bf2009-01-02 07:01:27 +0000790 // Parse attributes on the global.
791 while (Lex.getKind() == lltok::comma) {
792 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000793
Chris Lattnerac161bf2009-01-02 07:01:27 +0000794 if (Lex.getKind() == lltok::kw_section) {
795 Lex.Lex();
796 GV->setSection(Lex.getStrVal());
797 if (ParseToken(lltok::StringConstant, "expected global section string"))
798 return true;
799 } else if (Lex.getKind() == lltok::kw_align) {
800 unsigned Alignment;
801 if (ParseOptionalAlignment(Alignment)) return true;
802 GV->setAlignment(Alignment);
803 } else {
804 TokError("unknown global variable property!");
805 }
806 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000807
Chris Lattnerac161bf2009-01-02 07:01:27 +0000808 return false;
809}
810
Bill Wendling63b88192013-02-06 06:52:58 +0000811/// ParseUnnamedAttrGrp
Bill Wendlinga7c38772013-02-09 15:48:49 +0000812/// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
Bill Wendling63b88192013-02-06 06:52:58 +0000813bool LLParser::ParseUnnamedAttrGrp() {
Bill Wendlinga7c38772013-02-09 15:48:49 +0000814 assert(Lex.getKind() == lltok::kw_attributes);
Bill Wendling63b88192013-02-06 06:52:58 +0000815 LocTy AttrGrpLoc = Lex.getLoc();
Bill Wendlinga7c38772013-02-09 15:48:49 +0000816 Lex.Lex();
817
818 assert(Lex.getKind() == lltok::AttrGrpID);
Bill Wendling63b88192013-02-06 06:52:58 +0000819 unsigned VarID = Lex.getUIntVal();
Bill Wendlingb32b0412013-02-08 06:32:06 +0000820 std::vector<unsigned> unused;
Michael Gottesman41748d72013-06-27 00:25:01 +0000821 LocTy BuiltinLoc;
Bill Wendling63b88192013-02-06 06:52:58 +0000822 Lex.Lex();
823
824 if (ParseToken(lltok::equal, "expected '=' here") ||
Bill Wendling63b88192013-02-06 06:52:58 +0000825 ParseToken(lltok::lbrace, "expected '{' here") ||
Bill Wendling09bd1f72013-02-22 00:12:35 +0000826 ParseFnAttributeValuePairs(NumberedAttrBuilders[VarID], unused, true,
Michael Gottesman41748d72013-06-27 00:25:01 +0000827 BuiltinLoc) ||
Bill Wendling63b88192013-02-06 06:52:58 +0000828 ParseToken(lltok::rbrace, "expected end of attribute group"))
829 return true;
830
Bill Wendlingb32b0412013-02-08 06:32:06 +0000831 if (!NumberedAttrBuilders[VarID].hasAttributes())
Bill Wendling63b88192013-02-06 06:52:58 +0000832 return Error(AttrGrpLoc, "attribute group has no attributes");
833
834 return false;
835}
836
Bill Wendling8b0321d2013-02-08 00:52:31 +0000837/// ParseFnAttributeValuePairs
Bill Wendling63b88192013-02-06 06:52:58 +0000838/// ::= <attr> | <attr> '=' <value>
Bill Wendlingb32b0412013-02-08 06:32:06 +0000839bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B,
840 std::vector<unsigned> &FwdRefAttrGrps,
Michael Gottesman41748d72013-06-27 00:25:01 +0000841 bool inAttrGrp, LocTy &BuiltinLoc) {
Bill Wendling8b0321d2013-02-08 00:52:31 +0000842 bool HaveError = false;
843
844 B.clear();
845
Bill Wendling63b88192013-02-06 06:52:58 +0000846 while (true) {
847 lltok::Kind Token = Lex.getKind();
Michael Gottesman41748d72013-06-27 00:25:01 +0000848 if (Token == lltok::kw_builtin)
849 BuiltinLoc = Lex.getLoc();
Bill Wendling63b88192013-02-06 06:52:58 +0000850 switch (Token) {
851 default:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000852 if (!inAttrGrp) return HaveError;
Bill Wendling63b88192013-02-06 06:52:58 +0000853 return Error(Lex.getLoc(), "unterminated attribute group");
854 case lltok::rbrace:
855 // Finished.
856 return false;
857
Bill Wendlingb32b0412013-02-08 06:32:06 +0000858 case lltok::AttrGrpID: {
859 // Allow a function to reference an attribute group:
860 //
861 // define void @foo() #1 { ... }
862 if (inAttrGrp)
863 HaveError |=
864 Error(Lex.getLoc(),
865 "cannot have an attribute group reference in an attribute group");
866
867 unsigned AttrGrpNum = Lex.getUIntVal();
868 if (inAttrGrp) break;
869
870 // Save the reference to the attribute group. We'll fill it in later.
871 FwdRefAttrGrps.push_back(AttrGrpNum);
872 break;
873 }
Bill Wendling63b88192013-02-06 06:52:58 +0000874 // Target-dependent attributes:
875 case lltok::StringConstant: {
876 std::string Attr = Lex.getStrVal();
877 Lex.Lex();
878 std::string Val;
879 if (EatIfPresent(lltok::equal) &&
880 ParseStringConstant(Val))
881 return true;
882
883 B.addAttribute(Attr, Val);
Bill Wendlingb1ea9802013-02-10 10:12:50 +0000884 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000885 }
886
887 // Target-independent attributes:
888 case lltok::kw_align: {
Bill Wendlingc62789f2013-04-18 18:30:16 +0000889 // As a hack, we allow function alignment to be initially parsed as an
890 // attribute on a function declaration/definition or added to an attribute
891 // group and later moved to the alignment field.
Bill Wendling63b88192013-02-06 06:52:58 +0000892 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000893 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000894 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000895 if (ParseToken(lltok::equal, "expected '=' here") ||
896 ParseUInt32(Alignment))
897 return true;
898 } else {
899 if (ParseOptionalAlignment(Alignment))
900 return true;
901 }
Bill Wendling63b88192013-02-06 06:52:58 +0000902 B.addAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000903 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000904 }
905 case lltok::kw_alignstack: {
906 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000907 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000908 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000909 if (ParseToken(lltok::equal, "expected '=' here") ||
910 ParseUInt32(Alignment))
911 return true;
912 } else {
913 if (ParseOptionalStackAlignment(Alignment))
914 return true;
915 }
Bill Wendling63b88192013-02-06 06:52:58 +0000916 B.addStackAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000917 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000918 }
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000919 case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break;
Michael Gottesman41748d72013-06-27 00:25:01 +0000920 case lltok::kw_builtin: B.addAttribute(Attribute::Builtin); break;
Diego Novilloc6399532013-05-24 12:26:52 +0000921 case lltok::kw_cold: B.addAttribute(Attribute::Cold); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000922 case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break;
923 case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break;
924 case lltok::kw_naked: B.addAttribute(Attribute::Naked); break;
925 case lltok::kw_nobuiltin: B.addAttribute(Attribute::NoBuiltin); break;
926 case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break;
927 case lltok::kw_noimplicitfloat: B.addAttribute(Attribute::NoImplicitFloat); break;
928 case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break;
929 case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break;
930 case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break;
931 case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break;
932 case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000933 case lltok::kw_optnone: B.addAttribute(Attribute::OptimizeNone); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000934 case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break;
935 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
936 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
937 case lltok::kw_returns_twice: B.addAttribute(Attribute::ReturnsTwice); break;
938 case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break;
939 case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break;
940 case lltok::kw_sspstrong: B.addAttribute(Attribute::StackProtectStrong); break;
941 case lltok::kw_sanitize_address: B.addAttribute(Attribute::SanitizeAddress); break;
942 case lltok::kw_sanitize_thread: B.addAttribute(Attribute::SanitizeThread); break;
943 case lltok::kw_sanitize_memory: B.addAttribute(Attribute::SanitizeMemory); break;
944 case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000945
946 // Error handling.
947 case lltok::kw_inreg:
948 case lltok::kw_signext:
949 case lltok::kw_zeroext:
950 HaveError |=
951 Error(Lex.getLoc(),
952 "invalid use of attribute on a function");
953 break;
954 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +0000955 case lltok::kw_inalloca:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000956 case lltok::kw_nest:
957 case lltok::kw_noalias:
958 case lltok::kw_nocapture:
Stephen Linb8bd2322013-04-20 05:14:40 +0000959 case lltok::kw_returned:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000960 case lltok::kw_sret:
961 HaveError |=
962 Error(Lex.getLoc(),
963 "invalid use of parameter-only attribute on a function");
964 break;
Bill Wendling63b88192013-02-06 06:52:58 +0000965 }
966
967 Lex.Lex();
968 }
969}
Chris Lattnerac161bf2009-01-02 07:01:27 +0000970
971//===----------------------------------------------------------------------===//
972// GlobalValue Reference/Resolution Routines.
973//===----------------------------------------------------------------------===//
974
975/// GetGlobalVal - Get a value with the specified name or ID, creating a
976/// forward reference record if needed. This can return null if the value
977/// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +0000978GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +0000979 LocTy Loc) {
Chris Lattner229907c2011-07-18 04:54:35 +0000980 PointerType *PTy = dyn_cast<PointerType>(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000981 if (PTy == 0) {
982 Error(Loc, "global variable reference must have pointer type");
983 return 0;
984 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000985
Chris Lattnerac161bf2009-01-02 07:01:27 +0000986 // Look this name up in the normal function symbol table.
987 GlobalValue *Val =
988 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000989
Chris Lattnerac161bf2009-01-02 07:01:27 +0000990 // If this is a forward reference for the value, see if we already created a
991 // forward ref record.
992 if (Val == 0) {
993 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
994 I = ForwardRefVals.find(Name);
995 if (I != ForwardRefVals.end())
996 Val = I->second.first;
997 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000998
Chris Lattnerac161bf2009-01-02 07:01:27 +0000999 // If we have the value in the symbol table or fwd-ref table, return it.
1000 if (Val) {
1001 if (Val->getType() == Ty) return Val;
1002 Error(Loc, "'@" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001003 getTypeString(Val->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001004 return 0;
1005 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001006
Chris Lattnerac161bf2009-01-02 07:01:27 +00001007 // Otherwise, create a new forward reference for this value and remember it.
1008 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001009 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001010 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001011 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001012 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Justin Holewinski898a0a02012-11-16 21:03:47 +00001013 GlobalValue::ExternalWeakLinkage, 0, Name,
1014 0, GlobalVariable::NotThreadLocal,
1015 PTy->getAddressSpace());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001016
Chris Lattnerac161bf2009-01-02 07:01:27 +00001017 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1018 return FwdVal;
1019}
1020
Chris Lattner229907c2011-07-18 04:54:35 +00001021GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
1022 PointerType *PTy = dyn_cast<PointerType>(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001023 if (PTy == 0) {
1024 Error(Loc, "global variable reference must have pointer type");
1025 return 0;
1026 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001027
Chris Lattnerac161bf2009-01-02 07:01:27 +00001028 GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001029
Chris Lattnerac161bf2009-01-02 07:01:27 +00001030 // If this is a forward reference for the value, see if we already created a
1031 // forward ref record.
1032 if (Val == 0) {
1033 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
1034 I = ForwardRefValIDs.find(ID);
1035 if (I != ForwardRefValIDs.end())
1036 Val = I->second.first;
1037 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001038
Chris Lattnerac161bf2009-01-02 07:01:27 +00001039 // If we have the value in the symbol table or fwd-ref table, return it.
1040 if (Val) {
1041 if (Val->getType() == Ty) return Val;
Benjamin Kramerc7583112010-09-27 17:42:11 +00001042 Error(Loc, "'@" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001043 getTypeString(Val->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001044 return 0;
1045 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001046
Chris Lattnerac161bf2009-01-02 07:01:27 +00001047 // Otherwise, create a new forward reference for this value and remember it.
1048 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001049 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001050 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001051 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001052 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
1053 GlobalValue::ExternalWeakLinkage, 0, "");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001054
Chris Lattnerac161bf2009-01-02 07:01:27 +00001055 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1056 return FwdVal;
1057}
1058
1059
1060//===----------------------------------------------------------------------===//
1061// Helper Routines.
1062//===----------------------------------------------------------------------===//
1063
1064/// ParseToken - If the current token has the specified kind, eat it and return
1065/// success. Otherwise, emit the specified error and return failure.
1066bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
1067 if (Lex.getKind() != T)
1068 return TokError(ErrMsg);
1069 Lex.Lex();
1070 return false;
1071}
1072
Chris Lattner3822f632009-01-02 08:05:26 +00001073/// ParseStringConstant
1074/// ::= StringConstant
1075bool LLParser::ParseStringConstant(std::string &Result) {
1076 if (Lex.getKind() != lltok::StringConstant)
1077 return TokError("expected string constant");
1078 Result = Lex.getStrVal();
1079 Lex.Lex();
1080 return false;
1081}
1082
1083/// ParseUInt32
1084/// ::= uint32
1085bool LLParser::ParseUInt32(unsigned &Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001086 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1087 return TokError("expected integer");
1088 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
1089 if (Val64 != unsigned(Val64))
1090 return TokError("expected 32-bit integer (too large)");
1091 Val = Val64;
1092 Lex.Lex();
1093 return false;
1094}
1095
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001096/// ParseTLSModel
1097/// := 'localdynamic'
1098/// := 'initialexec'
1099/// := 'localexec'
1100bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
1101 switch (Lex.getKind()) {
1102 default:
1103 return TokError("expected localdynamic, initialexec or localexec");
1104 case lltok::kw_localdynamic:
1105 TLM = GlobalVariable::LocalDynamicTLSModel;
1106 break;
1107 case lltok::kw_initialexec:
1108 TLM = GlobalVariable::InitialExecTLSModel;
1109 break;
1110 case lltok::kw_localexec:
1111 TLM = GlobalVariable::LocalExecTLSModel;
1112 break;
1113 }
1114
1115 Lex.Lex();
1116 return false;
1117}
1118
1119/// ParseOptionalThreadLocal
1120/// := /*empty*/
1121/// := 'thread_local'
1122/// := 'thread_local' '(' tlsmodel ')'
1123bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
1124 TLM = GlobalVariable::NotThreadLocal;
1125 if (!EatIfPresent(lltok::kw_thread_local))
1126 return false;
1127
1128 TLM = GlobalVariable::GeneralDynamicTLSModel;
1129 if (Lex.getKind() == lltok::lparen) {
1130 Lex.Lex();
1131 return ParseTLSModel(TLM) ||
1132 ParseToken(lltok::rparen, "expected ')' after thread local model");
1133 }
1134 return false;
1135}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001136
1137/// ParseOptionalAddrSpace
1138/// := /*empty*/
1139/// := 'addrspace' '(' uint32 ')'
1140bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
1141 AddrSpace = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001142 if (!EatIfPresent(lltok::kw_addrspace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001143 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001144 return ParseToken(lltok::lparen, "expected '(' in address space") ||
Chris Lattner3822f632009-01-02 08:05:26 +00001145 ParseUInt32(AddrSpace) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00001146 ParseToken(lltok::rparen, "expected ')' in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001147}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001148
Bill Wendling34c2eb22012-12-04 23:40:58 +00001149/// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes.
1150bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
1151 bool HaveError = false;
1152
1153 B.clear();
1154
1155 while (1) {
1156 lltok::Kind Token = Lex.getKind();
1157 switch (Token) {
1158 default: // End of attributes.
1159 return HaveError;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001160 case lltok::kw_align: {
1161 unsigned Alignment;
1162 if (ParseOptionalAlignment(Alignment))
1163 return true;
Bill Wendling68d24012012-10-08 22:20:14 +00001164 B.addAlignmentAttr(Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001165 continue;
1166 }
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001167 case lltok::kw_byval: B.addAttribute(Attribute::ByVal); break;
Reid Klecknera534a382013-12-19 02:14:12 +00001168 case lltok::kw_inalloca: B.addAttribute(Attribute::InAlloca); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001169 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1170 case lltok::kw_nest: B.addAttribute(Attribute::Nest); break;
1171 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1172 case lltok::kw_nocapture: B.addAttribute(Attribute::NoCapture); break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001173 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
1174 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
Stephen Linb8bd2322013-04-20 05:14:40 +00001175 case lltok::kw_returned: B.addAttribute(Attribute::Returned); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001176 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1177 case lltok::kw_sret: B.addAttribute(Attribute::StructRet); break;
1178 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Charles Davisbe5557e2010-02-12 00:31:15 +00001179
Stephen Lin7577ed52013-04-20 13:16:13 +00001180 case lltok::kw_alignstack:
1181 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001182 case lltok::kw_builtin:
Stephen Lin7577ed52013-04-20 13:16:13 +00001183 case lltok::kw_inlinehint:
1184 case lltok::kw_minsize:
1185 case lltok::kw_naked:
1186 case lltok::kw_nobuiltin:
1187 case lltok::kw_noduplicate:
1188 case lltok::kw_noimplicitfloat:
1189 case lltok::kw_noinline:
1190 case lltok::kw_nonlazybind:
1191 case lltok::kw_noredzone:
1192 case lltok::kw_noreturn:
1193 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001194 case lltok::kw_optnone:
Stephen Lin7577ed52013-04-20 13:16:13 +00001195 case lltok::kw_optsize:
Stephen Lin7577ed52013-04-20 13:16:13 +00001196 case lltok::kw_returns_twice:
1197 case lltok::kw_sanitize_address:
1198 case lltok::kw_sanitize_memory:
1199 case lltok::kw_sanitize_thread:
1200 case lltok::kw_ssp:
1201 case lltok::kw_sspreq:
1202 case lltok::kw_sspstrong:
1203 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001204 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
1205 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001206 }
Bill Wendling0be9c402012-09-28 22:30:18 +00001207
Bill Wendling34c2eb22012-12-04 23:40:58 +00001208 Lex.Lex();
1209 }
1210}
1211
1212/// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes.
1213bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) {
1214 bool HaveError = false;
1215
1216 B.clear();
1217
1218 while (1) {
1219 lltok::Kind Token = Lex.getKind();
Bill Wendling0be9c402012-09-28 22:30:18 +00001220 switch (Token) {
Bill Wendling34c2eb22012-12-04 23:40:58 +00001221 default: // End of attributes.
1222 return HaveError;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001223 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1224 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1225 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1226 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Bill Wendling0be9c402012-09-28 22:30:18 +00001227
Bill Wendling34c2eb22012-12-04 23:40:58 +00001228 // Error handling.
Stephen Lin7577ed52013-04-20 13:16:13 +00001229 case lltok::kw_align:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001230 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +00001231 case lltok::kw_inalloca:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001232 case lltok::kw_nest:
1233 case lltok::kw_nocapture:
Stephen Linb8bd2322013-04-20 05:14:40 +00001234 case lltok::kw_returned:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001235 case lltok::kw_sret:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001236 HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001237 break;
James Molloy4f6fb952012-12-20 16:04:27 +00001238
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001239 case lltok::kw_alignstack:
1240 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001241 case lltok::kw_builtin:
Diego Novilloc6399532013-05-24 12:26:52 +00001242 case lltok::kw_cold:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001243 case lltok::kw_inlinehint:
1244 case lltok::kw_minsize:
1245 case lltok::kw_naked:
1246 case lltok::kw_nobuiltin:
1247 case lltok::kw_noduplicate:
1248 case lltok::kw_noimplicitfloat:
1249 case lltok::kw_noinline:
1250 case lltok::kw_nonlazybind:
1251 case lltok::kw_noredzone:
1252 case lltok::kw_noreturn:
1253 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001254 case lltok::kw_optnone:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001255 case lltok::kw_optsize:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001256 case lltok::kw_returns_twice:
1257 case lltok::kw_sanitize_address:
1258 case lltok::kw_sanitize_memory:
1259 case lltok::kw_sanitize_thread:
1260 case lltok::kw_ssp:
1261 case lltok::kw_sspreq:
1262 case lltok::kw_sspstrong:
1263 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001264 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001265 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001266
1267 case lltok::kw_readnone:
1268 case lltok::kw_readonly:
1269 HaveError |= Error(Lex.getLoc(), "invalid use of attribute on return type");
Bill Wendling0be9c402012-09-28 22:30:18 +00001270 }
1271
Chris Lattnerac161bf2009-01-02 07:01:27 +00001272 Lex.Lex();
1273 }
1274}
1275
1276/// ParseOptionalLinkage
1277/// ::= /*empty*/
Rafael Espindola6de96a12009-01-15 20:18:42 +00001278/// ::= 'private'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001279/// ::= 'internal'
1280/// ::= 'weak'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001281/// ::= 'weak_odr'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001282/// ::= 'linkonce'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001283/// ::= 'linkonce_odr'
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001284/// ::= 'available_externally'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001285/// ::= 'appending'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001286/// ::= 'common'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001287/// ::= 'extern_weak'
1288/// ::= 'external'
1289bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) {
1290 HasLinkage = false;
1291 switch (Lex.getKind()) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001292 default: Res=GlobalValue::ExternalLinkage; return false;
1293 case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001294 case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break;
1295 case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break;
1296 case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break;
1297 case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break;
1298 case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001299 case lltok::kw_available_externally:
1300 Res = GlobalValue::AvailableExternallyLinkage;
1301 break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001302 case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001303 case lltok::kw_common: Res = GlobalValue::CommonLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001304 case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break;
1305 case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001306 }
1307 Lex.Lex();
1308 HasLinkage = true;
1309 return false;
1310}
1311
1312/// ParseOptionalVisibility
1313/// ::= /*empty*/
1314/// ::= 'default'
1315/// ::= 'hidden'
1316/// ::= 'protected'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001317///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001318bool LLParser::ParseOptionalVisibility(unsigned &Res) {
1319 switch (Lex.getKind()) {
1320 default: Res = GlobalValue::DefaultVisibility; return false;
1321 case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break;
1322 case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break;
1323 case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break;
1324 }
1325 Lex.Lex();
1326 return false;
1327}
1328
Nico Rieck7157bb72014-01-14 15:22:47 +00001329/// ParseOptionalDLLStorageClass
1330/// ::= /*empty*/
1331/// ::= 'dllimport'
1332/// ::= 'dllexport'
1333///
1334bool LLParser::ParseOptionalDLLStorageClass(unsigned &Res) {
1335 switch (Lex.getKind()) {
1336 default: Res = GlobalValue::DefaultStorageClass; return false;
1337 case lltok::kw_dllimport: Res = GlobalValue::DLLImportStorageClass; break;
1338 case lltok::kw_dllexport: Res = GlobalValue::DLLExportStorageClass; break;
1339 }
1340 Lex.Lex();
1341 return false;
1342}
1343
Chris Lattnerac161bf2009-01-02 07:01:27 +00001344/// ParseOptionalCallingConv
1345/// ::= /*empty*/
1346/// ::= 'ccc'
1347/// ::= 'fastcc'
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001348/// ::= 'kw_intel_ocl_bicc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001349/// ::= 'coldcc'
1350/// ::= 'x86_stdcallcc'
1351/// ::= 'x86_fastcallcc'
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001352/// ::= 'x86_thiscallcc'
Reid Kleckner1c843222014-01-31 17:41:22 +00001353/// ::= 'x86_cdeclmethodcc'
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001354/// ::= 'arm_apcscc'
1355/// ::= 'arm_aapcscc'
1356/// ::= 'arm_aapcs_vfpcc'
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001357/// ::= 'msp430_intrcc'
Che-Liang Chiou29947902010-09-25 07:46:17 +00001358/// ::= 'ptx_kernel'
1359/// ::= 'ptx_device'
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001360/// ::= 'spir_func'
1361/// ::= 'spir_kernel'
Charles Davise8f297c2013-07-12 06:02:35 +00001362/// ::= 'x86_64_sysvcc'
1363/// ::= 'x86_64_win64cc'
Andrew Tricka3a11de2013-10-31 22:12:01 +00001364/// ::= 'webkit_jscc'
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001365/// ::= 'anyregcc'
Juergen Ributzkae6250132014-01-17 19:47:03 +00001366/// ::= 'preserve_mostcc'
1367/// ::= 'preserve_allcc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001368/// ::= 'cc' UINT
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001369///
Sandeep Patel68c5f472009-09-02 08:44:58 +00001370bool LLParser::ParseOptionalCallingConv(CallingConv::ID &CC) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001371 switch (Lex.getKind()) {
1372 default: CC = CallingConv::C; return false;
1373 case lltok::kw_ccc: CC = CallingConv::C; break;
1374 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
1375 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
1376 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
1377 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001378 case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
Reid Kleckner1c843222014-01-31 17:41:22 +00001379 case lltok::kw_x86_cdeclmethodcc:CC = CallingConv::X86_CDeclMethod; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001380 case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break;
1381 case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break;
1382 case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001383 case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
Che-Liang Chiou29947902010-09-25 07:46:17 +00001384 case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break;
1385 case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break;
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001386 case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break;
1387 case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break;
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001388 case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break;
Charles Davise8f297c2013-07-12 06:02:35 +00001389 case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break;
1390 case lltok::kw_x86_64_win64cc: CC = CallingConv::X86_64_Win64; break;
Andrew Tricka3a11de2013-10-31 22:12:01 +00001391 case lltok::kw_webkit_jscc: CC = CallingConv::WebKit_JS; break;
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001392 case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break;
Juergen Ributzkae6250132014-01-17 19:47:03 +00001393 case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break;
1394 case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001395 case lltok::kw_cc: {
1396 unsigned ArbitraryCC;
1397 Lex.Lex();
David Blaikie46a9f012012-01-20 21:51:11 +00001398 if (ParseUInt32(ArbitraryCC))
Sandeep Patel68c5f472009-09-02 08:44:58 +00001399 return true;
David Blaikie46a9f012012-01-20 21:51:11 +00001400 CC = static_cast<CallingConv::ID>(ArbitraryCC);
1401 return false;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001402 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00001403 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001404
Chris Lattnerac161bf2009-01-02 07:01:27 +00001405 Lex.Lex();
1406 return false;
1407}
1408
Chris Lattner5c427632009-12-30 05:31:19 +00001409/// ParseInstructionMetadata
Chris Lattner596760d2009-12-29 21:25:40 +00001410/// ::= !dbg !42 (',' !dbg !57)*
Dan Gohman338d9a42010-08-24 02:05:17 +00001411bool LLParser::ParseInstructionMetadata(Instruction *Inst,
1412 PerFunctionState *PFS) {
Chris Lattner5c427632009-12-30 05:31:19 +00001413 do {
1414 if (Lex.getKind() != lltok::MetadataVar)
1415 return TokError("expected metadata after comma");
Devang Patelba4a6fd2009-09-29 00:01:14 +00001416
Chris Lattner596760d2009-12-29 21:25:40 +00001417 std::string Name = Lex.getStrVal();
Benjamin Kramerb3bd0192011-12-06 11:50:26 +00001418 unsigned MDK = M->getMDKindID(Name);
Chris Lattner596760d2009-12-29 21:25:40 +00001419 Lex.Lex();
Chris Lattner8d58f2f2009-10-19 05:31:10 +00001420
Chris Lattner1797fc72009-12-29 21:53:55 +00001421 MDNode *Node;
Chris Lattner8eff0152010-04-01 05:14:45 +00001422 SMLoc Loc = Lex.getLoc();
Dan Gohmanc828c542010-08-24 02:24:03 +00001423
1424 if (ParseToken(lltok::exclaim, "expected '!' here"))
Chris Lattner1eed2d62009-12-30 04:56:59 +00001425 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001426
Dan Gohmanf0715b12010-08-24 14:35:45 +00001427 // This code is similar to that of ParseMetadataValue, however it needs to
1428 // have special-case code for a forward reference; see the comments on
1429 // ForwardRefInstMetadata for details. Also, MDStrings are not supported
1430 // at the top level here.
Dan Gohmanc828c542010-08-24 02:24:03 +00001431 if (Lex.getKind() == lltok::lbrace) {
1432 ValID ID;
1433 if (ParseMetadataListValue(ID, PFS))
1434 return true;
1435 assert(ID.Kind == ValID::t_MDNode);
1436 Inst->setMetadata(MDK, ID.MDNodeVal);
Chris Lattner8eff0152010-04-01 05:14:45 +00001437 } else {
Nick Lewycky912888f2010-09-30 21:04:13 +00001438 unsigned NodeID = 0;
Dan Gohmanc828c542010-08-24 02:24:03 +00001439 if (ParseMDNodeID(Node, NodeID))
1440 return true;
1441 if (Node) {
1442 // If we got the node, add it to the instruction.
1443 Inst->setMetadata(MDK, Node);
1444 } else {
1445 MDRef R = { Loc, MDK, NodeID };
1446 // Otherwise, remember that this should be resolved later.
1447 ForwardRefInstMetadata[Inst].push_back(R);
1448 }
Chris Lattner8eff0152010-04-01 05:14:45 +00001449 }
Chris Lattner596760d2009-12-29 21:25:40 +00001450
Manman Ren209b17c2013-09-28 00:22:27 +00001451 if (MDK == LLVMContext::MD_tbaa)
1452 InstsWithTBAATag.push_back(Inst);
1453
Chris Lattner596760d2009-12-29 21:25:40 +00001454 // If this is the end of the list, we're done.
Chris Lattner5c427632009-12-30 05:31:19 +00001455 } while (EatIfPresent(lltok::comma));
1456 return false;
Devang Patelea8a4b92009-09-17 23:04:48 +00001457}
1458
Chris Lattnerac161bf2009-01-02 07:01:27 +00001459/// ParseOptionalAlignment
1460/// ::= /* empty */
1461/// ::= 'align' 4
1462bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1463 Alignment = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001464 if (!EatIfPresent(lltok::kw_align))
1465 return false;
Chris Lattnerd11f5142009-01-05 07:46:05 +00001466 LocTy AlignLoc = Lex.getLoc();
1467 if (ParseUInt32(Alignment)) return true;
1468 if (!isPowerOf2_32(Alignment))
1469 return Error(AlignLoc, "alignment is not a power of two");
Dan Gohmand566d2c2010-07-30 21:07:05 +00001470 if (Alignment > Value::MaximumAlignment)
Dan Gohmana7e5a242010-07-28 20:12:04 +00001471 return Error(AlignLoc, "huge alignments are not supported yet");
Chris Lattnerd11f5142009-01-05 07:46:05 +00001472 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001473}
1474
Chris Lattnerb2f39502009-12-30 05:44:30 +00001475/// ParseOptionalCommaAlign
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001476/// ::=
Chris Lattnerb2f39502009-12-30 05:44:30 +00001477/// ::= ',' align 4
1478///
1479/// This returns with AteExtraComma set to true if it ate an excess comma at the
1480/// end.
1481bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
1482 bool &AteExtraComma) {
1483 AteExtraComma = false;
1484 while (EatIfPresent(lltok::comma)) {
1485 // Metadata at the end is an early exit.
Chris Lattnereafe4de2009-12-30 05:02:06 +00001486 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerb2f39502009-12-30 05:44:30 +00001487 AteExtraComma = true;
1488 return false;
1489 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001490
Chris Lattner95b0ff42010-04-23 00:50:50 +00001491 if (Lex.getKind() != lltok::kw_align)
1492 return Error(Lex.getLoc(), "expected metadata or 'align'");
Duncan Sands4c5c8582010-10-21 16:07:10 +00001493
Chris Lattner95b0ff42010-04-23 00:50:50 +00001494 if (ParseOptionalAlignment(Alignment)) return true;
Chris Lattnerb2f39502009-12-30 05:44:30 +00001495 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001496
Devang Patelea8a4b92009-09-17 23:04:48 +00001497 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001498}
1499
Eli Friedmanfee02c62011-07-25 23:16:38 +00001500/// ParseScopeAndOrdering
1501/// if isAtomic: ::= 'singlethread'? AtomicOrdering
1502/// else: ::=
1503///
1504/// This sets Scope and Ordering to the parsed values.
1505bool LLParser::ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
1506 AtomicOrdering &Ordering) {
1507 if (!isAtomic)
1508 return false;
1509
1510 Scope = CrossThread;
1511 if (EatIfPresent(lltok::kw_singlethread))
1512 Scope = SingleThread;
Tim Northovere94a5182014-03-11 10:48:52 +00001513
1514 return ParseOrdering(Ordering);
1515}
1516
1517/// ParseOrdering
1518/// ::= AtomicOrdering
1519///
1520/// This sets Ordering to the parsed value.
1521bool LLParser::ParseOrdering(AtomicOrdering &Ordering) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001522 switch (Lex.getKind()) {
1523 default: return TokError("Expected ordering on atomic instruction");
1524 case lltok::kw_unordered: Ordering = Unordered; break;
1525 case lltok::kw_monotonic: Ordering = Monotonic; break;
1526 case lltok::kw_acquire: Ordering = Acquire; break;
1527 case lltok::kw_release: Ordering = Release; break;
1528 case lltok::kw_acq_rel: Ordering = AcquireRelease; break;
1529 case lltok::kw_seq_cst: Ordering = SequentiallyConsistent; break;
1530 }
1531 Lex.Lex();
1532 return false;
1533}
1534
Charles Davisbe5557e2010-02-12 00:31:15 +00001535/// ParseOptionalStackAlignment
1536/// ::= /* empty */
1537/// ::= 'alignstack' '(' 4 ')'
1538bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
1539 Alignment = 0;
1540 if (!EatIfPresent(lltok::kw_alignstack))
1541 return false;
1542 LocTy ParenLoc = Lex.getLoc();
1543 if (!EatIfPresent(lltok::lparen))
1544 return Error(ParenLoc, "expected '('");
1545 LocTy AlignLoc = Lex.getLoc();
1546 if (ParseUInt32(Alignment)) return true;
1547 ParenLoc = Lex.getLoc();
1548 if (!EatIfPresent(lltok::rparen))
1549 return Error(ParenLoc, "expected ')'");
1550 if (!isPowerOf2_32(Alignment))
1551 return Error(AlignLoc, "stack alignment is not a power of two");
1552 return false;
1553}
Devang Patelea8a4b92009-09-17 23:04:48 +00001554
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001555/// ParseIndexList - This parses the index list for an insert/extractvalue
1556/// instruction. This sets AteExtraComma in the case where we eat an extra
1557/// comma at the end of the line and find that it is followed by metadata.
1558/// Clients that don't allow metadata can call the version of this function that
1559/// only takes one argument.
1560///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001561/// ParseIndexList
1562/// ::= (',' uint32)+
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001563///
1564bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
1565 bool &AteExtraComma) {
1566 AteExtraComma = false;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001567
Chris Lattnerac161bf2009-01-02 07:01:27 +00001568 if (Lex.getKind() != lltok::comma)
1569 return TokError("expected ',' as start of index list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001570
Chris Lattner3822f632009-01-02 08:05:26 +00001571 while (EatIfPresent(lltok::comma)) {
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001572 if (Lex.getKind() == lltok::MetadataVar) {
1573 AteExtraComma = true;
1574 return false;
1575 }
Nick Lewycky83e47112010-09-29 23:32:20 +00001576 unsigned Idx = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001577 if (ParseUInt32(Idx)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001578 Indices.push_back(Idx);
1579 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001580
Chris Lattnerac161bf2009-01-02 07:01:27 +00001581 return false;
1582}
1583
1584//===----------------------------------------------------------------------===//
1585// Type Parsing.
1586//===----------------------------------------------------------------------===//
1587
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001588/// ParseType - Parse a type.
1589bool LLParser::ParseType(Type *&Result, bool AllowVoid) {
1590 SMLoc TypeLoc = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001591 switch (Lex.getKind()) {
1592 default:
1593 return TokError("expected type");
1594 case lltok::Type:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001595 // Type ::= 'float' | 'void' (etc)
Chris Lattnerac161bf2009-01-02 07:01:27 +00001596 Result = Lex.getTyVal();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001597 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001598 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001599 case lltok::lbrace:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001600 // Type ::= StructType
1601 if (ParseAnonStructType(Result, false))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001602 return true;
1603 break;
1604 case lltok::lsquare:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001605 // Type ::= '[' ... ']'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001606 Lex.Lex(); // eat the lsquare.
1607 if (ParseArrayVectorType(Result, false))
1608 return true;
1609 break;
1610 case lltok::less: // Either vector or packed struct.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001611 // Type ::= '<' ... '>'
Chris Lattner3822f632009-01-02 08:05:26 +00001612 Lex.Lex();
1613 if (Lex.getKind() == lltok::lbrace) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001614 if (ParseAnonStructType(Result, true) ||
Chris Lattner3822f632009-01-02 08:05:26 +00001615 ParseToken(lltok::greater, "expected '>' at end of packed struct"))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001616 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001617 } else if (ParseArrayVectorType(Result, true))
1618 return true;
1619 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001620 case lltok::LocalVar: {
1621 // Type ::= %foo
1622 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001623
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001624 // If the type hasn't been defined yet, create a forward definition and
1625 // remember where that forward def'n was seen (in case it never is defined).
1626 if (Entry.first == 0) {
Chris Lattner335d3992011-08-12 18:06:37 +00001627 Entry.first = StructType::create(Context, Lex.getStrVal());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001628 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001629 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001630 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001631 Lex.Lex();
1632 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001633 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001634
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001635 case lltok::LocalVarID: {
1636 // Type ::= %4
1637 if (Lex.getUIntVal() >= NumberedTypes.size())
1638 NumberedTypes.resize(Lex.getUIntVal()+1);
1639 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001640
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001641 // If the type hasn't been defined yet, create a forward definition and
1642 // remember where that forward def'n was seen (in case it never is defined).
1643 if (Entry.first == 0) {
Chris Lattner335d3992011-08-12 18:06:37 +00001644 Entry.first = StructType::create(Context);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001645 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001646 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001647 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001648 Lex.Lex();
1649 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001650 }
1651 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001652
1653 // Parse the type suffixes.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001654 while (1) {
1655 switch (Lex.getKind()) {
1656 // End of type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001657 default:
1658 if (!AllowVoid && Result->isVoidTy())
1659 return Error(TypeLoc, "void type only allowed for function results");
1660 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001661
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001662 // Type ::= Type '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001663 case lltok::star:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001664 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001665 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001666 if (Result->isVoidTy())
1667 return TokError("pointers to void are invalid - use i8* instead");
1668 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001669 return TokError("pointer to this type is invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001670 Result = PointerType::getUnqual(Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001671 Lex.Lex();
1672 break;
1673
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001674 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001675 case lltok::kw_addrspace: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001676 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001677 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001678 if (Result->isVoidTy())
Dan Gohman9280a682009-02-09 17:41:21 +00001679 return TokError("pointers to void are invalid; use i8* instead");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001680 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001681 return TokError("pointer to this type is invalid");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001682 unsigned AddrSpace;
1683 if (ParseOptionalAddrSpace(AddrSpace) ||
1684 ParseToken(lltok::star, "expected '*' in address space"))
1685 return true;
1686
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001687 Result = PointerType::get(Result, AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001688 break;
1689 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001690
Chris Lattnerac161bf2009-01-02 07:01:27 +00001691 /// Types '(' ArgTypeListI ')' OptFuncAttrs
1692 case lltok::lparen:
1693 if (ParseFunctionType(Result))
1694 return true;
1695 break;
1696 }
1697 }
1698}
1699
1700/// ParseParameterList
1701/// ::= '(' ')'
1702/// ::= '(' Arg (',' Arg)* ')'
1703/// Arg
1704/// ::= Type OptionalAttributes Value OptionalAttributes
1705bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
1706 PerFunctionState &PFS) {
1707 if (ParseToken(lltok::lparen, "expected '(' in call"))
1708 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001709
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001710 unsigned AttrIndex = 1;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001711 while (Lex.getKind() != lltok::rparen) {
1712 // If this isn't the first argument, we need a comma.
1713 if (!ArgList.empty() &&
1714 ParseToken(lltok::comma, "expected ',' in argument list"))
1715 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001716
Chris Lattnerac161bf2009-01-02 07:01:27 +00001717 // Parse the argument.
1718 LocTy ArgLoc;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001719 Type *ArgTy = 0;
Bill Wendling50d27842012-10-15 20:35:56 +00001720 AttrBuilder ArgAttrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001721 Value *V;
Victor Hernandezfa232232009-12-03 23:40:58 +00001722 if (ParseType(ArgTy, ArgLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001723 return true;
Victor Hernandezfa232232009-12-03 23:40:58 +00001724
Chris Lattner5b4a9622009-12-30 02:11:14 +00001725 // Otherwise, handle normal operands.
Bill Wendling34c2eb22012-12-04 23:40:58 +00001726 if (ParseOptionalParamAttrs(ArgAttrs) || ParseValue(ArgTy, V, PFS))
Chris Lattner5b4a9622009-12-30 02:11:14 +00001727 return true;
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001728 ArgList.push_back(ParamInfo(ArgLoc, V, AttributeSet::get(V->getContext(),
1729 AttrIndex++,
1730 ArgAttrs)));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001731 }
1732
1733 Lex.Lex(); // Lex the ')'.
1734 return false;
1735}
1736
1737
1738
Chris Lattner2ed06b42009-01-05 18:34:07 +00001739/// ParseArgumentList - Parse the argument list for a function type or function
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001740/// prototype.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001741/// ::= '(' ArgTypeListI ')'
1742/// ArgTypeListI
1743/// ::= /*empty*/
1744/// ::= '...'
1745/// ::= ArgTypeList ',' '...'
1746/// ::= ArgType (',' ArgType)*
Chris Lattner2ed06b42009-01-05 18:34:07 +00001747///
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001748bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
1749 bool &isVarArg){
Chris Lattnerac161bf2009-01-02 07:01:27 +00001750 isVarArg = false;
1751 assert(Lex.getKind() == lltok::lparen);
1752 Lex.Lex(); // eat the (.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001753
Chris Lattnerac161bf2009-01-02 07:01:27 +00001754 if (Lex.getKind() == lltok::rparen) {
1755 // empty
1756 } else if (Lex.getKind() == lltok::dotdotdot) {
1757 isVarArg = true;
1758 Lex.Lex();
1759 } else {
1760 LocTy TypeLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001761 Type *ArgTy = 0;
Bill Wendling50d27842012-10-15 20:35:56 +00001762 AttrBuilder Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001763 std::string Name;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001764
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001765 if (ParseType(ArgTy) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00001766 ParseOptionalParamAttrs(Attrs)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001767
Chris Lattnerfdd87902009-10-05 05:54:46 +00001768 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001769 return Error(TypeLoc, "argument can not have void type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001770
Chris Lattnerdef19492011-06-17 06:36:20 +00001771 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001772 Name = Lex.getStrVal();
1773 Lex.Lex();
1774 }
Chris Lattner3822f632009-01-02 08:05:26 +00001775
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001776 if (!FunctionType::isValidArgumentType(ArgTy))
Chris Lattner3822f632009-01-02 08:05:26 +00001777 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001778
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001779 unsigned AttrIndex = 1;
Bill Wendlingd079a442012-10-15 04:46:55 +00001780 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001781 AttributeSet::get(ArgTy->getContext(),
1782 AttrIndex++, Attrs), Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001783
Chris Lattner3822f632009-01-02 08:05:26 +00001784 while (EatIfPresent(lltok::comma)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001785 // Handle ... at end of arg list.
Chris Lattner3822f632009-01-02 08:05:26 +00001786 if (EatIfPresent(lltok::dotdotdot)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001787 isVarArg = true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001788 break;
1789 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001790
Chris Lattnerac161bf2009-01-02 07:01:27 +00001791 // Otherwise must be an argument type.
1792 TypeLoc = Lex.getLoc();
Bill Wendling34c2eb22012-12-04 23:40:58 +00001793 if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) return true;
Chris Lattner3822f632009-01-02 08:05:26 +00001794
Chris Lattnerfdd87902009-10-05 05:54:46 +00001795 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001796 return Error(TypeLoc, "argument can not have void type");
1797
Chris Lattnerdef19492011-06-17 06:36:20 +00001798 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001799 Name = Lex.getStrVal();
1800 Lex.Lex();
1801 } else {
1802 Name = "";
1803 }
Chris Lattner3822f632009-01-02 08:05:26 +00001804
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001805 if (!ArgTy->isFirstClassType())
Chris Lattner3822f632009-01-02 08:05:26 +00001806 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001807
Bill Wendlingd079a442012-10-15 04:46:55 +00001808 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001809 AttributeSet::get(ArgTy->getContext(),
1810 AttrIndex++, Attrs),
Bill Wendlingd079a442012-10-15 04:46:55 +00001811 Name));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001812 }
1813 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001814
Chris Lattner3822f632009-01-02 08:05:26 +00001815 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001816}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001817
Chris Lattnerac161bf2009-01-02 07:01:27 +00001818/// ParseFunctionType
1819/// ::= Type ArgumentList OptionalAttrs
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001820bool LLParser::ParseFunctionType(Type *&Result) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001821 assert(Lex.getKind() == lltok::lparen);
1822
Chris Lattnerce473c72009-01-05 08:04:33 +00001823 if (!FunctionType::isValidReturnType(Result))
1824 return TokError("invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001825
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001826 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001827 bool isVarArg;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001828 if (ParseArgumentList(ArgList, isVarArg))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001829 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001830
Chris Lattnerac161bf2009-01-02 07:01:27 +00001831 // Reject names on the arguments lists.
1832 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
1833 if (!ArgList[i].Name.empty())
1834 return Error(ArgList[i].Loc, "argument name invalid in function type");
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001835 if (ArgList[i].Attrs.hasAttributes(i + 1))
Chris Lattner6bc5c892011-06-17 17:37:13 +00001836 return Error(ArgList[i].Loc,
1837 "argument attributes invalid in function type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001838 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001839
Jay Foadb804a2b2011-07-12 14:06:48 +00001840 SmallVector<Type*, 16> ArgListTy;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001841 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001842 ArgListTy.push_back(ArgList[i].Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001843
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001844 Result = FunctionType::get(Result, ArgListTy, isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001845 return false;
1846}
1847
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001848/// ParseAnonStructType - Parse an anonymous struct type, which is inlined into
1849/// other structs.
1850bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) {
1851 SmallVector<Type*, 8> Elts;
1852 if (ParseStructBody(Elts)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001853
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001854 Result = StructType::get(Context, Elts, Packed);
1855 return false;
1856}
1857
1858/// ParseStructDefinition - Parse a struct in a 'type' definition.
1859bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
1860 std::pair<Type*, LocTy> &Entry,
1861 Type *&ResultTy) {
1862 // If the type was already defined, diagnose the redefinition.
1863 if (Entry.first && !Entry.second.isValid())
1864 return Error(TypeLoc, "redefinition of type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001865
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001866 // If we have opaque, just return without filling in the definition for the
1867 // struct. This counts as a definition as far as the .ll file goes.
1868 if (EatIfPresent(lltok::kw_opaque)) {
1869 // This type is being defined, so clear the location to indicate this.
1870 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001871
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001872 // If this type number has never been uttered, create it.
1873 if (Entry.first == 0)
Chris Lattner335d3992011-08-12 18:06:37 +00001874 Entry.first = StructType::create(Context, Name);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001875 ResultTy = Entry.first;
1876 return false;
1877 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001878
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001879 // If the type starts with '<', then it is either a packed struct or a vector.
1880 bool isPacked = EatIfPresent(lltok::less);
1881
1882 // If we don't have a struct, then we have a random type alias, which we
1883 // accept for compatibility with old files. These types are not allowed to be
1884 // forward referenced and not allowed to be recursive.
1885 if (Lex.getKind() != lltok::lbrace) {
1886 if (Entry.first)
1887 return Error(TypeLoc, "forward references to non-struct type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001888
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001889 ResultTy = 0;
1890 if (isPacked)
1891 return ParseArrayVectorType(ResultTy, true);
1892 return ParseType(ResultTy);
1893 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001894
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001895 // This type is being defined, so clear the location to indicate this.
1896 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001897
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001898 // If this type number has never been uttered, create it.
1899 if (Entry.first == 0)
Chris Lattner335d3992011-08-12 18:06:37 +00001900 Entry.first = StructType::create(Context, Name);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001901
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001902 StructType *STy = cast<StructType>(Entry.first);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001903
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001904 SmallVector<Type*, 8> Body;
1905 if (ParseStructBody(Body) ||
1906 (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct")))
1907 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001908
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001909 STy->setBody(Body, isPacked);
1910 ResultTy = STy;
1911 return false;
1912}
1913
1914
Chris Lattnerac161bf2009-01-02 07:01:27 +00001915/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001916/// StructType
Chris Lattnerac161bf2009-01-02 07:01:27 +00001917/// ::= '{' '}'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001918/// ::= '{' Type (',' Type)* '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001919/// ::= '<' '{' '}' '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001920/// ::= '<' '{' Type (',' Type)* '}' '>'
1921bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001922 assert(Lex.getKind() == lltok::lbrace);
1923 Lex.Lex(); // Consume the '{'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001924
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001925 // Handle the empty struct.
1926 if (EatIfPresent(lltok::rbrace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001927 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001928
Chris Lattnerf880ca22009-03-09 04:49:14 +00001929 LocTy EltTyLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001930 Type *Ty = 0;
1931 if (ParseType(Ty)) return true;
1932 Body.push_back(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001933
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001934 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001935 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001936
Chris Lattner3822f632009-01-02 08:05:26 +00001937 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf880ca22009-03-09 04:49:14 +00001938 EltTyLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001939 if (ParseType(Ty)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001940
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001941 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001942 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001943
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001944 Body.push_back(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001945 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001946
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001947 return ParseToken(lltok::rbrace, "expected '}' at end of struct");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001948}
1949
1950/// ParseArrayVectorType - Parse an array or vector type, assuming the first
1951/// token has already been consumed.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001952/// Type
Chris Lattnerac161bf2009-01-02 07:01:27 +00001953/// ::= '[' APSINTVAL 'x' Types ']'
1954/// ::= '<' APSINTVAL 'x' Types '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001955bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001956 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
1957 Lex.getAPSIntVal().getBitWidth() > 64)
1958 return TokError("expected number in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001959
Chris Lattnerac161bf2009-01-02 07:01:27 +00001960 LocTy SizeLoc = Lex.getLoc();
1961 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
Chris Lattner3822f632009-01-02 08:05:26 +00001962 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001963
Chris Lattner3822f632009-01-02 08:05:26 +00001964 if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
1965 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001966
1967 LocTy TypeLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001968 Type *EltTy = 0;
1969 if (ParseType(EltTy)) return true;
Chris Lattnerf880ca22009-03-09 04:49:14 +00001970
Chris Lattner3822f632009-01-02 08:05:26 +00001971 if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
1972 "expected end of sequential type"))
1973 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001974
Chris Lattnerac161bf2009-01-02 07:01:27 +00001975 if (isVector) {
Chris Lattnerbb1fe8a2009-02-28 18:12:41 +00001976 if (Size == 0)
1977 return Error(SizeLoc, "zero element vector is illegal");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001978 if ((unsigned)Size != Size)
1979 return Error(SizeLoc, "size too large for vector");
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001980 if (!VectorType::isValidElementType(EltTy))
Duncan Sandse6beec62012-11-13 12:59:33 +00001981 return Error(TypeLoc, "invalid vector element type");
Owen Anderson4056ca92009-07-29 22:17:13 +00001982 Result = VectorType::get(EltTy, unsigned(Size));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001983 } else {
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001984 if (!ArrayType::isValidElementType(EltTy))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001985 return Error(TypeLoc, "invalid array element type");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001986 Result = ArrayType::get(EltTy, Size);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001987 }
1988 return false;
1989}
1990
1991//===----------------------------------------------------------------------===//
1992// Function Semantic Analysis.
1993//===----------------------------------------------------------------------===//
1994
Chris Lattner3432c622009-10-28 03:39:23 +00001995LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
1996 int functionNumber)
1997 : P(p), F(f), FunctionNumber(functionNumber) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001998
1999 // Insert unnamed arguments into the NumberedVals list.
2000 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
2001 AI != E; ++AI)
2002 if (!AI->hasName())
2003 NumberedVals.push_back(AI);
2004}
2005
2006LLParser::PerFunctionState::~PerFunctionState() {
2007 // If there were any forward referenced non-basicblock values, delete them.
2008 for (std::map<std::string, std::pair<Value*, LocTy> >::iterator
2009 I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
2010 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002011 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002012 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002013 delete I->second.first;
2014 I->second.first = 0;
2015 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002016
Chris Lattnerac161bf2009-01-02 07:01:27 +00002017 for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2018 I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
2019 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002020 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002021 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002022 delete I->second.first;
2023 I->second.first = 0;
2024 }
2025}
2026
Chris Lattner3432c622009-10-28 03:39:23 +00002027bool LLParser::PerFunctionState::FinishFunction() {
2028 // Check to see if someone took the address of labels in this block.
2029 if (!P.ForwardRefBlockAddresses.empty()) {
2030 ValID FunctionID;
2031 if (!F.getName().empty()) {
2032 FunctionID.Kind = ValID::t_GlobalName;
2033 FunctionID.StrVal = F.getName();
2034 } else {
2035 FunctionID.Kind = ValID::t_GlobalID;
2036 FunctionID.UIntVal = FunctionNumber;
2037 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002038
Chris Lattner3432c622009-10-28 03:39:23 +00002039 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >::iterator
2040 FRBAI = P.ForwardRefBlockAddresses.find(FunctionID);
2041 if (FRBAI != P.ForwardRefBlockAddresses.end()) {
2042 // Resolve all these references.
2043 if (P.ResolveForwardRefBlockAddresses(&F, FRBAI->second, this))
2044 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002045
Chris Lattner3432c622009-10-28 03:39:23 +00002046 P.ForwardRefBlockAddresses.erase(FRBAI);
2047 }
2048 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002049
Chris Lattnerac161bf2009-01-02 07:01:27 +00002050 if (!ForwardRefVals.empty())
2051 return P.Error(ForwardRefVals.begin()->second.second,
2052 "use of undefined value '%" + ForwardRefVals.begin()->first +
2053 "'");
2054 if (!ForwardRefValIDs.empty())
2055 return P.Error(ForwardRefValIDs.begin()->second.second,
2056 "use of undefined value '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002057 Twine(ForwardRefValIDs.begin()->first) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002058 return false;
2059}
2060
2061
2062/// GetVal - Get a value with the specified name or ID, creating a
2063/// forward reference record if needed. This can return null if the value
2064/// exists but does not have the right type.
2065Value *LLParser::PerFunctionState::GetVal(const std::string &Name,
Chris Lattner229907c2011-07-18 04:54:35 +00002066 Type *Ty, LocTy Loc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002067 // Look this name up in the normal function symbol table.
2068 Value *Val = F.getValueSymbolTable().lookup(Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002069
Chris Lattnerac161bf2009-01-02 07:01:27 +00002070 // If this is a forward reference for the value, see if we already created a
2071 // forward ref record.
2072 if (Val == 0) {
2073 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2074 I = ForwardRefVals.find(Name);
2075 if (I != ForwardRefVals.end())
2076 Val = I->second.first;
2077 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002078
Chris Lattnerac161bf2009-01-02 07:01:27 +00002079 // If we have the value in the symbol table or fwd-ref table, return it.
2080 if (Val) {
2081 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002082 if (Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002083 P.Error(Loc, "'%" + Name + "' is not a basic block");
2084 else
2085 P.Error(Loc, "'%" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002086 getTypeString(Val->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002087 return 0;
2088 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002089
Chris Lattnerac161bf2009-01-02 07:01:27 +00002090 // Don't make placeholders with invalid type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002091 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002092 P.Error(Loc, "invalid use of a non-first-class type");
2093 return 0;
2094 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002095
Chris Lattnerac161bf2009-01-02 07:01:27 +00002096 // Otherwise, create a new forward reference for this value and remember it.
2097 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002098 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002099 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002100 else
2101 FwdVal = new Argument(Ty, Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002102
Chris Lattnerac161bf2009-01-02 07:01:27 +00002103 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
2104 return FwdVal;
2105}
2106
Chris Lattner229907c2011-07-18 04:54:35 +00002107Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00002108 LocTy Loc) {
2109 // Look this name up in the normal function symbol table.
2110 Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002111
Chris Lattnerac161bf2009-01-02 07:01:27 +00002112 // If this is a forward reference for the value, see if we already created a
2113 // forward ref record.
2114 if (Val == 0) {
2115 std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2116 I = ForwardRefValIDs.find(ID);
2117 if (I != ForwardRefValIDs.end())
2118 Val = I->second.first;
2119 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002120
Chris Lattnerac161bf2009-01-02 07:01:27 +00002121 // If we have the value in the symbol table or fwd-ref table, return it.
2122 if (Val) {
2123 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002124 if (Ty->isLabelTy())
Benjamin Kramerc7583112010-09-27 17:42:11 +00002125 P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002126 else
Benjamin Kramerc7583112010-09-27 17:42:11 +00002127 P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002128 getTypeString(Val->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002129 return 0;
2130 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002131
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002132 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002133 P.Error(Loc, "invalid use of a non-first-class type");
2134 return 0;
2135 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002136
Chris Lattnerac161bf2009-01-02 07:01:27 +00002137 // Otherwise, create a new forward reference for this value and remember it.
2138 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002139 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002140 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002141 else
2142 FwdVal = new Argument(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002143
Chris Lattnerac161bf2009-01-02 07:01:27 +00002144 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
2145 return FwdVal;
2146}
2147
2148/// SetInstName - After an instruction is parsed and inserted into its
2149/// basic block, this installs its name.
2150bool LLParser::PerFunctionState::SetInstName(int NameID,
2151 const std::string &NameStr,
2152 LocTy NameLoc, Instruction *Inst) {
2153 // If this instruction has void type, it cannot have a name or ID specified.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002154 if (Inst->getType()->isVoidTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002155 if (NameID != -1 || !NameStr.empty())
2156 return P.Error(NameLoc, "instructions returning void cannot have a name");
2157 return false;
2158 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002159
Chris Lattnerac161bf2009-01-02 07:01:27 +00002160 // If this was a numbered instruction, verify that the instruction is the
2161 // expected value and resolve any forward references.
2162 if (NameStr.empty()) {
2163 // If neither a name nor an ID was specified, just use the next ID.
2164 if (NameID == -1)
2165 NameID = NumberedVals.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002166
Chris Lattnerac161bf2009-01-02 07:01:27 +00002167 if (unsigned(NameID) != NumberedVals.size())
2168 return P.Error(NameLoc, "instruction expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002169 Twine(NumberedVals.size()) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002170
Chris Lattnerac161bf2009-01-02 07:01:27 +00002171 std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI =
2172 ForwardRefValIDs.find(NameID);
2173 if (FI != ForwardRefValIDs.end()) {
2174 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002175 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002176 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002177 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2baa6a32009-09-02 15:02:57 +00002178 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002179 ForwardRefValIDs.erase(FI);
2180 }
2181
2182 NumberedVals.push_back(Inst);
2183 return false;
2184 }
2185
2186 // Otherwise, the instruction had a name. Resolve forward refs and set it.
2187 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2188 FI = ForwardRefVals.find(NameStr);
2189 if (FI != ForwardRefVals.end()) {
2190 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002191 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002192 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002193 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2fcee702009-09-02 14:22:03 +00002194 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002195 ForwardRefVals.erase(FI);
2196 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002197
Chris Lattnerac161bf2009-01-02 07:01:27 +00002198 // Set the name on the instruction.
2199 Inst->setName(NameStr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002200
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00002201 if (Inst->getName() != NameStr)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002202 return P.Error(NameLoc, "multiple definition of local value named '" +
Chris Lattnerac161bf2009-01-02 07:01:27 +00002203 NameStr + "'");
2204 return false;
2205}
2206
2207/// GetBB - Get a basic block with the specified name or ID, creating a
2208/// forward reference record if needed.
2209BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
2210 LocTy Loc) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002211 return cast_or_null<BasicBlock>(GetVal(Name,
2212 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002213}
2214
2215BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002216 return cast_or_null<BasicBlock>(GetVal(ID,
2217 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002218}
2219
2220/// DefineBB - Define the specified basic block, which is either named or
2221/// unnamed. If there is an error, this returns null otherwise it returns
2222/// the block being defined.
2223BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
2224 LocTy Loc) {
2225 BasicBlock *BB;
2226 if (Name.empty())
2227 BB = GetBB(NumberedVals.size(), Loc);
2228 else
2229 BB = GetBB(Name, Loc);
2230 if (BB == 0) return 0; // Already diagnosed error.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002231
Chris Lattnerac161bf2009-01-02 07:01:27 +00002232 // Move the block to the end of the function. Forward ref'd blocks are
2233 // inserted wherever they happen to be referenced.
2234 F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002235
Chris Lattnerac161bf2009-01-02 07:01:27 +00002236 // Remove the block from forward ref sets.
2237 if (Name.empty()) {
2238 ForwardRefValIDs.erase(NumberedVals.size());
2239 NumberedVals.push_back(BB);
2240 } else {
2241 // BB forward references are already in the function symbol table.
2242 ForwardRefVals.erase(Name);
2243 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002244
Chris Lattnerac161bf2009-01-02 07:01:27 +00002245 return BB;
2246}
2247
2248//===----------------------------------------------------------------------===//
2249// Constants.
2250//===----------------------------------------------------------------------===//
2251
2252/// ParseValID - Parse an abstract value that doesn't necessarily have a
2253/// type implied. For example, if we parse "4" we don't know what integer type
2254/// it has. The value will later be combined with its type and checked for
Victor Hernandezb8fd1522010-01-10 07:14:18 +00002255/// sanity. PFS is used to convert function-local operands of metadata (since
2256/// metadata operands are not just parsed here but also converted to values).
2257/// PFS can be null when we are not parsing metadata values inside a function.
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00002258bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002259 ID.Loc = Lex.getLoc();
2260 switch (Lex.getKind()) {
2261 default: return TokError("expected value token");
2262 case lltok::GlobalID: // @42
2263 ID.UIntVal = Lex.getUIntVal();
2264 ID.Kind = ValID::t_GlobalID;
2265 break;
2266 case lltok::GlobalVar: // @foo
2267 ID.StrVal = Lex.getStrVal();
2268 ID.Kind = ValID::t_GlobalName;
2269 break;
2270 case lltok::LocalVarID: // %42
2271 ID.UIntVal = Lex.getUIntVal();
2272 ID.Kind = ValID::t_LocalID;
2273 break;
2274 case lltok::LocalVar: // %foo
Chris Lattnerac161bf2009-01-02 07:01:27 +00002275 ID.StrVal = Lex.getStrVal();
2276 ID.Kind = ValID::t_LocalName;
2277 break;
Dan Gohman8939ba332010-07-14 18:26:50 +00002278 case lltok::exclaim: // !42, !{...}, or !"foo"
2279 return ParseMetadataValue(ID, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002280 case lltok::APSInt:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002281 ID.APSIntVal = Lex.getAPSIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00002282 ID.Kind = ValID::t_APSInt;
2283 break;
2284 case lltok::APFloat:
2285 ID.APFloatVal = Lex.getAPFloatVal();
2286 ID.Kind = ValID::t_APFloat;
2287 break;
2288 case lltok::kw_true:
Owen Anderson23a204d2009-07-31 17:39:07 +00002289 ID.ConstantVal = ConstantInt::getTrue(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002290 ID.Kind = ValID::t_Constant;
2291 break;
2292 case lltok::kw_false:
Owen Anderson23a204d2009-07-31 17:39:07 +00002293 ID.ConstantVal = ConstantInt::getFalse(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002294 ID.Kind = ValID::t_Constant;
2295 break;
2296 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
2297 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
2298 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002299
Chris Lattnerac161bf2009-01-02 07:01:27 +00002300 case lltok::lbrace: {
2301 // ValID ::= '{' ConstVector '}'
2302 Lex.Lex();
2303 SmallVector<Constant*, 16> Elts;
2304 if (ParseGlobalValueVector(Elts) ||
2305 ParseToken(lltok::rbrace, "expected end of struct constant"))
2306 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002307
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002308 ID.ConstantStructElts = new Constant*[Elts.size()];
2309 ID.UIntVal = Elts.size();
2310 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2311 ID.Kind = ValID::t_ConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002312 return false;
2313 }
2314 case lltok::less: {
2315 // ValID ::= '<' ConstVector '>' --> Vector.
2316 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
2317 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +00002318 bool isPackedStruct = EatIfPresent(lltok::lbrace);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002319
Chris Lattnerac161bf2009-01-02 07:01:27 +00002320 SmallVector<Constant*, 16> Elts;
2321 LocTy FirstEltLoc = Lex.getLoc();
2322 if (ParseGlobalValueVector(Elts) ||
2323 (isPackedStruct &&
2324 ParseToken(lltok::rbrace, "expected end of packed struct")) ||
2325 ParseToken(lltok::greater, "expected end of constant"))
2326 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002327
Chris Lattnerac161bf2009-01-02 07:01:27 +00002328 if (isPackedStruct) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002329 ID.ConstantStructElts = new Constant*[Elts.size()];
2330 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2331 ID.UIntVal = Elts.size();
2332 ID.Kind = ValID::t_PackedConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002333 return false;
2334 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002335
Chris Lattnerac161bf2009-01-02 07:01:27 +00002336 if (Elts.empty())
2337 return Error(ID.Loc, "constant vector must not be empty");
2338
Duncan Sands9dff9be2010-02-15 16:12:20 +00002339 if (!Elts[0]->getType()->isIntegerTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002340 !Elts[0]->getType()->isFloatingPointTy() &&
2341 !Elts[0]->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002342 return Error(FirstEltLoc,
Nadav Rotem3924cb02011-12-05 06:29:09 +00002343 "vector elements must have integer, pointer or floating point type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002344
Chris Lattnerac161bf2009-01-02 07:01:27 +00002345 // Verify that all the vector elements have the same type.
2346 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
2347 if (Elts[i]->getType() != Elts[0]->getType())
2348 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002349 "vector element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002350 " is not of type '" + getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002351
Chris Lattner69229312011-02-15 00:14:00 +00002352 ID.ConstantVal = ConstantVector::get(Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002353 ID.Kind = ValID::t_Constant;
2354 return false;
2355 }
2356 case lltok::lsquare: { // Array Constant
2357 Lex.Lex();
2358 SmallVector<Constant*, 16> Elts;
2359 LocTy FirstEltLoc = Lex.getLoc();
2360 if (ParseGlobalValueVector(Elts) ||
2361 ParseToken(lltok::rsquare, "expected end of array constant"))
2362 return true;
2363
2364 // Handle empty element.
2365 if (Elts.empty()) {
2366 // Use undef instead of an array because it's inconvenient to determine
2367 // the element type at this point, there being no elements to examine.
Chris Lattner998fa0a2009-01-05 07:52:51 +00002368 ID.Kind = ValID::t_EmptyArray;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002369 return false;
2370 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002371
Chris Lattnerac161bf2009-01-02 07:01:27 +00002372 if (!Elts[0]->getType()->isFirstClassType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002373 return Error(FirstEltLoc, "invalid array element type: " +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002374 getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002375
Owen Anderson4056ca92009-07-29 22:17:13 +00002376 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002377
Chris Lattnerac161bf2009-01-02 07:01:27 +00002378 // Verify all elements are correct type!
Chris Lattner59d0e3b2009-01-02 08:49:06 +00002379 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002380 if (Elts[i]->getType() != Elts[0]->getType())
2381 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002382 "array element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002383 " is not of type '" + getTypeString(Elts[0]->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002384 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002385
Jay Foad83be3612011-06-22 09:24:39 +00002386 ID.ConstantVal = ConstantArray::get(ATy, Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002387 ID.Kind = ValID::t_Constant;
2388 return false;
2389 }
2390 case lltok::kw_c: // c "foo"
2391 Lex.Lex();
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002392 ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
2393 false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002394 if (ParseToken(lltok::StringConstant, "expected string")) return true;
2395 ID.Kind = ValID::t_Constant;
2396 return false;
2397
2398 case lltok::kw_asm: {
Chad Rosier6f9c3852013-02-14 20:44:07 +00002399 // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
2400 // STRINGCONSTANT
Chad Rosierd8c76102012-09-05 19:00:49 +00002401 bool HasSideEffect, AlignStack, AsmDialect;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002402 Lex.Lex();
2403 if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002404 ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
Chad Rosierd8c76102012-09-05 19:00:49 +00002405 ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
Chris Lattner3822f632009-01-02 08:05:26 +00002406 ParseStringConstant(ID.StrVal) ||
2407 ParseToken(lltok::comma, "expected comma in inline asm expression") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002408 ParseToken(lltok::StringConstant, "expected constraint string"))
2409 return true;
2410 ID.StrVal2 = Lex.getStrVal();
Chad Rosierf42fad62012-09-05 00:08:17 +00002411 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) |
Chad Rosierd8c76102012-09-05 19:00:49 +00002412 (unsigned(AsmDialect)<<2);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002413 ID.Kind = ValID::t_InlineAsm;
2414 return false;
2415 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002416
Chris Lattner3432c622009-10-28 03:39:23 +00002417 case lltok::kw_blockaddress: {
2418 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
2419 Lex.Lex();
2420
2421 ValID Fn, Label;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002422
Chris Lattner3432c622009-10-28 03:39:23 +00002423 if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
2424 ParseValID(Fn) ||
2425 ParseToken(lltok::comma, "expected comma in block address expression")||
2426 ParseValID(Label) ||
2427 ParseToken(lltok::rparen, "expected ')' in block address expression"))
2428 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002429
Chris Lattner3432c622009-10-28 03:39:23 +00002430 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
2431 return Error(Fn.Loc, "expected function name in blockaddress");
Chris Lattneraa99c942009-11-01 01:27:45 +00002432 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
Chris Lattner3432c622009-10-28 03:39:23 +00002433 return Error(Label.Loc, "expected basic block name in blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002434
Chris Lattner3432c622009-10-28 03:39:23 +00002435 // Make a global variable as a placeholder for this reference.
2436 GlobalVariable *FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context),
2437 false, GlobalValue::InternalLinkage,
2438 0, "");
2439 ForwardRefBlockAddresses[Fn].push_back(std::make_pair(Label, FwdRef));
2440 ID.ConstantVal = FwdRef;
2441 ID.Kind = ValID::t_Constant;
2442 return false;
2443 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002444
Chris Lattnerac161bf2009-01-02 07:01:27 +00002445 case lltok::kw_trunc:
2446 case lltok::kw_zext:
2447 case lltok::kw_sext:
2448 case lltok::kw_fptrunc:
2449 case lltok::kw_fpext:
2450 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002451 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002452 case lltok::kw_uitofp:
2453 case lltok::kw_sitofp:
2454 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002455 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002456 case lltok::kw_inttoptr:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002457 case lltok::kw_ptrtoint: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002458 unsigned Opc = Lex.getUIntVal();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002459 Type *DestTy = 0;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002460 Constant *SrcVal;
2461 Lex.Lex();
2462 if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
2463 ParseGlobalTypeAndValue(SrcVal) ||
Dan Gohman0b5d0422009-06-15 21:52:11 +00002464 ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002465 ParseType(DestTy) ||
2466 ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
2467 return true;
2468 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
2469 return Error(ID.Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002470 getTypeString(SrcVal->getType()) + "' to '" +
2471 getTypeString(DestTy) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002472 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
Owen Anderson02a9da32009-07-01 23:57:11 +00002473 SrcVal, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002474 ID.Kind = ValID::t_Constant;
2475 return false;
2476 }
2477 case lltok::kw_extractvalue: {
2478 Lex.Lex();
2479 Constant *Val;
2480 SmallVector<unsigned, 4> Indices;
2481 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
2482 ParseGlobalTypeAndValue(Val) ||
2483 ParseIndexList(Indices) ||
2484 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
2485 return true;
Devang Patel1cb51162009-11-03 19:06:07 +00002486
Chris Lattner392be582010-02-12 20:49:41 +00002487 if (!Val->getType()->isAggregateType())
2488 return Error(ID.Loc, "extractvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002489 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002490 return Error(ID.Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002491 ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002492 ID.Kind = ValID::t_Constant;
2493 return false;
2494 }
2495 case lltok::kw_insertvalue: {
2496 Lex.Lex();
2497 Constant *Val0, *Val1;
2498 SmallVector<unsigned, 4> Indices;
2499 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
2500 ParseGlobalTypeAndValue(Val0) ||
2501 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
2502 ParseGlobalTypeAndValue(Val1) ||
2503 ParseIndexList(Indices) ||
2504 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
2505 return true;
Chris Lattner392be582010-02-12 20:49:41 +00002506 if (!Val0->getType()->isAggregateType())
2507 return Error(ID.Loc, "insertvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002508 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002509 return Error(ID.Loc, "invalid indices for insertvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002510 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002511 ID.Kind = ValID::t_Constant;
2512 return false;
2513 }
2514 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002515 case lltok::kw_fcmp: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002516 unsigned PredVal, Opc = Lex.getUIntVal();
2517 Constant *Val0, *Val1;
2518 Lex.Lex();
2519 if (ParseCmpPredicate(PredVal, Opc) ||
2520 ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
2521 ParseGlobalTypeAndValue(Val0) ||
2522 ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
2523 ParseGlobalTypeAndValue(Val1) ||
2524 ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
2525 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002526
Chris Lattnerac161bf2009-01-02 07:01:27 +00002527 if (Val0->getType() != Val1->getType())
2528 return Error(ID.Loc, "compare operands must have the same type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002529
Chris Lattnerac161bf2009-01-02 07:01:27 +00002530 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002531
Chris Lattnerac161bf2009-01-02 07:01:27 +00002532 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002533 if (!Val0->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002534 return Error(ID.Loc, "fcmp requires floating point operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002535 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002536 } else {
2537 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002538 if (!Val0->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002539 !Val0->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002540 return Error(ID.Loc, "icmp requires pointer or integer operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002541 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002542 }
2543 ID.Kind = ValID::t_Constant;
2544 return false;
2545 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002546
Chris Lattnerac161bf2009-01-02 07:01:27 +00002547 // Binary Operators.
2548 case lltok::kw_add:
Dan Gohmana5b96452009-06-04 22:49:04 +00002549 case lltok::kw_fadd:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002550 case lltok::kw_sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002551 case lltok::kw_fsub:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002552 case lltok::kw_mul:
Dan Gohmana5b96452009-06-04 22:49:04 +00002553 case lltok::kw_fmul:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002554 case lltok::kw_udiv:
2555 case lltok::kw_sdiv:
2556 case lltok::kw_fdiv:
2557 case lltok::kw_urem:
2558 case lltok::kw_srem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002559 case lltok::kw_frem:
2560 case lltok::kw_shl:
2561 case lltok::kw_lshr:
2562 case lltok::kw_ashr: {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002563 bool NUW = false;
2564 bool NSW = false;
2565 bool Exact = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002566 unsigned Opc = Lex.getUIntVal();
2567 Constant *Val0, *Val1;
2568 Lex.Lex();
Dan Gohman9c7f8082009-07-27 16:11:46 +00002569 LocTy ModifierLoc = Lex.getLoc();
Chris Lattnera676c0f2011-02-07 16:40:21 +00002570 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
2571 Opc == Instruction::Mul || Opc == Instruction::Shl) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002572 if (EatIfPresent(lltok::kw_nuw))
2573 NUW = true;
2574 if (EatIfPresent(lltok::kw_nsw)) {
2575 NSW = true;
2576 if (EatIfPresent(lltok::kw_nuw))
2577 NUW = true;
2578 }
Chris Lattnera676c0f2011-02-07 16:40:21 +00002579 } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
2580 Opc == Instruction::LShr || Opc == Instruction::AShr) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002581 if (EatIfPresent(lltok::kw_exact))
2582 Exact = true;
2583 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00002584 if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
2585 ParseGlobalTypeAndValue(Val0) ||
2586 ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
2587 ParseGlobalTypeAndValue(Val1) ||
2588 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
2589 return true;
2590 if (Val0->getType() != Val1->getType())
2591 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002592 if (!Val0->getType()->isIntOrIntVectorTy()) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002593 if (NUW)
2594 return Error(ModifierLoc, "nuw only applies to integer operations");
2595 if (NSW)
2596 return Error(ModifierLoc, "nsw only applies to integer operations");
2597 }
Dan Gohmana2414ea2010-05-03 22:44:19 +00002598 // Check that the type is valid for the operator.
2599 switch (Opc) {
2600 case Instruction::Add:
2601 case Instruction::Sub:
2602 case Instruction::Mul:
2603 case Instruction::UDiv:
2604 case Instruction::SDiv:
2605 case Instruction::URem:
2606 case Instruction::SRem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002607 case Instruction::Shl:
2608 case Instruction::AShr:
2609 case Instruction::LShr:
Dan Gohmana2414ea2010-05-03 22:44:19 +00002610 if (!Val0->getType()->isIntOrIntVectorTy())
2611 return Error(ID.Loc, "constexpr requires integer operands");
2612 break;
2613 case Instruction::FAdd:
2614 case Instruction::FSub:
2615 case Instruction::FMul:
2616 case Instruction::FDiv:
2617 case Instruction::FRem:
2618 if (!Val0->getType()->isFPOrFPVectorTy())
2619 return Error(ID.Loc, "constexpr requires fp operands");
2620 break;
2621 default: llvm_unreachable("Unknown binary operator!");
2622 }
Dan Gohman1b849082009-09-07 23:54:19 +00002623 unsigned Flags = 0;
2624 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2625 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
Chris Lattner35315d02011-02-06 21:44:57 +00002626 if (Exact) Flags |= PossiblyExactOperator::IsExact;
Dan Gohman1b849082009-09-07 23:54:19 +00002627 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
Dan Gohman9c7f8082009-07-27 16:11:46 +00002628 ID.ConstantVal = C;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002629 ID.Kind = ValID::t_Constant;
2630 return false;
2631 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002632
Chris Lattnerac161bf2009-01-02 07:01:27 +00002633 // Logical Operations
Chris Lattnerac161bf2009-01-02 07:01:27 +00002634 case lltok::kw_and:
2635 case lltok::kw_or:
2636 case lltok::kw_xor: {
2637 unsigned Opc = Lex.getUIntVal();
2638 Constant *Val0, *Val1;
2639 Lex.Lex();
2640 if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
2641 ParseGlobalTypeAndValue(Val0) ||
2642 ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
2643 ParseGlobalTypeAndValue(Val1) ||
2644 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
2645 return true;
2646 if (Val0->getType() != Val1->getType())
2647 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002648 if (!Val0->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002649 return Error(ID.Loc,
2650 "constexpr requires integer or integer vector operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002651 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002652 ID.Kind = ValID::t_Constant;
2653 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002654 }
2655
Chris Lattnerac161bf2009-01-02 07:01:27 +00002656 case lltok::kw_getelementptr:
2657 case lltok::kw_shufflevector:
2658 case lltok::kw_insertelement:
2659 case lltok::kw_extractelement:
2660 case lltok::kw_select: {
2661 unsigned Opc = Lex.getUIntVal();
2662 SmallVector<Constant*, 16> Elts;
Dan Gohman1639c392009-07-27 21:53:46 +00002663 bool InBounds = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002664 Lex.Lex();
Dan Gohman1639c392009-07-27 21:53:46 +00002665 if (Opc == Instruction::GetElementPtr)
Dan Gohman16cbbe42009-07-29 15:58:36 +00002666 InBounds = EatIfPresent(lltok::kw_inbounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002667 if (ParseToken(lltok::lparen, "expected '(' in constantexpr") ||
2668 ParseGlobalValueVector(Elts) ||
2669 ParseToken(lltok::rparen, "expected ')' in constantexpr"))
2670 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002671
Chris Lattnerac161bf2009-01-02 07:01:27 +00002672 if (Opc == Instruction::GetElementPtr) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00002673 if (Elts.size() == 0 ||
2674 !Elts[0]->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002675 return Error(ID.Loc, "getelementptr requires pointer operand");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002676
Jay Foaded8db7d2011-07-21 14:31:17 +00002677 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
Jay Foadd1b78492011-07-25 09:48:08 +00002678 if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002679 return Error(ID.Loc, "invalid indices for getelementptr");
Jay Foad2f5fc8c2011-07-21 15:15:37 +00002680 ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], Indices,
2681 InBounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002682 } else if (Opc == Instruction::Select) {
2683 if (Elts.size() != 3)
2684 return Error(ID.Loc, "expected three operands to select");
2685 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
2686 Elts[2]))
2687 return Error(ID.Loc, Reason);
Owen Anderson487375e2009-07-29 18:55:55 +00002688 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002689 } else if (Opc == Instruction::ShuffleVector) {
2690 if (Elts.size() != 3)
2691 return Error(ID.Loc, "expected three operands to shufflevector");
2692 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2693 return Error(ID.Loc, "invalid operands to shufflevector");
Owen Anderson02a9da32009-07-01 23:57:11 +00002694 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002695 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002696 } else if (Opc == Instruction::ExtractElement) {
2697 if (Elts.size() != 2)
2698 return Error(ID.Loc, "expected two operands to extractelement");
2699 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
2700 return Error(ID.Loc, "invalid extractelement operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002701 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002702 } else {
2703 assert(Opc == Instruction::InsertElement && "Unknown opcode");
2704 if (Elts.size() != 3)
2705 return Error(ID.Loc, "expected three operands to insertelement");
2706 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2707 return Error(ID.Loc, "invalid insertelement operands");
Owen Anderson02a9da32009-07-01 23:57:11 +00002708 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002709 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002710 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002711
Chris Lattnerac161bf2009-01-02 07:01:27 +00002712 ID.Kind = ValID::t_Constant;
2713 return false;
2714 }
2715 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002716
Chris Lattnerac161bf2009-01-02 07:01:27 +00002717 Lex.Lex();
2718 return false;
2719}
2720
2721/// ParseGlobalValue - Parse a global value with the specified type.
Chris Lattner229907c2011-07-18 04:54:35 +00002722bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) {
Victor Hernandez9d75c962010-01-11 22:31:58 +00002723 C = 0;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002724 ValID ID;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002725 Value *V = NULL;
2726 bool Parsed = ParseValID(ID) ||
2727 ConvertValIDToValue(Ty, ID, V, NULL);
2728 if (V && !(C = dyn_cast<Constant>(V)))
2729 return Error(ID.Loc, "global values must be constants");
2730 return Parsed;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002731}
2732
Victor Hernandez9d75c962010-01-11 22:31:58 +00002733bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002734 Type *Ty = 0;
2735 return ParseType(Ty) ||
2736 ParseGlobalValue(Ty, V);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002737}
2738
2739/// ParseGlobalValueVector
2740/// ::= /*empty*/
2741/// ::= TypeAndValue (',' TypeAndValue)*
2742bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts) {
2743 // Empty list.
2744 if (Lex.getKind() == lltok::rbrace ||
2745 Lex.getKind() == lltok::rsquare ||
2746 Lex.getKind() == lltok::greater ||
2747 Lex.getKind() == lltok::rparen)
2748 return false;
2749
2750 Constant *C;
2751 if (ParseGlobalTypeAndValue(C)) return true;
2752 Elts.push_back(C);
2753
2754 while (EatIfPresent(lltok::comma)) {
2755 if (ParseGlobalTypeAndValue(C)) return true;
2756 Elts.push_back(C);
2757 }
2758
2759 return false;
2760}
2761
Dan Gohmanc828c542010-08-24 02:24:03 +00002762bool LLParser::ParseMetadataListValue(ValID &ID, PerFunctionState *PFS) {
2763 assert(Lex.getKind() == lltok::lbrace);
2764 Lex.Lex();
2765
2766 SmallVector<Value*, 16> Elts;
2767 if (ParseMDNodeVector(Elts, PFS) ||
2768 ParseToken(lltok::rbrace, "expected end of metadata node"))
2769 return true;
2770
Jay Foad5514afe2011-04-21 19:59:31 +00002771 ID.MDNodeVal = MDNode::get(Context, Elts);
Dan Gohmanc828c542010-08-24 02:24:03 +00002772 ID.Kind = ValID::t_MDNode;
2773 return false;
2774}
2775
Dan Gohman8939ba332010-07-14 18:26:50 +00002776/// ParseMetadataValue
2777/// ::= !42
2778/// ::= !{...}
2779/// ::= !"string"
2780bool LLParser::ParseMetadataValue(ValID &ID, PerFunctionState *PFS) {
2781 assert(Lex.getKind() == lltok::exclaim);
2782 Lex.Lex();
2783
2784 // MDNode:
2785 // !{ ... }
Dan Gohmanc828c542010-08-24 02:24:03 +00002786 if (Lex.getKind() == lltok::lbrace)
2787 return ParseMetadataListValue(ID, PFS);
Dan Gohman8939ba332010-07-14 18:26:50 +00002788
2789 // Standalone metadata reference
2790 // !42
2791 if (Lex.getKind() == lltok::APSInt) {
2792 if (ParseMDNodeID(ID.MDNodeVal)) return true;
2793 ID.Kind = ValID::t_MDNode;
2794 return false;
2795 }
2796
2797 // MDString:
2798 // ::= '!' STRINGCONSTANT
2799 if (ParseMDString(ID.MDStringVal)) return true;
2800 ID.Kind = ValID::t_MDString;
2801 return false;
2802}
2803
Victor Hernandez9d75c962010-01-11 22:31:58 +00002804
2805//===----------------------------------------------------------------------===//
2806// Function Parsing.
2807//===----------------------------------------------------------------------===//
2808
Chris Lattner229907c2011-07-18 04:54:35 +00002809bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez9d75c962010-01-11 22:31:58 +00002810 PerFunctionState *PFS) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002811 if (Ty->isFunctionTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002812 return Error(ID.Loc, "functions are not values, refer to them as pointers");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002813
Chris Lattnerac161bf2009-01-02 07:01:27 +00002814 switch (ID.Kind) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002815 case ValID::t_LocalID:
Victor Hernandez9d75c962010-01-11 22:31:58 +00002816 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2817 V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc);
2818 return (V == 0);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002819 case ValID::t_LocalName:
Victor Hernandez9d75c962010-01-11 22:31:58 +00002820 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2821 V = PFS->GetVal(ID.StrVal, Ty, ID.Loc);
2822 return (V == 0);
2823 case ValID::t_InlineAsm: {
Chris Lattner229907c2011-07-18 04:54:35 +00002824 PointerType *PTy = dyn_cast<PointerType>(Ty);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002825 FunctionType *FTy =
Victor Hernandez9d75c962010-01-11 22:31:58 +00002826 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
2827 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
2828 return Error(ID.Loc, "invalid type for inline asm constraint string");
Chad Rosierf42fad62012-09-05 00:08:17 +00002829 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1,
Chad Rosierd8c76102012-09-05 19:00:49 +00002830 (ID.UIntVal>>1)&1, (InlineAsm::AsmDialect(ID.UIntVal>>2)));
Victor Hernandez9d75c962010-01-11 22:31:58 +00002831 return false;
2832 }
2833 case ValID::t_MDNode:
2834 if (!Ty->isMetadataTy())
2835 return Error(ID.Loc, "metadata value must have metadata type");
2836 V = ID.MDNodeVal;
2837 return false;
2838 case ValID::t_MDString:
2839 if (!Ty->isMetadataTy())
2840 return Error(ID.Loc, "metadata value must have metadata type");
2841 V = ID.MDStringVal;
2842 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002843 case ValID::t_GlobalName:
2844 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
2845 return V == 0;
2846 case ValID::t_GlobalID:
2847 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
2848 return V == 0;
2849 case ValID::t_APSInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002850 if (!Ty->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002851 return Error(ID.Loc, "integer constant must have integer type");
Jay Foad583abbc2010-12-07 08:25:19 +00002852 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
Owen Andersonedb4a702009-07-24 23:12:02 +00002853 V = ConstantInt::get(Context, ID.APSIntVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002854 return false;
2855 case ValID::t_APFloat:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002856 if (!Ty->isFloatingPointTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002857 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
2858 return Error(ID.Loc, "floating point constant invalid for type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002859
Dan Gohman518cda42011-12-17 00:04:22 +00002860 // The lexer has no type info, so builds all half, float, and double FP
2861 // constants as double. Fix this here. Long double does not need this.
2862 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002863 bool Ignored;
Dan Gohman518cda42011-12-17 00:04:22 +00002864 if (Ty->isHalfTy())
2865 ID.APFloatVal.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven,
2866 &Ignored);
2867 else if (Ty->isFloatTy())
2868 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
2869 &Ignored);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002870 }
Owen Anderson69c464d2009-07-27 20:59:43 +00002871 V = ConstantFP::get(Context, ID.APFloatVal);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002872
Chris Lattner8f57d29e2009-01-05 18:24:23 +00002873 if (V->getType() != Ty)
2874 return Error(ID.Loc, "floating point constant does not have type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002875 getTypeString(Ty) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002876
Chris Lattnerac161bf2009-01-02 07:01:27 +00002877 return false;
2878 case ValID::t_Null:
Duncan Sands19d0b472010-02-16 11:11:14 +00002879 if (!Ty->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002880 return Error(ID.Loc, "null must be a pointer type");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002881 V = ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002882 return false;
2883 case ValID::t_Undef:
Chris Lattnerffa07782009-01-05 08:13:38 +00002884 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002885 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerffa07782009-01-05 08:13:38 +00002886 return Error(ID.Loc, "invalid type for undef constant");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002887 V = UndefValue::get(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002888 return false;
Chris Lattner998fa0a2009-01-05 07:52:51 +00002889 case ValID::t_EmptyArray:
Duncan Sands19d0b472010-02-16 11:11:14 +00002890 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
Chris Lattner998fa0a2009-01-05 07:52:51 +00002891 return Error(ID.Loc, "invalid empty array initializer");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002892 V = UndefValue::get(Ty);
Chris Lattner998fa0a2009-01-05 07:52:51 +00002893 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002894 case ValID::t_Zero:
Chris Lattnerffa07782009-01-05 08:13:38 +00002895 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002896 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002897 return Error(ID.Loc, "invalid type for null constant");
Owen Anderson5a1acd92009-07-31 20:28:14 +00002898 V = Constant::getNullValue(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002899 return false;
2900 case ValID::t_Constant:
Chris Lattner13ee7952010-08-28 04:09:24 +00002901 if (ID.ConstantVal->getType() != Ty)
Chris Lattnerac161bf2009-01-02 07:01:27 +00002902 return Error(ID.Loc, "constant expression type mismatch");
Chris Lattner392be582010-02-12 20:49:41 +00002903
Chris Lattnerac161bf2009-01-02 07:01:27 +00002904 V = ID.ConstantVal;
2905 return false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002906 case ValID::t_ConstantStruct:
2907 case ValID::t_PackedConstantStruct:
Chris Lattner229907c2011-07-18 04:54:35 +00002908 if (StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002909 if (ST->getNumElements() != ID.UIntVal)
2910 return Error(ID.Loc,
2911 "initializer with struct type has wrong # elements");
2912 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
2913 return Error(ID.Loc, "packed'ness of initializer and type don't match");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002914
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002915 // Verify that the elements are compatible with the structtype.
2916 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
2917 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
2918 return Error(ID.Loc, "element " + Twine(i) +
2919 " of struct initializer doesn't match struct element type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002920
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002921 V = ConstantStruct::get(ST, makeArrayRef(ID.ConstantStructElts,
2922 ID.UIntVal));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002923 } else
2924 return Error(ID.Loc, "constant expression type mismatch");
2925 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002926 }
Chandler Carruthf3e85022012-01-10 18:08:01 +00002927 llvm_unreachable("Invalid ValID");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002928}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002929
Chris Lattner229907c2011-07-18 04:54:35 +00002930bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002931 V = 0;
2932 ValID ID;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002933 return ParseValID(ID, PFS) ||
2934 ConvertValIDToValue(Ty, ID, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002935}
2936
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002937bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) {
2938 Type *Ty = 0;
2939 return ParseType(Ty) ||
2940 ParseValue(Ty, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002941}
2942
Chris Lattner3ed871f2009-10-27 19:13:16 +00002943bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
2944 PerFunctionState &PFS) {
2945 Value *V;
2946 Loc = Lex.getLoc();
2947 if (ParseTypeAndValue(V, PFS)) return true;
2948 if (!isa<BasicBlock>(V))
2949 return Error(Loc, "expected a basic block");
2950 BB = cast<BasicBlock>(V);
2951 return false;
2952}
2953
2954
Chris Lattnerac161bf2009-01-02 07:01:27 +00002955/// FunctionHeader
2956/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
Rafael Espindola45e6c192011-01-08 16:42:36 +00002957/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00002958/// OptionalAlign OptGC OptionalPrefix
Chris Lattnerac161bf2009-01-02 07:01:27 +00002959bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
2960 // Parse the linkage.
2961 LocTy LinkageLoc = Lex.getLoc();
2962 unsigned Linkage;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002963
Kostya Serebryanya5054ad2012-01-20 17:56:17 +00002964 unsigned Visibility;
Nico Rieck7157bb72014-01-14 15:22:47 +00002965 unsigned DLLStorageClass;
Bill Wendling50d27842012-10-15 20:35:56 +00002966 AttrBuilder RetAttrs;
Sandeep Patel68c5f472009-09-02 08:44:58 +00002967 CallingConv::ID CC;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002968 Type *RetType = 0;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002969 LocTy RetTypeLoc = Lex.getLoc();
2970 if (ParseOptionalLinkage(Linkage) ||
2971 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +00002972 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002973 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00002974 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00002975 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002976 return true;
2977
2978 // Verify that the linkage is ok.
2979 switch ((GlobalValue::LinkageTypes)Linkage) {
2980 case GlobalValue::ExternalLinkage:
2981 break; // always ok.
Duncan Sandse2881052009-03-11 08:08:06 +00002982 case GlobalValue::ExternalWeakLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002983 if (isDefine)
2984 return Error(LinkageLoc, "invalid linkage for function definition");
2985 break;
Rafael Espindola6de96a12009-01-15 20:18:42 +00002986 case GlobalValue::PrivateLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002987 case GlobalValue::InternalLinkage:
Nick Lewycky8019af62009-04-13 07:02:02 +00002988 case GlobalValue::AvailableExternallyLinkage:
Duncan Sands12da8ce2009-03-07 15:45:40 +00002989 case GlobalValue::LinkOnceAnyLinkage:
2990 case GlobalValue::LinkOnceODRLinkage:
2991 case GlobalValue::WeakAnyLinkage:
2992 case GlobalValue::WeakODRLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002993 if (!isDefine)
2994 return Error(LinkageLoc, "invalid linkage for function declaration");
2995 break;
2996 case GlobalValue::AppendingLinkage:
Duncan Sands4581beb2009-03-11 20:14:15 +00002997 case GlobalValue::CommonLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002998 return Error(LinkageLoc, "invalid function linkage type");
2999 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003000
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003001 if (!FunctionType::isValidReturnType(RetType))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003002 return Error(RetTypeLoc, "invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003003
Chris Lattnerac161bf2009-01-02 07:01:27 +00003004 LocTy NameLoc = Lex.getLoc();
Chris Lattner778c62c2009-02-18 21:48:13 +00003005
3006 std::string FunctionName;
3007 if (Lex.getKind() == lltok::GlobalVar) {
3008 FunctionName = Lex.getStrVal();
3009 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
3010 unsigned NameID = Lex.getUIntVal();
3011
3012 if (NameID != NumberedVals.size())
3013 return TokError("function expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00003014 Twine(NumberedVals.size()) + "'");
Chris Lattner778c62c2009-02-18 21:48:13 +00003015 } else {
3016 return TokError("expected function name");
3017 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003018
Chris Lattner3822f632009-01-02 08:05:26 +00003019 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003020
Chris Lattner3822f632009-01-02 08:05:26 +00003021 if (Lex.getKind() != lltok::lparen)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003022 return TokError("expected '(' in function argument list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003023
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003024 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003025 bool isVarArg;
Bill Wendling50d27842012-10-15 20:35:56 +00003026 AttrBuilder FuncAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003027 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00003028 LocTy BuiltinLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003029 std::string Section;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003030 unsigned Alignment;
Chris Lattner3822f632009-01-02 08:05:26 +00003031 std::string GC;
Rafael Espindola563eb4b2011-01-25 19:09:56 +00003032 bool UnnamedAddr;
3033 LocTy UnnamedAddrLoc;
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003034 Constant *Prefix = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00003035
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003036 if (ParseArgumentList(ArgList, isVarArg) ||
Rafael Espindola563eb4b2011-01-25 19:09:56 +00003037 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
3038 &UnnamedAddrLoc) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003039 ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00003040 BuiltinLoc) ||
Chris Lattner3822f632009-01-02 08:05:26 +00003041 (EatIfPresent(lltok::kw_section) &&
3042 ParseStringConstant(Section)) ||
3043 ParseOptionalAlignment(Alignment) ||
3044 (EatIfPresent(lltok::kw_gc) &&
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003045 ParseStringConstant(GC)) ||
3046 (EatIfPresent(lltok::kw_prefix) &&
3047 ParseGlobalTypeAndValue(Prefix)))
Chris Lattner3822f632009-01-02 08:05:26 +00003048 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003049
Michael Gottesman41748d72013-06-27 00:25:01 +00003050 if (FuncAttrs.contains(Attribute::Builtin))
3051 return Error(BuiltinLoc, "'builtin' attribute not valid on function");
Bill Wendling09bd1f72013-02-22 00:12:35 +00003052
Chris Lattnerac161bf2009-01-02 07:01:27 +00003053 // If the alignment was parsed as an attribute, move to the alignment field.
Bill Wendlingc6daefa2012-10-08 23:27:46 +00003054 if (FuncAttrs.hasAlignmentAttr()) {
Bill Wendling9be77592012-09-21 15:26:31 +00003055 Alignment = FuncAttrs.getAlignment();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003056 FuncAttrs.removeAttribute(Attribute::Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003057 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003058
Chris Lattnerac161bf2009-01-02 07:01:27 +00003059 // Okay, if we got here, the function is syntactically valid. Convert types
3060 // and do semantic checks.
Jay Foadb804a2b2011-07-12 14:06:48 +00003061 std::vector<Type*> ParamTypeList;
Bill Wendlingf5075a42013-01-27 02:24:02 +00003062 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003063
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003064 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003065 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3066 AttributeSet::ReturnIndex,
3067 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003068
Chris Lattnerac161bf2009-01-02 07:01:27 +00003069 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003070 ParamTypeList.push_back(ArgList[i].Ty);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00003071 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3072 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00003073 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3074 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003075 }
3076
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003077 if (FuncAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003078 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3079 AttributeSet::FunctionIndex,
3080 FuncAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003081
Bill Wendlinge94d8432012-12-07 23:16:57 +00003082 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003083
Bill Wendling749a43d2012-12-30 13:50:49 +00003084 if (PAL.hasAttribute(1, Attribute::StructRet) && !RetType->isVoidTy())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003085 return Error(RetTypeLoc, "functions with 'sret' argument must return void");
3086
Chris Lattner229907c2011-07-18 04:54:35 +00003087 FunctionType *FT =
Owen Anderson4056ca92009-07-29 22:17:13 +00003088 FunctionType::get(RetType, ParamTypeList, isVarArg);
Chris Lattner229907c2011-07-18 04:54:35 +00003089 PointerType *PFT = PointerType::getUnqual(FT);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003090
3091 Fn = 0;
3092 if (!FunctionName.empty()) {
3093 // If this was a definition of a forward reference, remove the definition
3094 // from the forward reference table and fill in the forward ref.
3095 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI =
3096 ForwardRefVals.find(FunctionName);
3097 if (FRVI != ForwardRefVals.end()) {
3098 Fn = M->getFunction(FunctionName);
Nick Lewycky686d7cb2012-10-11 00:38:25 +00003099 if (!Fn)
3100 return Error(FRVI->second.second, "invalid forward reference to "
3101 "function as global value!");
Chris Lattnerc239eb72010-04-20 04:49:11 +00003102 if (Fn->getType() != PFT)
3103 return Error(FRVI->second.second, "invalid forward reference to "
3104 "function '" + FunctionName + "' with wrong type!");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003105
Chris Lattnerac161bf2009-01-02 07:01:27 +00003106 ForwardRefVals.erase(FRVI);
3107 } else if ((Fn = M->getFunction(FunctionName))) {
Chris Lattner5756c162011-06-17 07:06:44 +00003108 // Reject redefinitions.
3109 return Error(NameLoc, "invalid redefinition of function '" +
3110 FunctionName + "'");
Chris Lattnere38317f2009-10-25 23:22:50 +00003111 } else if (M->getNamedValue(FunctionName)) {
3112 return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003113 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003114
Dan Gohman399d6ae2009-08-29 23:37:49 +00003115 } else {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003116 // If this is a definition of a forward referenced function, make sure the
3117 // types agree.
3118 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I
3119 = ForwardRefValIDs.find(NumberedVals.size());
3120 if (I != ForwardRefValIDs.end()) {
3121 Fn = cast<Function>(I->second.first);
3122 if (Fn->getType() != PFT)
3123 return Error(NameLoc, "type of definition and forward reference of '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00003124 Twine(NumberedVals.size()) + "' disagree");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003125 ForwardRefValIDs.erase(I);
3126 }
3127 }
3128
3129 if (Fn == 0)
3130 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
3131 else // Move the forward-reference to the correct spot in the module.
3132 M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
3133
3134 if (FunctionName.empty())
3135 NumberedVals.push_back(Fn);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003136
Chris Lattnerac161bf2009-01-02 07:01:27 +00003137 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
3138 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +00003139 Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003140 Fn->setCallingConv(CC);
3141 Fn->setAttributes(PAL);
Rafael Espindola45e6c192011-01-08 16:42:36 +00003142 Fn->setUnnamedAddr(UnnamedAddr);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003143 Fn->setAlignment(Alignment);
3144 Fn->setSection(Section);
3145 if (!GC.empty()) Fn->setGC(GC.c_str());
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003146 Fn->setPrefixData(Prefix);
Bill Wendlingb32b0412013-02-08 06:32:06 +00003147 ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003148
Chris Lattnerac161bf2009-01-02 07:01:27 +00003149 // Add all of the arguments we parsed to the function.
3150 Function::arg_iterator ArgIt = Fn->arg_begin();
3151 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
3152 // If the argument has a name, insert it into the argument symbol table.
3153 if (ArgList[i].Name.empty()) continue;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003154
Chris Lattnerac161bf2009-01-02 07:01:27 +00003155 // Set the name, if it conflicted, it will be auto-renamed.
3156 ArgIt->setName(ArgList[i].Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003157
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00003158 if (ArgIt->getName() != ArgList[i].Name)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003159 return Error(ArgList[i].Loc, "redefinition of argument '%" +
3160 ArgList[i].Name + "'");
3161 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003162
Chris Lattnerac161bf2009-01-02 07:01:27 +00003163 return false;
3164}
3165
3166
3167/// ParseFunctionBody
3168/// ::= '{' BasicBlock+ '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00003169///
3170bool LLParser::ParseFunctionBody(Function &Fn) {
Chris Lattner4649a732011-06-17 06:42:57 +00003171 if (Lex.getKind() != lltok::lbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003172 return TokError("expected '{' in function body");
3173 Lex.Lex(); // eat the {.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003174
Chris Lattner3432c622009-10-28 03:39:23 +00003175 int FunctionNumber = -1;
3176 if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003177
Chris Lattner3432c622009-10-28 03:39:23 +00003178 PerFunctionState PFS(*this, Fn, FunctionNumber);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003179
Chris Lattnerbbddd962010-01-09 19:20:07 +00003180 // We need at least one basic block.
Chris Lattner4649a732011-06-17 06:42:57 +00003181 if (Lex.getKind() == lltok::rbrace)
Chris Lattnerbbddd962010-01-09 19:20:07 +00003182 return TokError("function body requires at least one basic block");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003183
Chris Lattner4649a732011-06-17 06:42:57 +00003184 while (Lex.getKind() != lltok::rbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003185 if (ParseBasicBlock(PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003186
Chris Lattnerac161bf2009-01-02 07:01:27 +00003187 // Eat the }.
3188 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003189
Chris Lattnerac161bf2009-01-02 07:01:27 +00003190 // Verify function is ok.
Chris Lattner3432c622009-10-28 03:39:23 +00003191 return PFS.FinishFunction();
Chris Lattnerac161bf2009-01-02 07:01:27 +00003192}
3193
3194/// ParseBasicBlock
3195/// ::= LabelStr? Instruction*
3196bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
3197 // If this basic block starts out with a name, remember it.
3198 std::string Name;
3199 LocTy NameLoc = Lex.getLoc();
3200 if (Lex.getKind() == lltok::LabelStr) {
3201 Name = Lex.getStrVal();
3202 Lex.Lex();
3203 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003204
Chris Lattnerac161bf2009-01-02 07:01:27 +00003205 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
3206 if (BB == 0) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003207
Chris Lattnerac161bf2009-01-02 07:01:27 +00003208 std::string NameStr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003209
Chris Lattnerac161bf2009-01-02 07:01:27 +00003210 // Parse the instructions in this block until we get a terminator.
3211 Instruction *Inst;
3212 do {
3213 // This instruction may have three possibilities for a name: a) none
3214 // specified, b) name specified "%foo =", c) number specified: "%4 =".
3215 LocTy NameLoc = Lex.getLoc();
3216 int NameID = -1;
3217 NameStr = "";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003218
Chris Lattnerac161bf2009-01-02 07:01:27 +00003219 if (Lex.getKind() == lltok::LocalVarID) {
3220 NameID = Lex.getUIntVal();
3221 Lex.Lex();
3222 if (ParseToken(lltok::equal, "expected '=' after instruction id"))
3223 return true;
Chris Lattnerdef19492011-06-17 06:36:20 +00003224 } else if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003225 NameStr = Lex.getStrVal();
3226 Lex.Lex();
3227 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
3228 return true;
3229 }
Devang Patelea8a4b92009-09-17 23:04:48 +00003230
Chris Lattner77b89dc2009-12-30 05:23:43 +00003231 switch (ParseInstruction(Inst, BB, PFS)) {
Craig Toppera2886c22012-02-07 05:05:23 +00003232 default: llvm_unreachable("Unknown ParseInstruction result!");
Chris Lattner77b89dc2009-12-30 05:23:43 +00003233 case InstError: return true;
3234 case InstNormal:
Chris Lattner2e664bd2010-04-07 04:08:57 +00003235 BB->getInstList().push_back(Inst);
3236
Chris Lattner77b89dc2009-12-30 05:23:43 +00003237 // With a normal result, we check to see if the instruction is followed by
3238 // a comma and metadata.
3239 if (EatIfPresent(lltok::comma))
Dan Gohman338d9a42010-08-24 02:05:17 +00003240 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattner77b89dc2009-12-30 05:23:43 +00003241 return true;
3242 break;
3243 case InstExtraComma:
Chris Lattner2e664bd2010-04-07 04:08:57 +00003244 BB->getInstList().push_back(Inst);
3245
Chris Lattner77b89dc2009-12-30 05:23:43 +00003246 // If the instruction parser ate an extra comma at the end of it, it
3247 // *must* be followed by metadata.
Dan Gohman338d9a42010-08-24 02:05:17 +00003248 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattner77b89dc2009-12-30 05:23:43 +00003249 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003250 break;
Chris Lattner77b89dc2009-12-30 05:23:43 +00003251 }
Devang Patelea8a4b92009-09-17 23:04:48 +00003252
Chris Lattnerac161bf2009-01-02 07:01:27 +00003253 // Set the name on the instruction.
3254 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
3255 } while (!isa<TerminatorInst>(Inst));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003256
Chris Lattnerac161bf2009-01-02 07:01:27 +00003257 return false;
3258}
3259
3260//===----------------------------------------------------------------------===//
3261// Instruction Parsing.
3262//===----------------------------------------------------------------------===//
3263
3264/// ParseInstruction - Parse one of the many different instructions.
3265///
Chris Lattner77b89dc2009-12-30 05:23:43 +00003266int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
3267 PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003268 lltok::Kind Token = Lex.getKind();
3269 if (Token == lltok::Eof)
3270 return TokError("found end of file when expecting more instructions");
3271 LocTy Loc = Lex.getLoc();
Chris Lattner89d856e2009-03-01 00:53:13 +00003272 unsigned KeywordVal = Lex.getUIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00003273 Lex.Lex(); // Eat the keyword.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003274
Chris Lattnerac161bf2009-01-02 07:01:27 +00003275 switch (Token) {
3276 default: return Error(Loc, "expected instruction opcode");
3277 // Terminator Instructions.
Owen Anderson55f1c092009-08-13 21:58:54 +00003278 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003279 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
3280 case lltok::kw_br: return ParseBr(Inst, PFS);
3281 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003282 case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003283 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +00003284 case lltok::kw_resume: return ParseResume(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003285 // Binary Operators.
3286 case lltok::kw_add:
3287 case lltok::kw_sub:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003288 case lltok::kw_mul:
3289 case lltok::kw_shl: {
Chris Lattnera676c0f2011-02-07 16:40:21 +00003290 bool NUW = EatIfPresent(lltok::kw_nuw);
3291 bool NSW = EatIfPresent(lltok::kw_nsw);
3292 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003293
Chris Lattnera676c0f2011-02-07 16:40:21 +00003294 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003295
Chris Lattnera676c0f2011-02-07 16:40:21 +00003296 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
3297 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
3298 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00003299 }
Dan Gohmana5b96452009-06-04 22:49:04 +00003300 case lltok::kw_fadd:
3301 case lltok::kw_fsub:
Michael Ilseman92053172012-11-27 00:42:44 +00003302 case lltok::kw_fmul:
3303 case lltok::kw_fdiv:
3304 case lltok::kw_frem: {
3305 FastMathFlags FMF = EatFastMathFlagsIfPresent();
3306 int Res = ParseArithmetic(Inst, PFS, KeywordVal, 2);
3307 if (Res != 0)
3308 return Res;
3309 if (FMF.any())
3310 Inst->setFastMathFlags(FMF);
3311 return 0;
3312 }
Dan Gohmana5b96452009-06-04 22:49:04 +00003313
Chris Lattner35315d02011-02-06 21:44:57 +00003314 case lltok::kw_sdiv:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003315 case lltok::kw_udiv:
3316 case lltok::kw_lshr:
3317 case lltok::kw_ashr: {
3318 bool Exact = EatIfPresent(lltok::kw_exact);
3319
3320 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
3321 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
3322 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00003323 }
3324
Chris Lattnerac161bf2009-01-02 07:01:27 +00003325 case lltok::kw_urem:
Chris Lattner89d856e2009-03-01 00:53:13 +00003326 case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003327 case lltok::kw_and:
3328 case lltok::kw_or:
Chris Lattner89d856e2009-03-01 00:53:13 +00003329 case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003330 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003331 case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003332 // Casts.
3333 case lltok::kw_trunc:
3334 case lltok::kw_zext:
3335 case lltok::kw_sext:
3336 case lltok::kw_fptrunc:
3337 case lltok::kw_fpext:
3338 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003339 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003340 case lltok::kw_uitofp:
3341 case lltok::kw_sitofp:
3342 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003343 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003344 case lltok::kw_inttoptr:
Chris Lattner89d856e2009-03-01 00:53:13 +00003345 case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003346 // Other.
3347 case lltok::kw_select: return ParseSelect(Inst, PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +00003348 case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003349 case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
3350 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
3351 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
3352 case lltok::kw_phi: return ParsePHI(Inst, PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +00003353 case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003354 case lltok::kw_call: return ParseCall(Inst, PFS, false);
3355 case lltok::kw_tail: return ParseCall(Inst, PFS, true);
3356 // Memory.
Victor Hernandezc7d6a832009-10-17 00:00:19 +00003357 case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +00003358 case lltok::kw_load: return ParseLoad(Inst, PFS);
3359 case lltok::kw_store: return ParseStore(Inst, PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +00003360 case lltok::kw_cmpxchg: return ParseCmpXchg(Inst, PFS);
3361 case lltok::kw_atomicrmw: return ParseAtomicRMW(Inst, PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +00003362 case lltok::kw_fence: return ParseFence(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003363 case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
3364 case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS);
3365 case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS);
3366 }
3367}
3368
3369/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
3370bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003371 if (Opc == Instruction::FCmp) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003372 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00003373 default: return TokError("expected fcmp predicate (e.g. 'oeq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003374 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
3375 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
3376 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
3377 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
3378 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
3379 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
3380 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
3381 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
3382 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
3383 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
3384 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
3385 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
3386 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
3387 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
3388 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
3389 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
3390 }
3391 } else {
3392 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00003393 default: return TokError("expected icmp predicate (e.g. 'eq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003394 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
3395 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
3396 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
3397 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
3398 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
3399 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
3400 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
3401 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
3402 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
3403 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
3404 }
3405 }
3406 Lex.Lex();
3407 return false;
3408}
3409
3410//===----------------------------------------------------------------------===//
3411// Terminator Instructions.
3412//===----------------------------------------------------------------------===//
3413
3414/// ParseRet - Parse a return instruction.
Chris Lattner596760d2009-12-29 21:25:40 +00003415/// ::= 'ret' void (',' !dbg, !1)*
3416/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
Chris Lattner33de4272011-06-17 06:49:41 +00003417bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003418 PerFunctionState &PFS) {
3419 SMLoc TypeLoc = Lex.getLoc();
3420 Type *Ty = 0;
Chris Lattnerf880ca22009-03-09 04:49:14 +00003421 if (ParseType(Ty, true /*void allowed*/)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003422
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003423 Type *ResType = PFS.getFunction().getReturnType();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003424
Chris Lattnerfdd87902009-10-05 05:54:46 +00003425 if (Ty->isVoidTy()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003426 if (!ResType->isVoidTy())
3427 return Error(TypeLoc, "value doesn't match function result type '" +
3428 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003429
Owen Anderson55f1c092009-08-13 21:58:54 +00003430 Inst = ReturnInst::Create(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003431 return false;
3432 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003433
Chris Lattnerac161bf2009-01-02 07:01:27 +00003434 Value *RV;
3435 if (ParseValue(Ty, RV, PFS)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003436
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003437 if (ResType != RV->getType())
3438 return Error(TypeLoc, "value doesn't match function result type '" +
3439 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003440
Owen Anderson55f1c092009-08-13 21:58:54 +00003441 Inst = ReturnInst::Create(Context, RV);
Chris Lattner33de4272011-06-17 06:49:41 +00003442 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003443}
3444
3445
3446/// ParseBr
3447/// ::= 'br' TypeAndValue
3448/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3449bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
3450 LocTy Loc, Loc2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003451 Value *Op0;
3452 BasicBlock *Op1, *Op2;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003453 if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003454
Chris Lattnerac161bf2009-01-02 07:01:27 +00003455 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
3456 Inst = BranchInst::Create(BB);
3457 return false;
3458 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003459
Owen Anderson55f1c092009-08-13 21:58:54 +00003460 if (Op0->getType() != Type::getInt1Ty(Context))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003461 return Error(Loc, "branch condition must have 'i1' type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003462
Chris Lattnerac161bf2009-01-02 07:01:27 +00003463 if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003464 ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003465 ParseToken(lltok::comma, "expected ',' after true destination") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003466 ParseTypeAndBasicBlock(Op2, Loc2, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003467 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003468
Chris Lattner3ed871f2009-10-27 19:13:16 +00003469 Inst = BranchInst::Create(Op1, Op2, Op0);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003470 return false;
3471}
3472
3473/// ParseSwitch
3474/// Instruction
3475/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
3476/// JumpTable
3477/// ::= (TypeAndValue ',' TypeAndValue)*
3478bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
3479 LocTy CondLoc, BBLoc;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003480 Value *Cond;
3481 BasicBlock *DefaultBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003482 if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
3483 ParseToken(lltok::comma, "expected ',' after switch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003484 ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003485 ParseToken(lltok::lsquare, "expected '[' with switch table"))
3486 return true;
3487
Duncan Sands19d0b472010-02-16 11:11:14 +00003488 if (!Cond->getType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003489 return Error(CondLoc, "switch condition must have integer type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003490
Chris Lattnerac161bf2009-01-02 07:01:27 +00003491 // Parse the jump table pairs.
3492 SmallPtrSet<Value*, 32> SeenCases;
3493 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
3494 while (Lex.getKind() != lltok::rsquare) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003495 Value *Constant;
3496 BasicBlock *DestBB;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003497
Chris Lattnerac161bf2009-01-02 07:01:27 +00003498 if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
3499 ParseToken(lltok::comma, "expected ',' after case value") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003500 ParseTypeAndBasicBlock(DestBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003501 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003502
Chris Lattnerac161bf2009-01-02 07:01:27 +00003503 if (!SeenCases.insert(Constant))
3504 return Error(CondLoc, "duplicate case value in switch");
3505 if (!isa<ConstantInt>(Constant))
3506 return Error(CondLoc, "case value is not a constant integer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003507
Chris Lattner3ed871f2009-10-27 19:13:16 +00003508 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003509 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003510
Chris Lattnerac161bf2009-01-02 07:01:27 +00003511 Lex.Lex(); // Eat the ']'.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003512
Chris Lattner3ed871f2009-10-27 19:13:16 +00003513 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00003514 for (unsigned i = 0, e = Table.size(); i != e; ++i)
3515 SI->addCase(Table[i].first, Table[i].second);
3516 Inst = SI;
3517 return false;
3518}
3519
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003520/// ParseIndirectBr
Chris Lattner3ed871f2009-10-27 19:13:16 +00003521/// Instruction
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003522/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
3523bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003524 LocTy AddrLoc;
3525 Value *Address;
3526 if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003527 ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
3528 ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
Chris Lattner3ed871f2009-10-27 19:13:16 +00003529 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003530
Duncan Sands19d0b472010-02-16 11:11:14 +00003531 if (!Address->getType()->isPointerTy())
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003532 return Error(AddrLoc, "indirectbr address must have pointer type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003533
Chris Lattner3ed871f2009-10-27 19:13:16 +00003534 // Parse the destination list.
3535 SmallVector<BasicBlock*, 16> DestList;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003536
Chris Lattner3ed871f2009-10-27 19:13:16 +00003537 if (Lex.getKind() != lltok::rsquare) {
3538 BasicBlock *DestBB;
3539 if (ParseTypeAndBasicBlock(DestBB, PFS))
3540 return true;
3541 DestList.push_back(DestBB);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003542
Chris Lattner3ed871f2009-10-27 19:13:16 +00003543 while (EatIfPresent(lltok::comma)) {
3544 if (ParseTypeAndBasicBlock(DestBB, PFS))
3545 return true;
3546 DestList.push_back(DestBB);
3547 }
3548 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003549
Chris Lattner3ed871f2009-10-27 19:13:16 +00003550 if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
3551 return true;
3552
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003553 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
Chris Lattner3ed871f2009-10-27 19:13:16 +00003554 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
3555 IBI->addDestination(DestList[i]);
3556 Inst = IBI;
3557 return false;
3558}
3559
3560
Chris Lattnerac161bf2009-01-02 07:01:27 +00003561/// ParseInvoke
3562/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
3563/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
3564bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
3565 LocTy CallLoc = Lex.getLoc();
Bill Wendling50d27842012-10-15 20:35:56 +00003566 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003567 std::vector<unsigned> FwdRefAttrGrps;
Bill Wendling09bd1f72013-02-22 00:12:35 +00003568 LocTy NoBuiltinLoc;
Sandeep Patel68c5f472009-09-02 08:44:58 +00003569 CallingConv::ID CC;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003570 Type *RetType = 0;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003571 LocTy RetTypeLoc;
3572 ValID CalleeID;
3573 SmallVector<ParamInfo, 16> ArgList;
3574
Chris Lattner3ed871f2009-10-27 19:13:16 +00003575 BasicBlock *NormalBB, *UnwindBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003576 if (ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00003577 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00003578 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003579 ParseValID(CalleeID) ||
3580 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003581 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
3582 NoBuiltinLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003583 ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003584 ParseTypeAndBasicBlock(NormalBB, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003585 ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003586 ParseTypeAndBasicBlock(UnwindBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003587 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003588
Chris Lattnerac161bf2009-01-02 07:01:27 +00003589 // If RetType is a non-function pointer type, then this is the short syntax
3590 // for the call, which means that RetType is just the return type. Infer the
3591 // rest of the function argument types from the arguments that are present.
Chris Lattner229907c2011-07-18 04:54:35 +00003592 PointerType *PFTy = 0;
3593 FunctionType *Ty = 0;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003594 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3595 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3596 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00003597 std::vector<Type*> ParamTypes;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003598 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3599 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003600
Chris Lattnerac161bf2009-01-02 07:01:27 +00003601 if (!FunctionType::isValidReturnType(RetType))
3602 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003603
Owen Anderson4056ca92009-07-29 22:17:13 +00003604 Ty = FunctionType::get(RetType, ParamTypes, false);
3605 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003606 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003607
Chris Lattnerac161bf2009-01-02 07:01:27 +00003608 // Look up the callee.
3609 Value *Callee;
Victor Hernandez9d75c962010-01-11 22:31:58 +00003610 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003611
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003612 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00003613 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003614 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003615 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3616 AttributeSet::ReturnIndex,
3617 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003618
Chris Lattnerac161bf2009-01-02 07:01:27 +00003619 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003620
Chris Lattnerac161bf2009-01-02 07:01:27 +00003621 // Loop through FunctionType's arguments and ensure they are specified
3622 // correctly. Also, gather any parameter attributes.
3623 FunctionType::param_iterator I = Ty->param_begin();
3624 FunctionType::param_iterator E = Ty->param_end();
3625 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattner229907c2011-07-18 04:54:35 +00003626 Type *ExpectedTy = 0;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003627 if (I != E) {
3628 ExpectedTy = *I++;
3629 } else if (!Ty->isVarArg()) {
3630 return Error(ArgList[i].Loc, "too many arguments specified");
3631 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003632
Chris Lattnerac161bf2009-01-02 07:01:27 +00003633 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3634 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003635 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003636 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00003637 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3638 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00003639 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3640 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003641 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003642
Chris Lattnerac161bf2009-01-02 07:01:27 +00003643 if (I != E)
3644 return Error(CallLoc, "not enough parameters specified for call");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003645
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003646 if (FnAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003647 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3648 AttributeSet::FunctionIndex,
3649 FnAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003650
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003651 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00003652 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003653
Jay Foad5bd375a2011-07-15 08:37:34 +00003654 InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB, Args);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003655 II->setCallingConv(CC);
3656 II->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00003657 ForwardRefAttrGroups[II] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003658 Inst = II;
3659 return false;
3660}
3661
Bill Wendlingf891bf82011-07-31 06:30:59 +00003662/// ParseResume
3663/// ::= 'resume' TypeAndValue
3664bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) {
3665 Value *Exn; LocTy ExnLoc;
Bill Wendlingf891bf82011-07-31 06:30:59 +00003666 if (ParseTypeAndValue(Exn, ExnLoc, PFS))
3667 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003668
Bill Wendlingf891bf82011-07-31 06:30:59 +00003669 ResumeInst *RI = ResumeInst::Create(Exn);
3670 Inst = RI;
3671 return false;
3672}
Chris Lattnerac161bf2009-01-02 07:01:27 +00003673
3674//===----------------------------------------------------------------------===//
3675// Binary Operators.
3676//===----------------------------------------------------------------------===//
3677
3678/// ParseArithmetic
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003679/// ::= ArithmeticOps TypeAndValue ',' Value
3680///
3681/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1,
3682/// then any integer operand is allowed, if it is 2, any fp operand is allowed.
Chris Lattnerac161bf2009-01-02 07:01:27 +00003683bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003684 unsigned Opc, unsigned OperandType) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003685 LocTy Loc; Value *LHS, *RHS;
3686 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3687 ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
3688 ParseValue(LHS->getType(), RHS, PFS))
3689 return true;
3690
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003691 bool Valid;
3692 switch (OperandType) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003693 default: llvm_unreachable("Unknown operand type!");
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003694 case 0: // int or FP.
Duncan Sands9dff9be2010-02-15 16:12:20 +00003695 Valid = LHS->getType()->isIntOrIntVectorTy() ||
3696 LHS->getType()->isFPOrFPVectorTy();
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003697 break;
Duncan Sands9dff9be2010-02-15 16:12:20 +00003698 case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
3699 case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003700 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003701
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003702 if (!Valid)
3703 return Error(Loc, "invalid operand type for instruction");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003704
Chris Lattnerac161bf2009-01-02 07:01:27 +00003705 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3706 return false;
3707}
3708
3709/// ParseLogical
3710/// ::= ArithmeticOps TypeAndValue ',' Value {
3711bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
3712 unsigned Opc) {
3713 LocTy Loc; Value *LHS, *RHS;
3714 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3715 ParseToken(lltok::comma, "expected ',' in logical operation") ||
3716 ParseValue(LHS->getType(), RHS, PFS))
3717 return true;
3718
Duncan Sands9dff9be2010-02-15 16:12:20 +00003719 if (!LHS->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003720 return Error(Loc,"instruction requires integer or integer vector operands");
3721
3722 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3723 return false;
3724}
3725
3726
3727/// ParseCompare
3728/// ::= 'icmp' IPredicates TypeAndValue ',' Value
3729/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00003730bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
3731 unsigned Opc) {
3732 // Parse the integer/fp comparison predicate.
3733 LocTy Loc;
3734 unsigned Pred;
3735 Value *LHS, *RHS;
3736 if (ParseCmpPredicate(Pred, Opc) ||
3737 ParseTypeAndValue(LHS, Loc, PFS) ||
3738 ParseToken(lltok::comma, "expected ',' after compare value") ||
3739 ParseValue(LHS->getType(), RHS, PFS))
3740 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003741
Chris Lattnerac161bf2009-01-02 07:01:27 +00003742 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00003743 if (!LHS->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003744 return Error(Loc, "fcmp requires floating point operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003745 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003746 } else {
3747 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00003748 if (!LHS->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00003749 !LHS->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003750 return Error(Loc, "icmp requires integer operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003751 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003752 }
3753 return false;
3754}
3755
3756//===----------------------------------------------------------------------===//
3757// Other Instructions.
3758//===----------------------------------------------------------------------===//
3759
3760
3761/// ParseCast
3762/// ::= CastOpc TypeAndValue 'to' Type
3763bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
3764 unsigned Opc) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003765 LocTy Loc;
3766 Value *Op;
3767 Type *DestTy = 0;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003768 if (ParseTypeAndValue(Op, Loc, PFS) ||
3769 ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
3770 ParseType(DestTy))
3771 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003772
Chris Lattner89d856e2009-03-01 00:53:13 +00003773 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
3774 CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003775 return Error(Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003776 getTypeString(Op->getType()) + "' to '" +
3777 getTypeString(DestTy) + "'");
Chris Lattner89d856e2009-03-01 00:53:13 +00003778 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003779 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
3780 return false;
3781}
3782
3783/// ParseSelect
3784/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3785bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
3786 LocTy Loc;
3787 Value *Op0, *Op1, *Op2;
3788 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3789 ParseToken(lltok::comma, "expected ',' after select condition") ||
3790 ParseTypeAndValue(Op1, PFS) ||
3791 ParseToken(lltok::comma, "expected ',' after select value") ||
3792 ParseTypeAndValue(Op2, PFS))
3793 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003794
Chris Lattnerac161bf2009-01-02 07:01:27 +00003795 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
3796 return Error(Loc, Reason);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003797
Chris Lattnerac161bf2009-01-02 07:01:27 +00003798 Inst = SelectInst::Create(Op0, Op1, Op2);
3799 return false;
3800}
3801
Chris Lattnerb55ab542009-01-05 08:18:44 +00003802/// ParseVA_Arg
3803/// ::= 'va_arg' TypeAndValue ',' Type
3804bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003805 Value *Op;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003806 Type *EltTy = 0;
Chris Lattnerb55ab542009-01-05 08:18:44 +00003807 LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003808 if (ParseTypeAndValue(Op, PFS) ||
3809 ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
Chris Lattnerb55ab542009-01-05 08:18:44 +00003810 ParseType(EltTy, TypeLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003811 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003812
Chris Lattnerb55ab542009-01-05 08:18:44 +00003813 if (!EltTy->isFirstClassType())
3814 return Error(TypeLoc, "va_arg requires operand with first class type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003815
3816 Inst = new VAArgInst(Op, EltTy);
3817 return false;
3818}
3819
3820/// ParseExtractElement
3821/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
3822bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
3823 LocTy Loc;
3824 Value *Op0, *Op1;
3825 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3826 ParseToken(lltok::comma, "expected ',' after extract value") ||
3827 ParseTypeAndValue(Op1, PFS))
3828 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003829
Chris Lattnerac161bf2009-01-02 07:01:27 +00003830 if (!ExtractElementInst::isValidOperands(Op0, Op1))
3831 return Error(Loc, "invalid extractelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003832
Eric Christopherc9742252009-07-25 02:28:41 +00003833 Inst = ExtractElementInst::Create(Op0, Op1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003834 return false;
3835}
3836
3837/// ParseInsertElement
3838/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3839bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
3840 LocTy Loc;
3841 Value *Op0, *Op1, *Op2;
3842 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3843 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3844 ParseTypeAndValue(Op1, PFS) ||
3845 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3846 ParseTypeAndValue(Op2, PFS))
3847 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003848
Chris Lattnerac161bf2009-01-02 07:01:27 +00003849 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
Eric Christopherfef8db62009-07-23 01:01:32 +00003850 return Error(Loc, "invalid insertelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003851
Chris Lattnerac161bf2009-01-02 07:01:27 +00003852 Inst = InsertElementInst::Create(Op0, Op1, Op2);
3853 return false;
3854}
3855
3856/// ParseShuffleVector
3857/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3858bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
3859 LocTy Loc;
3860 Value *Op0, *Op1, *Op2;
3861 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3862 ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
3863 ParseTypeAndValue(Op1, PFS) ||
3864 ParseToken(lltok::comma, "expected ',' after shuffle value") ||
3865 ParseTypeAndValue(Op2, PFS))
3866 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003867
Chris Lattnerac161bf2009-01-02 07:01:27 +00003868 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
Pete Cooperc1a6f982012-02-01 23:43:12 +00003869 return Error(Loc, "invalid shufflevector operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003870
Chris Lattnerac161bf2009-01-02 07:01:27 +00003871 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
3872 return false;
3873}
3874
3875/// ParsePHI
Chris Lattnerc05471e2009-10-18 05:27:44 +00003876/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
Chris Lattnerf4f03422009-12-30 05:27:33 +00003877int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003878 Type *Ty = 0; LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003879 Value *Op0, *Op1;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003880
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003881 if (ParseType(Ty, TypeLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003882 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
3883 ParseValue(Ty, Op0, PFS) ||
3884 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00003885 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003886 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3887 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003888
Chris Lattnerf4f03422009-12-30 05:27:33 +00003889 bool AteExtraComma = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003890 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
3891 while (1) {
3892 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003893
Chris Lattner3822f632009-01-02 08:05:26 +00003894 if (!EatIfPresent(lltok::comma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003895 break;
3896
Chris Lattnerf4f03422009-12-30 05:27:33 +00003897 if (Lex.getKind() == lltok::MetadataVar) {
3898 AteExtraComma = true;
Devang Patel8f842d32009-10-16 18:45:49 +00003899 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00003900 }
Devang Patel8f842d32009-10-16 18:45:49 +00003901
Chris Lattner3822f632009-01-02 08:05:26 +00003902 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003903 ParseValue(Ty, Op0, PFS) ||
3904 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00003905 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003906 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3907 return true;
3908 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003909
Chris Lattnerac161bf2009-01-02 07:01:27 +00003910 if (!Ty->isFirstClassType())
3911 return Error(TypeLoc, "phi node must have first class type");
3912
Jay Foad52131342011-03-30 11:28:46 +00003913 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00003914 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
3915 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
3916 Inst = PN;
Chris Lattnerf4f03422009-12-30 05:27:33 +00003917 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003918}
3919
Bill Wendlingfae14752011-08-12 20:24:12 +00003920/// ParseLandingPad
3921/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
3922/// Clause
3923/// ::= 'catch' TypeAndValue
3924/// ::= 'filter'
3925/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
3926bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
3927 Type *Ty = 0; LocTy TyLoc;
3928 Value *PersFn; LocTy PersFnLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00003929
3930 if (ParseType(Ty, TyLoc) ||
3931 ParseToken(lltok::kw_personality, "expected 'personality'") ||
3932 ParseTypeAndValue(PersFn, PersFnLoc, PFS))
3933 return true;
3934
3935 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, 0);
3936 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
3937
3938 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
3939 LandingPadInst::ClauseType CT;
3940 if (EatIfPresent(lltok::kw_catch))
3941 CT = LandingPadInst::Catch;
3942 else if (EatIfPresent(lltok::kw_filter))
3943 CT = LandingPadInst::Filter;
3944 else
3945 return TokError("expected 'catch' or 'filter' clause type");
3946
3947 Value *V; LocTy VLoc;
3948 if (ParseTypeAndValue(V, VLoc, PFS)) {
3949 delete LP;
3950 return true;
3951 }
3952
Bill Wendlinga52aa3c2011-08-12 20:52:25 +00003953 // A 'catch' type expects a non-array constant. A filter clause expects an
3954 // array constant.
3955 if (CT == LandingPadInst::Catch) {
3956 if (isa<ArrayType>(V->getType()))
3957 Error(VLoc, "'catch' clause has an invalid type");
3958 } else {
3959 if (!isa<ArrayType>(V->getType()))
3960 Error(VLoc, "'filter' clause has an invalid type");
3961 }
3962
Bill Wendlingfae14752011-08-12 20:24:12 +00003963 LP->addClause(V);
3964 }
3965
3966 Inst = LP;
3967 return false;
3968}
3969
Chris Lattnerac161bf2009-01-02 07:01:27 +00003970/// ParseCall
3971/// ::= 'tail'? 'call' OptionalCallingConv OptionalAttrs Type Value
3972/// ParameterList OptionalAttrs
3973bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
3974 bool isTail) {
Bill Wendling50d27842012-10-15 20:35:56 +00003975 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003976 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00003977 LocTy BuiltinLoc;
Sandeep Patel68c5f472009-09-02 08:44:58 +00003978 CallingConv::ID CC;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003979 Type *RetType = 0;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003980 LocTy RetTypeLoc;
3981 ValID CalleeID;
3982 SmallVector<ParamInfo, 16> ArgList;
3983 LocTy CallLoc = Lex.getLoc();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003984
Chris Lattnerac161bf2009-01-02 07:01:27 +00003985 if ((isTail && ParseToken(lltok::kw_call, "expected 'tail call'")) ||
3986 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00003987 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00003988 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003989 ParseValID(CalleeID) ||
3990 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003991 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00003992 BuiltinLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003993 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003994
Chris Lattnerac161bf2009-01-02 07:01:27 +00003995 // If RetType is a non-function pointer type, then this is the short syntax
3996 // for the call, which means that RetType is just the return type. Infer the
3997 // rest of the function argument types from the arguments that are present.
Chris Lattner229907c2011-07-18 04:54:35 +00003998 PointerType *PFTy = 0;
3999 FunctionType *Ty = 0;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004000 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
4001 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
4002 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00004003 std::vector<Type*> ParamTypes;
Eli Friedman6cf51412010-07-24 23:06:59 +00004004 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
4005 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004006
Chris Lattnerac161bf2009-01-02 07:01:27 +00004007 if (!FunctionType::isValidReturnType(RetType))
4008 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004009
Owen Anderson4056ca92009-07-29 22:17:13 +00004010 Ty = FunctionType::get(RetType, ParamTypes, false);
4011 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004012 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004013
Chris Lattnerac161bf2009-01-02 07:01:27 +00004014 // Look up the callee.
4015 Value *Callee;
Victor Hernandez9d75c962010-01-11 22:31:58 +00004016 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004017
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004018 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00004019 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004020 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004021 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4022 AttributeSet::ReturnIndex,
4023 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004024
Chris Lattnerac161bf2009-01-02 07:01:27 +00004025 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004026
Chris Lattnerac161bf2009-01-02 07:01:27 +00004027 // Loop through FunctionType's arguments and ensure they are specified
4028 // correctly. Also, gather any parameter attributes.
4029 FunctionType::param_iterator I = Ty->param_begin();
4030 FunctionType::param_iterator E = Ty->param_end();
4031 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattner229907c2011-07-18 04:54:35 +00004032 Type *ExpectedTy = 0;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004033 if (I != E) {
4034 ExpectedTy = *I++;
4035 } else if (!Ty->isVarArg()) {
4036 return Error(ArgList[i].Loc, "too many arguments specified");
4037 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004038
Chris Lattnerac161bf2009-01-02 07:01:27 +00004039 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
4040 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00004041 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004042 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00004043 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
4044 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00004045 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
4046 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004047 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004048
Chris Lattnerac161bf2009-01-02 07:01:27 +00004049 if (I != E)
4050 return Error(CallLoc, "not enough parameters specified for call");
4051
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004052 if (FnAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004053 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4054 AttributeSet::FunctionIndex,
4055 FnAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00004056
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004057 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00004058 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004059
Jay Foad5bd375a2011-07-15 08:37:34 +00004060 CallInst *CI = CallInst::Create(Callee, Args);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004061 CI->setTailCall(isTail);
4062 CI->setCallingConv(CC);
4063 CI->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00004064 ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004065 Inst = CI;
4066 return false;
4067}
4068
4069//===----------------------------------------------------------------------===//
4070// Memory Instructions.
4071//===----------------------------------------------------------------------===//
4072
4073/// ParseAlloc
David Majnemerc4ab61c2014-03-09 06:41:58 +00004074/// ::= 'alloca' 'inalloca'? Type (',' TypeAndValue)? (',' 'align' i32)?
Chris Lattner78103722011-06-17 03:16:47 +00004075int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004076 Value *Size = 0;
Chris Lattner200e0752009-07-02 23:08:13 +00004077 LocTy SizeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004078 unsigned Alignment = 0;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004079 Type *Ty = 0;
David Majnemerc4ab61c2014-03-09 06:41:58 +00004080
4081 bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
4082
Chris Lattner3822f632009-01-02 08:05:26 +00004083 if (ParseType(Ty)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004084
Chris Lattnerb2f39502009-12-30 05:44:30 +00004085 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00004086 if (EatIfPresent(lltok::comma)) {
David Majnemerc4ab61c2014-03-09 06:41:58 +00004087 if (Lex.getKind() == lltok::kw_align) {
4088 if (ParseOptionalAlignment(Alignment)) return true;
4089 } else if (Lex.getKind() == lltok::MetadataVar) {
4090 AteExtraComma = true;
4091 } else {
4092 if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
4093 ParseOptionalCommaAlign(Alignment, AteExtraComma))
4094 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004095 }
4096 }
4097
Dan Gohman2140a742010-05-28 01:14:11 +00004098 if (Size && !Size->getType()->isIntegerTy())
4099 return Error(SizeLoc, "element count must have integer type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004100
Reid Kleckner436c42e2014-01-17 23:58:17 +00004101 AllocaInst *AI = new AllocaInst(Ty, Size, Alignment);
4102 AI->setUsedWithInAlloca(IsInAlloca);
4103 Inst = AI;
Chris Lattner78103722011-06-17 03:16:47 +00004104 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004105}
4106
4107/// ParseLoad
Eli Friedman02e737b2011-08-12 22:50:01 +00004108/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004109/// ::= 'load' 'atomic' 'volatile'? TypeAndValue
Eli Friedman02e737b2011-08-12 22:50:01 +00004110/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00004111int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004112 Value *Val; LocTy Loc;
Devang Patelea8a4b92009-09-17 23:04:48 +00004113 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00004114 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004115 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00004116 AtomicOrdering Ordering = NotAtomic;
4117 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004118
4119 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004120 isAtomic = true;
4121 Lex.Lex();
4122 }
4123
Chris Lattnerbc639292011-11-27 06:56:53 +00004124 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004125 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004126 isVolatile = true;
4127 Lex.Lex();
4128 }
4129
Chris Lattnerb2f39502009-12-30 05:44:30 +00004130 if (ParseTypeAndValue(Val, Loc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00004131 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004132 ParseOptionalCommaAlign(Alignment, AteExtraComma))
4133 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004134
Duncan Sands19d0b472010-02-16 11:11:14 +00004135 if (!Val->getType()->isPointerTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004136 !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType())
4137 return Error(Loc, "load operand must be a pointer to a first class type");
Eli Friedman59b66882011-08-09 23:02:53 +00004138 if (isAtomic && !Alignment)
4139 return Error(Loc, "atomic load must have explicit non-zero alignment");
4140 if (Ordering == Release || Ordering == AcquireRelease)
4141 return Error(Loc, "atomic load cannot use Release ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004142
Eli Friedman59b66882011-08-09 23:02:53 +00004143 Inst = new LoadInst(Val, "", isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00004144 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004145}
4146
4147/// ParseStore
Eli Friedman02e737b2011-08-12 22:50:01 +00004148
4149/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
4150/// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
Eli Friedman59b66882011-08-09 23:02:53 +00004151/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00004152int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004153 Value *Val, *Ptr; LocTy Loc, PtrLoc;
Devang Patelea8a4b92009-09-17 23:04:48 +00004154 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00004155 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004156 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00004157 AtomicOrdering Ordering = NotAtomic;
4158 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004159
4160 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004161 isAtomic = true;
4162 Lex.Lex();
4163 }
4164
Chris Lattnerbc639292011-11-27 06:56:53 +00004165 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004166 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004167 isVolatile = true;
4168 Lex.Lex();
4169 }
4170
Chris Lattnerac161bf2009-01-02 07:01:27 +00004171 if (ParseTypeAndValue(Val, Loc, PFS) ||
4172 ParseToken(lltok::comma, "expected ',' after store operand") ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004173 ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00004174 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004175 ParseOptionalCommaAlign(Alignment, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004176 return true;
Devang Patelea8a4b92009-09-17 23:04:48 +00004177
Duncan Sands19d0b472010-02-16 11:11:14 +00004178 if (!Ptr->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004179 return Error(PtrLoc, "store operand must be a pointer");
4180 if (!Val->getType()->isFirstClassType())
4181 return Error(Loc, "store operand must be a first class value");
4182 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4183 return Error(Loc, "stored value and pointer type do not match");
Eli Friedman59b66882011-08-09 23:02:53 +00004184 if (isAtomic && !Alignment)
4185 return Error(Loc, "atomic store must have explicit non-zero alignment");
4186 if (Ordering == Acquire || Ordering == AcquireRelease)
4187 return Error(Loc, "atomic store cannot use Acquire ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004188
Eli Friedman59b66882011-08-09 23:02:53 +00004189 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00004190 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004191}
4192
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004193/// ParseCmpXchg
Eli Friedman02e737b2011-08-12 22:50:01 +00004194/// ::= 'cmpxchg' 'volatile'? TypeAndValue ',' TypeAndValue ',' TypeAndValue
Tim Northovere94a5182014-03-11 10:48:52 +00004195/// 'singlethread'? AtomicOrdering AtomicOrdering
Eli Friedman02e737b2011-08-12 22:50:01 +00004196int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004197 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
4198 bool AteExtraComma = false;
Tim Northovere94a5182014-03-11 10:48:52 +00004199 AtomicOrdering SuccessOrdering = NotAtomic;
4200 AtomicOrdering FailureOrdering = NotAtomic;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004201 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004202 bool isVolatile = false;
4203
4204 if (EatIfPresent(lltok::kw_volatile))
4205 isVolatile = true;
4206
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004207 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4208 ParseToken(lltok::comma, "expected ',' after cmpxchg address") ||
4209 ParseTypeAndValue(Cmp, CmpLoc, PFS) ||
4210 ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
4211 ParseTypeAndValue(New, NewLoc, PFS) ||
Tim Northovere94a5182014-03-11 10:48:52 +00004212 ParseScopeAndOrdering(true /*Always atomic*/, Scope, SuccessOrdering) ||
4213 ParseOrdering(FailureOrdering))
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004214 return true;
4215
Tim Northovere94a5182014-03-11 10:48:52 +00004216 if (SuccessOrdering == Unordered || FailureOrdering == Unordered)
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004217 return TokError("cmpxchg cannot be unordered");
Tim Northovere94a5182014-03-11 10:48:52 +00004218 if (SuccessOrdering < FailureOrdering)
4219 return TokError("cmpxchg must be at least as ordered on success as failure");
4220 if (FailureOrdering == Release || FailureOrdering == AcquireRelease)
4221 return TokError("cmpxchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004222 if (!Ptr->getType()->isPointerTy())
4223 return Error(PtrLoc, "cmpxchg operand must be a pointer");
4224 if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType())
4225 return Error(CmpLoc, "compare value and pointer type do not match");
4226 if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType())
4227 return Error(NewLoc, "new value and pointer type do not match");
4228 if (!New->getType()->isIntegerTy())
4229 return Error(NewLoc, "cmpxchg operand must be an integer");
4230 unsigned Size = New->getType()->getPrimitiveSizeInBits();
4231 if (Size < 8 || (Size & (Size - 1)))
4232 return Error(NewLoc, "cmpxchg operand must be power-of-two byte-sized"
4233 " integer");
4234
Tim Northovere94a5182014-03-11 10:48:52 +00004235 AtomicCmpXchgInst *CXI = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
4236 FailureOrdering, Scope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004237 CXI->setVolatile(isVolatile);
4238 Inst = CXI;
4239 return AteExtraComma ? InstExtraComma : InstNormal;
4240}
4241
4242/// ParseAtomicRMW
Eli Friedman02e737b2011-08-12 22:50:01 +00004243/// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
4244/// 'singlethread'? AtomicOrdering
4245int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004246 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
4247 bool AteExtraComma = false;
4248 AtomicOrdering Ordering = NotAtomic;
4249 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004250 bool isVolatile = false;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004251 AtomicRMWInst::BinOp Operation;
Eli Friedman02e737b2011-08-12 22:50:01 +00004252
4253 if (EatIfPresent(lltok::kw_volatile))
4254 isVolatile = true;
4255
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004256 switch (Lex.getKind()) {
4257 default: return TokError("expected binary operation in atomicrmw");
4258 case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break;
4259 case lltok::kw_add: Operation = AtomicRMWInst::Add; break;
4260 case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break;
4261 case lltok::kw_and: Operation = AtomicRMWInst::And; break;
4262 case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break;
4263 case lltok::kw_or: Operation = AtomicRMWInst::Or; break;
4264 case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break;
4265 case lltok::kw_max: Operation = AtomicRMWInst::Max; break;
4266 case lltok::kw_min: Operation = AtomicRMWInst::Min; break;
4267 case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break;
4268 case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break;
4269 }
4270 Lex.Lex(); // Eat the operation.
4271
4272 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4273 ParseToken(lltok::comma, "expected ',' after atomicrmw address") ||
4274 ParseTypeAndValue(Val, ValLoc, PFS) ||
4275 ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4276 return true;
4277
4278 if (Ordering == Unordered)
4279 return TokError("atomicrmw cannot be unordered");
4280 if (!Ptr->getType()->isPointerTy())
4281 return Error(PtrLoc, "atomicrmw operand must be a pointer");
4282 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4283 return Error(ValLoc, "atomicrmw value and pointer type do not match");
4284 if (!Val->getType()->isIntegerTy())
4285 return Error(ValLoc, "atomicrmw operand must be an integer");
4286 unsigned Size = Val->getType()->getPrimitiveSizeInBits();
4287 if (Size < 8 || (Size & (Size - 1)))
4288 return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
4289 " integer");
4290
4291 AtomicRMWInst *RMWI =
4292 new AtomicRMWInst(Operation, Ptr, Val, Ordering, Scope);
4293 RMWI->setVolatile(isVolatile);
4294 Inst = RMWI;
4295 return AteExtraComma ? InstExtraComma : InstNormal;
4296}
4297
Eli Friedmanfee02c62011-07-25 23:16:38 +00004298/// ParseFence
4299/// ::= 'fence' 'singlethread'? AtomicOrdering
4300int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) {
4301 AtomicOrdering Ordering = NotAtomic;
4302 SynchronizationScope Scope = CrossThread;
4303 if (ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4304 return true;
4305
4306 if (Ordering == Unordered)
4307 return TokError("fence cannot be unordered");
4308 if (Ordering == Monotonic)
4309 return TokError("fence cannot be monotonic");
4310
4311 Inst = new FenceInst(Context, Ordering, Scope);
4312 return InstNormal;
4313}
4314
Chris Lattnerac161bf2009-01-02 07:01:27 +00004315/// ParseGetElementPtr
Dan Gohman1639c392009-07-27 21:53:46 +00004316/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
Chris Lattnerf4f03422009-12-30 05:27:33 +00004317int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00004318 Value *Ptr = 0;
4319 Value *Val = 0;
4320 LocTy Loc, EltLoc;
Dan Gohman1639c392009-07-27 21:53:46 +00004321
Dan Gohman16cbbe42009-07-29 15:58:36 +00004322 bool InBounds = EatIfPresent(lltok::kw_inbounds);
Dan Gohman1639c392009-07-27 21:53:46 +00004323
Chris Lattner3822f632009-01-02 08:05:26 +00004324 if (ParseTypeAndValue(Ptr, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004325
Eli Benderskyd9806682013-04-22 17:03:42 +00004326 Type *BaseType = Ptr->getType();
4327 PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType());
4328 if (!BasePointerType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004329 return Error(Loc, "base of getelementptr must be a pointer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004330
Chris Lattnerac161bf2009-01-02 07:01:27 +00004331 SmallVector<Value*, 16> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004332 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00004333 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf4f03422009-12-30 05:27:33 +00004334 if (Lex.getKind() == lltok::MetadataVar) {
4335 AteExtraComma = true;
Devang Patel52b17452009-10-13 18:49:55 +00004336 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004337 }
Chris Lattner3822f632009-01-02 08:05:26 +00004338 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
Nadav Rotem3924cb02011-12-05 06:29:09 +00004339 if (!Val->getType()->getScalarType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004340 return Error(EltLoc, "getelementptr index must be an integer");
Nadav Rotem3924cb02011-12-05 06:29:09 +00004341 if (Val->getType()->isVectorTy() != Ptr->getType()->isVectorTy())
4342 return Error(EltLoc, "getelementptr index type missmatch");
4343 if (Val->getType()->isVectorTy()) {
4344 unsigned ValNumEl = cast<VectorType>(Val->getType())->getNumElements();
4345 unsigned PtrNumEl = cast<VectorType>(Ptr->getType())->getNumElements();
4346 if (ValNumEl != PtrNumEl)
4347 return Error(EltLoc,
4348 "getelementptr vector index has a wrong number of elements");
4349 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004350 Indices.push_back(Val);
4351 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004352
Eli Benderskyd9806682013-04-22 17:03:42 +00004353 if (!Indices.empty() && !BasePointerType->getElementType()->isSized())
4354 return Error(Loc, "base element of getelementptr must be sized");
4355
4356 if (!GetElementPtrInst::getIndexedType(BaseType, Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004357 return Error(Loc, "invalid getelementptr indices");
Jay Foadd1b78492011-07-25 09:48:08 +00004358 Inst = GetElementPtrInst::Create(Ptr, Indices);
Dan Gohman1639c392009-07-27 21:53:46 +00004359 if (InBounds)
Dan Gohman1b849082009-09-07 23:54:19 +00004360 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004361 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004362}
4363
4364/// ParseExtractValue
4365/// ::= 'extractvalue' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00004366int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004367 Value *Val; LocTy Loc;
4368 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004369 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004370 if (ParseTypeAndValue(Val, Loc, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00004371 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004372 return true;
4373
Chris Lattner392be582010-02-12 20:49:41 +00004374 if (!Val->getType()->isAggregateType())
4375 return Error(Loc, "extractvalue operand must be aggregate type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004376
Jay Foad57aa6362011-07-13 10:26:04 +00004377 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004378 return Error(Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00004379 Inst = ExtractValueInst::Create(Val, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004380 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004381}
4382
4383/// ParseInsertValue
4384/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00004385int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004386 Value *Val0, *Val1; LocTy Loc0, Loc1;
4387 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004388 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004389 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
4390 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
4391 ParseTypeAndValue(Val1, Loc1, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00004392 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004393 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004394
Chris Lattner392be582010-02-12 20:49:41 +00004395 if (!Val0->getType()->isAggregateType())
4396 return Error(Loc0, "insertvalue operand must be aggregate type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004397
Jay Foad57aa6362011-07-13 10:26:04 +00004398 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004399 return Error(Loc0, "invalid indices for insertvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00004400 Inst = InsertValueInst::Create(Val0, Val1, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004401 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004402}
Nick Lewycky49f89192009-04-04 07:22:01 +00004403
4404//===----------------------------------------------------------------------===//
4405// Embedded metadata.
4406//===----------------------------------------------------------------------===//
4407
4408/// ParseMDNodeVector
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004409/// ::= Element (',' Element)*
4410/// Element
4411/// ::= 'null' | TypeAndValue
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00004412bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts,
Victor Hernandezb8fd1522010-01-10 07:14:18 +00004413 PerFunctionState *PFS) {
Dan Gohman1e0213a2010-07-13 19:33:27 +00004414 // Check for an empty list.
4415 if (Lex.getKind() == lltok::rbrace)
4416 return false;
4417
Nick Lewycky49f89192009-04-04 07:22:01 +00004418 do {
Chris Lattnera01ddfc2009-12-30 04:42:57 +00004419 // Null is a special case since it is typeless.
4420 if (EatIfPresent(lltok::kw_null)) {
4421 Elts.push_back(0);
4422 continue;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004423 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004424
Chris Lattnera01ddfc2009-12-30 04:42:57 +00004425 Value *V = 0;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004426 if (ParseTypeAndValue(V, PFS)) return true;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004427 Elts.push_back(V);
Nick Lewycky49f89192009-04-04 07:22:01 +00004428 } while (EatIfPresent(lltok::comma));
4429
4430 return false;
4431}