blob: 0c188f983ff531a8d53eb627f4e01bb7d98e7eb9 [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;
Chris Lattner1797fc72009-12-29 21:53:55 +0000521 Result = MDString::get(Context, Str);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000522 return false;
523}
524
525// MDNode:
526// ::= '!' MDNodeNumber
Chris Lattner8eff0152010-04-01 05:14:45 +0000527//
528/// This version of ParseMDNodeID returns the slot number and null in the case
529/// of a forward reference.
530bool LLParser::ParseMDNodeID(MDNode *&Result, unsigned &SlotNo) {
531 // !{ ..., !42, ... }
532 if (ParseUInt32(SlotNo)) return true;
533
534 // Check existing MDNode.
Craig Topper2617dcc2014-04-15 06:32:26 +0000535 if (SlotNo < NumberedMetadata.size() && NumberedMetadata[SlotNo] != nullptr)
Chris Lattner8eff0152010-04-01 05:14:45 +0000536 Result = NumberedMetadata[SlotNo];
537 else
Craig Topper2617dcc2014-04-15 06:32:26 +0000538 Result = nullptr;
Chris Lattner8eff0152010-04-01 05:14:45 +0000539 return false;
540}
541
Chris Lattner6dac02a2009-12-30 04:15:23 +0000542bool LLParser::ParseMDNodeID(MDNode *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000543 // !{ ..., !42, ... }
544 unsigned MID = 0;
Chris Lattner8eff0152010-04-01 05:14:45 +0000545 if (ParseMDNodeID(Result, MID)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000546
Chris Lattner8eff0152010-04-01 05:14:45 +0000547 // If not a forward reference, just return it now.
548 if (Result) return false;
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000549
Chris Lattner8eff0152010-04-01 05:14:45 +0000550 // Otherwise, create MDNode forward reference.
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000551 MDNode *FwdNode = MDNode::getTemporary(Context, None);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000552 ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc());
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000553
Chris Lattnerfc58af22009-12-30 04:51:58 +0000554 if (NumberedMetadata.size() <= MID)
555 NumberedMetadata.resize(MID+1);
556 NumberedMetadata[MID] = FwdNode;
Chris Lattner1797fc72009-12-29 21:53:55 +0000557 Result = FwdNode;
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000558 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000559}
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000560
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000561/// ParseNamedMetadata:
Devang Patelbe626972009-07-29 00:34:02 +0000562/// !foo = !{ !1, !2 }
563bool LLParser::ParseNamedMetadata() {
Chris Lattnereafe4de2009-12-30 05:02:06 +0000564 assert(Lex.getKind() == lltok::MetadataVar);
Devang Patelbe626972009-07-29 00:34:02 +0000565 std::string Name = Lex.getStrVal();
Chris Lattnereafe4de2009-12-30 05:02:06 +0000566 Lex.Lex();
Devang Patelbe626972009-07-29 00:34:02 +0000567
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000568 if (ParseToken(lltok::equal, "expected '=' here") ||
Chris Lattner1eed2d62009-12-30 04:56:59 +0000569 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000570 ParseToken(lltok::lbrace, "Expected '{' here"))
Devang Patelbe626972009-07-29 00:34:02 +0000571 return true;
572
Dan Gohman2637cc12010-07-21 23:38:33 +0000573 NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000574 if (Lex.getKind() != lltok::rbrace)
575 do {
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000576 if (ParseToken(lltok::exclaim, "Expected '!' here"))
577 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000578
Craig Topper2617dcc2014-04-15 06:32:26 +0000579 MDNode *N = nullptr;
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000580 if (ParseMDNodeID(N)) return true;
Dan Gohman2637cc12010-07-21 23:38:33 +0000581 NMD->addOperand(N);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000582 } while (EatIfPresent(lltok::comma));
Devang Patelbe626972009-07-29 00:34:02 +0000583
584 if (ParseToken(lltok::rbrace, "expected end of metadata node"))
585 return true;
586
Devang Patelbe626972009-07-29 00:34:02 +0000587 return false;
588}
589
Devang Patel39e64d42009-07-01 19:21:12 +0000590/// ParseStandaloneMetadata:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000591/// !42 = !{...}
Devang Patel39e64d42009-07-01 19:21:12 +0000592bool LLParser::ParseStandaloneMetadata() {
Chris Lattner1eed2d62009-12-30 04:56:59 +0000593 assert(Lex.getKind() == lltok::exclaim);
Devang Patel39e64d42009-07-01 19:21:12 +0000594 Lex.Lex();
595 unsigned MetadataID = 0;
Devang Patel39e64d42009-07-01 19:21:12 +0000596
597 LocTy TyLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +0000598 Type *Ty = nullptr;
Devang Patele059ba6e2009-07-23 01:07:34 +0000599 SmallVector<Value *, 16> Elts;
Chris Lattner278bc952009-12-29 22:40:21 +0000600 if (ParseUInt32(MetadataID) ||
601 ParseToken(lltok::equal, "expected '=' here") ||
602 ParseType(Ty, TyLoc) ||
Chris Lattner1eed2d62009-12-30 04:56:59 +0000603 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattner278bc952009-12-29 22:40:21 +0000604 ParseToken(lltok::lbrace, "Expected '{' here") ||
Craig Topper2617dcc2014-04-15 06:32:26 +0000605 ParseMDNodeVector(Elts, nullptr) ||
Chris Lattner278bc952009-12-29 22:40:21 +0000606 ParseToken(lltok::rbrace, "expected end of metadata node"))
Devang Patele059ba6e2009-07-23 01:07:34 +0000607 return true;
608
Jay Foad5514afe2011-04-21 19:59:31 +0000609 MDNode *Init = MDNode::get(Context, Elts);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000610
Chris Lattnerfc58af22009-12-30 04:51:58 +0000611 // See if this was forward referenced, if so, handle it.
Chris Lattner218b22f2009-12-29 21:43:58 +0000612 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> >::iterator
Devang Pateld2541152009-07-08 19:23:54 +0000613 FI = ForwardRefMDNodes.find(MetadataID);
614 if (FI != ForwardRefMDNodes.end()) {
Dan Gohman16a5d982010-08-20 22:02:26 +0000615 MDNode *Temp = FI->second.first;
616 Temp->replaceAllUsesWith(Init);
617 MDNode::deleteTemporary(Temp);
Devang Pateld2541152009-07-08 19:23:54 +0000618 ForwardRefMDNodes.erase(FI);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000619
Chris Lattnerfc58af22009-12-30 04:51:58 +0000620 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
621 } else {
622 if (MetadataID >= NumberedMetadata.size())
623 NumberedMetadata.resize(MetadataID+1);
624
Craig Topper2617dcc2014-04-15 06:32:26 +0000625 if (NumberedMetadata[MetadataID] != nullptr)
Chris Lattnerfc58af22009-12-30 04:51:58 +0000626 return TokError("Metadata id is already used");
627 NumberedMetadata[MetadataID] = Init;
Devang Pateld2541152009-07-08 19:23:54 +0000628 }
629
Devang Patel39e64d42009-07-01 19:21:12 +0000630 return false;
631}
632
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000633static bool isValidVisibilityForLinkage(unsigned V, unsigned L) {
634 return !GlobalValue::isLocalLinkage((GlobalValue::LinkageTypes)L) ||
635 (GlobalValue::VisibilityTypes)V == GlobalValue::DefaultVisibility;
636}
637
Chris Lattnerac161bf2009-01-02 07:01:27 +0000638/// ParseAlias:
Rafael Espindola5d92ffb2014-06-03 20:25:26 +0000639/// ::= GlobalVar '=' OptionalVisibility OptionalDLLStorageClass
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000640/// OptionalThreadLocal OptionalUnNammedAddr 'alias'
641/// OptionalLinkage Aliasee
Rafael Espindola6b238632014-05-16 19:35:39 +0000642///
Chris Lattnerac161bf2009-01-02 07:01:27 +0000643/// Aliasee
Chris Lattner4c73d7a2009-04-25 21:26:00 +0000644/// ::= TypeAndValue
Chris Lattnerac161bf2009-01-02 07:01:27 +0000645///
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000646/// Everything through OptionalUnNammedAddr has already been parsed.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000647///
648bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000649 unsigned Visibility, unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000650 GlobalVariable::ThreadLocalMode TLM,
651 bool UnnamedAddr) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000652 assert(Lex.getKind() == lltok::kw_alias);
653 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000654 LocTy LinkageLoc = Lex.getLoc();
Rafael Espindola78527052013-10-06 15:10:43 +0000655 unsigned L;
656 if (ParseOptionalLinkage(L))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000657 return true;
658
Rafael Espindola78527052013-10-06 15:10:43 +0000659 GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L;
660
Rafael Espindolacaa43562013-10-09 16:07:32 +0000661 if(!GlobalAlias::isValidLinkage(Linkage))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000662 return Error(LinkageLoc, "invalid linkage type for alias");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000663
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000664 if (!isValidVisibilityForLinkage(Visibility, L))
665 return Error(LinkageLoc,
666 "symbol with local linkage must have default visibility");
667
Rafael Espindola64c1e182014-06-03 02:41:57 +0000668 Constant *Aliasee;
669 LocTy AliaseeLoc = Lex.getLoc();
670 if (Lex.getKind() != lltok::kw_bitcast &&
671 Lex.getKind() != lltok::kw_getelementptr &&
672 Lex.getKind() != lltok::kw_addrspacecast &&
673 Lex.getKind() != lltok::kw_inttoptr) {
674 if (ParseGlobalTypeAndValue(Aliasee))
Rafael Espindola6b238632014-05-16 19:35:39 +0000675 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000676 } else {
Rafael Espindola64c1e182014-06-03 02:41:57 +0000677 // The bitcast dest type is not present, it is implied by the dest type.
678 ValID ID;
679 if (ParseValID(ID))
680 return true;
681 if (ID.Kind != ValID::t_Constant)
682 return Error(AliaseeLoc, "invalid aliasee");
683 Aliasee = ID.ConstantVal;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000684 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000685
Rafael Espindola64c1e182014-06-03 02:41:57 +0000686 Type *AliaseeType = Aliasee->getType();
687 auto *PTy = dyn_cast<PointerType>(AliaseeType);
688 if (!PTy)
689 return Error(AliaseeLoc, "An alias must have pointer type");
690 Type *Ty = PTy->getElementType();
691 unsigned AddrSpace = PTy->getAddressSpace();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000692
693 // Okay, create the alias but do not insert it into the module yet.
Rafael Espindola4fe00942014-05-16 13:34:04 +0000694 std::unique_ptr<GlobalAlias> GA(
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000695 GlobalAlias::create(Ty, AddrSpace, (GlobalValue::LinkageTypes)Linkage,
696 Name, Aliasee, /*Parent*/ nullptr));
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000697 GA->setThreadLocalMode(TLM);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000698 GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000699 GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000700 GA->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000701
Chris Lattnerac161bf2009-01-02 07:01:27 +0000702 // See if this value already exists in the symbol table. If so, it is either
703 // a redefinition or a definition of a forward reference.
Chris Lattnere38317f2009-10-25 23:22:50 +0000704 if (GlobalValue *Val = M->getNamedValue(Name)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000705 // See if this was a redefinition. If so, there is no entry in
706 // ForwardRefVals.
707 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
708 I = ForwardRefVals.find(Name);
709 if (I == ForwardRefVals.end())
710 return Error(NameLoc, "redefinition of global named '@" + Name + "'");
711
712 // Otherwise, this was a definition of forward ref. Verify that types
713 // agree.
714 if (Val->getType() != GA->getType())
715 return Error(NameLoc,
716 "forward reference and definition of alias have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000717
Chris Lattnerac161bf2009-01-02 07:01:27 +0000718 // If they agree, just RAUW the old value with the alias and remove the
719 // forward ref info.
Rafael Espindolaaa273822014-05-09 21:49:17 +0000720 Val->replaceAllUsesWith(GA.get());
Chris Lattnerac161bf2009-01-02 07:01:27 +0000721 Val->eraseFromParent();
722 ForwardRefVals.erase(I);
723 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000724
Chris Lattnerac161bf2009-01-02 07:01:27 +0000725 // Insert into the module, we know its name won't collide now.
Rafael Espindolaaa273822014-05-09 21:49:17 +0000726 M->getAliasList().push_back(GA.get());
Benjamin Kramer1dc34b42010-10-16 11:28:23 +0000727 assert(GA->getName() == Name && "Should not be a name conflict!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000728
Rafael Espindolaaa273822014-05-09 21:49:17 +0000729 // The module owns this now
730 GA.release();
731
Chris Lattnerac161bf2009-01-02 07:01:27 +0000732 return false;
733}
734
735/// ParseGlobal
Nico Rieck7157bb72014-01-14 15:22:47 +0000736/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000737/// OptionalThreadLocal OptionalUnNammedAddr OptionalAddrSpace
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000738/// OptionalExternallyInitialized GlobalType Type Const
Nico Rieck7157bb72014-01-14 15:22:47 +0000739/// ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000740/// OptionalThreadLocal OptionalUnNammedAddr OptionalAddrSpace
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000741/// OptionalExternallyInitialized GlobalType Type Const
Chris Lattnerac161bf2009-01-02 07:01:27 +0000742///
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000743/// Everything up to and including OptionalUnNammedAddr has been parsed
David Majnemerc4ab61c2014-03-09 06:41:58 +0000744/// already.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000745///
746bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
747 unsigned Linkage, bool HasLinkage,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000748 unsigned Visibility, unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000749 GlobalVariable::ThreadLocalMode TLM,
750 bool UnnamedAddr) {
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000751 if (!isValidVisibilityForLinkage(Visibility, Linkage))
752 return Error(NameLoc,
753 "symbol with local linkage must have default visibility");
754
Chris Lattnerac161bf2009-01-02 07:01:27 +0000755 unsigned AddrSpace;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000756 bool IsConstant, IsExternallyInitialized;
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000757 LocTy IsExternallyInitializedLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000758 LocTy TyLoc;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000759
Craig Topper2617dcc2014-04-15 06:32:26 +0000760 Type *Ty = nullptr;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000761 if (ParseOptionalAddrSpace(AddrSpace) ||
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000762 ParseOptionalToken(lltok::kw_externally_initialized,
763 IsExternallyInitialized,
764 &IsExternallyInitializedLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000765 ParseGlobalType(IsConstant) ||
766 ParseType(Ty, TyLoc))
767 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000768
Chris Lattnerac161bf2009-01-02 07:01:27 +0000769 // If the linkage is specified and is external, then no initializer is
770 // present.
Craig Topper2617dcc2014-04-15 06:32:26 +0000771 Constant *Init = nullptr;
Nico Rieck7157bb72014-01-14 15:22:47 +0000772 if (!HasLinkage || (Linkage != GlobalValue::ExternalWeakLinkage &&
Chris Lattnerac161bf2009-01-02 07:01:27 +0000773 Linkage != GlobalValue::ExternalLinkage)) {
774 if (ParseGlobalValue(Ty, Init))
775 return true;
776 }
777
Duncan Sands19d0b472010-02-16 11:11:14 +0000778 if (Ty->isFunctionTy() || Ty->isLabelTy())
Chris Lattnerc9e1b482009-02-08 20:00:15 +0000779 return Error(TyLoc, "invalid type for global variable");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000780
Craig Topper2617dcc2014-04-15 06:32:26 +0000781 GlobalVariable *GV = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000782
783 // See if the global was forward referenced, if so, use the global.
Chris Lattner1f386b82009-02-02 07:24:28 +0000784 if (!Name.empty()) {
Chris Lattnere38317f2009-10-25 23:22:50 +0000785 if (GlobalValue *GVal = M->getNamedValue(Name)) {
786 if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal))
787 return Error(NameLoc, "redefinition of global '@" + Name + "'");
788 GV = cast<GlobalVariable>(GVal);
789 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000790 } else {
791 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
792 I = ForwardRefValIDs.find(NumberedVals.size());
793 if (I != ForwardRefValIDs.end()) {
794 GV = cast<GlobalVariable>(I->second.first);
795 ForwardRefValIDs.erase(I);
796 }
797 }
798
Craig Topper2617dcc2014-04-15 06:32:26 +0000799 if (!GV) {
800 GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, nullptr,
801 Name, nullptr, GlobalVariable::NotThreadLocal,
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000802 AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000803 } else {
804 if (GV->getType()->getElementType() != Ty)
805 return Error(TyLoc,
806 "forward reference and definition of global have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000807
Chris Lattnerac161bf2009-01-02 07:01:27 +0000808 // Move the forward-reference to the correct spot in the module.
809 M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
810 }
811
812 if (Name.empty())
813 NumberedVals.push_back(GV);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000814
Chris Lattnerac161bf2009-01-02 07:01:27 +0000815 // Set the parsed properties on the global.
816 if (Init)
817 GV->setInitializer(Init);
818 GV->setConstant(IsConstant);
819 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
820 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000821 GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000822 GV->setExternallyInitialized(IsExternallyInitialized);
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000823 GV->setThreadLocalMode(TLM);
Rafael Espindola45e6c192011-01-08 16:42:36 +0000824 GV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000825
Chris Lattnerac161bf2009-01-02 07:01:27 +0000826 // Parse attributes on the global.
827 while (Lex.getKind() == lltok::comma) {
828 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000829
Chris Lattnerac161bf2009-01-02 07:01:27 +0000830 if (Lex.getKind() == lltok::kw_section) {
831 Lex.Lex();
832 GV->setSection(Lex.getStrVal());
833 if (ParseToken(lltok::StringConstant, "expected global section string"))
834 return true;
835 } else if (Lex.getKind() == lltok::kw_align) {
836 unsigned Alignment;
837 if (ParseOptionalAlignment(Alignment)) return true;
838 GV->setAlignment(Alignment);
839 } else {
840 TokError("unknown global variable property!");
841 }
842 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000843
Chris Lattnerac161bf2009-01-02 07:01:27 +0000844 return false;
845}
846
Bill Wendling63b88192013-02-06 06:52:58 +0000847/// ParseUnnamedAttrGrp
Bill Wendlinga7c38772013-02-09 15:48:49 +0000848/// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
Bill Wendling63b88192013-02-06 06:52:58 +0000849bool LLParser::ParseUnnamedAttrGrp() {
Bill Wendlinga7c38772013-02-09 15:48:49 +0000850 assert(Lex.getKind() == lltok::kw_attributes);
Bill Wendling63b88192013-02-06 06:52:58 +0000851 LocTy AttrGrpLoc = Lex.getLoc();
Bill Wendlinga7c38772013-02-09 15:48:49 +0000852 Lex.Lex();
853
854 assert(Lex.getKind() == lltok::AttrGrpID);
Bill Wendling63b88192013-02-06 06:52:58 +0000855 unsigned VarID = Lex.getUIntVal();
Bill Wendlingb32b0412013-02-08 06:32:06 +0000856 std::vector<unsigned> unused;
Michael Gottesman41748d72013-06-27 00:25:01 +0000857 LocTy BuiltinLoc;
Bill Wendling63b88192013-02-06 06:52:58 +0000858 Lex.Lex();
859
860 if (ParseToken(lltok::equal, "expected '=' here") ||
Bill Wendling63b88192013-02-06 06:52:58 +0000861 ParseToken(lltok::lbrace, "expected '{' here") ||
Bill Wendling09bd1f72013-02-22 00:12:35 +0000862 ParseFnAttributeValuePairs(NumberedAttrBuilders[VarID], unused, true,
Michael Gottesman41748d72013-06-27 00:25:01 +0000863 BuiltinLoc) ||
Bill Wendling63b88192013-02-06 06:52:58 +0000864 ParseToken(lltok::rbrace, "expected end of attribute group"))
865 return true;
866
Bill Wendlingb32b0412013-02-08 06:32:06 +0000867 if (!NumberedAttrBuilders[VarID].hasAttributes())
Bill Wendling63b88192013-02-06 06:52:58 +0000868 return Error(AttrGrpLoc, "attribute group has no attributes");
869
870 return false;
871}
872
Bill Wendling8b0321d2013-02-08 00:52:31 +0000873/// ParseFnAttributeValuePairs
Bill Wendling63b88192013-02-06 06:52:58 +0000874/// ::= <attr> | <attr> '=' <value>
Bill Wendlingb32b0412013-02-08 06:32:06 +0000875bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B,
876 std::vector<unsigned> &FwdRefAttrGrps,
Michael Gottesman41748d72013-06-27 00:25:01 +0000877 bool inAttrGrp, LocTy &BuiltinLoc) {
Bill Wendling8b0321d2013-02-08 00:52:31 +0000878 bool HaveError = false;
879
880 B.clear();
881
Bill Wendling63b88192013-02-06 06:52:58 +0000882 while (true) {
883 lltok::Kind Token = Lex.getKind();
Michael Gottesman41748d72013-06-27 00:25:01 +0000884 if (Token == lltok::kw_builtin)
885 BuiltinLoc = Lex.getLoc();
Bill Wendling63b88192013-02-06 06:52:58 +0000886 switch (Token) {
887 default:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000888 if (!inAttrGrp) return HaveError;
Bill Wendling63b88192013-02-06 06:52:58 +0000889 return Error(Lex.getLoc(), "unterminated attribute group");
890 case lltok::rbrace:
891 // Finished.
892 return false;
893
Bill Wendlingb32b0412013-02-08 06:32:06 +0000894 case lltok::AttrGrpID: {
895 // Allow a function to reference an attribute group:
896 //
897 // define void @foo() #1 { ... }
898 if (inAttrGrp)
899 HaveError |=
900 Error(Lex.getLoc(),
901 "cannot have an attribute group reference in an attribute group");
902
903 unsigned AttrGrpNum = Lex.getUIntVal();
904 if (inAttrGrp) break;
905
906 // Save the reference to the attribute group. We'll fill it in later.
907 FwdRefAttrGrps.push_back(AttrGrpNum);
908 break;
909 }
Bill Wendling63b88192013-02-06 06:52:58 +0000910 // Target-dependent attributes:
911 case lltok::StringConstant: {
912 std::string Attr = Lex.getStrVal();
913 Lex.Lex();
914 std::string Val;
915 if (EatIfPresent(lltok::equal) &&
916 ParseStringConstant(Val))
917 return true;
918
919 B.addAttribute(Attr, Val);
Bill Wendlingb1ea9802013-02-10 10:12:50 +0000920 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000921 }
922
923 // Target-independent attributes:
924 case lltok::kw_align: {
Bill Wendlingc62789f2013-04-18 18:30:16 +0000925 // As a hack, we allow function alignment to be initially parsed as an
926 // attribute on a function declaration/definition or added to an attribute
927 // group and later moved to the alignment field.
Bill Wendling63b88192013-02-06 06:52:58 +0000928 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000929 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000930 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000931 if (ParseToken(lltok::equal, "expected '=' here") ||
932 ParseUInt32(Alignment))
933 return true;
934 } else {
935 if (ParseOptionalAlignment(Alignment))
936 return true;
937 }
Bill Wendling63b88192013-02-06 06:52:58 +0000938 B.addAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000939 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000940 }
941 case lltok::kw_alignstack: {
942 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000943 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000944 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000945 if (ParseToken(lltok::equal, "expected '=' here") ||
946 ParseUInt32(Alignment))
947 return true;
948 } else {
949 if (ParseOptionalStackAlignment(Alignment))
950 return true;
951 }
Bill Wendling63b88192013-02-06 06:52:58 +0000952 B.addStackAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000953 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000954 }
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000955 case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break;
Michael Gottesman41748d72013-06-27 00:25:01 +0000956 case lltok::kw_builtin: B.addAttribute(Attribute::Builtin); break;
Diego Novilloc6399532013-05-24 12:26:52 +0000957 case lltok::kw_cold: B.addAttribute(Attribute::Cold); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000958 case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000959 case lltok::kw_jumptable: B.addAttribute(Attribute::JumpTable); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000960 case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break;
961 case lltok::kw_naked: B.addAttribute(Attribute::Naked); break;
962 case lltok::kw_nobuiltin: B.addAttribute(Attribute::NoBuiltin); break;
963 case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break;
964 case lltok::kw_noimplicitfloat: B.addAttribute(Attribute::NoImplicitFloat); break;
965 case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break;
966 case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break;
967 case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break;
968 case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break;
969 case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000970 case lltok::kw_optnone: B.addAttribute(Attribute::OptimizeNone); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000971 case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break;
972 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
973 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
974 case lltok::kw_returns_twice: B.addAttribute(Attribute::ReturnsTwice); break;
975 case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break;
976 case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break;
977 case lltok::kw_sspstrong: B.addAttribute(Attribute::StackProtectStrong); break;
978 case lltok::kw_sanitize_address: B.addAttribute(Attribute::SanitizeAddress); break;
979 case lltok::kw_sanitize_thread: B.addAttribute(Attribute::SanitizeThread); break;
980 case lltok::kw_sanitize_memory: B.addAttribute(Attribute::SanitizeMemory); break;
981 case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000982
983 // Error handling.
984 case lltok::kw_inreg:
985 case lltok::kw_signext:
986 case lltok::kw_zeroext:
987 HaveError |=
988 Error(Lex.getLoc(),
989 "invalid use of attribute on a function");
990 break;
991 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +0000992 case lltok::kw_inalloca:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000993 case lltok::kw_nest:
994 case lltok::kw_noalias:
995 case lltok::kw_nocapture:
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000996 case lltok::kw_nonnull:
Stephen Linb8bd2322013-04-20 05:14:40 +0000997 case lltok::kw_returned:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000998 case lltok::kw_sret:
999 HaveError |=
1000 Error(Lex.getLoc(),
1001 "invalid use of parameter-only attribute on a function");
1002 break;
Bill Wendling63b88192013-02-06 06:52:58 +00001003 }
1004
1005 Lex.Lex();
1006 }
1007}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001008
1009//===----------------------------------------------------------------------===//
1010// GlobalValue Reference/Resolution Routines.
1011//===----------------------------------------------------------------------===//
1012
1013/// GetGlobalVal - Get a value with the specified name or ID, creating a
1014/// forward reference record if needed. This can return null if the value
1015/// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +00001016GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00001017 LocTy Loc) {
Chris Lattner229907c2011-07-18 04:54:35 +00001018 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001019 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001020 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001021 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001022 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001023
Chris Lattnerac161bf2009-01-02 07:01:27 +00001024 // Look this name up in the normal function symbol table.
1025 GlobalValue *Val =
1026 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001027
Chris Lattnerac161bf2009-01-02 07:01:27 +00001028 // If this is a forward reference for the value, see if we already created a
1029 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001030 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001031 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
1032 I = ForwardRefVals.find(Name);
1033 if (I != ForwardRefVals.end())
1034 Val = I->second.first;
1035 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001036
Chris Lattnerac161bf2009-01-02 07:01:27 +00001037 // If we have the value in the symbol table or fwd-ref table, return it.
1038 if (Val) {
1039 if (Val->getType() == Ty) return Val;
1040 Error(Loc, "'@" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001041 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001042 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001043 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001044
Chris Lattnerac161bf2009-01-02 07:01:27 +00001045 // Otherwise, create a new forward reference for this value and remember it.
1046 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001047 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001048 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001049 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001050 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001051 GlobalValue::ExternalWeakLinkage, nullptr, Name,
1052 nullptr, GlobalVariable::NotThreadLocal,
Justin Holewinski898a0a02012-11-16 21:03:47 +00001053 PTy->getAddressSpace());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001054
Chris Lattnerac161bf2009-01-02 07:01:27 +00001055 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1056 return FwdVal;
1057}
1058
Chris Lattner229907c2011-07-18 04:54:35 +00001059GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
1060 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001061 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001062 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001063 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001064 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001065
Craig Topper2617dcc2014-04-15 06:32:26 +00001066 GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001067
Chris Lattnerac161bf2009-01-02 07:01:27 +00001068 // If this is a forward reference for the value, see if we already created a
1069 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001070 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001071 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
1072 I = ForwardRefValIDs.find(ID);
1073 if (I != ForwardRefValIDs.end())
1074 Val = I->second.first;
1075 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001076
Chris Lattnerac161bf2009-01-02 07:01:27 +00001077 // If we have the value in the symbol table or fwd-ref table, return it.
1078 if (Val) {
1079 if (Val->getType() == Ty) return Val;
Benjamin Kramerc7583112010-09-27 17:42:11 +00001080 Error(Loc, "'@" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001081 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001082 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001083 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001084
Chris Lattnerac161bf2009-01-02 07:01:27 +00001085 // Otherwise, create a new forward reference for this value and remember it.
1086 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001087 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001088 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001089 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001090 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001091 GlobalValue::ExternalWeakLinkage, nullptr, "");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001092
Chris Lattnerac161bf2009-01-02 07:01:27 +00001093 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1094 return FwdVal;
1095}
1096
1097
1098//===----------------------------------------------------------------------===//
1099// Helper Routines.
1100//===----------------------------------------------------------------------===//
1101
1102/// ParseToken - If the current token has the specified kind, eat it and return
1103/// success. Otherwise, emit the specified error and return failure.
1104bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
1105 if (Lex.getKind() != T)
1106 return TokError(ErrMsg);
1107 Lex.Lex();
1108 return false;
1109}
1110
Chris Lattner3822f632009-01-02 08:05:26 +00001111/// ParseStringConstant
1112/// ::= StringConstant
1113bool LLParser::ParseStringConstant(std::string &Result) {
1114 if (Lex.getKind() != lltok::StringConstant)
1115 return TokError("expected string constant");
1116 Result = Lex.getStrVal();
1117 Lex.Lex();
1118 return false;
1119}
1120
1121/// ParseUInt32
1122/// ::= uint32
1123bool LLParser::ParseUInt32(unsigned &Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001124 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1125 return TokError("expected integer");
1126 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
1127 if (Val64 != unsigned(Val64))
1128 return TokError("expected 32-bit integer (too large)");
1129 Val = Val64;
1130 Lex.Lex();
1131 return false;
1132}
1133
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001134/// ParseTLSModel
1135/// := 'localdynamic'
1136/// := 'initialexec'
1137/// := 'localexec'
1138bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
1139 switch (Lex.getKind()) {
1140 default:
1141 return TokError("expected localdynamic, initialexec or localexec");
1142 case lltok::kw_localdynamic:
1143 TLM = GlobalVariable::LocalDynamicTLSModel;
1144 break;
1145 case lltok::kw_initialexec:
1146 TLM = GlobalVariable::InitialExecTLSModel;
1147 break;
1148 case lltok::kw_localexec:
1149 TLM = GlobalVariable::LocalExecTLSModel;
1150 break;
1151 }
1152
1153 Lex.Lex();
1154 return false;
1155}
1156
1157/// ParseOptionalThreadLocal
1158/// := /*empty*/
1159/// := 'thread_local'
1160/// := 'thread_local' '(' tlsmodel ')'
1161bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
1162 TLM = GlobalVariable::NotThreadLocal;
1163 if (!EatIfPresent(lltok::kw_thread_local))
1164 return false;
1165
1166 TLM = GlobalVariable::GeneralDynamicTLSModel;
1167 if (Lex.getKind() == lltok::lparen) {
1168 Lex.Lex();
1169 return ParseTLSModel(TLM) ||
1170 ParseToken(lltok::rparen, "expected ')' after thread local model");
1171 }
1172 return false;
1173}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001174
1175/// ParseOptionalAddrSpace
1176/// := /*empty*/
1177/// := 'addrspace' '(' uint32 ')'
1178bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
1179 AddrSpace = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001180 if (!EatIfPresent(lltok::kw_addrspace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001181 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001182 return ParseToken(lltok::lparen, "expected '(' in address space") ||
Chris Lattner3822f632009-01-02 08:05:26 +00001183 ParseUInt32(AddrSpace) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00001184 ParseToken(lltok::rparen, "expected ')' in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001185}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001186
Bill Wendling34c2eb22012-12-04 23:40:58 +00001187/// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes.
1188bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
1189 bool HaveError = false;
1190
1191 B.clear();
1192
1193 while (1) {
1194 lltok::Kind Token = Lex.getKind();
1195 switch (Token) {
1196 default: // End of attributes.
1197 return HaveError;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001198 case lltok::kw_align: {
1199 unsigned Alignment;
1200 if (ParseOptionalAlignment(Alignment))
1201 return true;
Bill Wendling68d24012012-10-08 22:20:14 +00001202 B.addAlignmentAttr(Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001203 continue;
1204 }
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001205 case lltok::kw_byval: B.addAttribute(Attribute::ByVal); break;
Reid Klecknera534a382013-12-19 02:14:12 +00001206 case lltok::kw_inalloca: B.addAttribute(Attribute::InAlloca); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001207 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1208 case lltok::kw_nest: B.addAttribute(Attribute::Nest); break;
1209 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1210 case lltok::kw_nocapture: B.addAttribute(Attribute::NoCapture); break;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001211 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001212 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
1213 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
Stephen Linb8bd2322013-04-20 05:14:40 +00001214 case lltok::kw_returned: B.addAttribute(Attribute::Returned); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001215 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1216 case lltok::kw_sret: B.addAttribute(Attribute::StructRet); break;
1217 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Charles Davisbe5557e2010-02-12 00:31:15 +00001218
Stephen Lin7577ed52013-04-20 13:16:13 +00001219 case lltok::kw_alignstack:
1220 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001221 case lltok::kw_builtin:
Stephen Lin7577ed52013-04-20 13:16:13 +00001222 case lltok::kw_inlinehint:
Tom Roeder44cb65f2014-06-05 19:29:43 +00001223 case lltok::kw_jumptable:
Stephen Lin7577ed52013-04-20 13:16:13 +00001224 case lltok::kw_minsize:
1225 case lltok::kw_naked:
1226 case lltok::kw_nobuiltin:
1227 case lltok::kw_noduplicate:
1228 case lltok::kw_noimplicitfloat:
1229 case lltok::kw_noinline:
1230 case lltok::kw_nonlazybind:
1231 case lltok::kw_noredzone:
1232 case lltok::kw_noreturn:
1233 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001234 case lltok::kw_optnone:
Stephen Lin7577ed52013-04-20 13:16:13 +00001235 case lltok::kw_optsize:
Stephen Lin7577ed52013-04-20 13:16:13 +00001236 case lltok::kw_returns_twice:
1237 case lltok::kw_sanitize_address:
1238 case lltok::kw_sanitize_memory:
1239 case lltok::kw_sanitize_thread:
1240 case lltok::kw_ssp:
1241 case lltok::kw_sspreq:
1242 case lltok::kw_sspstrong:
1243 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001244 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
1245 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001246 }
Bill Wendling0be9c402012-09-28 22:30:18 +00001247
Bill Wendling34c2eb22012-12-04 23:40:58 +00001248 Lex.Lex();
1249 }
1250}
1251
1252/// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes.
1253bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) {
1254 bool HaveError = false;
1255
1256 B.clear();
1257
1258 while (1) {
1259 lltok::Kind Token = Lex.getKind();
Bill Wendling0be9c402012-09-28 22:30:18 +00001260 switch (Token) {
Bill Wendling34c2eb22012-12-04 23:40:58 +00001261 default: // End of attributes.
1262 return HaveError;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001263 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1264 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001265 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001266 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1267 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Bill Wendling0be9c402012-09-28 22:30:18 +00001268
Bill Wendling34c2eb22012-12-04 23:40:58 +00001269 // Error handling.
Stephen Lin7577ed52013-04-20 13:16:13 +00001270 case lltok::kw_align:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001271 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +00001272 case lltok::kw_inalloca:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001273 case lltok::kw_nest:
1274 case lltok::kw_nocapture:
Stephen Linb8bd2322013-04-20 05:14:40 +00001275 case lltok::kw_returned:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001276 case lltok::kw_sret:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001277 HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001278 break;
James Molloy4f6fb952012-12-20 16:04:27 +00001279
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001280 case lltok::kw_alignstack:
1281 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001282 case lltok::kw_builtin:
Diego Novilloc6399532013-05-24 12:26:52 +00001283 case lltok::kw_cold:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001284 case lltok::kw_inlinehint:
Tom Roeder44cb65f2014-06-05 19:29:43 +00001285 case lltok::kw_jumptable:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001286 case lltok::kw_minsize:
1287 case lltok::kw_naked:
1288 case lltok::kw_nobuiltin:
1289 case lltok::kw_noduplicate:
1290 case lltok::kw_noimplicitfloat:
1291 case lltok::kw_noinline:
1292 case lltok::kw_nonlazybind:
1293 case lltok::kw_noredzone:
1294 case lltok::kw_noreturn:
1295 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001296 case lltok::kw_optnone:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001297 case lltok::kw_optsize:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001298 case lltok::kw_returns_twice:
1299 case lltok::kw_sanitize_address:
1300 case lltok::kw_sanitize_memory:
1301 case lltok::kw_sanitize_thread:
1302 case lltok::kw_ssp:
1303 case lltok::kw_sspreq:
1304 case lltok::kw_sspstrong:
1305 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001306 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001307 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001308
1309 case lltok::kw_readnone:
1310 case lltok::kw_readonly:
1311 HaveError |= Error(Lex.getLoc(), "invalid use of attribute on return type");
Bill Wendling0be9c402012-09-28 22:30:18 +00001312 }
1313
Chris Lattnerac161bf2009-01-02 07:01:27 +00001314 Lex.Lex();
1315 }
1316}
1317
1318/// ParseOptionalLinkage
1319/// ::= /*empty*/
Rafael Espindola6de96a12009-01-15 20:18:42 +00001320/// ::= 'private'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001321/// ::= 'internal'
1322/// ::= 'weak'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001323/// ::= 'weak_odr'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001324/// ::= 'linkonce'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001325/// ::= 'linkonce_odr'
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001326/// ::= 'available_externally'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001327/// ::= 'appending'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001328/// ::= 'common'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001329/// ::= 'extern_weak'
1330/// ::= 'external'
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001331///
1332/// Deprecated Values:
1333/// ::= 'linker_private'
1334/// ::= 'linker_private_weak'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001335bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) {
1336 HasLinkage = false;
1337 switch (Lex.getKind()) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001338 default: Res=GlobalValue::ExternalLinkage; return false;
1339 case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001340 case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break;
1341 case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break;
1342 case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break;
1343 case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break;
1344 case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001345 case lltok::kw_available_externally:
1346 Res = GlobalValue::AvailableExternallyLinkage;
1347 break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001348 case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001349 case lltok::kw_common: Res = GlobalValue::CommonLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001350 case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break;
1351 case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break;
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001352
1353 case lltok::kw_linker_private:
1354 case lltok::kw_linker_private_weak:
Saleem Abdulrasoolefa31a92014-04-05 22:42:53 +00001355 Lex.Warning("'" + Lex.getStrVal() + "' is deprecated, treating as"
1356 " PrivateLinkage");
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001357 Lex.Lex();
1358 // treat linker_private and linker_private_weak as PrivateLinkage
1359 Res = GlobalValue::PrivateLinkage;
1360 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001361 }
1362 Lex.Lex();
1363 HasLinkage = true;
1364 return false;
1365}
1366
1367/// ParseOptionalVisibility
1368/// ::= /*empty*/
1369/// ::= 'default'
1370/// ::= 'hidden'
1371/// ::= 'protected'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001372///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001373bool LLParser::ParseOptionalVisibility(unsigned &Res) {
1374 switch (Lex.getKind()) {
1375 default: Res = GlobalValue::DefaultVisibility; return false;
1376 case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break;
1377 case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break;
1378 case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break;
1379 }
1380 Lex.Lex();
1381 return false;
1382}
1383
Nico Rieck7157bb72014-01-14 15:22:47 +00001384/// ParseOptionalDLLStorageClass
1385/// ::= /*empty*/
1386/// ::= 'dllimport'
1387/// ::= 'dllexport'
1388///
1389bool LLParser::ParseOptionalDLLStorageClass(unsigned &Res) {
1390 switch (Lex.getKind()) {
1391 default: Res = GlobalValue::DefaultStorageClass; return false;
1392 case lltok::kw_dllimport: Res = GlobalValue::DLLImportStorageClass; break;
1393 case lltok::kw_dllexport: Res = GlobalValue::DLLExportStorageClass; break;
1394 }
1395 Lex.Lex();
1396 return false;
1397}
1398
Chris Lattnerac161bf2009-01-02 07:01:27 +00001399/// ParseOptionalCallingConv
1400/// ::= /*empty*/
1401/// ::= 'ccc'
1402/// ::= 'fastcc'
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001403/// ::= 'kw_intel_ocl_bicc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001404/// ::= 'coldcc'
1405/// ::= 'x86_stdcallcc'
1406/// ::= 'x86_fastcallcc'
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001407/// ::= 'x86_thiscallcc'
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001408/// ::= 'arm_apcscc'
1409/// ::= 'arm_aapcscc'
1410/// ::= 'arm_aapcs_vfpcc'
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001411/// ::= 'msp430_intrcc'
Che-Liang Chiou29947902010-09-25 07:46:17 +00001412/// ::= 'ptx_kernel'
1413/// ::= 'ptx_device'
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001414/// ::= 'spir_func'
1415/// ::= 'spir_kernel'
Charles Davise8f297c2013-07-12 06:02:35 +00001416/// ::= 'x86_64_sysvcc'
1417/// ::= 'x86_64_win64cc'
Andrew Tricka3a11de2013-10-31 22:12:01 +00001418/// ::= 'webkit_jscc'
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001419/// ::= 'anyregcc'
Juergen Ributzkae6250132014-01-17 19:47:03 +00001420/// ::= 'preserve_mostcc'
1421/// ::= 'preserve_allcc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001422/// ::= 'cc' UINT
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001423///
Sandeep Patel68c5f472009-09-02 08:44:58 +00001424bool LLParser::ParseOptionalCallingConv(CallingConv::ID &CC) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001425 switch (Lex.getKind()) {
1426 default: CC = CallingConv::C; return false;
1427 case lltok::kw_ccc: CC = CallingConv::C; break;
1428 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
1429 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
1430 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
1431 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001432 case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001433 case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break;
1434 case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break;
1435 case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001436 case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
Che-Liang Chiou29947902010-09-25 07:46:17 +00001437 case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break;
1438 case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break;
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001439 case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break;
1440 case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break;
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001441 case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break;
Charles Davise8f297c2013-07-12 06:02:35 +00001442 case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break;
1443 case lltok::kw_x86_64_win64cc: CC = CallingConv::X86_64_Win64; break;
Andrew Tricka3a11de2013-10-31 22:12:01 +00001444 case lltok::kw_webkit_jscc: CC = CallingConv::WebKit_JS; break;
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001445 case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break;
Juergen Ributzkae6250132014-01-17 19:47:03 +00001446 case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break;
1447 case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001448 case lltok::kw_cc: {
1449 unsigned ArbitraryCC;
1450 Lex.Lex();
David Blaikie46a9f012012-01-20 21:51:11 +00001451 if (ParseUInt32(ArbitraryCC))
Sandeep Patel68c5f472009-09-02 08:44:58 +00001452 return true;
David Blaikie46a9f012012-01-20 21:51:11 +00001453 CC = static_cast<CallingConv::ID>(ArbitraryCC);
1454 return false;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001455 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00001456 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001457
Chris Lattnerac161bf2009-01-02 07:01:27 +00001458 Lex.Lex();
1459 return false;
1460}
1461
Chris Lattner5c427632009-12-30 05:31:19 +00001462/// ParseInstructionMetadata
Chris Lattner596760d2009-12-29 21:25:40 +00001463/// ::= !dbg !42 (',' !dbg !57)*
Dan Gohman338d9a42010-08-24 02:05:17 +00001464bool LLParser::ParseInstructionMetadata(Instruction *Inst,
1465 PerFunctionState *PFS) {
Chris Lattner5c427632009-12-30 05:31:19 +00001466 do {
1467 if (Lex.getKind() != lltok::MetadataVar)
1468 return TokError("expected metadata after comma");
Devang Patelba4a6fd2009-09-29 00:01:14 +00001469
Chris Lattner596760d2009-12-29 21:25:40 +00001470 std::string Name = Lex.getStrVal();
Benjamin Kramerb3bd0192011-12-06 11:50:26 +00001471 unsigned MDK = M->getMDKindID(Name);
Chris Lattner596760d2009-12-29 21:25:40 +00001472 Lex.Lex();
Chris Lattner8d58f2f2009-10-19 05:31:10 +00001473
Chris Lattner1797fc72009-12-29 21:53:55 +00001474 MDNode *Node;
Chris Lattner8eff0152010-04-01 05:14:45 +00001475 SMLoc Loc = Lex.getLoc();
Dan Gohmanc828c542010-08-24 02:24:03 +00001476
1477 if (ParseToken(lltok::exclaim, "expected '!' here"))
Chris Lattner1eed2d62009-12-30 04:56:59 +00001478 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001479
Dan Gohmanf0715b12010-08-24 14:35:45 +00001480 // This code is similar to that of ParseMetadataValue, however it needs to
1481 // have special-case code for a forward reference; see the comments on
1482 // ForwardRefInstMetadata for details. Also, MDStrings are not supported
1483 // at the top level here.
Dan Gohmanc828c542010-08-24 02:24:03 +00001484 if (Lex.getKind() == lltok::lbrace) {
1485 ValID ID;
1486 if (ParseMetadataListValue(ID, PFS))
1487 return true;
1488 assert(ID.Kind == ValID::t_MDNode);
1489 Inst->setMetadata(MDK, ID.MDNodeVal);
Chris Lattner8eff0152010-04-01 05:14:45 +00001490 } else {
Nick Lewycky912888f2010-09-30 21:04:13 +00001491 unsigned NodeID = 0;
Dan Gohmanc828c542010-08-24 02:24:03 +00001492 if (ParseMDNodeID(Node, NodeID))
1493 return true;
1494 if (Node) {
1495 // If we got the node, add it to the instruction.
1496 Inst->setMetadata(MDK, Node);
1497 } else {
1498 MDRef R = { Loc, MDK, NodeID };
1499 // Otherwise, remember that this should be resolved later.
1500 ForwardRefInstMetadata[Inst].push_back(R);
1501 }
Chris Lattner8eff0152010-04-01 05:14:45 +00001502 }
Chris Lattner596760d2009-12-29 21:25:40 +00001503
Manman Ren209b17c2013-09-28 00:22:27 +00001504 if (MDK == LLVMContext::MD_tbaa)
1505 InstsWithTBAATag.push_back(Inst);
1506
Chris Lattner596760d2009-12-29 21:25:40 +00001507 // If this is the end of the list, we're done.
Chris Lattner5c427632009-12-30 05:31:19 +00001508 } while (EatIfPresent(lltok::comma));
1509 return false;
Devang Patelea8a4b92009-09-17 23:04:48 +00001510}
1511
Chris Lattnerac161bf2009-01-02 07:01:27 +00001512/// ParseOptionalAlignment
1513/// ::= /* empty */
1514/// ::= 'align' 4
1515bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1516 Alignment = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001517 if (!EatIfPresent(lltok::kw_align))
1518 return false;
Chris Lattnerd11f5142009-01-05 07:46:05 +00001519 LocTy AlignLoc = Lex.getLoc();
1520 if (ParseUInt32(Alignment)) return true;
1521 if (!isPowerOf2_32(Alignment))
1522 return Error(AlignLoc, "alignment is not a power of two");
Dan Gohmand566d2c2010-07-30 21:07:05 +00001523 if (Alignment > Value::MaximumAlignment)
Dan Gohmana7e5a242010-07-28 20:12:04 +00001524 return Error(AlignLoc, "huge alignments are not supported yet");
Chris Lattnerd11f5142009-01-05 07:46:05 +00001525 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001526}
1527
Chris Lattnerb2f39502009-12-30 05:44:30 +00001528/// ParseOptionalCommaAlign
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001529/// ::=
Chris Lattnerb2f39502009-12-30 05:44:30 +00001530/// ::= ',' align 4
1531///
1532/// This returns with AteExtraComma set to true if it ate an excess comma at the
1533/// end.
1534bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
1535 bool &AteExtraComma) {
1536 AteExtraComma = false;
1537 while (EatIfPresent(lltok::comma)) {
1538 // Metadata at the end is an early exit.
Chris Lattnereafe4de2009-12-30 05:02:06 +00001539 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerb2f39502009-12-30 05:44:30 +00001540 AteExtraComma = true;
1541 return false;
1542 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001543
Chris Lattner95b0ff42010-04-23 00:50:50 +00001544 if (Lex.getKind() != lltok::kw_align)
1545 return Error(Lex.getLoc(), "expected metadata or 'align'");
Duncan Sands4c5c8582010-10-21 16:07:10 +00001546
Chris Lattner95b0ff42010-04-23 00:50:50 +00001547 if (ParseOptionalAlignment(Alignment)) return true;
Chris Lattnerb2f39502009-12-30 05:44:30 +00001548 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001549
Devang Patelea8a4b92009-09-17 23:04:48 +00001550 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001551}
1552
Eli Friedmanfee02c62011-07-25 23:16:38 +00001553/// ParseScopeAndOrdering
1554/// if isAtomic: ::= 'singlethread'? AtomicOrdering
1555/// else: ::=
1556///
1557/// This sets Scope and Ordering to the parsed values.
1558bool LLParser::ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
1559 AtomicOrdering &Ordering) {
1560 if (!isAtomic)
1561 return false;
1562
1563 Scope = CrossThread;
1564 if (EatIfPresent(lltok::kw_singlethread))
1565 Scope = SingleThread;
Tim Northovere94a5182014-03-11 10:48:52 +00001566
1567 return ParseOrdering(Ordering);
1568}
1569
1570/// ParseOrdering
1571/// ::= AtomicOrdering
1572///
1573/// This sets Ordering to the parsed value.
1574bool LLParser::ParseOrdering(AtomicOrdering &Ordering) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001575 switch (Lex.getKind()) {
1576 default: return TokError("Expected ordering on atomic instruction");
1577 case lltok::kw_unordered: Ordering = Unordered; break;
1578 case lltok::kw_monotonic: Ordering = Monotonic; break;
1579 case lltok::kw_acquire: Ordering = Acquire; break;
1580 case lltok::kw_release: Ordering = Release; break;
1581 case lltok::kw_acq_rel: Ordering = AcquireRelease; break;
1582 case lltok::kw_seq_cst: Ordering = SequentiallyConsistent; break;
1583 }
1584 Lex.Lex();
1585 return false;
1586}
1587
Charles Davisbe5557e2010-02-12 00:31:15 +00001588/// ParseOptionalStackAlignment
1589/// ::= /* empty */
1590/// ::= 'alignstack' '(' 4 ')'
1591bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
1592 Alignment = 0;
1593 if (!EatIfPresent(lltok::kw_alignstack))
1594 return false;
1595 LocTy ParenLoc = Lex.getLoc();
1596 if (!EatIfPresent(lltok::lparen))
1597 return Error(ParenLoc, "expected '('");
1598 LocTy AlignLoc = Lex.getLoc();
1599 if (ParseUInt32(Alignment)) return true;
1600 ParenLoc = Lex.getLoc();
1601 if (!EatIfPresent(lltok::rparen))
1602 return Error(ParenLoc, "expected ')'");
1603 if (!isPowerOf2_32(Alignment))
1604 return Error(AlignLoc, "stack alignment is not a power of two");
1605 return false;
1606}
Devang Patelea8a4b92009-09-17 23:04:48 +00001607
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001608/// ParseIndexList - This parses the index list for an insert/extractvalue
1609/// instruction. This sets AteExtraComma in the case where we eat an extra
1610/// comma at the end of the line and find that it is followed by metadata.
1611/// Clients that don't allow metadata can call the version of this function that
1612/// only takes one argument.
1613///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001614/// ParseIndexList
1615/// ::= (',' uint32)+
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001616///
1617bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
1618 bool &AteExtraComma) {
1619 AteExtraComma = false;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001620
Chris Lattnerac161bf2009-01-02 07:01:27 +00001621 if (Lex.getKind() != lltok::comma)
1622 return TokError("expected ',' as start of index list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001623
Chris Lattner3822f632009-01-02 08:05:26 +00001624 while (EatIfPresent(lltok::comma)) {
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001625 if (Lex.getKind() == lltok::MetadataVar) {
1626 AteExtraComma = true;
1627 return false;
1628 }
Nick Lewycky83e47112010-09-29 23:32:20 +00001629 unsigned Idx = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001630 if (ParseUInt32(Idx)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001631 Indices.push_back(Idx);
1632 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001633
Chris Lattnerac161bf2009-01-02 07:01:27 +00001634 return false;
1635}
1636
1637//===----------------------------------------------------------------------===//
1638// Type Parsing.
1639//===----------------------------------------------------------------------===//
1640
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001641/// ParseType - Parse a type.
1642bool LLParser::ParseType(Type *&Result, bool AllowVoid) {
1643 SMLoc TypeLoc = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001644 switch (Lex.getKind()) {
1645 default:
1646 return TokError("expected type");
1647 case lltok::Type:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001648 // Type ::= 'float' | 'void' (etc)
Chris Lattnerac161bf2009-01-02 07:01:27 +00001649 Result = Lex.getTyVal();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001650 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001651 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001652 case lltok::lbrace:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001653 // Type ::= StructType
1654 if (ParseAnonStructType(Result, false))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001655 return true;
1656 break;
1657 case lltok::lsquare:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001658 // Type ::= '[' ... ']'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001659 Lex.Lex(); // eat the lsquare.
1660 if (ParseArrayVectorType(Result, false))
1661 return true;
1662 break;
1663 case lltok::less: // Either vector or packed struct.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001664 // Type ::= '<' ... '>'
Chris Lattner3822f632009-01-02 08:05:26 +00001665 Lex.Lex();
1666 if (Lex.getKind() == lltok::lbrace) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001667 if (ParseAnonStructType(Result, true) ||
Chris Lattner3822f632009-01-02 08:05:26 +00001668 ParseToken(lltok::greater, "expected '>' at end of packed struct"))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001669 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001670 } else if (ParseArrayVectorType(Result, true))
1671 return true;
1672 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001673 case lltok::LocalVar: {
1674 // Type ::= %foo
1675 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001676
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001677 // If the type hasn't been defined yet, create a forward definition and
1678 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001679 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001680 Entry.first = StructType::create(Context, Lex.getStrVal());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001681 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001682 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001683 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001684 Lex.Lex();
1685 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001686 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001687
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001688 case lltok::LocalVarID: {
1689 // Type ::= %4
1690 if (Lex.getUIntVal() >= NumberedTypes.size())
1691 NumberedTypes.resize(Lex.getUIntVal()+1);
1692 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001693
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001694 // If the type hasn't been defined yet, create a forward definition and
1695 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001696 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001697 Entry.first = StructType::create(Context);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001698 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001699 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001700 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001701 Lex.Lex();
1702 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001703 }
1704 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001705
1706 // Parse the type suffixes.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001707 while (1) {
1708 switch (Lex.getKind()) {
1709 // End of type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001710 default:
1711 if (!AllowVoid && Result->isVoidTy())
1712 return Error(TypeLoc, "void type only allowed for function results");
1713 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001714
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001715 // Type ::= Type '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001716 case lltok::star:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001717 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001718 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001719 if (Result->isVoidTy())
1720 return TokError("pointers to void are invalid - use i8* instead");
1721 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001722 return TokError("pointer to this type is invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001723 Result = PointerType::getUnqual(Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001724 Lex.Lex();
1725 break;
1726
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001727 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001728 case lltok::kw_addrspace: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001729 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001730 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001731 if (Result->isVoidTy())
Dan Gohman9280a682009-02-09 17:41:21 +00001732 return TokError("pointers to void are invalid; use i8* instead");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001733 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001734 return TokError("pointer to this type is invalid");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001735 unsigned AddrSpace;
1736 if (ParseOptionalAddrSpace(AddrSpace) ||
1737 ParseToken(lltok::star, "expected '*' in address space"))
1738 return true;
1739
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001740 Result = PointerType::get(Result, AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001741 break;
1742 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001743
Chris Lattnerac161bf2009-01-02 07:01:27 +00001744 /// Types '(' ArgTypeListI ')' OptFuncAttrs
1745 case lltok::lparen:
1746 if (ParseFunctionType(Result))
1747 return true;
1748 break;
1749 }
1750 }
1751}
1752
1753/// ParseParameterList
1754/// ::= '(' ')'
1755/// ::= '(' Arg (',' Arg)* ')'
1756/// Arg
1757/// ::= Type OptionalAttributes Value OptionalAttributes
1758bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
1759 PerFunctionState &PFS) {
1760 if (ParseToken(lltok::lparen, "expected '(' in call"))
1761 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001762
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001763 unsigned AttrIndex = 1;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001764 while (Lex.getKind() != lltok::rparen) {
1765 // If this isn't the first argument, we need a comma.
1766 if (!ArgList.empty() &&
1767 ParseToken(lltok::comma, "expected ',' in argument list"))
1768 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001769
Chris Lattnerac161bf2009-01-02 07:01:27 +00001770 // Parse the argument.
1771 LocTy ArgLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00001772 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001773 AttrBuilder ArgAttrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001774 Value *V;
Victor Hernandezfa232232009-12-03 23:40:58 +00001775 if (ParseType(ArgTy, ArgLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001776 return true;
Victor Hernandezfa232232009-12-03 23:40:58 +00001777
Chris Lattner5b4a9622009-12-30 02:11:14 +00001778 // Otherwise, handle normal operands.
Bill Wendling34c2eb22012-12-04 23:40:58 +00001779 if (ParseOptionalParamAttrs(ArgAttrs) || ParseValue(ArgTy, V, PFS))
Chris Lattner5b4a9622009-12-30 02:11:14 +00001780 return true;
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001781 ArgList.push_back(ParamInfo(ArgLoc, V, AttributeSet::get(V->getContext(),
1782 AttrIndex++,
1783 ArgAttrs)));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001784 }
1785
1786 Lex.Lex(); // Lex the ')'.
1787 return false;
1788}
1789
1790
1791
Chris Lattner2ed06b42009-01-05 18:34:07 +00001792/// ParseArgumentList - Parse the argument list for a function type or function
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001793/// prototype.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001794/// ::= '(' ArgTypeListI ')'
1795/// ArgTypeListI
1796/// ::= /*empty*/
1797/// ::= '...'
1798/// ::= ArgTypeList ',' '...'
1799/// ::= ArgType (',' ArgType)*
Chris Lattner2ed06b42009-01-05 18:34:07 +00001800///
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001801bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
1802 bool &isVarArg){
Chris Lattnerac161bf2009-01-02 07:01:27 +00001803 isVarArg = false;
1804 assert(Lex.getKind() == lltok::lparen);
1805 Lex.Lex(); // eat the (.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001806
Chris Lattnerac161bf2009-01-02 07:01:27 +00001807 if (Lex.getKind() == lltok::rparen) {
1808 // empty
1809 } else if (Lex.getKind() == lltok::dotdotdot) {
1810 isVarArg = true;
1811 Lex.Lex();
1812 } else {
1813 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001814 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001815 AttrBuilder Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001816 std::string Name;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001817
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001818 if (ParseType(ArgTy) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00001819 ParseOptionalParamAttrs(Attrs)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001820
Chris Lattnerfdd87902009-10-05 05:54:46 +00001821 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001822 return Error(TypeLoc, "argument can not have void type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001823
Chris Lattnerdef19492011-06-17 06:36:20 +00001824 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001825 Name = Lex.getStrVal();
1826 Lex.Lex();
1827 }
Chris Lattner3822f632009-01-02 08:05:26 +00001828
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001829 if (!FunctionType::isValidArgumentType(ArgTy))
Chris Lattner3822f632009-01-02 08:05:26 +00001830 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001831
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001832 unsigned AttrIndex = 1;
Bill Wendlingd079a442012-10-15 04:46:55 +00001833 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001834 AttributeSet::get(ArgTy->getContext(),
1835 AttrIndex++, Attrs), Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001836
Chris Lattner3822f632009-01-02 08:05:26 +00001837 while (EatIfPresent(lltok::comma)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001838 // Handle ... at end of arg list.
Chris Lattner3822f632009-01-02 08:05:26 +00001839 if (EatIfPresent(lltok::dotdotdot)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001840 isVarArg = true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001841 break;
1842 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001843
Chris Lattnerac161bf2009-01-02 07:01:27 +00001844 // Otherwise must be an argument type.
1845 TypeLoc = Lex.getLoc();
Bill Wendling34c2eb22012-12-04 23:40:58 +00001846 if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) return true;
Chris Lattner3822f632009-01-02 08:05:26 +00001847
Chris Lattnerfdd87902009-10-05 05:54:46 +00001848 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001849 return Error(TypeLoc, "argument can not have void type");
1850
Chris Lattnerdef19492011-06-17 06:36:20 +00001851 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001852 Name = Lex.getStrVal();
1853 Lex.Lex();
1854 } else {
1855 Name = "";
1856 }
Chris Lattner3822f632009-01-02 08:05:26 +00001857
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001858 if (!ArgTy->isFirstClassType())
Chris Lattner3822f632009-01-02 08:05:26 +00001859 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001860
Bill Wendlingd079a442012-10-15 04:46:55 +00001861 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001862 AttributeSet::get(ArgTy->getContext(),
1863 AttrIndex++, Attrs),
Bill Wendlingd079a442012-10-15 04:46:55 +00001864 Name));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001865 }
1866 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001867
Chris Lattner3822f632009-01-02 08:05:26 +00001868 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001869}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001870
Chris Lattnerac161bf2009-01-02 07:01:27 +00001871/// ParseFunctionType
1872/// ::= Type ArgumentList OptionalAttrs
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001873bool LLParser::ParseFunctionType(Type *&Result) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001874 assert(Lex.getKind() == lltok::lparen);
1875
Chris Lattnerce473c72009-01-05 08:04:33 +00001876 if (!FunctionType::isValidReturnType(Result))
1877 return TokError("invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001878
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001879 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001880 bool isVarArg;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001881 if (ParseArgumentList(ArgList, isVarArg))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001882 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001883
Chris Lattnerac161bf2009-01-02 07:01:27 +00001884 // Reject names on the arguments lists.
1885 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
1886 if (!ArgList[i].Name.empty())
1887 return Error(ArgList[i].Loc, "argument name invalid in function type");
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001888 if (ArgList[i].Attrs.hasAttributes(i + 1))
Chris Lattner6bc5c892011-06-17 17:37:13 +00001889 return Error(ArgList[i].Loc,
1890 "argument attributes invalid in function type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001891 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001892
Jay Foadb804a2b2011-07-12 14:06:48 +00001893 SmallVector<Type*, 16> ArgListTy;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001894 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001895 ArgListTy.push_back(ArgList[i].Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001896
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001897 Result = FunctionType::get(Result, ArgListTy, isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001898 return false;
1899}
1900
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001901/// ParseAnonStructType - Parse an anonymous struct type, which is inlined into
1902/// other structs.
1903bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) {
1904 SmallVector<Type*, 8> Elts;
1905 if (ParseStructBody(Elts)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001906
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001907 Result = StructType::get(Context, Elts, Packed);
1908 return false;
1909}
1910
1911/// ParseStructDefinition - Parse a struct in a 'type' definition.
1912bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
1913 std::pair<Type*, LocTy> &Entry,
1914 Type *&ResultTy) {
1915 // If the type was already defined, diagnose the redefinition.
1916 if (Entry.first && !Entry.second.isValid())
1917 return Error(TypeLoc, "redefinition of type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001918
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001919 // If we have opaque, just return without filling in the definition for the
1920 // struct. This counts as a definition as far as the .ll file goes.
1921 if (EatIfPresent(lltok::kw_opaque)) {
1922 // This type is being defined, so clear the location to indicate this.
1923 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001924
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001925 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00001926 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00001927 Entry.first = StructType::create(Context, Name);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001928 ResultTy = Entry.first;
1929 return false;
1930 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001931
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001932 // If the type starts with '<', then it is either a packed struct or a vector.
1933 bool isPacked = EatIfPresent(lltok::less);
1934
1935 // If we don't have a struct, then we have a random type alias, which we
1936 // accept for compatibility with old files. These types are not allowed to be
1937 // forward referenced and not allowed to be recursive.
1938 if (Lex.getKind() != lltok::lbrace) {
1939 if (Entry.first)
1940 return Error(TypeLoc, "forward references to non-struct type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001941
Craig Topper2617dcc2014-04-15 06:32:26 +00001942 ResultTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001943 if (isPacked)
1944 return ParseArrayVectorType(ResultTy, true);
1945 return ParseType(ResultTy);
1946 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001947
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001948 // This type is being defined, so clear the location to indicate this.
1949 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001950
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001951 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00001952 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00001953 Entry.first = StructType::create(Context, Name);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001954
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001955 StructType *STy = cast<StructType>(Entry.first);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001956
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001957 SmallVector<Type*, 8> Body;
1958 if (ParseStructBody(Body) ||
1959 (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct")))
1960 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001961
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001962 STy->setBody(Body, isPacked);
1963 ResultTy = STy;
1964 return false;
1965}
1966
1967
Chris Lattnerac161bf2009-01-02 07:01:27 +00001968/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001969/// StructType
Chris Lattnerac161bf2009-01-02 07:01:27 +00001970/// ::= '{' '}'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001971/// ::= '{' Type (',' Type)* '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001972/// ::= '<' '{' '}' '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001973/// ::= '<' '{' Type (',' Type)* '}' '>'
1974bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001975 assert(Lex.getKind() == lltok::lbrace);
1976 Lex.Lex(); // Consume the '{'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001977
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001978 // Handle the empty struct.
1979 if (EatIfPresent(lltok::rbrace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001980 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001981
Chris Lattnerf880ca22009-03-09 04:49:14 +00001982 LocTy EltTyLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001983 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001984 if (ParseType(Ty)) return true;
1985 Body.push_back(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001986
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001987 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001988 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001989
Chris Lattner3822f632009-01-02 08:05:26 +00001990 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf880ca22009-03-09 04:49:14 +00001991 EltTyLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001992 if (ParseType(Ty)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001993
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001994 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001995 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001996
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001997 Body.push_back(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001998 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001999
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002000 return ParseToken(lltok::rbrace, "expected '}' at end of struct");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002001}
2002
2003/// ParseArrayVectorType - Parse an array or vector type, assuming the first
2004/// token has already been consumed.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002005/// Type
Chris Lattnerac161bf2009-01-02 07:01:27 +00002006/// ::= '[' APSINTVAL 'x' Types ']'
2007/// ::= '<' APSINTVAL 'x' Types '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002008bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002009 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
2010 Lex.getAPSIntVal().getBitWidth() > 64)
2011 return TokError("expected number in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002012
Chris Lattnerac161bf2009-01-02 07:01:27 +00002013 LocTy SizeLoc = Lex.getLoc();
2014 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
Chris Lattner3822f632009-01-02 08:05:26 +00002015 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002016
Chris Lattner3822f632009-01-02 08:05:26 +00002017 if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
2018 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002019
2020 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00002021 Type *EltTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002022 if (ParseType(EltTy)) return true;
Chris Lattnerf880ca22009-03-09 04:49:14 +00002023
Chris Lattner3822f632009-01-02 08:05:26 +00002024 if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
2025 "expected end of sequential type"))
2026 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002027
Chris Lattnerac161bf2009-01-02 07:01:27 +00002028 if (isVector) {
Chris Lattnerbb1fe8a2009-02-28 18:12:41 +00002029 if (Size == 0)
2030 return Error(SizeLoc, "zero element vector is illegal");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002031 if ((unsigned)Size != Size)
2032 return Error(SizeLoc, "size too large for vector");
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002033 if (!VectorType::isValidElementType(EltTy))
Duncan Sandse6beec62012-11-13 12:59:33 +00002034 return Error(TypeLoc, "invalid vector element type");
Owen Anderson4056ca92009-07-29 22:17:13 +00002035 Result = VectorType::get(EltTy, unsigned(Size));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002036 } else {
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002037 if (!ArrayType::isValidElementType(EltTy))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002038 return Error(TypeLoc, "invalid array element type");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002039 Result = ArrayType::get(EltTy, Size);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002040 }
2041 return false;
2042}
2043
2044//===----------------------------------------------------------------------===//
2045// Function Semantic Analysis.
2046//===----------------------------------------------------------------------===//
2047
Chris Lattner3432c622009-10-28 03:39:23 +00002048LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
2049 int functionNumber)
2050 : P(p), F(f), FunctionNumber(functionNumber) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002051
2052 // Insert unnamed arguments into the NumberedVals list.
2053 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
2054 AI != E; ++AI)
2055 if (!AI->hasName())
2056 NumberedVals.push_back(AI);
2057}
2058
2059LLParser::PerFunctionState::~PerFunctionState() {
2060 // If there were any forward referenced non-basicblock values, delete them.
2061 for (std::map<std::string, std::pair<Value*, LocTy> >::iterator
2062 I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
2063 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002064 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002065 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002066 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002067 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002068 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002069
Chris Lattnerac161bf2009-01-02 07:01:27 +00002070 for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2071 I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
2072 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002073 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002074 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002075 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002076 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002077 }
2078}
2079
Chris Lattner3432c622009-10-28 03:39:23 +00002080bool LLParser::PerFunctionState::FinishFunction() {
2081 // Check to see if someone took the address of labels in this block.
2082 if (!P.ForwardRefBlockAddresses.empty()) {
2083 ValID FunctionID;
2084 if (!F.getName().empty()) {
2085 FunctionID.Kind = ValID::t_GlobalName;
2086 FunctionID.StrVal = F.getName();
2087 } else {
2088 FunctionID.Kind = ValID::t_GlobalID;
2089 FunctionID.UIntVal = FunctionNumber;
2090 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002091
Chris Lattner3432c622009-10-28 03:39:23 +00002092 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >::iterator
2093 FRBAI = P.ForwardRefBlockAddresses.find(FunctionID);
2094 if (FRBAI != P.ForwardRefBlockAddresses.end()) {
2095 // Resolve all these references.
2096 if (P.ResolveForwardRefBlockAddresses(&F, FRBAI->second, this))
2097 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002098
Chris Lattner3432c622009-10-28 03:39:23 +00002099 P.ForwardRefBlockAddresses.erase(FRBAI);
2100 }
2101 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002102
Chris Lattnerac161bf2009-01-02 07:01:27 +00002103 if (!ForwardRefVals.empty())
2104 return P.Error(ForwardRefVals.begin()->second.second,
2105 "use of undefined value '%" + ForwardRefVals.begin()->first +
2106 "'");
2107 if (!ForwardRefValIDs.empty())
2108 return P.Error(ForwardRefValIDs.begin()->second.second,
2109 "use of undefined value '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002110 Twine(ForwardRefValIDs.begin()->first) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002111 return false;
2112}
2113
2114
2115/// GetVal - Get a value with the specified name or ID, creating a
2116/// forward reference record if needed. This can return null if the value
2117/// exists but does not have the right type.
2118Value *LLParser::PerFunctionState::GetVal(const std::string &Name,
Chris Lattner229907c2011-07-18 04:54:35 +00002119 Type *Ty, LocTy Loc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002120 // Look this name up in the normal function symbol table.
2121 Value *Val = F.getValueSymbolTable().lookup(Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002122
Chris Lattnerac161bf2009-01-02 07:01:27 +00002123 // If this is a forward reference for the value, see if we already created a
2124 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002125 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002126 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2127 I = ForwardRefVals.find(Name);
2128 if (I != ForwardRefVals.end())
2129 Val = I->second.first;
2130 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002131
Chris Lattnerac161bf2009-01-02 07:01:27 +00002132 // If we have the value in the symbol table or fwd-ref table, return it.
2133 if (Val) {
2134 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002135 if (Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002136 P.Error(Loc, "'%" + Name + "' is not a basic block");
2137 else
2138 P.Error(Loc, "'%" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002139 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002140 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002141 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002142
Chris Lattnerac161bf2009-01-02 07:01:27 +00002143 // Don't make placeholders with invalid type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002144 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002145 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002146 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002147 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002148
Chris Lattnerac161bf2009-01-02 07:01:27 +00002149 // Otherwise, create a new forward reference for this value and remember it.
2150 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002151 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002152 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002153 else
2154 FwdVal = new Argument(Ty, Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002155
Chris Lattnerac161bf2009-01-02 07:01:27 +00002156 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
2157 return FwdVal;
2158}
2159
Chris Lattner229907c2011-07-18 04:54:35 +00002160Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00002161 LocTy Loc) {
2162 // Look this name up in the normal function symbol table.
Craig Topper2617dcc2014-04-15 06:32:26 +00002163 Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002164
Chris Lattnerac161bf2009-01-02 07:01:27 +00002165 // If this is a forward reference for the value, see if we already created a
2166 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002167 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002168 std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2169 I = ForwardRefValIDs.find(ID);
2170 if (I != ForwardRefValIDs.end())
2171 Val = I->second.first;
2172 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002173
Chris Lattnerac161bf2009-01-02 07:01:27 +00002174 // If we have the value in the symbol table or fwd-ref table, return it.
2175 if (Val) {
2176 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002177 if (Ty->isLabelTy())
Benjamin Kramerc7583112010-09-27 17:42:11 +00002178 P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002179 else
Benjamin Kramerc7583112010-09-27 17:42:11 +00002180 P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002181 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002182 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002183 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002184
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002185 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002186 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002187 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002188 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002189
Chris Lattnerac161bf2009-01-02 07:01:27 +00002190 // Otherwise, create a new forward reference for this value and remember it.
2191 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002192 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002193 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002194 else
2195 FwdVal = new Argument(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002196
Chris Lattnerac161bf2009-01-02 07:01:27 +00002197 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
2198 return FwdVal;
2199}
2200
2201/// SetInstName - After an instruction is parsed and inserted into its
2202/// basic block, this installs its name.
2203bool LLParser::PerFunctionState::SetInstName(int NameID,
2204 const std::string &NameStr,
2205 LocTy NameLoc, Instruction *Inst) {
2206 // If this instruction has void type, it cannot have a name or ID specified.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002207 if (Inst->getType()->isVoidTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002208 if (NameID != -1 || !NameStr.empty())
2209 return P.Error(NameLoc, "instructions returning void cannot have a name");
2210 return false;
2211 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002212
Chris Lattnerac161bf2009-01-02 07:01:27 +00002213 // If this was a numbered instruction, verify that the instruction is the
2214 // expected value and resolve any forward references.
2215 if (NameStr.empty()) {
2216 // If neither a name nor an ID was specified, just use the next ID.
2217 if (NameID == -1)
2218 NameID = NumberedVals.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002219
Chris Lattnerac161bf2009-01-02 07:01:27 +00002220 if (unsigned(NameID) != NumberedVals.size())
2221 return P.Error(NameLoc, "instruction expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002222 Twine(NumberedVals.size()) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002223
Chris Lattnerac161bf2009-01-02 07:01:27 +00002224 std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI =
2225 ForwardRefValIDs.find(NameID);
2226 if (FI != ForwardRefValIDs.end()) {
2227 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002228 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002229 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002230 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2baa6a32009-09-02 15:02:57 +00002231 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002232 ForwardRefValIDs.erase(FI);
2233 }
2234
2235 NumberedVals.push_back(Inst);
2236 return false;
2237 }
2238
2239 // Otherwise, the instruction had a name. Resolve forward refs and set it.
2240 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2241 FI = ForwardRefVals.find(NameStr);
2242 if (FI != ForwardRefVals.end()) {
2243 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002244 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002245 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002246 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2fcee702009-09-02 14:22:03 +00002247 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002248 ForwardRefVals.erase(FI);
2249 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002250
Chris Lattnerac161bf2009-01-02 07:01:27 +00002251 // Set the name on the instruction.
2252 Inst->setName(NameStr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002253
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00002254 if (Inst->getName() != NameStr)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002255 return P.Error(NameLoc, "multiple definition of local value named '" +
Chris Lattnerac161bf2009-01-02 07:01:27 +00002256 NameStr + "'");
2257 return false;
2258}
2259
2260/// GetBB - Get a basic block with the specified name or ID, creating a
2261/// forward reference record if needed.
2262BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
2263 LocTy Loc) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002264 return cast_or_null<BasicBlock>(GetVal(Name,
2265 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002266}
2267
2268BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002269 return cast_or_null<BasicBlock>(GetVal(ID,
2270 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002271}
2272
2273/// DefineBB - Define the specified basic block, which is either named or
2274/// unnamed. If there is an error, this returns null otherwise it returns
2275/// the block being defined.
2276BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
2277 LocTy Loc) {
2278 BasicBlock *BB;
2279 if (Name.empty())
2280 BB = GetBB(NumberedVals.size(), Loc);
2281 else
2282 BB = GetBB(Name, Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002283 if (!BB) return nullptr; // Already diagnosed error.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002284
Chris Lattnerac161bf2009-01-02 07:01:27 +00002285 // Move the block to the end of the function. Forward ref'd blocks are
2286 // inserted wherever they happen to be referenced.
2287 F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002288
Chris Lattnerac161bf2009-01-02 07:01:27 +00002289 // Remove the block from forward ref sets.
2290 if (Name.empty()) {
2291 ForwardRefValIDs.erase(NumberedVals.size());
2292 NumberedVals.push_back(BB);
2293 } else {
2294 // BB forward references are already in the function symbol table.
2295 ForwardRefVals.erase(Name);
2296 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002297
Chris Lattnerac161bf2009-01-02 07:01:27 +00002298 return BB;
2299}
2300
2301//===----------------------------------------------------------------------===//
2302// Constants.
2303//===----------------------------------------------------------------------===//
2304
2305/// ParseValID - Parse an abstract value that doesn't necessarily have a
2306/// type implied. For example, if we parse "4" we don't know what integer type
2307/// it has. The value will later be combined with its type and checked for
Victor Hernandezb8fd1522010-01-10 07:14:18 +00002308/// sanity. PFS is used to convert function-local operands of metadata (since
2309/// metadata operands are not just parsed here but also converted to values).
2310/// PFS can be null when we are not parsing metadata values inside a function.
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00002311bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002312 ID.Loc = Lex.getLoc();
2313 switch (Lex.getKind()) {
2314 default: return TokError("expected value token");
2315 case lltok::GlobalID: // @42
2316 ID.UIntVal = Lex.getUIntVal();
2317 ID.Kind = ValID::t_GlobalID;
2318 break;
2319 case lltok::GlobalVar: // @foo
2320 ID.StrVal = Lex.getStrVal();
2321 ID.Kind = ValID::t_GlobalName;
2322 break;
2323 case lltok::LocalVarID: // %42
2324 ID.UIntVal = Lex.getUIntVal();
2325 ID.Kind = ValID::t_LocalID;
2326 break;
2327 case lltok::LocalVar: // %foo
Chris Lattnerac161bf2009-01-02 07:01:27 +00002328 ID.StrVal = Lex.getStrVal();
2329 ID.Kind = ValID::t_LocalName;
2330 break;
Dan Gohman8939ba332010-07-14 18:26:50 +00002331 case lltok::exclaim: // !42, !{...}, or !"foo"
2332 return ParseMetadataValue(ID, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002333 case lltok::APSInt:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002334 ID.APSIntVal = Lex.getAPSIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00002335 ID.Kind = ValID::t_APSInt;
2336 break;
2337 case lltok::APFloat:
2338 ID.APFloatVal = Lex.getAPFloatVal();
2339 ID.Kind = ValID::t_APFloat;
2340 break;
2341 case lltok::kw_true:
Owen Anderson23a204d2009-07-31 17:39:07 +00002342 ID.ConstantVal = ConstantInt::getTrue(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002343 ID.Kind = ValID::t_Constant;
2344 break;
2345 case lltok::kw_false:
Owen Anderson23a204d2009-07-31 17:39:07 +00002346 ID.ConstantVal = ConstantInt::getFalse(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002347 ID.Kind = ValID::t_Constant;
2348 break;
2349 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
2350 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
2351 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002352
Chris Lattnerac161bf2009-01-02 07:01:27 +00002353 case lltok::lbrace: {
2354 // ValID ::= '{' ConstVector '}'
2355 Lex.Lex();
2356 SmallVector<Constant*, 16> Elts;
2357 if (ParseGlobalValueVector(Elts) ||
2358 ParseToken(lltok::rbrace, "expected end of struct constant"))
2359 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002360
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002361 ID.ConstantStructElts = new Constant*[Elts.size()];
2362 ID.UIntVal = Elts.size();
2363 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2364 ID.Kind = ValID::t_ConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002365 return false;
2366 }
2367 case lltok::less: {
2368 // ValID ::= '<' ConstVector '>' --> Vector.
2369 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
2370 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +00002371 bool isPackedStruct = EatIfPresent(lltok::lbrace);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002372
Chris Lattnerac161bf2009-01-02 07:01:27 +00002373 SmallVector<Constant*, 16> Elts;
2374 LocTy FirstEltLoc = Lex.getLoc();
2375 if (ParseGlobalValueVector(Elts) ||
2376 (isPackedStruct &&
2377 ParseToken(lltok::rbrace, "expected end of packed struct")) ||
2378 ParseToken(lltok::greater, "expected end of constant"))
2379 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002380
Chris Lattnerac161bf2009-01-02 07:01:27 +00002381 if (isPackedStruct) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002382 ID.ConstantStructElts = new Constant*[Elts.size()];
2383 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2384 ID.UIntVal = Elts.size();
2385 ID.Kind = ValID::t_PackedConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002386 return false;
2387 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002388
Chris Lattnerac161bf2009-01-02 07:01:27 +00002389 if (Elts.empty())
2390 return Error(ID.Loc, "constant vector must not be empty");
2391
Duncan Sands9dff9be2010-02-15 16:12:20 +00002392 if (!Elts[0]->getType()->isIntegerTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002393 !Elts[0]->getType()->isFloatingPointTy() &&
2394 !Elts[0]->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002395 return Error(FirstEltLoc,
Nadav Rotem3924cb02011-12-05 06:29:09 +00002396 "vector elements must have integer, pointer or floating point type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002397
Chris Lattnerac161bf2009-01-02 07:01:27 +00002398 // Verify that all the vector elements have the same type.
2399 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
2400 if (Elts[i]->getType() != Elts[0]->getType())
2401 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002402 "vector element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002403 " is not of type '" + getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002404
Chris Lattner69229312011-02-15 00:14:00 +00002405 ID.ConstantVal = ConstantVector::get(Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002406 ID.Kind = ValID::t_Constant;
2407 return false;
2408 }
2409 case lltok::lsquare: { // Array Constant
2410 Lex.Lex();
2411 SmallVector<Constant*, 16> Elts;
2412 LocTy FirstEltLoc = Lex.getLoc();
2413 if (ParseGlobalValueVector(Elts) ||
2414 ParseToken(lltok::rsquare, "expected end of array constant"))
2415 return true;
2416
2417 // Handle empty element.
2418 if (Elts.empty()) {
2419 // Use undef instead of an array because it's inconvenient to determine
2420 // the element type at this point, there being no elements to examine.
Chris Lattner998fa0a2009-01-05 07:52:51 +00002421 ID.Kind = ValID::t_EmptyArray;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002422 return false;
2423 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002424
Chris Lattnerac161bf2009-01-02 07:01:27 +00002425 if (!Elts[0]->getType()->isFirstClassType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002426 return Error(FirstEltLoc, "invalid array element type: " +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002427 getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002428
Owen Anderson4056ca92009-07-29 22:17:13 +00002429 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002430
Chris Lattnerac161bf2009-01-02 07:01:27 +00002431 // Verify all elements are correct type!
Chris Lattner59d0e3b2009-01-02 08:49:06 +00002432 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002433 if (Elts[i]->getType() != Elts[0]->getType())
2434 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002435 "array element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002436 " is not of type '" + getTypeString(Elts[0]->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002437 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002438
Jay Foad83be3612011-06-22 09:24:39 +00002439 ID.ConstantVal = ConstantArray::get(ATy, Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002440 ID.Kind = ValID::t_Constant;
2441 return false;
2442 }
2443 case lltok::kw_c: // c "foo"
2444 Lex.Lex();
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002445 ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
2446 false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002447 if (ParseToken(lltok::StringConstant, "expected string")) return true;
2448 ID.Kind = ValID::t_Constant;
2449 return false;
2450
2451 case lltok::kw_asm: {
Chad Rosier6f9c3852013-02-14 20:44:07 +00002452 // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
2453 // STRINGCONSTANT
Chad Rosierd8c76102012-09-05 19:00:49 +00002454 bool HasSideEffect, AlignStack, AsmDialect;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002455 Lex.Lex();
2456 if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002457 ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
Chad Rosierd8c76102012-09-05 19:00:49 +00002458 ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
Chris Lattner3822f632009-01-02 08:05:26 +00002459 ParseStringConstant(ID.StrVal) ||
2460 ParseToken(lltok::comma, "expected comma in inline asm expression") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002461 ParseToken(lltok::StringConstant, "expected constraint string"))
2462 return true;
2463 ID.StrVal2 = Lex.getStrVal();
Chad Rosierf42fad62012-09-05 00:08:17 +00002464 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) |
Chad Rosierd8c76102012-09-05 19:00:49 +00002465 (unsigned(AsmDialect)<<2);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002466 ID.Kind = ValID::t_InlineAsm;
2467 return false;
2468 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002469
Chris Lattner3432c622009-10-28 03:39:23 +00002470 case lltok::kw_blockaddress: {
2471 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
2472 Lex.Lex();
2473
2474 ValID Fn, Label;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002475
Chris Lattner3432c622009-10-28 03:39:23 +00002476 if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
2477 ParseValID(Fn) ||
2478 ParseToken(lltok::comma, "expected comma in block address expression")||
2479 ParseValID(Label) ||
2480 ParseToken(lltok::rparen, "expected ')' in block address expression"))
2481 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002482
Chris Lattner3432c622009-10-28 03:39:23 +00002483 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
2484 return Error(Fn.Loc, "expected function name in blockaddress");
Chris Lattneraa99c942009-11-01 01:27:45 +00002485 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
Chris Lattner3432c622009-10-28 03:39:23 +00002486 return Error(Label.Loc, "expected basic block name in blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002487
Chris Lattner3432c622009-10-28 03:39:23 +00002488 // Make a global variable as a placeholder for this reference.
2489 GlobalVariable *FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context),
2490 false, GlobalValue::InternalLinkage,
Craig Topper2617dcc2014-04-15 06:32:26 +00002491 nullptr, "");
Chris Lattner3432c622009-10-28 03:39:23 +00002492 ForwardRefBlockAddresses[Fn].push_back(std::make_pair(Label, FwdRef));
2493 ID.ConstantVal = FwdRef;
2494 ID.Kind = ValID::t_Constant;
2495 return false;
2496 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002497
Chris Lattnerac161bf2009-01-02 07:01:27 +00002498 case lltok::kw_trunc:
2499 case lltok::kw_zext:
2500 case lltok::kw_sext:
2501 case lltok::kw_fptrunc:
2502 case lltok::kw_fpext:
2503 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002504 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002505 case lltok::kw_uitofp:
2506 case lltok::kw_sitofp:
2507 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002508 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002509 case lltok::kw_inttoptr:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002510 case lltok::kw_ptrtoint: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002511 unsigned Opc = Lex.getUIntVal();
Craig Topper2617dcc2014-04-15 06:32:26 +00002512 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002513 Constant *SrcVal;
2514 Lex.Lex();
2515 if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
2516 ParseGlobalTypeAndValue(SrcVal) ||
Dan Gohman0b5d0422009-06-15 21:52:11 +00002517 ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002518 ParseType(DestTy) ||
2519 ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
2520 return true;
2521 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
2522 return Error(ID.Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002523 getTypeString(SrcVal->getType()) + "' to '" +
2524 getTypeString(DestTy) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002525 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
Owen Anderson02a9da32009-07-01 23:57:11 +00002526 SrcVal, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002527 ID.Kind = ValID::t_Constant;
2528 return false;
2529 }
2530 case lltok::kw_extractvalue: {
2531 Lex.Lex();
2532 Constant *Val;
2533 SmallVector<unsigned, 4> Indices;
2534 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
2535 ParseGlobalTypeAndValue(Val) ||
2536 ParseIndexList(Indices) ||
2537 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
2538 return true;
Devang Patel1cb51162009-11-03 19:06:07 +00002539
Chris Lattner392be582010-02-12 20:49:41 +00002540 if (!Val->getType()->isAggregateType())
2541 return Error(ID.Loc, "extractvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002542 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002543 return Error(ID.Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002544 ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002545 ID.Kind = ValID::t_Constant;
2546 return false;
2547 }
2548 case lltok::kw_insertvalue: {
2549 Lex.Lex();
2550 Constant *Val0, *Val1;
2551 SmallVector<unsigned, 4> Indices;
2552 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
2553 ParseGlobalTypeAndValue(Val0) ||
2554 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
2555 ParseGlobalTypeAndValue(Val1) ||
2556 ParseIndexList(Indices) ||
2557 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
2558 return true;
Chris Lattner392be582010-02-12 20:49:41 +00002559 if (!Val0->getType()->isAggregateType())
2560 return Error(ID.Loc, "insertvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002561 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002562 return Error(ID.Loc, "invalid indices for insertvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002563 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002564 ID.Kind = ValID::t_Constant;
2565 return false;
2566 }
2567 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002568 case lltok::kw_fcmp: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002569 unsigned PredVal, Opc = Lex.getUIntVal();
2570 Constant *Val0, *Val1;
2571 Lex.Lex();
2572 if (ParseCmpPredicate(PredVal, Opc) ||
2573 ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
2574 ParseGlobalTypeAndValue(Val0) ||
2575 ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
2576 ParseGlobalTypeAndValue(Val1) ||
2577 ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
2578 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002579
Chris Lattnerac161bf2009-01-02 07:01:27 +00002580 if (Val0->getType() != Val1->getType())
2581 return Error(ID.Loc, "compare operands must have the same type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002582
Chris Lattnerac161bf2009-01-02 07:01:27 +00002583 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002584
Chris Lattnerac161bf2009-01-02 07:01:27 +00002585 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002586 if (!Val0->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002587 return Error(ID.Loc, "fcmp requires floating point operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002588 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002589 } else {
2590 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002591 if (!Val0->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002592 !Val0->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002593 return Error(ID.Loc, "icmp requires pointer or integer operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002594 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002595 }
2596 ID.Kind = ValID::t_Constant;
2597 return false;
2598 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002599
Chris Lattnerac161bf2009-01-02 07:01:27 +00002600 // Binary Operators.
2601 case lltok::kw_add:
Dan Gohmana5b96452009-06-04 22:49:04 +00002602 case lltok::kw_fadd:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002603 case lltok::kw_sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002604 case lltok::kw_fsub:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002605 case lltok::kw_mul:
Dan Gohmana5b96452009-06-04 22:49:04 +00002606 case lltok::kw_fmul:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002607 case lltok::kw_udiv:
2608 case lltok::kw_sdiv:
2609 case lltok::kw_fdiv:
2610 case lltok::kw_urem:
2611 case lltok::kw_srem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002612 case lltok::kw_frem:
2613 case lltok::kw_shl:
2614 case lltok::kw_lshr:
2615 case lltok::kw_ashr: {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002616 bool NUW = false;
2617 bool NSW = false;
2618 bool Exact = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002619 unsigned Opc = Lex.getUIntVal();
2620 Constant *Val0, *Val1;
2621 Lex.Lex();
Dan Gohman9c7f8082009-07-27 16:11:46 +00002622 LocTy ModifierLoc = Lex.getLoc();
Chris Lattnera676c0f2011-02-07 16:40:21 +00002623 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
2624 Opc == Instruction::Mul || Opc == Instruction::Shl) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002625 if (EatIfPresent(lltok::kw_nuw))
2626 NUW = true;
2627 if (EatIfPresent(lltok::kw_nsw)) {
2628 NSW = true;
2629 if (EatIfPresent(lltok::kw_nuw))
2630 NUW = true;
2631 }
Chris Lattnera676c0f2011-02-07 16:40:21 +00002632 } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
2633 Opc == Instruction::LShr || Opc == Instruction::AShr) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002634 if (EatIfPresent(lltok::kw_exact))
2635 Exact = true;
2636 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00002637 if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
2638 ParseGlobalTypeAndValue(Val0) ||
2639 ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
2640 ParseGlobalTypeAndValue(Val1) ||
2641 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
2642 return true;
2643 if (Val0->getType() != Val1->getType())
2644 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002645 if (!Val0->getType()->isIntOrIntVectorTy()) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002646 if (NUW)
2647 return Error(ModifierLoc, "nuw only applies to integer operations");
2648 if (NSW)
2649 return Error(ModifierLoc, "nsw only applies to integer operations");
2650 }
Dan Gohmana2414ea2010-05-03 22:44:19 +00002651 // Check that the type is valid for the operator.
2652 switch (Opc) {
2653 case Instruction::Add:
2654 case Instruction::Sub:
2655 case Instruction::Mul:
2656 case Instruction::UDiv:
2657 case Instruction::SDiv:
2658 case Instruction::URem:
2659 case Instruction::SRem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002660 case Instruction::Shl:
2661 case Instruction::AShr:
2662 case Instruction::LShr:
Dan Gohmana2414ea2010-05-03 22:44:19 +00002663 if (!Val0->getType()->isIntOrIntVectorTy())
2664 return Error(ID.Loc, "constexpr requires integer operands");
2665 break;
2666 case Instruction::FAdd:
2667 case Instruction::FSub:
2668 case Instruction::FMul:
2669 case Instruction::FDiv:
2670 case Instruction::FRem:
2671 if (!Val0->getType()->isFPOrFPVectorTy())
2672 return Error(ID.Loc, "constexpr requires fp operands");
2673 break;
2674 default: llvm_unreachable("Unknown binary operator!");
2675 }
Dan Gohman1b849082009-09-07 23:54:19 +00002676 unsigned Flags = 0;
2677 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2678 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
Chris Lattner35315d02011-02-06 21:44:57 +00002679 if (Exact) Flags |= PossiblyExactOperator::IsExact;
Dan Gohman1b849082009-09-07 23:54:19 +00002680 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
Dan Gohman9c7f8082009-07-27 16:11:46 +00002681 ID.ConstantVal = C;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002682 ID.Kind = ValID::t_Constant;
2683 return false;
2684 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002685
Chris Lattnerac161bf2009-01-02 07:01:27 +00002686 // Logical Operations
Chris Lattnerac161bf2009-01-02 07:01:27 +00002687 case lltok::kw_and:
2688 case lltok::kw_or:
2689 case lltok::kw_xor: {
2690 unsigned Opc = Lex.getUIntVal();
2691 Constant *Val0, *Val1;
2692 Lex.Lex();
2693 if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
2694 ParseGlobalTypeAndValue(Val0) ||
2695 ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
2696 ParseGlobalTypeAndValue(Val1) ||
2697 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
2698 return true;
2699 if (Val0->getType() != Val1->getType())
2700 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002701 if (!Val0->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002702 return Error(ID.Loc,
2703 "constexpr requires integer or integer vector operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002704 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002705 ID.Kind = ValID::t_Constant;
2706 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002707 }
2708
Chris Lattnerac161bf2009-01-02 07:01:27 +00002709 case lltok::kw_getelementptr:
2710 case lltok::kw_shufflevector:
2711 case lltok::kw_insertelement:
2712 case lltok::kw_extractelement:
2713 case lltok::kw_select: {
2714 unsigned Opc = Lex.getUIntVal();
2715 SmallVector<Constant*, 16> Elts;
Dan Gohman1639c392009-07-27 21:53:46 +00002716 bool InBounds = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002717 Lex.Lex();
Dan Gohman1639c392009-07-27 21:53:46 +00002718 if (Opc == Instruction::GetElementPtr)
Dan Gohman16cbbe42009-07-29 15:58:36 +00002719 InBounds = EatIfPresent(lltok::kw_inbounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002720 if (ParseToken(lltok::lparen, "expected '(' in constantexpr") ||
2721 ParseGlobalValueVector(Elts) ||
2722 ParseToken(lltok::rparen, "expected ')' in constantexpr"))
2723 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002724
Chris Lattnerac161bf2009-01-02 07:01:27 +00002725 if (Opc == Instruction::GetElementPtr) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00002726 if (Elts.size() == 0 ||
2727 !Elts[0]->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002728 return Error(ID.Loc, "getelementptr requires pointer operand");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002729
Jay Foaded8db7d2011-07-21 14:31:17 +00002730 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
Jay Foadd1b78492011-07-25 09:48:08 +00002731 if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002732 return Error(ID.Loc, "invalid indices for getelementptr");
Jay Foad2f5fc8c2011-07-21 15:15:37 +00002733 ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], Indices,
2734 InBounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002735 } else if (Opc == Instruction::Select) {
2736 if (Elts.size() != 3)
2737 return Error(ID.Loc, "expected three operands to select");
2738 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
2739 Elts[2]))
2740 return Error(ID.Loc, Reason);
Owen Anderson487375e2009-07-29 18:55:55 +00002741 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002742 } else if (Opc == Instruction::ShuffleVector) {
2743 if (Elts.size() != 3)
2744 return Error(ID.Loc, "expected three operands to shufflevector");
2745 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2746 return Error(ID.Loc, "invalid operands to shufflevector");
Owen Anderson02a9da32009-07-01 23:57:11 +00002747 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002748 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002749 } else if (Opc == Instruction::ExtractElement) {
2750 if (Elts.size() != 2)
2751 return Error(ID.Loc, "expected two operands to extractelement");
2752 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
2753 return Error(ID.Loc, "invalid extractelement operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002754 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002755 } else {
2756 assert(Opc == Instruction::InsertElement && "Unknown opcode");
2757 if (Elts.size() != 3)
2758 return Error(ID.Loc, "expected three operands to insertelement");
2759 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2760 return Error(ID.Loc, "invalid insertelement operands");
Owen Anderson02a9da32009-07-01 23:57:11 +00002761 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002762 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002763 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002764
Chris Lattnerac161bf2009-01-02 07:01:27 +00002765 ID.Kind = ValID::t_Constant;
2766 return false;
2767 }
2768 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002769
Chris Lattnerac161bf2009-01-02 07:01:27 +00002770 Lex.Lex();
2771 return false;
2772}
2773
2774/// ParseGlobalValue - Parse a global value with the specified type.
Chris Lattner229907c2011-07-18 04:54:35 +00002775bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002776 C = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002777 ValID ID;
Craig Topper2617dcc2014-04-15 06:32:26 +00002778 Value *V = nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002779 bool Parsed = ParseValID(ID) ||
Craig Topper2617dcc2014-04-15 06:32:26 +00002780 ConvertValIDToValue(Ty, ID, V, nullptr);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002781 if (V && !(C = dyn_cast<Constant>(V)))
2782 return Error(ID.Loc, "global values must be constants");
2783 return Parsed;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002784}
2785
Victor Hernandez9d75c962010-01-11 22:31:58 +00002786bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002787 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002788 return ParseType(Ty) ||
2789 ParseGlobalValue(Ty, V);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002790}
2791
2792/// ParseGlobalValueVector
2793/// ::= /*empty*/
2794/// ::= TypeAndValue (',' TypeAndValue)*
2795bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts) {
2796 // Empty list.
2797 if (Lex.getKind() == lltok::rbrace ||
2798 Lex.getKind() == lltok::rsquare ||
2799 Lex.getKind() == lltok::greater ||
2800 Lex.getKind() == lltok::rparen)
2801 return false;
2802
2803 Constant *C;
2804 if (ParseGlobalTypeAndValue(C)) return true;
2805 Elts.push_back(C);
2806
2807 while (EatIfPresent(lltok::comma)) {
2808 if (ParseGlobalTypeAndValue(C)) return true;
2809 Elts.push_back(C);
2810 }
2811
2812 return false;
2813}
2814
Dan Gohmanc828c542010-08-24 02:24:03 +00002815bool LLParser::ParseMetadataListValue(ValID &ID, PerFunctionState *PFS) {
2816 assert(Lex.getKind() == lltok::lbrace);
2817 Lex.Lex();
2818
2819 SmallVector<Value*, 16> Elts;
2820 if (ParseMDNodeVector(Elts, PFS) ||
2821 ParseToken(lltok::rbrace, "expected end of metadata node"))
2822 return true;
2823
Jay Foad5514afe2011-04-21 19:59:31 +00002824 ID.MDNodeVal = MDNode::get(Context, Elts);
Dan Gohmanc828c542010-08-24 02:24:03 +00002825 ID.Kind = ValID::t_MDNode;
2826 return false;
2827}
2828
Dan Gohman8939ba332010-07-14 18:26:50 +00002829/// ParseMetadataValue
2830/// ::= !42
2831/// ::= !{...}
2832/// ::= !"string"
2833bool LLParser::ParseMetadataValue(ValID &ID, PerFunctionState *PFS) {
2834 assert(Lex.getKind() == lltok::exclaim);
2835 Lex.Lex();
2836
2837 // MDNode:
2838 // !{ ... }
Dan Gohmanc828c542010-08-24 02:24:03 +00002839 if (Lex.getKind() == lltok::lbrace)
2840 return ParseMetadataListValue(ID, PFS);
Dan Gohman8939ba332010-07-14 18:26:50 +00002841
2842 // Standalone metadata reference
2843 // !42
2844 if (Lex.getKind() == lltok::APSInt) {
2845 if (ParseMDNodeID(ID.MDNodeVal)) return true;
2846 ID.Kind = ValID::t_MDNode;
2847 return false;
2848 }
2849
2850 // MDString:
2851 // ::= '!' STRINGCONSTANT
2852 if (ParseMDString(ID.MDStringVal)) return true;
2853 ID.Kind = ValID::t_MDString;
2854 return false;
2855}
2856
Victor Hernandez9d75c962010-01-11 22:31:58 +00002857
2858//===----------------------------------------------------------------------===//
2859// Function Parsing.
2860//===----------------------------------------------------------------------===//
2861
Chris Lattner229907c2011-07-18 04:54:35 +00002862bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez9d75c962010-01-11 22:31:58 +00002863 PerFunctionState *PFS) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002864 if (Ty->isFunctionTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002865 return Error(ID.Loc, "functions are not values, refer to them as pointers");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002866
Chris Lattnerac161bf2009-01-02 07:01:27 +00002867 switch (ID.Kind) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002868 case ValID::t_LocalID:
Victor Hernandez9d75c962010-01-11 22:31:58 +00002869 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2870 V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002871 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002872 case ValID::t_LocalName:
Victor Hernandez9d75c962010-01-11 22:31:58 +00002873 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2874 V = PFS->GetVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002875 return V == nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002876 case ValID::t_InlineAsm: {
Chris Lattner229907c2011-07-18 04:54:35 +00002877 PointerType *PTy = dyn_cast<PointerType>(Ty);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002878 FunctionType *FTy =
Craig Topper2617dcc2014-04-15 06:32:26 +00002879 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002880 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
2881 return Error(ID.Loc, "invalid type for inline asm constraint string");
Chad Rosierf42fad62012-09-05 00:08:17 +00002882 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1,
Chad Rosierd8c76102012-09-05 19:00:49 +00002883 (ID.UIntVal>>1)&1, (InlineAsm::AsmDialect(ID.UIntVal>>2)));
Victor Hernandez9d75c962010-01-11 22:31:58 +00002884 return false;
2885 }
2886 case ValID::t_MDNode:
2887 if (!Ty->isMetadataTy())
2888 return Error(ID.Loc, "metadata value must have metadata type");
2889 V = ID.MDNodeVal;
2890 return false;
2891 case ValID::t_MDString:
2892 if (!Ty->isMetadataTy())
2893 return Error(ID.Loc, "metadata value must have metadata type");
2894 V = ID.MDStringVal;
2895 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002896 case ValID::t_GlobalName:
2897 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002898 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002899 case ValID::t_GlobalID:
2900 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002901 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002902 case ValID::t_APSInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002903 if (!Ty->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002904 return Error(ID.Loc, "integer constant must have integer type");
Jay Foad583abbc2010-12-07 08:25:19 +00002905 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
Owen Andersonedb4a702009-07-24 23:12:02 +00002906 V = ConstantInt::get(Context, ID.APSIntVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002907 return false;
2908 case ValID::t_APFloat:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002909 if (!Ty->isFloatingPointTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002910 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
2911 return Error(ID.Loc, "floating point constant invalid for type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002912
Dan Gohman518cda42011-12-17 00:04:22 +00002913 // The lexer has no type info, so builds all half, float, and double FP
2914 // constants as double. Fix this here. Long double does not need this.
2915 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002916 bool Ignored;
Dan Gohman518cda42011-12-17 00:04:22 +00002917 if (Ty->isHalfTy())
2918 ID.APFloatVal.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven,
2919 &Ignored);
2920 else if (Ty->isFloatTy())
2921 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
2922 &Ignored);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002923 }
Owen Anderson69c464d2009-07-27 20:59:43 +00002924 V = ConstantFP::get(Context, ID.APFloatVal);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002925
Chris Lattner8f57d29e2009-01-05 18:24:23 +00002926 if (V->getType() != Ty)
2927 return Error(ID.Loc, "floating point constant does not have type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002928 getTypeString(Ty) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002929
Chris Lattnerac161bf2009-01-02 07:01:27 +00002930 return false;
2931 case ValID::t_Null:
Duncan Sands19d0b472010-02-16 11:11:14 +00002932 if (!Ty->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002933 return Error(ID.Loc, "null must be a pointer type");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002934 V = ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002935 return false;
2936 case ValID::t_Undef:
Chris Lattnerffa07782009-01-05 08:13:38 +00002937 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002938 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerffa07782009-01-05 08:13:38 +00002939 return Error(ID.Loc, "invalid type for undef constant");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002940 V = UndefValue::get(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002941 return false;
Chris Lattner998fa0a2009-01-05 07:52:51 +00002942 case ValID::t_EmptyArray:
Duncan Sands19d0b472010-02-16 11:11:14 +00002943 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
Chris Lattner998fa0a2009-01-05 07:52:51 +00002944 return Error(ID.Loc, "invalid empty array initializer");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002945 V = UndefValue::get(Ty);
Chris Lattner998fa0a2009-01-05 07:52:51 +00002946 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002947 case ValID::t_Zero:
Chris Lattnerffa07782009-01-05 08:13:38 +00002948 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002949 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002950 return Error(ID.Loc, "invalid type for null constant");
Owen Anderson5a1acd92009-07-31 20:28:14 +00002951 V = Constant::getNullValue(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002952 return false;
2953 case ValID::t_Constant:
Chris Lattner13ee7952010-08-28 04:09:24 +00002954 if (ID.ConstantVal->getType() != Ty)
Chris Lattnerac161bf2009-01-02 07:01:27 +00002955 return Error(ID.Loc, "constant expression type mismatch");
Chris Lattner392be582010-02-12 20:49:41 +00002956
Chris Lattnerac161bf2009-01-02 07:01:27 +00002957 V = ID.ConstantVal;
2958 return false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002959 case ValID::t_ConstantStruct:
2960 case ValID::t_PackedConstantStruct:
Chris Lattner229907c2011-07-18 04:54:35 +00002961 if (StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002962 if (ST->getNumElements() != ID.UIntVal)
2963 return Error(ID.Loc,
2964 "initializer with struct type has wrong # elements");
2965 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
2966 return Error(ID.Loc, "packed'ness of initializer and type don't match");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002967
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002968 // Verify that the elements are compatible with the structtype.
2969 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
2970 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
2971 return Error(ID.Loc, "element " + Twine(i) +
2972 " of struct initializer doesn't match struct element type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002973
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002974 V = ConstantStruct::get(ST, makeArrayRef(ID.ConstantStructElts,
2975 ID.UIntVal));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002976 } else
2977 return Error(ID.Loc, "constant expression type mismatch");
2978 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002979 }
Chandler Carruthf3e85022012-01-10 18:08:01 +00002980 llvm_unreachable("Invalid ValID");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002981}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002982
Chris Lattner229907c2011-07-18 04:54:35 +00002983bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002984 V = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002985 ValID ID;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002986 return ParseValID(ID, PFS) ||
2987 ConvertValIDToValue(Ty, ID, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002988}
2989
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002990bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002991 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002992 return ParseType(Ty) ||
2993 ParseValue(Ty, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002994}
2995
Chris Lattner3ed871f2009-10-27 19:13:16 +00002996bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
2997 PerFunctionState &PFS) {
2998 Value *V;
2999 Loc = Lex.getLoc();
3000 if (ParseTypeAndValue(V, PFS)) return true;
3001 if (!isa<BasicBlock>(V))
3002 return Error(Loc, "expected a basic block");
3003 BB = cast<BasicBlock>(V);
3004 return false;
3005}
3006
3007
Chris Lattnerac161bf2009-01-02 07:01:27 +00003008/// FunctionHeader
3009/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
Rafael Espindola45e6c192011-01-08 16:42:36 +00003010/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003011/// OptionalAlign OptGC OptionalPrefix
Chris Lattnerac161bf2009-01-02 07:01:27 +00003012bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
3013 // Parse the linkage.
3014 LocTy LinkageLoc = Lex.getLoc();
3015 unsigned Linkage;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003016
Kostya Serebryanya5054ad2012-01-20 17:56:17 +00003017 unsigned Visibility;
Nico Rieck7157bb72014-01-14 15:22:47 +00003018 unsigned DLLStorageClass;
Bill Wendling50d27842012-10-15 20:35:56 +00003019 AttrBuilder RetAttrs;
Sandeep Patel68c5f472009-09-02 08:44:58 +00003020 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00003021 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003022 LocTy RetTypeLoc = Lex.getLoc();
3023 if (ParseOptionalLinkage(Linkage) ||
3024 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +00003025 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003026 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00003027 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00003028 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003029 return true;
3030
3031 // Verify that the linkage is ok.
3032 switch ((GlobalValue::LinkageTypes)Linkage) {
3033 case GlobalValue::ExternalLinkage:
3034 break; // always ok.
Duncan Sandse2881052009-03-11 08:08:06 +00003035 case GlobalValue::ExternalWeakLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003036 if (isDefine)
3037 return Error(LinkageLoc, "invalid linkage for function definition");
3038 break;
Rafael Espindola6de96a12009-01-15 20:18:42 +00003039 case GlobalValue::PrivateLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003040 case GlobalValue::InternalLinkage:
Nick Lewycky8019af62009-04-13 07:02:02 +00003041 case GlobalValue::AvailableExternallyLinkage:
Duncan Sands12da8ce2009-03-07 15:45:40 +00003042 case GlobalValue::LinkOnceAnyLinkage:
3043 case GlobalValue::LinkOnceODRLinkage:
3044 case GlobalValue::WeakAnyLinkage:
3045 case GlobalValue::WeakODRLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003046 if (!isDefine)
3047 return Error(LinkageLoc, "invalid linkage for function declaration");
3048 break;
3049 case GlobalValue::AppendingLinkage:
Duncan Sands4581beb2009-03-11 20:14:15 +00003050 case GlobalValue::CommonLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003051 return Error(LinkageLoc, "invalid function linkage type");
3052 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003053
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00003054 if (!isValidVisibilityForLinkage(Visibility, Linkage))
3055 return Error(LinkageLoc,
3056 "symbol with local linkage must have default visibility");
3057
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003058 if (!FunctionType::isValidReturnType(RetType))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003059 return Error(RetTypeLoc, "invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003060
Chris Lattnerac161bf2009-01-02 07:01:27 +00003061 LocTy NameLoc = Lex.getLoc();
Chris Lattner778c62c2009-02-18 21:48:13 +00003062
3063 std::string FunctionName;
3064 if (Lex.getKind() == lltok::GlobalVar) {
3065 FunctionName = Lex.getStrVal();
3066 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
3067 unsigned NameID = Lex.getUIntVal();
3068
3069 if (NameID != NumberedVals.size())
3070 return TokError("function expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00003071 Twine(NumberedVals.size()) + "'");
Chris Lattner778c62c2009-02-18 21:48:13 +00003072 } else {
3073 return TokError("expected function name");
3074 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003075
Chris Lattner3822f632009-01-02 08:05:26 +00003076 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003077
Chris Lattner3822f632009-01-02 08:05:26 +00003078 if (Lex.getKind() != lltok::lparen)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003079 return TokError("expected '(' in function argument list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003080
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003081 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003082 bool isVarArg;
Bill Wendling50d27842012-10-15 20:35:56 +00003083 AttrBuilder FuncAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003084 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00003085 LocTy BuiltinLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003086 std::string Section;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003087 unsigned Alignment;
Chris Lattner3822f632009-01-02 08:05:26 +00003088 std::string GC;
Rafael Espindola563eb4b2011-01-25 19:09:56 +00003089 bool UnnamedAddr;
3090 LocTy UnnamedAddrLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00003091 Constant *Prefix = nullptr;
Chris Lattner3822f632009-01-02 08:05:26 +00003092
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003093 if (ParseArgumentList(ArgList, isVarArg) ||
Rafael Espindola563eb4b2011-01-25 19:09:56 +00003094 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
3095 &UnnamedAddrLoc) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003096 ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00003097 BuiltinLoc) ||
Chris Lattner3822f632009-01-02 08:05:26 +00003098 (EatIfPresent(lltok::kw_section) &&
3099 ParseStringConstant(Section)) ||
3100 ParseOptionalAlignment(Alignment) ||
3101 (EatIfPresent(lltok::kw_gc) &&
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003102 ParseStringConstant(GC)) ||
3103 (EatIfPresent(lltok::kw_prefix) &&
3104 ParseGlobalTypeAndValue(Prefix)))
Chris Lattner3822f632009-01-02 08:05:26 +00003105 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003106
Michael Gottesman41748d72013-06-27 00:25:01 +00003107 if (FuncAttrs.contains(Attribute::Builtin))
3108 return Error(BuiltinLoc, "'builtin' attribute not valid on function");
Bill Wendling09bd1f72013-02-22 00:12:35 +00003109
Chris Lattnerac161bf2009-01-02 07:01:27 +00003110 // If the alignment was parsed as an attribute, move to the alignment field.
Bill Wendlingc6daefa2012-10-08 23:27:46 +00003111 if (FuncAttrs.hasAlignmentAttr()) {
Bill Wendling9be77592012-09-21 15:26:31 +00003112 Alignment = FuncAttrs.getAlignment();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003113 FuncAttrs.removeAttribute(Attribute::Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003114 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003115
Chris Lattnerac161bf2009-01-02 07:01:27 +00003116 // Okay, if we got here, the function is syntactically valid. Convert types
3117 // and do semantic checks.
Jay Foadb804a2b2011-07-12 14:06:48 +00003118 std::vector<Type*> ParamTypeList;
Bill Wendlingf5075a42013-01-27 02:24:02 +00003119 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003120
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003121 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003122 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3123 AttributeSet::ReturnIndex,
3124 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003125
Chris Lattnerac161bf2009-01-02 07:01:27 +00003126 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003127 ParamTypeList.push_back(ArgList[i].Ty);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00003128 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3129 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00003130 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3131 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003132 }
3133
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003134 if (FuncAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003135 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3136 AttributeSet::FunctionIndex,
3137 FuncAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003138
Bill Wendlinge94d8432012-12-07 23:16:57 +00003139 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003140
Bill Wendling749a43d2012-12-30 13:50:49 +00003141 if (PAL.hasAttribute(1, Attribute::StructRet) && !RetType->isVoidTy())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003142 return Error(RetTypeLoc, "functions with 'sret' argument must return void");
3143
Chris Lattner229907c2011-07-18 04:54:35 +00003144 FunctionType *FT =
Owen Anderson4056ca92009-07-29 22:17:13 +00003145 FunctionType::get(RetType, ParamTypeList, isVarArg);
Chris Lattner229907c2011-07-18 04:54:35 +00003146 PointerType *PFT = PointerType::getUnqual(FT);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003147
Craig Topper2617dcc2014-04-15 06:32:26 +00003148 Fn = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003149 if (!FunctionName.empty()) {
3150 // If this was a definition of a forward reference, remove the definition
3151 // from the forward reference table and fill in the forward ref.
3152 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI =
3153 ForwardRefVals.find(FunctionName);
3154 if (FRVI != ForwardRefVals.end()) {
3155 Fn = M->getFunction(FunctionName);
Nick Lewycky686d7cb2012-10-11 00:38:25 +00003156 if (!Fn)
3157 return Error(FRVI->second.second, "invalid forward reference to "
3158 "function as global value!");
Chris Lattnerc239eb72010-04-20 04:49:11 +00003159 if (Fn->getType() != PFT)
3160 return Error(FRVI->second.second, "invalid forward reference to "
3161 "function '" + FunctionName + "' with wrong type!");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003162
Chris Lattnerac161bf2009-01-02 07:01:27 +00003163 ForwardRefVals.erase(FRVI);
3164 } else if ((Fn = M->getFunction(FunctionName))) {
Chris Lattner5756c162011-06-17 07:06:44 +00003165 // Reject redefinitions.
3166 return Error(NameLoc, "invalid redefinition of function '" +
3167 FunctionName + "'");
Chris Lattnere38317f2009-10-25 23:22:50 +00003168 } else if (M->getNamedValue(FunctionName)) {
3169 return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003170 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003171
Dan Gohman399d6ae2009-08-29 23:37:49 +00003172 } else {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003173 // If this is a definition of a forward referenced function, make sure the
3174 // types agree.
3175 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I
3176 = ForwardRefValIDs.find(NumberedVals.size());
3177 if (I != ForwardRefValIDs.end()) {
3178 Fn = cast<Function>(I->second.first);
3179 if (Fn->getType() != PFT)
3180 return Error(NameLoc, "type of definition and forward reference of '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00003181 Twine(NumberedVals.size()) + "' disagree");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003182 ForwardRefValIDs.erase(I);
3183 }
3184 }
3185
Craig Topper2617dcc2014-04-15 06:32:26 +00003186 if (!Fn)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003187 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
3188 else // Move the forward-reference to the correct spot in the module.
3189 M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
3190
3191 if (FunctionName.empty())
3192 NumberedVals.push_back(Fn);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003193
Chris Lattnerac161bf2009-01-02 07:01:27 +00003194 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
3195 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +00003196 Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003197 Fn->setCallingConv(CC);
3198 Fn->setAttributes(PAL);
Rafael Espindola45e6c192011-01-08 16:42:36 +00003199 Fn->setUnnamedAddr(UnnamedAddr);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003200 Fn->setAlignment(Alignment);
3201 Fn->setSection(Section);
3202 if (!GC.empty()) Fn->setGC(GC.c_str());
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003203 Fn->setPrefixData(Prefix);
Bill Wendlingb32b0412013-02-08 06:32:06 +00003204 ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003205
Chris Lattnerac161bf2009-01-02 07:01:27 +00003206 // Add all of the arguments we parsed to the function.
3207 Function::arg_iterator ArgIt = Fn->arg_begin();
3208 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
3209 // If the argument has a name, insert it into the argument symbol table.
3210 if (ArgList[i].Name.empty()) continue;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003211
Chris Lattnerac161bf2009-01-02 07:01:27 +00003212 // Set the name, if it conflicted, it will be auto-renamed.
3213 ArgIt->setName(ArgList[i].Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003214
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00003215 if (ArgIt->getName() != ArgList[i].Name)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003216 return Error(ArgList[i].Loc, "redefinition of argument '%" +
3217 ArgList[i].Name + "'");
3218 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003219
Chris Lattnerac161bf2009-01-02 07:01:27 +00003220 return false;
3221}
3222
3223
3224/// ParseFunctionBody
3225/// ::= '{' BasicBlock+ '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00003226///
3227bool LLParser::ParseFunctionBody(Function &Fn) {
Chris Lattner4649a732011-06-17 06:42:57 +00003228 if (Lex.getKind() != lltok::lbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003229 return TokError("expected '{' in function body");
3230 Lex.Lex(); // eat the {.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003231
Chris Lattner3432c622009-10-28 03:39:23 +00003232 int FunctionNumber = -1;
3233 if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003234
Chris Lattner3432c622009-10-28 03:39:23 +00003235 PerFunctionState PFS(*this, Fn, FunctionNumber);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003236
Chris Lattnerbbddd962010-01-09 19:20:07 +00003237 // We need at least one basic block.
Chris Lattner4649a732011-06-17 06:42:57 +00003238 if (Lex.getKind() == lltok::rbrace)
Chris Lattnerbbddd962010-01-09 19:20:07 +00003239 return TokError("function body requires at least one basic block");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003240
Chris Lattner4649a732011-06-17 06:42:57 +00003241 while (Lex.getKind() != lltok::rbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003242 if (ParseBasicBlock(PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003243
Chris Lattnerac161bf2009-01-02 07:01:27 +00003244 // Eat the }.
3245 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003246
Chris Lattnerac161bf2009-01-02 07:01:27 +00003247 // Verify function is ok.
Chris Lattner3432c622009-10-28 03:39:23 +00003248 return PFS.FinishFunction();
Chris Lattnerac161bf2009-01-02 07:01:27 +00003249}
3250
3251/// ParseBasicBlock
3252/// ::= LabelStr? Instruction*
3253bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
3254 // If this basic block starts out with a name, remember it.
3255 std::string Name;
3256 LocTy NameLoc = Lex.getLoc();
3257 if (Lex.getKind() == lltok::LabelStr) {
3258 Name = Lex.getStrVal();
3259 Lex.Lex();
3260 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003261
Chris Lattnerac161bf2009-01-02 07:01:27 +00003262 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003263 if (!BB) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003264
Chris Lattnerac161bf2009-01-02 07:01:27 +00003265 std::string NameStr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003266
Chris Lattnerac161bf2009-01-02 07:01:27 +00003267 // Parse the instructions in this block until we get a terminator.
3268 Instruction *Inst;
3269 do {
3270 // This instruction may have three possibilities for a name: a) none
3271 // specified, b) name specified "%foo =", c) number specified: "%4 =".
3272 LocTy NameLoc = Lex.getLoc();
3273 int NameID = -1;
3274 NameStr = "";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003275
Chris Lattnerac161bf2009-01-02 07:01:27 +00003276 if (Lex.getKind() == lltok::LocalVarID) {
3277 NameID = Lex.getUIntVal();
3278 Lex.Lex();
3279 if (ParseToken(lltok::equal, "expected '=' after instruction id"))
3280 return true;
Chris Lattnerdef19492011-06-17 06:36:20 +00003281 } else if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003282 NameStr = Lex.getStrVal();
3283 Lex.Lex();
3284 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
3285 return true;
3286 }
Devang Patelea8a4b92009-09-17 23:04:48 +00003287
Chris Lattner77b89dc2009-12-30 05:23:43 +00003288 switch (ParseInstruction(Inst, BB, PFS)) {
Craig Toppera2886c22012-02-07 05:05:23 +00003289 default: llvm_unreachable("Unknown ParseInstruction result!");
Chris Lattner77b89dc2009-12-30 05:23:43 +00003290 case InstError: return true;
3291 case InstNormal:
Chris Lattner2e664bd2010-04-07 04:08:57 +00003292 BB->getInstList().push_back(Inst);
3293
Chris Lattner77b89dc2009-12-30 05:23:43 +00003294 // With a normal result, we check to see if the instruction is followed by
3295 // a comma and metadata.
3296 if (EatIfPresent(lltok::comma))
Dan Gohman338d9a42010-08-24 02:05:17 +00003297 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattner77b89dc2009-12-30 05:23:43 +00003298 return true;
3299 break;
3300 case InstExtraComma:
Chris Lattner2e664bd2010-04-07 04:08:57 +00003301 BB->getInstList().push_back(Inst);
3302
Chris Lattner77b89dc2009-12-30 05:23:43 +00003303 // If the instruction parser ate an extra comma at the end of it, it
3304 // *must* be followed by metadata.
Dan Gohman338d9a42010-08-24 02:05:17 +00003305 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattner77b89dc2009-12-30 05:23:43 +00003306 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003307 break;
Chris Lattner77b89dc2009-12-30 05:23:43 +00003308 }
Devang Patelea8a4b92009-09-17 23:04:48 +00003309
Chris Lattnerac161bf2009-01-02 07:01:27 +00003310 // Set the name on the instruction.
3311 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
3312 } while (!isa<TerminatorInst>(Inst));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003313
Chris Lattnerac161bf2009-01-02 07:01:27 +00003314 return false;
3315}
3316
3317//===----------------------------------------------------------------------===//
3318// Instruction Parsing.
3319//===----------------------------------------------------------------------===//
3320
3321/// ParseInstruction - Parse one of the many different instructions.
3322///
Chris Lattner77b89dc2009-12-30 05:23:43 +00003323int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
3324 PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003325 lltok::Kind Token = Lex.getKind();
3326 if (Token == lltok::Eof)
3327 return TokError("found end of file when expecting more instructions");
3328 LocTy Loc = Lex.getLoc();
Chris Lattner89d856e2009-03-01 00:53:13 +00003329 unsigned KeywordVal = Lex.getUIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00003330 Lex.Lex(); // Eat the keyword.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003331
Chris Lattnerac161bf2009-01-02 07:01:27 +00003332 switch (Token) {
3333 default: return Error(Loc, "expected instruction opcode");
3334 // Terminator Instructions.
Owen Anderson55f1c092009-08-13 21:58:54 +00003335 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003336 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
3337 case lltok::kw_br: return ParseBr(Inst, PFS);
3338 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003339 case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003340 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +00003341 case lltok::kw_resume: return ParseResume(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003342 // Binary Operators.
3343 case lltok::kw_add:
3344 case lltok::kw_sub:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003345 case lltok::kw_mul:
3346 case lltok::kw_shl: {
Chris Lattnera676c0f2011-02-07 16:40:21 +00003347 bool NUW = EatIfPresent(lltok::kw_nuw);
3348 bool NSW = EatIfPresent(lltok::kw_nsw);
3349 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003350
Chris Lattnera676c0f2011-02-07 16:40:21 +00003351 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003352
Chris Lattnera676c0f2011-02-07 16:40:21 +00003353 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
3354 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
3355 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00003356 }
Dan Gohmana5b96452009-06-04 22:49:04 +00003357 case lltok::kw_fadd:
3358 case lltok::kw_fsub:
Michael Ilseman92053172012-11-27 00:42:44 +00003359 case lltok::kw_fmul:
3360 case lltok::kw_fdiv:
3361 case lltok::kw_frem: {
3362 FastMathFlags FMF = EatFastMathFlagsIfPresent();
3363 int Res = ParseArithmetic(Inst, PFS, KeywordVal, 2);
3364 if (Res != 0)
3365 return Res;
3366 if (FMF.any())
3367 Inst->setFastMathFlags(FMF);
3368 return 0;
3369 }
Dan Gohmana5b96452009-06-04 22:49:04 +00003370
Chris Lattner35315d02011-02-06 21:44:57 +00003371 case lltok::kw_sdiv:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003372 case lltok::kw_udiv:
3373 case lltok::kw_lshr:
3374 case lltok::kw_ashr: {
3375 bool Exact = EatIfPresent(lltok::kw_exact);
3376
3377 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
3378 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
3379 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00003380 }
3381
Chris Lattnerac161bf2009-01-02 07:01:27 +00003382 case lltok::kw_urem:
Chris Lattner89d856e2009-03-01 00:53:13 +00003383 case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003384 case lltok::kw_and:
3385 case lltok::kw_or:
Chris Lattner89d856e2009-03-01 00:53:13 +00003386 case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003387 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003388 case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003389 // Casts.
3390 case lltok::kw_trunc:
3391 case lltok::kw_zext:
3392 case lltok::kw_sext:
3393 case lltok::kw_fptrunc:
3394 case lltok::kw_fpext:
3395 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003396 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003397 case lltok::kw_uitofp:
3398 case lltok::kw_sitofp:
3399 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003400 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003401 case lltok::kw_inttoptr:
Chris Lattner89d856e2009-03-01 00:53:13 +00003402 case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003403 // Other.
3404 case lltok::kw_select: return ParseSelect(Inst, PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +00003405 case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003406 case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
3407 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
3408 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
3409 case lltok::kw_phi: return ParsePHI(Inst, PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +00003410 case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS);
Reid Kleckner5772b772014-04-24 20:14:34 +00003411 // Call.
3412 case lltok::kw_call: return ParseCall(Inst, PFS, CallInst::TCK_None);
3413 case lltok::kw_tail: return ParseCall(Inst, PFS, CallInst::TCK_Tail);
3414 case lltok::kw_musttail: return ParseCall(Inst, PFS, CallInst::TCK_MustTail);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003415 // Memory.
Victor Hernandezc7d6a832009-10-17 00:00:19 +00003416 case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +00003417 case lltok::kw_load: return ParseLoad(Inst, PFS);
3418 case lltok::kw_store: return ParseStore(Inst, PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +00003419 case lltok::kw_cmpxchg: return ParseCmpXchg(Inst, PFS);
3420 case lltok::kw_atomicrmw: return ParseAtomicRMW(Inst, PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +00003421 case lltok::kw_fence: return ParseFence(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003422 case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
3423 case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS);
3424 case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS);
3425 }
3426}
3427
3428/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
3429bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003430 if (Opc == Instruction::FCmp) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003431 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00003432 default: return TokError("expected fcmp predicate (e.g. 'oeq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003433 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
3434 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
3435 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
3436 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
3437 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
3438 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
3439 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
3440 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
3441 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
3442 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
3443 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
3444 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
3445 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
3446 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
3447 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
3448 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
3449 }
3450 } else {
3451 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00003452 default: return TokError("expected icmp predicate (e.g. 'eq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003453 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
3454 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
3455 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
3456 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
3457 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
3458 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
3459 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
3460 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
3461 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
3462 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
3463 }
3464 }
3465 Lex.Lex();
3466 return false;
3467}
3468
3469//===----------------------------------------------------------------------===//
3470// Terminator Instructions.
3471//===----------------------------------------------------------------------===//
3472
3473/// ParseRet - Parse a return instruction.
Chris Lattner596760d2009-12-29 21:25:40 +00003474/// ::= 'ret' void (',' !dbg, !1)*
3475/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
Chris Lattner33de4272011-06-17 06:49:41 +00003476bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003477 PerFunctionState &PFS) {
3478 SMLoc TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00003479 Type *Ty = nullptr;
Chris Lattnerf880ca22009-03-09 04:49:14 +00003480 if (ParseType(Ty, true /*void allowed*/)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003481
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003482 Type *ResType = PFS.getFunction().getReturnType();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003483
Chris Lattnerfdd87902009-10-05 05:54:46 +00003484 if (Ty->isVoidTy()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003485 if (!ResType->isVoidTy())
3486 return Error(TypeLoc, "value doesn't match function result type '" +
3487 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003488
Owen Anderson55f1c092009-08-13 21:58:54 +00003489 Inst = ReturnInst::Create(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003490 return false;
3491 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003492
Chris Lattnerac161bf2009-01-02 07:01:27 +00003493 Value *RV;
3494 if (ParseValue(Ty, RV, PFS)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003495
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003496 if (ResType != RV->getType())
3497 return Error(TypeLoc, "value doesn't match function result type '" +
3498 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003499
Owen Anderson55f1c092009-08-13 21:58:54 +00003500 Inst = ReturnInst::Create(Context, RV);
Chris Lattner33de4272011-06-17 06:49:41 +00003501 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003502}
3503
3504
3505/// ParseBr
3506/// ::= 'br' TypeAndValue
3507/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3508bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
3509 LocTy Loc, Loc2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003510 Value *Op0;
3511 BasicBlock *Op1, *Op2;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003512 if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003513
Chris Lattnerac161bf2009-01-02 07:01:27 +00003514 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
3515 Inst = BranchInst::Create(BB);
3516 return false;
3517 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003518
Owen Anderson55f1c092009-08-13 21:58:54 +00003519 if (Op0->getType() != Type::getInt1Ty(Context))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003520 return Error(Loc, "branch condition must have 'i1' type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003521
Chris Lattnerac161bf2009-01-02 07:01:27 +00003522 if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003523 ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003524 ParseToken(lltok::comma, "expected ',' after true destination") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003525 ParseTypeAndBasicBlock(Op2, Loc2, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003526 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003527
Chris Lattner3ed871f2009-10-27 19:13:16 +00003528 Inst = BranchInst::Create(Op1, Op2, Op0);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003529 return false;
3530}
3531
3532/// ParseSwitch
3533/// Instruction
3534/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
3535/// JumpTable
3536/// ::= (TypeAndValue ',' TypeAndValue)*
3537bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
3538 LocTy CondLoc, BBLoc;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003539 Value *Cond;
3540 BasicBlock *DefaultBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003541 if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
3542 ParseToken(lltok::comma, "expected ',' after switch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003543 ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003544 ParseToken(lltok::lsquare, "expected '[' with switch table"))
3545 return true;
3546
Duncan Sands19d0b472010-02-16 11:11:14 +00003547 if (!Cond->getType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003548 return Error(CondLoc, "switch condition must have integer type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003549
Chris Lattnerac161bf2009-01-02 07:01:27 +00003550 // Parse the jump table pairs.
3551 SmallPtrSet<Value*, 32> SeenCases;
3552 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
3553 while (Lex.getKind() != lltok::rsquare) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003554 Value *Constant;
3555 BasicBlock *DestBB;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003556
Chris Lattnerac161bf2009-01-02 07:01:27 +00003557 if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
3558 ParseToken(lltok::comma, "expected ',' after case value") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003559 ParseTypeAndBasicBlock(DestBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003560 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003561
Chris Lattnerac161bf2009-01-02 07:01:27 +00003562 if (!SeenCases.insert(Constant))
3563 return Error(CondLoc, "duplicate case value in switch");
3564 if (!isa<ConstantInt>(Constant))
3565 return Error(CondLoc, "case value is not a constant integer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003566
Chris Lattner3ed871f2009-10-27 19:13:16 +00003567 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003568 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003569
Chris Lattnerac161bf2009-01-02 07:01:27 +00003570 Lex.Lex(); // Eat the ']'.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003571
Chris Lattner3ed871f2009-10-27 19:13:16 +00003572 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00003573 for (unsigned i = 0, e = Table.size(); i != e; ++i)
3574 SI->addCase(Table[i].first, Table[i].second);
3575 Inst = SI;
3576 return false;
3577}
3578
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003579/// ParseIndirectBr
Chris Lattner3ed871f2009-10-27 19:13:16 +00003580/// Instruction
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003581/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
3582bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003583 LocTy AddrLoc;
3584 Value *Address;
3585 if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003586 ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
3587 ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
Chris Lattner3ed871f2009-10-27 19:13:16 +00003588 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003589
Duncan Sands19d0b472010-02-16 11:11:14 +00003590 if (!Address->getType()->isPointerTy())
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003591 return Error(AddrLoc, "indirectbr address must have pointer type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003592
Chris Lattner3ed871f2009-10-27 19:13:16 +00003593 // Parse the destination list.
3594 SmallVector<BasicBlock*, 16> DestList;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003595
Chris Lattner3ed871f2009-10-27 19:13:16 +00003596 if (Lex.getKind() != lltok::rsquare) {
3597 BasicBlock *DestBB;
3598 if (ParseTypeAndBasicBlock(DestBB, PFS))
3599 return true;
3600 DestList.push_back(DestBB);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003601
Chris Lattner3ed871f2009-10-27 19:13:16 +00003602 while (EatIfPresent(lltok::comma)) {
3603 if (ParseTypeAndBasicBlock(DestBB, PFS))
3604 return true;
3605 DestList.push_back(DestBB);
3606 }
3607 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003608
Chris Lattner3ed871f2009-10-27 19:13:16 +00003609 if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
3610 return true;
3611
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003612 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
Chris Lattner3ed871f2009-10-27 19:13:16 +00003613 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
3614 IBI->addDestination(DestList[i]);
3615 Inst = IBI;
3616 return false;
3617}
3618
3619
Chris Lattnerac161bf2009-01-02 07:01:27 +00003620/// ParseInvoke
3621/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
3622/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
3623bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
3624 LocTy CallLoc = Lex.getLoc();
Bill Wendling50d27842012-10-15 20:35:56 +00003625 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003626 std::vector<unsigned> FwdRefAttrGrps;
Bill Wendling09bd1f72013-02-22 00:12:35 +00003627 LocTy NoBuiltinLoc;
Sandeep Patel68c5f472009-09-02 08:44:58 +00003628 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00003629 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003630 LocTy RetTypeLoc;
3631 ValID CalleeID;
3632 SmallVector<ParamInfo, 16> ArgList;
3633
Chris Lattner3ed871f2009-10-27 19:13:16 +00003634 BasicBlock *NormalBB, *UnwindBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003635 if (ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00003636 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00003637 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003638 ParseValID(CalleeID) ||
3639 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003640 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
3641 NoBuiltinLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003642 ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003643 ParseTypeAndBasicBlock(NormalBB, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003644 ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003645 ParseTypeAndBasicBlock(UnwindBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003646 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003647
Chris Lattnerac161bf2009-01-02 07:01:27 +00003648 // If RetType is a non-function pointer type, then this is the short syntax
3649 // for the call, which means that RetType is just the return type. Infer the
3650 // rest of the function argument types from the arguments that are present.
Craig Topper2617dcc2014-04-15 06:32:26 +00003651 PointerType *PFTy = nullptr;
3652 FunctionType *Ty = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003653 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3654 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3655 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00003656 std::vector<Type*> ParamTypes;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003657 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3658 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003659
Chris Lattnerac161bf2009-01-02 07:01:27 +00003660 if (!FunctionType::isValidReturnType(RetType))
3661 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003662
Owen Anderson4056ca92009-07-29 22:17:13 +00003663 Ty = FunctionType::get(RetType, ParamTypes, false);
3664 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003665 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003666
Chris Lattnerac161bf2009-01-02 07:01:27 +00003667 // Look up the callee.
3668 Value *Callee;
Victor Hernandez9d75c962010-01-11 22:31:58 +00003669 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003670
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003671 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00003672 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003673 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003674 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3675 AttributeSet::ReturnIndex,
3676 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003677
Chris Lattnerac161bf2009-01-02 07:01:27 +00003678 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003679
Chris Lattnerac161bf2009-01-02 07:01:27 +00003680 // Loop through FunctionType's arguments and ensure they are specified
3681 // correctly. Also, gather any parameter attributes.
3682 FunctionType::param_iterator I = Ty->param_begin();
3683 FunctionType::param_iterator E = Ty->param_end();
3684 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003685 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003686 if (I != E) {
3687 ExpectedTy = *I++;
3688 } else if (!Ty->isVarArg()) {
3689 return Error(ArgList[i].Loc, "too many arguments specified");
3690 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003691
Chris Lattnerac161bf2009-01-02 07:01:27 +00003692 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3693 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003694 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003695 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00003696 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3697 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00003698 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3699 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003700 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003701
Chris Lattnerac161bf2009-01-02 07:01:27 +00003702 if (I != E)
3703 return Error(CallLoc, "not enough parameters specified for call");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003704
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003705 if (FnAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003706 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3707 AttributeSet::FunctionIndex,
3708 FnAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003709
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003710 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00003711 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003712
Jay Foad5bd375a2011-07-15 08:37:34 +00003713 InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB, Args);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003714 II->setCallingConv(CC);
3715 II->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00003716 ForwardRefAttrGroups[II] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003717 Inst = II;
3718 return false;
3719}
3720
Bill Wendlingf891bf82011-07-31 06:30:59 +00003721/// ParseResume
3722/// ::= 'resume' TypeAndValue
3723bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) {
3724 Value *Exn; LocTy ExnLoc;
Bill Wendlingf891bf82011-07-31 06:30:59 +00003725 if (ParseTypeAndValue(Exn, ExnLoc, PFS))
3726 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003727
Bill Wendlingf891bf82011-07-31 06:30:59 +00003728 ResumeInst *RI = ResumeInst::Create(Exn);
3729 Inst = RI;
3730 return false;
3731}
Chris Lattnerac161bf2009-01-02 07:01:27 +00003732
3733//===----------------------------------------------------------------------===//
3734// Binary Operators.
3735//===----------------------------------------------------------------------===//
3736
3737/// ParseArithmetic
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003738/// ::= ArithmeticOps TypeAndValue ',' Value
3739///
3740/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1,
3741/// then any integer operand is allowed, if it is 2, any fp operand is allowed.
Chris Lattnerac161bf2009-01-02 07:01:27 +00003742bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003743 unsigned Opc, unsigned OperandType) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003744 LocTy Loc; Value *LHS, *RHS;
3745 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3746 ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
3747 ParseValue(LHS->getType(), RHS, PFS))
3748 return true;
3749
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003750 bool Valid;
3751 switch (OperandType) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003752 default: llvm_unreachable("Unknown operand type!");
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003753 case 0: // int or FP.
Duncan Sands9dff9be2010-02-15 16:12:20 +00003754 Valid = LHS->getType()->isIntOrIntVectorTy() ||
3755 LHS->getType()->isFPOrFPVectorTy();
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003756 break;
Duncan Sands9dff9be2010-02-15 16:12:20 +00003757 case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
3758 case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003759 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003760
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003761 if (!Valid)
3762 return Error(Loc, "invalid operand type for instruction");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003763
Chris Lattnerac161bf2009-01-02 07:01:27 +00003764 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3765 return false;
3766}
3767
3768/// ParseLogical
3769/// ::= ArithmeticOps TypeAndValue ',' Value {
3770bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
3771 unsigned Opc) {
3772 LocTy Loc; Value *LHS, *RHS;
3773 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3774 ParseToken(lltok::comma, "expected ',' in logical operation") ||
3775 ParseValue(LHS->getType(), RHS, PFS))
3776 return true;
3777
Duncan Sands9dff9be2010-02-15 16:12:20 +00003778 if (!LHS->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003779 return Error(Loc,"instruction requires integer or integer vector operands");
3780
3781 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3782 return false;
3783}
3784
3785
3786/// ParseCompare
3787/// ::= 'icmp' IPredicates TypeAndValue ',' Value
3788/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00003789bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
3790 unsigned Opc) {
3791 // Parse the integer/fp comparison predicate.
3792 LocTy Loc;
3793 unsigned Pred;
3794 Value *LHS, *RHS;
3795 if (ParseCmpPredicate(Pred, Opc) ||
3796 ParseTypeAndValue(LHS, Loc, PFS) ||
3797 ParseToken(lltok::comma, "expected ',' after compare value") ||
3798 ParseValue(LHS->getType(), RHS, PFS))
3799 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003800
Chris Lattnerac161bf2009-01-02 07:01:27 +00003801 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00003802 if (!LHS->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003803 return Error(Loc, "fcmp requires floating point operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003804 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003805 } else {
3806 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00003807 if (!LHS->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00003808 !LHS->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003809 return Error(Loc, "icmp requires integer operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003810 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003811 }
3812 return false;
3813}
3814
3815//===----------------------------------------------------------------------===//
3816// Other Instructions.
3817//===----------------------------------------------------------------------===//
3818
3819
3820/// ParseCast
3821/// ::= CastOpc TypeAndValue 'to' Type
3822bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
3823 unsigned Opc) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003824 LocTy Loc;
3825 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00003826 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003827 if (ParseTypeAndValue(Op, Loc, PFS) ||
3828 ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
3829 ParseType(DestTy))
3830 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003831
Chris Lattner89d856e2009-03-01 00:53:13 +00003832 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
3833 CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003834 return Error(Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003835 getTypeString(Op->getType()) + "' to '" +
3836 getTypeString(DestTy) + "'");
Chris Lattner89d856e2009-03-01 00:53:13 +00003837 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003838 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
3839 return false;
3840}
3841
3842/// ParseSelect
3843/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3844bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
3845 LocTy Loc;
3846 Value *Op0, *Op1, *Op2;
3847 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3848 ParseToken(lltok::comma, "expected ',' after select condition") ||
3849 ParseTypeAndValue(Op1, PFS) ||
3850 ParseToken(lltok::comma, "expected ',' after select value") ||
3851 ParseTypeAndValue(Op2, PFS))
3852 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003853
Chris Lattnerac161bf2009-01-02 07:01:27 +00003854 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
3855 return Error(Loc, Reason);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003856
Chris Lattnerac161bf2009-01-02 07:01:27 +00003857 Inst = SelectInst::Create(Op0, Op1, Op2);
3858 return false;
3859}
3860
Chris Lattnerb55ab542009-01-05 08:18:44 +00003861/// ParseVA_Arg
3862/// ::= 'va_arg' TypeAndValue ',' Type
3863bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003864 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00003865 Type *EltTy = nullptr;
Chris Lattnerb55ab542009-01-05 08:18:44 +00003866 LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003867 if (ParseTypeAndValue(Op, PFS) ||
3868 ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
Chris Lattnerb55ab542009-01-05 08:18:44 +00003869 ParseType(EltTy, TypeLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003870 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003871
Chris Lattnerb55ab542009-01-05 08:18:44 +00003872 if (!EltTy->isFirstClassType())
3873 return Error(TypeLoc, "va_arg requires operand with first class type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003874
3875 Inst = new VAArgInst(Op, EltTy);
3876 return false;
3877}
3878
3879/// ParseExtractElement
3880/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
3881bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
3882 LocTy Loc;
3883 Value *Op0, *Op1;
3884 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3885 ParseToken(lltok::comma, "expected ',' after extract value") ||
3886 ParseTypeAndValue(Op1, PFS))
3887 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003888
Chris Lattnerac161bf2009-01-02 07:01:27 +00003889 if (!ExtractElementInst::isValidOperands(Op0, Op1))
3890 return Error(Loc, "invalid extractelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003891
Eric Christopherc9742252009-07-25 02:28:41 +00003892 Inst = ExtractElementInst::Create(Op0, Op1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003893 return false;
3894}
3895
3896/// ParseInsertElement
3897/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3898bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
3899 LocTy Loc;
3900 Value *Op0, *Op1, *Op2;
3901 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3902 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3903 ParseTypeAndValue(Op1, PFS) ||
3904 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3905 ParseTypeAndValue(Op2, PFS))
3906 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003907
Chris Lattnerac161bf2009-01-02 07:01:27 +00003908 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
Eric Christopherfef8db62009-07-23 01:01:32 +00003909 return Error(Loc, "invalid insertelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003910
Chris Lattnerac161bf2009-01-02 07:01:27 +00003911 Inst = InsertElementInst::Create(Op0, Op1, Op2);
3912 return false;
3913}
3914
3915/// ParseShuffleVector
3916/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3917bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
3918 LocTy Loc;
3919 Value *Op0, *Op1, *Op2;
3920 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3921 ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
3922 ParseTypeAndValue(Op1, PFS) ||
3923 ParseToken(lltok::comma, "expected ',' after shuffle value") ||
3924 ParseTypeAndValue(Op2, PFS))
3925 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003926
Chris Lattnerac161bf2009-01-02 07:01:27 +00003927 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
Pete Cooperc1a6f982012-02-01 23:43:12 +00003928 return Error(Loc, "invalid shufflevector operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003929
Chris Lattnerac161bf2009-01-02 07:01:27 +00003930 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
3931 return false;
3932}
3933
3934/// ParsePHI
Chris Lattnerc05471e2009-10-18 05:27:44 +00003935/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
Chris Lattnerf4f03422009-12-30 05:27:33 +00003936int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003937 Type *Ty = nullptr; LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003938 Value *Op0, *Op1;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003939
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003940 if (ParseType(Ty, TypeLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003941 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
3942 ParseValue(Ty, Op0, PFS) ||
3943 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00003944 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003945 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3946 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003947
Chris Lattnerf4f03422009-12-30 05:27:33 +00003948 bool AteExtraComma = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003949 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
3950 while (1) {
3951 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003952
Chris Lattner3822f632009-01-02 08:05:26 +00003953 if (!EatIfPresent(lltok::comma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003954 break;
3955
Chris Lattnerf4f03422009-12-30 05:27:33 +00003956 if (Lex.getKind() == lltok::MetadataVar) {
3957 AteExtraComma = true;
Devang Patel8f842d32009-10-16 18:45:49 +00003958 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00003959 }
Devang Patel8f842d32009-10-16 18:45:49 +00003960
Chris Lattner3822f632009-01-02 08:05:26 +00003961 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003962 ParseValue(Ty, Op0, PFS) ||
3963 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00003964 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003965 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3966 return true;
3967 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003968
Chris Lattnerac161bf2009-01-02 07:01:27 +00003969 if (!Ty->isFirstClassType())
3970 return Error(TypeLoc, "phi node must have first class type");
3971
Jay Foad52131342011-03-30 11:28:46 +00003972 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00003973 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
3974 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
3975 Inst = PN;
Chris Lattnerf4f03422009-12-30 05:27:33 +00003976 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003977}
3978
Bill Wendlingfae14752011-08-12 20:24:12 +00003979/// ParseLandingPad
3980/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
3981/// Clause
3982/// ::= 'catch' TypeAndValue
3983/// ::= 'filter'
3984/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
3985bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003986 Type *Ty = nullptr; LocTy TyLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00003987 Value *PersFn; LocTy PersFnLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00003988
3989 if (ParseType(Ty, TyLoc) ||
3990 ParseToken(lltok::kw_personality, "expected 'personality'") ||
3991 ParseTypeAndValue(PersFn, PersFnLoc, PFS))
3992 return true;
3993
3994 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, 0);
3995 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
3996
3997 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
3998 LandingPadInst::ClauseType CT;
3999 if (EatIfPresent(lltok::kw_catch))
4000 CT = LandingPadInst::Catch;
4001 else if (EatIfPresent(lltok::kw_filter))
4002 CT = LandingPadInst::Filter;
4003 else
4004 return TokError("expected 'catch' or 'filter' clause type");
4005
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +00004006 Value *V;
4007 LocTy VLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00004008 if (ParseTypeAndValue(V, VLoc, PFS)) {
4009 delete LP;
4010 return true;
4011 }
4012
Bill Wendlinga52aa3c2011-08-12 20:52:25 +00004013 // A 'catch' type expects a non-array constant. A filter clause expects an
4014 // array constant.
4015 if (CT == LandingPadInst::Catch) {
4016 if (isa<ArrayType>(V->getType()))
4017 Error(VLoc, "'catch' clause has an invalid type");
4018 } else {
4019 if (!isa<ArrayType>(V->getType()))
4020 Error(VLoc, "'filter' clause has an invalid type");
4021 }
4022
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +00004023 LP->addClause(cast<Constant>(V));
Bill Wendlingfae14752011-08-12 20:24:12 +00004024 }
4025
4026 Inst = LP;
4027 return false;
4028}
4029
Chris Lattnerac161bf2009-01-02 07:01:27 +00004030/// ParseCall
Reid Kleckner5772b772014-04-24 20:14:34 +00004031/// ::= 'call' OptionalCallingConv OptionalAttrs Type Value
4032/// ParameterList OptionalAttrs
4033/// ::= 'tail' 'call' OptionalCallingConv OptionalAttrs Type Value
4034/// ParameterList OptionalAttrs
4035/// ::= 'musttail' 'call' OptionalCallingConv OptionalAttrs Type Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00004036/// ParameterList OptionalAttrs
4037bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
Reid Kleckner5772b772014-04-24 20:14:34 +00004038 CallInst::TailCallKind TCK) {
Bill Wendling50d27842012-10-15 20:35:56 +00004039 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00004040 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00004041 LocTy BuiltinLoc;
Sandeep Patel68c5f472009-09-02 08:44:58 +00004042 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00004043 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004044 LocTy RetTypeLoc;
4045 ValID CalleeID;
4046 SmallVector<ParamInfo, 16> ArgList;
4047 LocTy CallLoc = Lex.getLoc();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004048
Reid Kleckner5772b772014-04-24 20:14:34 +00004049 if ((TCK != CallInst::TCK_None &&
4050 ParseToken(lltok::kw_call, "expected 'tail call'")) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004051 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00004052 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00004053 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004054 ParseValID(CalleeID) ||
4055 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00004056 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00004057 BuiltinLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004058 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004059
Chris Lattnerac161bf2009-01-02 07:01:27 +00004060 // If RetType is a non-function pointer type, then this is the short syntax
4061 // for the call, which means that RetType is just the return type. Infer the
4062 // rest of the function argument types from the arguments that are present.
Craig Topper2617dcc2014-04-15 06:32:26 +00004063 PointerType *PFTy = nullptr;
4064 FunctionType *Ty = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004065 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
4066 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
4067 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00004068 std::vector<Type*> ParamTypes;
Eli Friedman6cf51412010-07-24 23:06:59 +00004069 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
4070 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004071
Chris Lattnerac161bf2009-01-02 07:01:27 +00004072 if (!FunctionType::isValidReturnType(RetType))
4073 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004074
Owen Anderson4056ca92009-07-29 22:17:13 +00004075 Ty = FunctionType::get(RetType, ParamTypes, false);
4076 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004077 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004078
Chris Lattnerac161bf2009-01-02 07:01:27 +00004079 // Look up the callee.
4080 Value *Callee;
Victor Hernandez9d75c962010-01-11 22:31:58 +00004081 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004082
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004083 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00004084 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004085 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004086 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4087 AttributeSet::ReturnIndex,
4088 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004089
Chris Lattnerac161bf2009-01-02 07:01:27 +00004090 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004091
Chris Lattnerac161bf2009-01-02 07:01:27 +00004092 // Loop through FunctionType's arguments and ensure they are specified
4093 // correctly. Also, gather any parameter attributes.
4094 FunctionType::param_iterator I = Ty->param_begin();
4095 FunctionType::param_iterator E = Ty->param_end();
4096 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004097 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004098 if (I != E) {
4099 ExpectedTy = *I++;
4100 } else if (!Ty->isVarArg()) {
4101 return Error(ArgList[i].Loc, "too many arguments specified");
4102 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004103
Chris Lattnerac161bf2009-01-02 07:01:27 +00004104 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
4105 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00004106 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004107 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00004108 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
4109 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00004110 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
4111 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004112 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004113
Chris Lattnerac161bf2009-01-02 07:01:27 +00004114 if (I != E)
4115 return Error(CallLoc, "not enough parameters specified for call");
4116
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004117 if (FnAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004118 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4119 AttributeSet::FunctionIndex,
4120 FnAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00004121
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004122 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00004123 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004124
Jay Foad5bd375a2011-07-15 08:37:34 +00004125 CallInst *CI = CallInst::Create(Callee, Args);
Reid Kleckner5772b772014-04-24 20:14:34 +00004126 CI->setTailCallKind(TCK);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004127 CI->setCallingConv(CC);
4128 CI->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00004129 ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004130 Inst = CI;
4131 return false;
4132}
4133
4134//===----------------------------------------------------------------------===//
4135// Memory Instructions.
4136//===----------------------------------------------------------------------===//
4137
4138/// ParseAlloc
David Majnemerc4ab61c2014-03-09 06:41:58 +00004139/// ::= 'alloca' 'inalloca'? Type (',' TypeAndValue)? (',' 'align' i32)?
Chris Lattner78103722011-06-17 03:16:47 +00004140int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004141 Value *Size = nullptr;
Chris Lattner200e0752009-07-02 23:08:13 +00004142 LocTy SizeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004143 unsigned Alignment = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00004144 Type *Ty = nullptr;
David Majnemerc4ab61c2014-03-09 06:41:58 +00004145
4146 bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
4147
Chris Lattner3822f632009-01-02 08:05:26 +00004148 if (ParseType(Ty)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004149
Chris Lattnerb2f39502009-12-30 05:44:30 +00004150 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00004151 if (EatIfPresent(lltok::comma)) {
David Majnemerc4ab61c2014-03-09 06:41:58 +00004152 if (Lex.getKind() == lltok::kw_align) {
4153 if (ParseOptionalAlignment(Alignment)) return true;
4154 } else if (Lex.getKind() == lltok::MetadataVar) {
4155 AteExtraComma = true;
4156 } else {
4157 if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
4158 ParseOptionalCommaAlign(Alignment, AteExtraComma))
4159 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004160 }
4161 }
4162
Dan Gohman2140a742010-05-28 01:14:11 +00004163 if (Size && !Size->getType()->isIntegerTy())
4164 return Error(SizeLoc, "element count must have integer type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004165
Reid Kleckner436c42e2014-01-17 23:58:17 +00004166 AllocaInst *AI = new AllocaInst(Ty, Size, Alignment);
4167 AI->setUsedWithInAlloca(IsInAlloca);
4168 Inst = AI;
Chris Lattner78103722011-06-17 03:16:47 +00004169 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004170}
4171
4172/// ParseLoad
Eli Friedman02e737b2011-08-12 22:50:01 +00004173/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004174/// ::= 'load' 'atomic' 'volatile'? TypeAndValue
Eli Friedman02e737b2011-08-12 22:50:01 +00004175/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00004176int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004177 Value *Val; LocTy Loc;
Devang Patelea8a4b92009-09-17 23:04:48 +00004178 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00004179 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004180 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00004181 AtomicOrdering Ordering = NotAtomic;
4182 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004183
4184 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004185 isAtomic = true;
4186 Lex.Lex();
4187 }
4188
Chris Lattnerbc639292011-11-27 06:56:53 +00004189 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004190 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004191 isVolatile = true;
4192 Lex.Lex();
4193 }
4194
Chris Lattnerb2f39502009-12-30 05:44:30 +00004195 if (ParseTypeAndValue(Val, Loc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00004196 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004197 ParseOptionalCommaAlign(Alignment, AteExtraComma))
4198 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004199
Duncan Sands19d0b472010-02-16 11:11:14 +00004200 if (!Val->getType()->isPointerTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004201 !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType())
4202 return Error(Loc, "load operand must be a pointer to a first class type");
Eli Friedman59b66882011-08-09 23:02:53 +00004203 if (isAtomic && !Alignment)
4204 return Error(Loc, "atomic load must have explicit non-zero alignment");
4205 if (Ordering == Release || Ordering == AcquireRelease)
4206 return Error(Loc, "atomic load cannot use Release ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004207
Eli Friedman59b66882011-08-09 23:02:53 +00004208 Inst = new LoadInst(Val, "", isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00004209 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004210}
4211
4212/// ParseStore
Eli Friedman02e737b2011-08-12 22:50:01 +00004213
4214/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
4215/// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
Eli Friedman59b66882011-08-09 23:02:53 +00004216/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00004217int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004218 Value *Val, *Ptr; LocTy Loc, PtrLoc;
Devang Patelea8a4b92009-09-17 23:04:48 +00004219 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00004220 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004221 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00004222 AtomicOrdering Ordering = NotAtomic;
4223 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004224
4225 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004226 isAtomic = true;
4227 Lex.Lex();
4228 }
4229
Chris Lattnerbc639292011-11-27 06:56:53 +00004230 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004231 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004232 isVolatile = true;
4233 Lex.Lex();
4234 }
4235
Chris Lattnerac161bf2009-01-02 07:01:27 +00004236 if (ParseTypeAndValue(Val, Loc, PFS) ||
4237 ParseToken(lltok::comma, "expected ',' after store operand") ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004238 ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00004239 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004240 ParseOptionalCommaAlign(Alignment, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004241 return true;
Devang Patelea8a4b92009-09-17 23:04:48 +00004242
Duncan Sands19d0b472010-02-16 11:11:14 +00004243 if (!Ptr->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004244 return Error(PtrLoc, "store operand must be a pointer");
4245 if (!Val->getType()->isFirstClassType())
4246 return Error(Loc, "store operand must be a first class value");
4247 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4248 return Error(Loc, "stored value and pointer type do not match");
Eli Friedman59b66882011-08-09 23:02:53 +00004249 if (isAtomic && !Alignment)
4250 return Error(Loc, "atomic store must have explicit non-zero alignment");
4251 if (Ordering == Acquire || Ordering == AcquireRelease)
4252 return Error(Loc, "atomic store cannot use Acquire ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004253
Eli Friedman59b66882011-08-09 23:02:53 +00004254 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00004255 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004256}
4257
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004258/// ParseCmpXchg
Tim Northover420a2162014-06-13 14:24:07 +00004259/// ::= 'cmpxchg' 'weak'? 'volatile'? TypeAndValue ',' TypeAndValue ','
4260/// TypeAndValue 'singlethread'? AtomicOrdering AtomicOrdering
Eli Friedman02e737b2011-08-12 22:50:01 +00004261int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004262 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
4263 bool AteExtraComma = false;
Tim Northovere94a5182014-03-11 10:48:52 +00004264 AtomicOrdering SuccessOrdering = NotAtomic;
4265 AtomicOrdering FailureOrdering = NotAtomic;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004266 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004267 bool isVolatile = false;
Tim Northover420a2162014-06-13 14:24:07 +00004268 bool isWeak = false;
4269
4270 if (EatIfPresent(lltok::kw_weak))
4271 isWeak = true;
Eli Friedman02e737b2011-08-12 22:50:01 +00004272
4273 if (EatIfPresent(lltok::kw_volatile))
4274 isVolatile = true;
4275
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004276 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4277 ParseToken(lltok::comma, "expected ',' after cmpxchg address") ||
4278 ParseTypeAndValue(Cmp, CmpLoc, PFS) ||
4279 ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
4280 ParseTypeAndValue(New, NewLoc, PFS) ||
Tim Northovere94a5182014-03-11 10:48:52 +00004281 ParseScopeAndOrdering(true /*Always atomic*/, Scope, SuccessOrdering) ||
4282 ParseOrdering(FailureOrdering))
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004283 return true;
4284
Tim Northovere94a5182014-03-11 10:48:52 +00004285 if (SuccessOrdering == Unordered || FailureOrdering == Unordered)
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004286 return TokError("cmpxchg cannot be unordered");
Tim Northovere94a5182014-03-11 10:48:52 +00004287 if (SuccessOrdering < FailureOrdering)
4288 return TokError("cmpxchg must be at least as ordered on success as failure");
4289 if (FailureOrdering == Release || FailureOrdering == AcquireRelease)
4290 return TokError("cmpxchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004291 if (!Ptr->getType()->isPointerTy())
4292 return Error(PtrLoc, "cmpxchg operand must be a pointer");
4293 if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType())
4294 return Error(CmpLoc, "compare value and pointer type do not match");
4295 if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType())
4296 return Error(NewLoc, "new value and pointer type do not match");
4297 if (!New->getType()->isIntegerTy())
4298 return Error(NewLoc, "cmpxchg operand must be an integer");
4299 unsigned Size = New->getType()->getPrimitiveSizeInBits();
4300 if (Size < 8 || (Size & (Size - 1)))
4301 return Error(NewLoc, "cmpxchg operand must be power-of-two byte-sized"
4302 " integer");
4303
Tim Northover420a2162014-06-13 14:24:07 +00004304 AtomicCmpXchgInst *CXI = new AtomicCmpXchgInst(
4305 Ptr, Cmp, New, SuccessOrdering, FailureOrdering, Scope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004306 CXI->setVolatile(isVolatile);
Tim Northover420a2162014-06-13 14:24:07 +00004307 CXI->setWeak(isWeak);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004308 Inst = CXI;
4309 return AteExtraComma ? InstExtraComma : InstNormal;
4310}
4311
4312/// ParseAtomicRMW
Eli Friedman02e737b2011-08-12 22:50:01 +00004313/// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
4314/// 'singlethread'? AtomicOrdering
4315int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004316 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
4317 bool AteExtraComma = false;
4318 AtomicOrdering Ordering = NotAtomic;
4319 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004320 bool isVolatile = false;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004321 AtomicRMWInst::BinOp Operation;
Eli Friedman02e737b2011-08-12 22:50:01 +00004322
4323 if (EatIfPresent(lltok::kw_volatile))
4324 isVolatile = true;
4325
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004326 switch (Lex.getKind()) {
4327 default: return TokError("expected binary operation in atomicrmw");
4328 case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break;
4329 case lltok::kw_add: Operation = AtomicRMWInst::Add; break;
4330 case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break;
4331 case lltok::kw_and: Operation = AtomicRMWInst::And; break;
4332 case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break;
4333 case lltok::kw_or: Operation = AtomicRMWInst::Or; break;
4334 case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break;
4335 case lltok::kw_max: Operation = AtomicRMWInst::Max; break;
4336 case lltok::kw_min: Operation = AtomicRMWInst::Min; break;
4337 case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break;
4338 case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break;
4339 }
4340 Lex.Lex(); // Eat the operation.
4341
4342 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4343 ParseToken(lltok::comma, "expected ',' after atomicrmw address") ||
4344 ParseTypeAndValue(Val, ValLoc, PFS) ||
4345 ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4346 return true;
4347
4348 if (Ordering == Unordered)
4349 return TokError("atomicrmw cannot be unordered");
4350 if (!Ptr->getType()->isPointerTy())
4351 return Error(PtrLoc, "atomicrmw operand must be a pointer");
4352 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4353 return Error(ValLoc, "atomicrmw value and pointer type do not match");
4354 if (!Val->getType()->isIntegerTy())
4355 return Error(ValLoc, "atomicrmw operand must be an integer");
4356 unsigned Size = Val->getType()->getPrimitiveSizeInBits();
4357 if (Size < 8 || (Size & (Size - 1)))
4358 return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
4359 " integer");
4360
4361 AtomicRMWInst *RMWI =
4362 new AtomicRMWInst(Operation, Ptr, Val, Ordering, Scope);
4363 RMWI->setVolatile(isVolatile);
4364 Inst = RMWI;
4365 return AteExtraComma ? InstExtraComma : InstNormal;
4366}
4367
Eli Friedmanfee02c62011-07-25 23:16:38 +00004368/// ParseFence
4369/// ::= 'fence' 'singlethread'? AtomicOrdering
4370int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) {
4371 AtomicOrdering Ordering = NotAtomic;
4372 SynchronizationScope Scope = CrossThread;
4373 if (ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4374 return true;
4375
4376 if (Ordering == Unordered)
4377 return TokError("fence cannot be unordered");
4378 if (Ordering == Monotonic)
4379 return TokError("fence cannot be monotonic");
4380
4381 Inst = new FenceInst(Context, Ordering, Scope);
4382 return InstNormal;
4383}
4384
Chris Lattnerac161bf2009-01-02 07:01:27 +00004385/// ParseGetElementPtr
Dan Gohman1639c392009-07-27 21:53:46 +00004386/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
Chris Lattnerf4f03422009-12-30 05:27:33 +00004387int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004388 Value *Ptr = nullptr;
4389 Value *Val = nullptr;
Nadav Rotem3924cb02011-12-05 06:29:09 +00004390 LocTy Loc, EltLoc;
Dan Gohman1639c392009-07-27 21:53:46 +00004391
Dan Gohman16cbbe42009-07-29 15:58:36 +00004392 bool InBounds = EatIfPresent(lltok::kw_inbounds);
Dan Gohman1639c392009-07-27 21:53:46 +00004393
Chris Lattner3822f632009-01-02 08:05:26 +00004394 if (ParseTypeAndValue(Ptr, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004395
Eli Benderskyd9806682013-04-22 17:03:42 +00004396 Type *BaseType = Ptr->getType();
4397 PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType());
4398 if (!BasePointerType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004399 return Error(Loc, "base of getelementptr must be a pointer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004400
Chris Lattnerac161bf2009-01-02 07:01:27 +00004401 SmallVector<Value*, 16> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004402 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00004403 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf4f03422009-12-30 05:27:33 +00004404 if (Lex.getKind() == lltok::MetadataVar) {
4405 AteExtraComma = true;
Devang Patel52b17452009-10-13 18:49:55 +00004406 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004407 }
Chris Lattner3822f632009-01-02 08:05:26 +00004408 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
Nadav Rotem3924cb02011-12-05 06:29:09 +00004409 if (!Val->getType()->getScalarType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004410 return Error(EltLoc, "getelementptr index must be an integer");
Nadav Rotem3924cb02011-12-05 06:29:09 +00004411 if (Val->getType()->isVectorTy() != Ptr->getType()->isVectorTy())
4412 return Error(EltLoc, "getelementptr index type missmatch");
4413 if (Val->getType()->isVectorTy()) {
4414 unsigned ValNumEl = cast<VectorType>(Val->getType())->getNumElements();
4415 unsigned PtrNumEl = cast<VectorType>(Ptr->getType())->getNumElements();
4416 if (ValNumEl != PtrNumEl)
4417 return Error(EltLoc,
4418 "getelementptr vector index has a wrong number of elements");
4419 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004420 Indices.push_back(Val);
4421 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004422
Eli Benderskyd9806682013-04-22 17:03:42 +00004423 if (!Indices.empty() && !BasePointerType->getElementType()->isSized())
4424 return Error(Loc, "base element of getelementptr must be sized");
4425
4426 if (!GetElementPtrInst::getIndexedType(BaseType, Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004427 return Error(Loc, "invalid getelementptr indices");
Jay Foadd1b78492011-07-25 09:48:08 +00004428 Inst = GetElementPtrInst::Create(Ptr, Indices);
Dan Gohman1639c392009-07-27 21:53:46 +00004429 if (InBounds)
Dan Gohman1b849082009-09-07 23:54:19 +00004430 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004431 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004432}
4433
4434/// ParseExtractValue
4435/// ::= 'extractvalue' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00004436int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004437 Value *Val; LocTy Loc;
4438 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004439 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004440 if (ParseTypeAndValue(Val, Loc, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00004441 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004442 return true;
4443
Chris Lattner392be582010-02-12 20:49:41 +00004444 if (!Val->getType()->isAggregateType())
4445 return Error(Loc, "extractvalue operand must be aggregate type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004446
Jay Foad57aa6362011-07-13 10:26:04 +00004447 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004448 return Error(Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00004449 Inst = ExtractValueInst::Create(Val, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004450 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004451}
4452
4453/// ParseInsertValue
4454/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00004455int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004456 Value *Val0, *Val1; LocTy Loc0, Loc1;
4457 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004458 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004459 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
4460 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
4461 ParseTypeAndValue(Val1, Loc1, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00004462 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004463 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004464
Chris Lattner392be582010-02-12 20:49:41 +00004465 if (!Val0->getType()->isAggregateType())
4466 return Error(Loc0, "insertvalue operand must be aggregate type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004467
Jay Foad57aa6362011-07-13 10:26:04 +00004468 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004469 return Error(Loc0, "invalid indices for insertvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00004470 Inst = InsertValueInst::Create(Val0, Val1, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004471 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004472}
Nick Lewycky49f89192009-04-04 07:22:01 +00004473
4474//===----------------------------------------------------------------------===//
4475// Embedded metadata.
4476//===----------------------------------------------------------------------===//
4477
4478/// ParseMDNodeVector
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004479/// ::= Element (',' Element)*
4480/// Element
4481/// ::= 'null' | TypeAndValue
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00004482bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts,
Victor Hernandezb8fd1522010-01-10 07:14:18 +00004483 PerFunctionState *PFS) {
Dan Gohman1e0213a2010-07-13 19:33:27 +00004484 // Check for an empty list.
4485 if (Lex.getKind() == lltok::rbrace)
4486 return false;
4487
Nick Lewycky49f89192009-04-04 07:22:01 +00004488 do {
Chris Lattnera01ddfc2009-12-30 04:42:57 +00004489 // Null is a special case since it is typeless.
4490 if (EatIfPresent(lltok::kw_null)) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004491 Elts.push_back(nullptr);
Chris Lattnera01ddfc2009-12-30 04:42:57 +00004492 continue;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004493 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004494
Craig Topper2617dcc2014-04-15 06:32:26 +00004495 Value *V = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004496 if (ParseTypeAndValue(V, PFS)) return true;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004497 Elts.push_back(V);
Nick Lewycky49f89192009-04-04 07:22:01 +00004498 } while (EatIfPresent(lltok::comma));
4499
4500 return false;
4501}