1193323Sed//===-- LLParser.cpp - Parser Class ---------------------------------------===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file defines the parser class for .ll files. 11193323Sed// 12193323Sed//===----------------------------------------------------------------------===// 13193323Sed 14193323Sed#include "LLParser.h" 15249423Sdim#include "llvm/ADT/SmallPtrSet.h" 16193323Sed#include "llvm/AutoUpgrade.h" 17249423Sdim#include "llvm/IR/CallingConv.h" 18249423Sdim#include "llvm/IR/Constants.h" 19249423Sdim#include "llvm/IR/DerivedTypes.h" 20249423Sdim#include "llvm/IR/InlineAsm.h" 21249423Sdim#include "llvm/IR/Instructions.h" 22263508Sdim#include "llvm/IR/LLVMContext.h" 23249423Sdim#include "llvm/IR/Module.h" 24249423Sdim#include "llvm/IR/Operator.h" 25249423Sdim#include "llvm/IR/ValueSymbolTable.h" 26198090Srdivacky#include "llvm/Support/ErrorHandling.h" 27193323Sed#include "llvm/Support/raw_ostream.h" 28193323Sedusing namespace llvm; 29193323Sed 30226633Sdimstatic std::string getTypeString(Type *T) { 31224145Sdim std::string Result; 32224145Sdim raw_string_ostream Tmp(Result); 33224145Sdim Tmp << *T; 34224145Sdim return Tmp.str(); 35224145Sdim} 36224145Sdim 37193323Sed/// Run: module ::= toplevelentity* 38193323Sedbool LLParser::Run() { 39193323Sed // Prime the lexer. 40193323Sed Lex.Lex(); 41193323Sed 42193323Sed return ParseTopLevelEntities() || 43193323Sed ValidateEndOfModule(); 44193323Sed} 45193323Sed 46193323Sed/// ValidateEndOfModule - Do final validity and sanity checks at the end of the 47193323Sed/// module. 48193323Sedbool LLParser::ValidateEndOfModule() { 49206083Srdivacky // Handle any instruction metadata forward references. 50206083Srdivacky if (!ForwardRefInstMetadata.empty()) { 51206083Srdivacky for (DenseMap<Instruction*, std::vector<MDRef> >::iterator 52206083Srdivacky I = ForwardRefInstMetadata.begin(), E = ForwardRefInstMetadata.end(); 53206083Srdivacky I != E; ++I) { 54206083Srdivacky Instruction *Inst = I->first; 55206083Srdivacky const std::vector<MDRef> &MDList = I->second; 56249423Sdim 57206083Srdivacky for (unsigned i = 0, e = MDList.size(); i != e; ++i) { 58206083Srdivacky unsigned SlotNo = MDList[i].MDSlot; 59249423Sdim 60206083Srdivacky if (SlotNo >= NumberedMetadata.size() || NumberedMetadata[SlotNo] == 0) 61206083Srdivacky return Error(MDList[i].Loc, "use of undefined metadata '!" + 62218893Sdim Twine(SlotNo) + "'"); 63206083Srdivacky Inst->setMetadata(MDList[i].MDKind, NumberedMetadata[SlotNo]); 64206083Srdivacky } 65206083Srdivacky } 66206083Srdivacky ForwardRefInstMetadata.clear(); 67206083Srdivacky } 68249423Sdim 69263508Sdim for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++) 70263508Sdim UpgradeInstWithTBAATag(InstsWithTBAATag[I]); 71263508Sdim 72249423Sdim // Handle any function attribute group forward references. 73249423Sdim for (std::map<Value*, std::vector<unsigned> >::iterator 74249423Sdim I = ForwardRefAttrGroups.begin(), E = ForwardRefAttrGroups.end(); 75249423Sdim I != E; ++I) { 76249423Sdim Value *V = I->first; 77249423Sdim std::vector<unsigned> &Vec = I->second; 78249423Sdim AttrBuilder B; 79249423Sdim 80249423Sdim for (std::vector<unsigned>::iterator VI = Vec.begin(), VE = Vec.end(); 81249423Sdim VI != VE; ++VI) 82249423Sdim B.merge(NumberedAttrBuilders[*VI]); 83249423Sdim 84249423Sdim if (Function *Fn = dyn_cast<Function>(V)) { 85249423Sdim AttributeSet AS = Fn->getAttributes(); 86249423Sdim AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex); 87249423Sdim AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex, 88249423Sdim AS.getFnAttributes()); 89249423Sdim 90249423Sdim FnAttrs.merge(B); 91249423Sdim 92249423Sdim // If the alignment was parsed as an attribute, move to the alignment 93249423Sdim // field. 94249423Sdim if (FnAttrs.hasAlignmentAttr()) { 95249423Sdim Fn->setAlignment(FnAttrs.getAlignment()); 96249423Sdim FnAttrs.removeAttribute(Attribute::Alignment); 97249423Sdim } 98249423Sdim 99249423Sdim AS = AS.addAttributes(Context, AttributeSet::FunctionIndex, 100249423Sdim AttributeSet::get(Context, 101249423Sdim AttributeSet::FunctionIndex, 102249423Sdim FnAttrs)); 103249423Sdim Fn->setAttributes(AS); 104249423Sdim } else if (CallInst *CI = dyn_cast<CallInst>(V)) { 105249423Sdim AttributeSet AS = CI->getAttributes(); 106249423Sdim AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex); 107249423Sdim AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex, 108249423Sdim AS.getFnAttributes()); 109249423Sdim FnAttrs.merge(B); 110249423Sdim AS = AS.addAttributes(Context, AttributeSet::FunctionIndex, 111249423Sdim AttributeSet::get(Context, 112249423Sdim AttributeSet::FunctionIndex, 113249423Sdim FnAttrs)); 114249423Sdim CI->setAttributes(AS); 115249423Sdim } else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) { 116249423Sdim AttributeSet AS = II->getAttributes(); 117249423Sdim AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex); 118249423Sdim AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex, 119249423Sdim AS.getFnAttributes()); 120249423Sdim FnAttrs.merge(B); 121249423Sdim AS = AS.addAttributes(Context, AttributeSet::FunctionIndex, 122249423Sdim AttributeSet::get(Context, 123249423Sdim AttributeSet::FunctionIndex, 124249423Sdim FnAttrs)); 125249423Sdim II->setAttributes(AS); 126249423Sdim } else { 127249423Sdim llvm_unreachable("invalid object with forward attribute group reference"); 128249423Sdim } 129249423Sdim } 130249423Sdim 131198892Srdivacky // If there are entries in ForwardRefBlockAddresses at this point, they are 132198892Srdivacky // references after the function was defined. Resolve those now. 133198892Srdivacky while (!ForwardRefBlockAddresses.empty()) { 134198892Srdivacky // Okay, we are referencing an already-parsed function, resolve them now. 135198892Srdivacky Function *TheFn = 0; 136198892Srdivacky const ValID &Fn = ForwardRefBlockAddresses.begin()->first; 137198892Srdivacky if (Fn.Kind == ValID::t_GlobalName) 138198892Srdivacky TheFn = M->getFunction(Fn.StrVal); 139198892Srdivacky else if (Fn.UIntVal < NumberedVals.size()) 140198892Srdivacky TheFn = dyn_cast<Function>(NumberedVals[Fn.UIntVal]); 141249423Sdim 142198892Srdivacky if (TheFn == 0) 143198892Srdivacky return Error(Fn.Loc, "unknown function referenced by blockaddress"); 144249423Sdim 145198892Srdivacky // Resolve all these references. 146249423Sdim if (ResolveForwardRefBlockAddresses(TheFn, 147198892Srdivacky ForwardRefBlockAddresses.begin()->second, 148198892Srdivacky 0)) 149198892Srdivacky return true; 150249423Sdim 151198892Srdivacky ForwardRefBlockAddresses.erase(ForwardRefBlockAddresses.begin()); 152198892Srdivacky } 153249423Sdim 154224145Sdim for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) 155224145Sdim if (NumberedTypes[i].second.isValid()) 156224145Sdim return Error(NumberedTypes[i].second, 157224145Sdim "use of undefined type '%" + Twine(i) + "'"); 158198090Srdivacky 159224145Sdim for (StringMap<std::pair<Type*, LocTy> >::iterator I = 160224145Sdim NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) 161224145Sdim if (I->second.second.isValid()) 162224145Sdim return Error(I->second.second, 163224145Sdim "use of undefined type named '" + I->getKey() + "'"); 164224145Sdim 165193323Sed if (!ForwardRefVals.empty()) 166193323Sed return Error(ForwardRefVals.begin()->second.second, 167193323Sed "use of undefined value '@" + ForwardRefVals.begin()->first + 168193323Sed "'"); 169198090Srdivacky 170193323Sed if (!ForwardRefValIDs.empty()) 171193323Sed return Error(ForwardRefValIDs.begin()->second.second, 172193323Sed "use of undefined value '@" + 173218893Sdim Twine(ForwardRefValIDs.begin()->first) + "'"); 174198090Srdivacky 175198090Srdivacky if (!ForwardRefMDNodes.empty()) 176198090Srdivacky return Error(ForwardRefMDNodes.begin()->second.second, 177198090Srdivacky "use of undefined metadata '!" + 178218893Sdim Twine(ForwardRefMDNodes.begin()->first) + "'"); 179198090Srdivacky 180198090Srdivacky 181193323Sed // Look for intrinsic functions and CallInst that need to be upgraded 182193323Sed for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ) 183193323Sed UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove 184198090Srdivacky 185263508Sdim UpgradeDebugInfo(*M); 186263508Sdim 187193323Sed return false; 188193323Sed} 189193323Sed 190249423Sdimbool LLParser::ResolveForwardRefBlockAddresses(Function *TheFn, 191198892Srdivacky std::vector<std::pair<ValID, GlobalValue*> > &Refs, 192198892Srdivacky PerFunctionState *PFS) { 193198892Srdivacky // Loop over all the references, resolving them. 194198892Srdivacky for (unsigned i = 0, e = Refs.size(); i != e; ++i) { 195198892Srdivacky BasicBlock *Res; 196198892Srdivacky if (PFS) { 197198892Srdivacky if (Refs[i].first.Kind == ValID::t_LocalName) 198198892Srdivacky Res = PFS->GetBB(Refs[i].first.StrVal, Refs[i].first.Loc); 199198892Srdivacky else 200198892Srdivacky Res = PFS->GetBB(Refs[i].first.UIntVal, Refs[i].first.Loc); 201198892Srdivacky } else if (Refs[i].first.Kind == ValID::t_LocalID) { 202198892Srdivacky return Error(Refs[i].first.Loc, 203198892Srdivacky "cannot take address of numeric label after the function is defined"); 204198892Srdivacky } else { 205198892Srdivacky Res = dyn_cast_or_null<BasicBlock>( 206198892Srdivacky TheFn->getValueSymbolTable().lookup(Refs[i].first.StrVal)); 207198892Srdivacky } 208249423Sdim 209198892Srdivacky if (Res == 0) 210198892Srdivacky return Error(Refs[i].first.Loc, 211198892Srdivacky "referenced value is not a basic block"); 212249423Sdim 213198892Srdivacky // Get the BlockAddress for this and update references to use it. 214198892Srdivacky BlockAddress *BA = BlockAddress::get(TheFn, Res); 215198892Srdivacky Refs[i].second->replaceAllUsesWith(BA); 216198892Srdivacky Refs[i].second->eraseFromParent(); 217198892Srdivacky } 218198892Srdivacky return false; 219198892Srdivacky} 220198892Srdivacky 221198892Srdivacky 222193323Sed//===----------------------------------------------------------------------===// 223193323Sed// Top-Level Entities 224193323Sed//===----------------------------------------------------------------------===// 225193323Sed 226193323Sedbool LLParser::ParseTopLevelEntities() { 227193323Sed while (1) { 228193323Sed switch (Lex.getKind()) { 229193323Sed default: return TokError("expected top-level entity"); 230193323Sed case lltok::Eof: return false; 231193323Sed case lltok::kw_declare: if (ParseDeclare()) return true; break; 232193323Sed case lltok::kw_define: if (ParseDefine()) return true; break; 233193323Sed case lltok::kw_module: if (ParseModuleAsm()) return true; break; 234193323Sed case lltok::kw_target: if (ParseTargetDefinition()) return true; break; 235193323Sed case lltok::kw_deplibs: if (ParseDepLibs()) return true; break; 236198090Srdivacky case lltok::LocalVarID: if (ParseUnnamedType()) return true; break; 237193323Sed case lltok::LocalVar: if (ParseNamedType()) return true; break; 238198090Srdivacky case lltok::GlobalID: if (ParseUnnamedGlobal()) return true; break; 239193323Sed case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break; 240201360Srdivacky case lltok::exclaim: if (ParseStandaloneMetadata()) return true; break; 241249423Sdim case lltok::MetadataVar:if (ParseNamedMetadata()) return true; break; 242193323Sed 243193323Sed // The Global variable production with no name can have many different 244193323Sed // optional leading prefixes, the production is: 245193323Sed // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalThreadLocal 246218893Sdim // OptionalAddrSpace OptionalUnNammedAddr 247218893Sdim // ('constant'|'global') ... 248210299Sed case lltok::kw_private: // OptionalLinkage 249210299Sed case lltok::kw_linker_private: // OptionalLinkage 250210299Sed case lltok::kw_linker_private_weak: // OptionalLinkage 251210299Sed case lltok::kw_internal: // OptionalLinkage 252210299Sed case lltok::kw_weak: // OptionalLinkage 253210299Sed case lltok::kw_weak_odr: // OptionalLinkage 254210299Sed case lltok::kw_linkonce: // OptionalLinkage 255210299Sed case lltok::kw_linkonce_odr: // OptionalLinkage 256210299Sed case lltok::kw_appending: // OptionalLinkage 257210299Sed case lltok::kw_dllexport: // OptionalLinkage 258210299Sed case lltok::kw_common: // OptionalLinkage 259210299Sed case lltok::kw_dllimport: // OptionalLinkage 260210299Sed case lltok::kw_extern_weak: // OptionalLinkage 261210299Sed case lltok::kw_external: { // OptionalLinkage 262193323Sed unsigned Linkage, Visibility; 263193323Sed if (ParseOptionalLinkage(Linkage) || 264193323Sed ParseOptionalVisibility(Visibility) || 265195340Sed ParseGlobal("", SMLoc(), Linkage, true, Visibility)) 266193323Sed return true; 267193323Sed break; 268193323Sed } 269193323Sed case lltok::kw_default: // OptionalVisibility 270193323Sed case lltok::kw_hidden: // OptionalVisibility 271193323Sed case lltok::kw_protected: { // OptionalVisibility 272193323Sed unsigned Visibility; 273193323Sed if (ParseOptionalVisibility(Visibility) || 274195340Sed ParseGlobal("", SMLoc(), 0, false, Visibility)) 275193323Sed return true; 276193323Sed break; 277193323Sed } 278198090Srdivacky 279193323Sed case lltok::kw_thread_local: // OptionalThreadLocal 280193323Sed case lltok::kw_addrspace: // OptionalAddrSpace 281193323Sed case lltok::kw_constant: // GlobalType 282193323Sed case lltok::kw_global: // GlobalType 283195340Sed if (ParseGlobal("", SMLoc(), 0, false, 0)) return true; 284193323Sed break; 285249423Sdim 286249423Sdim case lltok::kw_attributes: if (ParseUnnamedAttrGrp()) return true; break; 287193323Sed } 288193323Sed } 289193323Sed} 290193323Sed 291193323Sed 292193323Sed/// toplevelentity 293193323Sed/// ::= 'module' 'asm' STRINGCONSTANT 294193323Sedbool LLParser::ParseModuleAsm() { 295193323Sed assert(Lex.getKind() == lltok::kw_module); 296193323Sed Lex.Lex(); 297198090Srdivacky 298198090Srdivacky std::string AsmStr; 299193323Sed if (ParseToken(lltok::kw_asm, "expected 'module asm'") || 300193323Sed ParseStringConstant(AsmStr)) return true; 301198090Srdivacky 302221345Sdim M->appendModuleInlineAsm(AsmStr); 303193323Sed return false; 304193323Sed} 305193323Sed 306193323Sed/// toplevelentity 307193323Sed/// ::= 'target' 'triple' '=' STRINGCONSTANT 308193323Sed/// ::= 'target' 'datalayout' '=' STRINGCONSTANT 309193323Sedbool LLParser::ParseTargetDefinition() { 310193323Sed assert(Lex.getKind() == lltok::kw_target); 311193323Sed std::string Str; 312193323Sed switch (Lex.Lex()) { 313193323Sed default: return TokError("unknown target property"); 314193323Sed case lltok::kw_triple: 315193323Sed Lex.Lex(); 316193323Sed if (ParseToken(lltok::equal, "expected '=' after target triple") || 317193323Sed ParseStringConstant(Str)) 318193323Sed return true; 319193323Sed M->setTargetTriple(Str); 320193323Sed return false; 321193323Sed case lltok::kw_datalayout: 322193323Sed Lex.Lex(); 323193323Sed if (ParseToken(lltok::equal, "expected '=' after target datalayout") || 324193323Sed ParseStringConstant(Str)) 325193323Sed return true; 326193323Sed M->setDataLayout(Str); 327193323Sed return false; 328193323Sed } 329193323Sed} 330193323Sed 331193323Sed/// toplevelentity 332193323Sed/// ::= 'deplibs' '=' '[' ']' 333193323Sed/// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']' 334249423Sdim/// FIXME: Remove in 4.0. Currently parse, but ignore. 335193323Sedbool LLParser::ParseDepLibs() { 336193323Sed assert(Lex.getKind() == lltok::kw_deplibs); 337193323Sed Lex.Lex(); 338193323Sed if (ParseToken(lltok::equal, "expected '=' after deplibs") || 339193323Sed ParseToken(lltok::lsquare, "expected '=' after deplibs")) 340193323Sed return true; 341193323Sed 342193323Sed if (EatIfPresent(lltok::rsquare)) 343193323Sed return false; 344198090Srdivacky 345249423Sdim do { 346249423Sdim std::string Str; 347193323Sed if (ParseStringConstant(Str)) return true; 348249423Sdim } while (EatIfPresent(lltok::comma)); 349193323Sed 350193323Sed return ParseToken(lltok::rsquare, "expected ']' at end of list"); 351193323Sed} 352193323Sed 353198090Srdivacky/// ParseUnnamedType: 354198090Srdivacky/// ::= LocalVarID '=' 'type' type 355193323Sedbool LLParser::ParseUnnamedType() { 356193323Sed LocTy TypeLoc = Lex.getLoc(); 357224145Sdim unsigned TypeID = Lex.getUIntVal(); 358224145Sdim Lex.Lex(); // eat LocalVarID; 359193323Sed 360224145Sdim if (ParseToken(lltok::equal, "expected '=' after name") || 361224145Sdim ParseToken(lltok::kw_type, "expected 'type' after '='")) 362224145Sdim return true; 363198090Srdivacky 364224145Sdim if (TypeID >= NumberedTypes.size()) 365224145Sdim NumberedTypes.resize(TypeID+1); 366249423Sdim 367224145Sdim Type *Result = 0; 368224145Sdim if (ParseStructDefinition(TypeLoc, "", 369224145Sdim NumberedTypes[TypeID], Result)) return true; 370249423Sdim 371224145Sdim if (!isa<StructType>(Result)) { 372224145Sdim std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID]; 373224145Sdim if (Entry.first) 374224145Sdim return Error(TypeLoc, "non-struct types may not be recursive"); 375224145Sdim Entry.first = Result; 376224145Sdim Entry.second = SMLoc(); 377193323Sed } 378198090Srdivacky 379193323Sed return false; 380193323Sed} 381193323Sed 382224145Sdim 383193323Sed/// toplevelentity 384193323Sed/// ::= LocalVar '=' 'type' type 385193323Sedbool LLParser::ParseNamedType() { 386193323Sed std::string Name = Lex.getStrVal(); 387193323Sed LocTy NameLoc = Lex.getLoc(); 388193323Sed Lex.Lex(); // eat LocalVar. 389198090Srdivacky 390193323Sed if (ParseToken(lltok::equal, "expected '=' after name") || 391224145Sdim ParseToken(lltok::kw_type, "expected 'type' after name")) 392193323Sed return true; 393249423Sdim 394224145Sdim Type *Result = 0; 395224145Sdim if (ParseStructDefinition(NameLoc, Name, 396224145Sdim NamedTypes[Name], Result)) return true; 397249423Sdim 398224145Sdim if (!isa<StructType>(Result)) { 399224145Sdim std::pair<Type*, LocTy> &Entry = NamedTypes[Name]; 400224145Sdim if (Entry.first) 401224145Sdim return Error(NameLoc, "non-struct types may not be recursive"); 402224145Sdim Entry.first = Result; 403224145Sdim Entry.second = SMLoc(); 404193323Sed } 405249423Sdim 406224145Sdim return false; 407193323Sed} 408193323Sed 409193323Sed 410193323Sed/// toplevelentity 411193323Sed/// ::= 'declare' FunctionHeader 412193323Sedbool LLParser::ParseDeclare() { 413193323Sed assert(Lex.getKind() == lltok::kw_declare); 414193323Sed Lex.Lex(); 415198090Srdivacky 416193323Sed Function *F; 417193323Sed return ParseFunctionHeader(F, false); 418193323Sed} 419193323Sed 420193323Sed/// toplevelentity 421193323Sed/// ::= 'define' FunctionHeader '{' ... 422193323Sedbool LLParser::ParseDefine() { 423193323Sed assert(Lex.getKind() == lltok::kw_define); 424193323Sed Lex.Lex(); 425198090Srdivacky 426193323Sed Function *F; 427193323Sed return ParseFunctionHeader(F, true) || 428193323Sed ParseFunctionBody(*F); 429193323Sed} 430193323Sed 431193323Sed/// ParseGlobalType 432193323Sed/// ::= 'constant' 433193323Sed/// ::= 'global' 434193323Sedbool LLParser::ParseGlobalType(bool &IsConstant) { 435193323Sed if (Lex.getKind() == lltok::kw_constant) 436193323Sed IsConstant = true; 437193323Sed else if (Lex.getKind() == lltok::kw_global) 438193323Sed IsConstant = false; 439193323Sed else { 440193323Sed IsConstant = false; 441193323Sed return TokError("expected 'global' or 'constant'"); 442193323Sed } 443193323Sed Lex.Lex(); 444193323Sed return false; 445193323Sed} 446193323Sed 447198090Srdivacky/// ParseUnnamedGlobal: 448198090Srdivacky/// OptionalVisibility ALIAS ... 449198090Srdivacky/// OptionalLinkage OptionalVisibility ... -> global variable 450198090Srdivacky/// GlobalID '=' OptionalVisibility ALIAS ... 451198090Srdivacky/// GlobalID '=' OptionalLinkage OptionalVisibility ... -> global variable 452198090Srdivackybool LLParser::ParseUnnamedGlobal() { 453198090Srdivacky unsigned VarID = NumberedVals.size(); 454198090Srdivacky std::string Name; 455198090Srdivacky LocTy NameLoc = Lex.getLoc(); 456198090Srdivacky 457198090Srdivacky // Handle the GlobalID form. 458198090Srdivacky if (Lex.getKind() == lltok::GlobalID) { 459198090Srdivacky if (Lex.getUIntVal() != VarID) 460198090Srdivacky return Error(Lex.getLoc(), "variable expected to be numbered '%" + 461218893Sdim Twine(VarID) + "'"); 462198090Srdivacky Lex.Lex(); // eat GlobalID; 463198090Srdivacky 464198090Srdivacky if (ParseToken(lltok::equal, "expected '=' after name")) 465198090Srdivacky return true; 466198090Srdivacky } 467198090Srdivacky 468198090Srdivacky bool HasLinkage; 469198090Srdivacky unsigned Linkage, Visibility; 470198090Srdivacky if (ParseOptionalLinkage(Linkage, HasLinkage) || 471198090Srdivacky ParseOptionalVisibility(Visibility)) 472198090Srdivacky return true; 473198090Srdivacky 474198090Srdivacky if (HasLinkage || Lex.getKind() != lltok::kw_alias) 475198090Srdivacky return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility); 476198090Srdivacky return ParseAlias(Name, NameLoc, Visibility); 477198090Srdivacky} 478198090Srdivacky 479193323Sed/// ParseNamedGlobal: 480193323Sed/// GlobalVar '=' OptionalVisibility ALIAS ... 481193323Sed/// GlobalVar '=' OptionalLinkage OptionalVisibility ... -> global variable 482193323Sedbool LLParser::ParseNamedGlobal() { 483193323Sed assert(Lex.getKind() == lltok::GlobalVar); 484193323Sed LocTy NameLoc = Lex.getLoc(); 485193323Sed std::string Name = Lex.getStrVal(); 486193323Sed Lex.Lex(); 487198090Srdivacky 488193323Sed bool HasLinkage; 489193323Sed unsigned Linkage, Visibility; 490193323Sed if (ParseToken(lltok::equal, "expected '=' in global variable") || 491193323Sed ParseOptionalLinkage(Linkage, HasLinkage) || 492193323Sed ParseOptionalVisibility(Visibility)) 493193323Sed return true; 494198090Srdivacky 495193323Sed if (HasLinkage || Lex.getKind() != lltok::kw_alias) 496193323Sed return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility); 497193323Sed return ParseAlias(Name, NameLoc, Visibility); 498193323Sed} 499193323Sed 500198090Srdivacky// MDString: 501198090Srdivacky// ::= '!' STRINGCONSTANT 502201360Srdivackybool LLParser::ParseMDString(MDString *&Result) { 503198090Srdivacky std::string Str; 504198090Srdivacky if (ParseStringConstant(Str)) return true; 505201360Srdivacky Result = MDString::get(Context, Str); 506198090Srdivacky return false; 507198090Srdivacky} 508198090Srdivacky 509198090Srdivacky// MDNode: 510198090Srdivacky// ::= '!' MDNodeNumber 511206083Srdivacky// 512206083Srdivacky/// This version of ParseMDNodeID returns the slot number and null in the case 513206083Srdivacky/// of a forward reference. 514206083Srdivackybool LLParser::ParseMDNodeID(MDNode *&Result, unsigned &SlotNo) { 515206083Srdivacky // !{ ..., !42, ... } 516206083Srdivacky if (ParseUInt32(SlotNo)) return true; 517206083Srdivacky 518206083Srdivacky // Check existing MDNode. 519206083Srdivacky if (SlotNo < NumberedMetadata.size() && NumberedMetadata[SlotNo] != 0) 520206083Srdivacky Result = NumberedMetadata[SlotNo]; 521206083Srdivacky else 522206083Srdivacky Result = 0; 523206083Srdivacky return false; 524206083Srdivacky} 525206083Srdivacky 526201360Srdivackybool LLParser::ParseMDNodeID(MDNode *&Result) { 527198090Srdivacky // !{ ..., !42, ... } 528198090Srdivacky unsigned MID = 0; 529206083Srdivacky if (ParseMDNodeID(Result, MID)) return true; 530198090Srdivacky 531206083Srdivacky // If not a forward reference, just return it now. 532206083Srdivacky if (Result) return false; 533198090Srdivacky 534206083Srdivacky // Otherwise, create MDNode forward reference. 535251662Sdim MDNode *FwdNode = MDNode::getTemporary(Context, None); 536198090Srdivacky ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc()); 537249423Sdim 538201360Srdivacky if (NumberedMetadata.size() <= MID) 539201360Srdivacky NumberedMetadata.resize(MID+1); 540201360Srdivacky NumberedMetadata[MID] = FwdNode; 541201360Srdivacky Result = FwdNode; 542198090Srdivacky return false; 543198090Srdivacky} 544198090Srdivacky 545201360Srdivacky/// ParseNamedMetadata: 546198090Srdivacky/// !foo = !{ !1, !2 } 547198090Srdivackybool LLParser::ParseNamedMetadata() { 548201360Srdivacky assert(Lex.getKind() == lltok::MetadataVar); 549201360Srdivacky std::string Name = Lex.getStrVal(); 550198090Srdivacky Lex.Lex(); 551198090Srdivacky 552201360Srdivacky if (ParseToken(lltok::equal, "expected '=' here") || 553201360Srdivacky ParseToken(lltok::exclaim, "Expected '!' here") || 554201360Srdivacky ParseToken(lltok::lbrace, "Expected '{' here")) 555198090Srdivacky return true; 556198090Srdivacky 557212904Sdim NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name); 558210299Sed if (Lex.getKind() != lltok::rbrace) 559210299Sed do { 560210299Sed if (ParseToken(lltok::exclaim, "Expected '!' here")) 561210299Sed return true; 562249423Sdim 563210299Sed MDNode *N = 0; 564210299Sed if (ParseMDNodeID(N)) return true; 565212904Sdim NMD->addOperand(N); 566210299Sed } while (EatIfPresent(lltok::comma)); 567198090Srdivacky 568198090Srdivacky if (ParseToken(lltok::rbrace, "expected end of metadata node")) 569198090Srdivacky return true; 570198090Srdivacky 571198090Srdivacky return false; 572198090Srdivacky} 573198090Srdivacky 574195340Sed/// ParseStandaloneMetadata: 575198090Srdivacky/// !42 = !{...} 576195340Sedbool LLParser::ParseStandaloneMetadata() { 577201360Srdivacky assert(Lex.getKind() == lltok::exclaim); 578195340Sed Lex.Lex(); 579195340Sed unsigned MetadataID = 0; 580195340Sed 581195340Sed LocTy TyLoc; 582224145Sdim Type *Ty = 0; 583198090Srdivacky SmallVector<Value *, 16> Elts; 584201360Srdivacky if (ParseUInt32(MetadataID) || 585201360Srdivacky ParseToken(lltok::equal, "expected '=' here") || 586201360Srdivacky ParseType(Ty, TyLoc) || 587201360Srdivacky ParseToken(lltok::exclaim, "Expected '!' here") || 588201360Srdivacky ParseToken(lltok::lbrace, "Expected '{' here") || 589202375Srdivacky ParseMDNodeVector(Elts, NULL) || 590201360Srdivacky ParseToken(lltok::rbrace, "expected end of metadata node")) 591198090Srdivacky return true; 592198090Srdivacky 593221345Sdim MDNode *Init = MDNode::get(Context, Elts); 594249423Sdim 595201360Srdivacky // See if this was forward referenced, if so, handle it. 596201360Srdivacky std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> >::iterator 597198090Srdivacky FI = ForwardRefMDNodes.find(MetadataID); 598198090Srdivacky if (FI != ForwardRefMDNodes.end()) { 599212904Sdim MDNode *Temp = FI->second.first; 600212904Sdim Temp->replaceAllUsesWith(Init); 601212904Sdim MDNode::deleteTemporary(Temp); 602198090Srdivacky ForwardRefMDNodes.erase(FI); 603249423Sdim 604201360Srdivacky assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work"); 605201360Srdivacky } else { 606201360Srdivacky if (MetadataID >= NumberedMetadata.size()) 607201360Srdivacky NumberedMetadata.resize(MetadataID+1); 608198090Srdivacky 609201360Srdivacky if (NumberedMetadata[MetadataID] != 0) 610201360Srdivacky return TokError("Metadata id is already used"); 611201360Srdivacky NumberedMetadata[MetadataID] = Init; 612200581Srdivacky } 613200581Srdivacky 614200581Srdivacky return false; 615200581Srdivacky} 616200581Srdivacky 617193323Sed/// ParseAlias: 618193323Sed/// ::= GlobalVar '=' OptionalVisibility 'alias' OptionalLinkage Aliasee 619193323Sed/// Aliasee 620193323Sed/// ::= TypeAndValue 621193323Sed/// ::= 'bitcast' '(' TypeAndValue 'to' Type ')' 622198090Srdivacky/// ::= 'getelementptr' 'inbounds'? '(' ... ')' 623193323Sed/// 624193323Sed/// Everything through visibility has already been parsed. 625193323Sed/// 626193323Sedbool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc, 627193323Sed unsigned Visibility) { 628193323Sed assert(Lex.getKind() == lltok::kw_alias); 629193323Sed Lex.Lex(); 630193323Sed LocTy LinkageLoc = Lex.getLoc(); 631263508Sdim unsigned L; 632263508Sdim if (ParseOptionalLinkage(L)) 633193323Sed return true; 634193323Sed 635263508Sdim GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L; 636263508Sdim 637263508Sdim if(!GlobalAlias::isValidLinkage(Linkage)) 638193323Sed return Error(LinkageLoc, "invalid linkage type for alias"); 639198090Srdivacky 640193323Sed Constant *Aliasee; 641193323Sed LocTy AliaseeLoc = Lex.getLoc(); 642193323Sed if (Lex.getKind() != lltok::kw_bitcast && 643193323Sed Lex.getKind() != lltok::kw_getelementptr) { 644193323Sed if (ParseGlobalTypeAndValue(Aliasee)) return true; 645193323Sed } else { 646193323Sed // The bitcast dest type is not present, it is implied by the dest type. 647193323Sed ValID ID; 648193323Sed if (ParseValID(ID)) return true; 649193323Sed if (ID.Kind != ValID::t_Constant) 650193323Sed return Error(AliaseeLoc, "invalid aliasee"); 651193323Sed Aliasee = ID.ConstantVal; 652193323Sed } 653198090Srdivacky 654204642Srdivacky if (!Aliasee->getType()->isPointerTy()) 655193323Sed return Error(AliaseeLoc, "alias must have pointer type"); 656193323Sed 657193323Sed // Okay, create the alias but do not insert it into the module yet. 658193323Sed GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), 659193323Sed (GlobalValue::LinkageTypes)Linkage, Name, 660193323Sed Aliasee); 661193323Sed GA->setVisibility((GlobalValue::VisibilityTypes)Visibility); 662198090Srdivacky 663193323Sed // See if this value already exists in the symbol table. If so, it is either 664193323Sed // a redefinition or a definition of a forward reference. 665198892Srdivacky if (GlobalValue *Val = M->getNamedValue(Name)) { 666193323Sed // See if this was a redefinition. If so, there is no entry in 667193323Sed // ForwardRefVals. 668193323Sed std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator 669193323Sed I = ForwardRefVals.find(Name); 670193323Sed if (I == ForwardRefVals.end()) 671193323Sed return Error(NameLoc, "redefinition of global named '@" + Name + "'"); 672193323Sed 673193323Sed // Otherwise, this was a definition of forward ref. Verify that types 674193323Sed // agree. 675193323Sed if (Val->getType() != GA->getType()) 676193323Sed return Error(NameLoc, 677193323Sed "forward reference and definition of alias have different types"); 678198090Srdivacky 679193323Sed // If they agree, just RAUW the old value with the alias and remove the 680193323Sed // forward ref info. 681193323Sed Val->replaceAllUsesWith(GA); 682193323Sed Val->eraseFromParent(); 683193323Sed ForwardRefVals.erase(I); 684193323Sed } 685198090Srdivacky 686193323Sed // Insert into the module, we know its name won't collide now. 687193323Sed M->getAliasList().push_back(GA); 688218893Sdim assert(GA->getName() == Name && "Should not be a name conflict!"); 689198090Srdivacky 690193323Sed return false; 691193323Sed} 692193323Sed 693193323Sed/// ParseGlobal 694193323Sed/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalThreadLocal 695249423Sdim/// OptionalAddrSpace OptionalUnNammedAddr 696249423Sdim/// OptionalExternallyInitialized GlobalType Type Const 697193323Sed/// ::= OptionalLinkage OptionalVisibility OptionalThreadLocal 698249423Sdim/// OptionalAddrSpace OptionalUnNammedAddr 699249423Sdim/// OptionalExternallyInitialized GlobalType Type Const 700193323Sed/// 701193323Sed/// Everything through visibility has been parsed already. 702193323Sed/// 703193323Sedbool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc, 704193323Sed unsigned Linkage, bool HasLinkage, 705193323Sed unsigned Visibility) { 706193323Sed unsigned AddrSpace; 707249423Sdim bool IsConstant, UnnamedAddr, IsExternallyInitialized; 708239462Sdim GlobalVariable::ThreadLocalMode TLM; 709218893Sdim LocTy UnnamedAddrLoc; 710249423Sdim LocTy IsExternallyInitializedLoc; 711193323Sed LocTy TyLoc; 712198090Srdivacky 713224145Sdim Type *Ty = 0; 714239462Sdim if (ParseOptionalThreadLocal(TLM) || 715193323Sed ParseOptionalAddrSpace(AddrSpace) || 716218893Sdim ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr, 717218893Sdim &UnnamedAddrLoc) || 718249423Sdim ParseOptionalToken(lltok::kw_externally_initialized, 719249423Sdim IsExternallyInitialized, 720249423Sdim &IsExternallyInitializedLoc) || 721193323Sed ParseGlobalType(IsConstant) || 722193323Sed ParseType(Ty, TyLoc)) 723193323Sed return true; 724198090Srdivacky 725193323Sed // If the linkage is specified and is external, then no initializer is 726193323Sed // present. 727193323Sed Constant *Init = 0; 728193323Sed if (!HasLinkage || (Linkage != GlobalValue::DLLImportLinkage && 729193323Sed Linkage != GlobalValue::ExternalWeakLinkage && 730193323Sed Linkage != GlobalValue::ExternalLinkage)) { 731193323Sed if (ParseGlobalValue(Ty, Init)) 732193323Sed return true; 733193323Sed } 734193323Sed 735204642Srdivacky if (Ty->isFunctionTy() || Ty->isLabelTy()) 736193323Sed return Error(TyLoc, "invalid type for global variable"); 737198090Srdivacky 738193323Sed GlobalVariable *GV = 0; 739193323Sed 740193323Sed // See if the global was forward referenced, if so, use the global. 741193323Sed if (!Name.empty()) { 742198892Srdivacky if (GlobalValue *GVal = M->getNamedValue(Name)) { 743198892Srdivacky if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal)) 744198892Srdivacky return Error(NameLoc, "redefinition of global '@" + Name + "'"); 745198892Srdivacky GV = cast<GlobalVariable>(GVal); 746198892Srdivacky } 747193323Sed } else { 748193323Sed std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator 749193323Sed I = ForwardRefValIDs.find(NumberedVals.size()); 750193323Sed if (I != ForwardRefValIDs.end()) { 751193323Sed GV = cast<GlobalVariable>(I->second.first); 752193323Sed ForwardRefValIDs.erase(I); 753193323Sed } 754193323Sed } 755193323Sed 756193323Sed if (GV == 0) { 757198090Srdivacky GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, 0, 758239462Sdim Name, 0, GlobalVariable::NotThreadLocal, 759239462Sdim AddrSpace); 760193323Sed } else { 761193323Sed if (GV->getType()->getElementType() != Ty) 762193323Sed return Error(TyLoc, 763193323Sed "forward reference and definition of global have different types"); 764198090Srdivacky 765193323Sed // Move the forward-reference to the correct spot in the module. 766193323Sed M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV); 767193323Sed } 768193323Sed 769193323Sed if (Name.empty()) 770193323Sed NumberedVals.push_back(GV); 771198090Srdivacky 772193323Sed // Set the parsed properties on the global. 773193323Sed if (Init) 774193323Sed GV->setInitializer(Init); 775193323Sed GV->setConstant(IsConstant); 776193323Sed GV->setLinkage((GlobalValue::LinkageTypes)Linkage); 777193323Sed GV->setVisibility((GlobalValue::VisibilityTypes)Visibility); 778249423Sdim GV->setExternallyInitialized(IsExternallyInitialized); 779239462Sdim GV->setThreadLocalMode(TLM); 780218893Sdim GV->setUnnamedAddr(UnnamedAddr); 781198090Srdivacky 782193323Sed // Parse attributes on the global. 783193323Sed while (Lex.getKind() == lltok::comma) { 784193323Sed Lex.Lex(); 785198090Srdivacky 786193323Sed if (Lex.getKind() == lltok::kw_section) { 787193323Sed Lex.Lex(); 788193323Sed GV->setSection(Lex.getStrVal()); 789193323Sed if (ParseToken(lltok::StringConstant, "expected global section string")) 790193323Sed return true; 791193323Sed } else if (Lex.getKind() == lltok::kw_align) { 792193323Sed unsigned Alignment; 793193323Sed if (ParseOptionalAlignment(Alignment)) return true; 794193323Sed GV->setAlignment(Alignment); 795193323Sed } else { 796193323Sed TokError("unknown global variable property!"); 797193323Sed } 798193323Sed } 799198090Srdivacky 800193323Sed return false; 801193323Sed} 802193323Sed 803249423Sdim/// ParseUnnamedAttrGrp 804249423Sdim/// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}' 805249423Sdimbool LLParser::ParseUnnamedAttrGrp() { 806249423Sdim assert(Lex.getKind() == lltok::kw_attributes); 807249423Sdim LocTy AttrGrpLoc = Lex.getLoc(); 808249423Sdim Lex.Lex(); 809193323Sed 810249423Sdim assert(Lex.getKind() == lltok::AttrGrpID); 811249423Sdim unsigned VarID = Lex.getUIntVal(); 812249423Sdim std::vector<unsigned> unused; 813263508Sdim LocTy BuiltinLoc; 814249423Sdim Lex.Lex(); 815249423Sdim 816249423Sdim if (ParseToken(lltok::equal, "expected '=' here") || 817249423Sdim ParseToken(lltok::lbrace, "expected '{' here") || 818249423Sdim ParseFnAttributeValuePairs(NumberedAttrBuilders[VarID], unused, true, 819263508Sdim BuiltinLoc) || 820249423Sdim ParseToken(lltok::rbrace, "expected end of attribute group")) 821249423Sdim return true; 822249423Sdim 823249423Sdim if (!NumberedAttrBuilders[VarID].hasAttributes()) 824249423Sdim return Error(AttrGrpLoc, "attribute group has no attributes"); 825249423Sdim 826249423Sdim return false; 827249423Sdim} 828249423Sdim 829249423Sdim/// ParseFnAttributeValuePairs 830249423Sdim/// ::= <attr> | <attr> '=' <value> 831249423Sdimbool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B, 832249423Sdim std::vector<unsigned> &FwdRefAttrGrps, 833263508Sdim bool inAttrGrp, LocTy &BuiltinLoc) { 834249423Sdim bool HaveError = false; 835249423Sdim 836249423Sdim B.clear(); 837249423Sdim 838249423Sdim while (true) { 839249423Sdim lltok::Kind Token = Lex.getKind(); 840263508Sdim if (Token == lltok::kw_builtin) 841263508Sdim BuiltinLoc = Lex.getLoc(); 842249423Sdim switch (Token) { 843249423Sdim default: 844249423Sdim if (!inAttrGrp) return HaveError; 845249423Sdim return Error(Lex.getLoc(), "unterminated attribute group"); 846249423Sdim case lltok::rbrace: 847249423Sdim // Finished. 848249423Sdim return false; 849249423Sdim 850249423Sdim case lltok::AttrGrpID: { 851249423Sdim // Allow a function to reference an attribute group: 852249423Sdim // 853249423Sdim // define void @foo() #1 { ... } 854249423Sdim if (inAttrGrp) 855249423Sdim HaveError |= 856249423Sdim Error(Lex.getLoc(), 857249423Sdim "cannot have an attribute group reference in an attribute group"); 858249423Sdim 859249423Sdim unsigned AttrGrpNum = Lex.getUIntVal(); 860249423Sdim if (inAttrGrp) break; 861249423Sdim 862249423Sdim // Save the reference to the attribute group. We'll fill it in later. 863249423Sdim FwdRefAttrGrps.push_back(AttrGrpNum); 864249423Sdim break; 865249423Sdim } 866249423Sdim // Target-dependent attributes: 867249423Sdim case lltok::StringConstant: { 868249423Sdim std::string Attr = Lex.getStrVal(); 869249423Sdim Lex.Lex(); 870249423Sdim std::string Val; 871249423Sdim if (EatIfPresent(lltok::equal) && 872249423Sdim ParseStringConstant(Val)) 873249423Sdim return true; 874249423Sdim 875249423Sdim B.addAttribute(Attr, Val); 876249423Sdim continue; 877249423Sdim } 878249423Sdim 879249423Sdim // Target-independent attributes: 880249423Sdim case lltok::kw_align: { 881251662Sdim // As a hack, we allow function alignment to be initially parsed as an 882251662Sdim // attribute on a function declaration/definition or added to an attribute 883251662Sdim // group and later moved to the alignment field. 884249423Sdim unsigned Alignment; 885249423Sdim if (inAttrGrp) { 886249423Sdim Lex.Lex(); 887249423Sdim if (ParseToken(lltok::equal, "expected '=' here") || 888249423Sdim ParseUInt32(Alignment)) 889249423Sdim return true; 890249423Sdim } else { 891249423Sdim if (ParseOptionalAlignment(Alignment)) 892249423Sdim return true; 893249423Sdim } 894249423Sdim B.addAlignmentAttr(Alignment); 895249423Sdim continue; 896249423Sdim } 897249423Sdim case lltok::kw_alignstack: { 898249423Sdim unsigned Alignment; 899249423Sdim if (inAttrGrp) { 900249423Sdim Lex.Lex(); 901249423Sdim if (ParseToken(lltok::equal, "expected '=' here") || 902249423Sdim ParseUInt32(Alignment)) 903249423Sdim return true; 904249423Sdim } else { 905249423Sdim if (ParseOptionalStackAlignment(Alignment)) 906249423Sdim return true; 907249423Sdim } 908249423Sdim B.addStackAlignmentAttr(Alignment); 909249423Sdim continue; 910249423Sdim } 911249423Sdim case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break; 912263508Sdim case lltok::kw_builtin: B.addAttribute(Attribute::Builtin); break; 913263508Sdim case lltok::kw_cold: B.addAttribute(Attribute::Cold); break; 914249423Sdim case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break; 915249423Sdim case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break; 916249423Sdim case lltok::kw_naked: B.addAttribute(Attribute::Naked); break; 917249423Sdim case lltok::kw_nobuiltin: B.addAttribute(Attribute::NoBuiltin); break; 918249423Sdim case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break; 919249423Sdim case lltok::kw_noimplicitfloat: B.addAttribute(Attribute::NoImplicitFloat); break; 920249423Sdim case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break; 921249423Sdim case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break; 922249423Sdim case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break; 923249423Sdim case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break; 924249423Sdim case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break; 925263508Sdim case lltok::kw_optnone: B.addAttribute(Attribute::OptimizeNone); break; 926249423Sdim case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break; 927249423Sdim case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break; 928249423Sdim case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break; 929249423Sdim case lltok::kw_returns_twice: B.addAttribute(Attribute::ReturnsTwice); break; 930249423Sdim case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break; 931249423Sdim case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break; 932249423Sdim case lltok::kw_sspstrong: B.addAttribute(Attribute::StackProtectStrong); break; 933249423Sdim case lltok::kw_sanitize_address: B.addAttribute(Attribute::SanitizeAddress); break; 934249423Sdim case lltok::kw_sanitize_thread: B.addAttribute(Attribute::SanitizeThread); break; 935249423Sdim case lltok::kw_sanitize_memory: B.addAttribute(Attribute::SanitizeMemory); break; 936249423Sdim case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break; 937249423Sdim 938249423Sdim // Error handling. 939249423Sdim case lltok::kw_inreg: 940249423Sdim case lltok::kw_signext: 941249423Sdim case lltok::kw_zeroext: 942249423Sdim HaveError |= 943249423Sdim Error(Lex.getLoc(), 944249423Sdim "invalid use of attribute on a function"); 945249423Sdim break; 946249423Sdim case lltok::kw_byval: 947249423Sdim case lltok::kw_nest: 948249423Sdim case lltok::kw_noalias: 949249423Sdim case lltok::kw_nocapture: 950251662Sdim case lltok::kw_returned: 951249423Sdim case lltok::kw_sret: 952249423Sdim HaveError |= 953249423Sdim Error(Lex.getLoc(), 954249423Sdim "invalid use of parameter-only attribute on a function"); 955249423Sdim break; 956249423Sdim } 957249423Sdim 958249423Sdim Lex.Lex(); 959249423Sdim } 960249423Sdim} 961249423Sdim 962193323Sed//===----------------------------------------------------------------------===// 963193323Sed// GlobalValue Reference/Resolution Routines. 964193323Sed//===----------------------------------------------------------------------===// 965193323Sed 966193323Sed/// GetGlobalVal - Get a value with the specified name or ID, creating a 967193323Sed/// forward reference record if needed. This can return null if the value 968193323Sed/// exists but does not have the right type. 969226633SdimGlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty, 970193323Sed LocTy Loc) { 971226633Sdim PointerType *PTy = dyn_cast<PointerType>(Ty); 972193323Sed if (PTy == 0) { 973193323Sed Error(Loc, "global variable reference must have pointer type"); 974193323Sed return 0; 975193323Sed } 976198090Srdivacky 977193323Sed // Look this name up in the normal function symbol table. 978193323Sed GlobalValue *Val = 979193323Sed cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name)); 980198090Srdivacky 981193323Sed // If this is a forward reference for the value, see if we already created a 982193323Sed // forward ref record. 983193323Sed if (Val == 0) { 984193323Sed std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator 985193323Sed I = ForwardRefVals.find(Name); 986193323Sed if (I != ForwardRefVals.end()) 987193323Sed Val = I->second.first; 988193323Sed } 989198090Srdivacky 990193323Sed // If we have the value in the symbol table or fwd-ref table, return it. 991193323Sed if (Val) { 992193323Sed if (Val->getType() == Ty) return Val; 993193323Sed Error(Loc, "'@" + Name + "' defined with type '" + 994224145Sdim getTypeString(Val->getType()) + "'"); 995193323Sed return 0; 996193323Sed } 997198090Srdivacky 998193323Sed // Otherwise, create a new forward reference for this value and remember it. 999193323Sed GlobalValue *FwdVal; 1000226633Sdim if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType())) 1001193323Sed FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M); 1002224145Sdim else 1003198090Srdivacky FwdVal = new GlobalVariable(*M, PTy->getElementType(), false, 1004243830Sdim GlobalValue::ExternalWeakLinkage, 0, Name, 1005243830Sdim 0, GlobalVariable::NotThreadLocal, 1006243830Sdim PTy->getAddressSpace()); 1007198090Srdivacky 1008193323Sed ForwardRefVals[Name] = std::make_pair(FwdVal, Loc); 1009193323Sed return FwdVal; 1010193323Sed} 1011193323Sed 1012226633SdimGlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) { 1013226633Sdim PointerType *PTy = dyn_cast<PointerType>(Ty); 1014193323Sed if (PTy == 0) { 1015193323Sed Error(Loc, "global variable reference must have pointer type"); 1016193323Sed return 0; 1017193323Sed } 1018198090Srdivacky 1019193323Sed GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0; 1020198090Srdivacky 1021193323Sed // If this is a forward reference for the value, see if we already created a 1022193323Sed // forward ref record. 1023193323Sed if (Val == 0) { 1024193323Sed std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator 1025193323Sed I = ForwardRefValIDs.find(ID); 1026193323Sed if (I != ForwardRefValIDs.end()) 1027193323Sed Val = I->second.first; 1028193323Sed } 1029198090Srdivacky 1030193323Sed // If we have the value in the symbol table or fwd-ref table, return it. 1031193323Sed if (Val) { 1032193323Sed if (Val->getType() == Ty) return Val; 1033218893Sdim Error(Loc, "'@" + Twine(ID) + "' defined with type '" + 1034224145Sdim getTypeString(Val->getType()) + "'"); 1035193323Sed return 0; 1036193323Sed } 1037198090Srdivacky 1038193323Sed // Otherwise, create a new forward reference for this value and remember it. 1039193323Sed GlobalValue *FwdVal; 1040226633Sdim if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType())) 1041193323Sed FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M); 1042224145Sdim else 1043198090Srdivacky FwdVal = new GlobalVariable(*M, PTy->getElementType(), false, 1044198090Srdivacky GlobalValue::ExternalWeakLinkage, 0, ""); 1045198090Srdivacky 1046193323Sed ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc); 1047193323Sed return FwdVal; 1048193323Sed} 1049193323Sed 1050193323Sed 1051193323Sed//===----------------------------------------------------------------------===// 1052193323Sed// Helper Routines. 1053193323Sed//===----------------------------------------------------------------------===// 1054193323Sed 1055193323Sed/// ParseToken - If the current token has the specified kind, eat it and return 1056193323Sed/// success. Otherwise, emit the specified error and return failure. 1057193323Sedbool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) { 1058193323Sed if (Lex.getKind() != T) 1059193323Sed return TokError(ErrMsg); 1060193323Sed Lex.Lex(); 1061193323Sed return false; 1062193323Sed} 1063193323Sed 1064193323Sed/// ParseStringConstant 1065193323Sed/// ::= StringConstant 1066193323Sedbool LLParser::ParseStringConstant(std::string &Result) { 1067193323Sed if (Lex.getKind() != lltok::StringConstant) 1068193323Sed return TokError("expected string constant"); 1069193323Sed Result = Lex.getStrVal(); 1070193323Sed Lex.Lex(); 1071193323Sed return false; 1072193323Sed} 1073193323Sed 1074193323Sed/// ParseUInt32 1075193323Sed/// ::= uint32 1076193323Sedbool LLParser::ParseUInt32(unsigned &Val) { 1077193323Sed if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned()) 1078193323Sed return TokError("expected integer"); 1079193323Sed uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1); 1080193323Sed if (Val64 != unsigned(Val64)) 1081193323Sed return TokError("expected 32-bit integer (too large)"); 1082193323Sed Val = Val64; 1083193323Sed Lex.Lex(); 1084193323Sed return false; 1085193323Sed} 1086193323Sed 1087239462Sdim/// ParseTLSModel 1088239462Sdim/// := 'localdynamic' 1089239462Sdim/// := 'initialexec' 1090239462Sdim/// := 'localexec' 1091239462Sdimbool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) { 1092239462Sdim switch (Lex.getKind()) { 1093239462Sdim default: 1094239462Sdim return TokError("expected localdynamic, initialexec or localexec"); 1095239462Sdim case lltok::kw_localdynamic: 1096239462Sdim TLM = GlobalVariable::LocalDynamicTLSModel; 1097239462Sdim break; 1098239462Sdim case lltok::kw_initialexec: 1099239462Sdim TLM = GlobalVariable::InitialExecTLSModel; 1100239462Sdim break; 1101239462Sdim case lltok::kw_localexec: 1102239462Sdim TLM = GlobalVariable::LocalExecTLSModel; 1103239462Sdim break; 1104239462Sdim } 1105193323Sed 1106239462Sdim Lex.Lex(); 1107239462Sdim return false; 1108239462Sdim} 1109239462Sdim 1110239462Sdim/// ParseOptionalThreadLocal 1111239462Sdim/// := /*empty*/ 1112239462Sdim/// := 'thread_local' 1113239462Sdim/// := 'thread_local' '(' tlsmodel ')' 1114239462Sdimbool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) { 1115239462Sdim TLM = GlobalVariable::NotThreadLocal; 1116239462Sdim if (!EatIfPresent(lltok::kw_thread_local)) 1117239462Sdim return false; 1118239462Sdim 1119239462Sdim TLM = GlobalVariable::GeneralDynamicTLSModel; 1120239462Sdim if (Lex.getKind() == lltok::lparen) { 1121239462Sdim Lex.Lex(); 1122239462Sdim return ParseTLSModel(TLM) || 1123239462Sdim ParseToken(lltok::rparen, "expected ')' after thread local model"); 1124239462Sdim } 1125239462Sdim return false; 1126239462Sdim} 1127239462Sdim 1128193323Sed/// ParseOptionalAddrSpace 1129193323Sed/// := /*empty*/ 1130193323Sed/// := 'addrspace' '(' uint32 ')' 1131193323Sedbool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) { 1132193323Sed AddrSpace = 0; 1133193323Sed if (!EatIfPresent(lltok::kw_addrspace)) 1134193323Sed return false; 1135193323Sed return ParseToken(lltok::lparen, "expected '(' in address space") || 1136193323Sed ParseUInt32(AddrSpace) || 1137193323Sed ParseToken(lltok::rparen, "expected ')' in address space"); 1138198090Srdivacky} 1139193323Sed 1140249423Sdim/// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes. 1141249423Sdimbool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) { 1142243830Sdim bool HaveError = false; 1143198090Srdivacky 1144243830Sdim B.clear(); 1145243830Sdim 1146193323Sed while (1) { 1147243830Sdim lltok::Kind Token = Lex.getKind(); 1148243830Sdim switch (Token) { 1149193323Sed default: // End of attributes. 1150243830Sdim return HaveError; 1151193323Sed case lltok::kw_align: { 1152193323Sed unsigned Alignment; 1153193323Sed if (ParseOptionalAlignment(Alignment)) 1154193323Sed return true; 1155243830Sdim B.addAlignmentAttr(Alignment); 1156193323Sed continue; 1157193323Sed } 1158249423Sdim case lltok::kw_byval: B.addAttribute(Attribute::ByVal); break; 1159249423Sdim case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break; 1160249423Sdim case lltok::kw_nest: B.addAttribute(Attribute::Nest); break; 1161249423Sdim case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break; 1162249423Sdim case lltok::kw_nocapture: B.addAttribute(Attribute::NoCapture); break; 1163263508Sdim case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break; 1164263508Sdim case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break; 1165251662Sdim case lltok::kw_returned: B.addAttribute(Attribute::Returned); break; 1166249423Sdim case lltok::kw_signext: B.addAttribute(Attribute::SExt); break; 1167249423Sdim case lltok::kw_sret: B.addAttribute(Attribute::StructRet); break; 1168249423Sdim case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break; 1169203954Srdivacky 1170251662Sdim case lltok::kw_alignstack: 1171251662Sdim case lltok::kw_alwaysinline: 1172263508Sdim case lltok::kw_builtin: 1173251662Sdim case lltok::kw_inlinehint: 1174251662Sdim case lltok::kw_minsize: 1175251662Sdim case lltok::kw_naked: 1176251662Sdim case lltok::kw_nobuiltin: 1177251662Sdim case lltok::kw_noduplicate: 1178251662Sdim case lltok::kw_noimplicitfloat: 1179251662Sdim case lltok::kw_noinline: 1180251662Sdim case lltok::kw_nonlazybind: 1181251662Sdim case lltok::kw_noredzone: 1182251662Sdim case lltok::kw_noreturn: 1183251662Sdim case lltok::kw_nounwind: 1184263508Sdim case lltok::kw_optnone: 1185251662Sdim case lltok::kw_optsize: 1186251662Sdim case lltok::kw_returns_twice: 1187251662Sdim case lltok::kw_sanitize_address: 1188251662Sdim case lltok::kw_sanitize_memory: 1189251662Sdim case lltok::kw_sanitize_thread: 1190251662Sdim case lltok::kw_ssp: 1191251662Sdim case lltok::kw_sspreq: 1192251662Sdim case lltok::kw_sspstrong: 1193251662Sdim case lltok::kw_uwtable: 1194249423Sdim HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute"); 1195249423Sdim break; 1196193323Sed } 1197243830Sdim 1198249423Sdim Lex.Lex(); 1199249423Sdim } 1200249423Sdim} 1201249423Sdim 1202249423Sdim/// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes. 1203249423Sdimbool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) { 1204249423Sdim bool HaveError = false; 1205249423Sdim 1206249423Sdim B.clear(); 1207249423Sdim 1208249423Sdim while (1) { 1209249423Sdim lltok::Kind Token = Lex.getKind(); 1210243830Sdim switch (Token) { 1211249423Sdim default: // End of attributes. 1212249423Sdim return HaveError; 1213249423Sdim case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break; 1214249423Sdim case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break; 1215249423Sdim case lltok::kw_signext: B.addAttribute(Attribute::SExt); break; 1216249423Sdim case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break; 1217243830Sdim 1218249423Sdim // Error handling. 1219251662Sdim case lltok::kw_align: 1220251662Sdim case lltok::kw_byval: 1221251662Sdim case lltok::kw_nest: 1222251662Sdim case lltok::kw_nocapture: 1223251662Sdim case lltok::kw_returned: 1224251662Sdim case lltok::kw_sret: 1225249423Sdim HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute"); 1226243830Sdim break; 1227243830Sdim 1228251662Sdim case lltok::kw_alignstack: 1229251662Sdim case lltok::kw_alwaysinline: 1230263508Sdim case lltok::kw_builtin: 1231263508Sdim case lltok::kw_cold: 1232251662Sdim case lltok::kw_inlinehint: 1233251662Sdim case lltok::kw_minsize: 1234251662Sdim case lltok::kw_naked: 1235251662Sdim case lltok::kw_nobuiltin: 1236251662Sdim case lltok::kw_noduplicate: 1237251662Sdim case lltok::kw_noimplicitfloat: 1238251662Sdim case lltok::kw_noinline: 1239251662Sdim case lltok::kw_nonlazybind: 1240251662Sdim case lltok::kw_noredzone: 1241251662Sdim case lltok::kw_noreturn: 1242251662Sdim case lltok::kw_nounwind: 1243263508Sdim case lltok::kw_optnone: 1244251662Sdim case lltok::kw_optsize: 1245251662Sdim case lltok::kw_returns_twice: 1246251662Sdim case lltok::kw_sanitize_address: 1247251662Sdim case lltok::kw_sanitize_memory: 1248251662Sdim case lltok::kw_sanitize_thread: 1249251662Sdim case lltok::kw_ssp: 1250251662Sdim case lltok::kw_sspreq: 1251251662Sdim case lltok::kw_sspstrong: 1252251662Sdim case lltok::kw_uwtable: 1253249423Sdim HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute"); 1254243830Sdim break; 1255263508Sdim 1256263508Sdim case lltok::kw_readnone: 1257263508Sdim case lltok::kw_readonly: 1258263508Sdim HaveError |= Error(Lex.getLoc(), "invalid use of attribute on return type"); 1259243830Sdim } 1260243830Sdim 1261193323Sed Lex.Lex(); 1262193323Sed } 1263193323Sed} 1264193323Sed 1265193323Sed/// ParseOptionalLinkage 1266193323Sed/// ::= /*empty*/ 1267193323Sed/// ::= 'private' 1268198090Srdivacky/// ::= 'linker_private' 1269210299Sed/// ::= 'linker_private_weak' 1270193323Sed/// ::= 'internal' 1271193323Sed/// ::= 'weak' 1272193323Sed/// ::= 'weak_odr' 1273193323Sed/// ::= 'linkonce' 1274193323Sed/// ::= 'linkonce_odr' 1275210299Sed/// ::= 'available_externally' 1276193323Sed/// ::= 'appending' 1277193323Sed/// ::= 'dllexport' 1278193323Sed/// ::= 'common' 1279193323Sed/// ::= 'dllimport' 1280193323Sed/// ::= 'extern_weak' 1281193323Sed/// ::= 'external' 1282193323Sedbool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) { 1283193323Sed HasLinkage = false; 1284193323Sed switch (Lex.getKind()) { 1285198090Srdivacky default: Res=GlobalValue::ExternalLinkage; return false; 1286198090Srdivacky case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break; 1287198090Srdivacky case lltok::kw_linker_private: Res = GlobalValue::LinkerPrivateLinkage; break; 1288210299Sed case lltok::kw_linker_private_weak: 1289210299Sed Res = GlobalValue::LinkerPrivateWeakLinkage; 1290210299Sed break; 1291198090Srdivacky case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break; 1292198090Srdivacky case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break; 1293198090Srdivacky case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break; 1294198090Srdivacky case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break; 1295198090Srdivacky case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break; 1296193323Sed case lltok::kw_available_externally: 1297193323Sed Res = GlobalValue::AvailableExternallyLinkage; 1298193323Sed break; 1299198090Srdivacky case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break; 1300198090Srdivacky case lltok::kw_dllexport: Res = GlobalValue::DLLExportLinkage; break; 1301198090Srdivacky case lltok::kw_common: Res = GlobalValue::CommonLinkage; break; 1302198090Srdivacky case lltok::kw_dllimport: Res = GlobalValue::DLLImportLinkage; break; 1303198090Srdivacky case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break; 1304198090Srdivacky case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break; 1305193323Sed } 1306193323Sed Lex.Lex(); 1307193323Sed HasLinkage = true; 1308193323Sed return false; 1309193323Sed} 1310193323Sed 1311193323Sed/// ParseOptionalVisibility 1312193323Sed/// ::= /*empty*/ 1313193323Sed/// ::= 'default' 1314193323Sed/// ::= 'hidden' 1315193323Sed/// ::= 'protected' 1316198090Srdivacky/// 1317193323Sedbool LLParser::ParseOptionalVisibility(unsigned &Res) { 1318193323Sed switch (Lex.getKind()) { 1319193323Sed default: Res = GlobalValue::DefaultVisibility; return false; 1320193323Sed case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break; 1321193323Sed case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break; 1322193323Sed case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break; 1323193323Sed } 1324193323Sed Lex.Lex(); 1325193323Sed return false; 1326193323Sed} 1327193323Sed 1328193323Sed/// ParseOptionalCallingConv 1329193323Sed/// ::= /*empty*/ 1330193323Sed/// ::= 'ccc' 1331193323Sed/// ::= 'fastcc' 1332243830Sdim/// ::= 'kw_intel_ocl_bicc' 1333193323Sed/// ::= 'coldcc' 1334193323Sed/// ::= 'x86_stdcallcc' 1335193323Sed/// ::= 'x86_fastcallcc' 1336208599Srdivacky/// ::= 'x86_thiscallcc' 1337194612Sed/// ::= 'arm_apcscc' 1338194612Sed/// ::= 'arm_aapcscc' 1339194612Sed/// ::= 'arm_aapcs_vfpcc' 1340200581Srdivacky/// ::= 'msp430_intrcc' 1341218893Sdim/// ::= 'ptx_kernel' 1342218893Sdim/// ::= 'ptx_device' 1343243830Sdim/// ::= 'spir_func' 1344243830Sdim/// ::= 'spir_kernel' 1345256030Sdim/// ::= 'x86_64_sysvcc' 1346256030Sdim/// ::= 'x86_64_win64cc' 1347263508Sdim/// ::= 'webkit_jscc' 1348263508Sdim/// ::= 'anyregcc' 1349193323Sed/// ::= 'cc' UINT 1350194612Sed/// 1351198090Srdivackybool LLParser::ParseOptionalCallingConv(CallingConv::ID &CC) { 1352193323Sed switch (Lex.getKind()) { 1353193323Sed default: CC = CallingConv::C; return false; 1354193323Sed case lltok::kw_ccc: CC = CallingConv::C; break; 1355193323Sed case lltok::kw_fastcc: CC = CallingConv::Fast; break; 1356193323Sed case lltok::kw_coldcc: CC = CallingConv::Cold; break; 1357193323Sed case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break; 1358193323Sed case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break; 1359208599Srdivacky case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break; 1360194612Sed case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break; 1361194612Sed case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break; 1362194612Sed case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break; 1363200581Srdivacky case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break; 1364218893Sdim case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break; 1365218893Sdim case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break; 1366243830Sdim case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break; 1367243830Sdim case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break; 1368243830Sdim case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break; 1369256030Sdim case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break; 1370256030Sdim case lltok::kw_x86_64_win64cc: CC = CallingConv::X86_64_Win64; break; 1371263508Sdim case lltok::kw_webkit_jscc: CC = CallingConv::WebKit_JS; break; 1372263508Sdim case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break; 1373198090Srdivacky case lltok::kw_cc: { 1374198090Srdivacky unsigned ArbitraryCC; 1375198090Srdivacky Lex.Lex(); 1376234353Sdim if (ParseUInt32(ArbitraryCC)) 1377198090Srdivacky return true; 1378234353Sdim CC = static_cast<CallingConv::ID>(ArbitraryCC); 1379234353Sdim return false; 1380198090Srdivacky } 1381193323Sed } 1382198090Srdivacky 1383193323Sed Lex.Lex(); 1384193323Sed return false; 1385193323Sed} 1386193323Sed 1387201360Srdivacky/// ParseInstructionMetadata 1388201360Srdivacky/// ::= !dbg !42 (',' !dbg !57)* 1389212904Sdimbool LLParser::ParseInstructionMetadata(Instruction *Inst, 1390212904Sdim PerFunctionState *PFS) { 1391201360Srdivacky do { 1392201360Srdivacky if (Lex.getKind() != lltok::MetadataVar) 1393201360Srdivacky return TokError("expected metadata after comma"); 1394198090Srdivacky 1395201360Srdivacky std::string Name = Lex.getStrVal(); 1396234353Sdim unsigned MDK = M->getMDKindID(Name); 1397201360Srdivacky Lex.Lex(); 1398198396Srdivacky 1399201360Srdivacky MDNode *Node; 1400206083Srdivacky SMLoc Loc = Lex.getLoc(); 1401212904Sdim 1402212904Sdim if (ParseToken(lltok::exclaim, "expected '!' here")) 1403201360Srdivacky return true; 1404198090Srdivacky 1405212904Sdim // This code is similar to that of ParseMetadataValue, however it needs to 1406212904Sdim // have special-case code for a forward reference; see the comments on 1407212904Sdim // ForwardRefInstMetadata for details. Also, MDStrings are not supported 1408212904Sdim // at the top level here. 1409212904Sdim if (Lex.getKind() == lltok::lbrace) { 1410212904Sdim ValID ID; 1411212904Sdim if (ParseMetadataListValue(ID, PFS)) 1412212904Sdim return true; 1413212904Sdim assert(ID.Kind == ValID::t_MDNode); 1414212904Sdim Inst->setMetadata(MDK, ID.MDNodeVal); 1415206083Srdivacky } else { 1416218893Sdim unsigned NodeID = 0; 1417212904Sdim if (ParseMDNodeID(Node, NodeID)) 1418212904Sdim return true; 1419212904Sdim if (Node) { 1420212904Sdim // If we got the node, add it to the instruction. 1421212904Sdim Inst->setMetadata(MDK, Node); 1422212904Sdim } else { 1423212904Sdim MDRef R = { Loc, MDK, NodeID }; 1424212904Sdim // Otherwise, remember that this should be resolved later. 1425212904Sdim ForwardRefInstMetadata[Inst].push_back(R); 1426212904Sdim } 1427206083Srdivacky } 1428198090Srdivacky 1429263508Sdim if (MDK == LLVMContext::MD_tbaa) 1430263508Sdim InstsWithTBAATag.push_back(Inst); 1431263508Sdim 1432201360Srdivacky // If this is the end of the list, we're done. 1433201360Srdivacky } while (EatIfPresent(lltok::comma)); 1434198090Srdivacky return false; 1435198090Srdivacky} 1436198090Srdivacky 1437193323Sed/// ParseOptionalAlignment 1438193323Sed/// ::= /* empty */ 1439193323Sed/// ::= 'align' 4 1440193323Sedbool LLParser::ParseOptionalAlignment(unsigned &Alignment) { 1441193323Sed Alignment = 0; 1442193323Sed if (!EatIfPresent(lltok::kw_align)) 1443193323Sed return false; 1444193323Sed LocTy AlignLoc = Lex.getLoc(); 1445193323Sed if (ParseUInt32(Alignment)) return true; 1446193323Sed if (!isPowerOf2_32(Alignment)) 1447193323Sed return Error(AlignLoc, "alignment is not a power of two"); 1448212904Sdim if (Alignment > Value::MaximumAlignment) 1449212904Sdim return Error(AlignLoc, "huge alignments are not supported yet"); 1450193323Sed return false; 1451193323Sed} 1452193323Sed 1453201360Srdivacky/// ParseOptionalCommaAlign 1454249423Sdim/// ::= 1455201360Srdivacky/// ::= ',' align 4 1456201360Srdivacky/// 1457201360Srdivacky/// This returns with AteExtraComma set to true if it ate an excess comma at the 1458201360Srdivacky/// end. 1459201360Srdivackybool LLParser::ParseOptionalCommaAlign(unsigned &Alignment, 1460201360Srdivacky bool &AteExtraComma) { 1461201360Srdivacky AteExtraComma = false; 1462201360Srdivacky while (EatIfPresent(lltok::comma)) { 1463201360Srdivacky // Metadata at the end is an early exit. 1464201360Srdivacky if (Lex.getKind() == lltok::MetadataVar) { 1465201360Srdivacky AteExtraComma = true; 1466201360Srdivacky return false; 1467201360Srdivacky } 1468249423Sdim 1469207618Srdivacky if (Lex.getKind() != lltok::kw_align) 1470207618Srdivacky return Error(Lex.getLoc(), "expected metadata or 'align'"); 1471218893Sdim 1472207618Srdivacky if (ParseOptionalAlignment(Alignment)) return true; 1473201360Srdivacky } 1474198090Srdivacky 1475198090Srdivacky return false; 1476193323Sed} 1477193323Sed 1478226633Sdim/// ParseScopeAndOrdering 1479226633Sdim/// if isAtomic: ::= 'singlethread'? AtomicOrdering 1480226633Sdim/// else: ::= 1481226633Sdim/// 1482226633Sdim/// This sets Scope and Ordering to the parsed values. 1483226633Sdimbool LLParser::ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope, 1484226633Sdim AtomicOrdering &Ordering) { 1485226633Sdim if (!isAtomic) 1486226633Sdim return false; 1487226633Sdim 1488226633Sdim Scope = CrossThread; 1489226633Sdim if (EatIfPresent(lltok::kw_singlethread)) 1490226633Sdim Scope = SingleThread; 1491226633Sdim switch (Lex.getKind()) { 1492226633Sdim default: return TokError("Expected ordering on atomic instruction"); 1493226633Sdim case lltok::kw_unordered: Ordering = Unordered; break; 1494226633Sdim case lltok::kw_monotonic: Ordering = Monotonic; break; 1495226633Sdim case lltok::kw_acquire: Ordering = Acquire; break; 1496226633Sdim case lltok::kw_release: Ordering = Release; break; 1497226633Sdim case lltok::kw_acq_rel: Ordering = AcquireRelease; break; 1498226633Sdim case lltok::kw_seq_cst: Ordering = SequentiallyConsistent; break; 1499226633Sdim } 1500226633Sdim Lex.Lex(); 1501226633Sdim return false; 1502226633Sdim} 1503226633Sdim 1504203954Srdivacky/// ParseOptionalStackAlignment 1505203954Srdivacky/// ::= /* empty */ 1506203954Srdivacky/// ::= 'alignstack' '(' 4 ')' 1507203954Srdivackybool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) { 1508203954Srdivacky Alignment = 0; 1509203954Srdivacky if (!EatIfPresent(lltok::kw_alignstack)) 1510203954Srdivacky return false; 1511203954Srdivacky LocTy ParenLoc = Lex.getLoc(); 1512203954Srdivacky if (!EatIfPresent(lltok::lparen)) 1513203954Srdivacky return Error(ParenLoc, "expected '('"); 1514203954Srdivacky LocTy AlignLoc = Lex.getLoc(); 1515203954Srdivacky if (ParseUInt32(Alignment)) return true; 1516203954Srdivacky ParenLoc = Lex.getLoc(); 1517203954Srdivacky if (!EatIfPresent(lltok::rparen)) 1518203954Srdivacky return Error(ParenLoc, "expected ')'"); 1519203954Srdivacky if (!isPowerOf2_32(Alignment)) 1520203954Srdivacky return Error(AlignLoc, "stack alignment is not a power of two"); 1521203954Srdivacky return false; 1522203954Srdivacky} 1523198090Srdivacky 1524201360Srdivacky/// ParseIndexList - This parses the index list for an insert/extractvalue 1525201360Srdivacky/// instruction. This sets AteExtraComma in the case where we eat an extra 1526201360Srdivacky/// comma at the end of the line and find that it is followed by metadata. 1527201360Srdivacky/// Clients that don't allow metadata can call the version of this function that 1528201360Srdivacky/// only takes one argument. 1529201360Srdivacky/// 1530193323Sed/// ParseIndexList 1531193323Sed/// ::= (',' uint32)+ 1532201360Srdivacky/// 1533201360Srdivackybool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices, 1534201360Srdivacky bool &AteExtraComma) { 1535201360Srdivacky AteExtraComma = false; 1536249423Sdim 1537193323Sed if (Lex.getKind() != lltok::comma) 1538193323Sed return TokError("expected ',' as start of index list"); 1539198090Srdivacky 1540193323Sed while (EatIfPresent(lltok::comma)) { 1541201360Srdivacky if (Lex.getKind() == lltok::MetadataVar) { 1542201360Srdivacky AteExtraComma = true; 1543201360Srdivacky return false; 1544201360Srdivacky } 1545218893Sdim unsigned Idx = 0; 1546193323Sed if (ParseUInt32(Idx)) return true; 1547193323Sed Indices.push_back(Idx); 1548193323Sed } 1549198090Srdivacky 1550193323Sed return false; 1551193323Sed} 1552193323Sed 1553193323Sed//===----------------------------------------------------------------------===// 1554193323Sed// Type Parsing. 1555193323Sed//===----------------------------------------------------------------------===// 1556193323Sed 1557224145Sdim/// ParseType - Parse a type. 1558224145Sdimbool LLParser::ParseType(Type *&Result, bool AllowVoid) { 1559224145Sdim SMLoc TypeLoc = Lex.getLoc(); 1560193323Sed switch (Lex.getKind()) { 1561193323Sed default: 1562193323Sed return TokError("expected type"); 1563193323Sed case lltok::Type: 1564224145Sdim // Type ::= 'float' | 'void' (etc) 1565193323Sed Result = Lex.getTyVal(); 1566198090Srdivacky Lex.Lex(); 1567193323Sed break; 1568193323Sed case lltok::lbrace: 1569224145Sdim // Type ::= StructType 1570224145Sdim if (ParseAnonStructType(Result, false)) 1571193323Sed return true; 1572193323Sed break; 1573193323Sed case lltok::lsquare: 1574224145Sdim // Type ::= '[' ... ']' 1575193323Sed Lex.Lex(); // eat the lsquare. 1576193323Sed if (ParseArrayVectorType(Result, false)) 1577193323Sed return true; 1578193323Sed break; 1579193323Sed case lltok::less: // Either vector or packed struct. 1580224145Sdim // Type ::= '<' ... '>' 1581193323Sed Lex.Lex(); 1582193323Sed if (Lex.getKind() == lltok::lbrace) { 1583224145Sdim if (ParseAnonStructType(Result, true) || 1584193323Sed ParseToken(lltok::greater, "expected '>' at end of packed struct")) 1585193323Sed return true; 1586193323Sed } else if (ParseArrayVectorType(Result, true)) 1587193323Sed return true; 1588193323Sed break; 1589224145Sdim case lltok::LocalVar: { 1590224145Sdim // Type ::= %foo 1591224145Sdim std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()]; 1592249423Sdim 1593224145Sdim // If the type hasn't been defined yet, create a forward definition and 1594224145Sdim // remember where that forward def'n was seen (in case it never is defined). 1595224145Sdim if (Entry.first == 0) { 1596226633Sdim Entry.first = StructType::create(Context, Lex.getStrVal()); 1597224145Sdim Entry.second = Lex.getLoc(); 1598193323Sed } 1599224145Sdim Result = Entry.first; 1600193323Sed Lex.Lex(); 1601193323Sed break; 1602224145Sdim } 1603198090Srdivacky 1604224145Sdim case lltok::LocalVarID: { 1605224145Sdim // Type ::= %4 1606224145Sdim if (Lex.getUIntVal() >= NumberedTypes.size()) 1607224145Sdim NumberedTypes.resize(Lex.getUIntVal()+1); 1608224145Sdim std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()]; 1609249423Sdim 1610224145Sdim // If the type hasn't been defined yet, create a forward definition and 1611224145Sdim // remember where that forward def'n was seen (in case it never is defined). 1612224145Sdim if (Entry.first == 0) { 1613226633Sdim Entry.first = StructType::create(Context); 1614224145Sdim Entry.second = Lex.getLoc(); 1615193323Sed } 1616224145Sdim Result = Entry.first; 1617193323Sed Lex.Lex(); 1618193323Sed break; 1619193323Sed } 1620193323Sed } 1621198090Srdivacky 1622198090Srdivacky // Parse the type suffixes. 1623193323Sed while (1) { 1624193323Sed switch (Lex.getKind()) { 1625193323Sed // End of type. 1626224145Sdim default: 1627224145Sdim if (!AllowVoid && Result->isVoidTy()) 1628224145Sdim return Error(TypeLoc, "void type only allowed for function results"); 1629224145Sdim return false; 1630193323Sed 1631224145Sdim // Type ::= Type '*' 1632193323Sed case lltok::star: 1633224145Sdim if (Result->isLabelTy()) 1634193323Sed return TokError("basic block pointers are invalid"); 1635224145Sdim if (Result->isVoidTy()) 1636224145Sdim return TokError("pointers to void are invalid - use i8* instead"); 1637224145Sdim if (!PointerType::isValidElementType(Result)) 1638193630Sed return TokError("pointer to this type is invalid"); 1639224145Sdim Result = PointerType::getUnqual(Result); 1640193323Sed Lex.Lex(); 1641193323Sed break; 1642193323Sed 1643224145Sdim // Type ::= Type 'addrspace' '(' uint32 ')' '*' 1644193323Sed case lltok::kw_addrspace: { 1645224145Sdim if (Result->isLabelTy()) 1646193323Sed return TokError("basic block pointers are invalid"); 1647224145Sdim if (Result->isVoidTy()) 1648193323Sed return TokError("pointers to void are invalid; use i8* instead"); 1649224145Sdim if (!PointerType::isValidElementType(Result)) 1650193630Sed return TokError("pointer to this type is invalid"); 1651193323Sed unsigned AddrSpace; 1652193323Sed if (ParseOptionalAddrSpace(AddrSpace) || 1653193323Sed ParseToken(lltok::star, "expected '*' in address space")) 1654193323Sed return true; 1655193323Sed 1656224145Sdim Result = PointerType::get(Result, AddrSpace); 1657193323Sed break; 1658193323Sed } 1659198090Srdivacky 1660193323Sed /// Types '(' ArgTypeListI ')' OptFuncAttrs 1661193323Sed case lltok::lparen: 1662193323Sed if (ParseFunctionType(Result)) 1663193323Sed return true; 1664193323Sed break; 1665193323Sed } 1666193323Sed } 1667193323Sed} 1668193323Sed 1669193323Sed/// ParseParameterList 1670193323Sed/// ::= '(' ')' 1671193323Sed/// ::= '(' Arg (',' Arg)* ')' 1672193323Sed/// Arg 1673193323Sed/// ::= Type OptionalAttributes Value OptionalAttributes 1674193323Sedbool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList, 1675193323Sed PerFunctionState &PFS) { 1676193323Sed if (ParseToken(lltok::lparen, "expected '(' in call")) 1677193323Sed return true; 1678198090Srdivacky 1679249423Sdim unsigned AttrIndex = 1; 1680193323Sed while (Lex.getKind() != lltok::rparen) { 1681193323Sed // If this isn't the first argument, we need a comma. 1682193323Sed if (!ArgList.empty() && 1683193323Sed ParseToken(lltok::comma, "expected ',' in argument list")) 1684193323Sed return true; 1685198090Srdivacky 1686193323Sed // Parse the argument. 1687193323Sed LocTy ArgLoc; 1688224145Sdim Type *ArgTy = 0; 1689243830Sdim AttrBuilder ArgAttrs; 1690193323Sed Value *V; 1691200581Srdivacky if (ParseType(ArgTy, ArgLoc)) 1692193323Sed return true; 1693200581Srdivacky 1694201360Srdivacky // Otherwise, handle normal operands. 1695249423Sdim if (ParseOptionalParamAttrs(ArgAttrs) || ParseValue(ArgTy, V, PFS)) 1696201360Srdivacky return true; 1697249423Sdim ArgList.push_back(ParamInfo(ArgLoc, V, AttributeSet::get(V->getContext(), 1698249423Sdim AttrIndex++, 1699249423Sdim ArgAttrs))); 1700193323Sed } 1701193323Sed 1702193323Sed Lex.Lex(); // Lex the ')'. 1703193323Sed return false; 1704193323Sed} 1705193323Sed 1706193323Sed 1707193323Sed 1708193323Sed/// ParseArgumentList - Parse the argument list for a function type or function 1709224145Sdim/// prototype. 1710193323Sed/// ::= '(' ArgTypeListI ')' 1711193323Sed/// ArgTypeListI 1712193323Sed/// ::= /*empty*/ 1713193323Sed/// ::= '...' 1714193323Sed/// ::= ArgTypeList ',' '...' 1715193323Sed/// ::= ArgType (',' ArgType)* 1716193323Sed/// 1717224145Sdimbool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, 1718224145Sdim bool &isVarArg){ 1719193323Sed isVarArg = false; 1720193323Sed assert(Lex.getKind() == lltok::lparen); 1721193323Sed Lex.Lex(); // eat the (. 1722198090Srdivacky 1723193323Sed if (Lex.getKind() == lltok::rparen) { 1724193323Sed // empty 1725193323Sed } else if (Lex.getKind() == lltok::dotdotdot) { 1726193323Sed isVarArg = true; 1727193323Sed Lex.Lex(); 1728193323Sed } else { 1729193323Sed LocTy TypeLoc = Lex.getLoc(); 1730224145Sdim Type *ArgTy = 0; 1731243830Sdim AttrBuilder Attrs; 1732193323Sed std::string Name; 1733198090Srdivacky 1734224145Sdim if (ParseType(ArgTy) || 1735249423Sdim ParseOptionalParamAttrs(Attrs)) return true; 1736198090Srdivacky 1737198090Srdivacky if (ArgTy->isVoidTy()) 1738193323Sed return Error(TypeLoc, "argument can not have void type"); 1739198090Srdivacky 1740224145Sdim if (Lex.getKind() == lltok::LocalVar) { 1741193323Sed Name = Lex.getStrVal(); 1742193323Sed Lex.Lex(); 1743193323Sed } 1744193323Sed 1745193630Sed if (!FunctionType::isValidArgumentType(ArgTy)) 1746193323Sed return Error(TypeLoc, "invalid type for function argument"); 1747198090Srdivacky 1748249423Sdim unsigned AttrIndex = 1; 1749243830Sdim ArgList.push_back(ArgInfo(TypeLoc, ArgTy, 1750249423Sdim AttributeSet::get(ArgTy->getContext(), 1751249423Sdim AttrIndex++, Attrs), Name)); 1752198090Srdivacky 1753193323Sed while (EatIfPresent(lltok::comma)) { 1754193323Sed // Handle ... at end of arg list. 1755193323Sed if (EatIfPresent(lltok::dotdotdot)) { 1756193323Sed isVarArg = true; 1757193323Sed break; 1758193323Sed } 1759198090Srdivacky 1760193323Sed // Otherwise must be an argument type. 1761193323Sed TypeLoc = Lex.getLoc(); 1762249423Sdim if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) return true; 1763193323Sed 1764198090Srdivacky if (ArgTy->isVoidTy()) 1765193323Sed return Error(TypeLoc, "argument can not have void type"); 1766193323Sed 1767224145Sdim if (Lex.getKind() == lltok::LocalVar) { 1768193323Sed Name = Lex.getStrVal(); 1769193323Sed Lex.Lex(); 1770193323Sed } else { 1771193323Sed Name = ""; 1772193323Sed } 1773193323Sed 1774224145Sdim if (!ArgTy->isFirstClassType()) 1775193323Sed return Error(TypeLoc, "invalid type for function argument"); 1776198090Srdivacky 1777243830Sdim ArgList.push_back(ArgInfo(TypeLoc, ArgTy, 1778249423Sdim AttributeSet::get(ArgTy->getContext(), 1779249423Sdim AttrIndex++, Attrs), 1780243830Sdim Name)); 1781193323Sed } 1782193323Sed } 1783198090Srdivacky 1784193323Sed return ParseToken(lltok::rparen, "expected ')' at end of argument list"); 1785193323Sed} 1786198090Srdivacky 1787193323Sed/// ParseFunctionType 1788193323Sed/// ::= Type ArgumentList OptionalAttrs 1789224145Sdimbool LLParser::ParseFunctionType(Type *&Result) { 1790193323Sed assert(Lex.getKind() == lltok::lparen); 1791193323Sed 1792193323Sed if (!FunctionType::isValidReturnType(Result)) 1793193323Sed return TokError("invalid function return type"); 1794198090Srdivacky 1795224145Sdim SmallVector<ArgInfo, 8> ArgList; 1796193323Sed bool isVarArg; 1797224145Sdim if (ParseArgumentList(ArgList, isVarArg)) 1798193323Sed return true; 1799198090Srdivacky 1800193323Sed // Reject names on the arguments lists. 1801193323Sed for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { 1802193323Sed if (!ArgList[i].Name.empty()) 1803193323Sed return Error(ArgList[i].Loc, "argument name invalid in function type"); 1804249423Sdim if (ArgList[i].Attrs.hasAttributes(i + 1)) 1805224145Sdim return Error(ArgList[i].Loc, 1806224145Sdim "argument attributes invalid in function type"); 1807193323Sed } 1808198090Srdivacky 1809224145Sdim SmallVector<Type*, 16> ArgListTy; 1810193323Sed for (unsigned i = 0, e = ArgList.size(); i != e; ++i) 1811224145Sdim ArgListTy.push_back(ArgList[i].Ty); 1812198090Srdivacky 1813224145Sdim Result = FunctionType::get(Result, ArgListTy, isVarArg); 1814193323Sed return false; 1815193323Sed} 1816193323Sed 1817224145Sdim/// ParseAnonStructType - Parse an anonymous struct type, which is inlined into 1818224145Sdim/// other structs. 1819224145Sdimbool LLParser::ParseAnonStructType(Type *&Result, bool Packed) { 1820224145Sdim SmallVector<Type*, 8> Elts; 1821224145Sdim if (ParseStructBody(Elts)) return true; 1822249423Sdim 1823224145Sdim Result = StructType::get(Context, Elts, Packed); 1824224145Sdim return false; 1825224145Sdim} 1826224145Sdim 1827224145Sdim/// ParseStructDefinition - Parse a struct in a 'type' definition. 1828224145Sdimbool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name, 1829224145Sdim std::pair<Type*, LocTy> &Entry, 1830224145Sdim Type *&ResultTy) { 1831224145Sdim // If the type was already defined, diagnose the redefinition. 1832224145Sdim if (Entry.first && !Entry.second.isValid()) 1833224145Sdim return Error(TypeLoc, "redefinition of type"); 1834249423Sdim 1835224145Sdim // If we have opaque, just return without filling in the definition for the 1836224145Sdim // struct. This counts as a definition as far as the .ll file goes. 1837224145Sdim if (EatIfPresent(lltok::kw_opaque)) { 1838224145Sdim // This type is being defined, so clear the location to indicate this. 1839224145Sdim Entry.second = SMLoc(); 1840249423Sdim 1841224145Sdim // If this type number has never been uttered, create it. 1842224145Sdim if (Entry.first == 0) 1843226633Sdim Entry.first = StructType::create(Context, Name); 1844224145Sdim ResultTy = Entry.first; 1845224145Sdim return false; 1846224145Sdim } 1847249423Sdim 1848224145Sdim // If the type starts with '<', then it is either a packed struct or a vector. 1849224145Sdim bool isPacked = EatIfPresent(lltok::less); 1850224145Sdim 1851224145Sdim // If we don't have a struct, then we have a random type alias, which we 1852224145Sdim // accept for compatibility with old files. These types are not allowed to be 1853224145Sdim // forward referenced and not allowed to be recursive. 1854224145Sdim if (Lex.getKind() != lltok::lbrace) { 1855224145Sdim if (Entry.first) 1856224145Sdim return Error(TypeLoc, "forward references to non-struct type"); 1857249423Sdim 1858224145Sdim ResultTy = 0; 1859224145Sdim if (isPacked) 1860224145Sdim return ParseArrayVectorType(ResultTy, true); 1861224145Sdim return ParseType(ResultTy); 1862224145Sdim } 1863249423Sdim 1864224145Sdim // This type is being defined, so clear the location to indicate this. 1865224145Sdim Entry.second = SMLoc(); 1866249423Sdim 1867224145Sdim // If this type number has never been uttered, create it. 1868224145Sdim if (Entry.first == 0) 1869226633Sdim Entry.first = StructType::create(Context, Name); 1870249423Sdim 1871224145Sdim StructType *STy = cast<StructType>(Entry.first); 1872249423Sdim 1873224145Sdim SmallVector<Type*, 8> Body; 1874224145Sdim if (ParseStructBody(Body) || 1875224145Sdim (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct"))) 1876224145Sdim return true; 1877249423Sdim 1878224145Sdim STy->setBody(Body, isPacked); 1879224145Sdim ResultTy = STy; 1880224145Sdim return false; 1881224145Sdim} 1882224145Sdim 1883224145Sdim 1884193323Sed/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere. 1885224145Sdim/// StructType 1886193323Sed/// ::= '{' '}' 1887224145Sdim/// ::= '{' Type (',' Type)* '}' 1888193323Sed/// ::= '<' '{' '}' '>' 1889224145Sdim/// ::= '<' '{' Type (',' Type)* '}' '>' 1890224145Sdimbool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) { 1891193323Sed assert(Lex.getKind() == lltok::lbrace); 1892193323Sed Lex.Lex(); // Consume the '{' 1893198090Srdivacky 1894224145Sdim // Handle the empty struct. 1895224145Sdim if (EatIfPresent(lltok::rbrace)) 1896193323Sed return false; 1897193323Sed 1898193323Sed LocTy EltTyLoc = Lex.getLoc(); 1899224145Sdim Type *Ty = 0; 1900224145Sdim if (ParseType(Ty)) return true; 1901224145Sdim Body.push_back(Ty); 1902198090Srdivacky 1903224145Sdim if (!StructType::isValidElementType(Ty)) 1904193630Sed return Error(EltTyLoc, "invalid element type for struct"); 1905198090Srdivacky 1906193323Sed while (EatIfPresent(lltok::comma)) { 1907193323Sed EltTyLoc = Lex.getLoc(); 1908224145Sdim if (ParseType(Ty)) return true; 1909198090Srdivacky 1910224145Sdim if (!StructType::isValidElementType(Ty)) 1911193630Sed return Error(EltTyLoc, "invalid element type for struct"); 1912198090Srdivacky 1913224145Sdim Body.push_back(Ty); 1914193323Sed } 1915198090Srdivacky 1916224145Sdim return ParseToken(lltok::rbrace, "expected '}' at end of struct"); 1917193323Sed} 1918193323Sed 1919193323Sed/// ParseArrayVectorType - Parse an array or vector type, assuming the first 1920193323Sed/// token has already been consumed. 1921224145Sdim/// Type 1922193323Sed/// ::= '[' APSINTVAL 'x' Types ']' 1923193323Sed/// ::= '<' APSINTVAL 'x' Types '>' 1924224145Sdimbool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) { 1925193323Sed if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() || 1926193323Sed Lex.getAPSIntVal().getBitWidth() > 64) 1927193323Sed return TokError("expected number in address space"); 1928198090Srdivacky 1929193323Sed LocTy SizeLoc = Lex.getLoc(); 1930193323Sed uint64_t Size = Lex.getAPSIntVal().getZExtValue(); 1931193323Sed Lex.Lex(); 1932198090Srdivacky 1933193323Sed if (ParseToken(lltok::kw_x, "expected 'x' after element count")) 1934193323Sed return true; 1935193323Sed 1936193323Sed LocTy TypeLoc = Lex.getLoc(); 1937224145Sdim Type *EltTy = 0; 1938224145Sdim if (ParseType(EltTy)) return true; 1939198090Srdivacky 1940193323Sed if (ParseToken(isVector ? lltok::greater : lltok::rsquare, 1941193323Sed "expected end of sequential type")) 1942193323Sed return true; 1943198090Srdivacky 1944193323Sed if (isVector) { 1945193323Sed if (Size == 0) 1946193323Sed return Error(SizeLoc, "zero element vector is illegal"); 1947193323Sed if ((unsigned)Size != Size) 1948193323Sed return Error(SizeLoc, "size too large for vector"); 1949193630Sed if (!VectorType::isValidElementType(EltTy)) 1950249423Sdim return Error(TypeLoc, "invalid vector element type"); 1951198090Srdivacky Result = VectorType::get(EltTy, unsigned(Size)); 1952193323Sed } else { 1953193630Sed if (!ArrayType::isValidElementType(EltTy)) 1954193323Sed return Error(TypeLoc, "invalid array element type"); 1955224145Sdim Result = ArrayType::get(EltTy, Size); 1956193323Sed } 1957193323Sed return false; 1958193323Sed} 1959193323Sed 1960193323Sed//===----------------------------------------------------------------------===// 1961193323Sed// Function Semantic Analysis. 1962193323Sed//===----------------------------------------------------------------------===// 1963193323Sed 1964198892SrdivackyLLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f, 1965198892Srdivacky int functionNumber) 1966198892Srdivacky : P(p), F(f), FunctionNumber(functionNumber) { 1967193323Sed 1968193323Sed // Insert unnamed arguments into the NumberedVals list. 1969193323Sed for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); 1970193323Sed AI != E; ++AI) 1971193323Sed if (!AI->hasName()) 1972193323Sed NumberedVals.push_back(AI); 1973193323Sed} 1974193323Sed 1975193323SedLLParser::PerFunctionState::~PerFunctionState() { 1976193323Sed // If there were any forward referenced non-basicblock values, delete them. 1977193323Sed for (std::map<std::string, std::pair<Value*, LocTy> >::iterator 1978193323Sed I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I) 1979193323Sed if (!isa<BasicBlock>(I->second.first)) { 1980195340Sed I->second.first->replaceAllUsesWith( 1981198090Srdivacky UndefValue::get(I->second.first->getType())); 1982193323Sed delete I->second.first; 1983193323Sed I->second.first = 0; 1984193323Sed } 1985198090Srdivacky 1986193323Sed for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator 1987193323Sed I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I) 1988193323Sed if (!isa<BasicBlock>(I->second.first)) { 1989195340Sed I->second.first->replaceAllUsesWith( 1990198090Srdivacky UndefValue::get(I->second.first->getType())); 1991193323Sed delete I->second.first; 1992193323Sed I->second.first = 0; 1993193323Sed } 1994193323Sed} 1995193323Sed 1996198892Srdivackybool LLParser::PerFunctionState::FinishFunction() { 1997198892Srdivacky // Check to see if someone took the address of labels in this block. 1998198892Srdivacky if (!P.ForwardRefBlockAddresses.empty()) { 1999198892Srdivacky ValID FunctionID; 2000198892Srdivacky if (!F.getName().empty()) { 2001198892Srdivacky FunctionID.Kind = ValID::t_GlobalName; 2002198892Srdivacky FunctionID.StrVal = F.getName(); 2003198892Srdivacky } else { 2004198892Srdivacky FunctionID.Kind = ValID::t_GlobalID; 2005198892Srdivacky FunctionID.UIntVal = FunctionNumber; 2006198892Srdivacky } 2007249423Sdim 2008198892Srdivacky std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >::iterator 2009198892Srdivacky FRBAI = P.ForwardRefBlockAddresses.find(FunctionID); 2010198892Srdivacky if (FRBAI != P.ForwardRefBlockAddresses.end()) { 2011198892Srdivacky // Resolve all these references. 2012198892Srdivacky if (P.ResolveForwardRefBlockAddresses(&F, FRBAI->second, this)) 2013198892Srdivacky return true; 2014249423Sdim 2015198892Srdivacky P.ForwardRefBlockAddresses.erase(FRBAI); 2016198892Srdivacky } 2017198892Srdivacky } 2018249423Sdim 2019193323Sed if (!ForwardRefVals.empty()) 2020193323Sed return P.Error(ForwardRefVals.begin()->second.second, 2021193323Sed "use of undefined value '%" + ForwardRefVals.begin()->first + 2022193323Sed "'"); 2023193323Sed if (!ForwardRefValIDs.empty()) 2024193323Sed return P.Error(ForwardRefValIDs.begin()->second.second, 2025193323Sed "use of undefined value '%" + 2026218893Sdim Twine(ForwardRefValIDs.begin()->first) + "'"); 2027193323Sed return false; 2028193323Sed} 2029193323Sed 2030193323Sed 2031193323Sed/// GetVal - Get a value with the specified name or ID, creating a 2032193323Sed/// forward reference record if needed. This can return null if the value 2033193323Sed/// exists but does not have the right type. 2034193323SedValue *LLParser::PerFunctionState::GetVal(const std::string &Name, 2035226633Sdim Type *Ty, LocTy Loc) { 2036193323Sed // Look this name up in the normal function symbol table. 2037193323Sed Value *Val = F.getValueSymbolTable().lookup(Name); 2038198090Srdivacky 2039193323Sed // If this is a forward reference for the value, see if we already created a 2040193323Sed // forward ref record. 2041193323Sed if (Val == 0) { 2042193323Sed std::map<std::string, std::pair<Value*, LocTy> >::iterator 2043193323Sed I = ForwardRefVals.find(Name); 2044193323Sed if (I != ForwardRefVals.end()) 2045193323Sed Val = I->second.first; 2046193323Sed } 2047198090Srdivacky 2048193323Sed // If we have the value in the symbol table or fwd-ref table, return it. 2049193323Sed if (Val) { 2050193323Sed if (Val->getType() == Ty) return Val; 2051198090Srdivacky if (Ty->isLabelTy()) 2052193323Sed P.Error(Loc, "'%" + Name + "' is not a basic block"); 2053193323Sed else 2054193323Sed P.Error(Loc, "'%" + Name + "' defined with type '" + 2055224145Sdim getTypeString(Val->getType()) + "'"); 2056193323Sed return 0; 2057193323Sed } 2058198090Srdivacky 2059193323Sed // Don't make placeholders with invalid type. 2060224145Sdim if (!Ty->isFirstClassType() && !Ty->isLabelTy()) { 2061193323Sed P.Error(Loc, "invalid use of a non-first-class type"); 2062193323Sed return 0; 2063193323Sed } 2064198090Srdivacky 2065193323Sed // Otherwise, create a new forward reference for this value and remember it. 2066193323Sed Value *FwdVal; 2067198090Srdivacky if (Ty->isLabelTy()) 2068198090Srdivacky FwdVal = BasicBlock::Create(F.getContext(), Name, &F); 2069193323Sed else 2070193323Sed FwdVal = new Argument(Ty, Name); 2071198090Srdivacky 2072193323Sed ForwardRefVals[Name] = std::make_pair(FwdVal, Loc); 2073193323Sed return FwdVal; 2074193323Sed} 2075193323Sed 2076226633SdimValue *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty, 2077193323Sed LocTy Loc) { 2078193323Sed // Look this name up in the normal function symbol table. 2079193323Sed Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0; 2080198090Srdivacky 2081193323Sed // If this is a forward reference for the value, see if we already created a 2082193323Sed // forward ref record. 2083193323Sed if (Val == 0) { 2084193323Sed std::map<unsigned, std::pair<Value*, LocTy> >::iterator 2085193323Sed I = ForwardRefValIDs.find(ID); 2086193323Sed if (I != ForwardRefValIDs.end()) 2087193323Sed Val = I->second.first; 2088193323Sed } 2089198090Srdivacky 2090193323Sed // If we have the value in the symbol table or fwd-ref table, return it. 2091193323Sed if (Val) { 2092193323Sed if (Val->getType() == Ty) return Val; 2093198090Srdivacky if (Ty->isLabelTy()) 2094218893Sdim P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block"); 2095193323Sed else 2096218893Sdim P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" + 2097224145Sdim getTypeString(Val->getType()) + "'"); 2098193323Sed return 0; 2099193323Sed } 2100198090Srdivacky 2101224145Sdim if (!Ty->isFirstClassType() && !Ty->isLabelTy()) { 2102193323Sed P.Error(Loc, "invalid use of a non-first-class type"); 2103193323Sed return 0; 2104193323Sed } 2105198090Srdivacky 2106193323Sed // Otherwise, create a new forward reference for this value and remember it. 2107193323Sed Value *FwdVal; 2108198090Srdivacky if (Ty->isLabelTy()) 2109198090Srdivacky FwdVal = BasicBlock::Create(F.getContext(), "", &F); 2110193323Sed else 2111193323Sed FwdVal = new Argument(Ty); 2112198090Srdivacky 2113193323Sed ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc); 2114193323Sed return FwdVal; 2115193323Sed} 2116193323Sed 2117193323Sed/// SetInstName - After an instruction is parsed and inserted into its 2118193323Sed/// basic block, this installs its name. 2119193323Sedbool LLParser::PerFunctionState::SetInstName(int NameID, 2120193323Sed const std::string &NameStr, 2121193323Sed LocTy NameLoc, Instruction *Inst) { 2122193323Sed // If this instruction has void type, it cannot have a name or ID specified. 2123198090Srdivacky if (Inst->getType()->isVoidTy()) { 2124193323Sed if (NameID != -1 || !NameStr.empty()) 2125193323Sed return P.Error(NameLoc, "instructions returning void cannot have a name"); 2126193323Sed return false; 2127193323Sed } 2128198090Srdivacky 2129193323Sed // If this was a numbered instruction, verify that the instruction is the 2130193323Sed // expected value and resolve any forward references. 2131193323Sed if (NameStr.empty()) { 2132193323Sed // If neither a name nor an ID was specified, just use the next ID. 2133193323Sed if (NameID == -1) 2134193323Sed NameID = NumberedVals.size(); 2135198090Srdivacky 2136193323Sed if (unsigned(NameID) != NumberedVals.size()) 2137193323Sed return P.Error(NameLoc, "instruction expected to be numbered '%" + 2138218893Sdim Twine(NumberedVals.size()) + "'"); 2139198090Srdivacky 2140193323Sed std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI = 2141193323Sed ForwardRefValIDs.find(NameID); 2142193323Sed if (FI != ForwardRefValIDs.end()) { 2143193323Sed if (FI->second.first->getType() != Inst->getType()) 2144198090Srdivacky return P.Error(NameLoc, "instruction forward referenced with type '" + 2145224145Sdim getTypeString(FI->second.first->getType()) + "'"); 2146193323Sed FI->second.first->replaceAllUsesWith(Inst); 2147198090Srdivacky delete FI->second.first; 2148193323Sed ForwardRefValIDs.erase(FI); 2149193323Sed } 2150193323Sed 2151193323Sed NumberedVals.push_back(Inst); 2152193323Sed return false; 2153193323Sed } 2154193323Sed 2155193323Sed // Otherwise, the instruction had a name. Resolve forward refs and set it. 2156193323Sed std::map<std::string, std::pair<Value*, LocTy> >::iterator 2157193323Sed FI = ForwardRefVals.find(NameStr); 2158193323Sed if (FI != ForwardRefVals.end()) { 2159193323Sed if (FI->second.first->getType() != Inst->getType()) 2160198090Srdivacky return P.Error(NameLoc, "instruction forward referenced with type '" + 2161224145Sdim getTypeString(FI->second.first->getType()) + "'"); 2162193323Sed FI->second.first->replaceAllUsesWith(Inst); 2163198090Srdivacky delete FI->second.first; 2164193323Sed ForwardRefVals.erase(FI); 2165193323Sed } 2166198090Srdivacky 2167193323Sed // Set the name on the instruction. 2168193323Sed Inst->setName(NameStr); 2169198090Srdivacky 2170218893Sdim if (Inst->getName() != NameStr) 2171198090Srdivacky return P.Error(NameLoc, "multiple definition of local value named '" + 2172193323Sed NameStr + "'"); 2173193323Sed return false; 2174193323Sed} 2175193323Sed 2176193323Sed/// GetBB - Get a basic block with the specified name or ID, creating a 2177193323Sed/// forward reference record if needed. 2178193323SedBasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name, 2179193323Sed LocTy Loc) { 2180198090Srdivacky return cast_or_null<BasicBlock>(GetVal(Name, 2181198090Srdivacky Type::getLabelTy(F.getContext()), Loc)); 2182193323Sed} 2183193323Sed 2184193323SedBasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) { 2185198090Srdivacky return cast_or_null<BasicBlock>(GetVal(ID, 2186198090Srdivacky Type::getLabelTy(F.getContext()), Loc)); 2187193323Sed} 2188193323Sed 2189193323Sed/// DefineBB - Define the specified basic block, which is either named or 2190193323Sed/// unnamed. If there is an error, this returns null otherwise it returns 2191193323Sed/// the block being defined. 2192193323SedBasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name, 2193193323Sed LocTy Loc) { 2194193323Sed BasicBlock *BB; 2195193323Sed if (Name.empty()) 2196193323Sed BB = GetBB(NumberedVals.size(), Loc); 2197193323Sed else 2198193323Sed BB = GetBB(Name, Loc); 2199193323Sed if (BB == 0) return 0; // Already diagnosed error. 2200198090Srdivacky 2201193323Sed // Move the block to the end of the function. Forward ref'd blocks are 2202193323Sed // inserted wherever they happen to be referenced. 2203193323Sed F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB); 2204198090Srdivacky 2205193323Sed // Remove the block from forward ref sets. 2206193323Sed if (Name.empty()) { 2207193323Sed ForwardRefValIDs.erase(NumberedVals.size()); 2208193323Sed NumberedVals.push_back(BB); 2209193323Sed } else { 2210193323Sed // BB forward references are already in the function symbol table. 2211193323Sed ForwardRefVals.erase(Name); 2212193323Sed } 2213198090Srdivacky 2214193323Sed return BB; 2215193323Sed} 2216193323Sed 2217193323Sed//===----------------------------------------------------------------------===// 2218193323Sed// Constants. 2219193323Sed//===----------------------------------------------------------------------===// 2220193323Sed 2221193323Sed/// ParseValID - Parse an abstract value that doesn't necessarily have a 2222193323Sed/// type implied. For example, if we parse "4" we don't know what integer type 2223193323Sed/// it has. The value will later be combined with its type and checked for 2224202375Srdivacky/// sanity. PFS is used to convert function-local operands of metadata (since 2225202375Srdivacky/// metadata operands are not just parsed here but also converted to values). 2226202375Srdivacky/// PFS can be null when we are not parsing metadata values inside a function. 2227202375Srdivackybool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) { 2228193323Sed ID.Loc = Lex.getLoc(); 2229193323Sed switch (Lex.getKind()) { 2230193323Sed default: return TokError("expected value token"); 2231193323Sed case lltok::GlobalID: // @42 2232193323Sed ID.UIntVal = Lex.getUIntVal(); 2233193323Sed ID.Kind = ValID::t_GlobalID; 2234193323Sed break; 2235193323Sed case lltok::GlobalVar: // @foo 2236193323Sed ID.StrVal = Lex.getStrVal(); 2237193323Sed ID.Kind = ValID::t_GlobalName; 2238193323Sed break; 2239193323Sed case lltok::LocalVarID: // %42 2240193323Sed ID.UIntVal = Lex.getUIntVal(); 2241193323Sed ID.Kind = ValID::t_LocalID; 2242193323Sed break; 2243193323Sed case lltok::LocalVar: // %foo 2244193323Sed ID.StrVal = Lex.getStrVal(); 2245193323Sed ID.Kind = ValID::t_LocalName; 2246193323Sed break; 2247210299Sed case lltok::exclaim: // !42, !{...}, or !"foo" 2248210299Sed return ParseMetadataValue(ID, PFS); 2249193323Sed case lltok::APSInt: 2250198090Srdivacky ID.APSIntVal = Lex.getAPSIntVal(); 2251193323Sed ID.Kind = ValID::t_APSInt; 2252193323Sed break; 2253193323Sed case lltok::APFloat: 2254193323Sed ID.APFloatVal = Lex.getAPFloatVal(); 2255193323Sed ID.Kind = ValID::t_APFloat; 2256193323Sed break; 2257193323Sed case lltok::kw_true: 2258198090Srdivacky ID.ConstantVal = ConstantInt::getTrue(Context); 2259193323Sed ID.Kind = ValID::t_Constant; 2260193323Sed break; 2261193323Sed case lltok::kw_false: 2262198090Srdivacky ID.ConstantVal = ConstantInt::getFalse(Context); 2263193323Sed ID.Kind = ValID::t_Constant; 2264193323Sed break; 2265193323Sed case lltok::kw_null: ID.Kind = ValID::t_Null; break; 2266193323Sed case lltok::kw_undef: ID.Kind = ValID::t_Undef; break; 2267193323Sed case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break; 2268198090Srdivacky 2269193323Sed case lltok::lbrace: { 2270193323Sed // ValID ::= '{' ConstVector '}' 2271193323Sed Lex.Lex(); 2272193323Sed SmallVector<Constant*, 16> Elts; 2273193323Sed if (ParseGlobalValueVector(Elts) || 2274193323Sed ParseToken(lltok::rbrace, "expected end of struct constant")) 2275193323Sed return true; 2276198090Srdivacky 2277224145Sdim ID.ConstantStructElts = new Constant*[Elts.size()]; 2278224145Sdim ID.UIntVal = Elts.size(); 2279224145Sdim memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0])); 2280224145Sdim ID.Kind = ValID::t_ConstantStruct; 2281193323Sed return false; 2282193323Sed } 2283193323Sed case lltok::less: { 2284193323Sed // ValID ::= '<' ConstVector '>' --> Vector. 2285193323Sed // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct. 2286193323Sed Lex.Lex(); 2287193323Sed bool isPackedStruct = EatIfPresent(lltok::lbrace); 2288198090Srdivacky 2289193323Sed SmallVector<Constant*, 16> Elts; 2290193323Sed LocTy FirstEltLoc = Lex.getLoc(); 2291193323Sed if (ParseGlobalValueVector(Elts) || 2292193323Sed (isPackedStruct && 2293193323Sed ParseToken(lltok::rbrace, "expected end of packed struct")) || 2294193323Sed ParseToken(lltok::greater, "expected end of constant")) 2295193323Sed return true; 2296198090Srdivacky 2297193323Sed if (isPackedStruct) { 2298224145Sdim ID.ConstantStructElts = new Constant*[Elts.size()]; 2299224145Sdim memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0])); 2300224145Sdim ID.UIntVal = Elts.size(); 2301224145Sdim ID.Kind = ValID::t_PackedConstantStruct; 2302193323Sed return false; 2303193323Sed } 2304198090Srdivacky 2305193323Sed if (Elts.empty()) 2306193323Sed return Error(ID.Loc, "constant vector must not be empty"); 2307193323Sed 2308203954Srdivacky if (!Elts[0]->getType()->isIntegerTy() && 2309234353Sdim !Elts[0]->getType()->isFloatingPointTy() && 2310234353Sdim !Elts[0]->getType()->isPointerTy()) 2311193323Sed return Error(FirstEltLoc, 2312234353Sdim "vector elements must have integer, pointer or floating point type"); 2313198090Srdivacky 2314193323Sed // Verify that all the vector elements have the same type. 2315193323Sed for (unsigned i = 1, e = Elts.size(); i != e; ++i) 2316193323Sed if (Elts[i]->getType() != Elts[0]->getType()) 2317193323Sed return Error(FirstEltLoc, 2318218893Sdim "vector element #" + Twine(i) + 2319224145Sdim " is not of type '" + getTypeString(Elts[0]->getType())); 2320198090Srdivacky 2321218893Sdim ID.ConstantVal = ConstantVector::get(Elts); 2322193323Sed ID.Kind = ValID::t_Constant; 2323193323Sed return false; 2324193323Sed } 2325193323Sed case lltok::lsquare: { // Array Constant 2326193323Sed Lex.Lex(); 2327193323Sed SmallVector<Constant*, 16> Elts; 2328193323Sed LocTy FirstEltLoc = Lex.getLoc(); 2329193323Sed if (ParseGlobalValueVector(Elts) || 2330193323Sed ParseToken(lltok::rsquare, "expected end of array constant")) 2331193323Sed return true; 2332193323Sed 2333193323Sed // Handle empty element. 2334193323Sed if (Elts.empty()) { 2335193323Sed // Use undef instead of an array because it's inconvenient to determine 2336193323Sed // the element type at this point, there being no elements to examine. 2337193323Sed ID.Kind = ValID::t_EmptyArray; 2338193323Sed return false; 2339193323Sed } 2340198090Srdivacky 2341193323Sed if (!Elts[0]->getType()->isFirstClassType()) 2342198090Srdivacky return Error(FirstEltLoc, "invalid array element type: " + 2343224145Sdim getTypeString(Elts[0]->getType())); 2344198090Srdivacky 2345198090Srdivacky ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size()); 2346198090Srdivacky 2347193323Sed // Verify all elements are correct type! 2348193323Sed for (unsigned i = 0, e = Elts.size(); i != e; ++i) { 2349193323Sed if (Elts[i]->getType() != Elts[0]->getType()) 2350193323Sed return Error(FirstEltLoc, 2351218893Sdim "array element #" + Twine(i) + 2352224145Sdim " is not of type '" + getTypeString(Elts[0]->getType())); 2353193323Sed } 2354198090Srdivacky 2355224145Sdim ID.ConstantVal = ConstantArray::get(ATy, Elts); 2356193323Sed ID.Kind = ValID::t_Constant; 2357193323Sed return false; 2358193323Sed } 2359193323Sed case lltok::kw_c: // c "foo" 2360193323Sed Lex.Lex(); 2361234353Sdim ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(), 2362234353Sdim false); 2363193323Sed if (ParseToken(lltok::StringConstant, "expected string")) return true; 2364193323Sed ID.Kind = ValID::t_Constant; 2365193323Sed return false; 2366193323Sed 2367193323Sed case lltok::kw_asm: { 2368249423Sdim // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ',' 2369249423Sdim // STRINGCONSTANT 2370243830Sdim bool HasSideEffect, AlignStack, AsmDialect; 2371193323Sed Lex.Lex(); 2372193323Sed if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) || 2373198396Srdivacky ParseOptionalToken(lltok::kw_alignstack, AlignStack) || 2374243830Sdim ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) || 2375193323Sed ParseStringConstant(ID.StrVal) || 2376193323Sed ParseToken(lltok::comma, "expected comma in inline asm expression") || 2377193323Sed ParseToken(lltok::StringConstant, "expected constraint string")) 2378193323Sed return true; 2379193323Sed ID.StrVal2 = Lex.getStrVal(); 2380243830Sdim ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) | 2381243830Sdim (unsigned(AsmDialect)<<2); 2382193323Sed ID.Kind = ValID::t_InlineAsm; 2383193323Sed return false; 2384193323Sed } 2385198090Srdivacky 2386198892Srdivacky case lltok::kw_blockaddress: { 2387198892Srdivacky // ValID ::= 'blockaddress' '(' @foo ',' %bar ')' 2388198892Srdivacky Lex.Lex(); 2389198892Srdivacky 2390198892Srdivacky ValID Fn, Label; 2391249423Sdim 2392198892Srdivacky if (ParseToken(lltok::lparen, "expected '(' in block address expression") || 2393198892Srdivacky ParseValID(Fn) || 2394198892Srdivacky ParseToken(lltok::comma, "expected comma in block address expression")|| 2395198892Srdivacky ParseValID(Label) || 2396198892Srdivacky ParseToken(lltok::rparen, "expected ')' in block address expression")) 2397198892Srdivacky return true; 2398249423Sdim 2399198892Srdivacky if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName) 2400198892Srdivacky return Error(Fn.Loc, "expected function name in blockaddress"); 2401198892Srdivacky if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName) 2402198892Srdivacky return Error(Label.Loc, "expected basic block name in blockaddress"); 2403249423Sdim 2404198892Srdivacky // Make a global variable as a placeholder for this reference. 2405198892Srdivacky GlobalVariable *FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context), 2406198892Srdivacky false, GlobalValue::InternalLinkage, 2407198892Srdivacky 0, ""); 2408198892Srdivacky ForwardRefBlockAddresses[Fn].push_back(std::make_pair(Label, FwdRef)); 2409198892Srdivacky ID.ConstantVal = FwdRef; 2410198892Srdivacky ID.Kind = ValID::t_Constant; 2411198892Srdivacky return false; 2412198892Srdivacky } 2413249423Sdim 2414193323Sed case lltok::kw_trunc: 2415193323Sed case lltok::kw_zext: 2416193323Sed case lltok::kw_sext: 2417193323Sed case lltok::kw_fptrunc: 2418193323Sed case lltok::kw_fpext: 2419193323Sed case lltok::kw_bitcast: 2420263508Sdim case lltok::kw_addrspacecast: 2421193323Sed case lltok::kw_uitofp: 2422193323Sed case lltok::kw_sitofp: 2423193323Sed case lltok::kw_fptoui: 2424198090Srdivacky case lltok::kw_fptosi: 2425193323Sed case lltok::kw_inttoptr: 2426198090Srdivacky case lltok::kw_ptrtoint: { 2427193323Sed unsigned Opc = Lex.getUIntVal(); 2428224145Sdim Type *DestTy = 0; 2429193323Sed Constant *SrcVal; 2430193323Sed Lex.Lex(); 2431193323Sed if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") || 2432193323Sed ParseGlobalTypeAndValue(SrcVal) || 2433194612Sed ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") || 2434193323Sed ParseType(DestTy) || 2435193323Sed ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast")) 2436193323Sed return true; 2437193323Sed if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy)) 2438193323Sed return Error(ID.Loc, "invalid cast opcode for cast from '" + 2439224145Sdim getTypeString(SrcVal->getType()) + "' to '" + 2440224145Sdim getTypeString(DestTy) + "'"); 2441198090Srdivacky ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc, 2442195340Sed SrcVal, DestTy); 2443193323Sed ID.Kind = ValID::t_Constant; 2444193323Sed return false; 2445193323Sed } 2446193323Sed case lltok::kw_extractvalue: { 2447193323Sed Lex.Lex(); 2448193323Sed Constant *Val; 2449193323Sed SmallVector<unsigned, 4> Indices; 2450193323Sed if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")|| 2451193323Sed ParseGlobalTypeAndValue(Val) || 2452193323Sed ParseIndexList(Indices) || 2453193323Sed ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr")) 2454193323Sed return true; 2455198892Srdivacky 2456203954Srdivacky if (!Val->getType()->isAggregateType()) 2457203954Srdivacky return Error(ID.Loc, "extractvalue operand must be aggregate type"); 2458224145Sdim if (!ExtractValueInst::getIndexedType(Val->getType(), Indices)) 2459193323Sed return Error(ID.Loc, "invalid indices for extractvalue"); 2460224145Sdim ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices); 2461193323Sed ID.Kind = ValID::t_Constant; 2462193323Sed return false; 2463193323Sed } 2464193323Sed case lltok::kw_insertvalue: { 2465193323Sed Lex.Lex(); 2466193323Sed Constant *Val0, *Val1; 2467193323Sed SmallVector<unsigned, 4> Indices; 2468193323Sed if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")|| 2469193323Sed ParseGlobalTypeAndValue(Val0) || 2470193323Sed ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")|| 2471193323Sed ParseGlobalTypeAndValue(Val1) || 2472193323Sed ParseIndexList(Indices) || 2473193323Sed ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr")) 2474193323Sed return true; 2475203954Srdivacky if (!Val0->getType()->isAggregateType()) 2476203954Srdivacky return Error(ID.Loc, "insertvalue operand must be aggregate type"); 2477224145Sdim if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices)) 2478193323Sed return Error(ID.Loc, "invalid indices for insertvalue"); 2479224145Sdim ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices); 2480193323Sed ID.Kind = ValID::t_Constant; 2481193323Sed return false; 2482193323Sed } 2483193323Sed case lltok::kw_icmp: 2484198090Srdivacky case lltok::kw_fcmp: { 2485193323Sed unsigned PredVal, Opc = Lex.getUIntVal(); 2486193323Sed Constant *Val0, *Val1; 2487193323Sed Lex.Lex(); 2488193323Sed if (ParseCmpPredicate(PredVal, Opc) || 2489193323Sed ParseToken(lltok::lparen, "expected '(' in compare constantexpr") || 2490193323Sed ParseGlobalTypeAndValue(Val0) || 2491193323Sed ParseToken(lltok::comma, "expected comma in compare constantexpr") || 2492193323Sed ParseGlobalTypeAndValue(Val1) || 2493193323Sed ParseToken(lltok::rparen, "expected ')' in compare constantexpr")) 2494193323Sed return true; 2495198090Srdivacky 2496193323Sed if (Val0->getType() != Val1->getType()) 2497193323Sed return Error(ID.Loc, "compare operands must have the same type"); 2498198090Srdivacky 2499193323Sed CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal; 2500198090Srdivacky 2501193323Sed if (Opc == Instruction::FCmp) { 2502203954Srdivacky if (!Val0->getType()->isFPOrFPVectorTy()) 2503193323Sed return Error(ID.Loc, "fcmp requires floating point operands"); 2504198090Srdivacky ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1); 2505198090Srdivacky } else { 2506198090Srdivacky assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!"); 2507203954Srdivacky if (!Val0->getType()->isIntOrIntVectorTy() && 2508234353Sdim !Val0->getType()->getScalarType()->isPointerTy()) 2509193323Sed return Error(ID.Loc, "icmp requires pointer or integer operands"); 2510198090Srdivacky ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1); 2511193323Sed } 2512193323Sed ID.Kind = ValID::t_Constant; 2513193323Sed return false; 2514193323Sed } 2515198090Srdivacky 2516193323Sed // Binary Operators. 2517193323Sed case lltok::kw_add: 2518193574Sed case lltok::kw_fadd: 2519193323Sed case lltok::kw_sub: 2520193574Sed case lltok::kw_fsub: 2521193323Sed case lltok::kw_mul: 2522193574Sed case lltok::kw_fmul: 2523193323Sed case lltok::kw_udiv: 2524193323Sed case lltok::kw_sdiv: 2525193323Sed case lltok::kw_fdiv: 2526193323Sed case lltok::kw_urem: 2527193323Sed case lltok::kw_srem: 2528218893Sdim case lltok::kw_frem: 2529218893Sdim case lltok::kw_shl: 2530218893Sdim case lltok::kw_lshr: 2531218893Sdim case lltok::kw_ashr: { 2532198090Srdivacky bool NUW = false; 2533198090Srdivacky bool NSW = false; 2534198090Srdivacky bool Exact = false; 2535193323Sed unsigned Opc = Lex.getUIntVal(); 2536193323Sed Constant *Val0, *Val1; 2537193323Sed Lex.Lex(); 2538198090Srdivacky LocTy ModifierLoc = Lex.getLoc(); 2539218893Sdim if (Opc == Instruction::Add || Opc == Instruction::Sub || 2540218893Sdim Opc == Instruction::Mul || Opc == Instruction::Shl) { 2541198090Srdivacky if (EatIfPresent(lltok::kw_nuw)) 2542198090Srdivacky NUW = true; 2543198090Srdivacky if (EatIfPresent(lltok::kw_nsw)) { 2544198090Srdivacky NSW = true; 2545198090Srdivacky if (EatIfPresent(lltok::kw_nuw)) 2546198090Srdivacky NUW = true; 2547198090Srdivacky } 2548218893Sdim } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv || 2549218893Sdim Opc == Instruction::LShr || Opc == Instruction::AShr) { 2550198090Srdivacky if (EatIfPresent(lltok::kw_exact)) 2551198090Srdivacky Exact = true; 2552198090Srdivacky } 2553193323Sed if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") || 2554193323Sed ParseGlobalTypeAndValue(Val0) || 2555193323Sed ParseToken(lltok::comma, "expected comma in binary constantexpr") || 2556193323Sed ParseGlobalTypeAndValue(Val1) || 2557193323Sed ParseToken(lltok::rparen, "expected ')' in binary constantexpr")) 2558193323Sed return true; 2559193323Sed if (Val0->getType() != Val1->getType()) 2560193323Sed return Error(ID.Loc, "operands of constexpr must have same type"); 2561203954Srdivacky if (!Val0->getType()->isIntOrIntVectorTy()) { 2562198090Srdivacky if (NUW) 2563198090Srdivacky return Error(ModifierLoc, "nuw only applies to integer operations"); 2564198090Srdivacky if (NSW) 2565198090Srdivacky return Error(ModifierLoc, "nsw only applies to integer operations"); 2566198090Srdivacky } 2567207618Srdivacky // Check that the type is valid for the operator. 2568207618Srdivacky switch (Opc) { 2569207618Srdivacky case Instruction::Add: 2570207618Srdivacky case Instruction::Sub: 2571207618Srdivacky case Instruction::Mul: 2572207618Srdivacky case Instruction::UDiv: 2573207618Srdivacky case Instruction::SDiv: 2574207618Srdivacky case Instruction::URem: 2575207618Srdivacky case Instruction::SRem: 2576218893Sdim case Instruction::Shl: 2577218893Sdim case Instruction::AShr: 2578218893Sdim case Instruction::LShr: 2579207618Srdivacky if (!Val0->getType()->isIntOrIntVectorTy()) 2580207618Srdivacky return Error(ID.Loc, "constexpr requires integer operands"); 2581207618Srdivacky break; 2582207618Srdivacky case Instruction::FAdd: 2583207618Srdivacky case Instruction::FSub: 2584207618Srdivacky case Instruction::FMul: 2585207618Srdivacky case Instruction::FDiv: 2586207618Srdivacky case Instruction::FRem: 2587207618Srdivacky if (!Val0->getType()->isFPOrFPVectorTy()) 2588207618Srdivacky return Error(ID.Loc, "constexpr requires fp operands"); 2589207618Srdivacky break; 2590207618Srdivacky default: llvm_unreachable("Unknown binary operator!"); 2591207618Srdivacky } 2592198090Srdivacky unsigned Flags = 0; 2593198090Srdivacky if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap; 2594198090Srdivacky if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap; 2595218893Sdim if (Exact) Flags |= PossiblyExactOperator::IsExact; 2596198090Srdivacky Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags); 2597198090Srdivacky ID.ConstantVal = C; 2598193323Sed ID.Kind = ValID::t_Constant; 2599193323Sed return false; 2600193323Sed } 2601198090Srdivacky 2602193323Sed // Logical Operations 2603193323Sed case lltok::kw_and: 2604193323Sed case lltok::kw_or: 2605193323Sed case lltok::kw_xor: { 2606193323Sed unsigned Opc = Lex.getUIntVal(); 2607193323Sed Constant *Val0, *Val1; 2608193323Sed Lex.Lex(); 2609193323Sed if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") || 2610193323Sed ParseGlobalTypeAndValue(Val0) || 2611193323Sed ParseToken(lltok::comma, "expected comma in logical constantexpr") || 2612193323Sed ParseGlobalTypeAndValue(Val1) || 2613193323Sed ParseToken(lltok::rparen, "expected ')' in logical constantexpr")) 2614193323Sed return true; 2615193323Sed if (Val0->getType() != Val1->getType()) 2616193323Sed return Error(ID.Loc, "operands of constexpr must have same type"); 2617203954Srdivacky if (!Val0->getType()->isIntOrIntVectorTy()) 2618193323Sed return Error(ID.Loc, 2619193323Sed "constexpr requires integer or integer vector operands"); 2620198090Srdivacky ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1); 2621193323Sed ID.Kind = ValID::t_Constant; 2622193323Sed return false; 2623198090Srdivacky } 2624198090Srdivacky 2625193323Sed case lltok::kw_getelementptr: 2626193323Sed case lltok::kw_shufflevector: 2627193323Sed case lltok::kw_insertelement: 2628193323Sed case lltok::kw_extractelement: 2629193323Sed case lltok::kw_select: { 2630193323Sed unsigned Opc = Lex.getUIntVal(); 2631193323Sed SmallVector<Constant*, 16> Elts; 2632198090Srdivacky bool InBounds = false; 2633193323Sed Lex.Lex(); 2634198090Srdivacky if (Opc == Instruction::GetElementPtr) 2635198090Srdivacky InBounds = EatIfPresent(lltok::kw_inbounds); 2636193323Sed if (ParseToken(lltok::lparen, "expected '(' in constantexpr") || 2637193323Sed ParseGlobalValueVector(Elts) || 2638193323Sed ParseToken(lltok::rparen, "expected ')' in constantexpr")) 2639193323Sed return true; 2640198090Srdivacky 2641193323Sed if (Opc == Instruction::GetElementPtr) { 2642234353Sdim if (Elts.size() == 0 || 2643234353Sdim !Elts[0]->getType()->getScalarType()->isPointerTy()) 2644193323Sed return Error(ID.Loc, "getelementptr requires pointer operand"); 2645198090Srdivacky 2646226633Sdim ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end()); 2647226633Sdim if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), Indices)) 2648193323Sed return Error(ID.Loc, "invalid indices for getelementptr"); 2649226633Sdim ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], Indices, 2650226633Sdim InBounds); 2651193323Sed } else if (Opc == Instruction::Select) { 2652193323Sed if (Elts.size() != 3) 2653193323Sed return Error(ID.Loc, "expected three operands to select"); 2654193323Sed if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1], 2655193323Sed Elts[2])) 2656193323Sed return Error(ID.Loc, Reason); 2657198090Srdivacky ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]); 2658193323Sed } else if (Opc == Instruction::ShuffleVector) { 2659193323Sed if (Elts.size() != 3) 2660193323Sed return Error(ID.Loc, "expected three operands to shufflevector"); 2661193323Sed if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2])) 2662193323Sed return Error(ID.Loc, "invalid operands to shufflevector"); 2663195340Sed ID.ConstantVal = 2664198090Srdivacky ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]); 2665193323Sed } else if (Opc == Instruction::ExtractElement) { 2666193323Sed if (Elts.size() != 2) 2667193323Sed return Error(ID.Loc, "expected two operands to extractelement"); 2668193323Sed if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1])) 2669193323Sed return Error(ID.Loc, "invalid extractelement operands"); 2670198090Srdivacky ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]); 2671193323Sed } else { 2672193323Sed assert(Opc == Instruction::InsertElement && "Unknown opcode"); 2673193323Sed if (Elts.size() != 3) 2674193323Sed return Error(ID.Loc, "expected three operands to insertelement"); 2675193323Sed if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2])) 2676193323Sed return Error(ID.Loc, "invalid insertelement operands"); 2677195340Sed ID.ConstantVal = 2678198090Srdivacky ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]); 2679193323Sed } 2680198090Srdivacky 2681193323Sed ID.Kind = ValID::t_Constant; 2682193323Sed return false; 2683193323Sed } 2684193323Sed } 2685198090Srdivacky 2686193323Sed Lex.Lex(); 2687193323Sed return false; 2688193323Sed} 2689193323Sed 2690193323Sed/// ParseGlobalValue - Parse a global value with the specified type. 2691226633Sdimbool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) { 2692202375Srdivacky C = 0; 2693193323Sed ValID ID; 2694202375Srdivacky Value *V = NULL; 2695202375Srdivacky bool Parsed = ParseValID(ID) || 2696202375Srdivacky ConvertValIDToValue(Ty, ID, V, NULL); 2697202375Srdivacky if (V && !(C = dyn_cast<Constant>(V))) 2698202375Srdivacky return Error(ID.Loc, "global values must be constants"); 2699202375Srdivacky return Parsed; 2700193323Sed} 2701193323Sed 2702202375Srdivackybool LLParser::ParseGlobalTypeAndValue(Constant *&V) { 2703224145Sdim Type *Ty = 0; 2704224145Sdim return ParseType(Ty) || 2705224145Sdim ParseGlobalValue(Ty, V); 2706202375Srdivacky} 2707202375Srdivacky 2708202375Srdivacky/// ParseGlobalValueVector 2709202375Srdivacky/// ::= /*empty*/ 2710202375Srdivacky/// ::= TypeAndValue (',' TypeAndValue)* 2711202375Srdivackybool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts) { 2712202375Srdivacky // Empty list. 2713202375Srdivacky if (Lex.getKind() == lltok::rbrace || 2714202375Srdivacky Lex.getKind() == lltok::rsquare || 2715202375Srdivacky Lex.getKind() == lltok::greater || 2716202375Srdivacky Lex.getKind() == lltok::rparen) 2717202375Srdivacky return false; 2718202375Srdivacky 2719202375Srdivacky Constant *C; 2720202375Srdivacky if (ParseGlobalTypeAndValue(C)) return true; 2721202375Srdivacky Elts.push_back(C); 2722202375Srdivacky 2723202375Srdivacky while (EatIfPresent(lltok::comma)) { 2724202375Srdivacky if (ParseGlobalTypeAndValue(C)) return true; 2725202375Srdivacky Elts.push_back(C); 2726202375Srdivacky } 2727202375Srdivacky 2728202375Srdivacky return false; 2729202375Srdivacky} 2730202375Srdivacky 2731212904Sdimbool LLParser::ParseMetadataListValue(ValID &ID, PerFunctionState *PFS) { 2732212904Sdim assert(Lex.getKind() == lltok::lbrace); 2733212904Sdim Lex.Lex(); 2734212904Sdim 2735212904Sdim SmallVector<Value*, 16> Elts; 2736212904Sdim if (ParseMDNodeVector(Elts, PFS) || 2737212904Sdim ParseToken(lltok::rbrace, "expected end of metadata node")) 2738212904Sdim return true; 2739212904Sdim 2740221345Sdim ID.MDNodeVal = MDNode::get(Context, Elts); 2741212904Sdim ID.Kind = ValID::t_MDNode; 2742212904Sdim return false; 2743212904Sdim} 2744212904Sdim 2745210299Sed/// ParseMetadataValue 2746210299Sed/// ::= !42 2747210299Sed/// ::= !{...} 2748210299Sed/// ::= !"string" 2749210299Sedbool LLParser::ParseMetadataValue(ValID &ID, PerFunctionState *PFS) { 2750210299Sed assert(Lex.getKind() == lltok::exclaim); 2751210299Sed Lex.Lex(); 2752202375Srdivacky 2753210299Sed // MDNode: 2754210299Sed // !{ ... } 2755212904Sdim if (Lex.getKind() == lltok::lbrace) 2756212904Sdim return ParseMetadataListValue(ID, PFS); 2757210299Sed 2758210299Sed // Standalone metadata reference 2759210299Sed // !42 2760210299Sed if (Lex.getKind() == lltok::APSInt) { 2761210299Sed if (ParseMDNodeID(ID.MDNodeVal)) return true; 2762210299Sed ID.Kind = ValID::t_MDNode; 2763210299Sed return false; 2764210299Sed } 2765210299Sed 2766210299Sed // MDString: 2767210299Sed // ::= '!' STRINGCONSTANT 2768210299Sed if (ParseMDString(ID.MDStringVal)) return true; 2769210299Sed ID.Kind = ValID::t_MDString; 2770210299Sed return false; 2771210299Sed} 2772210299Sed 2773210299Sed 2774202375Srdivacky//===----------------------------------------------------------------------===// 2775202375Srdivacky// Function Parsing. 2776202375Srdivacky//===----------------------------------------------------------------------===// 2777202375Srdivacky 2778226633Sdimbool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V, 2779202375Srdivacky PerFunctionState *PFS) { 2780204642Srdivacky if (Ty->isFunctionTy()) 2781193323Sed return Error(ID.Loc, "functions are not values, refer to them as pointers"); 2782198090Srdivacky 2783193323Sed switch (ID.Kind) { 2784202375Srdivacky case ValID::t_LocalID: 2785202375Srdivacky if (!PFS) return Error(ID.Loc, "invalid use of function-local name"); 2786202375Srdivacky V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc); 2787202375Srdivacky return (V == 0); 2788202375Srdivacky case ValID::t_LocalName: 2789202375Srdivacky if (!PFS) return Error(ID.Loc, "invalid use of function-local name"); 2790202375Srdivacky V = PFS->GetVal(ID.StrVal, Ty, ID.Loc); 2791202375Srdivacky return (V == 0); 2792202375Srdivacky case ValID::t_InlineAsm: { 2793226633Sdim PointerType *PTy = dyn_cast<PointerType>(Ty); 2794249423Sdim FunctionType *FTy = 2795202375Srdivacky PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0; 2796202375Srdivacky if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2)) 2797202375Srdivacky return Error(ID.Loc, "invalid type for inline asm constraint string"); 2798243830Sdim V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1, 2799243830Sdim (ID.UIntVal>>1)&1, (InlineAsm::AsmDialect(ID.UIntVal>>2))); 2800202375Srdivacky return false; 2801202375Srdivacky } 2802201360Srdivacky case ValID::t_MDNode: 2803202375Srdivacky if (!Ty->isMetadataTy()) 2804202375Srdivacky return Error(ID.Loc, "metadata value must have metadata type"); 2805202375Srdivacky V = ID.MDNodeVal; 2806202375Srdivacky return false; 2807201360Srdivacky case ValID::t_MDString: 2808202375Srdivacky if (!Ty->isMetadataTy()) 2809202375Srdivacky return Error(ID.Loc, "metadata value must have metadata type"); 2810202375Srdivacky V = ID.MDStringVal; 2811202375Srdivacky return false; 2812193323Sed case ValID::t_GlobalName: 2813193323Sed V = GetGlobalVal(ID.StrVal, Ty, ID.Loc); 2814193323Sed return V == 0; 2815193323Sed case ValID::t_GlobalID: 2816193323Sed V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc); 2817193323Sed return V == 0; 2818193323Sed case ValID::t_APSInt: 2819204642Srdivacky if (!Ty->isIntegerTy()) 2820193323Sed return Error(ID.Loc, "integer constant must have integer type"); 2821218893Sdim ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits()); 2822198090Srdivacky V = ConstantInt::get(Context, ID.APSIntVal); 2823193323Sed return false; 2824193323Sed case ValID::t_APFloat: 2825203954Srdivacky if (!Ty->isFloatingPointTy() || 2826193323Sed !ConstantFP::isValueValidForType(Ty, ID.APFloatVal)) 2827193323Sed return Error(ID.Loc, "floating point constant invalid for type"); 2828198090Srdivacky 2829234353Sdim // The lexer has no type info, so builds all half, float, and double FP 2830234353Sdim // constants as double. Fix this here. Long double does not need this. 2831234353Sdim if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble) { 2832193323Sed bool Ignored; 2833234353Sdim if (Ty->isHalfTy()) 2834234353Sdim ID.APFloatVal.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven, 2835234353Sdim &Ignored); 2836234353Sdim else if (Ty->isFloatTy()) 2837234353Sdim ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, 2838234353Sdim &Ignored); 2839193323Sed } 2840198090Srdivacky V = ConstantFP::get(Context, ID.APFloatVal); 2841198090Srdivacky 2842193323Sed if (V->getType() != Ty) 2843193323Sed return Error(ID.Loc, "floating point constant does not have type '" + 2844224145Sdim getTypeString(Ty) + "'"); 2845198090Srdivacky 2846193323Sed return false; 2847193323Sed case ValID::t_Null: 2848204642Srdivacky if (!Ty->isPointerTy()) 2849193323Sed return Error(ID.Loc, "null must be a pointer type"); 2850198090Srdivacky V = ConstantPointerNull::get(cast<PointerType>(Ty)); 2851193323Sed return false; 2852193323Sed case ValID::t_Undef: 2853193323Sed // FIXME: LabelTy should not be a first-class type. 2854224145Sdim if (!Ty->isFirstClassType() || Ty->isLabelTy()) 2855193323Sed return Error(ID.Loc, "invalid type for undef constant"); 2856198090Srdivacky V = UndefValue::get(Ty); 2857193323Sed return false; 2858193323Sed case ValID::t_EmptyArray: 2859204642Srdivacky if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0) 2860193323Sed return Error(ID.Loc, "invalid empty array initializer"); 2861198090Srdivacky V = UndefValue::get(Ty); 2862193323Sed return false; 2863193323Sed case ValID::t_Zero: 2864193323Sed // FIXME: LabelTy should not be a first-class type. 2865198090Srdivacky if (!Ty->isFirstClassType() || Ty->isLabelTy()) 2866193323Sed return Error(ID.Loc, "invalid type for null constant"); 2867198090Srdivacky V = Constant::getNullValue(Ty); 2868193323Sed return false; 2869193323Sed case ValID::t_Constant: 2870212904Sdim if (ID.ConstantVal->getType() != Ty) 2871193323Sed return Error(ID.Loc, "constant expression type mismatch"); 2872203954Srdivacky 2873193323Sed V = ID.ConstantVal; 2874193323Sed return false; 2875224145Sdim case ValID::t_ConstantStruct: 2876224145Sdim case ValID::t_PackedConstantStruct: 2877226633Sdim if (StructType *ST = dyn_cast<StructType>(Ty)) { 2878224145Sdim if (ST->getNumElements() != ID.UIntVal) 2879224145Sdim return Error(ID.Loc, 2880224145Sdim "initializer with struct type has wrong # elements"); 2881224145Sdim if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct)) 2882224145Sdim return Error(ID.Loc, "packed'ness of initializer and type don't match"); 2883249423Sdim 2884224145Sdim // Verify that the elements are compatible with the structtype. 2885224145Sdim for (unsigned i = 0, e = ID.UIntVal; i != e; ++i) 2886224145Sdim if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i)) 2887224145Sdim return Error(ID.Loc, "element " + Twine(i) + 2888224145Sdim " of struct initializer doesn't match struct element type"); 2889249423Sdim 2890226633Sdim V = ConstantStruct::get(ST, makeArrayRef(ID.ConstantStructElts, 2891226633Sdim ID.UIntVal)); 2892224145Sdim } else 2893224145Sdim return Error(ID.Loc, "constant expression type mismatch"); 2894224145Sdim return false; 2895193323Sed } 2896234353Sdim llvm_unreachable("Invalid ValID"); 2897193323Sed} 2898198090Srdivacky 2899226633Sdimbool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) { 2900193323Sed V = 0; 2901193323Sed ValID ID; 2902224145Sdim return ParseValID(ID, PFS) || 2903224145Sdim ConvertValIDToValue(Ty, ID, V, PFS); 2904193323Sed} 2905193323Sed 2906224145Sdimbool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) { 2907224145Sdim Type *Ty = 0; 2908224145Sdim return ParseType(Ty) || 2909224145Sdim ParseValue(Ty, V, PFS); 2910193323Sed} 2911193323Sed 2912198892Srdivackybool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc, 2913198892Srdivacky PerFunctionState &PFS) { 2914198892Srdivacky Value *V; 2915198892Srdivacky Loc = Lex.getLoc(); 2916198892Srdivacky if (ParseTypeAndValue(V, PFS)) return true; 2917198892Srdivacky if (!isa<BasicBlock>(V)) 2918198892Srdivacky return Error(Loc, "expected a basic block"); 2919198892Srdivacky BB = cast<BasicBlock>(V); 2920198892Srdivacky return false; 2921198892Srdivacky} 2922198892Srdivacky 2923198892Srdivacky 2924193323Sed/// FunctionHeader 2925193323Sed/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs 2926218893Sdim/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection 2927263508Sdim/// OptionalAlign OptGC OptionalPrefix 2928193323Sedbool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) { 2929193323Sed // Parse the linkage. 2930193323Sed LocTy LinkageLoc = Lex.getLoc(); 2931193323Sed unsigned Linkage; 2932198090Srdivacky 2933234353Sdim unsigned Visibility; 2934243830Sdim AttrBuilder RetAttrs; 2935198090Srdivacky CallingConv::ID CC; 2936224145Sdim Type *RetType = 0; 2937193323Sed LocTy RetTypeLoc = Lex.getLoc(); 2938193323Sed if (ParseOptionalLinkage(Linkage) || 2939193323Sed ParseOptionalVisibility(Visibility) || 2940193323Sed ParseOptionalCallingConv(CC) || 2941249423Sdim ParseOptionalReturnAttrs(RetAttrs) || 2942193323Sed ParseType(RetType, RetTypeLoc, true /*void allowed*/)) 2943193323Sed return true; 2944193323Sed 2945193323Sed // Verify that the linkage is ok. 2946193323Sed switch ((GlobalValue::LinkageTypes)Linkage) { 2947193323Sed case GlobalValue::ExternalLinkage: 2948193323Sed break; // always ok. 2949193323Sed case GlobalValue::DLLImportLinkage: 2950193323Sed case GlobalValue::ExternalWeakLinkage: 2951193323Sed if (isDefine) 2952193323Sed return Error(LinkageLoc, "invalid linkage for function definition"); 2953193323Sed break; 2954193323Sed case GlobalValue::PrivateLinkage: 2955198090Srdivacky case GlobalValue::LinkerPrivateLinkage: 2956210299Sed case GlobalValue::LinkerPrivateWeakLinkage: 2957193323Sed case GlobalValue::InternalLinkage: 2958193323Sed case GlobalValue::AvailableExternallyLinkage: 2959193323Sed case GlobalValue::LinkOnceAnyLinkage: 2960193323Sed case GlobalValue::LinkOnceODRLinkage: 2961193323Sed case GlobalValue::WeakAnyLinkage: 2962193323Sed case GlobalValue::WeakODRLinkage: 2963193323Sed case GlobalValue::DLLExportLinkage: 2964193323Sed if (!isDefine) 2965193323Sed return Error(LinkageLoc, "invalid linkage for function declaration"); 2966193323Sed break; 2967193323Sed case GlobalValue::AppendingLinkage: 2968193323Sed case GlobalValue::CommonLinkage: 2969193323Sed return Error(LinkageLoc, "invalid function linkage type"); 2970193323Sed } 2971198090Srdivacky 2972224145Sdim if (!FunctionType::isValidReturnType(RetType)) 2973193323Sed return Error(RetTypeLoc, "invalid function return type"); 2974198090Srdivacky 2975193323Sed LocTy NameLoc = Lex.getLoc(); 2976193323Sed 2977193323Sed std::string FunctionName; 2978193323Sed if (Lex.getKind() == lltok::GlobalVar) { 2979193323Sed FunctionName = Lex.getStrVal(); 2980193323Sed } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok. 2981193323Sed unsigned NameID = Lex.getUIntVal(); 2982193323Sed 2983193323Sed if (NameID != NumberedVals.size()) 2984193323Sed return TokError("function expected to be numbered '%" + 2985218893Sdim Twine(NumberedVals.size()) + "'"); 2986193323Sed } else { 2987193323Sed return TokError("expected function name"); 2988193323Sed } 2989198090Srdivacky 2990193323Sed Lex.Lex(); 2991198090Srdivacky 2992193323Sed if (Lex.getKind() != lltok::lparen) 2993193323Sed return TokError("expected '(' in function argument list"); 2994198090Srdivacky 2995224145Sdim SmallVector<ArgInfo, 8> ArgList; 2996193323Sed bool isVarArg; 2997243830Sdim AttrBuilder FuncAttrs; 2998249423Sdim std::vector<unsigned> FwdRefAttrGrps; 2999263508Sdim LocTy BuiltinLoc; 3000193323Sed std::string Section; 3001193323Sed unsigned Alignment; 3002193323Sed std::string GC; 3003218893Sdim bool UnnamedAddr; 3004218893Sdim LocTy UnnamedAddrLoc; 3005263508Sdim Constant *Prefix = 0; 3006193323Sed 3007224145Sdim if (ParseArgumentList(ArgList, isVarArg) || 3008218893Sdim ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr, 3009218893Sdim &UnnamedAddrLoc) || 3010249423Sdim ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false, 3011263508Sdim BuiltinLoc) || 3012193323Sed (EatIfPresent(lltok::kw_section) && 3013193323Sed ParseStringConstant(Section)) || 3014193323Sed ParseOptionalAlignment(Alignment) || 3015193323Sed (EatIfPresent(lltok::kw_gc) && 3016263508Sdim ParseStringConstant(GC)) || 3017263508Sdim (EatIfPresent(lltok::kw_prefix) && 3018263508Sdim ParseGlobalTypeAndValue(Prefix))) 3019193323Sed return true; 3020193323Sed 3021263508Sdim if (FuncAttrs.contains(Attribute::Builtin)) 3022263508Sdim return Error(BuiltinLoc, "'builtin' attribute not valid on function"); 3023249423Sdim 3024193323Sed // If the alignment was parsed as an attribute, move to the alignment field. 3025243830Sdim if (FuncAttrs.hasAlignmentAttr()) { 3026243830Sdim Alignment = FuncAttrs.getAlignment(); 3027249423Sdim FuncAttrs.removeAttribute(Attribute::Alignment); 3028193323Sed } 3029198090Srdivacky 3030193323Sed // Okay, if we got here, the function is syntactically valid. Convert types 3031193323Sed // and do semantic checks. 3032224145Sdim std::vector<Type*> ParamTypeList; 3033249423Sdim SmallVector<AttributeSet, 8> Attrs; 3034198090Srdivacky 3035243830Sdim if (RetAttrs.hasAttributes()) 3036249423Sdim Attrs.push_back(AttributeSet::get(RetType->getContext(), 3037249423Sdim AttributeSet::ReturnIndex, 3038249423Sdim RetAttrs)); 3039198090Srdivacky 3040193323Sed for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { 3041224145Sdim ParamTypeList.push_back(ArgList[i].Ty); 3042249423Sdim if (ArgList[i].Attrs.hasAttributes(i + 1)) { 3043249423Sdim AttrBuilder B(ArgList[i].Attrs, i + 1); 3044249423Sdim Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B)); 3045249423Sdim } 3046193323Sed } 3047193323Sed 3048243830Sdim if (FuncAttrs.hasAttributes()) 3049249423Sdim Attrs.push_back(AttributeSet::get(RetType->getContext(), 3050249423Sdim AttributeSet::FunctionIndex, 3051249423Sdim FuncAttrs)); 3052193323Sed 3053249423Sdim AttributeSet PAL = AttributeSet::get(Context, Attrs); 3054198090Srdivacky 3055249423Sdim if (PAL.hasAttribute(1, Attribute::StructRet) && !RetType->isVoidTy()) 3056198090Srdivacky return Error(RetTypeLoc, "functions with 'sret' argument must return void"); 3057198090Srdivacky 3058226633Sdim FunctionType *FT = 3059198090Srdivacky FunctionType::get(RetType, ParamTypeList, isVarArg); 3060226633Sdim PointerType *PFT = PointerType::getUnqual(FT); 3061193323Sed 3062193323Sed Fn = 0; 3063193323Sed if (!FunctionName.empty()) { 3064193323Sed // If this was a definition of a forward reference, remove the definition 3065193323Sed // from the forward reference table and fill in the forward ref. 3066193323Sed std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI = 3067193323Sed ForwardRefVals.find(FunctionName); 3068193323Sed if (FRVI != ForwardRefVals.end()) { 3069193323Sed Fn = M->getFunction(FunctionName); 3070243830Sdim if (!Fn) 3071243830Sdim return Error(FRVI->second.second, "invalid forward reference to " 3072243830Sdim "function as global value!"); 3073207618Srdivacky if (Fn->getType() != PFT) 3074207618Srdivacky return Error(FRVI->second.second, "invalid forward reference to " 3075207618Srdivacky "function '" + FunctionName + "' with wrong type!"); 3076249423Sdim 3077193323Sed ForwardRefVals.erase(FRVI); 3078193323Sed } else if ((Fn = M->getFunction(FunctionName))) { 3079224145Sdim // Reject redefinitions. 3080224145Sdim return Error(NameLoc, "invalid redefinition of function '" + 3081224145Sdim FunctionName + "'"); 3082198892Srdivacky } else if (M->getNamedValue(FunctionName)) { 3083198892Srdivacky return Error(NameLoc, "redefinition of function '@" + FunctionName + "'"); 3084193323Sed } 3085198090Srdivacky 3086198090Srdivacky } else { 3087193323Sed // If this is a definition of a forward referenced function, make sure the 3088193323Sed // types agree. 3089193323Sed std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I 3090193323Sed = ForwardRefValIDs.find(NumberedVals.size()); 3091193323Sed if (I != ForwardRefValIDs.end()) { 3092193323Sed Fn = cast<Function>(I->second.first); 3093193323Sed if (Fn->getType() != PFT) 3094193323Sed return Error(NameLoc, "type of definition and forward reference of '@" + 3095218893Sdim Twine(NumberedVals.size()) + "' disagree"); 3096193323Sed ForwardRefValIDs.erase(I); 3097193323Sed } 3098193323Sed } 3099193323Sed 3100193323Sed if (Fn == 0) 3101193323Sed Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M); 3102193323Sed else // Move the forward-reference to the correct spot in the module. 3103193323Sed M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn); 3104193323Sed 3105193323Sed if (FunctionName.empty()) 3106193323Sed NumberedVals.push_back(Fn); 3107198090Srdivacky 3108193323Sed Fn->setLinkage((GlobalValue::LinkageTypes)Linkage); 3109193323Sed Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility); 3110193323Sed Fn->setCallingConv(CC); 3111193323Sed Fn->setAttributes(PAL); 3112218893Sdim Fn->setUnnamedAddr(UnnamedAddr); 3113193323Sed Fn->setAlignment(Alignment); 3114193323Sed Fn->setSection(Section); 3115193323Sed if (!GC.empty()) Fn->setGC(GC.c_str()); 3116263508Sdim Fn->setPrefixData(Prefix); 3117249423Sdim ForwardRefAttrGroups[Fn] = FwdRefAttrGrps; 3118198090Srdivacky 3119193323Sed // Add all of the arguments we parsed to the function. 3120193323Sed Function::arg_iterator ArgIt = Fn->arg_begin(); 3121193323Sed for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) { 3122193323Sed // If the argument has a name, insert it into the argument symbol table. 3123193323Sed if (ArgList[i].Name.empty()) continue; 3124198090Srdivacky 3125193323Sed // Set the name, if it conflicted, it will be auto-renamed. 3126193323Sed ArgIt->setName(ArgList[i].Name); 3127198090Srdivacky 3128218893Sdim if (ArgIt->getName() != ArgList[i].Name) 3129193323Sed return Error(ArgList[i].Loc, "redefinition of argument '%" + 3130193323Sed ArgList[i].Name + "'"); 3131193323Sed } 3132198090Srdivacky 3133193323Sed return false; 3134193323Sed} 3135193323Sed 3136193323Sed 3137193323Sed/// ParseFunctionBody 3138193323Sed/// ::= '{' BasicBlock+ '}' 3139193323Sed/// 3140193323Sedbool LLParser::ParseFunctionBody(Function &Fn) { 3141224145Sdim if (Lex.getKind() != lltok::lbrace) 3142193323Sed return TokError("expected '{' in function body"); 3143193323Sed Lex.Lex(); // eat the {. 3144198090Srdivacky 3145198892Srdivacky int FunctionNumber = -1; 3146198892Srdivacky if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1; 3147249423Sdim 3148198892Srdivacky PerFunctionState PFS(*this, Fn, FunctionNumber); 3149198090Srdivacky 3150202375Srdivacky // We need at least one basic block. 3151224145Sdim if (Lex.getKind() == lltok::rbrace) 3152202375Srdivacky return TokError("function body requires at least one basic block"); 3153249423Sdim 3154224145Sdim while (Lex.getKind() != lltok::rbrace) 3155193323Sed if (ParseBasicBlock(PFS)) return true; 3156198090Srdivacky 3157193323Sed // Eat the }. 3158193323Sed Lex.Lex(); 3159198090Srdivacky 3160193323Sed // Verify function is ok. 3161198892Srdivacky return PFS.FinishFunction(); 3162193323Sed} 3163193323Sed 3164193323Sed/// ParseBasicBlock 3165193323Sed/// ::= LabelStr? Instruction* 3166193323Sedbool LLParser::ParseBasicBlock(PerFunctionState &PFS) { 3167193323Sed // If this basic block starts out with a name, remember it. 3168193323Sed std::string Name; 3169193323Sed LocTy NameLoc = Lex.getLoc(); 3170193323Sed if (Lex.getKind() == lltok::LabelStr) { 3171193323Sed Name = Lex.getStrVal(); 3172193323Sed Lex.Lex(); 3173193323Sed } 3174198090Srdivacky 3175193323Sed BasicBlock *BB = PFS.DefineBB(Name, NameLoc); 3176193323Sed if (BB == 0) return true; 3177198090Srdivacky 3178193323Sed std::string NameStr; 3179198090Srdivacky 3180193323Sed // Parse the instructions in this block until we get a terminator. 3181193323Sed Instruction *Inst; 3182193323Sed do { 3183193323Sed // This instruction may have three possibilities for a name: a) none 3184193323Sed // specified, b) name specified "%foo =", c) number specified: "%4 =". 3185193323Sed LocTy NameLoc = Lex.getLoc(); 3186193323Sed int NameID = -1; 3187193323Sed NameStr = ""; 3188198090Srdivacky 3189193323Sed if (Lex.getKind() == lltok::LocalVarID) { 3190193323Sed NameID = Lex.getUIntVal(); 3191193323Sed Lex.Lex(); 3192193323Sed if (ParseToken(lltok::equal, "expected '=' after instruction id")) 3193193323Sed return true; 3194224145Sdim } else if (Lex.getKind() == lltok::LocalVar) { 3195193323Sed NameStr = Lex.getStrVal(); 3196193323Sed Lex.Lex(); 3197193323Sed if (ParseToken(lltok::equal, "expected '=' after instruction name")) 3198193323Sed return true; 3199193323Sed } 3200198090Srdivacky 3201201360Srdivacky switch (ParseInstruction(Inst, BB, PFS)) { 3202234353Sdim default: llvm_unreachable("Unknown ParseInstruction result!"); 3203201360Srdivacky case InstError: return true; 3204201360Srdivacky case InstNormal: 3205207618Srdivacky BB->getInstList().push_back(Inst); 3206207618Srdivacky 3207201360Srdivacky // With a normal result, we check to see if the instruction is followed by 3208201360Srdivacky // a comma and metadata. 3209201360Srdivacky if (EatIfPresent(lltok::comma)) 3210212904Sdim if (ParseInstructionMetadata(Inst, &PFS)) 3211201360Srdivacky return true; 3212201360Srdivacky break; 3213201360Srdivacky case InstExtraComma: 3214207618Srdivacky BB->getInstList().push_back(Inst); 3215207618Srdivacky 3216201360Srdivacky // If the instruction parser ate an extra comma at the end of it, it 3217201360Srdivacky // *must* be followed by metadata. 3218212904Sdim if (ParseInstructionMetadata(Inst, &PFS)) 3219201360Srdivacky return true; 3220249423Sdim break; 3221201360Srdivacky } 3222198090Srdivacky 3223193323Sed // Set the name on the instruction. 3224193323Sed if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true; 3225193323Sed } while (!isa<TerminatorInst>(Inst)); 3226198090Srdivacky 3227193323Sed return false; 3228193323Sed} 3229193323Sed 3230193323Sed//===----------------------------------------------------------------------===// 3231193323Sed// Instruction Parsing. 3232193323Sed//===----------------------------------------------------------------------===// 3233193323Sed 3234193323Sed/// ParseInstruction - Parse one of the many different instructions. 3235193323Sed/// 3236201360Srdivackyint LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB, 3237201360Srdivacky PerFunctionState &PFS) { 3238193323Sed lltok::Kind Token = Lex.getKind(); 3239193323Sed if (Token == lltok::Eof) 3240193323Sed return TokError("found end of file when expecting more instructions"); 3241193323Sed LocTy Loc = Lex.getLoc(); 3242193323Sed unsigned KeywordVal = Lex.getUIntVal(); 3243193323Sed Lex.Lex(); // Eat the keyword. 3244198090Srdivacky 3245193323Sed switch (Token) { 3246193323Sed default: return Error(Loc, "expected instruction opcode"); 3247193323Sed // Terminator Instructions. 3248198090Srdivacky case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false; 3249193323Sed case lltok::kw_ret: return ParseRet(Inst, BB, PFS); 3250193323Sed case lltok::kw_br: return ParseBr(Inst, PFS); 3251193323Sed case lltok::kw_switch: return ParseSwitch(Inst, PFS); 3252198892Srdivacky case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS); 3253193323Sed case lltok::kw_invoke: return ParseInvoke(Inst, PFS); 3254226633Sdim case lltok::kw_resume: return ParseResume(Inst, PFS); 3255193323Sed // Binary Operators. 3256193323Sed case lltok::kw_add: 3257193323Sed case lltok::kw_sub: 3258218893Sdim case lltok::kw_mul: 3259218893Sdim case lltok::kw_shl: { 3260218893Sdim bool NUW = EatIfPresent(lltok::kw_nuw); 3261218893Sdim bool NSW = EatIfPresent(lltok::kw_nsw); 3262218893Sdim if (!NUW) NUW = EatIfPresent(lltok::kw_nuw); 3263249423Sdim 3264218893Sdim if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true; 3265249423Sdim 3266218893Sdim if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true); 3267218893Sdim if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true); 3268218893Sdim return false; 3269198090Srdivacky } 3270193574Sed case lltok::kw_fadd: 3271193574Sed case lltok::kw_fsub: 3272249423Sdim case lltok::kw_fmul: 3273249423Sdim case lltok::kw_fdiv: 3274249423Sdim case lltok::kw_frem: { 3275249423Sdim FastMathFlags FMF = EatFastMathFlagsIfPresent(); 3276249423Sdim int Res = ParseArithmetic(Inst, PFS, KeywordVal, 2); 3277249423Sdim if (Res != 0) 3278249423Sdim return Res; 3279249423Sdim if (FMF.any()) 3280249423Sdim Inst->setFastMathFlags(FMF); 3281249423Sdim return 0; 3282249423Sdim } 3283193574Sed 3284218893Sdim case lltok::kw_sdiv: 3285218893Sdim case lltok::kw_udiv: 3286218893Sdim case lltok::kw_lshr: 3287218893Sdim case lltok::kw_ashr: { 3288218893Sdim bool Exact = EatIfPresent(lltok::kw_exact); 3289218893Sdim 3290218893Sdim if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true; 3291218893Sdim if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true); 3292218893Sdim return false; 3293198090Srdivacky } 3294198090Srdivacky 3295193323Sed case lltok::kw_urem: 3296193323Sed case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1); 3297193323Sed case lltok::kw_and: 3298193323Sed case lltok::kw_or: 3299193323Sed case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal); 3300193323Sed case lltok::kw_icmp: 3301198090Srdivacky case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal); 3302193323Sed // Casts. 3303193323Sed case lltok::kw_trunc: 3304193323Sed case lltok::kw_zext: 3305193323Sed case lltok::kw_sext: 3306193323Sed case lltok::kw_fptrunc: 3307193323Sed case lltok::kw_fpext: 3308193323Sed case lltok::kw_bitcast: 3309263508Sdim case lltok::kw_addrspacecast: 3310193323Sed case lltok::kw_uitofp: 3311193323Sed case lltok::kw_sitofp: 3312193323Sed case lltok::kw_fptoui: 3313198090Srdivacky case lltok::kw_fptosi: 3314193323Sed case lltok::kw_inttoptr: 3315193323Sed case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal); 3316193323Sed // Other. 3317193323Sed case lltok::kw_select: return ParseSelect(Inst, PFS); 3318193323Sed case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS); 3319193323Sed case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS); 3320193323Sed case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS); 3321193323Sed case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS); 3322193323Sed case lltok::kw_phi: return ParsePHI(Inst, PFS); 3323226633Sdim case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS); 3324193323Sed case lltok::kw_call: return ParseCall(Inst, PFS, false); 3325193323Sed case lltok::kw_tail: return ParseCall(Inst, PFS, true); 3326193323Sed // Memory. 3327198396Srdivacky case lltok::kw_alloca: return ParseAlloc(Inst, PFS); 3328234353Sdim case lltok::kw_load: return ParseLoad(Inst, PFS); 3329234353Sdim case lltok::kw_store: return ParseStore(Inst, PFS); 3330226633Sdim case lltok::kw_cmpxchg: return ParseCmpXchg(Inst, PFS); 3331226633Sdim case lltok::kw_atomicrmw: return ParseAtomicRMW(Inst, PFS); 3332226633Sdim case lltok::kw_fence: return ParseFence(Inst, PFS); 3333193323Sed case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS); 3334193323Sed case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS); 3335193323Sed case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS); 3336193323Sed } 3337193323Sed} 3338193323Sed 3339193323Sed/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind. 3340193323Sedbool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) { 3341198090Srdivacky if (Opc == Instruction::FCmp) { 3342193323Sed switch (Lex.getKind()) { 3343249423Sdim default: return TokError("expected fcmp predicate (e.g. 'oeq')"); 3344193323Sed case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break; 3345193323Sed case lltok::kw_one: P = CmpInst::FCMP_ONE; break; 3346193323Sed case lltok::kw_olt: P = CmpInst::FCMP_OLT; break; 3347193323Sed case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break; 3348193323Sed case lltok::kw_ole: P = CmpInst::FCMP_OLE; break; 3349193323Sed case lltok::kw_oge: P = CmpInst::FCMP_OGE; break; 3350193323Sed case lltok::kw_ord: P = CmpInst::FCMP_ORD; break; 3351193323Sed case lltok::kw_uno: P = CmpInst::FCMP_UNO; break; 3352193323Sed case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break; 3353193323Sed case lltok::kw_une: P = CmpInst::FCMP_UNE; break; 3354193323Sed case lltok::kw_ult: P = CmpInst::FCMP_ULT; break; 3355193323Sed case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break; 3356193323Sed case lltok::kw_ule: P = CmpInst::FCMP_ULE; break; 3357193323Sed case lltok::kw_uge: P = CmpInst::FCMP_UGE; break; 3358193323Sed case lltok::kw_true: P = CmpInst::FCMP_TRUE; break; 3359193323Sed case lltok::kw_false: P = CmpInst::FCMP_FALSE; break; 3360193323Sed } 3361193323Sed } else { 3362193323Sed switch (Lex.getKind()) { 3363249423Sdim default: return TokError("expected icmp predicate (e.g. 'eq')"); 3364193323Sed case lltok::kw_eq: P = CmpInst::ICMP_EQ; break; 3365193323Sed case lltok::kw_ne: P = CmpInst::ICMP_NE; break; 3366193323Sed case lltok::kw_slt: P = CmpInst::ICMP_SLT; break; 3367193323Sed case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break; 3368193323Sed case lltok::kw_sle: P = CmpInst::ICMP_SLE; break; 3369193323Sed case lltok::kw_sge: P = CmpInst::ICMP_SGE; break; 3370193323Sed case lltok::kw_ult: P = CmpInst::ICMP_ULT; break; 3371193323Sed case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break; 3372193323Sed case lltok::kw_ule: P = CmpInst::ICMP_ULE; break; 3373193323Sed case lltok::kw_uge: P = CmpInst::ICMP_UGE; break; 3374193323Sed } 3375193323Sed } 3376193323Sed Lex.Lex(); 3377193323Sed return false; 3378193323Sed} 3379193323Sed 3380193323Sed//===----------------------------------------------------------------------===// 3381193323Sed// Terminator Instructions. 3382193323Sed//===----------------------------------------------------------------------===// 3383193323Sed 3384193323Sed/// ParseRet - Parse a return instruction. 3385201360Srdivacky/// ::= 'ret' void (',' !dbg, !1)* 3386201360Srdivacky/// ::= 'ret' TypeAndValue (',' !dbg, !1)* 3387224145Sdimbool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB, 3388224145Sdim PerFunctionState &PFS) { 3389224145Sdim SMLoc TypeLoc = Lex.getLoc(); 3390224145Sdim Type *Ty = 0; 3391193323Sed if (ParseType(Ty, true /*void allowed*/)) return true; 3392198090Srdivacky 3393224145Sdim Type *ResType = PFS.getFunction().getReturnType(); 3394249423Sdim 3395198090Srdivacky if (Ty->isVoidTy()) { 3396224145Sdim if (!ResType->isVoidTy()) 3397224145Sdim return Error(TypeLoc, "value doesn't match function result type '" + 3398224145Sdim getTypeString(ResType) + "'"); 3399249423Sdim 3400198090Srdivacky Inst = ReturnInst::Create(Context); 3401193323Sed return false; 3402193323Sed } 3403198090Srdivacky 3404193323Sed Value *RV; 3405193323Sed if (ParseValue(Ty, RV, PFS)) return true; 3406198090Srdivacky 3407224145Sdim if (ResType != RV->getType()) 3408224145Sdim return Error(TypeLoc, "value doesn't match function result type '" + 3409224145Sdim getTypeString(ResType) + "'"); 3410249423Sdim 3411198090Srdivacky Inst = ReturnInst::Create(Context, RV); 3412224145Sdim return false; 3413193323Sed} 3414193323Sed 3415193323Sed 3416193323Sed/// ParseBr 3417193323Sed/// ::= 'br' TypeAndValue 3418193323Sed/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue 3419193323Sedbool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) { 3420193323Sed LocTy Loc, Loc2; 3421198892Srdivacky Value *Op0; 3422198892Srdivacky BasicBlock *Op1, *Op2; 3423193323Sed if (ParseTypeAndValue(Op0, Loc, PFS)) return true; 3424198090Srdivacky 3425193323Sed if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) { 3426193323Sed Inst = BranchInst::Create(BB); 3427193323Sed return false; 3428193323Sed } 3429198090Srdivacky 3430198090Srdivacky if (Op0->getType() != Type::getInt1Ty(Context)) 3431193323Sed return Error(Loc, "branch condition must have 'i1' type"); 3432198090Srdivacky 3433193323Sed if (ParseToken(lltok::comma, "expected ',' after branch condition") || 3434198892Srdivacky ParseTypeAndBasicBlock(Op1, Loc, PFS) || 3435193323Sed ParseToken(lltok::comma, "expected ',' after true destination") || 3436198892Srdivacky ParseTypeAndBasicBlock(Op2, Loc2, PFS)) 3437193323Sed return true; 3438198090Srdivacky 3439198892Srdivacky Inst = BranchInst::Create(Op1, Op2, Op0); 3440193323Sed return false; 3441193323Sed} 3442193323Sed 3443193323Sed/// ParseSwitch 3444193323Sed/// Instruction 3445193323Sed/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']' 3446193323Sed/// JumpTable 3447193323Sed/// ::= (TypeAndValue ',' TypeAndValue)* 3448193323Sedbool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) { 3449193323Sed LocTy CondLoc, BBLoc; 3450198892Srdivacky Value *Cond; 3451198892Srdivacky BasicBlock *DefaultBB; 3452193323Sed if (ParseTypeAndValue(Cond, CondLoc, PFS) || 3453193323Sed ParseToken(lltok::comma, "expected ',' after switch condition") || 3454198892Srdivacky ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) || 3455193323Sed ParseToken(lltok::lsquare, "expected '[' with switch table")) 3456193323Sed return true; 3457193323Sed 3458204642Srdivacky if (!Cond->getType()->isIntegerTy()) 3459193323Sed return Error(CondLoc, "switch condition must have integer type"); 3460198090Srdivacky 3461193323Sed // Parse the jump table pairs. 3462193323Sed SmallPtrSet<Value*, 32> SeenCases; 3463193323Sed SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table; 3464193323Sed while (Lex.getKind() != lltok::rsquare) { 3465198892Srdivacky Value *Constant; 3466198892Srdivacky BasicBlock *DestBB; 3467198090Srdivacky 3468193323Sed if (ParseTypeAndValue(Constant, CondLoc, PFS) || 3469193323Sed ParseToken(lltok::comma, "expected ',' after case value") || 3470198892Srdivacky ParseTypeAndBasicBlock(DestBB, PFS)) 3471193323Sed return true; 3472249423Sdim 3473193323Sed if (!SeenCases.insert(Constant)) 3474193323Sed return Error(CondLoc, "duplicate case value in switch"); 3475193323Sed if (!isa<ConstantInt>(Constant)) 3476193323Sed return Error(CondLoc, "case value is not a constant integer"); 3477198090Srdivacky 3478198892Srdivacky Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB)); 3479193323Sed } 3480198090Srdivacky 3481193323Sed Lex.Lex(); // Eat the ']'. 3482198090Srdivacky 3483198892Srdivacky SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size()); 3484193323Sed for (unsigned i = 0, e = Table.size(); i != e; ++i) 3485193323Sed SI->addCase(Table[i].first, Table[i].second); 3486193323Sed Inst = SI; 3487193323Sed return false; 3488193323Sed} 3489193323Sed 3490198892Srdivacky/// ParseIndirectBr 3491198892Srdivacky/// Instruction 3492198892Srdivacky/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']' 3493198892Srdivackybool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) { 3494198892Srdivacky LocTy AddrLoc; 3495198892Srdivacky Value *Address; 3496198892Srdivacky if (ParseTypeAndValue(Address, AddrLoc, PFS) || 3497198892Srdivacky ParseToken(lltok::comma, "expected ',' after indirectbr address") || 3498198892Srdivacky ParseToken(lltok::lsquare, "expected '[' with indirectbr")) 3499198892Srdivacky return true; 3500249423Sdim 3501204642Srdivacky if (!Address->getType()->isPointerTy()) 3502198892Srdivacky return Error(AddrLoc, "indirectbr address must have pointer type"); 3503249423Sdim 3504198892Srdivacky // Parse the destination list. 3505198892Srdivacky SmallVector<BasicBlock*, 16> DestList; 3506249423Sdim 3507198892Srdivacky if (Lex.getKind() != lltok::rsquare) { 3508198892Srdivacky BasicBlock *DestBB; 3509198892Srdivacky if (ParseTypeAndBasicBlock(DestBB, PFS)) 3510198892Srdivacky return true; 3511198892Srdivacky DestList.push_back(DestBB); 3512249423Sdim 3513198892Srdivacky while (EatIfPresent(lltok::comma)) { 3514198892Srdivacky if (ParseTypeAndBasicBlock(DestBB, PFS)) 3515198892Srdivacky return true; 3516198892Srdivacky DestList.push_back(DestBB); 3517198892Srdivacky } 3518198892Srdivacky } 3519249423Sdim 3520198892Srdivacky if (ParseToken(lltok::rsquare, "expected ']' at end of block list")) 3521198892Srdivacky return true; 3522198892Srdivacky 3523198892Srdivacky IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size()); 3524198892Srdivacky for (unsigned i = 0, e = DestList.size(); i != e; ++i) 3525198892Srdivacky IBI->addDestination(DestList[i]); 3526198892Srdivacky Inst = IBI; 3527198892Srdivacky return false; 3528198892Srdivacky} 3529198892Srdivacky 3530198892Srdivacky 3531193323Sed/// ParseInvoke 3532193323Sed/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList 3533193323Sed/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue 3534193323Sedbool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) { 3535193323Sed LocTy CallLoc = Lex.getLoc(); 3536243830Sdim AttrBuilder RetAttrs, FnAttrs; 3537249423Sdim std::vector<unsigned> FwdRefAttrGrps; 3538249423Sdim LocTy NoBuiltinLoc; 3539198090Srdivacky CallingConv::ID CC; 3540224145Sdim Type *RetType = 0; 3541193323Sed LocTy RetTypeLoc; 3542193323Sed ValID CalleeID; 3543193323Sed SmallVector<ParamInfo, 16> ArgList; 3544193323Sed 3545198892Srdivacky BasicBlock *NormalBB, *UnwindBB; 3546193323Sed if (ParseOptionalCallingConv(CC) || 3547249423Sdim ParseOptionalReturnAttrs(RetAttrs) || 3548193323Sed ParseType(RetType, RetTypeLoc, true /*void allowed*/) || 3549193323Sed ParseValID(CalleeID) || 3550193323Sed ParseParameterList(ArgList, PFS) || 3551249423Sdim ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false, 3552249423Sdim NoBuiltinLoc) || 3553193323Sed ParseToken(lltok::kw_to, "expected 'to' in invoke") || 3554198892Srdivacky ParseTypeAndBasicBlock(NormalBB, PFS) || 3555193323Sed ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") || 3556198892Srdivacky ParseTypeAndBasicBlock(UnwindBB, PFS)) 3557193323Sed return true; 3558198090Srdivacky 3559193323Sed // If RetType is a non-function pointer type, then this is the short syntax 3560193323Sed // for the call, which means that RetType is just the return type. Infer the 3561193323Sed // rest of the function argument types from the arguments that are present. 3562226633Sdim PointerType *PFTy = 0; 3563226633Sdim FunctionType *Ty = 0; 3564193323Sed if (!(PFTy = dyn_cast<PointerType>(RetType)) || 3565193323Sed !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) { 3566193323Sed // Pull out the types of all of the arguments... 3567224145Sdim std::vector<Type*> ParamTypes; 3568193323Sed for (unsigned i = 0, e = ArgList.size(); i != e; ++i) 3569193323Sed ParamTypes.push_back(ArgList[i].V->getType()); 3570198090Srdivacky 3571193323Sed if (!FunctionType::isValidReturnType(RetType)) 3572193323Sed return Error(RetTypeLoc, "Invalid result type for LLVM function"); 3573198090Srdivacky 3574198090Srdivacky Ty = FunctionType::get(RetType, ParamTypes, false); 3575198090Srdivacky PFTy = PointerType::getUnqual(Ty); 3576193323Sed } 3577198090Srdivacky 3578193323Sed // Look up the callee. 3579193323Sed Value *Callee; 3580202375Srdivacky if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true; 3581198090Srdivacky 3582249423Sdim // Set up the Attribute for the function. 3583249423Sdim SmallVector<AttributeSet, 8> Attrs; 3584243830Sdim if (RetAttrs.hasAttributes()) 3585249423Sdim Attrs.push_back(AttributeSet::get(RetType->getContext(), 3586249423Sdim AttributeSet::ReturnIndex, 3587249423Sdim RetAttrs)); 3588198090Srdivacky 3589193323Sed SmallVector<Value*, 8> Args; 3590198090Srdivacky 3591193323Sed // Loop through FunctionType's arguments and ensure they are specified 3592193323Sed // correctly. Also, gather any parameter attributes. 3593193323Sed FunctionType::param_iterator I = Ty->param_begin(); 3594193323Sed FunctionType::param_iterator E = Ty->param_end(); 3595193323Sed for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { 3596226633Sdim Type *ExpectedTy = 0; 3597193323Sed if (I != E) { 3598193323Sed ExpectedTy = *I++; 3599193323Sed } else if (!Ty->isVarArg()) { 3600193323Sed return Error(ArgList[i].Loc, "too many arguments specified"); 3601193323Sed } 3602198090Srdivacky 3603193323Sed if (ExpectedTy && ExpectedTy != ArgList[i].V->getType()) 3604193323Sed return Error(ArgList[i].Loc, "argument is not of expected type '" + 3605224145Sdim getTypeString(ExpectedTy) + "'"); 3606193323Sed Args.push_back(ArgList[i].V); 3607249423Sdim if (ArgList[i].Attrs.hasAttributes(i + 1)) { 3608249423Sdim AttrBuilder B(ArgList[i].Attrs, i + 1); 3609249423Sdim Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B)); 3610249423Sdim } 3611193323Sed } 3612198090Srdivacky 3613193323Sed if (I != E) 3614193323Sed return Error(CallLoc, "not enough parameters specified for call"); 3615198090Srdivacky 3616243830Sdim if (FnAttrs.hasAttributes()) 3617249423Sdim Attrs.push_back(AttributeSet::get(RetType->getContext(), 3618249423Sdim AttributeSet::FunctionIndex, 3619249423Sdim FnAttrs)); 3620198090Srdivacky 3621249423Sdim // Finish off the Attribute and check them 3622249423Sdim AttributeSet PAL = AttributeSet::get(Context, Attrs); 3623198090Srdivacky 3624224145Sdim InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB, Args); 3625193323Sed II->setCallingConv(CC); 3626193323Sed II->setAttributes(PAL); 3627249423Sdim ForwardRefAttrGroups[II] = FwdRefAttrGrps; 3628193323Sed Inst = II; 3629193323Sed return false; 3630193323Sed} 3631193323Sed 3632226633Sdim/// ParseResume 3633226633Sdim/// ::= 'resume' TypeAndValue 3634226633Sdimbool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) { 3635226633Sdim Value *Exn; LocTy ExnLoc; 3636226633Sdim if (ParseTypeAndValue(Exn, ExnLoc, PFS)) 3637226633Sdim return true; 3638193323Sed 3639226633Sdim ResumeInst *RI = ResumeInst::Create(Exn); 3640226633Sdim Inst = RI; 3641226633Sdim return false; 3642226633Sdim} 3643193323Sed 3644193323Sed//===----------------------------------------------------------------------===// 3645193323Sed// Binary Operators. 3646193323Sed//===----------------------------------------------------------------------===// 3647193323Sed 3648193323Sed/// ParseArithmetic 3649193323Sed/// ::= ArithmeticOps TypeAndValue ',' Value 3650193323Sed/// 3651193323Sed/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1, 3652193323Sed/// then any integer operand is allowed, if it is 2, any fp operand is allowed. 3653193323Sedbool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS, 3654193323Sed unsigned Opc, unsigned OperandType) { 3655193323Sed LocTy Loc; Value *LHS, *RHS; 3656193323Sed if (ParseTypeAndValue(LHS, Loc, PFS) || 3657193323Sed ParseToken(lltok::comma, "expected ',' in arithmetic operation") || 3658193323Sed ParseValue(LHS->getType(), RHS, PFS)) 3659193323Sed return true; 3660193323Sed 3661193323Sed bool Valid; 3662193323Sed switch (OperandType) { 3663198090Srdivacky default: llvm_unreachable("Unknown operand type!"); 3664193323Sed case 0: // int or FP. 3665203954Srdivacky Valid = LHS->getType()->isIntOrIntVectorTy() || 3666203954Srdivacky LHS->getType()->isFPOrFPVectorTy(); 3667193323Sed break; 3668203954Srdivacky case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break; 3669203954Srdivacky case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break; 3670193323Sed } 3671198090Srdivacky 3672193323Sed if (!Valid) 3673193323Sed return Error(Loc, "invalid operand type for instruction"); 3674198090Srdivacky 3675193323Sed Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); 3676193323Sed return false; 3677193323Sed} 3678193323Sed 3679193323Sed/// ParseLogical 3680193323Sed/// ::= ArithmeticOps TypeAndValue ',' Value { 3681193323Sedbool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS, 3682193323Sed unsigned Opc) { 3683193323Sed LocTy Loc; Value *LHS, *RHS; 3684193323Sed if (ParseTypeAndValue(LHS, Loc, PFS) || 3685193323Sed ParseToken(lltok::comma, "expected ',' in logical operation") || 3686193323Sed ParseValue(LHS->getType(), RHS, PFS)) 3687193323Sed return true; 3688193323Sed 3689203954Srdivacky if (!LHS->getType()->isIntOrIntVectorTy()) 3690193323Sed return Error(Loc,"instruction requires integer or integer vector operands"); 3691193323Sed 3692193323Sed Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); 3693193323Sed return false; 3694193323Sed} 3695193323Sed 3696193323Sed 3697193323Sed/// ParseCompare 3698193323Sed/// ::= 'icmp' IPredicates TypeAndValue ',' Value 3699193323Sed/// ::= 'fcmp' FPredicates TypeAndValue ',' Value 3700193323Sedbool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS, 3701193323Sed unsigned Opc) { 3702193323Sed // Parse the integer/fp comparison predicate. 3703193323Sed LocTy Loc; 3704193323Sed unsigned Pred; 3705193323Sed Value *LHS, *RHS; 3706193323Sed if (ParseCmpPredicate(Pred, Opc) || 3707193323Sed ParseTypeAndValue(LHS, Loc, PFS) || 3708193323Sed ParseToken(lltok::comma, "expected ',' after compare value") || 3709193323Sed ParseValue(LHS->getType(), RHS, PFS)) 3710193323Sed return true; 3711198090Srdivacky 3712193323Sed if (Opc == Instruction::FCmp) { 3713203954Srdivacky if (!LHS->getType()->isFPOrFPVectorTy()) 3714193323Sed return Error(Loc, "fcmp requires floating point operands"); 3715193323Sed Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS); 3716198090Srdivacky } else { 3717198090Srdivacky assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!"); 3718203954Srdivacky if (!LHS->getType()->isIntOrIntVectorTy() && 3719234353Sdim !LHS->getType()->getScalarType()->isPointerTy()) 3720193323Sed return Error(Loc, "icmp requires integer operands"); 3721193323Sed Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS); 3722193323Sed } 3723193323Sed return false; 3724193323Sed} 3725193323Sed 3726193323Sed//===----------------------------------------------------------------------===// 3727193323Sed// Other Instructions. 3728193323Sed//===----------------------------------------------------------------------===// 3729193323Sed 3730193323Sed 3731193323Sed/// ParseCast 3732193323Sed/// ::= CastOpc TypeAndValue 'to' Type 3733193323Sedbool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS, 3734193323Sed unsigned Opc) { 3735224145Sdim LocTy Loc; 3736224145Sdim Value *Op; 3737224145Sdim Type *DestTy = 0; 3738193323Sed if (ParseTypeAndValue(Op, Loc, PFS) || 3739193323Sed ParseToken(lltok::kw_to, "expected 'to' after cast value") || 3740193323Sed ParseType(DestTy)) 3741193323Sed return true; 3742198090Srdivacky 3743193323Sed if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) { 3744193323Sed CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy); 3745193323Sed return Error(Loc, "invalid cast opcode for cast from '" + 3746224145Sdim getTypeString(Op->getType()) + "' to '" + 3747224145Sdim getTypeString(DestTy) + "'"); 3748193323Sed } 3749193323Sed Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy); 3750193323Sed return false; 3751193323Sed} 3752193323Sed 3753193323Sed/// ParseSelect 3754193323Sed/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue 3755193323Sedbool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) { 3756193323Sed LocTy Loc; 3757193323Sed Value *Op0, *Op1, *Op2; 3758193323Sed if (ParseTypeAndValue(Op0, Loc, PFS) || 3759193323Sed ParseToken(lltok::comma, "expected ',' after select condition") || 3760193323Sed ParseTypeAndValue(Op1, PFS) || 3761193323Sed ParseToken(lltok::comma, "expected ',' after select value") || 3762193323Sed ParseTypeAndValue(Op2, PFS)) 3763193323Sed return true; 3764198090Srdivacky 3765193323Sed if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2)) 3766193323Sed return Error(Loc, Reason); 3767198090Srdivacky 3768193323Sed Inst = SelectInst::Create(Op0, Op1, Op2); 3769193323Sed return false; 3770193323Sed} 3771193323Sed 3772193323Sed/// ParseVA_Arg 3773193323Sed/// ::= 'va_arg' TypeAndValue ',' Type 3774193323Sedbool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) { 3775193323Sed Value *Op; 3776224145Sdim Type *EltTy = 0; 3777193323Sed LocTy TypeLoc; 3778193323Sed if (ParseTypeAndValue(Op, PFS) || 3779193323Sed ParseToken(lltok::comma, "expected ',' after vaarg operand") || 3780193323Sed ParseType(EltTy, TypeLoc)) 3781193323Sed return true; 3782198090Srdivacky 3783193323Sed if (!EltTy->isFirstClassType()) 3784193323Sed return Error(TypeLoc, "va_arg requires operand with first class type"); 3785193323Sed 3786193323Sed Inst = new VAArgInst(Op, EltTy); 3787193323Sed return false; 3788193323Sed} 3789193323Sed 3790193323Sed/// ParseExtractElement 3791193323Sed/// ::= 'extractelement' TypeAndValue ',' TypeAndValue 3792193323Sedbool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) { 3793193323Sed LocTy Loc; 3794193323Sed Value *Op0, *Op1; 3795193323Sed if (ParseTypeAndValue(Op0, Loc, PFS) || 3796193323Sed ParseToken(lltok::comma, "expected ',' after extract value") || 3797193323Sed ParseTypeAndValue(Op1, PFS)) 3798193323Sed return true; 3799198090Srdivacky 3800193323Sed if (!ExtractElementInst::isValidOperands(Op0, Op1)) 3801193323Sed return Error(Loc, "invalid extractelement operands"); 3802198090Srdivacky 3803198090Srdivacky Inst = ExtractElementInst::Create(Op0, Op1); 3804193323Sed return false; 3805193323Sed} 3806193323Sed 3807193323Sed/// ParseInsertElement 3808193323Sed/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue 3809193323Sedbool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) { 3810193323Sed LocTy Loc; 3811193323Sed Value *Op0, *Op1, *Op2; 3812193323Sed if (ParseTypeAndValue(Op0, Loc, PFS) || 3813193323Sed ParseToken(lltok::comma, "expected ',' after insertelement value") || 3814193323Sed ParseTypeAndValue(Op1, PFS) || 3815193323Sed ParseToken(lltok::comma, "expected ',' after insertelement value") || 3816193323Sed ParseTypeAndValue(Op2, PFS)) 3817193323Sed return true; 3818198090Srdivacky 3819193323Sed if (!InsertElementInst::isValidOperands(Op0, Op1, Op2)) 3820198090Srdivacky return Error(Loc, "invalid insertelement operands"); 3821198090Srdivacky 3822193323Sed Inst = InsertElementInst::Create(Op0, Op1, Op2); 3823193323Sed return false; 3824193323Sed} 3825193323Sed 3826193323Sed/// ParseShuffleVector 3827193323Sed/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue 3828193323Sedbool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) { 3829193323Sed LocTy Loc; 3830193323Sed Value *Op0, *Op1, *Op2; 3831193323Sed if (ParseTypeAndValue(Op0, Loc, PFS) || 3832193323Sed ParseToken(lltok::comma, "expected ',' after shuffle mask") || 3833193323Sed ParseTypeAndValue(Op1, PFS) || 3834193323Sed ParseToken(lltok::comma, "expected ',' after shuffle value") || 3835193323Sed ParseTypeAndValue(Op2, PFS)) 3836193323Sed return true; 3837198090Srdivacky 3838193323Sed if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2)) 3839234353Sdim return Error(Loc, "invalid shufflevector operands"); 3840198090Srdivacky 3841193323Sed Inst = new ShuffleVectorInst(Op0, Op1, Op2); 3842193323Sed return false; 3843193323Sed} 3844193323Sed 3845193323Sed/// ParsePHI 3846198396Srdivacky/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')* 3847201360Srdivackyint LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) { 3848224145Sdim Type *Ty = 0; LocTy TypeLoc; 3849193323Sed Value *Op0, *Op1; 3850198090Srdivacky 3851224145Sdim if (ParseType(Ty, TypeLoc) || 3852193323Sed ParseToken(lltok::lsquare, "expected '[' in phi value list") || 3853193323Sed ParseValue(Ty, Op0, PFS) || 3854193323Sed ParseToken(lltok::comma, "expected ',' after insertelement value") || 3855198090Srdivacky ParseValue(Type::getLabelTy(Context), Op1, PFS) || 3856193323Sed ParseToken(lltok::rsquare, "expected ']' in phi value list")) 3857193323Sed return true; 3858198090Srdivacky 3859201360Srdivacky bool AteExtraComma = false; 3860193323Sed SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals; 3861193323Sed while (1) { 3862193323Sed PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1))); 3863198090Srdivacky 3864193323Sed if (!EatIfPresent(lltok::comma)) 3865193323Sed break; 3866193323Sed 3867201360Srdivacky if (Lex.getKind() == lltok::MetadataVar) { 3868201360Srdivacky AteExtraComma = true; 3869198396Srdivacky break; 3870201360Srdivacky } 3871198396Srdivacky 3872193323Sed if (ParseToken(lltok::lsquare, "expected '[' in phi value list") || 3873193323Sed ParseValue(Ty, Op0, PFS) || 3874193323Sed ParseToken(lltok::comma, "expected ',' after insertelement value") || 3875198090Srdivacky ParseValue(Type::getLabelTy(Context), Op1, PFS) || 3876193323Sed ParseToken(lltok::rsquare, "expected ']' in phi value list")) 3877193323Sed return true; 3878193323Sed } 3879198090Srdivacky 3880193323Sed if (!Ty->isFirstClassType()) 3881193323Sed return Error(TypeLoc, "phi node must have first class type"); 3882193323Sed 3883221345Sdim PHINode *PN = PHINode::Create(Ty, PHIVals.size()); 3884193323Sed for (unsigned i = 0, e = PHIVals.size(); i != e; ++i) 3885193323Sed PN->addIncoming(PHIVals[i].first, PHIVals[i].second); 3886193323Sed Inst = PN; 3887201360Srdivacky return AteExtraComma ? InstExtraComma : InstNormal; 3888193323Sed} 3889193323Sed 3890226633Sdim/// ParseLandingPad 3891226633Sdim/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+ 3892226633Sdim/// Clause 3893226633Sdim/// ::= 'catch' TypeAndValue 3894226633Sdim/// ::= 'filter' 3895226633Sdim/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )* 3896226633Sdimbool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) { 3897226633Sdim Type *Ty = 0; LocTy TyLoc; 3898226633Sdim Value *PersFn; LocTy PersFnLoc; 3899226633Sdim 3900226633Sdim if (ParseType(Ty, TyLoc) || 3901226633Sdim ParseToken(lltok::kw_personality, "expected 'personality'") || 3902226633Sdim ParseTypeAndValue(PersFn, PersFnLoc, PFS)) 3903226633Sdim return true; 3904226633Sdim 3905226633Sdim LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, 0); 3906226633Sdim LP->setCleanup(EatIfPresent(lltok::kw_cleanup)); 3907226633Sdim 3908226633Sdim while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){ 3909226633Sdim LandingPadInst::ClauseType CT; 3910226633Sdim if (EatIfPresent(lltok::kw_catch)) 3911226633Sdim CT = LandingPadInst::Catch; 3912226633Sdim else if (EatIfPresent(lltok::kw_filter)) 3913226633Sdim CT = LandingPadInst::Filter; 3914226633Sdim else 3915226633Sdim return TokError("expected 'catch' or 'filter' clause type"); 3916226633Sdim 3917226633Sdim Value *V; LocTy VLoc; 3918226633Sdim if (ParseTypeAndValue(V, VLoc, PFS)) { 3919226633Sdim delete LP; 3920226633Sdim return true; 3921226633Sdim } 3922226633Sdim 3923226633Sdim // A 'catch' type expects a non-array constant. A filter clause expects an 3924226633Sdim // array constant. 3925226633Sdim if (CT == LandingPadInst::Catch) { 3926226633Sdim if (isa<ArrayType>(V->getType())) 3927226633Sdim Error(VLoc, "'catch' clause has an invalid type"); 3928226633Sdim } else { 3929226633Sdim if (!isa<ArrayType>(V->getType())) 3930226633Sdim Error(VLoc, "'filter' clause has an invalid type"); 3931226633Sdim } 3932226633Sdim 3933226633Sdim LP->addClause(V); 3934226633Sdim } 3935226633Sdim 3936226633Sdim Inst = LP; 3937226633Sdim return false; 3938226633Sdim} 3939226633Sdim 3940193323Sed/// ParseCall 3941193323Sed/// ::= 'tail'? 'call' OptionalCallingConv OptionalAttrs Type Value 3942193323Sed/// ParameterList OptionalAttrs 3943193323Sedbool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS, 3944193323Sed bool isTail) { 3945243830Sdim AttrBuilder RetAttrs, FnAttrs; 3946249423Sdim std::vector<unsigned> FwdRefAttrGrps; 3947263508Sdim LocTy BuiltinLoc; 3948198090Srdivacky CallingConv::ID CC; 3949224145Sdim Type *RetType = 0; 3950193323Sed LocTy RetTypeLoc; 3951193323Sed ValID CalleeID; 3952193323Sed SmallVector<ParamInfo, 16> ArgList; 3953193323Sed LocTy CallLoc = Lex.getLoc(); 3954198090Srdivacky 3955193323Sed if ((isTail && ParseToken(lltok::kw_call, "expected 'tail call'")) || 3956193323Sed ParseOptionalCallingConv(CC) || 3957249423Sdim ParseOptionalReturnAttrs(RetAttrs) || 3958193323Sed ParseType(RetType, RetTypeLoc, true /*void allowed*/) || 3959193323Sed ParseValID(CalleeID) || 3960193323Sed ParseParameterList(ArgList, PFS) || 3961249423Sdim ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false, 3962263508Sdim BuiltinLoc)) 3963193323Sed return true; 3964198090Srdivacky 3965193323Sed // If RetType is a non-function pointer type, then this is the short syntax 3966193323Sed // for the call, which means that RetType is just the return type. Infer the 3967193323Sed // rest of the function argument types from the arguments that are present. 3968226633Sdim PointerType *PFTy = 0; 3969226633Sdim FunctionType *Ty = 0; 3970193323Sed if (!(PFTy = dyn_cast<PointerType>(RetType)) || 3971193323Sed !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) { 3972193323Sed // Pull out the types of all of the arguments... 3973224145Sdim std::vector<Type*> ParamTypes; 3974193323Sed for (unsigned i = 0, e = ArgList.size(); i != e; ++i) 3975193323Sed ParamTypes.push_back(ArgList[i].V->getType()); 3976198090Srdivacky 3977193323Sed if (!FunctionType::isValidReturnType(RetType)) 3978193323Sed return Error(RetTypeLoc, "Invalid result type for LLVM function"); 3979198090Srdivacky 3980198090Srdivacky Ty = FunctionType::get(RetType, ParamTypes, false); 3981198090Srdivacky PFTy = PointerType::getUnqual(Ty); 3982193323Sed } 3983198090Srdivacky 3984193323Sed // Look up the callee. 3985193323Sed Value *Callee; 3986202375Srdivacky if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true; 3987198090Srdivacky 3988249423Sdim // Set up the Attribute for the function. 3989249423Sdim SmallVector<AttributeSet, 8> Attrs; 3990243830Sdim if (RetAttrs.hasAttributes()) 3991249423Sdim Attrs.push_back(AttributeSet::get(RetType->getContext(), 3992249423Sdim AttributeSet::ReturnIndex, 3993249423Sdim RetAttrs)); 3994198090Srdivacky 3995193323Sed SmallVector<Value*, 8> Args; 3996198090Srdivacky 3997193323Sed // Loop through FunctionType's arguments and ensure they are specified 3998193323Sed // correctly. Also, gather any parameter attributes. 3999193323Sed FunctionType::param_iterator I = Ty->param_begin(); 4000193323Sed FunctionType::param_iterator E = Ty->param_end(); 4001193323Sed for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { 4002226633Sdim Type *ExpectedTy = 0; 4003193323Sed if (I != E) { 4004193323Sed ExpectedTy = *I++; 4005193323Sed } else if (!Ty->isVarArg()) { 4006193323Sed return Error(ArgList[i].Loc, "too many arguments specified"); 4007193323Sed } 4008198090Srdivacky 4009193323Sed if (ExpectedTy && ExpectedTy != ArgList[i].V->getType()) 4010193323Sed return Error(ArgList[i].Loc, "argument is not of expected type '" + 4011224145Sdim getTypeString(ExpectedTy) + "'"); 4012193323Sed Args.push_back(ArgList[i].V); 4013249423Sdim if (ArgList[i].Attrs.hasAttributes(i + 1)) { 4014249423Sdim AttrBuilder B(ArgList[i].Attrs, i + 1); 4015249423Sdim Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B)); 4016249423Sdim } 4017193323Sed } 4018198090Srdivacky 4019193323Sed if (I != E) 4020193323Sed return Error(CallLoc, "not enough parameters specified for call"); 4021193323Sed 4022243830Sdim if (FnAttrs.hasAttributes()) 4023249423Sdim Attrs.push_back(AttributeSet::get(RetType->getContext(), 4024249423Sdim AttributeSet::FunctionIndex, 4025249423Sdim FnAttrs)); 4026193323Sed 4027249423Sdim // Finish off the Attribute and check them 4028249423Sdim AttributeSet PAL = AttributeSet::get(Context, Attrs); 4029198090Srdivacky 4030224145Sdim CallInst *CI = CallInst::Create(Callee, Args); 4031193323Sed CI->setTailCall(isTail); 4032193323Sed CI->setCallingConv(CC); 4033193323Sed CI->setAttributes(PAL); 4034249423Sdim ForwardRefAttrGroups[CI] = FwdRefAttrGrps; 4035193323Sed Inst = CI; 4036193323Sed return false; 4037193323Sed} 4038193323Sed 4039193323Sed//===----------------------------------------------------------------------===// 4040193323Sed// Memory Instructions. 4041193323Sed//===----------------------------------------------------------------------===// 4042193323Sed 4043193323Sed/// ParseAlloc 4044198090Srdivacky/// ::= 'alloca' Type (',' TypeAndValue)? (',' OptionalInfo)? 4045224145Sdimint LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) { 4046193323Sed Value *Size = 0; 4047195340Sed LocTy SizeLoc; 4048193323Sed unsigned Alignment = 0; 4049224145Sdim Type *Ty = 0; 4050193323Sed if (ParseType(Ty)) return true; 4051193323Sed 4052201360Srdivacky bool AteExtraComma = false; 4053193323Sed if (EatIfPresent(lltok::comma)) { 4054201360Srdivacky if (Lex.getKind() == lltok::kw_align) { 4055201360Srdivacky if (ParseOptionalAlignment(Alignment)) return true; 4056201360Srdivacky } else if (Lex.getKind() == lltok::MetadataVar) { 4057201360Srdivacky AteExtraComma = true; 4058198090Srdivacky } else { 4059201360Srdivacky if (ParseTypeAndValue(Size, SizeLoc, PFS) || 4060201360Srdivacky ParseOptionalCommaAlign(Alignment, AteExtraComma)) 4061201360Srdivacky return true; 4062193323Sed } 4063193323Sed } 4064193323Sed 4065210299Sed if (Size && !Size->getType()->isIntegerTy()) 4066210299Sed return Error(SizeLoc, "element count must have integer type"); 4067193323Sed 4068224145Sdim Inst = new AllocaInst(Ty, Size, Alignment); 4069224145Sdim return AteExtraComma ? InstExtraComma : InstNormal; 4070193323Sed} 4071193323Sed 4072193323Sed/// ParseLoad 4073226633Sdim/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)? 4074249423Sdim/// ::= 'load' 'atomic' 'volatile'? TypeAndValue 4075226633Sdim/// 'singlethread'? AtomicOrdering (',' 'align' i32)? 4076234353Sdimint LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) { 4077193323Sed Value *Val; LocTy Loc; 4078198090Srdivacky unsigned Alignment = 0; 4079201360Srdivacky bool AteExtraComma = false; 4080226633Sdim bool isAtomic = false; 4081226633Sdim AtomicOrdering Ordering = NotAtomic; 4082226633Sdim SynchronizationScope Scope = CrossThread; 4083226633Sdim 4084226633Sdim if (Lex.getKind() == lltok::kw_atomic) { 4085226633Sdim isAtomic = true; 4086226633Sdim Lex.Lex(); 4087226633Sdim } 4088226633Sdim 4089234353Sdim bool isVolatile = false; 4090226633Sdim if (Lex.getKind() == lltok::kw_volatile) { 4091226633Sdim isVolatile = true; 4092226633Sdim Lex.Lex(); 4093226633Sdim } 4094226633Sdim 4095201360Srdivacky if (ParseTypeAndValue(Val, Loc, PFS) || 4096226633Sdim ParseScopeAndOrdering(isAtomic, Scope, Ordering) || 4097201360Srdivacky ParseOptionalCommaAlign(Alignment, AteExtraComma)) 4098201360Srdivacky return true; 4099193323Sed 4100204642Srdivacky if (!Val->getType()->isPointerTy() || 4101193323Sed !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType()) 4102193323Sed return Error(Loc, "load operand must be a pointer to a first class type"); 4103226633Sdim if (isAtomic && !Alignment) 4104226633Sdim return Error(Loc, "atomic load must have explicit non-zero alignment"); 4105226633Sdim if (Ordering == Release || Ordering == AcquireRelease) 4106226633Sdim return Error(Loc, "atomic load cannot use Release ordering"); 4107198090Srdivacky 4108226633Sdim Inst = new LoadInst(Val, "", isVolatile, Alignment, Ordering, Scope); 4109201360Srdivacky return AteExtraComma ? InstExtraComma : InstNormal; 4110193323Sed} 4111193323Sed 4112193323Sed/// ParseStore 4113226633Sdim 4114226633Sdim/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)? 4115226633Sdim/// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue 4116226633Sdim/// 'singlethread'? AtomicOrdering (',' 'align' i32)? 4117234353Sdimint LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) { 4118193323Sed Value *Val, *Ptr; LocTy Loc, PtrLoc; 4119198090Srdivacky unsigned Alignment = 0; 4120201360Srdivacky bool AteExtraComma = false; 4121226633Sdim bool isAtomic = false; 4122226633Sdim AtomicOrdering Ordering = NotAtomic; 4123226633Sdim SynchronizationScope Scope = CrossThread; 4124226633Sdim 4125226633Sdim if (Lex.getKind() == lltok::kw_atomic) { 4126226633Sdim isAtomic = true; 4127226633Sdim Lex.Lex(); 4128226633Sdim } 4129226633Sdim 4130234353Sdim bool isVolatile = false; 4131226633Sdim if (Lex.getKind() == lltok::kw_volatile) { 4132226633Sdim isVolatile = true; 4133226633Sdim Lex.Lex(); 4134226633Sdim } 4135226633Sdim 4136193323Sed if (ParseTypeAndValue(Val, Loc, PFS) || 4137193323Sed ParseToken(lltok::comma, "expected ',' after store operand") || 4138201360Srdivacky ParseTypeAndValue(Ptr, PtrLoc, PFS) || 4139226633Sdim ParseScopeAndOrdering(isAtomic, Scope, Ordering) || 4140201360Srdivacky ParseOptionalCommaAlign(Alignment, AteExtraComma)) 4141193323Sed return true; 4142198090Srdivacky 4143204642Srdivacky if (!Ptr->getType()->isPointerTy()) 4144193323Sed return Error(PtrLoc, "store operand must be a pointer"); 4145193323Sed if (!Val->getType()->isFirstClassType()) 4146193323Sed return Error(Loc, "store operand must be a first class value"); 4147193323Sed if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType()) 4148193323Sed return Error(Loc, "stored value and pointer type do not match"); 4149226633Sdim if (isAtomic && !Alignment) 4150226633Sdim return Error(Loc, "atomic store must have explicit non-zero alignment"); 4151226633Sdim if (Ordering == Acquire || Ordering == AcquireRelease) 4152226633Sdim return Error(Loc, "atomic store cannot use Acquire ordering"); 4153198090Srdivacky 4154226633Sdim Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, Scope); 4155201360Srdivacky return AteExtraComma ? InstExtraComma : InstNormal; 4156193323Sed} 4157193323Sed 4158226633Sdim/// ParseCmpXchg 4159226633Sdim/// ::= 'cmpxchg' 'volatile'? TypeAndValue ',' TypeAndValue ',' TypeAndValue 4160226633Sdim/// 'singlethread'? AtomicOrdering 4161226633Sdimint LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) { 4162226633Sdim Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc; 4163226633Sdim bool AteExtraComma = false; 4164226633Sdim AtomicOrdering Ordering = NotAtomic; 4165226633Sdim SynchronizationScope Scope = CrossThread; 4166226633Sdim bool isVolatile = false; 4167226633Sdim 4168226633Sdim if (EatIfPresent(lltok::kw_volatile)) 4169226633Sdim isVolatile = true; 4170226633Sdim 4171226633Sdim if (ParseTypeAndValue(Ptr, PtrLoc, PFS) || 4172226633Sdim ParseToken(lltok::comma, "expected ',' after cmpxchg address") || 4173226633Sdim ParseTypeAndValue(Cmp, CmpLoc, PFS) || 4174226633Sdim ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") || 4175226633Sdim ParseTypeAndValue(New, NewLoc, PFS) || 4176226633Sdim ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering)) 4177226633Sdim return true; 4178226633Sdim 4179226633Sdim if (Ordering == Unordered) 4180226633Sdim return TokError("cmpxchg cannot be unordered"); 4181226633Sdim if (!Ptr->getType()->isPointerTy()) 4182226633Sdim return Error(PtrLoc, "cmpxchg operand must be a pointer"); 4183226633Sdim if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType()) 4184226633Sdim return Error(CmpLoc, "compare value and pointer type do not match"); 4185226633Sdim if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType()) 4186226633Sdim return Error(NewLoc, "new value and pointer type do not match"); 4187226633Sdim if (!New->getType()->isIntegerTy()) 4188226633Sdim return Error(NewLoc, "cmpxchg operand must be an integer"); 4189226633Sdim unsigned Size = New->getType()->getPrimitiveSizeInBits(); 4190226633Sdim if (Size < 8 || (Size & (Size - 1))) 4191226633Sdim return Error(NewLoc, "cmpxchg operand must be power-of-two byte-sized" 4192226633Sdim " integer"); 4193226633Sdim 4194226633Sdim AtomicCmpXchgInst *CXI = 4195226633Sdim new AtomicCmpXchgInst(Ptr, Cmp, New, Ordering, Scope); 4196226633Sdim CXI->setVolatile(isVolatile); 4197226633Sdim Inst = CXI; 4198226633Sdim return AteExtraComma ? InstExtraComma : InstNormal; 4199226633Sdim} 4200226633Sdim 4201226633Sdim/// ParseAtomicRMW 4202226633Sdim/// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue 4203226633Sdim/// 'singlethread'? AtomicOrdering 4204226633Sdimint LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) { 4205226633Sdim Value *Ptr, *Val; LocTy PtrLoc, ValLoc; 4206226633Sdim bool AteExtraComma = false; 4207226633Sdim AtomicOrdering Ordering = NotAtomic; 4208226633Sdim SynchronizationScope Scope = CrossThread; 4209226633Sdim bool isVolatile = false; 4210226633Sdim AtomicRMWInst::BinOp Operation; 4211226633Sdim 4212226633Sdim if (EatIfPresent(lltok::kw_volatile)) 4213226633Sdim isVolatile = true; 4214226633Sdim 4215226633Sdim switch (Lex.getKind()) { 4216226633Sdim default: return TokError("expected binary operation in atomicrmw"); 4217226633Sdim case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break; 4218226633Sdim case lltok::kw_add: Operation = AtomicRMWInst::Add; break; 4219226633Sdim case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break; 4220226633Sdim case lltok::kw_and: Operation = AtomicRMWInst::And; break; 4221226633Sdim case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break; 4222226633Sdim case lltok::kw_or: Operation = AtomicRMWInst::Or; break; 4223226633Sdim case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break; 4224226633Sdim case lltok::kw_max: Operation = AtomicRMWInst::Max; break; 4225226633Sdim case lltok::kw_min: Operation = AtomicRMWInst::Min; break; 4226226633Sdim case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break; 4227226633Sdim case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break; 4228226633Sdim } 4229226633Sdim Lex.Lex(); // Eat the operation. 4230226633Sdim 4231226633Sdim if (ParseTypeAndValue(Ptr, PtrLoc, PFS) || 4232226633Sdim ParseToken(lltok::comma, "expected ',' after atomicrmw address") || 4233226633Sdim ParseTypeAndValue(Val, ValLoc, PFS) || 4234226633Sdim ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering)) 4235226633Sdim return true; 4236226633Sdim 4237226633Sdim if (Ordering == Unordered) 4238226633Sdim return TokError("atomicrmw cannot be unordered"); 4239226633Sdim if (!Ptr->getType()->isPointerTy()) 4240226633Sdim return Error(PtrLoc, "atomicrmw operand must be a pointer"); 4241226633Sdim if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType()) 4242226633Sdim return Error(ValLoc, "atomicrmw value and pointer type do not match"); 4243226633Sdim if (!Val->getType()->isIntegerTy()) 4244226633Sdim return Error(ValLoc, "atomicrmw operand must be an integer"); 4245226633Sdim unsigned Size = Val->getType()->getPrimitiveSizeInBits(); 4246226633Sdim if (Size < 8 || (Size & (Size - 1))) 4247226633Sdim return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized" 4248226633Sdim " integer"); 4249226633Sdim 4250226633Sdim AtomicRMWInst *RMWI = 4251226633Sdim new AtomicRMWInst(Operation, Ptr, Val, Ordering, Scope); 4252226633Sdim RMWI->setVolatile(isVolatile); 4253226633Sdim Inst = RMWI; 4254226633Sdim return AteExtraComma ? InstExtraComma : InstNormal; 4255226633Sdim} 4256226633Sdim 4257226633Sdim/// ParseFence 4258226633Sdim/// ::= 'fence' 'singlethread'? AtomicOrdering 4259226633Sdimint LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) { 4260226633Sdim AtomicOrdering Ordering = NotAtomic; 4261226633Sdim SynchronizationScope Scope = CrossThread; 4262226633Sdim if (ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering)) 4263226633Sdim return true; 4264226633Sdim 4265226633Sdim if (Ordering == Unordered) 4266226633Sdim return TokError("fence cannot be unordered"); 4267226633Sdim if (Ordering == Monotonic) 4268226633Sdim return TokError("fence cannot be monotonic"); 4269226633Sdim 4270226633Sdim Inst = new FenceInst(Context, Ordering, Scope); 4271226633Sdim return InstNormal; 4272226633Sdim} 4273226633Sdim 4274193323Sed/// ParseGetElementPtr 4275198090Srdivacky/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)* 4276201360Srdivackyint LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) { 4277234353Sdim Value *Ptr = 0; 4278234353Sdim Value *Val = 0; 4279234353Sdim LocTy Loc, EltLoc; 4280198090Srdivacky 4281198090Srdivacky bool InBounds = EatIfPresent(lltok::kw_inbounds); 4282198090Srdivacky 4283193323Sed if (ParseTypeAndValue(Ptr, Loc, PFS)) return true; 4284198090Srdivacky 4285251662Sdim Type *BaseType = Ptr->getType(); 4286251662Sdim PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType()); 4287251662Sdim if (!BasePointerType) 4288193323Sed return Error(Loc, "base of getelementptr must be a pointer"); 4289198090Srdivacky 4290193323Sed SmallVector<Value*, 16> Indices; 4291201360Srdivacky bool AteExtraComma = false; 4292193323Sed while (EatIfPresent(lltok::comma)) { 4293201360Srdivacky if (Lex.getKind() == lltok::MetadataVar) { 4294201360Srdivacky AteExtraComma = true; 4295198090Srdivacky break; 4296201360Srdivacky } 4297193323Sed if (ParseTypeAndValue(Val, EltLoc, PFS)) return true; 4298234353Sdim if (!Val->getType()->getScalarType()->isIntegerTy()) 4299193323Sed return Error(EltLoc, "getelementptr index must be an integer"); 4300234353Sdim if (Val->getType()->isVectorTy() != Ptr->getType()->isVectorTy()) 4301234353Sdim return Error(EltLoc, "getelementptr index type missmatch"); 4302234353Sdim if (Val->getType()->isVectorTy()) { 4303234353Sdim unsigned ValNumEl = cast<VectorType>(Val->getType())->getNumElements(); 4304234353Sdim unsigned PtrNumEl = cast<VectorType>(Ptr->getType())->getNumElements(); 4305234353Sdim if (ValNumEl != PtrNumEl) 4306234353Sdim return Error(EltLoc, 4307234353Sdim "getelementptr vector index has a wrong number of elements"); 4308234353Sdim } 4309193323Sed Indices.push_back(Val); 4310193323Sed } 4311198090Srdivacky 4312251662Sdim if (!Indices.empty() && !BasePointerType->getElementType()->isSized()) 4313251662Sdim return Error(Loc, "base element of getelementptr must be sized"); 4314251662Sdim 4315251662Sdim if (!GetElementPtrInst::getIndexedType(BaseType, Indices)) 4316193323Sed return Error(Loc, "invalid getelementptr indices"); 4317226633Sdim Inst = GetElementPtrInst::Create(Ptr, Indices); 4318198090Srdivacky if (InBounds) 4319198090Srdivacky cast<GetElementPtrInst>(Inst)->setIsInBounds(true); 4320201360Srdivacky return AteExtraComma ? InstExtraComma : InstNormal; 4321193323Sed} 4322193323Sed 4323193323Sed/// ParseExtractValue 4324193323Sed/// ::= 'extractvalue' TypeAndValue (',' uint32)+ 4325201360Srdivackyint LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) { 4326193323Sed Value *Val; LocTy Loc; 4327193323Sed SmallVector<unsigned, 4> Indices; 4328201360Srdivacky bool AteExtraComma; 4329193323Sed if (ParseTypeAndValue(Val, Loc, PFS) || 4330201360Srdivacky ParseIndexList(Indices, AteExtraComma)) 4331193323Sed return true; 4332193323Sed 4333203954Srdivacky if (!Val->getType()->isAggregateType()) 4334203954Srdivacky return Error(Loc, "extractvalue operand must be aggregate type"); 4335193323Sed 4336224145Sdim if (!ExtractValueInst::getIndexedType(Val->getType(), Indices)) 4337193323Sed return Error(Loc, "invalid indices for extractvalue"); 4338224145Sdim Inst = ExtractValueInst::Create(Val, Indices); 4339201360Srdivacky return AteExtraComma ? InstExtraComma : InstNormal; 4340193323Sed} 4341193323Sed 4342193323Sed/// ParseInsertValue 4343193323Sed/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+ 4344201360Srdivackyint LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) { 4345193323Sed Value *Val0, *Val1; LocTy Loc0, Loc1; 4346193323Sed SmallVector<unsigned, 4> Indices; 4347201360Srdivacky bool AteExtraComma; 4348193323Sed if (ParseTypeAndValue(Val0, Loc0, PFS) || 4349193323Sed ParseToken(lltok::comma, "expected comma after insertvalue operand") || 4350193323Sed ParseTypeAndValue(Val1, Loc1, PFS) || 4351201360Srdivacky ParseIndexList(Indices, AteExtraComma)) 4352193323Sed return true; 4353249423Sdim 4354203954Srdivacky if (!Val0->getType()->isAggregateType()) 4355203954Srdivacky return Error(Loc0, "insertvalue operand must be aggregate type"); 4356198090Srdivacky 4357224145Sdim if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices)) 4358193323Sed return Error(Loc0, "invalid indices for insertvalue"); 4359224145Sdim Inst = InsertValueInst::Create(Val0, Val1, Indices); 4360201360Srdivacky return AteExtraComma ? InstExtraComma : InstNormal; 4361193323Sed} 4362193323Sed 4363193323Sed//===----------------------------------------------------------------------===// 4364193323Sed// Embedded metadata. 4365193323Sed//===----------------------------------------------------------------------===// 4366193323Sed 4367193323Sed/// ParseMDNodeVector 4368193323Sed/// ::= Element (',' Element)* 4369193323Sed/// Element 4370193323Sed/// ::= 'null' | TypeAndValue 4371202375Srdivackybool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts, 4372202375Srdivacky PerFunctionState *PFS) { 4373210299Sed // Check for an empty list. 4374210299Sed if (Lex.getKind() == lltok::rbrace) 4375210299Sed return false; 4376210299Sed 4377193323Sed do { 4378201360Srdivacky // Null is a special case since it is typeless. 4379201360Srdivacky if (EatIfPresent(lltok::kw_null)) { 4380201360Srdivacky Elts.push_back(0); 4381201360Srdivacky continue; 4382201360Srdivacky } 4383249423Sdim 4384198090Srdivacky Value *V = 0; 4385224145Sdim if (ParseTypeAndValue(V, PFS)) return true; 4386193323Sed Elts.push_back(V); 4387193323Sed } while (EatIfPresent(lltok::comma)); 4388193323Sed 4389193323Sed return false; 4390193323Sed} 4391