blob: f44420685203c85ae6487d273bd964479af2100f [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
Craig Topper2617dcc2014-04-15 06:32:26 +000060 if (SlotNo >= NumberedMetadata.size() ||
61 NumberedMetadata[SlotNo] == nullptr)
Chris Lattner8eff0152010-04-01 05:14:45 +000062 return Error(MDList[i].Loc, "use of undefined metadata '!" +
Benjamin Kramerc7583112010-09-27 17:42:11 +000063 Twine(SlotNo) + "'");
Chris Lattner8eff0152010-04-01 05:14:45 +000064 Inst->setMetadata(MDList[i].MDKind, NumberedMetadata[SlotNo]);
65 }
66 }
67 ForwardRefInstMetadata.clear();
68 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +000069
Manman Ren209b17c2013-09-28 00:22:27 +000070 for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
71 UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
72
Bill Wendlingb32b0412013-02-08 06:32:06 +000073 // Handle any function attribute group forward references.
74 for (std::map<Value*, std::vector<unsigned> >::iterator
75 I = ForwardRefAttrGroups.begin(), E = ForwardRefAttrGroups.end();
76 I != E; ++I) {
77 Value *V = I->first;
78 std::vector<unsigned> &Vec = I->second;
79 AttrBuilder B;
80
81 for (std::vector<unsigned>::iterator VI = Vec.begin(), VE = Vec.end();
82 VI != VE; ++VI)
83 B.merge(NumberedAttrBuilders[*VI]);
84
85 if (Function *Fn = dyn_cast<Function>(V)) {
86 AttributeSet AS = Fn->getAttributes();
87 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
88 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
89 AS.getFnAttributes());
90
91 FnAttrs.merge(B);
92
93 // If the alignment was parsed as an attribute, move to the alignment
94 // field.
95 if (FnAttrs.hasAlignmentAttr()) {
96 Fn->setAlignment(FnAttrs.getAlignment());
97 FnAttrs.removeAttribute(Attribute::Alignment);
98 }
99
100 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
101 AttributeSet::get(Context,
102 AttributeSet::FunctionIndex,
103 FnAttrs));
104 Fn->setAttributes(AS);
105 } else if (CallInst *CI = dyn_cast<CallInst>(V)) {
106 AttributeSet AS = CI->getAttributes();
107 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
108 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
109 AS.getFnAttributes());
Bill Wendling59dce372013-02-12 10:13:06 +0000110 FnAttrs.merge(B);
Bill Wendlingb32b0412013-02-08 06:32:06 +0000111 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
112 AttributeSet::get(Context,
113 AttributeSet::FunctionIndex,
114 FnAttrs));
115 CI->setAttributes(AS);
116 } else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) {
117 AttributeSet AS = II->getAttributes();
118 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
119 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
120 AS.getFnAttributes());
Bill Wendling59dce372013-02-12 10:13:06 +0000121 FnAttrs.merge(B);
Bill Wendlingb32b0412013-02-08 06:32:06 +0000122 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
123 AttributeSet::get(Context,
124 AttributeSet::FunctionIndex,
125 FnAttrs));
126 II->setAttributes(AS);
127 } else {
128 llvm_unreachable("invalid object with forward attribute group reference");
129 }
130 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000131
Chris Lattner3432c622009-10-28 03:39:23 +0000132 // If there are entries in ForwardRefBlockAddresses at this point, they are
133 // references after the function was defined. Resolve those now.
134 while (!ForwardRefBlockAddresses.empty()) {
135 // Okay, we are referencing an already-parsed function, resolve them now.
Craig Topper2617dcc2014-04-15 06:32:26 +0000136 Function *TheFn = nullptr;
Chris Lattner3432c622009-10-28 03:39:23 +0000137 const ValID &Fn = ForwardRefBlockAddresses.begin()->first;
138 if (Fn.Kind == ValID::t_GlobalName)
139 TheFn = M->getFunction(Fn.StrVal);
140 else if (Fn.UIntVal < NumberedVals.size())
141 TheFn = dyn_cast<Function>(NumberedVals[Fn.UIntVal]);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000142
Craig Topper2617dcc2014-04-15 06:32:26 +0000143 if (!TheFn)
Chris Lattner3432c622009-10-28 03:39:23 +0000144 return Error(Fn.Loc, "unknown function referenced by blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000145
Chris Lattner3432c622009-10-28 03:39:23 +0000146 // Resolve all these references.
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000147 if (ResolveForwardRefBlockAddresses(TheFn,
Chris Lattner3432c622009-10-28 03:39:23 +0000148 ForwardRefBlockAddresses.begin()->second,
Craig Topper2617dcc2014-04-15 06:32:26 +0000149 nullptr))
Chris Lattner3432c622009-10-28 03:39:23 +0000150 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000151
Chris Lattner3432c622009-10-28 03:39:23 +0000152 ForwardRefBlockAddresses.erase(ForwardRefBlockAddresses.begin());
153 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000154
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000155 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i)
156 if (NumberedTypes[i].second.isValid())
157 return Error(NumberedTypes[i].second,
158 "use of undefined type '%" + Twine(i) + "'");
159
160 for (StringMap<std::pair<Type*, LocTy> >::iterator I =
161 NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I)
162 if (I->second.second.isValid())
163 return Error(I->second.second,
164 "use of undefined type named '" + I->getKey() + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000165
Chris Lattnerac161bf2009-01-02 07:01:27 +0000166 if (!ForwardRefVals.empty())
167 return Error(ForwardRefVals.begin()->second.second,
168 "use of undefined value '@" + ForwardRefVals.begin()->first +
169 "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000170
Chris Lattnerac161bf2009-01-02 07:01:27 +0000171 if (!ForwardRefValIDs.empty())
172 return Error(ForwardRefValIDs.begin()->second.second,
173 "use of undefined value '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000174 Twine(ForwardRefValIDs.begin()->first) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000175
Devang Pateld2541152009-07-08 19:23:54 +0000176 if (!ForwardRefMDNodes.empty())
177 return Error(ForwardRefMDNodes.begin()->second.second,
178 "use of undefined metadata '!" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000179 Twine(ForwardRefMDNodes.begin()->first) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000180
Devang Pateld2541152009-07-08 19:23:54 +0000181
Chris Lattnerac161bf2009-01-02 07:01:27 +0000182 // Look for intrinsic functions and CallInst that need to be upgraded
183 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; )
184 UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000185
Manman Ren8b4306c2013-12-02 21:29:56 +0000186 UpgradeDebugInfo(*M);
187
Chris Lattnerac161bf2009-01-02 07:01:27 +0000188 return false;
189}
190
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000191bool LLParser::ResolveForwardRefBlockAddresses(Function *TheFn,
Chris Lattner3432c622009-10-28 03:39:23 +0000192 std::vector<std::pair<ValID, GlobalValue*> > &Refs,
193 PerFunctionState *PFS) {
194 // Loop over all the references, resolving them.
195 for (unsigned i = 0, e = Refs.size(); i != e; ++i) {
196 BasicBlock *Res;
Chris Lattneraa99c942009-11-01 01:27:45 +0000197 if (PFS) {
Chris Lattner3432c622009-10-28 03:39:23 +0000198 if (Refs[i].first.Kind == ValID::t_LocalName)
199 Res = PFS->GetBB(Refs[i].first.StrVal, Refs[i].first.Loc);
Chris Lattneraa99c942009-11-01 01:27:45 +0000200 else
Chris Lattner3432c622009-10-28 03:39:23 +0000201 Res = PFS->GetBB(Refs[i].first.UIntVal, Refs[i].first.Loc);
202 } else if (Refs[i].first.Kind == ValID::t_LocalID) {
203 return Error(Refs[i].first.Loc,
Chris Lattnera38a4df2009-11-02 18:28:45 +0000204 "cannot take address of numeric label after the function is defined");
Chris Lattner3432c622009-10-28 03:39:23 +0000205 } else {
206 Res = dyn_cast_or_null<BasicBlock>(
207 TheFn->getValueSymbolTable().lookup(Refs[i].first.StrVal));
208 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000209
Craig Topper2617dcc2014-04-15 06:32:26 +0000210 if (!Res)
Chris Lattner3432c622009-10-28 03:39:23 +0000211 return Error(Refs[i].first.Loc,
212 "referenced value is not a basic block");
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000213
Chris Lattner3432c622009-10-28 03:39:23 +0000214 // Get the BlockAddress for this and update references to use it.
215 BlockAddress *BA = BlockAddress::get(TheFn, Res);
216 Refs[i].second->replaceAllUsesWith(BA);
217 Refs[i].second->eraseFromParent();
218 }
219 return false;
220}
221
222
Chris Lattnerac161bf2009-01-02 07:01:27 +0000223//===----------------------------------------------------------------------===//
224// Top-Level Entities
225//===----------------------------------------------------------------------===//
226
227bool LLParser::ParseTopLevelEntities() {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000228 while (1) {
229 switch (Lex.getKind()) {
230 default: return TokError("expected top-level entity");
231 case lltok::Eof: return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000232 case lltok::kw_declare: if (ParseDeclare()) return true; break;
233 case lltok::kw_define: if (ParseDefine()) return true; break;
234 case lltok::kw_module: if (ParseModuleAsm()) return true; break;
235 case lltok::kw_target: if (ParseTargetDefinition()) return true; break;
Bill Wendling706d3d62012-11-28 08:41:48 +0000236 case lltok::kw_deplibs: if (ParseDepLibs()) return true; break;
Dan Gohman466876b2009-08-12 23:32:33 +0000237 case lltok::LocalVarID: if (ParseUnnamedType()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000238 case lltok::LocalVar: if (ParseNamedType()) return true; break;
Dan Gohman466876b2009-08-12 23:32:33 +0000239 case lltok::GlobalID: if (ParseUnnamedGlobal()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000240 case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break;
Chris Lattner1eed2d62009-12-30 04:56:59 +0000241 case lltok::exclaim: if (ParseStandaloneMetadata()) return true; break;
Bill Wendling63b88192013-02-06 06:52:58 +0000242 case lltok::MetadataVar:if (ParseNamedMetadata()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000243
244 // The Global variable production with no name can have many different
245 // optional leading prefixes, the production is:
Nico Rieck7157bb72014-01-14 15:22:47 +0000246 // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
247 // OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
Rafael Espindola45e6c192011-01-08 16:42:36 +0000248 // ('constant'|'global') ...
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000249 case lltok::kw_private: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000250 case lltok::kw_internal: // OptionalLinkage
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +0000251 case lltok::kw_linker_private: // Obsolete OptionalLinkage
252 case lltok::kw_linker_private_weak: // Obsolete OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000253 case lltok::kw_weak: // OptionalLinkage
254 case lltok::kw_weak_odr: // OptionalLinkage
255 case lltok::kw_linkonce: // OptionalLinkage
256 case lltok::kw_linkonce_odr: // OptionalLinkage
257 case lltok::kw_appending: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000258 case lltok::kw_common: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000259 case lltok::kw_extern_weak: // OptionalLinkage
Rafael Espindola52b74422014-06-03 20:00:20 +0000260 case lltok::kw_external: // OptionalLinkage
261 case lltok::kw_default: // OptionalVisibility
262 case lltok::kw_hidden: // OptionalVisibility
263 case lltok::kw_protected: // OptionalVisibility
Rafael Espindola63e92fb2014-06-03 20:07:32 +0000264 case lltok::kw_dllimport: // OptionalDLLStorageClass
265 case lltok::kw_dllexport: // OptionalDLLStorageClass
Rafael Espindola52b74422014-06-03 20:00:20 +0000266 case lltok::kw_thread_local: // OptionalThreadLocal
267 case lltok::kw_addrspace: // OptionalAddrSpace
268 case lltok::kw_constant: // GlobalType
269 case lltok::kw_global: { // GlobalType
Nico Rieck7157bb72014-01-14 15:22:47 +0000270 unsigned Linkage, Visibility, DLLStorageClass;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000271 bool UnnamedAddr;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000272 GlobalVariable::ThreadLocalMode TLM;
Rafael Espindola52b74422014-06-03 20:00:20 +0000273 bool HasLinkage;
274 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000275 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000276 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000277 ParseOptionalThreadLocal(TLM) ||
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000278 parseOptionalUnnamedAddr(UnnamedAddr) ||
Rafael Espindola52b74422014-06-03 20:00:20 +0000279 ParseGlobal("", SMLoc(), Linkage, HasLinkage, Visibility,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000280 DLLStorageClass, TLM, UnnamedAddr))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000281 return true;
282 break;
283 }
Bill Wendlinga7c38772013-02-09 15:48:49 +0000284
285 case lltok::kw_attributes: if (ParseUnnamedAttrGrp()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000286 }
287 }
288}
289
290
291/// toplevelentity
292/// ::= 'module' 'asm' STRINGCONSTANT
293bool LLParser::ParseModuleAsm() {
294 assert(Lex.getKind() == lltok::kw_module);
295 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000296
297 std::string AsmStr;
Chris Lattner3822f632009-01-02 08:05:26 +0000298 if (ParseToken(lltok::kw_asm, "expected 'module asm'") ||
299 ParseStringConstant(AsmStr)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000300
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000301 M->appendModuleInlineAsm(AsmStr);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000302 return false;
303}
304
305/// toplevelentity
306/// ::= 'target' 'triple' '=' STRINGCONSTANT
307/// ::= 'target' 'datalayout' '=' STRINGCONSTANT
308bool LLParser::ParseTargetDefinition() {
309 assert(Lex.getKind() == lltok::kw_target);
Chris Lattner3822f632009-01-02 08:05:26 +0000310 std::string Str;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000311 switch (Lex.Lex()) {
312 default: return TokError("unknown target property");
313 case lltok::kw_triple:
314 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +0000315 if (ParseToken(lltok::equal, "expected '=' after target triple") ||
316 ParseStringConstant(Str))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000317 return true;
Chris Lattner3822f632009-01-02 08:05:26 +0000318 M->setTargetTriple(Str);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000319 return false;
320 case lltok::kw_datalayout:
321 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +0000322 if (ParseToken(lltok::equal, "expected '=' after target datalayout") ||
323 ParseStringConstant(Str))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000324 return true;
Chris Lattner3822f632009-01-02 08:05:26 +0000325 M->setDataLayout(Str);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000326 return false;
327 }
328}
329
Bill Wendling706d3d62012-11-28 08:41:48 +0000330/// toplevelentity
331/// ::= 'deplibs' '=' '[' ']'
332/// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']'
333/// FIXME: Remove in 4.0. Currently parse, but ignore.
334bool LLParser::ParseDepLibs() {
335 assert(Lex.getKind() == lltok::kw_deplibs);
336 Lex.Lex();
337 if (ParseToken(lltok::equal, "expected '=' after deplibs") ||
338 ParseToken(lltok::lsquare, "expected '=' after deplibs"))
339 return true;
340
341 if (EatIfPresent(lltok::rsquare))
342 return false;
343
344 do {
345 std::string Str;
346 if (ParseStringConstant(Str)) return true;
347 } while (EatIfPresent(lltok::comma));
348
349 return ParseToken(lltok::rsquare, "expected ']' at end of list");
350}
351
Dan Gohman466876b2009-08-12 23:32:33 +0000352/// ParseUnnamedType:
Dan Gohman466876b2009-08-12 23:32:33 +0000353/// ::= LocalVarID '=' 'type' type
Chris Lattnerac161bf2009-01-02 07:01:27 +0000354bool LLParser::ParseUnnamedType() {
Chris Lattner07037362011-06-18 23:51:31 +0000355 LocTy TypeLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000356 unsigned TypeID = Lex.getUIntVal();
Chris Lattner8936d2b2011-06-19 00:03:46 +0000357 Lex.Lex(); // eat LocalVarID;
358
359 if (ParseToken(lltok::equal, "expected '=' after name") ||
360 ParseToken(lltok::kw_type, "expected 'type' after '='"))
361 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000362
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000363 if (TypeID >= NumberedTypes.size())
364 NumberedTypes.resize(TypeID+1);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000365
Craig Topper2617dcc2014-04-15 06:32:26 +0000366 Type *Result = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000367 if (ParseStructDefinition(TypeLoc, "",
368 NumberedTypes[TypeID], Result)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000369
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000370 if (!isa<StructType>(Result)) {
371 std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID];
372 if (Entry.first)
373 return Error(TypeLoc, "non-struct types may not be recursive");
374 Entry.first = Result;
375 Entry.second = SMLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000376 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000377
Chris Lattnerac161bf2009-01-02 07:01:27 +0000378 return false;
379}
380
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000381
Chris Lattnerac161bf2009-01-02 07:01:27 +0000382/// toplevelentity
383/// ::= LocalVar '=' 'type' type
384bool LLParser::ParseNamedType() {
385 std::string Name = Lex.getStrVal();
386 LocTy NameLoc = Lex.getLoc();
Chris Lattner3822f632009-01-02 08:05:26 +0000387 Lex.Lex(); // eat LocalVar.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000388
Chris Lattner3822f632009-01-02 08:05:26 +0000389 if (ParseToken(lltok::equal, "expected '=' after name") ||
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000390 ParseToken(lltok::kw_type, "expected 'type' after name"))
Chris Lattner3822f632009-01-02 08:05:26 +0000391 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000392
Craig Topper2617dcc2014-04-15 06:32:26 +0000393 Type *Result = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000394 if (ParseStructDefinition(NameLoc, Name,
395 NamedTypes[Name], Result)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000396
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000397 if (!isa<StructType>(Result)) {
398 std::pair<Type*, LocTy> &Entry = NamedTypes[Name];
399 if (Entry.first)
400 return Error(NameLoc, "non-struct types may not be recursive");
401 Entry.first = Result;
402 Entry.second = SMLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000403 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000404
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000405 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000406}
407
408
409/// toplevelentity
410/// ::= 'declare' FunctionHeader
411bool LLParser::ParseDeclare() {
412 assert(Lex.getKind() == lltok::kw_declare);
413 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000414
Chris Lattnerac161bf2009-01-02 07:01:27 +0000415 Function *F;
416 return ParseFunctionHeader(F, false);
417}
418
419/// toplevelentity
420/// ::= 'define' FunctionHeader '{' ...
421bool LLParser::ParseDefine() {
422 assert(Lex.getKind() == lltok::kw_define);
423 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000424
Chris Lattnerac161bf2009-01-02 07:01:27 +0000425 Function *F;
Chris Lattner3822f632009-01-02 08:05:26 +0000426 return ParseFunctionHeader(F, true) ||
427 ParseFunctionBody(*F);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000428}
429
Chris Lattner3822f632009-01-02 08:05:26 +0000430/// ParseGlobalType
431/// ::= 'constant'
432/// ::= 'global'
Chris Lattnerac161bf2009-01-02 07:01:27 +0000433bool LLParser::ParseGlobalType(bool &IsConstant) {
434 if (Lex.getKind() == lltok::kw_constant)
435 IsConstant = true;
436 else if (Lex.getKind() == lltok::kw_global)
437 IsConstant = false;
Duncan Sandsd1de45a2009-02-10 16:24:55 +0000438 else {
439 IsConstant = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000440 return TokError("expected 'global' or 'constant'");
Duncan Sandsd1de45a2009-02-10 16:24:55 +0000441 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000442 Lex.Lex();
443 return false;
444}
445
Dan Gohman466876b2009-08-12 23:32:33 +0000446/// ParseUnnamedGlobal:
447/// OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000448/// OptionalLinkage OptionalVisibility OptionalDLLStorageClass
449/// ... -> global variable
Dan Gohman466876b2009-08-12 23:32:33 +0000450/// GlobalID '=' OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000451/// GlobalID '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
452/// ... -> global variable
Dan Gohman466876b2009-08-12 23:32:33 +0000453bool LLParser::ParseUnnamedGlobal() {
454 unsigned VarID = NumberedVals.size();
455 std::string Name;
456 LocTy NameLoc = Lex.getLoc();
457
458 // Handle the GlobalID form.
459 if (Lex.getKind() == lltok::GlobalID) {
460 if (Lex.getUIntVal() != VarID)
461 return Error(Lex.getLoc(), "variable expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000462 Twine(VarID) + "'");
Dan Gohman466876b2009-08-12 23:32:33 +0000463 Lex.Lex(); // eat GlobalID;
464
465 if (ParseToken(lltok::equal, "expected '=' after name"))
466 return true;
467 }
468
469 bool HasLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000470 unsigned Linkage, Visibility, DLLStorageClass;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000471 GlobalVariable::ThreadLocalMode TLM;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000472 bool UnnamedAddr;
Dan Gohman466876b2009-08-12 23:32:33 +0000473 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000474 ParseOptionalVisibility(Visibility) ||
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000475 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000476 ParseOptionalThreadLocal(TLM) ||
477 parseOptionalUnnamedAddr(UnnamedAddr))
Dan Gohman466876b2009-08-12 23:32:33 +0000478 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000479
Dan Gohman466876b2009-08-12 23:32:33 +0000480 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
Nico Rieck7157bb72014-01-14 15:22:47 +0000481 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000482 DLLStorageClass, TLM, UnnamedAddr);
483 return ParseAlias(Name, NameLoc, Visibility, DLLStorageClass, TLM,
484 UnnamedAddr);
Dan Gohman466876b2009-08-12 23:32:33 +0000485}
486
Chris Lattnerac161bf2009-01-02 07:01:27 +0000487/// ParseNamedGlobal:
488/// GlobalVar '=' OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000489/// GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
490/// ... -> global variable
Chris Lattnerac161bf2009-01-02 07:01:27 +0000491bool LLParser::ParseNamedGlobal() {
492 assert(Lex.getKind() == lltok::GlobalVar);
493 LocTy NameLoc = Lex.getLoc();
494 std::string Name = Lex.getStrVal();
495 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000496
Chris Lattnerac161bf2009-01-02 07:01:27 +0000497 bool HasLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000498 unsigned Linkage, Visibility, DLLStorageClass;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000499 GlobalVariable::ThreadLocalMode TLM;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000500 bool UnnamedAddr;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000501 if (ParseToken(lltok::equal, "expected '=' in global variable") ||
502 ParseOptionalLinkage(Linkage, HasLinkage) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000503 ParseOptionalVisibility(Visibility) ||
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000504 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000505 ParseOptionalThreadLocal(TLM) ||
506 parseOptionalUnnamedAddr(UnnamedAddr))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000507 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000508
Chris Lattnerac161bf2009-01-02 07:01:27 +0000509 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
Nico Rieck7157bb72014-01-14 15:22:47 +0000510 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000511 DLLStorageClass, TLM, UnnamedAddr);
512 return ParseAlias(Name, NameLoc, Visibility, DLLStorageClass, TLM,
513 UnnamedAddr);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000514}
515
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000516// MDString:
517// ::= '!' STRINGCONSTANT
Chris Lattner1797fc72009-12-29 21:53:55 +0000518bool LLParser::ParseMDString(MDString *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000519 std::string Str;
520 if (ParseStringConstant(Str)) return true;
Eli Bendersky5d5e18d2014-06-25 15:41:00 +0000521 llvm::UpgradeMDStringConstant(Str);
Chris Lattner1797fc72009-12-29 21:53:55 +0000522 Result = MDString::get(Context, Str);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000523 return false;
524}
525
526// MDNode:
527// ::= '!' MDNodeNumber
Chris Lattner8eff0152010-04-01 05:14:45 +0000528//
529/// This version of ParseMDNodeID returns the slot number and null in the case
530/// of a forward reference.
531bool LLParser::ParseMDNodeID(MDNode *&Result, unsigned &SlotNo) {
532 // !{ ..., !42, ... }
533 if (ParseUInt32(SlotNo)) return true;
534
535 // Check existing MDNode.
Craig Topper2617dcc2014-04-15 06:32:26 +0000536 if (SlotNo < NumberedMetadata.size() && NumberedMetadata[SlotNo] != nullptr)
Chris Lattner8eff0152010-04-01 05:14:45 +0000537 Result = NumberedMetadata[SlotNo];
538 else
Craig Topper2617dcc2014-04-15 06:32:26 +0000539 Result = nullptr;
Chris Lattner8eff0152010-04-01 05:14:45 +0000540 return false;
541}
542
Chris Lattner6dac02a2009-12-30 04:15:23 +0000543bool LLParser::ParseMDNodeID(MDNode *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000544 // !{ ..., !42, ... }
545 unsigned MID = 0;
Chris Lattner8eff0152010-04-01 05:14:45 +0000546 if (ParseMDNodeID(Result, MID)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000547
Chris Lattner8eff0152010-04-01 05:14:45 +0000548 // If not a forward reference, just return it now.
549 if (Result) return false;
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000550
Chris Lattner8eff0152010-04-01 05:14:45 +0000551 // Otherwise, create MDNode forward reference.
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000552 MDNode *FwdNode = MDNode::getTemporary(Context, None);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000553 ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc());
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000554
Chris Lattnerfc58af22009-12-30 04:51:58 +0000555 if (NumberedMetadata.size() <= MID)
556 NumberedMetadata.resize(MID+1);
557 NumberedMetadata[MID] = FwdNode;
Chris Lattner1797fc72009-12-29 21:53:55 +0000558 Result = FwdNode;
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000559 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000560}
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000561
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000562/// ParseNamedMetadata:
Devang Patelbe626972009-07-29 00:34:02 +0000563/// !foo = !{ !1, !2 }
564bool LLParser::ParseNamedMetadata() {
Chris Lattnereafe4de2009-12-30 05:02:06 +0000565 assert(Lex.getKind() == lltok::MetadataVar);
Devang Patelbe626972009-07-29 00:34:02 +0000566 std::string Name = Lex.getStrVal();
Chris Lattnereafe4de2009-12-30 05:02:06 +0000567 Lex.Lex();
Devang Patelbe626972009-07-29 00:34:02 +0000568
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000569 if (ParseToken(lltok::equal, "expected '=' here") ||
Chris Lattner1eed2d62009-12-30 04:56:59 +0000570 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000571 ParseToken(lltok::lbrace, "Expected '{' here"))
Devang Patelbe626972009-07-29 00:34:02 +0000572 return true;
573
Dan Gohman2637cc12010-07-21 23:38:33 +0000574 NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000575 if (Lex.getKind() != lltok::rbrace)
576 do {
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000577 if (ParseToken(lltok::exclaim, "Expected '!' here"))
578 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000579
Craig Topper2617dcc2014-04-15 06:32:26 +0000580 MDNode *N = nullptr;
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000581 if (ParseMDNodeID(N)) return true;
Dan Gohman2637cc12010-07-21 23:38:33 +0000582 NMD->addOperand(N);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000583 } while (EatIfPresent(lltok::comma));
Devang Patelbe626972009-07-29 00:34:02 +0000584
585 if (ParseToken(lltok::rbrace, "expected end of metadata node"))
586 return true;
587
Devang Patelbe626972009-07-29 00:34:02 +0000588 return false;
589}
590
Devang Patel39e64d42009-07-01 19:21:12 +0000591/// ParseStandaloneMetadata:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000592/// !42 = !{...}
Devang Patel39e64d42009-07-01 19:21:12 +0000593bool LLParser::ParseStandaloneMetadata() {
Chris Lattner1eed2d62009-12-30 04:56:59 +0000594 assert(Lex.getKind() == lltok::exclaim);
Devang Patel39e64d42009-07-01 19:21:12 +0000595 Lex.Lex();
596 unsigned MetadataID = 0;
Devang Patel39e64d42009-07-01 19:21:12 +0000597
598 LocTy TyLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +0000599 Type *Ty = nullptr;
Devang Patele059ba6e2009-07-23 01:07:34 +0000600 SmallVector<Value *, 16> Elts;
Chris Lattner278bc952009-12-29 22:40:21 +0000601 if (ParseUInt32(MetadataID) ||
602 ParseToken(lltok::equal, "expected '=' here") ||
603 ParseType(Ty, TyLoc) ||
Chris Lattner1eed2d62009-12-30 04:56:59 +0000604 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattner278bc952009-12-29 22:40:21 +0000605 ParseToken(lltok::lbrace, "Expected '{' here") ||
Craig Topper2617dcc2014-04-15 06:32:26 +0000606 ParseMDNodeVector(Elts, nullptr) ||
Chris Lattner278bc952009-12-29 22:40:21 +0000607 ParseToken(lltok::rbrace, "expected end of metadata node"))
Devang Patele059ba6e2009-07-23 01:07:34 +0000608 return true;
609
Jay Foad5514afe2011-04-21 19:59:31 +0000610 MDNode *Init = MDNode::get(Context, Elts);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000611
Chris Lattnerfc58af22009-12-30 04:51:58 +0000612 // See if this was forward referenced, if so, handle it.
Chris Lattner218b22f2009-12-29 21:43:58 +0000613 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> >::iterator
Devang Pateld2541152009-07-08 19:23:54 +0000614 FI = ForwardRefMDNodes.find(MetadataID);
615 if (FI != ForwardRefMDNodes.end()) {
Dan Gohman16a5d982010-08-20 22:02:26 +0000616 MDNode *Temp = FI->second.first;
617 Temp->replaceAllUsesWith(Init);
618 MDNode::deleteTemporary(Temp);
Devang Pateld2541152009-07-08 19:23:54 +0000619 ForwardRefMDNodes.erase(FI);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000620
Chris Lattnerfc58af22009-12-30 04:51:58 +0000621 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
622 } else {
623 if (MetadataID >= NumberedMetadata.size())
624 NumberedMetadata.resize(MetadataID+1);
625
Craig Topper2617dcc2014-04-15 06:32:26 +0000626 if (NumberedMetadata[MetadataID] != nullptr)
Chris Lattnerfc58af22009-12-30 04:51:58 +0000627 return TokError("Metadata id is already used");
628 NumberedMetadata[MetadataID] = Init;
Devang Pateld2541152009-07-08 19:23:54 +0000629 }
630
Devang Patel39e64d42009-07-01 19:21:12 +0000631 return false;
632}
633
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000634static bool isValidVisibilityForLinkage(unsigned V, unsigned L) {
635 return !GlobalValue::isLocalLinkage((GlobalValue::LinkageTypes)L) ||
636 (GlobalValue::VisibilityTypes)V == GlobalValue::DefaultVisibility;
637}
638
Chris Lattnerac161bf2009-01-02 07:01:27 +0000639/// ParseAlias:
Rafael Espindola5d92ffb2014-06-03 20:25:26 +0000640/// ::= GlobalVar '=' OptionalVisibility OptionalDLLStorageClass
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000641/// OptionalThreadLocal OptionalUnNammedAddr 'alias'
642/// OptionalLinkage Aliasee
Rafael Espindola6b238632014-05-16 19:35:39 +0000643///
Chris Lattnerac161bf2009-01-02 07:01:27 +0000644/// Aliasee
Chris Lattner4c73d7a2009-04-25 21:26:00 +0000645/// ::= TypeAndValue
Chris Lattnerac161bf2009-01-02 07:01:27 +0000646///
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000647/// Everything through OptionalUnNammedAddr has already been parsed.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000648///
649bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000650 unsigned Visibility, unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000651 GlobalVariable::ThreadLocalMode TLM,
652 bool UnnamedAddr) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000653 assert(Lex.getKind() == lltok::kw_alias);
654 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000655 LocTy LinkageLoc = Lex.getLoc();
Rafael Espindola78527052013-10-06 15:10:43 +0000656 unsigned L;
657 if (ParseOptionalLinkage(L))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000658 return true;
659
Rafael Espindola78527052013-10-06 15:10:43 +0000660 GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L;
661
Rafael Espindolacaa43562013-10-09 16:07:32 +0000662 if(!GlobalAlias::isValidLinkage(Linkage))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000663 return Error(LinkageLoc, "invalid linkage type for alias");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000664
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000665 if (!isValidVisibilityForLinkage(Visibility, L))
666 return Error(LinkageLoc,
667 "symbol with local linkage must have default visibility");
668
Rafael Espindola64c1e182014-06-03 02:41:57 +0000669 Constant *Aliasee;
670 LocTy AliaseeLoc = Lex.getLoc();
671 if (Lex.getKind() != lltok::kw_bitcast &&
672 Lex.getKind() != lltok::kw_getelementptr &&
673 Lex.getKind() != lltok::kw_addrspacecast &&
674 Lex.getKind() != lltok::kw_inttoptr) {
675 if (ParseGlobalTypeAndValue(Aliasee))
Rafael Espindola6b238632014-05-16 19:35:39 +0000676 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000677 } else {
Rafael Espindola64c1e182014-06-03 02:41:57 +0000678 // The bitcast dest type is not present, it is implied by the dest type.
679 ValID ID;
680 if (ParseValID(ID))
681 return true;
682 if (ID.Kind != ValID::t_Constant)
683 return Error(AliaseeLoc, "invalid aliasee");
684 Aliasee = ID.ConstantVal;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000685 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000686
Rafael Espindola64c1e182014-06-03 02:41:57 +0000687 Type *AliaseeType = Aliasee->getType();
688 auto *PTy = dyn_cast<PointerType>(AliaseeType);
689 if (!PTy)
690 return Error(AliaseeLoc, "An alias must have pointer type");
691 Type *Ty = PTy->getElementType();
692 unsigned AddrSpace = PTy->getAddressSpace();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000693
694 // Okay, create the alias but do not insert it into the module yet.
Rafael Espindola4fe00942014-05-16 13:34:04 +0000695 std::unique_ptr<GlobalAlias> GA(
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000696 GlobalAlias::create(Ty, AddrSpace, (GlobalValue::LinkageTypes)Linkage,
697 Name, Aliasee, /*Parent*/ nullptr));
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000698 GA->setThreadLocalMode(TLM);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000699 GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000700 GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000701 GA->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000702
Chris Lattnerac161bf2009-01-02 07:01:27 +0000703 // See if this value already exists in the symbol table. If so, it is either
704 // a redefinition or a definition of a forward reference.
Chris Lattnere38317f2009-10-25 23:22:50 +0000705 if (GlobalValue *Val = M->getNamedValue(Name)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000706 // See if this was a redefinition. If so, there is no entry in
707 // ForwardRefVals.
708 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
709 I = ForwardRefVals.find(Name);
710 if (I == ForwardRefVals.end())
711 return Error(NameLoc, "redefinition of global named '@" + Name + "'");
712
713 // Otherwise, this was a definition of forward ref. Verify that types
714 // agree.
715 if (Val->getType() != GA->getType())
716 return Error(NameLoc,
717 "forward reference and definition of alias have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000718
Chris Lattnerac161bf2009-01-02 07:01:27 +0000719 // If they agree, just RAUW the old value with the alias and remove the
720 // forward ref info.
Rafael Espindolaaa273822014-05-09 21:49:17 +0000721 Val->replaceAllUsesWith(GA.get());
Chris Lattnerac161bf2009-01-02 07:01:27 +0000722 Val->eraseFromParent();
723 ForwardRefVals.erase(I);
724 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000725
Chris Lattnerac161bf2009-01-02 07:01:27 +0000726 // Insert into the module, we know its name won't collide now.
Rafael Espindolaaa273822014-05-09 21:49:17 +0000727 M->getAliasList().push_back(GA.get());
Benjamin Kramer1dc34b42010-10-16 11:28:23 +0000728 assert(GA->getName() == Name && "Should not be a name conflict!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000729
Rafael Espindolaaa273822014-05-09 21:49:17 +0000730 // The module owns this now
731 GA.release();
732
Chris Lattnerac161bf2009-01-02 07:01:27 +0000733 return false;
734}
735
736/// ParseGlobal
Nico Rieck7157bb72014-01-14 15:22:47 +0000737/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000738/// OptionalThreadLocal OptionalUnNammedAddr OptionalAddrSpace
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000739/// OptionalExternallyInitialized GlobalType Type Const
Nico Rieck7157bb72014-01-14 15:22:47 +0000740/// ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000741/// OptionalThreadLocal OptionalUnNammedAddr OptionalAddrSpace
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000742/// OptionalExternallyInitialized GlobalType Type Const
Chris Lattnerac161bf2009-01-02 07:01:27 +0000743///
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000744/// Everything up to and including OptionalUnNammedAddr has been parsed
David Majnemerc4ab61c2014-03-09 06:41:58 +0000745/// already.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000746///
747bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
748 unsigned Linkage, bool HasLinkage,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000749 unsigned Visibility, unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000750 GlobalVariable::ThreadLocalMode TLM,
751 bool UnnamedAddr) {
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000752 if (!isValidVisibilityForLinkage(Visibility, Linkage))
753 return Error(NameLoc,
754 "symbol with local linkage must have default visibility");
755
Chris Lattnerac161bf2009-01-02 07:01:27 +0000756 unsigned AddrSpace;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000757 bool IsConstant, IsExternallyInitialized;
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000758 LocTy IsExternallyInitializedLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000759 LocTy TyLoc;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000760
Craig Topper2617dcc2014-04-15 06:32:26 +0000761 Type *Ty = nullptr;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000762 if (ParseOptionalAddrSpace(AddrSpace) ||
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000763 ParseOptionalToken(lltok::kw_externally_initialized,
764 IsExternallyInitialized,
765 &IsExternallyInitializedLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000766 ParseGlobalType(IsConstant) ||
767 ParseType(Ty, TyLoc))
768 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000769
Chris Lattnerac161bf2009-01-02 07:01:27 +0000770 // If the linkage is specified and is external, then no initializer is
771 // present.
Craig Topper2617dcc2014-04-15 06:32:26 +0000772 Constant *Init = nullptr;
Nico Rieck7157bb72014-01-14 15:22:47 +0000773 if (!HasLinkage || (Linkage != GlobalValue::ExternalWeakLinkage &&
Chris Lattnerac161bf2009-01-02 07:01:27 +0000774 Linkage != GlobalValue::ExternalLinkage)) {
775 if (ParseGlobalValue(Ty, Init))
776 return true;
777 }
778
Duncan Sands19d0b472010-02-16 11:11:14 +0000779 if (Ty->isFunctionTy() || Ty->isLabelTy())
Chris Lattnerc9e1b482009-02-08 20:00:15 +0000780 return Error(TyLoc, "invalid type for global variable");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000781
Craig Topper2617dcc2014-04-15 06:32:26 +0000782 GlobalVariable *GV = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000783
784 // See if the global was forward referenced, if so, use the global.
Chris Lattner1f386b82009-02-02 07:24:28 +0000785 if (!Name.empty()) {
Chris Lattnere38317f2009-10-25 23:22:50 +0000786 if (GlobalValue *GVal = M->getNamedValue(Name)) {
787 if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal))
788 return Error(NameLoc, "redefinition of global '@" + Name + "'");
789 GV = cast<GlobalVariable>(GVal);
790 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000791 } else {
792 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
793 I = ForwardRefValIDs.find(NumberedVals.size());
794 if (I != ForwardRefValIDs.end()) {
795 GV = cast<GlobalVariable>(I->second.first);
796 ForwardRefValIDs.erase(I);
797 }
798 }
799
Craig Topper2617dcc2014-04-15 06:32:26 +0000800 if (!GV) {
801 GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, nullptr,
802 Name, nullptr, GlobalVariable::NotThreadLocal,
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000803 AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000804 } else {
805 if (GV->getType()->getElementType() != Ty)
806 return Error(TyLoc,
807 "forward reference and definition of global have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000808
Chris Lattnerac161bf2009-01-02 07:01:27 +0000809 // Move the forward-reference to the correct spot in the module.
810 M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
811 }
812
813 if (Name.empty())
814 NumberedVals.push_back(GV);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000815
Chris Lattnerac161bf2009-01-02 07:01:27 +0000816 // Set the parsed properties on the global.
817 if (Init)
818 GV->setInitializer(Init);
819 GV->setConstant(IsConstant);
820 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
821 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000822 GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000823 GV->setExternallyInitialized(IsExternallyInitialized);
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000824 GV->setThreadLocalMode(TLM);
Rafael Espindola45e6c192011-01-08 16:42:36 +0000825 GV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000826
Chris Lattnerac161bf2009-01-02 07:01:27 +0000827 // Parse attributes on the global.
828 while (Lex.getKind() == lltok::comma) {
829 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000830
Chris Lattnerac161bf2009-01-02 07:01:27 +0000831 if (Lex.getKind() == lltok::kw_section) {
832 Lex.Lex();
833 GV->setSection(Lex.getStrVal());
834 if (ParseToken(lltok::StringConstant, "expected global section string"))
835 return true;
836 } else if (Lex.getKind() == lltok::kw_align) {
837 unsigned Alignment;
838 if (ParseOptionalAlignment(Alignment)) return true;
839 GV->setAlignment(Alignment);
840 } else {
841 TokError("unknown global variable property!");
842 }
843 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000844
Chris Lattnerac161bf2009-01-02 07:01:27 +0000845 return false;
846}
847
Bill Wendling63b88192013-02-06 06:52:58 +0000848/// ParseUnnamedAttrGrp
Bill Wendlinga7c38772013-02-09 15:48:49 +0000849/// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
Bill Wendling63b88192013-02-06 06:52:58 +0000850bool LLParser::ParseUnnamedAttrGrp() {
Bill Wendlinga7c38772013-02-09 15:48:49 +0000851 assert(Lex.getKind() == lltok::kw_attributes);
Bill Wendling63b88192013-02-06 06:52:58 +0000852 LocTy AttrGrpLoc = Lex.getLoc();
Bill Wendlinga7c38772013-02-09 15:48:49 +0000853 Lex.Lex();
854
855 assert(Lex.getKind() == lltok::AttrGrpID);
Bill Wendling63b88192013-02-06 06:52:58 +0000856 unsigned VarID = Lex.getUIntVal();
Bill Wendlingb32b0412013-02-08 06:32:06 +0000857 std::vector<unsigned> unused;
Michael Gottesman41748d72013-06-27 00:25:01 +0000858 LocTy BuiltinLoc;
Bill Wendling63b88192013-02-06 06:52:58 +0000859 Lex.Lex();
860
861 if (ParseToken(lltok::equal, "expected '=' here") ||
Bill Wendling63b88192013-02-06 06:52:58 +0000862 ParseToken(lltok::lbrace, "expected '{' here") ||
Bill Wendling09bd1f72013-02-22 00:12:35 +0000863 ParseFnAttributeValuePairs(NumberedAttrBuilders[VarID], unused, true,
Michael Gottesman41748d72013-06-27 00:25:01 +0000864 BuiltinLoc) ||
Bill Wendling63b88192013-02-06 06:52:58 +0000865 ParseToken(lltok::rbrace, "expected end of attribute group"))
866 return true;
867
Bill Wendlingb32b0412013-02-08 06:32:06 +0000868 if (!NumberedAttrBuilders[VarID].hasAttributes())
Bill Wendling63b88192013-02-06 06:52:58 +0000869 return Error(AttrGrpLoc, "attribute group has no attributes");
870
871 return false;
872}
873
Bill Wendling8b0321d2013-02-08 00:52:31 +0000874/// ParseFnAttributeValuePairs
Bill Wendling63b88192013-02-06 06:52:58 +0000875/// ::= <attr> | <attr> '=' <value>
Bill Wendlingb32b0412013-02-08 06:32:06 +0000876bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B,
877 std::vector<unsigned> &FwdRefAttrGrps,
Michael Gottesman41748d72013-06-27 00:25:01 +0000878 bool inAttrGrp, LocTy &BuiltinLoc) {
Bill Wendling8b0321d2013-02-08 00:52:31 +0000879 bool HaveError = false;
880
881 B.clear();
882
Bill Wendling63b88192013-02-06 06:52:58 +0000883 while (true) {
884 lltok::Kind Token = Lex.getKind();
Michael Gottesman41748d72013-06-27 00:25:01 +0000885 if (Token == lltok::kw_builtin)
886 BuiltinLoc = Lex.getLoc();
Bill Wendling63b88192013-02-06 06:52:58 +0000887 switch (Token) {
888 default:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000889 if (!inAttrGrp) return HaveError;
Bill Wendling63b88192013-02-06 06:52:58 +0000890 return Error(Lex.getLoc(), "unterminated attribute group");
891 case lltok::rbrace:
892 // Finished.
893 return false;
894
Bill Wendlingb32b0412013-02-08 06:32:06 +0000895 case lltok::AttrGrpID: {
896 // Allow a function to reference an attribute group:
897 //
898 // define void @foo() #1 { ... }
899 if (inAttrGrp)
900 HaveError |=
901 Error(Lex.getLoc(),
902 "cannot have an attribute group reference in an attribute group");
903
904 unsigned AttrGrpNum = Lex.getUIntVal();
905 if (inAttrGrp) break;
906
907 // Save the reference to the attribute group. We'll fill it in later.
908 FwdRefAttrGrps.push_back(AttrGrpNum);
909 break;
910 }
Bill Wendling63b88192013-02-06 06:52:58 +0000911 // Target-dependent attributes:
912 case lltok::StringConstant: {
913 std::string Attr = Lex.getStrVal();
914 Lex.Lex();
915 std::string Val;
916 if (EatIfPresent(lltok::equal) &&
917 ParseStringConstant(Val))
918 return true;
919
920 B.addAttribute(Attr, Val);
Bill Wendlingb1ea9802013-02-10 10:12:50 +0000921 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000922 }
923
924 // Target-independent attributes:
925 case lltok::kw_align: {
Bill Wendlingc62789f2013-04-18 18:30:16 +0000926 // As a hack, we allow function alignment to be initially parsed as an
927 // attribute on a function declaration/definition or added to an attribute
928 // group and later moved to the alignment field.
Bill Wendling63b88192013-02-06 06:52:58 +0000929 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000930 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000931 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000932 if (ParseToken(lltok::equal, "expected '=' here") ||
933 ParseUInt32(Alignment))
934 return true;
935 } else {
936 if (ParseOptionalAlignment(Alignment))
937 return true;
938 }
Bill Wendling63b88192013-02-06 06:52:58 +0000939 B.addAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000940 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000941 }
942 case lltok::kw_alignstack: {
943 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000944 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000945 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000946 if (ParseToken(lltok::equal, "expected '=' here") ||
947 ParseUInt32(Alignment))
948 return true;
949 } else {
950 if (ParseOptionalStackAlignment(Alignment))
951 return true;
952 }
Bill Wendling63b88192013-02-06 06:52:58 +0000953 B.addStackAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000954 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000955 }
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000956 case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break;
Michael Gottesman41748d72013-06-27 00:25:01 +0000957 case lltok::kw_builtin: B.addAttribute(Attribute::Builtin); break;
Diego Novilloc6399532013-05-24 12:26:52 +0000958 case lltok::kw_cold: B.addAttribute(Attribute::Cold); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000959 case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000960 case lltok::kw_jumptable: B.addAttribute(Attribute::JumpTable); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000961 case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break;
962 case lltok::kw_naked: B.addAttribute(Attribute::Naked); break;
963 case lltok::kw_nobuiltin: B.addAttribute(Attribute::NoBuiltin); break;
964 case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break;
965 case lltok::kw_noimplicitfloat: B.addAttribute(Attribute::NoImplicitFloat); break;
966 case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break;
967 case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break;
968 case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break;
969 case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break;
970 case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000971 case lltok::kw_optnone: B.addAttribute(Attribute::OptimizeNone); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000972 case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break;
973 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
974 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
975 case lltok::kw_returns_twice: B.addAttribute(Attribute::ReturnsTwice); break;
976 case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break;
977 case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break;
978 case lltok::kw_sspstrong: B.addAttribute(Attribute::StackProtectStrong); break;
979 case lltok::kw_sanitize_address: B.addAttribute(Attribute::SanitizeAddress); break;
980 case lltok::kw_sanitize_thread: B.addAttribute(Attribute::SanitizeThread); break;
981 case lltok::kw_sanitize_memory: B.addAttribute(Attribute::SanitizeMemory); break;
982 case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000983
984 // Error handling.
985 case lltok::kw_inreg:
986 case lltok::kw_signext:
987 case lltok::kw_zeroext:
988 HaveError |=
989 Error(Lex.getLoc(),
990 "invalid use of attribute on a function");
991 break;
992 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +0000993 case lltok::kw_inalloca:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000994 case lltok::kw_nest:
995 case lltok::kw_noalias:
996 case lltok::kw_nocapture:
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000997 case lltok::kw_nonnull:
Stephen Linb8bd2322013-04-20 05:14:40 +0000998 case lltok::kw_returned:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000999 case lltok::kw_sret:
1000 HaveError |=
1001 Error(Lex.getLoc(),
1002 "invalid use of parameter-only attribute on a function");
1003 break;
Bill Wendling63b88192013-02-06 06:52:58 +00001004 }
1005
1006 Lex.Lex();
1007 }
1008}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001009
1010//===----------------------------------------------------------------------===//
1011// GlobalValue Reference/Resolution Routines.
1012//===----------------------------------------------------------------------===//
1013
1014/// GetGlobalVal - Get a value with the specified name or ID, creating a
1015/// forward reference record if needed. This can return null if the value
1016/// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +00001017GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00001018 LocTy Loc) {
Chris Lattner229907c2011-07-18 04:54:35 +00001019 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001020 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001021 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001022 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001023 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001024
Chris Lattnerac161bf2009-01-02 07:01:27 +00001025 // Look this name up in the normal function symbol table.
1026 GlobalValue *Val =
1027 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001028
Chris Lattnerac161bf2009-01-02 07:01:27 +00001029 // If this is a forward reference for the value, see if we already created a
1030 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001031 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001032 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
1033 I = ForwardRefVals.find(Name);
1034 if (I != ForwardRefVals.end())
1035 Val = I->second.first;
1036 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001037
Chris Lattnerac161bf2009-01-02 07:01:27 +00001038 // If we have the value in the symbol table or fwd-ref table, return it.
1039 if (Val) {
1040 if (Val->getType() == Ty) return Val;
1041 Error(Loc, "'@" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001042 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001043 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001044 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001045
Chris Lattnerac161bf2009-01-02 07:01:27 +00001046 // Otherwise, create a new forward reference for this value and remember it.
1047 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001048 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001049 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001050 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001051 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001052 GlobalValue::ExternalWeakLinkage, nullptr, Name,
1053 nullptr, GlobalVariable::NotThreadLocal,
Justin Holewinski898a0a02012-11-16 21:03:47 +00001054 PTy->getAddressSpace());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001055
Chris Lattnerac161bf2009-01-02 07:01:27 +00001056 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1057 return FwdVal;
1058}
1059
Chris Lattner229907c2011-07-18 04:54:35 +00001060GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
1061 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001062 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001063 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001064 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001065 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001066
Craig Topper2617dcc2014-04-15 06:32:26 +00001067 GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001068
Chris Lattnerac161bf2009-01-02 07:01:27 +00001069 // If this is a forward reference for the value, see if we already created a
1070 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001071 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001072 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
1073 I = ForwardRefValIDs.find(ID);
1074 if (I != ForwardRefValIDs.end())
1075 Val = I->second.first;
1076 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001077
Chris Lattnerac161bf2009-01-02 07:01:27 +00001078 // If we have the value in the symbol table or fwd-ref table, return it.
1079 if (Val) {
1080 if (Val->getType() == Ty) return Val;
Benjamin Kramerc7583112010-09-27 17:42:11 +00001081 Error(Loc, "'@" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001082 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001083 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001084 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001085
Chris Lattnerac161bf2009-01-02 07:01:27 +00001086 // Otherwise, create a new forward reference for this value and remember it.
1087 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001088 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001089 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001090 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001091 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001092 GlobalValue::ExternalWeakLinkage, nullptr, "");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001093
Chris Lattnerac161bf2009-01-02 07:01:27 +00001094 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1095 return FwdVal;
1096}
1097
1098
1099//===----------------------------------------------------------------------===//
1100// Helper Routines.
1101//===----------------------------------------------------------------------===//
1102
1103/// ParseToken - If the current token has the specified kind, eat it and return
1104/// success. Otherwise, emit the specified error and return failure.
1105bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
1106 if (Lex.getKind() != T)
1107 return TokError(ErrMsg);
1108 Lex.Lex();
1109 return false;
1110}
1111
Chris Lattner3822f632009-01-02 08:05:26 +00001112/// ParseStringConstant
1113/// ::= StringConstant
1114bool LLParser::ParseStringConstant(std::string &Result) {
1115 if (Lex.getKind() != lltok::StringConstant)
1116 return TokError("expected string constant");
1117 Result = Lex.getStrVal();
1118 Lex.Lex();
1119 return false;
1120}
1121
1122/// ParseUInt32
1123/// ::= uint32
1124bool LLParser::ParseUInt32(unsigned &Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001125 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1126 return TokError("expected integer");
1127 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
1128 if (Val64 != unsigned(Val64))
1129 return TokError("expected 32-bit integer (too large)");
1130 Val = Val64;
1131 Lex.Lex();
1132 return false;
1133}
1134
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001135/// ParseTLSModel
1136/// := 'localdynamic'
1137/// := 'initialexec'
1138/// := 'localexec'
1139bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
1140 switch (Lex.getKind()) {
1141 default:
1142 return TokError("expected localdynamic, initialexec or localexec");
1143 case lltok::kw_localdynamic:
1144 TLM = GlobalVariable::LocalDynamicTLSModel;
1145 break;
1146 case lltok::kw_initialexec:
1147 TLM = GlobalVariable::InitialExecTLSModel;
1148 break;
1149 case lltok::kw_localexec:
1150 TLM = GlobalVariable::LocalExecTLSModel;
1151 break;
1152 }
1153
1154 Lex.Lex();
1155 return false;
1156}
1157
1158/// ParseOptionalThreadLocal
1159/// := /*empty*/
1160/// := 'thread_local'
1161/// := 'thread_local' '(' tlsmodel ')'
1162bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
1163 TLM = GlobalVariable::NotThreadLocal;
1164 if (!EatIfPresent(lltok::kw_thread_local))
1165 return false;
1166
1167 TLM = GlobalVariable::GeneralDynamicTLSModel;
1168 if (Lex.getKind() == lltok::lparen) {
1169 Lex.Lex();
1170 return ParseTLSModel(TLM) ||
1171 ParseToken(lltok::rparen, "expected ')' after thread local model");
1172 }
1173 return false;
1174}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001175
1176/// ParseOptionalAddrSpace
1177/// := /*empty*/
1178/// := 'addrspace' '(' uint32 ')'
1179bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
1180 AddrSpace = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001181 if (!EatIfPresent(lltok::kw_addrspace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001182 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001183 return ParseToken(lltok::lparen, "expected '(' in address space") ||
Chris Lattner3822f632009-01-02 08:05:26 +00001184 ParseUInt32(AddrSpace) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00001185 ParseToken(lltok::rparen, "expected ')' in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001186}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001187
Bill Wendling34c2eb22012-12-04 23:40:58 +00001188/// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes.
1189bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
1190 bool HaveError = false;
1191
1192 B.clear();
1193
1194 while (1) {
1195 lltok::Kind Token = Lex.getKind();
1196 switch (Token) {
1197 default: // End of attributes.
1198 return HaveError;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001199 case lltok::kw_align: {
1200 unsigned Alignment;
1201 if (ParseOptionalAlignment(Alignment))
1202 return true;
Bill Wendling68d24012012-10-08 22:20:14 +00001203 B.addAlignmentAttr(Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001204 continue;
1205 }
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001206 case lltok::kw_byval: B.addAttribute(Attribute::ByVal); break;
Reid Klecknera534a382013-12-19 02:14:12 +00001207 case lltok::kw_inalloca: B.addAttribute(Attribute::InAlloca); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001208 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1209 case lltok::kw_nest: B.addAttribute(Attribute::Nest); break;
1210 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1211 case lltok::kw_nocapture: B.addAttribute(Attribute::NoCapture); break;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001212 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001213 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
1214 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
Stephen Linb8bd2322013-04-20 05:14:40 +00001215 case lltok::kw_returned: B.addAttribute(Attribute::Returned); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001216 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1217 case lltok::kw_sret: B.addAttribute(Attribute::StructRet); break;
1218 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Charles Davisbe5557e2010-02-12 00:31:15 +00001219
Stephen Lin7577ed52013-04-20 13:16:13 +00001220 case lltok::kw_alignstack:
1221 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001222 case lltok::kw_builtin:
Stephen Lin7577ed52013-04-20 13:16:13 +00001223 case lltok::kw_inlinehint:
Tom Roeder44cb65f2014-06-05 19:29:43 +00001224 case lltok::kw_jumptable:
Stephen Lin7577ed52013-04-20 13:16:13 +00001225 case lltok::kw_minsize:
1226 case lltok::kw_naked:
1227 case lltok::kw_nobuiltin:
1228 case lltok::kw_noduplicate:
1229 case lltok::kw_noimplicitfloat:
1230 case lltok::kw_noinline:
1231 case lltok::kw_nonlazybind:
1232 case lltok::kw_noredzone:
1233 case lltok::kw_noreturn:
1234 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001235 case lltok::kw_optnone:
Stephen Lin7577ed52013-04-20 13:16:13 +00001236 case lltok::kw_optsize:
Stephen Lin7577ed52013-04-20 13:16:13 +00001237 case lltok::kw_returns_twice:
1238 case lltok::kw_sanitize_address:
1239 case lltok::kw_sanitize_memory:
1240 case lltok::kw_sanitize_thread:
1241 case lltok::kw_ssp:
1242 case lltok::kw_sspreq:
1243 case lltok::kw_sspstrong:
1244 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001245 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
1246 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001247 }
Bill Wendling0be9c402012-09-28 22:30:18 +00001248
Bill Wendling34c2eb22012-12-04 23:40:58 +00001249 Lex.Lex();
1250 }
1251}
1252
1253/// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes.
1254bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) {
1255 bool HaveError = false;
1256
1257 B.clear();
1258
1259 while (1) {
1260 lltok::Kind Token = Lex.getKind();
Bill Wendling0be9c402012-09-28 22:30:18 +00001261 switch (Token) {
Bill Wendling34c2eb22012-12-04 23:40:58 +00001262 default: // End of attributes.
1263 return HaveError;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001264 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1265 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001266 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001267 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1268 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Bill Wendling0be9c402012-09-28 22:30:18 +00001269
Bill Wendling34c2eb22012-12-04 23:40:58 +00001270 // Error handling.
Stephen Lin7577ed52013-04-20 13:16:13 +00001271 case lltok::kw_align:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001272 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +00001273 case lltok::kw_inalloca:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001274 case lltok::kw_nest:
1275 case lltok::kw_nocapture:
Stephen Linb8bd2322013-04-20 05:14:40 +00001276 case lltok::kw_returned:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001277 case lltok::kw_sret:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001278 HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001279 break;
James Molloy4f6fb952012-12-20 16:04:27 +00001280
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001281 case lltok::kw_alignstack:
1282 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001283 case lltok::kw_builtin:
Diego Novilloc6399532013-05-24 12:26:52 +00001284 case lltok::kw_cold:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001285 case lltok::kw_inlinehint:
Tom Roeder44cb65f2014-06-05 19:29:43 +00001286 case lltok::kw_jumptable:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001287 case lltok::kw_minsize:
1288 case lltok::kw_naked:
1289 case lltok::kw_nobuiltin:
1290 case lltok::kw_noduplicate:
1291 case lltok::kw_noimplicitfloat:
1292 case lltok::kw_noinline:
1293 case lltok::kw_nonlazybind:
1294 case lltok::kw_noredzone:
1295 case lltok::kw_noreturn:
1296 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001297 case lltok::kw_optnone:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001298 case lltok::kw_optsize:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001299 case lltok::kw_returns_twice:
1300 case lltok::kw_sanitize_address:
1301 case lltok::kw_sanitize_memory:
1302 case lltok::kw_sanitize_thread:
1303 case lltok::kw_ssp:
1304 case lltok::kw_sspreq:
1305 case lltok::kw_sspstrong:
1306 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001307 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001308 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001309
1310 case lltok::kw_readnone:
1311 case lltok::kw_readonly:
1312 HaveError |= Error(Lex.getLoc(), "invalid use of attribute on return type");
Bill Wendling0be9c402012-09-28 22:30:18 +00001313 }
1314
Chris Lattnerac161bf2009-01-02 07:01:27 +00001315 Lex.Lex();
1316 }
1317}
1318
1319/// ParseOptionalLinkage
1320/// ::= /*empty*/
Rafael Espindola6de96a12009-01-15 20:18:42 +00001321/// ::= 'private'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001322/// ::= 'internal'
1323/// ::= 'weak'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001324/// ::= 'weak_odr'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001325/// ::= 'linkonce'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001326/// ::= 'linkonce_odr'
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001327/// ::= 'available_externally'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001328/// ::= 'appending'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001329/// ::= 'common'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001330/// ::= 'extern_weak'
1331/// ::= 'external'
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001332///
1333/// Deprecated Values:
1334/// ::= 'linker_private'
1335/// ::= 'linker_private_weak'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001336bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) {
1337 HasLinkage = false;
1338 switch (Lex.getKind()) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001339 default: Res=GlobalValue::ExternalLinkage; return false;
1340 case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001341 case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break;
1342 case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break;
1343 case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break;
1344 case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break;
1345 case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001346 case lltok::kw_available_externally:
1347 Res = GlobalValue::AvailableExternallyLinkage;
1348 break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001349 case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001350 case lltok::kw_common: Res = GlobalValue::CommonLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001351 case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break;
1352 case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break;
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001353
1354 case lltok::kw_linker_private:
1355 case lltok::kw_linker_private_weak:
Saleem Abdulrasoolefa31a92014-04-05 22:42:53 +00001356 Lex.Warning("'" + Lex.getStrVal() + "' is deprecated, treating as"
1357 " PrivateLinkage");
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001358 Lex.Lex();
1359 // treat linker_private and linker_private_weak as PrivateLinkage
1360 Res = GlobalValue::PrivateLinkage;
1361 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001362 }
1363 Lex.Lex();
1364 HasLinkage = true;
1365 return false;
1366}
1367
1368/// ParseOptionalVisibility
1369/// ::= /*empty*/
1370/// ::= 'default'
1371/// ::= 'hidden'
1372/// ::= 'protected'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001373///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001374bool LLParser::ParseOptionalVisibility(unsigned &Res) {
1375 switch (Lex.getKind()) {
1376 default: Res = GlobalValue::DefaultVisibility; return false;
1377 case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break;
1378 case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break;
1379 case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break;
1380 }
1381 Lex.Lex();
1382 return false;
1383}
1384
Nico Rieck7157bb72014-01-14 15:22:47 +00001385/// ParseOptionalDLLStorageClass
1386/// ::= /*empty*/
1387/// ::= 'dllimport'
1388/// ::= 'dllexport'
1389///
1390bool LLParser::ParseOptionalDLLStorageClass(unsigned &Res) {
1391 switch (Lex.getKind()) {
1392 default: Res = GlobalValue::DefaultStorageClass; return false;
1393 case lltok::kw_dllimport: Res = GlobalValue::DLLImportStorageClass; break;
1394 case lltok::kw_dllexport: Res = GlobalValue::DLLExportStorageClass; break;
1395 }
1396 Lex.Lex();
1397 return false;
1398}
1399
Chris Lattnerac161bf2009-01-02 07:01:27 +00001400/// ParseOptionalCallingConv
1401/// ::= /*empty*/
1402/// ::= 'ccc'
1403/// ::= 'fastcc'
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001404/// ::= 'kw_intel_ocl_bicc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001405/// ::= 'coldcc'
1406/// ::= 'x86_stdcallcc'
1407/// ::= 'x86_fastcallcc'
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001408/// ::= 'x86_thiscallcc'
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001409/// ::= 'arm_apcscc'
1410/// ::= 'arm_aapcscc'
1411/// ::= 'arm_aapcs_vfpcc'
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001412/// ::= 'msp430_intrcc'
Che-Liang Chiou29947902010-09-25 07:46:17 +00001413/// ::= 'ptx_kernel'
1414/// ::= 'ptx_device'
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001415/// ::= 'spir_func'
1416/// ::= 'spir_kernel'
Charles Davise8f297c2013-07-12 06:02:35 +00001417/// ::= 'x86_64_sysvcc'
1418/// ::= 'x86_64_win64cc'
Andrew Tricka3a11de2013-10-31 22:12:01 +00001419/// ::= 'webkit_jscc'
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001420/// ::= 'anyregcc'
Juergen Ributzkae6250132014-01-17 19:47:03 +00001421/// ::= 'preserve_mostcc'
1422/// ::= 'preserve_allcc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001423/// ::= 'cc' UINT
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001424///
Sandeep Patel68c5f472009-09-02 08:44:58 +00001425bool LLParser::ParseOptionalCallingConv(CallingConv::ID &CC) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001426 switch (Lex.getKind()) {
1427 default: CC = CallingConv::C; return false;
1428 case lltok::kw_ccc: CC = CallingConv::C; break;
1429 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
1430 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
1431 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
1432 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001433 case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001434 case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break;
1435 case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break;
1436 case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001437 case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
Che-Liang Chiou29947902010-09-25 07:46:17 +00001438 case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break;
1439 case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break;
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001440 case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break;
1441 case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break;
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001442 case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break;
Charles Davise8f297c2013-07-12 06:02:35 +00001443 case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break;
1444 case lltok::kw_x86_64_win64cc: CC = CallingConv::X86_64_Win64; break;
Andrew Tricka3a11de2013-10-31 22:12:01 +00001445 case lltok::kw_webkit_jscc: CC = CallingConv::WebKit_JS; break;
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001446 case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break;
Juergen Ributzkae6250132014-01-17 19:47:03 +00001447 case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break;
1448 case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001449 case lltok::kw_cc: {
1450 unsigned ArbitraryCC;
1451 Lex.Lex();
David Blaikie46a9f012012-01-20 21:51:11 +00001452 if (ParseUInt32(ArbitraryCC))
Sandeep Patel68c5f472009-09-02 08:44:58 +00001453 return true;
David Blaikie46a9f012012-01-20 21:51:11 +00001454 CC = static_cast<CallingConv::ID>(ArbitraryCC);
1455 return false;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001456 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00001457 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001458
Chris Lattnerac161bf2009-01-02 07:01:27 +00001459 Lex.Lex();
1460 return false;
1461}
1462
Chris Lattner5c427632009-12-30 05:31:19 +00001463/// ParseInstructionMetadata
Chris Lattner596760d2009-12-29 21:25:40 +00001464/// ::= !dbg !42 (',' !dbg !57)*
Dan Gohman338d9a42010-08-24 02:05:17 +00001465bool LLParser::ParseInstructionMetadata(Instruction *Inst,
1466 PerFunctionState *PFS) {
Chris Lattner5c427632009-12-30 05:31:19 +00001467 do {
1468 if (Lex.getKind() != lltok::MetadataVar)
1469 return TokError("expected metadata after comma");
Devang Patelba4a6fd2009-09-29 00:01:14 +00001470
Chris Lattner596760d2009-12-29 21:25:40 +00001471 std::string Name = Lex.getStrVal();
Benjamin Kramerb3bd0192011-12-06 11:50:26 +00001472 unsigned MDK = M->getMDKindID(Name);
Chris Lattner596760d2009-12-29 21:25:40 +00001473 Lex.Lex();
Chris Lattner8d58f2f2009-10-19 05:31:10 +00001474
Chris Lattner1797fc72009-12-29 21:53:55 +00001475 MDNode *Node;
Chris Lattner8eff0152010-04-01 05:14:45 +00001476 SMLoc Loc = Lex.getLoc();
Dan Gohmanc828c542010-08-24 02:24:03 +00001477
1478 if (ParseToken(lltok::exclaim, "expected '!' here"))
Chris Lattner1eed2d62009-12-30 04:56:59 +00001479 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001480
Dan Gohmanf0715b12010-08-24 14:35:45 +00001481 // This code is similar to that of ParseMetadataValue, however it needs to
1482 // have special-case code for a forward reference; see the comments on
1483 // ForwardRefInstMetadata for details. Also, MDStrings are not supported
1484 // at the top level here.
Dan Gohmanc828c542010-08-24 02:24:03 +00001485 if (Lex.getKind() == lltok::lbrace) {
1486 ValID ID;
1487 if (ParseMetadataListValue(ID, PFS))
1488 return true;
1489 assert(ID.Kind == ValID::t_MDNode);
1490 Inst->setMetadata(MDK, ID.MDNodeVal);
Chris Lattner8eff0152010-04-01 05:14:45 +00001491 } else {
Nick Lewycky912888f2010-09-30 21:04:13 +00001492 unsigned NodeID = 0;
Dan Gohmanc828c542010-08-24 02:24:03 +00001493 if (ParseMDNodeID(Node, NodeID))
1494 return true;
1495 if (Node) {
1496 // If we got the node, add it to the instruction.
1497 Inst->setMetadata(MDK, Node);
1498 } else {
1499 MDRef R = { Loc, MDK, NodeID };
1500 // Otherwise, remember that this should be resolved later.
1501 ForwardRefInstMetadata[Inst].push_back(R);
1502 }
Chris Lattner8eff0152010-04-01 05:14:45 +00001503 }
Chris Lattner596760d2009-12-29 21:25:40 +00001504
Manman Ren209b17c2013-09-28 00:22:27 +00001505 if (MDK == LLVMContext::MD_tbaa)
1506 InstsWithTBAATag.push_back(Inst);
1507
Chris Lattner596760d2009-12-29 21:25:40 +00001508 // If this is the end of the list, we're done.
Chris Lattner5c427632009-12-30 05:31:19 +00001509 } while (EatIfPresent(lltok::comma));
1510 return false;
Devang Patelea8a4b92009-09-17 23:04:48 +00001511}
1512
Chris Lattnerac161bf2009-01-02 07:01:27 +00001513/// ParseOptionalAlignment
1514/// ::= /* empty */
1515/// ::= 'align' 4
1516bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1517 Alignment = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001518 if (!EatIfPresent(lltok::kw_align))
1519 return false;
Chris Lattnerd11f5142009-01-05 07:46:05 +00001520 LocTy AlignLoc = Lex.getLoc();
1521 if (ParseUInt32(Alignment)) return true;
1522 if (!isPowerOf2_32(Alignment))
1523 return Error(AlignLoc, "alignment is not a power of two");
Dan Gohmand566d2c2010-07-30 21:07:05 +00001524 if (Alignment > Value::MaximumAlignment)
Dan Gohmana7e5a242010-07-28 20:12:04 +00001525 return Error(AlignLoc, "huge alignments are not supported yet");
Chris Lattnerd11f5142009-01-05 07:46:05 +00001526 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001527}
1528
Chris Lattnerb2f39502009-12-30 05:44:30 +00001529/// ParseOptionalCommaAlign
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001530/// ::=
Chris Lattnerb2f39502009-12-30 05:44:30 +00001531/// ::= ',' align 4
1532///
1533/// This returns with AteExtraComma set to true if it ate an excess comma at the
1534/// end.
1535bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
1536 bool &AteExtraComma) {
1537 AteExtraComma = false;
1538 while (EatIfPresent(lltok::comma)) {
1539 // Metadata at the end is an early exit.
Chris Lattnereafe4de2009-12-30 05:02:06 +00001540 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerb2f39502009-12-30 05:44:30 +00001541 AteExtraComma = true;
1542 return false;
1543 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001544
Chris Lattner95b0ff42010-04-23 00:50:50 +00001545 if (Lex.getKind() != lltok::kw_align)
1546 return Error(Lex.getLoc(), "expected metadata or 'align'");
Duncan Sands4c5c8582010-10-21 16:07:10 +00001547
Chris Lattner95b0ff42010-04-23 00:50:50 +00001548 if (ParseOptionalAlignment(Alignment)) return true;
Chris Lattnerb2f39502009-12-30 05:44:30 +00001549 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001550
Devang Patelea8a4b92009-09-17 23:04:48 +00001551 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001552}
1553
Eli Friedmanfee02c62011-07-25 23:16:38 +00001554/// ParseScopeAndOrdering
1555/// if isAtomic: ::= 'singlethread'? AtomicOrdering
1556/// else: ::=
1557///
1558/// This sets Scope and Ordering to the parsed values.
1559bool LLParser::ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
1560 AtomicOrdering &Ordering) {
1561 if (!isAtomic)
1562 return false;
1563
1564 Scope = CrossThread;
1565 if (EatIfPresent(lltok::kw_singlethread))
1566 Scope = SingleThread;
Tim Northovere94a5182014-03-11 10:48:52 +00001567
1568 return ParseOrdering(Ordering);
1569}
1570
1571/// ParseOrdering
1572/// ::= AtomicOrdering
1573///
1574/// This sets Ordering to the parsed value.
1575bool LLParser::ParseOrdering(AtomicOrdering &Ordering) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001576 switch (Lex.getKind()) {
1577 default: return TokError("Expected ordering on atomic instruction");
1578 case lltok::kw_unordered: Ordering = Unordered; break;
1579 case lltok::kw_monotonic: Ordering = Monotonic; break;
1580 case lltok::kw_acquire: Ordering = Acquire; break;
1581 case lltok::kw_release: Ordering = Release; break;
1582 case lltok::kw_acq_rel: Ordering = AcquireRelease; break;
1583 case lltok::kw_seq_cst: Ordering = SequentiallyConsistent; break;
1584 }
1585 Lex.Lex();
1586 return false;
1587}
1588
Charles Davisbe5557e2010-02-12 00:31:15 +00001589/// ParseOptionalStackAlignment
1590/// ::= /* empty */
1591/// ::= 'alignstack' '(' 4 ')'
1592bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
1593 Alignment = 0;
1594 if (!EatIfPresent(lltok::kw_alignstack))
1595 return false;
1596 LocTy ParenLoc = Lex.getLoc();
1597 if (!EatIfPresent(lltok::lparen))
1598 return Error(ParenLoc, "expected '('");
1599 LocTy AlignLoc = Lex.getLoc();
1600 if (ParseUInt32(Alignment)) return true;
1601 ParenLoc = Lex.getLoc();
1602 if (!EatIfPresent(lltok::rparen))
1603 return Error(ParenLoc, "expected ')'");
1604 if (!isPowerOf2_32(Alignment))
1605 return Error(AlignLoc, "stack alignment is not a power of two");
1606 return false;
1607}
Devang Patelea8a4b92009-09-17 23:04:48 +00001608
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001609/// ParseIndexList - This parses the index list for an insert/extractvalue
1610/// instruction. This sets AteExtraComma in the case where we eat an extra
1611/// comma at the end of the line and find that it is followed by metadata.
1612/// Clients that don't allow metadata can call the version of this function that
1613/// only takes one argument.
1614///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001615/// ParseIndexList
1616/// ::= (',' uint32)+
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001617///
1618bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
1619 bool &AteExtraComma) {
1620 AteExtraComma = false;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001621
Chris Lattnerac161bf2009-01-02 07:01:27 +00001622 if (Lex.getKind() != lltok::comma)
1623 return TokError("expected ',' as start of index list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001624
Chris Lattner3822f632009-01-02 08:05:26 +00001625 while (EatIfPresent(lltok::comma)) {
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001626 if (Lex.getKind() == lltok::MetadataVar) {
1627 AteExtraComma = true;
1628 return false;
1629 }
Nick Lewycky83e47112010-09-29 23:32:20 +00001630 unsigned Idx = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001631 if (ParseUInt32(Idx)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001632 Indices.push_back(Idx);
1633 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001634
Chris Lattnerac161bf2009-01-02 07:01:27 +00001635 return false;
1636}
1637
1638//===----------------------------------------------------------------------===//
1639// Type Parsing.
1640//===----------------------------------------------------------------------===//
1641
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001642/// ParseType - Parse a type.
1643bool LLParser::ParseType(Type *&Result, bool AllowVoid) {
1644 SMLoc TypeLoc = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001645 switch (Lex.getKind()) {
1646 default:
1647 return TokError("expected type");
1648 case lltok::Type:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001649 // Type ::= 'float' | 'void' (etc)
Chris Lattnerac161bf2009-01-02 07:01:27 +00001650 Result = Lex.getTyVal();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001651 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001652 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001653 case lltok::lbrace:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001654 // Type ::= StructType
1655 if (ParseAnonStructType(Result, false))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001656 return true;
1657 break;
1658 case lltok::lsquare:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001659 // Type ::= '[' ... ']'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001660 Lex.Lex(); // eat the lsquare.
1661 if (ParseArrayVectorType(Result, false))
1662 return true;
1663 break;
1664 case lltok::less: // Either vector or packed struct.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001665 // Type ::= '<' ... '>'
Chris Lattner3822f632009-01-02 08:05:26 +00001666 Lex.Lex();
1667 if (Lex.getKind() == lltok::lbrace) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001668 if (ParseAnonStructType(Result, true) ||
Chris Lattner3822f632009-01-02 08:05:26 +00001669 ParseToken(lltok::greater, "expected '>' at end of packed struct"))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001670 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001671 } else if (ParseArrayVectorType(Result, true))
1672 return true;
1673 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001674 case lltok::LocalVar: {
1675 // Type ::= %foo
1676 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001677
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001678 // If the type hasn't been defined yet, create a forward definition and
1679 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001680 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001681 Entry.first = StructType::create(Context, Lex.getStrVal());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001682 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001683 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001684 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001685 Lex.Lex();
1686 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001687 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001688
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001689 case lltok::LocalVarID: {
1690 // Type ::= %4
1691 if (Lex.getUIntVal() >= NumberedTypes.size())
1692 NumberedTypes.resize(Lex.getUIntVal()+1);
1693 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001694
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001695 // If the type hasn't been defined yet, create a forward definition and
1696 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001697 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001698 Entry.first = StructType::create(Context);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001699 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001700 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001701 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001702 Lex.Lex();
1703 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001704 }
1705 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001706
1707 // Parse the type suffixes.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001708 while (1) {
1709 switch (Lex.getKind()) {
1710 // End of type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001711 default:
1712 if (!AllowVoid && Result->isVoidTy())
1713 return Error(TypeLoc, "void type only allowed for function results");
1714 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001715
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001716 // Type ::= Type '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001717 case lltok::star:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001718 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001719 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001720 if (Result->isVoidTy())
1721 return TokError("pointers to void are invalid - use i8* instead");
1722 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001723 return TokError("pointer to this type is invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001724 Result = PointerType::getUnqual(Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001725 Lex.Lex();
1726 break;
1727
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001728 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001729 case lltok::kw_addrspace: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001730 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001731 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001732 if (Result->isVoidTy())
Dan Gohman9280a682009-02-09 17:41:21 +00001733 return TokError("pointers to void are invalid; use i8* instead");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001734 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001735 return TokError("pointer to this type is invalid");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001736 unsigned AddrSpace;
1737 if (ParseOptionalAddrSpace(AddrSpace) ||
1738 ParseToken(lltok::star, "expected '*' in address space"))
1739 return true;
1740
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001741 Result = PointerType::get(Result, AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001742 break;
1743 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001744
Chris Lattnerac161bf2009-01-02 07:01:27 +00001745 /// Types '(' ArgTypeListI ')' OptFuncAttrs
1746 case lltok::lparen:
1747 if (ParseFunctionType(Result))
1748 return true;
1749 break;
1750 }
1751 }
1752}
1753
1754/// ParseParameterList
1755/// ::= '(' ')'
1756/// ::= '(' Arg (',' Arg)* ')'
1757/// Arg
1758/// ::= Type OptionalAttributes Value OptionalAttributes
1759bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
1760 PerFunctionState &PFS) {
1761 if (ParseToken(lltok::lparen, "expected '(' in call"))
1762 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001763
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001764 unsigned AttrIndex = 1;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001765 while (Lex.getKind() != lltok::rparen) {
1766 // If this isn't the first argument, we need a comma.
1767 if (!ArgList.empty() &&
1768 ParseToken(lltok::comma, "expected ',' in argument list"))
1769 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001770
Chris Lattnerac161bf2009-01-02 07:01:27 +00001771 // Parse the argument.
1772 LocTy ArgLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00001773 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001774 AttrBuilder ArgAttrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001775 Value *V;
Victor Hernandezfa232232009-12-03 23:40:58 +00001776 if (ParseType(ArgTy, ArgLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001777 return true;
Victor Hernandezfa232232009-12-03 23:40:58 +00001778
Chris Lattner5b4a9622009-12-30 02:11:14 +00001779 // Otherwise, handle normal operands.
Bill Wendling34c2eb22012-12-04 23:40:58 +00001780 if (ParseOptionalParamAttrs(ArgAttrs) || ParseValue(ArgTy, V, PFS))
Chris Lattner5b4a9622009-12-30 02:11:14 +00001781 return true;
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001782 ArgList.push_back(ParamInfo(ArgLoc, V, AttributeSet::get(V->getContext(),
1783 AttrIndex++,
1784 ArgAttrs)));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001785 }
1786
1787 Lex.Lex(); // Lex the ')'.
1788 return false;
1789}
1790
1791
1792
Chris Lattner2ed06b42009-01-05 18:34:07 +00001793/// ParseArgumentList - Parse the argument list for a function type or function
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001794/// prototype.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001795/// ::= '(' ArgTypeListI ')'
1796/// ArgTypeListI
1797/// ::= /*empty*/
1798/// ::= '...'
1799/// ::= ArgTypeList ',' '...'
1800/// ::= ArgType (',' ArgType)*
Chris Lattner2ed06b42009-01-05 18:34:07 +00001801///
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001802bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
1803 bool &isVarArg){
Chris Lattnerac161bf2009-01-02 07:01:27 +00001804 isVarArg = false;
1805 assert(Lex.getKind() == lltok::lparen);
1806 Lex.Lex(); // eat the (.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001807
Chris Lattnerac161bf2009-01-02 07:01:27 +00001808 if (Lex.getKind() == lltok::rparen) {
1809 // empty
1810 } else if (Lex.getKind() == lltok::dotdotdot) {
1811 isVarArg = true;
1812 Lex.Lex();
1813 } else {
1814 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001815 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001816 AttrBuilder Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001817 std::string Name;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001818
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001819 if (ParseType(ArgTy) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00001820 ParseOptionalParamAttrs(Attrs)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001821
Chris Lattnerfdd87902009-10-05 05:54:46 +00001822 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001823 return Error(TypeLoc, "argument can not have void type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001824
Chris Lattnerdef19492011-06-17 06:36:20 +00001825 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001826 Name = Lex.getStrVal();
1827 Lex.Lex();
1828 }
Chris Lattner3822f632009-01-02 08:05:26 +00001829
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001830 if (!FunctionType::isValidArgumentType(ArgTy))
Chris Lattner3822f632009-01-02 08:05:26 +00001831 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001832
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001833 unsigned AttrIndex = 1;
Bill Wendlingd079a442012-10-15 04:46:55 +00001834 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001835 AttributeSet::get(ArgTy->getContext(),
1836 AttrIndex++, Attrs), Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001837
Chris Lattner3822f632009-01-02 08:05:26 +00001838 while (EatIfPresent(lltok::comma)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001839 // Handle ... at end of arg list.
Chris Lattner3822f632009-01-02 08:05:26 +00001840 if (EatIfPresent(lltok::dotdotdot)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001841 isVarArg = true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001842 break;
1843 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001844
Chris Lattnerac161bf2009-01-02 07:01:27 +00001845 // Otherwise must be an argument type.
1846 TypeLoc = Lex.getLoc();
Bill Wendling34c2eb22012-12-04 23:40:58 +00001847 if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) return true;
Chris Lattner3822f632009-01-02 08:05:26 +00001848
Chris Lattnerfdd87902009-10-05 05:54:46 +00001849 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001850 return Error(TypeLoc, "argument can not have void type");
1851
Chris Lattnerdef19492011-06-17 06:36:20 +00001852 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001853 Name = Lex.getStrVal();
1854 Lex.Lex();
1855 } else {
1856 Name = "";
1857 }
Chris Lattner3822f632009-01-02 08:05:26 +00001858
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001859 if (!ArgTy->isFirstClassType())
Chris Lattner3822f632009-01-02 08:05:26 +00001860 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001861
Bill Wendlingd079a442012-10-15 04:46:55 +00001862 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001863 AttributeSet::get(ArgTy->getContext(),
1864 AttrIndex++, Attrs),
Bill Wendlingd079a442012-10-15 04:46:55 +00001865 Name));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001866 }
1867 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001868
Chris Lattner3822f632009-01-02 08:05:26 +00001869 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001870}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001871
Chris Lattnerac161bf2009-01-02 07:01:27 +00001872/// ParseFunctionType
1873/// ::= Type ArgumentList OptionalAttrs
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001874bool LLParser::ParseFunctionType(Type *&Result) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001875 assert(Lex.getKind() == lltok::lparen);
1876
Chris Lattnerce473c72009-01-05 08:04:33 +00001877 if (!FunctionType::isValidReturnType(Result))
1878 return TokError("invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001879
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001880 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001881 bool isVarArg;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001882 if (ParseArgumentList(ArgList, isVarArg))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001883 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001884
Chris Lattnerac161bf2009-01-02 07:01:27 +00001885 // Reject names on the arguments lists.
1886 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
1887 if (!ArgList[i].Name.empty())
1888 return Error(ArgList[i].Loc, "argument name invalid in function type");
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001889 if (ArgList[i].Attrs.hasAttributes(i + 1))
Chris Lattner6bc5c892011-06-17 17:37:13 +00001890 return Error(ArgList[i].Loc,
1891 "argument attributes invalid in function type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001892 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001893
Jay Foadb804a2b2011-07-12 14:06:48 +00001894 SmallVector<Type*, 16> ArgListTy;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001895 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001896 ArgListTy.push_back(ArgList[i].Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001897
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001898 Result = FunctionType::get(Result, ArgListTy, isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001899 return false;
1900}
1901
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001902/// ParseAnonStructType - Parse an anonymous struct type, which is inlined into
1903/// other structs.
1904bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) {
1905 SmallVector<Type*, 8> Elts;
1906 if (ParseStructBody(Elts)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001907
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001908 Result = StructType::get(Context, Elts, Packed);
1909 return false;
1910}
1911
1912/// ParseStructDefinition - Parse a struct in a 'type' definition.
1913bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
1914 std::pair<Type*, LocTy> &Entry,
1915 Type *&ResultTy) {
1916 // If the type was already defined, diagnose the redefinition.
1917 if (Entry.first && !Entry.second.isValid())
1918 return Error(TypeLoc, "redefinition of type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001919
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001920 // If we have opaque, just return without filling in the definition for the
1921 // struct. This counts as a definition as far as the .ll file goes.
1922 if (EatIfPresent(lltok::kw_opaque)) {
1923 // This type is being defined, so clear the location to indicate this.
1924 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001925
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001926 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00001927 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00001928 Entry.first = StructType::create(Context, Name);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001929 ResultTy = Entry.first;
1930 return false;
1931 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001932
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001933 // If the type starts with '<', then it is either a packed struct or a vector.
1934 bool isPacked = EatIfPresent(lltok::less);
1935
1936 // If we don't have a struct, then we have a random type alias, which we
1937 // accept for compatibility with old files. These types are not allowed to be
1938 // forward referenced and not allowed to be recursive.
1939 if (Lex.getKind() != lltok::lbrace) {
1940 if (Entry.first)
1941 return Error(TypeLoc, "forward references to non-struct type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001942
Craig Topper2617dcc2014-04-15 06:32:26 +00001943 ResultTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001944 if (isPacked)
1945 return ParseArrayVectorType(ResultTy, true);
1946 return ParseType(ResultTy);
1947 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001948
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001949 // This type is being defined, so clear the location to indicate this.
1950 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001951
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001952 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00001953 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00001954 Entry.first = StructType::create(Context, Name);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001955
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001956 StructType *STy = cast<StructType>(Entry.first);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001957
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001958 SmallVector<Type*, 8> Body;
1959 if (ParseStructBody(Body) ||
1960 (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct")))
1961 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001962
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001963 STy->setBody(Body, isPacked);
1964 ResultTy = STy;
1965 return false;
1966}
1967
1968
Chris Lattnerac161bf2009-01-02 07:01:27 +00001969/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001970/// StructType
Chris Lattnerac161bf2009-01-02 07:01:27 +00001971/// ::= '{' '}'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001972/// ::= '{' Type (',' Type)* '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001973/// ::= '<' '{' '}' '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001974/// ::= '<' '{' Type (',' Type)* '}' '>'
1975bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001976 assert(Lex.getKind() == lltok::lbrace);
1977 Lex.Lex(); // Consume the '{'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001978
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001979 // Handle the empty struct.
1980 if (EatIfPresent(lltok::rbrace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001981 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001982
Chris Lattnerf880ca22009-03-09 04:49:14 +00001983 LocTy EltTyLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001984 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001985 if (ParseType(Ty)) return true;
1986 Body.push_back(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001987
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001988 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001989 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001990
Chris Lattner3822f632009-01-02 08:05:26 +00001991 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf880ca22009-03-09 04:49:14 +00001992 EltTyLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001993 if (ParseType(Ty)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001994
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001995 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001996 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001997
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001998 Body.push_back(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001999 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002000
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002001 return ParseToken(lltok::rbrace, "expected '}' at end of struct");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002002}
2003
2004/// ParseArrayVectorType - Parse an array or vector type, assuming the first
2005/// token has already been consumed.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002006/// Type
Chris Lattnerac161bf2009-01-02 07:01:27 +00002007/// ::= '[' APSINTVAL 'x' Types ']'
2008/// ::= '<' APSINTVAL 'x' Types '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002009bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002010 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
2011 Lex.getAPSIntVal().getBitWidth() > 64)
2012 return TokError("expected number in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002013
Chris Lattnerac161bf2009-01-02 07:01:27 +00002014 LocTy SizeLoc = Lex.getLoc();
2015 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
Chris Lattner3822f632009-01-02 08:05:26 +00002016 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002017
Chris Lattner3822f632009-01-02 08:05:26 +00002018 if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
2019 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002020
2021 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00002022 Type *EltTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002023 if (ParseType(EltTy)) return true;
Chris Lattnerf880ca22009-03-09 04:49:14 +00002024
Chris Lattner3822f632009-01-02 08:05:26 +00002025 if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
2026 "expected end of sequential type"))
2027 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002028
Chris Lattnerac161bf2009-01-02 07:01:27 +00002029 if (isVector) {
Chris Lattnerbb1fe8a2009-02-28 18:12:41 +00002030 if (Size == 0)
2031 return Error(SizeLoc, "zero element vector is illegal");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002032 if ((unsigned)Size != Size)
2033 return Error(SizeLoc, "size too large for vector");
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002034 if (!VectorType::isValidElementType(EltTy))
Duncan Sandse6beec62012-11-13 12:59:33 +00002035 return Error(TypeLoc, "invalid vector element type");
Owen Anderson4056ca92009-07-29 22:17:13 +00002036 Result = VectorType::get(EltTy, unsigned(Size));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002037 } else {
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002038 if (!ArrayType::isValidElementType(EltTy))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002039 return Error(TypeLoc, "invalid array element type");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002040 Result = ArrayType::get(EltTy, Size);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002041 }
2042 return false;
2043}
2044
2045//===----------------------------------------------------------------------===//
2046// Function Semantic Analysis.
2047//===----------------------------------------------------------------------===//
2048
Chris Lattner3432c622009-10-28 03:39:23 +00002049LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
2050 int functionNumber)
2051 : P(p), F(f), FunctionNumber(functionNumber) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002052
2053 // Insert unnamed arguments into the NumberedVals list.
2054 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
2055 AI != E; ++AI)
2056 if (!AI->hasName())
2057 NumberedVals.push_back(AI);
2058}
2059
2060LLParser::PerFunctionState::~PerFunctionState() {
2061 // If there were any forward referenced non-basicblock values, delete them.
2062 for (std::map<std::string, std::pair<Value*, LocTy> >::iterator
2063 I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
2064 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002065 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002066 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002067 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002068 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002069 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002070
Chris Lattnerac161bf2009-01-02 07:01:27 +00002071 for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2072 I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
2073 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002074 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002075 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002076 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002077 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002078 }
2079}
2080
Chris Lattner3432c622009-10-28 03:39:23 +00002081bool LLParser::PerFunctionState::FinishFunction() {
2082 // Check to see if someone took the address of labels in this block.
2083 if (!P.ForwardRefBlockAddresses.empty()) {
2084 ValID FunctionID;
2085 if (!F.getName().empty()) {
2086 FunctionID.Kind = ValID::t_GlobalName;
2087 FunctionID.StrVal = F.getName();
2088 } else {
2089 FunctionID.Kind = ValID::t_GlobalID;
2090 FunctionID.UIntVal = FunctionNumber;
2091 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002092
Chris Lattner3432c622009-10-28 03:39:23 +00002093 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >::iterator
2094 FRBAI = P.ForwardRefBlockAddresses.find(FunctionID);
2095 if (FRBAI != P.ForwardRefBlockAddresses.end()) {
2096 // Resolve all these references.
2097 if (P.ResolveForwardRefBlockAddresses(&F, FRBAI->second, this))
2098 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002099
Chris Lattner3432c622009-10-28 03:39:23 +00002100 P.ForwardRefBlockAddresses.erase(FRBAI);
2101 }
2102 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002103
Chris Lattnerac161bf2009-01-02 07:01:27 +00002104 if (!ForwardRefVals.empty())
2105 return P.Error(ForwardRefVals.begin()->second.second,
2106 "use of undefined value '%" + ForwardRefVals.begin()->first +
2107 "'");
2108 if (!ForwardRefValIDs.empty())
2109 return P.Error(ForwardRefValIDs.begin()->second.second,
2110 "use of undefined value '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002111 Twine(ForwardRefValIDs.begin()->first) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002112 return false;
2113}
2114
2115
2116/// GetVal - Get a value with the specified name or ID, creating a
2117/// forward reference record if needed. This can return null if the value
2118/// exists but does not have the right type.
2119Value *LLParser::PerFunctionState::GetVal(const std::string &Name,
Chris Lattner229907c2011-07-18 04:54:35 +00002120 Type *Ty, LocTy Loc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002121 // Look this name up in the normal function symbol table.
2122 Value *Val = F.getValueSymbolTable().lookup(Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002123
Chris Lattnerac161bf2009-01-02 07:01:27 +00002124 // If this is a forward reference for the value, see if we already created a
2125 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002126 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002127 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2128 I = ForwardRefVals.find(Name);
2129 if (I != ForwardRefVals.end())
2130 Val = I->second.first;
2131 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002132
Chris Lattnerac161bf2009-01-02 07:01:27 +00002133 // If we have the value in the symbol table or fwd-ref table, return it.
2134 if (Val) {
2135 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002136 if (Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002137 P.Error(Loc, "'%" + Name + "' is not a basic block");
2138 else
2139 P.Error(Loc, "'%" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002140 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002141 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002142 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002143
Chris Lattnerac161bf2009-01-02 07:01:27 +00002144 // Don't make placeholders with invalid type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002145 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002146 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002147 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002148 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002149
Chris Lattnerac161bf2009-01-02 07:01:27 +00002150 // Otherwise, create a new forward reference for this value and remember it.
2151 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002152 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002153 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002154 else
2155 FwdVal = new Argument(Ty, Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002156
Chris Lattnerac161bf2009-01-02 07:01:27 +00002157 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
2158 return FwdVal;
2159}
2160
Chris Lattner229907c2011-07-18 04:54:35 +00002161Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00002162 LocTy Loc) {
2163 // Look this name up in the normal function symbol table.
Craig Topper2617dcc2014-04-15 06:32:26 +00002164 Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002165
Chris Lattnerac161bf2009-01-02 07:01:27 +00002166 // If this is a forward reference for the value, see if we already created a
2167 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002168 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002169 std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2170 I = ForwardRefValIDs.find(ID);
2171 if (I != ForwardRefValIDs.end())
2172 Val = I->second.first;
2173 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002174
Chris Lattnerac161bf2009-01-02 07:01:27 +00002175 // If we have the value in the symbol table or fwd-ref table, return it.
2176 if (Val) {
2177 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002178 if (Ty->isLabelTy())
Benjamin Kramerc7583112010-09-27 17:42:11 +00002179 P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002180 else
Benjamin Kramerc7583112010-09-27 17:42:11 +00002181 P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002182 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002183 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002184 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002185
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002186 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002187 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002188 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002189 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002190
Chris Lattnerac161bf2009-01-02 07:01:27 +00002191 // Otherwise, create a new forward reference for this value and remember it.
2192 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002193 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002194 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002195 else
2196 FwdVal = new Argument(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002197
Chris Lattnerac161bf2009-01-02 07:01:27 +00002198 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
2199 return FwdVal;
2200}
2201
2202/// SetInstName - After an instruction is parsed and inserted into its
2203/// basic block, this installs its name.
2204bool LLParser::PerFunctionState::SetInstName(int NameID,
2205 const std::string &NameStr,
2206 LocTy NameLoc, Instruction *Inst) {
2207 // If this instruction has void type, it cannot have a name or ID specified.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002208 if (Inst->getType()->isVoidTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002209 if (NameID != -1 || !NameStr.empty())
2210 return P.Error(NameLoc, "instructions returning void cannot have a name");
2211 return false;
2212 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002213
Chris Lattnerac161bf2009-01-02 07:01:27 +00002214 // If this was a numbered instruction, verify that the instruction is the
2215 // expected value and resolve any forward references.
2216 if (NameStr.empty()) {
2217 // If neither a name nor an ID was specified, just use the next ID.
2218 if (NameID == -1)
2219 NameID = NumberedVals.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002220
Chris Lattnerac161bf2009-01-02 07:01:27 +00002221 if (unsigned(NameID) != NumberedVals.size())
2222 return P.Error(NameLoc, "instruction expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002223 Twine(NumberedVals.size()) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002224
Chris Lattnerac161bf2009-01-02 07:01:27 +00002225 std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI =
2226 ForwardRefValIDs.find(NameID);
2227 if (FI != ForwardRefValIDs.end()) {
2228 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002229 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002230 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002231 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2baa6a32009-09-02 15:02:57 +00002232 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002233 ForwardRefValIDs.erase(FI);
2234 }
2235
2236 NumberedVals.push_back(Inst);
2237 return false;
2238 }
2239
2240 // Otherwise, the instruction had a name. Resolve forward refs and set it.
2241 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2242 FI = ForwardRefVals.find(NameStr);
2243 if (FI != ForwardRefVals.end()) {
2244 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002245 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002246 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002247 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2fcee702009-09-02 14:22:03 +00002248 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002249 ForwardRefVals.erase(FI);
2250 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002251
Chris Lattnerac161bf2009-01-02 07:01:27 +00002252 // Set the name on the instruction.
2253 Inst->setName(NameStr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002254
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00002255 if (Inst->getName() != NameStr)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002256 return P.Error(NameLoc, "multiple definition of local value named '" +
Chris Lattnerac161bf2009-01-02 07:01:27 +00002257 NameStr + "'");
2258 return false;
2259}
2260
2261/// GetBB - Get a basic block with the specified name or ID, creating a
2262/// forward reference record if needed.
2263BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
2264 LocTy Loc) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002265 return cast_or_null<BasicBlock>(GetVal(Name,
2266 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002267}
2268
2269BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002270 return cast_or_null<BasicBlock>(GetVal(ID,
2271 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002272}
2273
2274/// DefineBB - Define the specified basic block, which is either named or
2275/// unnamed. If there is an error, this returns null otherwise it returns
2276/// the block being defined.
2277BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
2278 LocTy Loc) {
2279 BasicBlock *BB;
2280 if (Name.empty())
2281 BB = GetBB(NumberedVals.size(), Loc);
2282 else
2283 BB = GetBB(Name, Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002284 if (!BB) return nullptr; // Already diagnosed error.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002285
Chris Lattnerac161bf2009-01-02 07:01:27 +00002286 // Move the block to the end of the function. Forward ref'd blocks are
2287 // inserted wherever they happen to be referenced.
2288 F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002289
Chris Lattnerac161bf2009-01-02 07:01:27 +00002290 // Remove the block from forward ref sets.
2291 if (Name.empty()) {
2292 ForwardRefValIDs.erase(NumberedVals.size());
2293 NumberedVals.push_back(BB);
2294 } else {
2295 // BB forward references are already in the function symbol table.
2296 ForwardRefVals.erase(Name);
2297 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002298
Chris Lattnerac161bf2009-01-02 07:01:27 +00002299 return BB;
2300}
2301
2302//===----------------------------------------------------------------------===//
2303// Constants.
2304//===----------------------------------------------------------------------===//
2305
2306/// ParseValID - Parse an abstract value that doesn't necessarily have a
2307/// type implied. For example, if we parse "4" we don't know what integer type
2308/// it has. The value will later be combined with its type and checked for
Victor Hernandezb8fd1522010-01-10 07:14:18 +00002309/// sanity. PFS is used to convert function-local operands of metadata (since
2310/// metadata operands are not just parsed here but also converted to values).
2311/// PFS can be null when we are not parsing metadata values inside a function.
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00002312bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002313 ID.Loc = Lex.getLoc();
2314 switch (Lex.getKind()) {
2315 default: return TokError("expected value token");
2316 case lltok::GlobalID: // @42
2317 ID.UIntVal = Lex.getUIntVal();
2318 ID.Kind = ValID::t_GlobalID;
2319 break;
2320 case lltok::GlobalVar: // @foo
2321 ID.StrVal = Lex.getStrVal();
2322 ID.Kind = ValID::t_GlobalName;
2323 break;
2324 case lltok::LocalVarID: // %42
2325 ID.UIntVal = Lex.getUIntVal();
2326 ID.Kind = ValID::t_LocalID;
2327 break;
2328 case lltok::LocalVar: // %foo
Chris Lattnerac161bf2009-01-02 07:01:27 +00002329 ID.StrVal = Lex.getStrVal();
2330 ID.Kind = ValID::t_LocalName;
2331 break;
Dan Gohman8939ba332010-07-14 18:26:50 +00002332 case lltok::exclaim: // !42, !{...}, or !"foo"
2333 return ParseMetadataValue(ID, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002334 case lltok::APSInt:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002335 ID.APSIntVal = Lex.getAPSIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00002336 ID.Kind = ValID::t_APSInt;
2337 break;
2338 case lltok::APFloat:
2339 ID.APFloatVal = Lex.getAPFloatVal();
2340 ID.Kind = ValID::t_APFloat;
2341 break;
2342 case lltok::kw_true:
Owen Anderson23a204d2009-07-31 17:39:07 +00002343 ID.ConstantVal = ConstantInt::getTrue(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002344 ID.Kind = ValID::t_Constant;
2345 break;
2346 case lltok::kw_false:
Owen Anderson23a204d2009-07-31 17:39:07 +00002347 ID.ConstantVal = ConstantInt::getFalse(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002348 ID.Kind = ValID::t_Constant;
2349 break;
2350 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
2351 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
2352 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002353
Chris Lattnerac161bf2009-01-02 07:01:27 +00002354 case lltok::lbrace: {
2355 // ValID ::= '{' ConstVector '}'
2356 Lex.Lex();
2357 SmallVector<Constant*, 16> Elts;
2358 if (ParseGlobalValueVector(Elts) ||
2359 ParseToken(lltok::rbrace, "expected end of struct constant"))
2360 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002361
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002362 ID.ConstantStructElts = new Constant*[Elts.size()];
2363 ID.UIntVal = Elts.size();
2364 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2365 ID.Kind = ValID::t_ConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002366 return false;
2367 }
2368 case lltok::less: {
2369 // ValID ::= '<' ConstVector '>' --> Vector.
2370 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
2371 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +00002372 bool isPackedStruct = EatIfPresent(lltok::lbrace);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002373
Chris Lattnerac161bf2009-01-02 07:01:27 +00002374 SmallVector<Constant*, 16> Elts;
2375 LocTy FirstEltLoc = Lex.getLoc();
2376 if (ParseGlobalValueVector(Elts) ||
2377 (isPackedStruct &&
2378 ParseToken(lltok::rbrace, "expected end of packed struct")) ||
2379 ParseToken(lltok::greater, "expected end of constant"))
2380 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002381
Chris Lattnerac161bf2009-01-02 07:01:27 +00002382 if (isPackedStruct) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002383 ID.ConstantStructElts = new Constant*[Elts.size()];
2384 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2385 ID.UIntVal = Elts.size();
2386 ID.Kind = ValID::t_PackedConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002387 return false;
2388 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002389
Chris Lattnerac161bf2009-01-02 07:01:27 +00002390 if (Elts.empty())
2391 return Error(ID.Loc, "constant vector must not be empty");
2392
Duncan Sands9dff9be2010-02-15 16:12:20 +00002393 if (!Elts[0]->getType()->isIntegerTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002394 !Elts[0]->getType()->isFloatingPointTy() &&
2395 !Elts[0]->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002396 return Error(FirstEltLoc,
Nadav Rotem3924cb02011-12-05 06:29:09 +00002397 "vector elements must have integer, pointer or floating point type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002398
Chris Lattnerac161bf2009-01-02 07:01:27 +00002399 // Verify that all the vector elements have the same type.
2400 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
2401 if (Elts[i]->getType() != Elts[0]->getType())
2402 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002403 "vector element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002404 " is not of type '" + getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002405
Chris Lattner69229312011-02-15 00:14:00 +00002406 ID.ConstantVal = ConstantVector::get(Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002407 ID.Kind = ValID::t_Constant;
2408 return false;
2409 }
2410 case lltok::lsquare: { // Array Constant
2411 Lex.Lex();
2412 SmallVector<Constant*, 16> Elts;
2413 LocTy FirstEltLoc = Lex.getLoc();
2414 if (ParseGlobalValueVector(Elts) ||
2415 ParseToken(lltok::rsquare, "expected end of array constant"))
2416 return true;
2417
2418 // Handle empty element.
2419 if (Elts.empty()) {
2420 // Use undef instead of an array because it's inconvenient to determine
2421 // the element type at this point, there being no elements to examine.
Chris Lattner998fa0a2009-01-05 07:52:51 +00002422 ID.Kind = ValID::t_EmptyArray;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002423 return false;
2424 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002425
Chris Lattnerac161bf2009-01-02 07:01:27 +00002426 if (!Elts[0]->getType()->isFirstClassType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002427 return Error(FirstEltLoc, "invalid array element type: " +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002428 getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002429
Owen Anderson4056ca92009-07-29 22:17:13 +00002430 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002431
Chris Lattnerac161bf2009-01-02 07:01:27 +00002432 // Verify all elements are correct type!
Chris Lattner59d0e3b2009-01-02 08:49:06 +00002433 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002434 if (Elts[i]->getType() != Elts[0]->getType())
2435 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002436 "array element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002437 " is not of type '" + getTypeString(Elts[0]->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002438 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002439
Jay Foad83be3612011-06-22 09:24:39 +00002440 ID.ConstantVal = ConstantArray::get(ATy, Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002441 ID.Kind = ValID::t_Constant;
2442 return false;
2443 }
2444 case lltok::kw_c: // c "foo"
2445 Lex.Lex();
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002446 ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
2447 false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002448 if (ParseToken(lltok::StringConstant, "expected string")) return true;
2449 ID.Kind = ValID::t_Constant;
2450 return false;
2451
2452 case lltok::kw_asm: {
Chad Rosier6f9c3852013-02-14 20:44:07 +00002453 // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
2454 // STRINGCONSTANT
Chad Rosierd8c76102012-09-05 19:00:49 +00002455 bool HasSideEffect, AlignStack, AsmDialect;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002456 Lex.Lex();
2457 if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002458 ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
Chad Rosierd8c76102012-09-05 19:00:49 +00002459 ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
Chris Lattner3822f632009-01-02 08:05:26 +00002460 ParseStringConstant(ID.StrVal) ||
2461 ParseToken(lltok::comma, "expected comma in inline asm expression") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002462 ParseToken(lltok::StringConstant, "expected constraint string"))
2463 return true;
2464 ID.StrVal2 = Lex.getStrVal();
Chad Rosierf42fad62012-09-05 00:08:17 +00002465 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) |
Chad Rosierd8c76102012-09-05 19:00:49 +00002466 (unsigned(AsmDialect)<<2);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002467 ID.Kind = ValID::t_InlineAsm;
2468 return false;
2469 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002470
Chris Lattner3432c622009-10-28 03:39:23 +00002471 case lltok::kw_blockaddress: {
2472 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
2473 Lex.Lex();
2474
2475 ValID Fn, Label;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002476
Chris Lattner3432c622009-10-28 03:39:23 +00002477 if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
2478 ParseValID(Fn) ||
2479 ParseToken(lltok::comma, "expected comma in block address expression")||
2480 ParseValID(Label) ||
2481 ParseToken(lltok::rparen, "expected ')' in block address expression"))
2482 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002483
Chris Lattner3432c622009-10-28 03:39:23 +00002484 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
2485 return Error(Fn.Loc, "expected function name in blockaddress");
Chris Lattneraa99c942009-11-01 01:27:45 +00002486 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
Chris Lattner3432c622009-10-28 03:39:23 +00002487 return Error(Label.Loc, "expected basic block name in blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002488
Chris Lattner3432c622009-10-28 03:39:23 +00002489 // Make a global variable as a placeholder for this reference.
2490 GlobalVariable *FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context),
2491 false, GlobalValue::InternalLinkage,
Craig Topper2617dcc2014-04-15 06:32:26 +00002492 nullptr, "");
Chris Lattner3432c622009-10-28 03:39:23 +00002493 ForwardRefBlockAddresses[Fn].push_back(std::make_pair(Label, FwdRef));
2494 ID.ConstantVal = FwdRef;
2495 ID.Kind = ValID::t_Constant;
2496 return false;
2497 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002498
Chris Lattnerac161bf2009-01-02 07:01:27 +00002499 case lltok::kw_trunc:
2500 case lltok::kw_zext:
2501 case lltok::kw_sext:
2502 case lltok::kw_fptrunc:
2503 case lltok::kw_fpext:
2504 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002505 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002506 case lltok::kw_uitofp:
2507 case lltok::kw_sitofp:
2508 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002509 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002510 case lltok::kw_inttoptr:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002511 case lltok::kw_ptrtoint: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002512 unsigned Opc = Lex.getUIntVal();
Craig Topper2617dcc2014-04-15 06:32:26 +00002513 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002514 Constant *SrcVal;
2515 Lex.Lex();
2516 if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
2517 ParseGlobalTypeAndValue(SrcVal) ||
Dan Gohman0b5d0422009-06-15 21:52:11 +00002518 ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002519 ParseType(DestTy) ||
2520 ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
2521 return true;
2522 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
2523 return Error(ID.Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002524 getTypeString(SrcVal->getType()) + "' to '" +
2525 getTypeString(DestTy) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002526 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
Owen Anderson02a9da32009-07-01 23:57:11 +00002527 SrcVal, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002528 ID.Kind = ValID::t_Constant;
2529 return false;
2530 }
2531 case lltok::kw_extractvalue: {
2532 Lex.Lex();
2533 Constant *Val;
2534 SmallVector<unsigned, 4> Indices;
2535 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
2536 ParseGlobalTypeAndValue(Val) ||
2537 ParseIndexList(Indices) ||
2538 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
2539 return true;
Devang Patel1cb51162009-11-03 19:06:07 +00002540
Chris Lattner392be582010-02-12 20:49:41 +00002541 if (!Val->getType()->isAggregateType())
2542 return Error(ID.Loc, "extractvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002543 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002544 return Error(ID.Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002545 ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002546 ID.Kind = ValID::t_Constant;
2547 return false;
2548 }
2549 case lltok::kw_insertvalue: {
2550 Lex.Lex();
2551 Constant *Val0, *Val1;
2552 SmallVector<unsigned, 4> Indices;
2553 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
2554 ParseGlobalTypeAndValue(Val0) ||
2555 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
2556 ParseGlobalTypeAndValue(Val1) ||
2557 ParseIndexList(Indices) ||
2558 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
2559 return true;
Chris Lattner392be582010-02-12 20:49:41 +00002560 if (!Val0->getType()->isAggregateType())
2561 return Error(ID.Loc, "insertvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002562 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002563 return Error(ID.Loc, "invalid indices for insertvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002564 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002565 ID.Kind = ValID::t_Constant;
2566 return false;
2567 }
2568 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002569 case lltok::kw_fcmp: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002570 unsigned PredVal, Opc = Lex.getUIntVal();
2571 Constant *Val0, *Val1;
2572 Lex.Lex();
2573 if (ParseCmpPredicate(PredVal, Opc) ||
2574 ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
2575 ParseGlobalTypeAndValue(Val0) ||
2576 ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
2577 ParseGlobalTypeAndValue(Val1) ||
2578 ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
2579 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002580
Chris Lattnerac161bf2009-01-02 07:01:27 +00002581 if (Val0->getType() != Val1->getType())
2582 return Error(ID.Loc, "compare operands must have the same type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002583
Chris Lattnerac161bf2009-01-02 07:01:27 +00002584 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002585
Chris Lattnerac161bf2009-01-02 07:01:27 +00002586 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002587 if (!Val0->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002588 return Error(ID.Loc, "fcmp requires floating point operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002589 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002590 } else {
2591 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002592 if (!Val0->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002593 !Val0->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002594 return Error(ID.Loc, "icmp requires pointer or integer operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002595 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002596 }
2597 ID.Kind = ValID::t_Constant;
2598 return false;
2599 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002600
Chris Lattnerac161bf2009-01-02 07:01:27 +00002601 // Binary Operators.
2602 case lltok::kw_add:
Dan Gohmana5b96452009-06-04 22:49:04 +00002603 case lltok::kw_fadd:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002604 case lltok::kw_sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002605 case lltok::kw_fsub:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002606 case lltok::kw_mul:
Dan Gohmana5b96452009-06-04 22:49:04 +00002607 case lltok::kw_fmul:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002608 case lltok::kw_udiv:
2609 case lltok::kw_sdiv:
2610 case lltok::kw_fdiv:
2611 case lltok::kw_urem:
2612 case lltok::kw_srem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002613 case lltok::kw_frem:
2614 case lltok::kw_shl:
2615 case lltok::kw_lshr:
2616 case lltok::kw_ashr: {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002617 bool NUW = false;
2618 bool NSW = false;
2619 bool Exact = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002620 unsigned Opc = Lex.getUIntVal();
2621 Constant *Val0, *Val1;
2622 Lex.Lex();
Dan Gohman9c7f8082009-07-27 16:11:46 +00002623 LocTy ModifierLoc = Lex.getLoc();
Chris Lattnera676c0f2011-02-07 16:40:21 +00002624 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
2625 Opc == Instruction::Mul || Opc == Instruction::Shl) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002626 if (EatIfPresent(lltok::kw_nuw))
2627 NUW = true;
2628 if (EatIfPresent(lltok::kw_nsw)) {
2629 NSW = true;
2630 if (EatIfPresent(lltok::kw_nuw))
2631 NUW = true;
2632 }
Chris Lattnera676c0f2011-02-07 16:40:21 +00002633 } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
2634 Opc == Instruction::LShr || Opc == Instruction::AShr) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002635 if (EatIfPresent(lltok::kw_exact))
2636 Exact = true;
2637 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00002638 if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
2639 ParseGlobalTypeAndValue(Val0) ||
2640 ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
2641 ParseGlobalTypeAndValue(Val1) ||
2642 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
2643 return true;
2644 if (Val0->getType() != Val1->getType())
2645 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002646 if (!Val0->getType()->isIntOrIntVectorTy()) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002647 if (NUW)
2648 return Error(ModifierLoc, "nuw only applies to integer operations");
2649 if (NSW)
2650 return Error(ModifierLoc, "nsw only applies to integer operations");
2651 }
Dan Gohmana2414ea2010-05-03 22:44:19 +00002652 // Check that the type is valid for the operator.
2653 switch (Opc) {
2654 case Instruction::Add:
2655 case Instruction::Sub:
2656 case Instruction::Mul:
2657 case Instruction::UDiv:
2658 case Instruction::SDiv:
2659 case Instruction::URem:
2660 case Instruction::SRem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002661 case Instruction::Shl:
2662 case Instruction::AShr:
2663 case Instruction::LShr:
Dan Gohmana2414ea2010-05-03 22:44:19 +00002664 if (!Val0->getType()->isIntOrIntVectorTy())
2665 return Error(ID.Loc, "constexpr requires integer operands");
2666 break;
2667 case Instruction::FAdd:
2668 case Instruction::FSub:
2669 case Instruction::FMul:
2670 case Instruction::FDiv:
2671 case Instruction::FRem:
2672 if (!Val0->getType()->isFPOrFPVectorTy())
2673 return Error(ID.Loc, "constexpr requires fp operands");
2674 break;
2675 default: llvm_unreachable("Unknown binary operator!");
2676 }
Dan Gohman1b849082009-09-07 23:54:19 +00002677 unsigned Flags = 0;
2678 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2679 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
Chris Lattner35315d02011-02-06 21:44:57 +00002680 if (Exact) Flags |= PossiblyExactOperator::IsExact;
Dan Gohman1b849082009-09-07 23:54:19 +00002681 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
Dan Gohman9c7f8082009-07-27 16:11:46 +00002682 ID.ConstantVal = C;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002683 ID.Kind = ValID::t_Constant;
2684 return false;
2685 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002686
Chris Lattnerac161bf2009-01-02 07:01:27 +00002687 // Logical Operations
Chris Lattnerac161bf2009-01-02 07:01:27 +00002688 case lltok::kw_and:
2689 case lltok::kw_or:
2690 case lltok::kw_xor: {
2691 unsigned Opc = Lex.getUIntVal();
2692 Constant *Val0, *Val1;
2693 Lex.Lex();
2694 if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
2695 ParseGlobalTypeAndValue(Val0) ||
2696 ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
2697 ParseGlobalTypeAndValue(Val1) ||
2698 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
2699 return true;
2700 if (Val0->getType() != Val1->getType())
2701 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002702 if (!Val0->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002703 return Error(ID.Loc,
2704 "constexpr requires integer or integer vector operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002705 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002706 ID.Kind = ValID::t_Constant;
2707 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002708 }
2709
Chris Lattnerac161bf2009-01-02 07:01:27 +00002710 case lltok::kw_getelementptr:
2711 case lltok::kw_shufflevector:
2712 case lltok::kw_insertelement:
2713 case lltok::kw_extractelement:
2714 case lltok::kw_select: {
2715 unsigned Opc = Lex.getUIntVal();
2716 SmallVector<Constant*, 16> Elts;
Dan Gohman1639c392009-07-27 21:53:46 +00002717 bool InBounds = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002718 Lex.Lex();
Dan Gohman1639c392009-07-27 21:53:46 +00002719 if (Opc == Instruction::GetElementPtr)
Dan Gohman16cbbe42009-07-29 15:58:36 +00002720 InBounds = EatIfPresent(lltok::kw_inbounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002721 if (ParseToken(lltok::lparen, "expected '(' in constantexpr") ||
2722 ParseGlobalValueVector(Elts) ||
2723 ParseToken(lltok::rparen, "expected ')' in constantexpr"))
2724 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002725
Chris Lattnerac161bf2009-01-02 07:01:27 +00002726 if (Opc == Instruction::GetElementPtr) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00002727 if (Elts.size() == 0 ||
2728 !Elts[0]->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002729 return Error(ID.Loc, "getelementptr requires pointer operand");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002730
Jay Foaded8db7d2011-07-21 14:31:17 +00002731 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
Jay Foadd1b78492011-07-25 09:48:08 +00002732 if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002733 return Error(ID.Loc, "invalid indices for getelementptr");
Jay Foad2f5fc8c2011-07-21 15:15:37 +00002734 ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], Indices,
2735 InBounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002736 } else if (Opc == Instruction::Select) {
2737 if (Elts.size() != 3)
2738 return Error(ID.Loc, "expected three operands to select");
2739 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
2740 Elts[2]))
2741 return Error(ID.Loc, Reason);
Owen Anderson487375e2009-07-29 18:55:55 +00002742 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002743 } else if (Opc == Instruction::ShuffleVector) {
2744 if (Elts.size() != 3)
2745 return Error(ID.Loc, "expected three operands to shufflevector");
2746 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2747 return Error(ID.Loc, "invalid operands to shufflevector");
Owen Anderson02a9da32009-07-01 23:57:11 +00002748 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002749 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002750 } else if (Opc == Instruction::ExtractElement) {
2751 if (Elts.size() != 2)
2752 return Error(ID.Loc, "expected two operands to extractelement");
2753 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
2754 return Error(ID.Loc, "invalid extractelement operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002755 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002756 } else {
2757 assert(Opc == Instruction::InsertElement && "Unknown opcode");
2758 if (Elts.size() != 3)
2759 return Error(ID.Loc, "expected three operands to insertelement");
2760 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2761 return Error(ID.Loc, "invalid insertelement operands");
Owen Anderson02a9da32009-07-01 23:57:11 +00002762 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002763 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002764 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002765
Chris Lattnerac161bf2009-01-02 07:01:27 +00002766 ID.Kind = ValID::t_Constant;
2767 return false;
2768 }
2769 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002770
Chris Lattnerac161bf2009-01-02 07:01:27 +00002771 Lex.Lex();
2772 return false;
2773}
2774
2775/// ParseGlobalValue - Parse a global value with the specified type.
Chris Lattner229907c2011-07-18 04:54:35 +00002776bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002777 C = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002778 ValID ID;
Craig Topper2617dcc2014-04-15 06:32:26 +00002779 Value *V = nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002780 bool Parsed = ParseValID(ID) ||
Craig Topper2617dcc2014-04-15 06:32:26 +00002781 ConvertValIDToValue(Ty, ID, V, nullptr);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002782 if (V && !(C = dyn_cast<Constant>(V)))
2783 return Error(ID.Loc, "global values must be constants");
2784 return Parsed;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002785}
2786
Victor Hernandez9d75c962010-01-11 22:31:58 +00002787bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002788 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002789 return ParseType(Ty) ||
2790 ParseGlobalValue(Ty, V);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002791}
2792
2793/// ParseGlobalValueVector
2794/// ::= /*empty*/
2795/// ::= TypeAndValue (',' TypeAndValue)*
2796bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts) {
2797 // Empty list.
2798 if (Lex.getKind() == lltok::rbrace ||
2799 Lex.getKind() == lltok::rsquare ||
2800 Lex.getKind() == lltok::greater ||
2801 Lex.getKind() == lltok::rparen)
2802 return false;
2803
2804 Constant *C;
2805 if (ParseGlobalTypeAndValue(C)) return true;
2806 Elts.push_back(C);
2807
2808 while (EatIfPresent(lltok::comma)) {
2809 if (ParseGlobalTypeAndValue(C)) return true;
2810 Elts.push_back(C);
2811 }
2812
2813 return false;
2814}
2815
Dan Gohmanc828c542010-08-24 02:24:03 +00002816bool LLParser::ParseMetadataListValue(ValID &ID, PerFunctionState *PFS) {
2817 assert(Lex.getKind() == lltok::lbrace);
2818 Lex.Lex();
2819
2820 SmallVector<Value*, 16> Elts;
2821 if (ParseMDNodeVector(Elts, PFS) ||
2822 ParseToken(lltok::rbrace, "expected end of metadata node"))
2823 return true;
2824
Jay Foad5514afe2011-04-21 19:59:31 +00002825 ID.MDNodeVal = MDNode::get(Context, Elts);
Dan Gohmanc828c542010-08-24 02:24:03 +00002826 ID.Kind = ValID::t_MDNode;
2827 return false;
2828}
2829
Dan Gohman8939ba332010-07-14 18:26:50 +00002830/// ParseMetadataValue
2831/// ::= !42
2832/// ::= !{...}
2833/// ::= !"string"
2834bool LLParser::ParseMetadataValue(ValID &ID, PerFunctionState *PFS) {
2835 assert(Lex.getKind() == lltok::exclaim);
2836 Lex.Lex();
2837
2838 // MDNode:
2839 // !{ ... }
Dan Gohmanc828c542010-08-24 02:24:03 +00002840 if (Lex.getKind() == lltok::lbrace)
2841 return ParseMetadataListValue(ID, PFS);
Dan Gohman8939ba332010-07-14 18:26:50 +00002842
2843 // Standalone metadata reference
2844 // !42
2845 if (Lex.getKind() == lltok::APSInt) {
2846 if (ParseMDNodeID(ID.MDNodeVal)) return true;
2847 ID.Kind = ValID::t_MDNode;
2848 return false;
2849 }
2850
2851 // MDString:
2852 // ::= '!' STRINGCONSTANT
2853 if (ParseMDString(ID.MDStringVal)) return true;
2854 ID.Kind = ValID::t_MDString;
2855 return false;
2856}
2857
Victor Hernandez9d75c962010-01-11 22:31:58 +00002858
2859//===----------------------------------------------------------------------===//
2860// Function Parsing.
2861//===----------------------------------------------------------------------===//
2862
Chris Lattner229907c2011-07-18 04:54:35 +00002863bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez9d75c962010-01-11 22:31:58 +00002864 PerFunctionState *PFS) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002865 if (Ty->isFunctionTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002866 return Error(ID.Loc, "functions are not values, refer to them as pointers");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002867
Chris Lattnerac161bf2009-01-02 07:01:27 +00002868 switch (ID.Kind) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002869 case ValID::t_LocalID:
Victor Hernandez9d75c962010-01-11 22:31:58 +00002870 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2871 V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002872 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002873 case ValID::t_LocalName:
Victor Hernandez9d75c962010-01-11 22:31:58 +00002874 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2875 V = PFS->GetVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002876 return V == nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002877 case ValID::t_InlineAsm: {
Chris Lattner229907c2011-07-18 04:54:35 +00002878 PointerType *PTy = dyn_cast<PointerType>(Ty);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002879 FunctionType *FTy =
Craig Topper2617dcc2014-04-15 06:32:26 +00002880 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002881 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
2882 return Error(ID.Loc, "invalid type for inline asm constraint string");
Chad Rosierf42fad62012-09-05 00:08:17 +00002883 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1,
Chad Rosierd8c76102012-09-05 19:00:49 +00002884 (ID.UIntVal>>1)&1, (InlineAsm::AsmDialect(ID.UIntVal>>2)));
Victor Hernandez9d75c962010-01-11 22:31:58 +00002885 return false;
2886 }
2887 case ValID::t_MDNode:
2888 if (!Ty->isMetadataTy())
2889 return Error(ID.Loc, "metadata value must have metadata type");
2890 V = ID.MDNodeVal;
2891 return false;
2892 case ValID::t_MDString:
2893 if (!Ty->isMetadataTy())
2894 return Error(ID.Loc, "metadata value must have metadata type");
2895 V = ID.MDStringVal;
2896 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002897 case ValID::t_GlobalName:
2898 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002899 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002900 case ValID::t_GlobalID:
2901 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002902 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002903 case ValID::t_APSInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002904 if (!Ty->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002905 return Error(ID.Loc, "integer constant must have integer type");
Jay Foad583abbc2010-12-07 08:25:19 +00002906 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
Owen Andersonedb4a702009-07-24 23:12:02 +00002907 V = ConstantInt::get(Context, ID.APSIntVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002908 return false;
2909 case ValID::t_APFloat:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002910 if (!Ty->isFloatingPointTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002911 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
2912 return Error(ID.Loc, "floating point constant invalid for type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002913
Dan Gohman518cda42011-12-17 00:04:22 +00002914 // The lexer has no type info, so builds all half, float, and double FP
2915 // constants as double. Fix this here. Long double does not need this.
2916 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002917 bool Ignored;
Dan Gohman518cda42011-12-17 00:04:22 +00002918 if (Ty->isHalfTy())
2919 ID.APFloatVal.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven,
2920 &Ignored);
2921 else if (Ty->isFloatTy())
2922 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
2923 &Ignored);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002924 }
Owen Anderson69c464d2009-07-27 20:59:43 +00002925 V = ConstantFP::get(Context, ID.APFloatVal);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002926
Chris Lattner8f57d29e2009-01-05 18:24:23 +00002927 if (V->getType() != Ty)
2928 return Error(ID.Loc, "floating point constant does not have type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002929 getTypeString(Ty) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002930
Chris Lattnerac161bf2009-01-02 07:01:27 +00002931 return false;
2932 case ValID::t_Null:
Duncan Sands19d0b472010-02-16 11:11:14 +00002933 if (!Ty->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002934 return Error(ID.Loc, "null must be a pointer type");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002935 V = ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002936 return false;
2937 case ValID::t_Undef:
Chris Lattnerffa07782009-01-05 08:13:38 +00002938 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002939 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerffa07782009-01-05 08:13:38 +00002940 return Error(ID.Loc, "invalid type for undef constant");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002941 V = UndefValue::get(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002942 return false;
Chris Lattner998fa0a2009-01-05 07:52:51 +00002943 case ValID::t_EmptyArray:
Duncan Sands19d0b472010-02-16 11:11:14 +00002944 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
Chris Lattner998fa0a2009-01-05 07:52:51 +00002945 return Error(ID.Loc, "invalid empty array initializer");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002946 V = UndefValue::get(Ty);
Chris Lattner998fa0a2009-01-05 07:52:51 +00002947 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002948 case ValID::t_Zero:
Chris Lattnerffa07782009-01-05 08:13:38 +00002949 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002950 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002951 return Error(ID.Loc, "invalid type for null constant");
Owen Anderson5a1acd92009-07-31 20:28:14 +00002952 V = Constant::getNullValue(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002953 return false;
2954 case ValID::t_Constant:
Chris Lattner13ee7952010-08-28 04:09:24 +00002955 if (ID.ConstantVal->getType() != Ty)
Chris Lattnerac161bf2009-01-02 07:01:27 +00002956 return Error(ID.Loc, "constant expression type mismatch");
Chris Lattner392be582010-02-12 20:49:41 +00002957
Chris Lattnerac161bf2009-01-02 07:01:27 +00002958 V = ID.ConstantVal;
2959 return false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002960 case ValID::t_ConstantStruct:
2961 case ValID::t_PackedConstantStruct:
Chris Lattner229907c2011-07-18 04:54:35 +00002962 if (StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002963 if (ST->getNumElements() != ID.UIntVal)
2964 return Error(ID.Loc,
2965 "initializer with struct type has wrong # elements");
2966 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
2967 return Error(ID.Loc, "packed'ness of initializer and type don't match");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002968
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002969 // Verify that the elements are compatible with the structtype.
2970 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
2971 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
2972 return Error(ID.Loc, "element " + Twine(i) +
2973 " of struct initializer doesn't match struct element type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002974
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002975 V = ConstantStruct::get(ST, makeArrayRef(ID.ConstantStructElts,
2976 ID.UIntVal));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002977 } else
2978 return Error(ID.Loc, "constant expression type mismatch");
2979 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002980 }
Chandler Carruthf3e85022012-01-10 18:08:01 +00002981 llvm_unreachable("Invalid ValID");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002982}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002983
Chris Lattner229907c2011-07-18 04:54:35 +00002984bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002985 V = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002986 ValID ID;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002987 return ParseValID(ID, PFS) ||
2988 ConvertValIDToValue(Ty, ID, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002989}
2990
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002991bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002992 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002993 return ParseType(Ty) ||
2994 ParseValue(Ty, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002995}
2996
Chris Lattner3ed871f2009-10-27 19:13:16 +00002997bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
2998 PerFunctionState &PFS) {
2999 Value *V;
3000 Loc = Lex.getLoc();
3001 if (ParseTypeAndValue(V, PFS)) return true;
3002 if (!isa<BasicBlock>(V))
3003 return Error(Loc, "expected a basic block");
3004 BB = cast<BasicBlock>(V);
3005 return false;
3006}
3007
3008
Chris Lattnerac161bf2009-01-02 07:01:27 +00003009/// FunctionHeader
3010/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
Rafael Espindola45e6c192011-01-08 16:42:36 +00003011/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003012/// OptionalAlign OptGC OptionalPrefix
Chris Lattnerac161bf2009-01-02 07:01:27 +00003013bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
3014 // Parse the linkage.
3015 LocTy LinkageLoc = Lex.getLoc();
3016 unsigned Linkage;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003017
Kostya Serebryanya5054ad2012-01-20 17:56:17 +00003018 unsigned Visibility;
Nico Rieck7157bb72014-01-14 15:22:47 +00003019 unsigned DLLStorageClass;
Bill Wendling50d27842012-10-15 20:35:56 +00003020 AttrBuilder RetAttrs;
Sandeep Patel68c5f472009-09-02 08:44:58 +00003021 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00003022 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003023 LocTy RetTypeLoc = Lex.getLoc();
3024 if (ParseOptionalLinkage(Linkage) ||
3025 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +00003026 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003027 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00003028 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00003029 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003030 return true;
3031
3032 // Verify that the linkage is ok.
3033 switch ((GlobalValue::LinkageTypes)Linkage) {
3034 case GlobalValue::ExternalLinkage:
3035 break; // always ok.
Duncan Sandse2881052009-03-11 08:08:06 +00003036 case GlobalValue::ExternalWeakLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003037 if (isDefine)
3038 return Error(LinkageLoc, "invalid linkage for function definition");
3039 break;
Rafael Espindola6de96a12009-01-15 20:18:42 +00003040 case GlobalValue::PrivateLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003041 case GlobalValue::InternalLinkage:
Nick Lewycky8019af62009-04-13 07:02:02 +00003042 case GlobalValue::AvailableExternallyLinkage:
Duncan Sands12da8ce2009-03-07 15:45:40 +00003043 case GlobalValue::LinkOnceAnyLinkage:
3044 case GlobalValue::LinkOnceODRLinkage:
3045 case GlobalValue::WeakAnyLinkage:
3046 case GlobalValue::WeakODRLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003047 if (!isDefine)
3048 return Error(LinkageLoc, "invalid linkage for function declaration");
3049 break;
3050 case GlobalValue::AppendingLinkage:
Duncan Sands4581beb2009-03-11 20:14:15 +00003051 case GlobalValue::CommonLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003052 return Error(LinkageLoc, "invalid function linkage type");
3053 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003054
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00003055 if (!isValidVisibilityForLinkage(Visibility, Linkage))
3056 return Error(LinkageLoc,
3057 "symbol with local linkage must have default visibility");
3058
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003059 if (!FunctionType::isValidReturnType(RetType))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003060 return Error(RetTypeLoc, "invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003061
Chris Lattnerac161bf2009-01-02 07:01:27 +00003062 LocTy NameLoc = Lex.getLoc();
Chris Lattner778c62c2009-02-18 21:48:13 +00003063
3064 std::string FunctionName;
3065 if (Lex.getKind() == lltok::GlobalVar) {
3066 FunctionName = Lex.getStrVal();
3067 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
3068 unsigned NameID = Lex.getUIntVal();
3069
3070 if (NameID != NumberedVals.size())
3071 return TokError("function expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00003072 Twine(NumberedVals.size()) + "'");
Chris Lattner778c62c2009-02-18 21:48:13 +00003073 } else {
3074 return TokError("expected function name");
3075 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003076
Chris Lattner3822f632009-01-02 08:05:26 +00003077 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003078
Chris Lattner3822f632009-01-02 08:05:26 +00003079 if (Lex.getKind() != lltok::lparen)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003080 return TokError("expected '(' in function argument list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003081
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003082 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003083 bool isVarArg;
Bill Wendling50d27842012-10-15 20:35:56 +00003084 AttrBuilder FuncAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003085 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00003086 LocTy BuiltinLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003087 std::string Section;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003088 unsigned Alignment;
Chris Lattner3822f632009-01-02 08:05:26 +00003089 std::string GC;
Rafael Espindola563eb4b2011-01-25 19:09:56 +00003090 bool UnnamedAddr;
3091 LocTy UnnamedAddrLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00003092 Constant *Prefix = nullptr;
Chris Lattner3822f632009-01-02 08:05:26 +00003093
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003094 if (ParseArgumentList(ArgList, isVarArg) ||
Rafael Espindola563eb4b2011-01-25 19:09:56 +00003095 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
3096 &UnnamedAddrLoc) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003097 ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00003098 BuiltinLoc) ||
Chris Lattner3822f632009-01-02 08:05:26 +00003099 (EatIfPresent(lltok::kw_section) &&
3100 ParseStringConstant(Section)) ||
3101 ParseOptionalAlignment(Alignment) ||
3102 (EatIfPresent(lltok::kw_gc) &&
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003103 ParseStringConstant(GC)) ||
3104 (EatIfPresent(lltok::kw_prefix) &&
3105 ParseGlobalTypeAndValue(Prefix)))
Chris Lattner3822f632009-01-02 08:05:26 +00003106 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003107
Michael Gottesman41748d72013-06-27 00:25:01 +00003108 if (FuncAttrs.contains(Attribute::Builtin))
3109 return Error(BuiltinLoc, "'builtin' attribute not valid on function");
Bill Wendling09bd1f72013-02-22 00:12:35 +00003110
Chris Lattnerac161bf2009-01-02 07:01:27 +00003111 // If the alignment was parsed as an attribute, move to the alignment field.
Bill Wendlingc6daefa2012-10-08 23:27:46 +00003112 if (FuncAttrs.hasAlignmentAttr()) {
Bill Wendling9be77592012-09-21 15:26:31 +00003113 Alignment = FuncAttrs.getAlignment();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003114 FuncAttrs.removeAttribute(Attribute::Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003115 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003116
Chris Lattnerac161bf2009-01-02 07:01:27 +00003117 // Okay, if we got here, the function is syntactically valid. Convert types
3118 // and do semantic checks.
Jay Foadb804a2b2011-07-12 14:06:48 +00003119 std::vector<Type*> ParamTypeList;
Bill Wendlingf5075a42013-01-27 02:24:02 +00003120 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003121
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003122 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003123 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3124 AttributeSet::ReturnIndex,
3125 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003126
Chris Lattnerac161bf2009-01-02 07:01:27 +00003127 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003128 ParamTypeList.push_back(ArgList[i].Ty);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00003129 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3130 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00003131 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3132 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003133 }
3134
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003135 if (FuncAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003136 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3137 AttributeSet::FunctionIndex,
3138 FuncAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003139
Bill Wendlinge94d8432012-12-07 23:16:57 +00003140 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003141
Bill Wendling749a43d2012-12-30 13:50:49 +00003142 if (PAL.hasAttribute(1, Attribute::StructRet) && !RetType->isVoidTy())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003143 return Error(RetTypeLoc, "functions with 'sret' argument must return void");
3144
Chris Lattner229907c2011-07-18 04:54:35 +00003145 FunctionType *FT =
Owen Anderson4056ca92009-07-29 22:17:13 +00003146 FunctionType::get(RetType, ParamTypeList, isVarArg);
Chris Lattner229907c2011-07-18 04:54:35 +00003147 PointerType *PFT = PointerType::getUnqual(FT);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003148
Craig Topper2617dcc2014-04-15 06:32:26 +00003149 Fn = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003150 if (!FunctionName.empty()) {
3151 // If this was a definition of a forward reference, remove the definition
3152 // from the forward reference table and fill in the forward ref.
3153 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI =
3154 ForwardRefVals.find(FunctionName);
3155 if (FRVI != ForwardRefVals.end()) {
3156 Fn = M->getFunction(FunctionName);
Nick Lewycky686d7cb2012-10-11 00:38:25 +00003157 if (!Fn)
3158 return Error(FRVI->second.second, "invalid forward reference to "
3159 "function as global value!");
Chris Lattnerc239eb72010-04-20 04:49:11 +00003160 if (Fn->getType() != PFT)
3161 return Error(FRVI->second.second, "invalid forward reference to "
3162 "function '" + FunctionName + "' with wrong type!");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003163
Chris Lattnerac161bf2009-01-02 07:01:27 +00003164 ForwardRefVals.erase(FRVI);
3165 } else if ((Fn = M->getFunction(FunctionName))) {
Chris Lattner5756c162011-06-17 07:06:44 +00003166 // Reject redefinitions.
3167 return Error(NameLoc, "invalid redefinition of function '" +
3168 FunctionName + "'");
Chris Lattnere38317f2009-10-25 23:22:50 +00003169 } else if (M->getNamedValue(FunctionName)) {
3170 return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003171 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003172
Dan Gohman399d6ae2009-08-29 23:37:49 +00003173 } else {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003174 // If this is a definition of a forward referenced function, make sure the
3175 // types agree.
3176 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I
3177 = ForwardRefValIDs.find(NumberedVals.size());
3178 if (I != ForwardRefValIDs.end()) {
3179 Fn = cast<Function>(I->second.first);
3180 if (Fn->getType() != PFT)
3181 return Error(NameLoc, "type of definition and forward reference of '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00003182 Twine(NumberedVals.size()) + "' disagree");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003183 ForwardRefValIDs.erase(I);
3184 }
3185 }
3186
Craig Topper2617dcc2014-04-15 06:32:26 +00003187 if (!Fn)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003188 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
3189 else // Move the forward-reference to the correct spot in the module.
3190 M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
3191
3192 if (FunctionName.empty())
3193 NumberedVals.push_back(Fn);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003194
Chris Lattnerac161bf2009-01-02 07:01:27 +00003195 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
3196 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +00003197 Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003198 Fn->setCallingConv(CC);
3199 Fn->setAttributes(PAL);
Rafael Espindola45e6c192011-01-08 16:42:36 +00003200 Fn->setUnnamedAddr(UnnamedAddr);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003201 Fn->setAlignment(Alignment);
3202 Fn->setSection(Section);
3203 if (!GC.empty()) Fn->setGC(GC.c_str());
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003204 Fn->setPrefixData(Prefix);
Bill Wendlingb32b0412013-02-08 06:32:06 +00003205 ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003206
Chris Lattnerac161bf2009-01-02 07:01:27 +00003207 // Add all of the arguments we parsed to the function.
3208 Function::arg_iterator ArgIt = Fn->arg_begin();
3209 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
3210 // If the argument has a name, insert it into the argument symbol table.
3211 if (ArgList[i].Name.empty()) continue;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003212
Chris Lattnerac161bf2009-01-02 07:01:27 +00003213 // Set the name, if it conflicted, it will be auto-renamed.
3214 ArgIt->setName(ArgList[i].Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003215
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00003216 if (ArgIt->getName() != ArgList[i].Name)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003217 return Error(ArgList[i].Loc, "redefinition of argument '%" +
3218 ArgList[i].Name + "'");
3219 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003220
Chris Lattnerac161bf2009-01-02 07:01:27 +00003221 return false;
3222}
3223
3224
3225/// ParseFunctionBody
3226/// ::= '{' BasicBlock+ '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00003227///
3228bool LLParser::ParseFunctionBody(Function &Fn) {
Chris Lattner4649a732011-06-17 06:42:57 +00003229 if (Lex.getKind() != lltok::lbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003230 return TokError("expected '{' in function body");
3231 Lex.Lex(); // eat the {.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003232
Chris Lattner3432c622009-10-28 03:39:23 +00003233 int FunctionNumber = -1;
3234 if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003235
Chris Lattner3432c622009-10-28 03:39:23 +00003236 PerFunctionState PFS(*this, Fn, FunctionNumber);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003237
Chris Lattnerbbddd962010-01-09 19:20:07 +00003238 // We need at least one basic block.
Chris Lattner4649a732011-06-17 06:42:57 +00003239 if (Lex.getKind() == lltok::rbrace)
Chris Lattnerbbddd962010-01-09 19:20:07 +00003240 return TokError("function body requires at least one basic block");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003241
Chris Lattner4649a732011-06-17 06:42:57 +00003242 while (Lex.getKind() != lltok::rbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003243 if (ParseBasicBlock(PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003244
Chris Lattnerac161bf2009-01-02 07:01:27 +00003245 // Eat the }.
3246 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003247
Chris Lattnerac161bf2009-01-02 07:01:27 +00003248 // Verify function is ok.
Chris Lattner3432c622009-10-28 03:39:23 +00003249 return PFS.FinishFunction();
Chris Lattnerac161bf2009-01-02 07:01:27 +00003250}
3251
3252/// ParseBasicBlock
3253/// ::= LabelStr? Instruction*
3254bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
3255 // If this basic block starts out with a name, remember it.
3256 std::string Name;
3257 LocTy NameLoc = Lex.getLoc();
3258 if (Lex.getKind() == lltok::LabelStr) {
3259 Name = Lex.getStrVal();
3260 Lex.Lex();
3261 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003262
Chris Lattnerac161bf2009-01-02 07:01:27 +00003263 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003264 if (!BB) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003265
Chris Lattnerac161bf2009-01-02 07:01:27 +00003266 std::string NameStr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003267
Chris Lattnerac161bf2009-01-02 07:01:27 +00003268 // Parse the instructions in this block until we get a terminator.
3269 Instruction *Inst;
3270 do {
3271 // This instruction may have three possibilities for a name: a) none
3272 // specified, b) name specified "%foo =", c) number specified: "%4 =".
3273 LocTy NameLoc = Lex.getLoc();
3274 int NameID = -1;
3275 NameStr = "";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003276
Chris Lattnerac161bf2009-01-02 07:01:27 +00003277 if (Lex.getKind() == lltok::LocalVarID) {
3278 NameID = Lex.getUIntVal();
3279 Lex.Lex();
3280 if (ParseToken(lltok::equal, "expected '=' after instruction id"))
3281 return true;
Chris Lattnerdef19492011-06-17 06:36:20 +00003282 } else if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003283 NameStr = Lex.getStrVal();
3284 Lex.Lex();
3285 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
3286 return true;
3287 }
Devang Patelea8a4b92009-09-17 23:04:48 +00003288
Chris Lattner77b89dc2009-12-30 05:23:43 +00003289 switch (ParseInstruction(Inst, BB, PFS)) {
Craig Toppera2886c22012-02-07 05:05:23 +00003290 default: llvm_unreachable("Unknown ParseInstruction result!");
Chris Lattner77b89dc2009-12-30 05:23:43 +00003291 case InstError: return true;
3292 case InstNormal:
Chris Lattner2e664bd2010-04-07 04:08:57 +00003293 BB->getInstList().push_back(Inst);
3294
Chris Lattner77b89dc2009-12-30 05:23:43 +00003295 // With a normal result, we check to see if the instruction is followed by
3296 // a comma and metadata.
3297 if (EatIfPresent(lltok::comma))
Dan Gohman338d9a42010-08-24 02:05:17 +00003298 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattner77b89dc2009-12-30 05:23:43 +00003299 return true;
3300 break;
3301 case InstExtraComma:
Chris Lattner2e664bd2010-04-07 04:08:57 +00003302 BB->getInstList().push_back(Inst);
3303
Chris Lattner77b89dc2009-12-30 05:23:43 +00003304 // If the instruction parser ate an extra comma at the end of it, it
3305 // *must* be followed by metadata.
Dan Gohman338d9a42010-08-24 02:05:17 +00003306 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattner77b89dc2009-12-30 05:23:43 +00003307 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003308 break;
Chris Lattner77b89dc2009-12-30 05:23:43 +00003309 }
Devang Patelea8a4b92009-09-17 23:04:48 +00003310
Chris Lattnerac161bf2009-01-02 07:01:27 +00003311 // Set the name on the instruction.
3312 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
3313 } while (!isa<TerminatorInst>(Inst));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003314
Chris Lattnerac161bf2009-01-02 07:01:27 +00003315 return false;
3316}
3317
3318//===----------------------------------------------------------------------===//
3319// Instruction Parsing.
3320//===----------------------------------------------------------------------===//
3321
3322/// ParseInstruction - Parse one of the many different instructions.
3323///
Chris Lattner77b89dc2009-12-30 05:23:43 +00003324int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
3325 PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003326 lltok::Kind Token = Lex.getKind();
3327 if (Token == lltok::Eof)
3328 return TokError("found end of file when expecting more instructions");
3329 LocTy Loc = Lex.getLoc();
Chris Lattner89d856e2009-03-01 00:53:13 +00003330 unsigned KeywordVal = Lex.getUIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00003331 Lex.Lex(); // Eat the keyword.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003332
Chris Lattnerac161bf2009-01-02 07:01:27 +00003333 switch (Token) {
3334 default: return Error(Loc, "expected instruction opcode");
3335 // Terminator Instructions.
Owen Anderson55f1c092009-08-13 21:58:54 +00003336 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003337 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
3338 case lltok::kw_br: return ParseBr(Inst, PFS);
3339 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003340 case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003341 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +00003342 case lltok::kw_resume: return ParseResume(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003343 // Binary Operators.
3344 case lltok::kw_add:
3345 case lltok::kw_sub:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003346 case lltok::kw_mul:
3347 case lltok::kw_shl: {
Chris Lattnera676c0f2011-02-07 16:40:21 +00003348 bool NUW = EatIfPresent(lltok::kw_nuw);
3349 bool NSW = EatIfPresent(lltok::kw_nsw);
3350 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003351
Chris Lattnera676c0f2011-02-07 16:40:21 +00003352 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003353
Chris Lattnera676c0f2011-02-07 16:40:21 +00003354 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
3355 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
3356 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00003357 }
Dan Gohmana5b96452009-06-04 22:49:04 +00003358 case lltok::kw_fadd:
3359 case lltok::kw_fsub:
Michael Ilseman92053172012-11-27 00:42:44 +00003360 case lltok::kw_fmul:
3361 case lltok::kw_fdiv:
3362 case lltok::kw_frem: {
3363 FastMathFlags FMF = EatFastMathFlagsIfPresent();
3364 int Res = ParseArithmetic(Inst, PFS, KeywordVal, 2);
3365 if (Res != 0)
3366 return Res;
3367 if (FMF.any())
3368 Inst->setFastMathFlags(FMF);
3369 return 0;
3370 }
Dan Gohmana5b96452009-06-04 22:49:04 +00003371
Chris Lattner35315d02011-02-06 21:44:57 +00003372 case lltok::kw_sdiv:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003373 case lltok::kw_udiv:
3374 case lltok::kw_lshr:
3375 case lltok::kw_ashr: {
3376 bool Exact = EatIfPresent(lltok::kw_exact);
3377
3378 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
3379 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
3380 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00003381 }
3382
Chris Lattnerac161bf2009-01-02 07:01:27 +00003383 case lltok::kw_urem:
Chris Lattner89d856e2009-03-01 00:53:13 +00003384 case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003385 case lltok::kw_and:
3386 case lltok::kw_or:
Chris Lattner89d856e2009-03-01 00:53:13 +00003387 case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003388 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003389 case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003390 // Casts.
3391 case lltok::kw_trunc:
3392 case lltok::kw_zext:
3393 case lltok::kw_sext:
3394 case lltok::kw_fptrunc:
3395 case lltok::kw_fpext:
3396 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003397 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003398 case lltok::kw_uitofp:
3399 case lltok::kw_sitofp:
3400 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003401 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003402 case lltok::kw_inttoptr:
Chris Lattner89d856e2009-03-01 00:53:13 +00003403 case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003404 // Other.
3405 case lltok::kw_select: return ParseSelect(Inst, PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +00003406 case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003407 case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
3408 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
3409 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
3410 case lltok::kw_phi: return ParsePHI(Inst, PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +00003411 case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS);
Reid Kleckner5772b772014-04-24 20:14:34 +00003412 // Call.
3413 case lltok::kw_call: return ParseCall(Inst, PFS, CallInst::TCK_None);
3414 case lltok::kw_tail: return ParseCall(Inst, PFS, CallInst::TCK_Tail);
3415 case lltok::kw_musttail: return ParseCall(Inst, PFS, CallInst::TCK_MustTail);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003416 // Memory.
Victor Hernandezc7d6a832009-10-17 00:00:19 +00003417 case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +00003418 case lltok::kw_load: return ParseLoad(Inst, PFS);
3419 case lltok::kw_store: return ParseStore(Inst, PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +00003420 case lltok::kw_cmpxchg: return ParseCmpXchg(Inst, PFS);
3421 case lltok::kw_atomicrmw: return ParseAtomicRMW(Inst, PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +00003422 case lltok::kw_fence: return ParseFence(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003423 case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
3424 case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS);
3425 case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS);
3426 }
3427}
3428
3429/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
3430bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003431 if (Opc == Instruction::FCmp) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003432 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00003433 default: return TokError("expected fcmp predicate (e.g. 'oeq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003434 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
3435 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
3436 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
3437 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
3438 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
3439 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
3440 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
3441 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
3442 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
3443 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
3444 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
3445 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
3446 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
3447 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
3448 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
3449 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
3450 }
3451 } else {
3452 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00003453 default: return TokError("expected icmp predicate (e.g. 'eq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003454 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
3455 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
3456 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
3457 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
3458 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
3459 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
3460 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
3461 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
3462 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
3463 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
3464 }
3465 }
3466 Lex.Lex();
3467 return false;
3468}
3469
3470//===----------------------------------------------------------------------===//
3471// Terminator Instructions.
3472//===----------------------------------------------------------------------===//
3473
3474/// ParseRet - Parse a return instruction.
Chris Lattner596760d2009-12-29 21:25:40 +00003475/// ::= 'ret' void (',' !dbg, !1)*
3476/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
Chris Lattner33de4272011-06-17 06:49:41 +00003477bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003478 PerFunctionState &PFS) {
3479 SMLoc TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00003480 Type *Ty = nullptr;
Chris Lattnerf880ca22009-03-09 04:49:14 +00003481 if (ParseType(Ty, true /*void allowed*/)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003482
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003483 Type *ResType = PFS.getFunction().getReturnType();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003484
Chris Lattnerfdd87902009-10-05 05:54:46 +00003485 if (Ty->isVoidTy()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003486 if (!ResType->isVoidTy())
3487 return Error(TypeLoc, "value doesn't match function result type '" +
3488 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003489
Owen Anderson55f1c092009-08-13 21:58:54 +00003490 Inst = ReturnInst::Create(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003491 return false;
3492 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003493
Chris Lattnerac161bf2009-01-02 07:01:27 +00003494 Value *RV;
3495 if (ParseValue(Ty, RV, PFS)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003496
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003497 if (ResType != RV->getType())
3498 return Error(TypeLoc, "value doesn't match function result type '" +
3499 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003500
Owen Anderson55f1c092009-08-13 21:58:54 +00003501 Inst = ReturnInst::Create(Context, RV);
Chris Lattner33de4272011-06-17 06:49:41 +00003502 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003503}
3504
3505
3506/// ParseBr
3507/// ::= 'br' TypeAndValue
3508/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3509bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
3510 LocTy Loc, Loc2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003511 Value *Op0;
3512 BasicBlock *Op1, *Op2;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003513 if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003514
Chris Lattnerac161bf2009-01-02 07:01:27 +00003515 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
3516 Inst = BranchInst::Create(BB);
3517 return false;
3518 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003519
Owen Anderson55f1c092009-08-13 21:58:54 +00003520 if (Op0->getType() != Type::getInt1Ty(Context))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003521 return Error(Loc, "branch condition must have 'i1' type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003522
Chris Lattnerac161bf2009-01-02 07:01:27 +00003523 if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003524 ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003525 ParseToken(lltok::comma, "expected ',' after true destination") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003526 ParseTypeAndBasicBlock(Op2, Loc2, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003527 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003528
Chris Lattner3ed871f2009-10-27 19:13:16 +00003529 Inst = BranchInst::Create(Op1, Op2, Op0);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003530 return false;
3531}
3532
3533/// ParseSwitch
3534/// Instruction
3535/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
3536/// JumpTable
3537/// ::= (TypeAndValue ',' TypeAndValue)*
3538bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
3539 LocTy CondLoc, BBLoc;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003540 Value *Cond;
3541 BasicBlock *DefaultBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003542 if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
3543 ParseToken(lltok::comma, "expected ',' after switch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003544 ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003545 ParseToken(lltok::lsquare, "expected '[' with switch table"))
3546 return true;
3547
Duncan Sands19d0b472010-02-16 11:11:14 +00003548 if (!Cond->getType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003549 return Error(CondLoc, "switch condition must have integer type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003550
Chris Lattnerac161bf2009-01-02 07:01:27 +00003551 // Parse the jump table pairs.
3552 SmallPtrSet<Value*, 32> SeenCases;
3553 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
3554 while (Lex.getKind() != lltok::rsquare) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003555 Value *Constant;
3556 BasicBlock *DestBB;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003557
Chris Lattnerac161bf2009-01-02 07:01:27 +00003558 if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
3559 ParseToken(lltok::comma, "expected ',' after case value") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003560 ParseTypeAndBasicBlock(DestBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003561 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003562
Chris Lattnerac161bf2009-01-02 07:01:27 +00003563 if (!SeenCases.insert(Constant))
3564 return Error(CondLoc, "duplicate case value in switch");
3565 if (!isa<ConstantInt>(Constant))
3566 return Error(CondLoc, "case value is not a constant integer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003567
Chris Lattner3ed871f2009-10-27 19:13:16 +00003568 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003569 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003570
Chris Lattnerac161bf2009-01-02 07:01:27 +00003571 Lex.Lex(); // Eat the ']'.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003572
Chris Lattner3ed871f2009-10-27 19:13:16 +00003573 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00003574 for (unsigned i = 0, e = Table.size(); i != e; ++i)
3575 SI->addCase(Table[i].first, Table[i].second);
3576 Inst = SI;
3577 return false;
3578}
3579
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003580/// ParseIndirectBr
Chris Lattner3ed871f2009-10-27 19:13:16 +00003581/// Instruction
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003582/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
3583bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003584 LocTy AddrLoc;
3585 Value *Address;
3586 if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003587 ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
3588 ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
Chris Lattner3ed871f2009-10-27 19:13:16 +00003589 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003590
Duncan Sands19d0b472010-02-16 11:11:14 +00003591 if (!Address->getType()->isPointerTy())
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003592 return Error(AddrLoc, "indirectbr address must have pointer type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003593
Chris Lattner3ed871f2009-10-27 19:13:16 +00003594 // Parse the destination list.
3595 SmallVector<BasicBlock*, 16> DestList;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003596
Chris Lattner3ed871f2009-10-27 19:13:16 +00003597 if (Lex.getKind() != lltok::rsquare) {
3598 BasicBlock *DestBB;
3599 if (ParseTypeAndBasicBlock(DestBB, PFS))
3600 return true;
3601 DestList.push_back(DestBB);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003602
Chris Lattner3ed871f2009-10-27 19:13:16 +00003603 while (EatIfPresent(lltok::comma)) {
3604 if (ParseTypeAndBasicBlock(DestBB, PFS))
3605 return true;
3606 DestList.push_back(DestBB);
3607 }
3608 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003609
Chris Lattner3ed871f2009-10-27 19:13:16 +00003610 if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
3611 return true;
3612
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003613 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
Chris Lattner3ed871f2009-10-27 19:13:16 +00003614 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
3615 IBI->addDestination(DestList[i]);
3616 Inst = IBI;
3617 return false;
3618}
3619
3620
Chris Lattnerac161bf2009-01-02 07:01:27 +00003621/// ParseInvoke
3622/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
3623/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
3624bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
3625 LocTy CallLoc = Lex.getLoc();
Bill Wendling50d27842012-10-15 20:35:56 +00003626 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003627 std::vector<unsigned> FwdRefAttrGrps;
Bill Wendling09bd1f72013-02-22 00:12:35 +00003628 LocTy NoBuiltinLoc;
Sandeep Patel68c5f472009-09-02 08:44:58 +00003629 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00003630 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003631 LocTy RetTypeLoc;
3632 ValID CalleeID;
3633 SmallVector<ParamInfo, 16> ArgList;
3634
Chris Lattner3ed871f2009-10-27 19:13:16 +00003635 BasicBlock *NormalBB, *UnwindBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003636 if (ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00003637 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00003638 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003639 ParseValID(CalleeID) ||
3640 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003641 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
3642 NoBuiltinLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003643 ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003644 ParseTypeAndBasicBlock(NormalBB, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003645 ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003646 ParseTypeAndBasicBlock(UnwindBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003647 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003648
Chris Lattnerac161bf2009-01-02 07:01:27 +00003649 // If RetType is a non-function pointer type, then this is the short syntax
3650 // for the call, which means that RetType is just the return type. Infer the
3651 // rest of the function argument types from the arguments that are present.
Craig Topper2617dcc2014-04-15 06:32:26 +00003652 PointerType *PFTy = nullptr;
3653 FunctionType *Ty = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003654 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3655 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3656 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00003657 std::vector<Type*> ParamTypes;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003658 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3659 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003660
Chris Lattnerac161bf2009-01-02 07:01:27 +00003661 if (!FunctionType::isValidReturnType(RetType))
3662 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003663
Owen Anderson4056ca92009-07-29 22:17:13 +00003664 Ty = FunctionType::get(RetType, ParamTypes, false);
3665 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003666 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003667
Chris Lattnerac161bf2009-01-02 07:01:27 +00003668 // Look up the callee.
3669 Value *Callee;
Victor Hernandez9d75c962010-01-11 22:31:58 +00003670 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003671
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003672 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00003673 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003674 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003675 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3676 AttributeSet::ReturnIndex,
3677 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003678
Chris Lattnerac161bf2009-01-02 07:01:27 +00003679 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003680
Chris Lattnerac161bf2009-01-02 07:01:27 +00003681 // Loop through FunctionType's arguments and ensure they are specified
3682 // correctly. Also, gather any parameter attributes.
3683 FunctionType::param_iterator I = Ty->param_begin();
3684 FunctionType::param_iterator E = Ty->param_end();
3685 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003686 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003687 if (I != E) {
3688 ExpectedTy = *I++;
3689 } else if (!Ty->isVarArg()) {
3690 return Error(ArgList[i].Loc, "too many arguments specified");
3691 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003692
Chris Lattnerac161bf2009-01-02 07:01:27 +00003693 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3694 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003695 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003696 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00003697 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3698 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00003699 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3700 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003701 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003702
Chris Lattnerac161bf2009-01-02 07:01:27 +00003703 if (I != E)
3704 return Error(CallLoc, "not enough parameters specified for call");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003705
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003706 if (FnAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003707 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3708 AttributeSet::FunctionIndex,
3709 FnAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003710
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003711 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00003712 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003713
Jay Foad5bd375a2011-07-15 08:37:34 +00003714 InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB, Args);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003715 II->setCallingConv(CC);
3716 II->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00003717 ForwardRefAttrGroups[II] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003718 Inst = II;
3719 return false;
3720}
3721
Bill Wendlingf891bf82011-07-31 06:30:59 +00003722/// ParseResume
3723/// ::= 'resume' TypeAndValue
3724bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) {
3725 Value *Exn; LocTy ExnLoc;
Bill Wendlingf891bf82011-07-31 06:30:59 +00003726 if (ParseTypeAndValue(Exn, ExnLoc, PFS))
3727 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003728
Bill Wendlingf891bf82011-07-31 06:30:59 +00003729 ResumeInst *RI = ResumeInst::Create(Exn);
3730 Inst = RI;
3731 return false;
3732}
Chris Lattnerac161bf2009-01-02 07:01:27 +00003733
3734//===----------------------------------------------------------------------===//
3735// Binary Operators.
3736//===----------------------------------------------------------------------===//
3737
3738/// ParseArithmetic
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003739/// ::= ArithmeticOps TypeAndValue ',' Value
3740///
3741/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1,
3742/// then any integer operand is allowed, if it is 2, any fp operand is allowed.
Chris Lattnerac161bf2009-01-02 07:01:27 +00003743bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003744 unsigned Opc, unsigned OperandType) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003745 LocTy Loc; Value *LHS, *RHS;
3746 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3747 ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
3748 ParseValue(LHS->getType(), RHS, PFS))
3749 return true;
3750
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003751 bool Valid;
3752 switch (OperandType) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003753 default: llvm_unreachable("Unknown operand type!");
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003754 case 0: // int or FP.
Duncan Sands9dff9be2010-02-15 16:12:20 +00003755 Valid = LHS->getType()->isIntOrIntVectorTy() ||
3756 LHS->getType()->isFPOrFPVectorTy();
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003757 break;
Duncan Sands9dff9be2010-02-15 16:12:20 +00003758 case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
3759 case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003760 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003761
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003762 if (!Valid)
3763 return Error(Loc, "invalid operand type for instruction");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003764
Chris Lattnerac161bf2009-01-02 07:01:27 +00003765 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3766 return false;
3767}
3768
3769/// ParseLogical
3770/// ::= ArithmeticOps TypeAndValue ',' Value {
3771bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
3772 unsigned Opc) {
3773 LocTy Loc; Value *LHS, *RHS;
3774 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3775 ParseToken(lltok::comma, "expected ',' in logical operation") ||
3776 ParseValue(LHS->getType(), RHS, PFS))
3777 return true;
3778
Duncan Sands9dff9be2010-02-15 16:12:20 +00003779 if (!LHS->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003780 return Error(Loc,"instruction requires integer or integer vector operands");
3781
3782 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3783 return false;
3784}
3785
3786
3787/// ParseCompare
3788/// ::= 'icmp' IPredicates TypeAndValue ',' Value
3789/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00003790bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
3791 unsigned Opc) {
3792 // Parse the integer/fp comparison predicate.
3793 LocTy Loc;
3794 unsigned Pred;
3795 Value *LHS, *RHS;
3796 if (ParseCmpPredicate(Pred, Opc) ||
3797 ParseTypeAndValue(LHS, Loc, PFS) ||
3798 ParseToken(lltok::comma, "expected ',' after compare value") ||
3799 ParseValue(LHS->getType(), RHS, PFS))
3800 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003801
Chris Lattnerac161bf2009-01-02 07:01:27 +00003802 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00003803 if (!LHS->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003804 return Error(Loc, "fcmp requires floating point operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003805 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003806 } else {
3807 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00003808 if (!LHS->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00003809 !LHS->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003810 return Error(Loc, "icmp requires integer operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003811 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003812 }
3813 return false;
3814}
3815
3816//===----------------------------------------------------------------------===//
3817// Other Instructions.
3818//===----------------------------------------------------------------------===//
3819
3820
3821/// ParseCast
3822/// ::= CastOpc TypeAndValue 'to' Type
3823bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
3824 unsigned Opc) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003825 LocTy Loc;
3826 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00003827 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003828 if (ParseTypeAndValue(Op, Loc, PFS) ||
3829 ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
3830 ParseType(DestTy))
3831 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003832
Chris Lattner89d856e2009-03-01 00:53:13 +00003833 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
3834 CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003835 return Error(Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003836 getTypeString(Op->getType()) + "' to '" +
3837 getTypeString(DestTy) + "'");
Chris Lattner89d856e2009-03-01 00:53:13 +00003838 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003839 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
3840 return false;
3841}
3842
3843/// ParseSelect
3844/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3845bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
3846 LocTy Loc;
3847 Value *Op0, *Op1, *Op2;
3848 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3849 ParseToken(lltok::comma, "expected ',' after select condition") ||
3850 ParseTypeAndValue(Op1, PFS) ||
3851 ParseToken(lltok::comma, "expected ',' after select value") ||
3852 ParseTypeAndValue(Op2, PFS))
3853 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003854
Chris Lattnerac161bf2009-01-02 07:01:27 +00003855 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
3856 return Error(Loc, Reason);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003857
Chris Lattnerac161bf2009-01-02 07:01:27 +00003858 Inst = SelectInst::Create(Op0, Op1, Op2);
3859 return false;
3860}
3861
Chris Lattnerb55ab542009-01-05 08:18:44 +00003862/// ParseVA_Arg
3863/// ::= 'va_arg' TypeAndValue ',' Type
3864bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003865 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00003866 Type *EltTy = nullptr;
Chris Lattnerb55ab542009-01-05 08:18:44 +00003867 LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003868 if (ParseTypeAndValue(Op, PFS) ||
3869 ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
Chris Lattnerb55ab542009-01-05 08:18:44 +00003870 ParseType(EltTy, TypeLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003871 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003872
Chris Lattnerb55ab542009-01-05 08:18:44 +00003873 if (!EltTy->isFirstClassType())
3874 return Error(TypeLoc, "va_arg requires operand with first class type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003875
3876 Inst = new VAArgInst(Op, EltTy);
3877 return false;
3878}
3879
3880/// ParseExtractElement
3881/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
3882bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
3883 LocTy Loc;
3884 Value *Op0, *Op1;
3885 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3886 ParseToken(lltok::comma, "expected ',' after extract value") ||
3887 ParseTypeAndValue(Op1, PFS))
3888 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003889
Chris Lattnerac161bf2009-01-02 07:01:27 +00003890 if (!ExtractElementInst::isValidOperands(Op0, Op1))
3891 return Error(Loc, "invalid extractelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003892
Eric Christopherc9742252009-07-25 02:28:41 +00003893 Inst = ExtractElementInst::Create(Op0, Op1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003894 return false;
3895}
3896
3897/// ParseInsertElement
3898/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3899bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
3900 LocTy Loc;
3901 Value *Op0, *Op1, *Op2;
3902 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3903 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3904 ParseTypeAndValue(Op1, PFS) ||
3905 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3906 ParseTypeAndValue(Op2, PFS))
3907 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003908
Chris Lattnerac161bf2009-01-02 07:01:27 +00003909 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
Eric Christopherfef8db62009-07-23 01:01:32 +00003910 return Error(Loc, "invalid insertelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003911
Chris Lattnerac161bf2009-01-02 07:01:27 +00003912 Inst = InsertElementInst::Create(Op0, Op1, Op2);
3913 return false;
3914}
3915
3916/// ParseShuffleVector
3917/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3918bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
3919 LocTy Loc;
3920 Value *Op0, *Op1, *Op2;
3921 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3922 ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
3923 ParseTypeAndValue(Op1, PFS) ||
3924 ParseToken(lltok::comma, "expected ',' after shuffle value") ||
3925 ParseTypeAndValue(Op2, PFS))
3926 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003927
Chris Lattnerac161bf2009-01-02 07:01:27 +00003928 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
Pete Cooperc1a6f982012-02-01 23:43:12 +00003929 return Error(Loc, "invalid shufflevector operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003930
Chris Lattnerac161bf2009-01-02 07:01:27 +00003931 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
3932 return false;
3933}
3934
3935/// ParsePHI
Chris Lattnerc05471e2009-10-18 05:27:44 +00003936/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
Chris Lattnerf4f03422009-12-30 05:27:33 +00003937int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003938 Type *Ty = nullptr; LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003939 Value *Op0, *Op1;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003940
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003941 if (ParseType(Ty, TypeLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003942 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
3943 ParseValue(Ty, Op0, PFS) ||
3944 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00003945 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003946 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3947 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003948
Chris Lattnerf4f03422009-12-30 05:27:33 +00003949 bool AteExtraComma = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003950 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
3951 while (1) {
3952 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003953
Chris Lattner3822f632009-01-02 08:05:26 +00003954 if (!EatIfPresent(lltok::comma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003955 break;
3956
Chris Lattnerf4f03422009-12-30 05:27:33 +00003957 if (Lex.getKind() == lltok::MetadataVar) {
3958 AteExtraComma = true;
Devang Patel8f842d32009-10-16 18:45:49 +00003959 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00003960 }
Devang Patel8f842d32009-10-16 18:45:49 +00003961
Chris Lattner3822f632009-01-02 08:05:26 +00003962 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003963 ParseValue(Ty, Op0, PFS) ||
3964 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00003965 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003966 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3967 return true;
3968 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003969
Chris Lattnerac161bf2009-01-02 07:01:27 +00003970 if (!Ty->isFirstClassType())
3971 return Error(TypeLoc, "phi node must have first class type");
3972
Jay Foad52131342011-03-30 11:28:46 +00003973 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00003974 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
3975 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
3976 Inst = PN;
Chris Lattnerf4f03422009-12-30 05:27:33 +00003977 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003978}
3979
Bill Wendlingfae14752011-08-12 20:24:12 +00003980/// ParseLandingPad
3981/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
3982/// Clause
3983/// ::= 'catch' TypeAndValue
3984/// ::= 'filter'
3985/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
3986bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003987 Type *Ty = nullptr; LocTy TyLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00003988 Value *PersFn; LocTy PersFnLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00003989
3990 if (ParseType(Ty, TyLoc) ||
3991 ParseToken(lltok::kw_personality, "expected 'personality'") ||
3992 ParseTypeAndValue(PersFn, PersFnLoc, PFS))
3993 return true;
3994
3995 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, 0);
3996 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
3997
3998 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
3999 LandingPadInst::ClauseType CT;
4000 if (EatIfPresent(lltok::kw_catch))
4001 CT = LandingPadInst::Catch;
4002 else if (EatIfPresent(lltok::kw_filter))
4003 CT = LandingPadInst::Filter;
4004 else
4005 return TokError("expected 'catch' or 'filter' clause type");
4006
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +00004007 Value *V;
4008 LocTy VLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00004009 if (ParseTypeAndValue(V, VLoc, PFS)) {
4010 delete LP;
4011 return true;
4012 }
4013
Bill Wendlinga52aa3c2011-08-12 20:52:25 +00004014 // A 'catch' type expects a non-array constant. A filter clause expects an
4015 // array constant.
4016 if (CT == LandingPadInst::Catch) {
4017 if (isa<ArrayType>(V->getType()))
4018 Error(VLoc, "'catch' clause has an invalid type");
4019 } else {
4020 if (!isa<ArrayType>(V->getType()))
4021 Error(VLoc, "'filter' clause has an invalid type");
4022 }
4023
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +00004024 LP->addClause(cast<Constant>(V));
Bill Wendlingfae14752011-08-12 20:24:12 +00004025 }
4026
4027 Inst = LP;
4028 return false;
4029}
4030
Chris Lattnerac161bf2009-01-02 07:01:27 +00004031/// ParseCall
Reid Kleckner5772b772014-04-24 20:14:34 +00004032/// ::= 'call' OptionalCallingConv OptionalAttrs Type Value
4033/// ParameterList OptionalAttrs
4034/// ::= 'tail' 'call' OptionalCallingConv OptionalAttrs Type Value
4035/// ParameterList OptionalAttrs
4036/// ::= 'musttail' 'call' OptionalCallingConv OptionalAttrs Type Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00004037/// ParameterList OptionalAttrs
4038bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
Reid Kleckner5772b772014-04-24 20:14:34 +00004039 CallInst::TailCallKind TCK) {
Bill Wendling50d27842012-10-15 20:35:56 +00004040 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00004041 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00004042 LocTy BuiltinLoc;
Sandeep Patel68c5f472009-09-02 08:44:58 +00004043 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00004044 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004045 LocTy RetTypeLoc;
4046 ValID CalleeID;
4047 SmallVector<ParamInfo, 16> ArgList;
4048 LocTy CallLoc = Lex.getLoc();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004049
Reid Kleckner5772b772014-04-24 20:14:34 +00004050 if ((TCK != CallInst::TCK_None &&
4051 ParseToken(lltok::kw_call, "expected 'tail call'")) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004052 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00004053 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00004054 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004055 ParseValID(CalleeID) ||
4056 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00004057 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00004058 BuiltinLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004059 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004060
Chris Lattnerac161bf2009-01-02 07:01:27 +00004061 // If RetType is a non-function pointer type, then this is the short syntax
4062 // for the call, which means that RetType is just the return type. Infer the
4063 // rest of the function argument types from the arguments that are present.
Craig Topper2617dcc2014-04-15 06:32:26 +00004064 PointerType *PFTy = nullptr;
4065 FunctionType *Ty = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004066 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
4067 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
4068 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00004069 std::vector<Type*> ParamTypes;
Eli Friedman6cf51412010-07-24 23:06:59 +00004070 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
4071 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004072
Chris Lattnerac161bf2009-01-02 07:01:27 +00004073 if (!FunctionType::isValidReturnType(RetType))
4074 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004075
Owen Anderson4056ca92009-07-29 22:17:13 +00004076 Ty = FunctionType::get(RetType, ParamTypes, false);
4077 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004078 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004079
Chris Lattnerac161bf2009-01-02 07:01:27 +00004080 // Look up the callee.
4081 Value *Callee;
Victor Hernandez9d75c962010-01-11 22:31:58 +00004082 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004083
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004084 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00004085 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004086 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004087 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4088 AttributeSet::ReturnIndex,
4089 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004090
Chris Lattnerac161bf2009-01-02 07:01:27 +00004091 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004092
Chris Lattnerac161bf2009-01-02 07:01:27 +00004093 // Loop through FunctionType's arguments and ensure they are specified
4094 // correctly. Also, gather any parameter attributes.
4095 FunctionType::param_iterator I = Ty->param_begin();
4096 FunctionType::param_iterator E = Ty->param_end();
4097 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004098 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004099 if (I != E) {
4100 ExpectedTy = *I++;
4101 } else if (!Ty->isVarArg()) {
4102 return Error(ArgList[i].Loc, "too many arguments specified");
4103 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004104
Chris Lattnerac161bf2009-01-02 07:01:27 +00004105 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
4106 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00004107 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004108 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00004109 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
4110 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00004111 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
4112 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004113 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004114
Chris Lattnerac161bf2009-01-02 07:01:27 +00004115 if (I != E)
4116 return Error(CallLoc, "not enough parameters specified for call");
4117
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004118 if (FnAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004119 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4120 AttributeSet::FunctionIndex,
4121 FnAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00004122
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004123 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00004124 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004125
Jay Foad5bd375a2011-07-15 08:37:34 +00004126 CallInst *CI = CallInst::Create(Callee, Args);
Reid Kleckner5772b772014-04-24 20:14:34 +00004127 CI->setTailCallKind(TCK);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004128 CI->setCallingConv(CC);
4129 CI->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00004130 ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004131 Inst = CI;
4132 return false;
4133}
4134
4135//===----------------------------------------------------------------------===//
4136// Memory Instructions.
4137//===----------------------------------------------------------------------===//
4138
4139/// ParseAlloc
David Majnemerc4ab61c2014-03-09 06:41:58 +00004140/// ::= 'alloca' 'inalloca'? Type (',' TypeAndValue)? (',' 'align' i32)?
Chris Lattner78103722011-06-17 03:16:47 +00004141int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004142 Value *Size = nullptr;
Chris Lattner200e0752009-07-02 23:08:13 +00004143 LocTy SizeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004144 unsigned Alignment = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00004145 Type *Ty = nullptr;
David Majnemerc4ab61c2014-03-09 06:41:58 +00004146
4147 bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
4148
Chris Lattner3822f632009-01-02 08:05:26 +00004149 if (ParseType(Ty)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004150
Chris Lattnerb2f39502009-12-30 05:44:30 +00004151 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00004152 if (EatIfPresent(lltok::comma)) {
David Majnemerc4ab61c2014-03-09 06:41:58 +00004153 if (Lex.getKind() == lltok::kw_align) {
4154 if (ParseOptionalAlignment(Alignment)) return true;
4155 } else if (Lex.getKind() == lltok::MetadataVar) {
4156 AteExtraComma = true;
4157 } else {
4158 if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
4159 ParseOptionalCommaAlign(Alignment, AteExtraComma))
4160 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004161 }
4162 }
4163
Dan Gohman2140a742010-05-28 01:14:11 +00004164 if (Size && !Size->getType()->isIntegerTy())
4165 return Error(SizeLoc, "element count must have integer type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004166
Reid Kleckner436c42e2014-01-17 23:58:17 +00004167 AllocaInst *AI = new AllocaInst(Ty, Size, Alignment);
4168 AI->setUsedWithInAlloca(IsInAlloca);
4169 Inst = AI;
Chris Lattner78103722011-06-17 03:16:47 +00004170 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004171}
4172
4173/// ParseLoad
Eli Friedman02e737b2011-08-12 22:50:01 +00004174/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004175/// ::= 'load' 'atomic' 'volatile'? TypeAndValue
Eli Friedman02e737b2011-08-12 22:50:01 +00004176/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00004177int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004178 Value *Val; LocTy Loc;
Devang Patelea8a4b92009-09-17 23:04:48 +00004179 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00004180 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004181 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00004182 AtomicOrdering Ordering = NotAtomic;
4183 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004184
4185 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004186 isAtomic = true;
4187 Lex.Lex();
4188 }
4189
Chris Lattnerbc639292011-11-27 06:56:53 +00004190 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004191 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004192 isVolatile = true;
4193 Lex.Lex();
4194 }
4195
Chris Lattnerb2f39502009-12-30 05:44:30 +00004196 if (ParseTypeAndValue(Val, Loc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00004197 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004198 ParseOptionalCommaAlign(Alignment, AteExtraComma))
4199 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004200
Duncan Sands19d0b472010-02-16 11:11:14 +00004201 if (!Val->getType()->isPointerTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004202 !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType())
4203 return Error(Loc, "load operand must be a pointer to a first class type");
Eli Friedman59b66882011-08-09 23:02:53 +00004204 if (isAtomic && !Alignment)
4205 return Error(Loc, "atomic load must have explicit non-zero alignment");
4206 if (Ordering == Release || Ordering == AcquireRelease)
4207 return Error(Loc, "atomic load cannot use Release ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004208
Eli Friedman59b66882011-08-09 23:02:53 +00004209 Inst = new LoadInst(Val, "", isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00004210 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004211}
4212
4213/// ParseStore
Eli Friedman02e737b2011-08-12 22:50:01 +00004214
4215/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
4216/// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
Eli Friedman59b66882011-08-09 23:02:53 +00004217/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00004218int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004219 Value *Val, *Ptr; LocTy Loc, PtrLoc;
Devang Patelea8a4b92009-09-17 23:04:48 +00004220 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00004221 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004222 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00004223 AtomicOrdering Ordering = NotAtomic;
4224 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004225
4226 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004227 isAtomic = true;
4228 Lex.Lex();
4229 }
4230
Chris Lattnerbc639292011-11-27 06:56:53 +00004231 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004232 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004233 isVolatile = true;
4234 Lex.Lex();
4235 }
4236
Chris Lattnerac161bf2009-01-02 07:01:27 +00004237 if (ParseTypeAndValue(Val, Loc, PFS) ||
4238 ParseToken(lltok::comma, "expected ',' after store operand") ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004239 ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00004240 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004241 ParseOptionalCommaAlign(Alignment, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004242 return true;
Devang Patelea8a4b92009-09-17 23:04:48 +00004243
Duncan Sands19d0b472010-02-16 11:11:14 +00004244 if (!Ptr->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004245 return Error(PtrLoc, "store operand must be a pointer");
4246 if (!Val->getType()->isFirstClassType())
4247 return Error(Loc, "store operand must be a first class value");
4248 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4249 return Error(Loc, "stored value and pointer type do not match");
Eli Friedman59b66882011-08-09 23:02:53 +00004250 if (isAtomic && !Alignment)
4251 return Error(Loc, "atomic store must have explicit non-zero alignment");
4252 if (Ordering == Acquire || Ordering == AcquireRelease)
4253 return Error(Loc, "atomic store cannot use Acquire ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004254
Eli Friedman59b66882011-08-09 23:02:53 +00004255 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00004256 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004257}
4258
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004259/// ParseCmpXchg
Tim Northover420a2162014-06-13 14:24:07 +00004260/// ::= 'cmpxchg' 'weak'? 'volatile'? TypeAndValue ',' TypeAndValue ','
4261/// TypeAndValue 'singlethread'? AtomicOrdering AtomicOrdering
Eli Friedman02e737b2011-08-12 22:50:01 +00004262int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004263 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
4264 bool AteExtraComma = false;
Tim Northovere94a5182014-03-11 10:48:52 +00004265 AtomicOrdering SuccessOrdering = NotAtomic;
4266 AtomicOrdering FailureOrdering = NotAtomic;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004267 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004268 bool isVolatile = false;
Tim Northover420a2162014-06-13 14:24:07 +00004269 bool isWeak = false;
4270
4271 if (EatIfPresent(lltok::kw_weak))
4272 isWeak = true;
Eli Friedman02e737b2011-08-12 22:50:01 +00004273
4274 if (EatIfPresent(lltok::kw_volatile))
4275 isVolatile = true;
4276
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004277 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4278 ParseToken(lltok::comma, "expected ',' after cmpxchg address") ||
4279 ParseTypeAndValue(Cmp, CmpLoc, PFS) ||
4280 ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
4281 ParseTypeAndValue(New, NewLoc, PFS) ||
Tim Northovere94a5182014-03-11 10:48:52 +00004282 ParseScopeAndOrdering(true /*Always atomic*/, Scope, SuccessOrdering) ||
4283 ParseOrdering(FailureOrdering))
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004284 return true;
4285
Tim Northovere94a5182014-03-11 10:48:52 +00004286 if (SuccessOrdering == Unordered || FailureOrdering == Unordered)
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004287 return TokError("cmpxchg cannot be unordered");
Tim Northovere94a5182014-03-11 10:48:52 +00004288 if (SuccessOrdering < FailureOrdering)
4289 return TokError("cmpxchg must be at least as ordered on success as failure");
4290 if (FailureOrdering == Release || FailureOrdering == AcquireRelease)
4291 return TokError("cmpxchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004292 if (!Ptr->getType()->isPointerTy())
4293 return Error(PtrLoc, "cmpxchg operand must be a pointer");
4294 if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType())
4295 return Error(CmpLoc, "compare value and pointer type do not match");
4296 if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType())
4297 return Error(NewLoc, "new value and pointer type do not match");
4298 if (!New->getType()->isIntegerTy())
4299 return Error(NewLoc, "cmpxchg operand must be an integer");
4300 unsigned Size = New->getType()->getPrimitiveSizeInBits();
4301 if (Size < 8 || (Size & (Size - 1)))
4302 return Error(NewLoc, "cmpxchg operand must be power-of-two byte-sized"
4303 " integer");
4304
Tim Northover420a2162014-06-13 14:24:07 +00004305 AtomicCmpXchgInst *CXI = new AtomicCmpXchgInst(
4306 Ptr, Cmp, New, SuccessOrdering, FailureOrdering, Scope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004307 CXI->setVolatile(isVolatile);
Tim Northover420a2162014-06-13 14:24:07 +00004308 CXI->setWeak(isWeak);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004309 Inst = CXI;
4310 return AteExtraComma ? InstExtraComma : InstNormal;
4311}
4312
4313/// ParseAtomicRMW
Eli Friedman02e737b2011-08-12 22:50:01 +00004314/// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
4315/// 'singlethread'? AtomicOrdering
4316int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004317 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
4318 bool AteExtraComma = false;
4319 AtomicOrdering Ordering = NotAtomic;
4320 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004321 bool isVolatile = false;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004322 AtomicRMWInst::BinOp Operation;
Eli Friedman02e737b2011-08-12 22:50:01 +00004323
4324 if (EatIfPresent(lltok::kw_volatile))
4325 isVolatile = true;
4326
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004327 switch (Lex.getKind()) {
4328 default: return TokError("expected binary operation in atomicrmw");
4329 case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break;
4330 case lltok::kw_add: Operation = AtomicRMWInst::Add; break;
4331 case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break;
4332 case lltok::kw_and: Operation = AtomicRMWInst::And; break;
4333 case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break;
4334 case lltok::kw_or: Operation = AtomicRMWInst::Or; break;
4335 case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break;
4336 case lltok::kw_max: Operation = AtomicRMWInst::Max; break;
4337 case lltok::kw_min: Operation = AtomicRMWInst::Min; break;
4338 case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break;
4339 case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break;
4340 }
4341 Lex.Lex(); // Eat the operation.
4342
4343 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4344 ParseToken(lltok::comma, "expected ',' after atomicrmw address") ||
4345 ParseTypeAndValue(Val, ValLoc, PFS) ||
4346 ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4347 return true;
4348
4349 if (Ordering == Unordered)
4350 return TokError("atomicrmw cannot be unordered");
4351 if (!Ptr->getType()->isPointerTy())
4352 return Error(PtrLoc, "atomicrmw operand must be a pointer");
4353 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4354 return Error(ValLoc, "atomicrmw value and pointer type do not match");
4355 if (!Val->getType()->isIntegerTy())
4356 return Error(ValLoc, "atomicrmw operand must be an integer");
4357 unsigned Size = Val->getType()->getPrimitiveSizeInBits();
4358 if (Size < 8 || (Size & (Size - 1)))
4359 return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
4360 " integer");
4361
4362 AtomicRMWInst *RMWI =
4363 new AtomicRMWInst(Operation, Ptr, Val, Ordering, Scope);
4364 RMWI->setVolatile(isVolatile);
4365 Inst = RMWI;
4366 return AteExtraComma ? InstExtraComma : InstNormal;
4367}
4368
Eli Friedmanfee02c62011-07-25 23:16:38 +00004369/// ParseFence
4370/// ::= 'fence' 'singlethread'? AtomicOrdering
4371int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) {
4372 AtomicOrdering Ordering = NotAtomic;
4373 SynchronizationScope Scope = CrossThread;
4374 if (ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4375 return true;
4376
4377 if (Ordering == Unordered)
4378 return TokError("fence cannot be unordered");
4379 if (Ordering == Monotonic)
4380 return TokError("fence cannot be monotonic");
4381
4382 Inst = new FenceInst(Context, Ordering, Scope);
4383 return InstNormal;
4384}
4385
Chris Lattnerac161bf2009-01-02 07:01:27 +00004386/// ParseGetElementPtr
Dan Gohman1639c392009-07-27 21:53:46 +00004387/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
Chris Lattnerf4f03422009-12-30 05:27:33 +00004388int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004389 Value *Ptr = nullptr;
4390 Value *Val = nullptr;
Nadav Rotem3924cb02011-12-05 06:29:09 +00004391 LocTy Loc, EltLoc;
Dan Gohman1639c392009-07-27 21:53:46 +00004392
Dan Gohman16cbbe42009-07-29 15:58:36 +00004393 bool InBounds = EatIfPresent(lltok::kw_inbounds);
Dan Gohman1639c392009-07-27 21:53:46 +00004394
Chris Lattner3822f632009-01-02 08:05:26 +00004395 if (ParseTypeAndValue(Ptr, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004396
Eli Benderskyd9806682013-04-22 17:03:42 +00004397 Type *BaseType = Ptr->getType();
4398 PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType());
4399 if (!BasePointerType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004400 return Error(Loc, "base of getelementptr must be a pointer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004401
Chris Lattnerac161bf2009-01-02 07:01:27 +00004402 SmallVector<Value*, 16> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004403 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00004404 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf4f03422009-12-30 05:27:33 +00004405 if (Lex.getKind() == lltok::MetadataVar) {
4406 AteExtraComma = true;
Devang Patel52b17452009-10-13 18:49:55 +00004407 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004408 }
Chris Lattner3822f632009-01-02 08:05:26 +00004409 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
Nadav Rotem3924cb02011-12-05 06:29:09 +00004410 if (!Val->getType()->getScalarType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004411 return Error(EltLoc, "getelementptr index must be an integer");
Nadav Rotem3924cb02011-12-05 06:29:09 +00004412 if (Val->getType()->isVectorTy() != Ptr->getType()->isVectorTy())
4413 return Error(EltLoc, "getelementptr index type missmatch");
4414 if (Val->getType()->isVectorTy()) {
4415 unsigned ValNumEl = cast<VectorType>(Val->getType())->getNumElements();
4416 unsigned PtrNumEl = cast<VectorType>(Ptr->getType())->getNumElements();
4417 if (ValNumEl != PtrNumEl)
4418 return Error(EltLoc,
4419 "getelementptr vector index has a wrong number of elements");
4420 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004421 Indices.push_back(Val);
4422 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004423
Eli Benderskyd9806682013-04-22 17:03:42 +00004424 if (!Indices.empty() && !BasePointerType->getElementType()->isSized())
4425 return Error(Loc, "base element of getelementptr must be sized");
4426
4427 if (!GetElementPtrInst::getIndexedType(BaseType, Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004428 return Error(Loc, "invalid getelementptr indices");
Jay Foadd1b78492011-07-25 09:48:08 +00004429 Inst = GetElementPtrInst::Create(Ptr, Indices);
Dan Gohman1639c392009-07-27 21:53:46 +00004430 if (InBounds)
Dan Gohman1b849082009-09-07 23:54:19 +00004431 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004432 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004433}
4434
4435/// ParseExtractValue
4436/// ::= 'extractvalue' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00004437int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004438 Value *Val; LocTy Loc;
4439 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004440 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004441 if (ParseTypeAndValue(Val, Loc, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00004442 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004443 return true;
4444
Chris Lattner392be582010-02-12 20:49:41 +00004445 if (!Val->getType()->isAggregateType())
4446 return Error(Loc, "extractvalue operand must be aggregate type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004447
Jay Foad57aa6362011-07-13 10:26:04 +00004448 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004449 return Error(Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00004450 Inst = ExtractValueInst::Create(Val, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004451 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004452}
4453
4454/// ParseInsertValue
4455/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00004456int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004457 Value *Val0, *Val1; LocTy Loc0, Loc1;
4458 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004459 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004460 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
4461 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
4462 ParseTypeAndValue(Val1, Loc1, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00004463 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004464 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004465
Chris Lattner392be582010-02-12 20:49:41 +00004466 if (!Val0->getType()->isAggregateType())
4467 return Error(Loc0, "insertvalue operand must be aggregate type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004468
Jay Foad57aa6362011-07-13 10:26:04 +00004469 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004470 return Error(Loc0, "invalid indices for insertvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00004471 Inst = InsertValueInst::Create(Val0, Val1, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004472 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004473}
Nick Lewycky49f89192009-04-04 07:22:01 +00004474
4475//===----------------------------------------------------------------------===//
4476// Embedded metadata.
4477//===----------------------------------------------------------------------===//
4478
4479/// ParseMDNodeVector
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004480/// ::= Element (',' Element)*
4481/// Element
4482/// ::= 'null' | TypeAndValue
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00004483bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts,
Victor Hernandezb8fd1522010-01-10 07:14:18 +00004484 PerFunctionState *PFS) {
Dan Gohman1e0213a2010-07-13 19:33:27 +00004485 // Check for an empty list.
4486 if (Lex.getKind() == lltok::rbrace)
4487 return false;
4488
Nick Lewycky49f89192009-04-04 07:22:01 +00004489 do {
Chris Lattnera01ddfc2009-12-30 04:42:57 +00004490 // Null is a special case since it is typeless.
4491 if (EatIfPresent(lltok::kw_null)) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004492 Elts.push_back(nullptr);
Chris Lattnera01ddfc2009-12-30 04:42:57 +00004493 continue;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004494 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004495
Craig Topper2617dcc2014-04-15 06:32:26 +00004496 Value *V = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004497 if (ParseTypeAndValue(V, PFS)) return true;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004498 Elts.push_back(V);
Nick Lewycky49f89192009-04-04 07:22:01 +00004499 } while (EatIfPresent(lltok::comma));
4500
4501 return false;
4502}