LLParser.cpp revision 203954
1167514Skmacy//===-- LLParser.cpp - Parser Class ---------------------------------------===// 2167514Skmacy// 3167514Skmacy// The LLVM Compiler Infrastructure 4167514Skmacy// 5167514Skmacy// This file is distributed under the University of Illinois Open Source 6167514Skmacy// License. See LICENSE.TXT for details. 7167514Skmacy// 8167514Skmacy//===----------------------------------------------------------------------===// 9167514Skmacy// 10167514Skmacy// This file defines the parser class for .ll files. 11167514Skmacy// 12170076Skmacy//===----------------------------------------------------------------------===// 13167514Skmacy 14167514Skmacy#include "LLParser.h" 15167514Skmacy#include "llvm/AutoUpgrade.h" 16167514Skmacy#include "llvm/CallingConv.h" 17167514Skmacy#include "llvm/Constants.h" 18167514Skmacy#include "llvm/DerivedTypes.h" 19167514Skmacy#include "llvm/InlineAsm.h" 20167514Skmacy#include "llvm/Instructions.h" 21167514Skmacy#include "llvm/Module.h" 22167514Skmacy#include "llvm/Operator.h" 23167514Skmacy#include "llvm/ValueSymbolTable.h" 24167514Skmacy#include "llvm/ADT/SmallPtrSet.h" 25167514Skmacy#include "llvm/ADT/StringExtras.h" 26167514Skmacy#include "llvm/Support/ErrorHandling.h" 27167514Skmacy#include "llvm/Support/raw_ostream.h" 28167514Skmacyusing namespace llvm; 29167514Skmacy 30167514Skmacy/// Run: module ::= toplevelentity* 31167514Skmacybool LLParser::Run() { 32167514Skmacy // Prime the lexer. 33167514Skmacy Lex.Lex(); 34167514Skmacy 35167514Skmacy return ParseTopLevelEntities() || 36167514Skmacy ValidateEndOfModule(); 37167514Skmacy} 38176472Skmacy 39176472Skmacy/// ValidateEndOfModule - Do final validity and sanity checks at the end of the 40176472Skmacy/// module. 41176472Skmacybool LLParser::ValidateEndOfModule() { 42176472Skmacy // Update auto-upgraded malloc calls to "malloc". 43176472Skmacy // FIXME: Remove in LLVM 3.0. 44176472Skmacy if (MallocF) { 45176472Skmacy MallocF->setName("malloc"); 46176472Skmacy // If setName() does not set the name to "malloc", then there is already a 47176472Skmacy // declaration of "malloc". In that case, iterate over all calls to MallocF 48176472Skmacy // and get them to call the declared "malloc" instead. 49176472Skmacy if (MallocF->getName() != "malloc") { 50176472Skmacy Constant *RealMallocF = M->getFunction("malloc"); 51176472Skmacy if (RealMallocF->getType() != MallocF->getType()) 52176472Skmacy RealMallocF = ConstantExpr::getBitCast(RealMallocF, MallocF->getType()); 53176472Skmacy MallocF->replaceAllUsesWith(RealMallocF); 54176472Skmacy MallocF->eraseFromParent(); 55176472Skmacy MallocF = NULL; 56176472Skmacy } 57176472Skmacy } 58176472Skmacy 59176472Skmacy 60176472Skmacy // If there are entries in ForwardRefBlockAddresses at this point, they are 61176472Skmacy // references after the function was defined. Resolve those now. 62176472Skmacy while (!ForwardRefBlockAddresses.empty()) { 63176472Skmacy // Okay, we are referencing an already-parsed function, resolve them now. 64176472Skmacy Function *TheFn = 0; 65176472Skmacy const ValID &Fn = ForwardRefBlockAddresses.begin()->first; 66176472Skmacy if (Fn.Kind == ValID::t_GlobalName) 67176472Skmacy TheFn = M->getFunction(Fn.StrVal); 68176472Skmacy else if (Fn.UIntVal < NumberedVals.size()) 69176472Skmacy TheFn = dyn_cast<Function>(NumberedVals[Fn.UIntVal]); 70167514Skmacy 71167514Skmacy if (TheFn == 0) 72167514Skmacy return Error(Fn.Loc, "unknown function referenced by blockaddress"); 73167514Skmacy 74167514Skmacy // Resolve all these references. 75167514Skmacy if (ResolveForwardRefBlockAddresses(TheFn, 76167514Skmacy ForwardRefBlockAddresses.begin()->second, 77167514Skmacy 0)) 78167514Skmacy return true; 79167514Skmacy 80167514Skmacy ForwardRefBlockAddresses.erase(ForwardRefBlockAddresses.begin()); 81167514Skmacy } 82167514Skmacy 83167514Skmacy 84167514Skmacy if (!ForwardRefTypes.empty()) 85167514Skmacy return Error(ForwardRefTypes.begin()->second.second, 86167514Skmacy "use of undefined type named '" + 87167514Skmacy ForwardRefTypes.begin()->first + "'"); 88167514Skmacy if (!ForwardRefTypeIDs.empty()) 89167514Skmacy return Error(ForwardRefTypeIDs.begin()->second.second, 90167514Skmacy "use of undefined type '%" + 91167514Skmacy utostr(ForwardRefTypeIDs.begin()->first) + "'"); 92167514Skmacy 93167514Skmacy if (!ForwardRefVals.empty()) 94167514Skmacy return Error(ForwardRefVals.begin()->second.second, 95167514Skmacy "use of undefined value '@" + ForwardRefVals.begin()->first + 96167514Skmacy "'"); 97167514Skmacy 98167514Skmacy if (!ForwardRefValIDs.empty()) 99167514Skmacy return Error(ForwardRefValIDs.begin()->second.second, 100167514Skmacy "use of undefined value '@" + 101167514Skmacy utostr(ForwardRefValIDs.begin()->first) + "'"); 102167514Skmacy 103167514Skmacy if (!ForwardRefMDNodes.empty()) 104167514Skmacy return Error(ForwardRefMDNodes.begin()->second.second, 105167514Skmacy "use of undefined metadata '!" + 106167514Skmacy utostr(ForwardRefMDNodes.begin()->first) + "'"); 107167514Skmacy 108167514Skmacy 109167514Skmacy // Look for intrinsic functions and CallInst that need to be upgraded 110167514Skmacy for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ) 111167514Skmacy UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove 112167514Skmacy 113167514Skmacy // Check debug info intrinsics. 114167514Skmacy CheckDebugInfoIntrinsics(M); 115167514Skmacy return false; 116167514Skmacy} 117167514Skmacy 118167514Skmacybool LLParser::ResolveForwardRefBlockAddresses(Function *TheFn, 119167514Skmacy std::vector<std::pair<ValID, GlobalValue*> > &Refs, 120167514Skmacy PerFunctionState *PFS) { 121167514Skmacy // Loop over all the references, resolving them. 122167514Skmacy for (unsigned i = 0, e = Refs.size(); i != e; ++i) { 123167514Skmacy BasicBlock *Res; 124167514Skmacy if (PFS) { 125167514Skmacy if (Refs[i].first.Kind == ValID::t_LocalName) 126167514Skmacy Res = PFS->GetBB(Refs[i].first.StrVal, Refs[i].first.Loc); 127167514Skmacy else 128167514Skmacy Res = PFS->GetBB(Refs[i].first.UIntVal, Refs[i].first.Loc); 129167514Skmacy } else if (Refs[i].first.Kind == ValID::t_LocalID) { 130167514Skmacy return Error(Refs[i].first.Loc, 131167514Skmacy "cannot take address of numeric label after the function is defined"); 132167514Skmacy } else { 133167514Skmacy Res = dyn_cast_or_null<BasicBlock>( 134167514Skmacy TheFn->getValueSymbolTable().lookup(Refs[i].first.StrVal)); 135167514Skmacy } 136167514Skmacy 137167514Skmacy if (Res == 0) 138167514Skmacy return Error(Refs[i].first.Loc, 139167514Skmacy "referenced value is not a basic block"); 140167514Skmacy 141167514Skmacy // Get the BlockAddress for this and update references to use it. 142167514Skmacy BlockAddress *BA = BlockAddress::get(TheFn, Res); 143167514Skmacy Refs[i].second->replaceAllUsesWith(BA); 144167514Skmacy Refs[i].second->eraseFromParent(); 145167514Skmacy } 146167514Skmacy return false; 147167514Skmacy} 148167514Skmacy 149167514Skmacy 150167514Skmacy//===----------------------------------------------------------------------===// 151167514Skmacy// Top-Level Entities 152167514Skmacy//===----------------------------------------------------------------------===// 153167514Skmacy 154167514Skmacybool LLParser::ParseTopLevelEntities() { 155167514Skmacy while (1) { 156167514Skmacy switch (Lex.getKind()) { 157167514Skmacy default: return TokError("expected top-level entity"); 158167514Skmacy case lltok::Eof: return false; 159167514Skmacy //case lltok::kw_define: 160167514Skmacy case lltok::kw_declare: if (ParseDeclare()) return true; break; 161167514Skmacy case lltok::kw_define: if (ParseDefine()) return true; break; 162167514Skmacy case lltok::kw_module: if (ParseModuleAsm()) return true; break; 163167514Skmacy case lltok::kw_target: if (ParseTargetDefinition()) return true; break; 164167514Skmacy case lltok::kw_deplibs: if (ParseDepLibs()) return true; break; 165167514Skmacy case lltok::kw_type: if (ParseUnnamedType()) return true; break; 166167514Skmacy case lltok::LocalVarID: if (ParseUnnamedType()) return true; break; 167167514Skmacy case lltok::StringConstant: // FIXME: REMOVE IN LLVM 3.0 168167514Skmacy case lltok::LocalVar: if (ParseNamedType()) return true; break; 169167514Skmacy case lltok::GlobalID: if (ParseUnnamedGlobal()) return true; break; 170167514Skmacy case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break; 171167514Skmacy case lltok::exclaim: if (ParseStandaloneMetadata()) return true; break; 172167514Skmacy case lltok::MetadataVar: if (ParseNamedMetadata()) return true; break; 173167514Skmacy 174167514Skmacy // The Global variable production with no name can have many different 175167514Skmacy // optional leading prefixes, the production is: 176167514Skmacy // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalThreadLocal 177167514Skmacy // OptionalAddrSpace ('constant'|'global') ... 178167514Skmacy case lltok::kw_private : // OptionalLinkage 179167514Skmacy case lltok::kw_linker_private: // OptionalLinkage 180167514Skmacy case lltok::kw_internal: // OptionalLinkage 181167514Skmacy case lltok::kw_weak: // OptionalLinkage 182167514Skmacy case lltok::kw_weak_odr: // OptionalLinkage 183167514Skmacy case lltok::kw_linkonce: // OptionalLinkage 184167514Skmacy case lltok::kw_linkonce_odr: // OptionalLinkage 185167514Skmacy case lltok::kw_appending: // OptionalLinkage 186167514Skmacy case lltok::kw_dllexport: // OptionalLinkage 187167514Skmacy case lltok::kw_common: // OptionalLinkage 188167514Skmacy case lltok::kw_dllimport: // OptionalLinkage 189167514Skmacy case lltok::kw_extern_weak: // OptionalLinkage 190167514Skmacy case lltok::kw_external: { // OptionalLinkage 191167514Skmacy unsigned Linkage, Visibility; 192167514Skmacy if (ParseOptionalLinkage(Linkage) || 193167514Skmacy ParseOptionalVisibility(Visibility) || 194167514Skmacy ParseGlobal("", SMLoc(), Linkage, true, Visibility)) 195167514Skmacy return true; 196167514Skmacy break; 197167514Skmacy } 198167514Skmacy case lltok::kw_default: // OptionalVisibility 199167514Skmacy case lltok::kw_hidden: // OptionalVisibility 200167514Skmacy case lltok::kw_protected: { // OptionalVisibility 201167514Skmacy unsigned Visibility; 202167514Skmacy if (ParseOptionalVisibility(Visibility) || 203167514Skmacy ParseGlobal("", SMLoc(), 0, false, Visibility)) 204167514Skmacy return true; 205167514Skmacy break; 206167514Skmacy } 207167514Skmacy 208167514Skmacy case lltok::kw_thread_local: // OptionalThreadLocal 209167514Skmacy case lltok::kw_addrspace: // OptionalAddrSpace 210167514Skmacy case lltok::kw_constant: // GlobalType 211167514Skmacy case lltok::kw_global: // GlobalType 212167514Skmacy if (ParseGlobal("", SMLoc(), 0, false, 0)) return true; 213167514Skmacy break; 214167514Skmacy } 215167514Skmacy } 216167514Skmacy} 217167514Skmacy 218167514Skmacy 219167514Skmacy/// toplevelentity 220167514Skmacy/// ::= 'module' 'asm' STRINGCONSTANT 221167514Skmacybool LLParser::ParseModuleAsm() { 222167514Skmacy assert(Lex.getKind() == lltok::kw_module); 223167514Skmacy Lex.Lex(); 224167514Skmacy 225167514Skmacy std::string AsmStr; 226167514Skmacy if (ParseToken(lltok::kw_asm, "expected 'module asm'") || 227167514Skmacy ParseStringConstant(AsmStr)) return true; 228167514Skmacy 229167514Skmacy const std::string &AsmSoFar = M->getModuleInlineAsm(); 230167514Skmacy if (AsmSoFar.empty()) 231167514Skmacy M->setModuleInlineAsm(AsmStr); 232167514Skmacy else 233167514Skmacy M->setModuleInlineAsm(AsmSoFar+"\n"+AsmStr); 234167514Skmacy return false; 235167514Skmacy} 236167514Skmacy 237167514Skmacy/// toplevelentity 238167514Skmacy/// ::= 'target' 'triple' '=' STRINGCONSTANT 239167514Skmacy/// ::= 'target' 'datalayout' '=' STRINGCONSTANT 240167514Skmacybool LLParser::ParseTargetDefinition() { 241167514Skmacy assert(Lex.getKind() == lltok::kw_target); 242167514Skmacy std::string Str; 243167514Skmacy switch (Lex.Lex()) { 244167514Skmacy default: return TokError("unknown target property"); 245167514Skmacy case lltok::kw_triple: 246167514Skmacy Lex.Lex(); 247167514Skmacy if (ParseToken(lltok::equal, "expected '=' after target triple") || 248167514Skmacy ParseStringConstant(Str)) 249167514Skmacy return true; 250167514Skmacy M->setTargetTriple(Str); 251167514Skmacy return false; 252167514Skmacy case lltok::kw_datalayout: 253167514Skmacy Lex.Lex(); 254167514Skmacy if (ParseToken(lltok::equal, "expected '=' after target datalayout") || 255167514Skmacy ParseStringConstant(Str)) 256167514Skmacy return true; 257167514Skmacy M->setDataLayout(Str); 258167514Skmacy return false; 259167514Skmacy } 260167514Skmacy} 261167514Skmacy 262167514Skmacy/// toplevelentity 263167514Skmacy/// ::= 'deplibs' '=' '[' ']' 264167514Skmacy/// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']' 265167514Skmacybool LLParser::ParseDepLibs() { 266167514Skmacy assert(Lex.getKind() == lltok::kw_deplibs); 267167514Skmacy Lex.Lex(); 268167514Skmacy if (ParseToken(lltok::equal, "expected '=' after deplibs") || 269167514Skmacy ParseToken(lltok::lsquare, "expected '=' after deplibs")) 270167514Skmacy return true; 271167514Skmacy 272167514Skmacy if (EatIfPresent(lltok::rsquare)) 273167514Skmacy return false; 274167514Skmacy 275167514Skmacy std::string Str; 276167514Skmacy if (ParseStringConstant(Str)) return true; 277167514Skmacy M->addLibrary(Str); 278167514Skmacy 279167514Skmacy while (EatIfPresent(lltok::comma)) { 280167514Skmacy if (ParseStringConstant(Str)) return true; 281167514Skmacy M->addLibrary(Str); 282167514Skmacy } 283197791Snp 284197791Snp return ParseToken(lltok::rsquare, "expected ']' at end of list"); 285197791Snp} 286197791Snp 287197791Snp/// ParseUnnamedType: 288167514Skmacy/// ::= 'type' type 289167514Skmacy/// ::= LocalVarID '=' 'type' type 290167514Skmacybool LLParser::ParseUnnamedType() { 291167514Skmacy unsigned TypeID = NumberedTypes.size(); 292167514Skmacy 293167514Skmacy // Handle the LocalVarID form. 294167514Skmacy if (Lex.getKind() == lltok::LocalVarID) { 295167514Skmacy if (Lex.getUIntVal() != TypeID) 296167514Skmacy return Error(Lex.getLoc(), "type expected to be numbered '%" + 297167514Skmacy utostr(TypeID) + "'"); 298167514Skmacy Lex.Lex(); // eat LocalVarID; 299167514Skmacy 300167514Skmacy if (ParseToken(lltok::equal, "expected '=' after name")) 301167514Skmacy return true; 302167514Skmacy } 303167514Skmacy 304167514Skmacy assert(Lex.getKind() == lltok::kw_type); 305167514Skmacy LocTy TypeLoc = Lex.getLoc(); 306167514Skmacy Lex.Lex(); // eat kw_type 307167514Skmacy 308167514Skmacy PATypeHolder Ty(Type::getVoidTy(Context)); 309167514Skmacy if (ParseType(Ty)) return true; 310167514Skmacy 311167514Skmacy // See if this type was previously referenced. 312167514Skmacy std::map<unsigned, std::pair<PATypeHolder, LocTy> >::iterator 313167514Skmacy FI = ForwardRefTypeIDs.find(TypeID); 314167514Skmacy if (FI != ForwardRefTypeIDs.end()) { 315167514Skmacy if (FI->second.first.get() == Ty) 316167514Skmacy return Error(TypeLoc, "self referential type is invalid"); 317167514Skmacy 318167514Skmacy cast<DerivedType>(FI->second.first.get())->refineAbstractTypeTo(Ty); 319167514Skmacy Ty = FI->second.first.get(); 320167514Skmacy ForwardRefTypeIDs.erase(FI); 321167514Skmacy } 322167514Skmacy 323167514Skmacy NumberedTypes.push_back(Ty); 324167514Skmacy 325167514Skmacy return false; 326167514Skmacy} 327167514Skmacy 328167514Skmacy/// toplevelentity 329167514Skmacy/// ::= LocalVar '=' 'type' type 330167514Skmacybool LLParser::ParseNamedType() { 331167514Skmacy std::string Name = Lex.getStrVal(); 332167514Skmacy LocTy NameLoc = Lex.getLoc(); 333167514Skmacy Lex.Lex(); // eat LocalVar. 334167514Skmacy 335167514Skmacy PATypeHolder Ty(Type::getVoidTy(Context)); 336167514Skmacy 337167514Skmacy if (ParseToken(lltok::equal, "expected '=' after name") || 338167514Skmacy ParseToken(lltok::kw_type, "expected 'type' after name") || 339167514Skmacy ParseType(Ty)) 340167514Skmacy return true; 341167514Skmacy 342167514Skmacy // Set the type name, checking for conflicts as we do so. 343167514Skmacy bool AlreadyExists = M->addTypeName(Name, Ty); 344167514Skmacy if (!AlreadyExists) return false; 345167514Skmacy 346167514Skmacy // See if this type is a forward reference. We need to eagerly resolve 347167514Skmacy // types to allow recursive type redefinitions below. 348167514Skmacy std::map<std::string, std::pair<PATypeHolder, LocTy> >::iterator 349167514Skmacy FI = ForwardRefTypes.find(Name); 350167514Skmacy if (FI != ForwardRefTypes.end()) { 351167514Skmacy if (FI->second.first.get() == Ty) 352167514Skmacy return Error(NameLoc, "self referential type is invalid"); 353167514Skmacy 354167514Skmacy cast<DerivedType>(FI->second.first.get())->refineAbstractTypeTo(Ty); 355167514Skmacy Ty = FI->second.first.get(); 356167514Skmacy ForwardRefTypes.erase(FI); 357167514Skmacy } 358167514Skmacy 359167514Skmacy // Inserting a name that is already defined, get the existing name. 360167514Skmacy const Type *Existing = M->getTypeByName(Name); 361167514Skmacy assert(Existing && "Conflict but no matching type?!"); 362167514Skmacy 363167514Skmacy // Otherwise, this is an attempt to redefine a type. That's okay if 364167514Skmacy // the redefinition is identical to the original. 365167514Skmacy // FIXME: REMOVE REDEFINITIONS IN LLVM 3.0 366167514Skmacy if (Existing == Ty) return false; 367167514Skmacy 368167514Skmacy // Any other kind of (non-equivalent) redefinition is an error. 369167514Skmacy return Error(NameLoc, "redefinition of type named '" + Name + "' of type '" + 370167514Skmacy Ty->getDescription() + "'"); 371167514Skmacy} 372167514Skmacy 373167514Skmacy 374167514Skmacy/// toplevelentity 375167514Skmacy/// ::= 'declare' FunctionHeader 376167514Skmacybool LLParser::ParseDeclare() { 377167514Skmacy assert(Lex.getKind() == lltok::kw_declare); 378167514Skmacy Lex.Lex(); 379167514Skmacy 380167514Skmacy Function *F; 381167514Skmacy return ParseFunctionHeader(F, false); 382167514Skmacy} 383167514Skmacy 384197791Snp/// toplevelentity 385197791Snp/// ::= 'define' FunctionHeader '{' ... 386197791Snpbool LLParser::ParseDefine() { 387197791Snp assert(Lex.getKind() == lltok::kw_define); 388197791Snp Lex.Lex(); 389167514Skmacy 390167514Skmacy Function *F; 391167514Skmacy return ParseFunctionHeader(F, true) || 392167514Skmacy ParseFunctionBody(*F); 393167514Skmacy} 394167514Skmacy 395167514Skmacy/// ParseGlobalType 396167514Skmacy/// ::= 'constant' 397167514Skmacy/// ::= 'global' 398167514Skmacybool LLParser::ParseGlobalType(bool &IsConstant) { 399167514Skmacy if (Lex.getKind() == lltok::kw_constant) 400167514Skmacy IsConstant = true; 401167514Skmacy else if (Lex.getKind() == lltok::kw_global) 402167514Skmacy IsConstant = false; 403167514Skmacy else { 404167514Skmacy IsConstant = false; 405167514Skmacy return TokError("expected 'global' or 'constant'"); 406176472Skmacy } 407176472Skmacy Lex.Lex(); 408176472Skmacy return false; 409176472Skmacy} 410176472Skmacy 411167514Skmacy/// ParseUnnamedGlobal: 412167514Skmacy/// OptionalVisibility ALIAS ... 413167514Skmacy/// OptionalLinkage OptionalVisibility ... -> global variable 414167514Skmacy/// GlobalID '=' OptionalVisibility ALIAS ... 415167514Skmacy/// GlobalID '=' OptionalLinkage OptionalVisibility ... -> global variable 416167514Skmacybool LLParser::ParseUnnamedGlobal() { 417167514Skmacy unsigned VarID = NumberedVals.size(); 418167514Skmacy std::string Name; 419167514Skmacy LocTy NameLoc = Lex.getLoc(); 420167514Skmacy 421167514Skmacy // Handle the GlobalID form. 422167514Skmacy if (Lex.getKind() == lltok::GlobalID) { 423167514Skmacy if (Lex.getUIntVal() != VarID) 424167514Skmacy return Error(Lex.getLoc(), "variable expected to be numbered '%" + 425167514Skmacy utostr(VarID) + "'"); 426167514Skmacy Lex.Lex(); // eat GlobalID; 427167514Skmacy 428167514Skmacy if (ParseToken(lltok::equal, "expected '=' after name")) 429167514Skmacy return true; 430167514Skmacy } 431167514Skmacy 432176472Skmacy bool HasLinkage; 433176472Skmacy unsigned Linkage, Visibility; 434176472Skmacy if (ParseOptionalLinkage(Linkage, HasLinkage) || 435176472Skmacy ParseOptionalVisibility(Visibility)) 436176472Skmacy return true; 437176472Skmacy 438176472Skmacy if (HasLinkage || Lex.getKind() != lltok::kw_alias) 439176472Skmacy return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility); 440176472Skmacy return ParseAlias(Name, NameLoc, Visibility); 441176472Skmacy} 442176472Skmacy 443176472Skmacy/// ParseNamedGlobal: 444176472Skmacy/// GlobalVar '=' OptionalVisibility ALIAS ... 445176472Skmacy/// GlobalVar '=' OptionalLinkage OptionalVisibility ... -> global variable 446176472Skmacybool LLParser::ParseNamedGlobal() { 447176472Skmacy assert(Lex.getKind() == lltok::GlobalVar); 448176472Skmacy LocTy NameLoc = Lex.getLoc(); 449176472Skmacy std::string Name = Lex.getStrVal(); 450176472Skmacy Lex.Lex(); 451176472Skmacy 452176472Skmacy bool HasLinkage; 453176472Skmacy unsigned Linkage, Visibility; 454176472Skmacy if (ParseToken(lltok::equal, "expected '=' in global variable") || 455176472Skmacy ParseOptionalLinkage(Linkage, HasLinkage) || 456176472Skmacy ParseOptionalVisibility(Visibility)) 457176472Skmacy return true; 458176472Skmacy 459176472Skmacy if (HasLinkage || Lex.getKind() != lltok::kw_alias) 460176472Skmacy return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility); 461176472Skmacy return ParseAlias(Name, NameLoc, Visibility); 462176472Skmacy} 463176472Skmacy 464176472Skmacy// MDString: 465176472Skmacy// ::= '!' STRINGCONSTANT 466176472Skmacybool LLParser::ParseMDString(MDString *&Result) { 467176472Skmacy std::string Str; 468176472Skmacy if (ParseStringConstant(Str)) return true; 469176472Skmacy Result = MDString::get(Context, Str); 470176472Skmacy return false; 471176472Skmacy} 472176472Skmacy 473176472Skmacy// MDNode: 474176472Skmacy// ::= '!' MDNodeNumber 475176472Skmacybool LLParser::ParseMDNodeID(MDNode *&Result) { 476176472Skmacy // !{ ..., !42, ... } 477176472Skmacy unsigned MID = 0; 478176472Skmacy if (ParseUInt32(MID)) return true; 479176472Skmacy 480176472Skmacy // Check existing MDNode. 481176472Skmacy if (MID < NumberedMetadata.size() && NumberedMetadata[MID] != 0) { 482167514Skmacy Result = NumberedMetadata[MID]; 483167514Skmacy return false; 484167514Skmacy } 485167514Skmacy 486167514Skmacy // Create MDNode forward reference. 487167514Skmacy 488167514Skmacy // FIXME: This is not unique enough! 489167514Skmacy std::string FwdRefName = "llvm.mdnode.fwdref." + utostr(MID); 490167514Skmacy Value *V = MDString::get(Context, FwdRefName); 491167514Skmacy MDNode *FwdNode = MDNode::get(Context, &V, 1); 492167514Skmacy ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc()); 493167514Skmacy 494167514Skmacy if (NumberedMetadata.size() <= MID) 495167514Skmacy NumberedMetadata.resize(MID+1); 496167514Skmacy NumberedMetadata[MID] = FwdNode; 497167514Skmacy Result = FwdNode; 498167514Skmacy return false; 499167514Skmacy} 500167514Skmacy 501167514Skmacy/// ParseNamedMetadata: 502167514Skmacy/// !foo = !{ !1, !2 } 503167514Skmacybool LLParser::ParseNamedMetadata() { 504167514Skmacy assert(Lex.getKind() == lltok::MetadataVar); 505167514Skmacy std::string Name = Lex.getStrVal(); 506167514Skmacy Lex.Lex(); 507167514Skmacy 508167514Skmacy if (ParseToken(lltok::equal, "expected '=' here") || 509167514Skmacy ParseToken(lltok::exclaim, "Expected '!' here") || 510167514Skmacy ParseToken(lltok::lbrace, "Expected '{' here")) 511167514Skmacy return true; 512167514Skmacy 513167514Skmacy SmallVector<MDNode *, 8> Elts; 514167514Skmacy do { 515167514Skmacy // Null is a special case since it is typeless. 516167514Skmacy if (EatIfPresent(lltok::kw_null)) { 517167514Skmacy Elts.push_back(0); 518167514Skmacy continue; 519167514Skmacy } 520167514Skmacy 521167514Skmacy if (ParseToken(lltok::exclaim, "Expected '!' here")) 522167514Skmacy return true; 523167514Skmacy 524167514Skmacy MDNode *N = 0; 525167514Skmacy if (ParseMDNodeID(N)) return true; 526167514Skmacy Elts.push_back(N); 527167514Skmacy } while (EatIfPresent(lltok::comma)); 528167514Skmacy 529167514Skmacy if (ParseToken(lltok::rbrace, "expected end of metadata node")) 530167514Skmacy return true; 531167514Skmacy 532167514Skmacy NamedMDNode::Create(Context, Name, Elts.data(), Elts.size(), M); 533167514Skmacy return false; 534167514Skmacy} 535167514Skmacy 536167514Skmacy/// ParseStandaloneMetadata: 537167514Skmacy/// !42 = !{...} 538167514Skmacybool LLParser::ParseStandaloneMetadata() { 539167514Skmacy assert(Lex.getKind() == lltok::exclaim); 540167514Skmacy Lex.Lex(); 541167514Skmacy unsigned MetadataID = 0; 542167514Skmacy 543167514Skmacy LocTy TyLoc; 544167514Skmacy PATypeHolder Ty(Type::getVoidTy(Context)); 545167514Skmacy SmallVector<Value *, 16> Elts; 546167514Skmacy if (ParseUInt32(MetadataID) || 547167514Skmacy ParseToken(lltok::equal, "expected '=' here") || 548167514Skmacy ParseType(Ty, TyLoc) || 549167514Skmacy ParseToken(lltok::exclaim, "Expected '!' here") || 550167514Skmacy ParseToken(lltok::lbrace, "Expected '{' here") || 551167514Skmacy ParseMDNodeVector(Elts, NULL) || 552167514Skmacy ParseToken(lltok::rbrace, "expected end of metadata node")) 553167514Skmacy return true; 554167514Skmacy 555167514Skmacy MDNode *Init = MDNode::get(Context, Elts.data(), Elts.size()); 556167514Skmacy 557167514Skmacy // See if this was forward referenced, if so, handle it. 558167514Skmacy std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> >::iterator 559167514Skmacy FI = ForwardRefMDNodes.find(MetadataID); 560167514Skmacy if (FI != ForwardRefMDNodes.end()) { 561167514Skmacy FI->second.first->replaceAllUsesWith(Init); 562167514Skmacy ForwardRefMDNodes.erase(FI); 563167514Skmacy 564167514Skmacy assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work"); 565167514Skmacy } else { 566167514Skmacy if (MetadataID >= NumberedMetadata.size()) 567167514Skmacy NumberedMetadata.resize(MetadataID+1); 568167514Skmacy 569167514Skmacy if (NumberedMetadata[MetadataID] != 0) 570167514Skmacy return TokError("Metadata id is already used"); 571167514Skmacy NumberedMetadata[MetadataID] = Init; 572167514Skmacy } 573167514Skmacy 574167514Skmacy return false; 575167514Skmacy} 576167514Skmacy 577167514Skmacy/// ParseAlias: 578167514Skmacy/// ::= GlobalVar '=' OptionalVisibility 'alias' OptionalLinkage Aliasee 579167514Skmacy/// Aliasee 580167514Skmacy/// ::= TypeAndValue 581167514Skmacy/// ::= 'bitcast' '(' TypeAndValue 'to' Type ')' 582167514Skmacy/// ::= 'getelementptr' 'inbounds'? '(' ... ')' 583167514Skmacy/// 584167514Skmacy/// Everything through visibility has already been parsed. 585167514Skmacy/// 586167514Skmacybool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc, 587167514Skmacy unsigned Visibility) { 588167514Skmacy assert(Lex.getKind() == lltok::kw_alias); 589167514Skmacy Lex.Lex(); 590167514Skmacy unsigned Linkage; 591167514Skmacy LocTy LinkageLoc = Lex.getLoc(); 592167514Skmacy if (ParseOptionalLinkage(Linkage)) 593167514Skmacy return true; 594167514Skmacy 595167514Skmacy if (Linkage != GlobalValue::ExternalLinkage && 596167514Skmacy Linkage != GlobalValue::WeakAnyLinkage && 597167514Skmacy Linkage != GlobalValue::WeakODRLinkage && 598167514Skmacy Linkage != GlobalValue::InternalLinkage && 599167514Skmacy Linkage != GlobalValue::PrivateLinkage && 600167514Skmacy Linkage != GlobalValue::LinkerPrivateLinkage) 601167514Skmacy return Error(LinkageLoc, "invalid linkage type for alias"); 602167514Skmacy 603167514Skmacy Constant *Aliasee; 604167514Skmacy LocTy AliaseeLoc = Lex.getLoc(); 605167514Skmacy if (Lex.getKind() != lltok::kw_bitcast && 606167514Skmacy Lex.getKind() != lltok::kw_getelementptr) { 607167514Skmacy if (ParseGlobalTypeAndValue(Aliasee)) return true; 608167514Skmacy } else { 609167514Skmacy // The bitcast dest type is not present, it is implied by the dest type. 610167514Skmacy ValID ID; 611167514Skmacy if (ParseValID(ID)) return true; 612167514Skmacy if (ID.Kind != ValID::t_Constant) 613167514Skmacy return Error(AliaseeLoc, "invalid aliasee"); 614167514Skmacy Aliasee = ID.ConstantVal; 615167514Skmacy } 616167514Skmacy 617167514Skmacy if (!isa<PointerType>(Aliasee->getType())) 618167514Skmacy return Error(AliaseeLoc, "alias must have pointer type"); 619167514Skmacy 620167514Skmacy // Okay, create the alias but do not insert it into the module yet. 621167514Skmacy GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), 622167514Skmacy (GlobalValue::LinkageTypes)Linkage, Name, 623167514Skmacy Aliasee); 624167514Skmacy GA->setVisibility((GlobalValue::VisibilityTypes)Visibility); 625167514Skmacy 626167514Skmacy // See if this value already exists in the symbol table. If so, it is either 627167514Skmacy // a redefinition or a definition of a forward reference. 628167514Skmacy if (GlobalValue *Val = M->getNamedValue(Name)) { 629167514Skmacy // See if this was a redefinition. If so, there is no entry in 630167514Skmacy // ForwardRefVals. 631167514Skmacy std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator 632167514Skmacy I = ForwardRefVals.find(Name); 633167514Skmacy if (I == ForwardRefVals.end()) 634167514Skmacy return Error(NameLoc, "redefinition of global named '@" + Name + "'"); 635167514Skmacy 636167514Skmacy // Otherwise, this was a definition of forward ref. Verify that types 637167514Skmacy // agree. 638167514Skmacy if (Val->getType() != GA->getType()) 639167514Skmacy return Error(NameLoc, 640167514Skmacy "forward reference and definition of alias have different types"); 641167514Skmacy 642167514Skmacy // If they agree, just RAUW the old value with the alias and remove the 643167514Skmacy // forward ref info. 644167514Skmacy Val->replaceAllUsesWith(GA); 645167514Skmacy Val->eraseFromParent(); 646167514Skmacy ForwardRefVals.erase(I); 647167514Skmacy } 648167514Skmacy 649167514Skmacy // Insert into the module, we know its name won't collide now. 650167514Skmacy M->getAliasList().push_back(GA); 651167514Skmacy assert(GA->getNameStr() == Name && "Should not be a name conflict!"); 652167514Skmacy 653167514Skmacy return false; 654167514Skmacy} 655167514Skmacy 656167514Skmacy/// ParseGlobal 657176472Skmacy/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalThreadLocal 658176472Skmacy/// OptionalAddrSpace GlobalType Type Const 659176472Skmacy/// ::= OptionalLinkage OptionalVisibility OptionalThreadLocal 660176472Skmacy/// OptionalAddrSpace GlobalType Type Const 661167514Skmacy/// 662167514Skmacy/// Everything through visibility has been parsed already. 663167514Skmacy/// 664167514Skmacybool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc, 665167514Skmacy unsigned Linkage, bool HasLinkage, 666167514Skmacy unsigned Visibility) { 667167514Skmacy unsigned AddrSpace; 668167514Skmacy bool ThreadLocal, IsConstant; 669167514Skmacy LocTy TyLoc; 670167514Skmacy 671167514Skmacy PATypeHolder Ty(Type::getVoidTy(Context)); 672167514Skmacy if (ParseOptionalToken(lltok::kw_thread_local, ThreadLocal) || 673167514Skmacy ParseOptionalAddrSpace(AddrSpace) || 674167514Skmacy ParseGlobalType(IsConstant) || 675167514Skmacy ParseType(Ty, TyLoc)) 676167514Skmacy return true; 677167514Skmacy 678167514Skmacy // If the linkage is specified and is external, then no initializer is 679167514Skmacy // present. 680167514Skmacy Constant *Init = 0; 681167514Skmacy if (!HasLinkage || (Linkage != GlobalValue::DLLImportLinkage && 682167514Skmacy Linkage != GlobalValue::ExternalWeakLinkage && 683167514Skmacy Linkage != GlobalValue::ExternalLinkage)) { 684167514Skmacy if (ParseGlobalValue(Ty, Init)) 685167514Skmacy return true; 686167514Skmacy } 687167514Skmacy 688167514Skmacy if (isa<FunctionType>(Ty) || Ty->isLabelTy()) 689167514Skmacy return Error(TyLoc, "invalid type for global variable"); 690167514Skmacy 691167514Skmacy GlobalVariable *GV = 0; 692167514Skmacy 693167514Skmacy // See if the global was forward referenced, if so, use the global. 694167514Skmacy if (!Name.empty()) { 695167514Skmacy if (GlobalValue *GVal = M->getNamedValue(Name)) { 696167514Skmacy if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal)) 697167514Skmacy return Error(NameLoc, "redefinition of global '@" + Name + "'"); 698167514Skmacy GV = cast<GlobalVariable>(GVal); 699167514Skmacy } 700167514Skmacy } else { 701167514Skmacy std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator 702167514Skmacy I = ForwardRefValIDs.find(NumberedVals.size()); 703167514Skmacy if (I != ForwardRefValIDs.end()) { 704167514Skmacy GV = cast<GlobalVariable>(I->second.first); 705167514Skmacy ForwardRefValIDs.erase(I); 706167514Skmacy } 707167514Skmacy } 708167514Skmacy 709167514Skmacy if (GV == 0) { 710167514Skmacy GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, 0, 711167514Skmacy Name, 0, false, AddrSpace); 712167514Skmacy } else { 713167514Skmacy if (GV->getType()->getElementType() != Ty) 714167514Skmacy return Error(TyLoc, 715167514Skmacy "forward reference and definition of global have different types"); 716167514Skmacy 717167514Skmacy // Move the forward-reference to the correct spot in the module. 718167514Skmacy M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV); 719167514Skmacy } 720167514Skmacy 721167514Skmacy if (Name.empty()) 722167514Skmacy NumberedVals.push_back(GV); 723167514Skmacy 724167514Skmacy // Set the parsed properties on the global. 725167514Skmacy if (Init) 726167514Skmacy GV->setInitializer(Init); 727167514Skmacy GV->setConstant(IsConstant); 728167514Skmacy GV->setLinkage((GlobalValue::LinkageTypes)Linkage); 729167514Skmacy GV->setVisibility((GlobalValue::VisibilityTypes)Visibility); 730167514Skmacy GV->setThreadLocal(ThreadLocal); 731167514Skmacy 732167514Skmacy // Parse attributes on the global. 733167514Skmacy while (Lex.getKind() == lltok::comma) { 734167514Skmacy Lex.Lex(); 735167514Skmacy 736167514Skmacy if (Lex.getKind() == lltok::kw_section) { 737167514Skmacy Lex.Lex(); 738167514Skmacy GV->setSection(Lex.getStrVal()); 739167514Skmacy if (ParseToken(lltok::StringConstant, "expected global section string")) 740167514Skmacy return true; 741167514Skmacy } else if (Lex.getKind() == lltok::kw_align) { 742167514Skmacy unsigned Alignment; 743167514Skmacy if (ParseOptionalAlignment(Alignment)) return true; 744167514Skmacy GV->setAlignment(Alignment); 745167514Skmacy } else { 746167514Skmacy TokError("unknown global variable property!"); 747167514Skmacy } 748167514Skmacy } 749167514Skmacy 750167514Skmacy return false; 751167514Skmacy} 752167514Skmacy 753167514Skmacy 754167514Skmacy//===----------------------------------------------------------------------===// 755167514Skmacy// GlobalValue Reference/Resolution Routines. 756167514Skmacy//===----------------------------------------------------------------------===// 757167514Skmacy 758167514Skmacy/// GetGlobalVal - Get a value with the specified name or ID, creating a 759167514Skmacy/// forward reference record if needed. This can return null if the value 760167514Skmacy/// exists but does not have the right type. 761167514SkmacyGlobalValue *LLParser::GetGlobalVal(const std::string &Name, const Type *Ty, 762167514Skmacy LocTy Loc) { 763167514Skmacy const PointerType *PTy = dyn_cast<PointerType>(Ty); 764167514Skmacy if (PTy == 0) { 765167514Skmacy Error(Loc, "global variable reference must have pointer type"); 766167514Skmacy return 0; 767167514Skmacy } 768167514Skmacy 769167514Skmacy // Look this name up in the normal function symbol table. 770167514Skmacy GlobalValue *Val = 771167514Skmacy cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name)); 772167514Skmacy 773167514Skmacy // If this is a forward reference for the value, see if we already created a 774167514Skmacy // forward ref record. 775167514Skmacy if (Val == 0) { 776167514Skmacy std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator 777167514Skmacy I = ForwardRefVals.find(Name); 778167514Skmacy if (I != ForwardRefVals.end()) 779167514Skmacy Val = I->second.first; 780167514Skmacy } 781167514Skmacy 782167514Skmacy // If we have the value in the symbol table or fwd-ref table, return it. 783167514Skmacy if (Val) { 784167514Skmacy if (Val->getType() == Ty) return Val; 785167514Skmacy Error(Loc, "'@" + Name + "' defined with type '" + 786167514Skmacy Val->getType()->getDescription() + "'"); 787167514Skmacy return 0; 788167514Skmacy } 789167514Skmacy 790167514Skmacy // Otherwise, create a new forward reference for this value and remember it. 791167514Skmacy GlobalValue *FwdVal; 792167514Skmacy if (const FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType())) { 793167514Skmacy // Function types can return opaque but functions can't. 794167514Skmacy if (isa<OpaqueType>(FT->getReturnType())) { 795167514Skmacy Error(Loc, "function may not return opaque type"); 796167514Skmacy return 0; 797167514Skmacy } 798167514Skmacy 799167514Skmacy FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M); 800176472Skmacy } else { 801176472Skmacy FwdVal = new GlobalVariable(*M, PTy->getElementType(), false, 802176472Skmacy GlobalValue::ExternalWeakLinkage, 0, Name); 803176472Skmacy } 804176472Skmacy 805176472Skmacy ForwardRefVals[Name] = std::make_pair(FwdVal, Loc); 806176472Skmacy return FwdVal; 807176472Skmacy} 808176472Skmacy 809176472SkmacyGlobalValue *LLParser::GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc) { 810176472Skmacy const PointerType *PTy = dyn_cast<PointerType>(Ty); 811176472Skmacy if (PTy == 0) { 812176472Skmacy Error(Loc, "global variable reference must have pointer type"); 813176472Skmacy return 0; 814176472Skmacy } 815176472Skmacy 816176472Skmacy GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0; 817176472Skmacy 818176472Skmacy // If this is a forward reference for the value, see if we already created a 819176472Skmacy // forward ref record. 820176472Skmacy if (Val == 0) { 821176472Skmacy std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator 822176472Skmacy I = ForwardRefValIDs.find(ID); 823176472Skmacy if (I != ForwardRefValIDs.end()) 824176472Skmacy Val = I->second.first; 825176472Skmacy } 826176472Skmacy 827176472Skmacy // If we have the value in the symbol table or fwd-ref table, return it. 828176472Skmacy if (Val) { 829176472Skmacy if (Val->getType() == Ty) return Val; 830176472Skmacy Error(Loc, "'@" + utostr(ID) + "' defined with type '" + 831176472Skmacy Val->getType()->getDescription() + "'"); 832176472Skmacy return 0; 833176472Skmacy } 834176472Skmacy 835176472Skmacy // Otherwise, create a new forward reference for this value and remember it. 836176472Skmacy GlobalValue *FwdVal; 837176472Skmacy if (const FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType())) { 838176472Skmacy // Function types can return opaque but functions can't. 839176472Skmacy if (isa<OpaqueType>(FT->getReturnType())) { 840176472Skmacy Error(Loc, "function may not return opaque type"); 841176472Skmacy return 0; 842176472Skmacy } 843176472Skmacy FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M); 844176472Skmacy } else { 845176472Skmacy FwdVal = new GlobalVariable(*M, PTy->getElementType(), false, 846176472Skmacy GlobalValue::ExternalWeakLinkage, 0, ""); 847176472Skmacy } 848176472Skmacy 849176472Skmacy ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc); 850176472Skmacy return FwdVal; 851176472Skmacy} 852176472Skmacy 853176472Skmacy 854176472Skmacy//===----------------------------------------------------------------------===// 855176472Skmacy// Helper Routines. 856176472Skmacy//===----------------------------------------------------------------------===// 857176472Skmacy 858176472Skmacy/// ParseToken - If the current token has the specified kind, eat it and return 859176472Skmacy/// success. Otherwise, emit the specified error and return failure. 860176472Skmacybool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) { 861176472Skmacy if (Lex.getKind() != T) 862176472Skmacy return TokError(ErrMsg); 863176472Skmacy Lex.Lex(); 864176472Skmacy return false; 865176472Skmacy} 866176472Skmacy 867176472Skmacy/// ParseStringConstant 868176472Skmacy/// ::= StringConstant 869176472Skmacybool LLParser::ParseStringConstant(std::string &Result) { 870176472Skmacy if (Lex.getKind() != lltok::StringConstant) 871176472Skmacy return TokError("expected string constant"); 872176472Skmacy Result = Lex.getStrVal(); 873176472Skmacy Lex.Lex(); 874176472Skmacy return false; 875176472Skmacy} 876176472Skmacy 877176472Skmacy/// ParseUInt32 878176472Skmacy/// ::= uint32 879176472Skmacybool LLParser::ParseUInt32(unsigned &Val) { 880176472Skmacy if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned()) 881176472Skmacy return TokError("expected integer"); 882176472Skmacy uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1); 883176472Skmacy if (Val64 != unsigned(Val64)) 884176472Skmacy return TokError("expected 32-bit integer (too large)"); 885176472Skmacy Val = Val64; 886176472Skmacy Lex.Lex(); 887176472Skmacy return false; 888176472Skmacy} 889176472Skmacy 890176472Skmacy 891176472Skmacy/// ParseOptionalAddrSpace 892176472Skmacy/// := /*empty*/ 893176472Skmacy/// := 'addrspace' '(' uint32 ')' 894176472Skmacybool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) { 895176472Skmacy AddrSpace = 0; 896176472Skmacy if (!EatIfPresent(lltok::kw_addrspace)) 897176472Skmacy return false; 898176472Skmacy return ParseToken(lltok::lparen, "expected '(' in address space") || 899176472Skmacy ParseUInt32(AddrSpace) || 900176472Skmacy ParseToken(lltok::rparen, "expected ')' in address space"); 901176472Skmacy} 902176472Skmacy 903176472Skmacy/// ParseOptionalAttrs - Parse a potentially empty attribute list. AttrKind 904176472Skmacy/// indicates what kind of attribute list this is: 0: function arg, 1: result, 905176472Skmacy/// 2: function attr. 906176472Skmacy/// 3: function arg after value: FIXME: REMOVE IN LLVM 3.0 907176472Skmacybool LLParser::ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind) { 908176472Skmacy Attrs = Attribute::None; 909176472Skmacy LocTy AttrLoc = Lex.getLoc(); 910176472Skmacy 911176472Skmacy while (1) { 912176472Skmacy switch (Lex.getKind()) { 913176472Skmacy case lltok::kw_sext: 914176472Skmacy case lltok::kw_zext: 915176472Skmacy // Treat these as signext/zeroext if they occur in the argument list after 916176472Skmacy // the value, as in "call i8 @foo(i8 10 sext)". If they occur before the 917176472Skmacy // value, as in "call i8 @foo(i8 sext (" then it is part of a constant 918176472Skmacy // expr. 919176472Skmacy // FIXME: REMOVE THIS IN LLVM 3.0 920176472Skmacy if (AttrKind == 3) { 921176472Skmacy if (Lex.getKind() == lltok::kw_sext) 922176472Skmacy Attrs |= Attribute::SExt; 923176472Skmacy else 924176472Skmacy Attrs |= Attribute::ZExt; 925176472Skmacy break; 926176472Skmacy } 927176472Skmacy // FALL THROUGH. 928176472Skmacy default: // End of attributes. 929176472Skmacy if (AttrKind != 2 && (Attrs & Attribute::FunctionOnly)) 930176472Skmacy return Error(AttrLoc, "invalid use of function-only attribute"); 931176472Skmacy 932176472Skmacy if (AttrKind != 0 && AttrKind != 3 && (Attrs & Attribute::ParameterOnly)) 933176472Skmacy return Error(AttrLoc, "invalid use of parameter-only attribute"); 934176472Skmacy 935176472Skmacy return false; 936176472Skmacy case lltok::kw_zeroext: Attrs |= Attribute::ZExt; break; 937176472Skmacy case lltok::kw_signext: Attrs |= Attribute::SExt; break; 938176472Skmacy case lltok::kw_inreg: Attrs |= Attribute::InReg; break; 939176472Skmacy case lltok::kw_sret: Attrs |= Attribute::StructRet; break; 940176472Skmacy case lltok::kw_noalias: Attrs |= Attribute::NoAlias; break; 941176472Skmacy case lltok::kw_nocapture: Attrs |= Attribute::NoCapture; break; 942176472Skmacy case lltok::kw_byval: Attrs |= Attribute::ByVal; break; 943167514Skmacy case lltok::kw_nest: Attrs |= Attribute::Nest; break; 944167514Skmacy 945167514Skmacy case lltok::kw_noreturn: Attrs |= Attribute::NoReturn; break; 946167514Skmacy case lltok::kw_nounwind: Attrs |= Attribute::NoUnwind; break; 947167514Skmacy case lltok::kw_noinline: Attrs |= Attribute::NoInline; break; 948176472Skmacy case lltok::kw_readnone: Attrs |= Attribute::ReadNone; break; 949167514Skmacy case lltok::kw_readonly: Attrs |= Attribute::ReadOnly; break; 950167514Skmacy case lltok::kw_inlinehint: Attrs |= Attribute::InlineHint; break; 951167514Skmacy case lltok::kw_alwaysinline: Attrs |= Attribute::AlwaysInline; break; 952167514Skmacy case lltok::kw_optsize: Attrs |= Attribute::OptimizeForSize; break; 953176472Skmacy case lltok::kw_ssp: Attrs |= Attribute::StackProtect; break; 954176472Skmacy case lltok::kw_sspreq: Attrs |= Attribute::StackProtectReq; break; 955176472Skmacy case lltok::kw_noredzone: Attrs |= Attribute::NoRedZone; break; 956176472Skmacy case lltok::kw_noimplicitfloat: Attrs |= Attribute::NoImplicitFloat; break; 957176472Skmacy case lltok::kw_naked: Attrs |= Attribute::Naked; break; 958176472Skmacy 959176472Skmacy case lltok::kw_alignstack: { 960176472Skmacy unsigned Alignment; 961176472Skmacy if (ParseOptionalStackAlignment(Alignment)) 962176472Skmacy return true; 963176472Skmacy Attrs |= Attribute::constructStackAlignmentFromInt(Alignment); 964176472Skmacy continue; 965176472Skmacy } 966176472Skmacy 967176472Skmacy case lltok::kw_align: { 968176472Skmacy unsigned Alignment; 969167514Skmacy if (ParseOptionalAlignment(Alignment)) 970167514Skmacy return true; 971167514Skmacy Attrs |= Attribute::constructAlignmentFromInt(Alignment); 972167514Skmacy continue; 973167514Skmacy } 974167514Skmacy 975167514Skmacy } 976167514Skmacy Lex.Lex(); 977167514Skmacy } 978167514Skmacy} 979167514Skmacy 980167514Skmacy/// ParseOptionalLinkage 981167514Skmacy/// ::= /*empty*/ 982167514Skmacy/// ::= 'private' 983167514Skmacy/// ::= 'linker_private' 984167514Skmacy/// ::= 'internal' 985167514Skmacy/// ::= 'weak' 986167514Skmacy/// ::= 'weak_odr' 987167514Skmacy/// ::= 'linkonce' 988167514Skmacy/// ::= 'linkonce_odr' 989167514Skmacy/// ::= 'appending' 990167514Skmacy/// ::= 'dllexport' 991167514Skmacy/// ::= 'common' 992167514Skmacy/// ::= 'dllimport' 993167514Skmacy/// ::= 'extern_weak' 994167514Skmacy/// ::= 'external' 995167514Skmacybool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) { 996167514Skmacy HasLinkage = false; 997167514Skmacy switch (Lex.getKind()) { 998167514Skmacy default: Res=GlobalValue::ExternalLinkage; return false; 999167514Skmacy case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break; 1000167514Skmacy case lltok::kw_linker_private: Res = GlobalValue::LinkerPrivateLinkage; break; 1001167514Skmacy case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break; 1002167514Skmacy case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break; 1003167514Skmacy case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break; 1004167514Skmacy case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break; 1005167514Skmacy case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break; 1006167514Skmacy case lltok::kw_available_externally: 1007167514Skmacy Res = GlobalValue::AvailableExternallyLinkage; 1008167514Skmacy break; 1009167514Skmacy case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break; 1010167514Skmacy case lltok::kw_dllexport: Res = GlobalValue::DLLExportLinkage; break; 1011167514Skmacy case lltok::kw_common: Res = GlobalValue::CommonLinkage; break; 1012167514Skmacy case lltok::kw_dllimport: Res = GlobalValue::DLLImportLinkage; break; 1013167514Skmacy case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break; 1014167514Skmacy case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break; 1015167514Skmacy } 1016167514Skmacy Lex.Lex(); 1017167514Skmacy HasLinkage = true; 1018167514Skmacy return false; 1019167514Skmacy} 1020167514Skmacy 1021167514Skmacy/// ParseOptionalVisibility 1022167514Skmacy/// ::= /*empty*/ 1023167514Skmacy/// ::= 'default' 1024167514Skmacy/// ::= 'hidden' 1025176472Skmacy/// ::= 'protected' 1026176472Skmacy/// 1027176472Skmacybool LLParser::ParseOptionalVisibility(unsigned &Res) { 1028176472Skmacy switch (Lex.getKind()) { 1029176472Skmacy default: Res = GlobalValue::DefaultVisibility; return false; 1030176472Skmacy case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break; 1031176472Skmacy case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break; 1032176472Skmacy case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break; 1033176472Skmacy } 1034176472Skmacy Lex.Lex(); 1035176472Skmacy return false; 1036176472Skmacy} 1037167514Skmacy 1038167514Skmacy/// ParseOptionalCallingConv 1039167514Skmacy/// ::= /*empty*/ 1040167514Skmacy/// ::= 'ccc' 1041167514Skmacy/// ::= 'fastcc' 1042167514Skmacy/// ::= 'coldcc' 1043167514Skmacy/// ::= 'x86_stdcallcc' 1044167514Skmacy/// ::= 'x86_fastcallcc' 1045167514Skmacy/// ::= 'arm_apcscc' 1046167514Skmacy/// ::= 'arm_aapcscc' 1047167514Skmacy/// ::= 'arm_aapcs_vfpcc' 1048167514Skmacy/// ::= 'msp430_intrcc' 1049167514Skmacy/// ::= 'cc' UINT 1050167514Skmacy/// 1051167514Skmacybool LLParser::ParseOptionalCallingConv(CallingConv::ID &CC) { 1052167514Skmacy switch (Lex.getKind()) { 1053167514Skmacy default: CC = CallingConv::C; return false; 1054167514Skmacy case lltok::kw_ccc: CC = CallingConv::C; break; 1055167514Skmacy case lltok::kw_fastcc: CC = CallingConv::Fast; break; 1056167514Skmacy case lltok::kw_coldcc: CC = CallingConv::Cold; break; 1057167514Skmacy case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break; 1058167514Skmacy case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break; 1059167514Skmacy case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break; 1060167514Skmacy case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break; 1061167514Skmacy case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break; 1062167514Skmacy case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break; 1063167514Skmacy case lltok::kw_cc: { 1064167514Skmacy unsigned ArbitraryCC; 1065167514Skmacy Lex.Lex(); 1066167514Skmacy if (ParseUInt32(ArbitraryCC)) { 1067167514Skmacy return true; 1068167514Skmacy } else 1069167514Skmacy CC = static_cast<CallingConv::ID>(ArbitraryCC); 1070167514Skmacy return false; 1071167514Skmacy } 1072167514Skmacy break; 1073167514Skmacy } 1074167514Skmacy 1075176472Skmacy Lex.Lex(); 1076167514Skmacy return false; 1077176472Skmacy} 1078176472Skmacy 1079176472Skmacy/// ParseInstructionMetadata 1080176472Skmacy/// ::= !dbg !42 (',' !dbg !57)* 1081167514Skmacybool LLParser:: 1082176472SkmacyParseInstructionMetadata(SmallVectorImpl<std::pair<unsigned, 1083176472Skmacy MDNode *> > &Result){ 1084176472Skmacy do { 1085176472Skmacy if (Lex.getKind() != lltok::MetadataVar) 1086167514Skmacy return TokError("expected metadata after comma"); 1087176472Skmacy 1088176472Skmacy std::string Name = Lex.getStrVal(); 1089176472Skmacy Lex.Lex(); 1090176472Skmacy 1091176472Skmacy MDNode *Node; 1092176472Skmacy if (ParseToken(lltok::exclaim, "expected '!' here") || 1093176472Skmacy ParseMDNodeID(Node)) 1094176472Skmacy return true; 1095176472Skmacy 1096176472Skmacy unsigned MDK = M->getMDKindID(Name.c_str()); 1097176472Skmacy Result.push_back(std::make_pair(MDK, Node)); 1098176472Skmacy 1099176472Skmacy // If this is the end of the list, we're done. 1100176472Skmacy } while (EatIfPresent(lltok::comma)); 1101176472Skmacy return false; 1102176472Skmacy} 1103176472Skmacy 1104176472Skmacy/// ParseOptionalAlignment 1105176472Skmacy/// ::= /* empty */ 1106176472Skmacy/// ::= 'align' 4 1107167514Skmacybool LLParser::ParseOptionalAlignment(unsigned &Alignment) { 1108167514Skmacy Alignment = 0; 1109167514Skmacy if (!EatIfPresent(lltok::kw_align)) 1110167514Skmacy return false; 1111167514Skmacy LocTy AlignLoc = Lex.getLoc(); 1112167514Skmacy if (ParseUInt32(Alignment)) return true; 1113167514Skmacy if (!isPowerOf2_32(Alignment)) 1114167514Skmacy return Error(AlignLoc, "alignment is not a power of two"); 1115167514Skmacy return false; 1116167514Skmacy} 1117167514Skmacy 1118167514Skmacy/// ParseOptionalCommaAlign 1119167514Skmacy/// ::= 1120176472Skmacy/// ::= ',' align 4 1121167514Skmacy/// 1122176472Skmacy/// This returns with AteExtraComma set to true if it ate an excess comma at the 1123176472Skmacy/// end. 1124176472Skmacybool LLParser::ParseOptionalCommaAlign(unsigned &Alignment, 1125176472Skmacy bool &AteExtraComma) { 1126176472Skmacy AteExtraComma = false; 1127176472Skmacy while (EatIfPresent(lltok::comma)) { 1128176472Skmacy // Metadata at the end is an early exit. 1129176472Skmacy if (Lex.getKind() == lltok::MetadataVar) { 1130176472Skmacy AteExtraComma = true; 1131176472Skmacy return false; 1132176472Skmacy } 1133176472Skmacy 1134176472Skmacy if (Lex.getKind() == lltok::kw_align) { 1135176472Skmacy if (ParseOptionalAlignment(Alignment)) return true; 1136176472Skmacy } else 1137176472Skmacy return true; 1138176472Skmacy } 1139176472Skmacy 1140176472Skmacy return false; 1141176472Skmacy} 1142176472Skmacy 1143176472Skmacy/// ParseOptionalStackAlignment 1144176472Skmacy/// ::= /* empty */ 1145176472Skmacy/// ::= 'alignstack' '(' 4 ')' 1146176472Skmacybool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) { 1147176472Skmacy Alignment = 0; 1148176472Skmacy if (!EatIfPresent(lltok::kw_alignstack)) 1149176472Skmacy return false; 1150176472Skmacy LocTy ParenLoc = Lex.getLoc(); 1151176472Skmacy if (!EatIfPresent(lltok::lparen)) 1152176472Skmacy return Error(ParenLoc, "expected '('"); 1153176472Skmacy LocTy AlignLoc = Lex.getLoc(); 1154176472Skmacy if (ParseUInt32(Alignment)) return true; 1155176472Skmacy ParenLoc = Lex.getLoc(); 1156176472Skmacy if (!EatIfPresent(lltok::rparen)) 1157176472Skmacy return Error(ParenLoc, "expected ')'"); 1158176472Skmacy if (!isPowerOf2_32(Alignment)) 1159176472Skmacy return Error(AlignLoc, "stack alignment is not a power of two"); 1160176472Skmacy return false; 1161176472Skmacy} 1162176472Skmacy 1163176472Skmacy/// ParseIndexList - This parses the index list for an insert/extractvalue 1164176472Skmacy/// instruction. This sets AteExtraComma in the case where we eat an extra 1165176472Skmacy/// comma at the end of the line and find that it is followed by metadata. 1166176472Skmacy/// Clients that don't allow metadata can call the version of this function that 1167176472Skmacy/// only takes one argument. 1168176472Skmacy/// 1169176472Skmacy/// ParseIndexList 1170176472Skmacy/// ::= (',' uint32)+ 1171176472Skmacy/// 1172176472Skmacybool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices, 1173176472Skmacy bool &AteExtraComma) { 1174176472Skmacy AteExtraComma = false; 1175176472Skmacy 1176176472Skmacy if (Lex.getKind() != lltok::comma) 1177176472Skmacy return TokError("expected ',' as start of index list"); 1178176472Skmacy 1179176472Skmacy while (EatIfPresent(lltok::comma)) { 1180176472Skmacy if (Lex.getKind() == lltok::MetadataVar) { 1181176472Skmacy AteExtraComma = true; 1182176472Skmacy return false; 1183176472Skmacy } 1184176472Skmacy unsigned Idx; 1185176472Skmacy if (ParseUInt32(Idx)) return true; 1186176472Skmacy Indices.push_back(Idx); 1187176472Skmacy } 1188176472Skmacy 1189176472Skmacy return false; 1190176472Skmacy} 1191167514Skmacy 1192167514Skmacy//===----------------------------------------------------------------------===// 1193167514Skmacy// Type Parsing. 1194167514Skmacy//===----------------------------------------------------------------------===// 1195167514Skmacy 1196167514Skmacy/// ParseType - Parse and resolve a full type. 1197167514Skmacybool LLParser::ParseType(PATypeHolder &Result, bool AllowVoid) { 1198167514Skmacy LocTy TypeLoc = Lex.getLoc(); 1199167514Skmacy if (ParseTypeRec(Result)) return true; 1200167514Skmacy 1201167514Skmacy // Verify no unresolved uprefs. 1202167514Skmacy if (!UpRefs.empty()) 1203167514Skmacy return Error(UpRefs.back().Loc, "invalid unresolved type up reference"); 1204167514Skmacy 1205167514Skmacy if (!AllowVoid && Result.get()->isVoidTy()) 1206167514Skmacy return Error(TypeLoc, "void type only allowed for function results"); 1207167514Skmacy 1208167514Skmacy return false; 1209167514Skmacy} 1210167514Skmacy 1211167514Skmacy/// HandleUpRefs - Every time we finish a new layer of types, this function is 1212167514Skmacy/// called. It loops through the UpRefs vector, which is a list of the 1213167514Skmacy/// currently active types. For each type, if the up-reference is contained in 1214167514Skmacy/// the newly completed type, we decrement the level count. When the level 1215167514Skmacy/// count reaches zero, the up-referenced type is the type that is passed in: 1216167514Skmacy/// thus we can complete the cycle. 1217167514Skmacy/// 1218167514SkmacyPATypeHolder LLParser::HandleUpRefs(const Type *ty) { 1219167514Skmacy // If Ty isn't abstract, or if there are no up-references in it, then there is 1220167514Skmacy // nothing to resolve here. 1221176472Skmacy if (!ty->isAbstract() || UpRefs.empty()) return ty; 1222176472Skmacy 1223176472Skmacy PATypeHolder Ty(ty); 1224176472Skmacy#if 0 1225176472Skmacy dbgs() << "Type '" << Ty->getDescription() 1226176472Skmacy << "' newly formed. Resolving upreferences.\n" 1227176472Skmacy << UpRefs.size() << " upreferences active!\n"; 1228176472Skmacy#endif 1229176472Skmacy 1230176472Skmacy // If we find any resolvable upreferences (i.e., those whose NestingLevel goes 1231176472Skmacy // to zero), we resolve them all together before we resolve them to Ty. At 1232176472Skmacy // the end of the loop, if there is anything to resolve to Ty, it will be in 1233176472Skmacy // this variable. 1234176472Skmacy OpaqueType *TypeToResolve = 0; 1235176472Skmacy 1236176472Skmacy for (unsigned i = 0; i != UpRefs.size(); ++i) { 1237176472Skmacy // Determine if 'Ty' directly contains this up-references 'LastContainedTy'. 1238176472Skmacy bool ContainsType = 1239176472Skmacy std::find(Ty->subtype_begin(), Ty->subtype_end(), 1240176472Skmacy UpRefs[i].LastContainedTy) != Ty->subtype_end(); 1241176472Skmacy 1242167514Skmacy#if 0 1243167514Skmacy dbgs() << " UR#" << i << " - TypeContains(" << Ty->getDescription() << ", " 1244176472Skmacy << UpRefs[i].LastContainedTy->getDescription() << ") = " 1245176472Skmacy << (ContainsType ? "true" : "false") 1246176472Skmacy << " level=" << UpRefs[i].NestingLevel << "\n"; 1247176472Skmacy#endif 1248176472Skmacy if (!ContainsType) 1249176472Skmacy continue; 1250176472Skmacy 1251176472Skmacy // Decrement level of upreference 1252167514Skmacy unsigned Level = --UpRefs[i].NestingLevel; 1253167514Skmacy UpRefs[i].LastContainedTy = Ty; 1254167514Skmacy 1255167514Skmacy // If the Up-reference has a non-zero level, it shouldn't be resolved yet. 1256167514Skmacy if (Level != 0) 1257167514Skmacy continue; 1258167514Skmacy 1259167514Skmacy#if 0 1260167514Skmacy dbgs() << " * Resolving upreference for " << UpRefs[i].UpRefTy << "\n"; 1261167514Skmacy#endif 1262167514Skmacy if (!TypeToResolve) 1263167514Skmacy TypeToResolve = UpRefs[i].UpRefTy; 1264167514Skmacy else 1265167514Skmacy UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve); 1266167514Skmacy UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list. 1267167514Skmacy --i; // Do not skip the next element. 1268167514Skmacy } 1269167514Skmacy 1270167514Skmacy if (TypeToResolve) 1271167514Skmacy TypeToResolve->refineAbstractTypeTo(Ty); 1272167514Skmacy 1273167514Skmacy return Ty; 1274167514Skmacy} 1275167514Skmacy 1276167514Skmacy 1277167514Skmacy/// ParseTypeRec - The recursive function used to process the internal 1278167514Skmacy/// implementation details of types. 1279167514Skmacybool LLParser::ParseTypeRec(PATypeHolder &Result) { 1280167514Skmacy switch (Lex.getKind()) { 1281167514Skmacy default: 1282167514Skmacy return TokError("expected type"); 1283167514Skmacy case lltok::Type: 1284167514Skmacy // TypeRec ::= 'float' | 'void' (etc) 1285167514Skmacy Result = Lex.getTyVal(); 1286176472Skmacy Lex.Lex(); 1287176472Skmacy break; 1288176472Skmacy case lltok::kw_opaque: 1289176472Skmacy // TypeRec ::= 'opaque' 1290176472Skmacy Result = OpaqueType::get(Context); 1291176472Skmacy Lex.Lex(); 1292176472Skmacy break; 1293176472Skmacy case lltok::lbrace: 1294176472Skmacy // TypeRec ::= '{' ... '}' 1295176472Skmacy if (ParseStructType(Result, false)) 1296167514Skmacy return true; 1297167514Skmacy break; 1298176472Skmacy case lltok::kw_union: 1299176472Skmacy // TypeRec ::= 'union' '{' ... '}' 1300176472Skmacy if (ParseUnionType(Result)) 1301176472Skmacy return true; 1302176472Skmacy break; 1303176472Skmacy case lltok::lsquare: 1304176472Skmacy // TypeRec ::= '[' ... ']' 1305176472Skmacy Lex.Lex(); // eat the lsquare. 1306176472Skmacy if (ParseArrayVectorType(Result, false)) 1307167514Skmacy return true; 1308167514Skmacy break; 1309167514Skmacy case lltok::less: // Either vector or packed struct. 1310176472Skmacy // TypeRec ::= '<' ... '>' 1311167514Skmacy Lex.Lex(); 1312167514Skmacy if (Lex.getKind() == lltok::lbrace) { 1313167514Skmacy if (ParseStructType(Result, true) || 1314176472Skmacy ParseToken(lltok::greater, "expected '>' at end of packed struct")) 1315167514Skmacy return true; 1316167514Skmacy } else if (ParseArrayVectorType(Result, true)) 1317167514Skmacy return true; 1318176472Skmacy break; 1319167514Skmacy case lltok::LocalVar: 1320167514Skmacy case lltok::StringConstant: // FIXME: REMOVE IN LLVM 3.0 1321167514Skmacy // TypeRec ::= %foo 1322167514Skmacy if (const Type *T = M->getTypeByName(Lex.getStrVal())) { 1323176472Skmacy Result = T; 1324167514Skmacy } else { 1325167514Skmacy Result = OpaqueType::get(Context); 1326167514Skmacy ForwardRefTypes.insert(std::make_pair(Lex.getStrVal(), 1327167514Skmacy std::make_pair(Result, 1328167514Skmacy Lex.getLoc()))); 1329167514Skmacy M->addTypeName(Lex.getStrVal(), Result.get()); 1330167514Skmacy } 1331167514Skmacy Lex.Lex(); 1332167514Skmacy break; 1333167514Skmacy 1334167514Skmacy case lltok::LocalVarID: 1335167514Skmacy // TypeRec ::= %4 1336167514Skmacy if (Lex.getUIntVal() < NumberedTypes.size()) 1337167514Skmacy Result = NumberedTypes[Lex.getUIntVal()]; 1338167514Skmacy else { 1339167514Skmacy std::map<unsigned, std::pair<PATypeHolder, LocTy> >::iterator 1340176472Skmacy I = ForwardRefTypeIDs.find(Lex.getUIntVal()); 1341176472Skmacy if (I != ForwardRefTypeIDs.end()) 1342176472Skmacy Result = I->second.first; 1343167514Skmacy else { 1344167514Skmacy Result = OpaqueType::get(Context); 1345167514Skmacy ForwardRefTypeIDs.insert(std::make_pair(Lex.getUIntVal(), 1346176472Skmacy std::make_pair(Result, 1347176472Skmacy Lex.getLoc()))); 1348176472Skmacy } 1349176472Skmacy } 1350176472Skmacy Lex.Lex(); 1351167514Skmacy break; 1352167514Skmacy case lltok::backslash: { 1353167514Skmacy // TypeRec ::= '\' 4 1354167514Skmacy Lex.Lex(); 1355167514Skmacy unsigned Val; 1356167514Skmacy if (ParseUInt32(Val)) return true; 1357167514Skmacy OpaqueType *OT = OpaqueType::get(Context); //Use temporary placeholder. 1358167514Skmacy UpRefs.push_back(UpRefRecord(Lex.getLoc(), Val, OT)); 1359167514Skmacy Result = OT; 1360167514Skmacy break; 1361167514Skmacy } 1362167514Skmacy } 1363167514Skmacy 1364167514Skmacy // Parse the type suffixes. 1365167514Skmacy while (1) { 1366167514Skmacy switch (Lex.getKind()) { 1367167514Skmacy // End of type. 1368167514Skmacy default: return false; 1369167514Skmacy 1370167514Skmacy // TypeRec ::= TypeRec '*' 1371167514Skmacy case lltok::star: 1372167514Skmacy if (Result.get()->isLabelTy()) 1373167514Skmacy return TokError("basic block pointers are invalid"); 1374167514Skmacy if (Result.get()->isVoidTy()) 1375167514Skmacy return TokError("pointers to void are invalid; use i8* instead"); 1376167514Skmacy if (!PointerType::isValidElementType(Result.get())) 1377167514Skmacy return TokError("pointer to this type is invalid"); 1378167514Skmacy Result = HandleUpRefs(PointerType::getUnqual(Result.get())); 1379167514Skmacy Lex.Lex(); 1380167514Skmacy break; 1381167514Skmacy 1382167514Skmacy // TypeRec ::= TypeRec 'addrspace' '(' uint32 ')' '*' 1383167514Skmacy case lltok::kw_addrspace: { 1384167514Skmacy if (Result.get()->isLabelTy()) 1385167514Skmacy return TokError("basic block pointers are invalid"); 1386167514Skmacy if (Result.get()->isVoidTy()) 1387167514Skmacy return TokError("pointers to void are invalid; use i8* instead"); 1388167514Skmacy if (!PointerType::isValidElementType(Result.get())) 1389167514Skmacy return TokError("pointer to this type is invalid"); 1390167514Skmacy unsigned AddrSpace; 1391167514Skmacy if (ParseOptionalAddrSpace(AddrSpace) || 1392167514Skmacy ParseToken(lltok::star, "expected '*' in address space")) 1393167514Skmacy return true; 1394167514Skmacy 1395167514Skmacy Result = HandleUpRefs(PointerType::get(Result.get(), AddrSpace)); 1396167514Skmacy break; 1397167514Skmacy } 1398167514Skmacy 1399167514Skmacy /// Types '(' ArgTypeListI ')' OptFuncAttrs 1400167514Skmacy case lltok::lparen: 1401167514Skmacy if (ParseFunctionType(Result)) 1402167514Skmacy return true; 1403167514Skmacy break; 1404167514Skmacy } 1405167514Skmacy } 1406167514Skmacy} 1407167514Skmacy 1408167514Skmacy/// ParseParameterList 1409167514Skmacy/// ::= '(' ')' 1410167514Skmacy/// ::= '(' Arg (',' Arg)* ')' 1411167514Skmacy/// Arg 1412167514Skmacy/// ::= Type OptionalAttributes Value OptionalAttributes 1413167514Skmacybool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList, 1414167514Skmacy PerFunctionState &PFS) { 1415167514Skmacy if (ParseToken(lltok::lparen, "expected '(' in call")) 1416167514Skmacy return true; 1417167514Skmacy 1418167514Skmacy while (Lex.getKind() != lltok::rparen) { 1419167514Skmacy // If this isn't the first argument, we need a comma. 1420167514Skmacy if (!ArgList.empty() && 1421167514Skmacy ParseToken(lltok::comma, "expected ',' in argument list")) 1422167514Skmacy return true; 1423176472Skmacy 1424167514Skmacy // Parse the argument. 1425176472Skmacy LocTy ArgLoc; 1426176472Skmacy PATypeHolder ArgTy(Type::getVoidTy(Context)); 1427176472Skmacy unsigned ArgAttrs1 = Attribute::None; 1428176472Skmacy unsigned ArgAttrs2 = Attribute::None; 1429176472Skmacy Value *V; 1430176472Skmacy if (ParseType(ArgTy, ArgLoc)) 1431176472Skmacy return true; 1432176472Skmacy 1433176472Skmacy // Otherwise, handle normal operands. 1434167514Skmacy if (ParseOptionalAttrs(ArgAttrs1, 0) || 1435167514Skmacy ParseValue(ArgTy, V, PFS) || 1436167514Skmacy // FIXME: Should not allow attributes after the argument, remove this 1437167514Skmacy // in LLVM 3.0. 1438167514Skmacy ParseOptionalAttrs(ArgAttrs2, 3)) 1439167514Skmacy return true; 1440167514Skmacy ArgList.push_back(ParamInfo(ArgLoc, V, ArgAttrs1|ArgAttrs2)); 1441167514Skmacy } 1442167514Skmacy 1443167514Skmacy Lex.Lex(); // Lex the ')'. 1444167514Skmacy return false; 1445167514Skmacy} 1446167514Skmacy 1447167514Skmacy 1448167514Skmacy 1449167514Skmacy/// ParseArgumentList - Parse the argument list for a function type or function 1450167514Skmacy/// prototype. If 'inType' is true then we are parsing a FunctionType. 1451167514Skmacy/// ::= '(' ArgTypeListI ')' 1452167514Skmacy/// ArgTypeListI 1453167514Skmacy/// ::= /*empty*/ 1454167514Skmacy/// ::= '...' 1455167514Skmacy/// ::= ArgTypeList ',' '...' 1456167514Skmacy/// ::= ArgType (',' ArgType)* 1457167514Skmacy/// 1458167514Skmacybool LLParser::ParseArgumentList(std::vector<ArgInfo> &ArgList, 1459167514Skmacy bool &isVarArg, bool inType) { 1460167514Skmacy isVarArg = false; 1461167514Skmacy assert(Lex.getKind() == lltok::lparen); 1462167514Skmacy Lex.Lex(); // eat the (. 1463176472Skmacy 1464167514Skmacy if (Lex.getKind() == lltok::rparen) { 1465176472Skmacy // empty 1466176472Skmacy } else if (Lex.getKind() == lltok::dotdotdot) { 1467176472Skmacy isVarArg = true; 1468176472Skmacy Lex.Lex(); 1469167514Skmacy } else { 1470176472Skmacy LocTy TypeLoc = Lex.getLoc(); 1471176472Skmacy PATypeHolder ArgTy(Type::getVoidTy(Context)); 1472176472Skmacy unsigned Attrs; 1473176472Skmacy std::string Name; 1474167514Skmacy 1475176472Skmacy // If we're parsing a type, use ParseTypeRec, because we allow recursive 1476176472Skmacy // types (such as a function returning a pointer to itself). If parsing a 1477176472Skmacy // function prototype, we require fully resolved types. 1478176472Skmacy if ((inType ? ParseTypeRec(ArgTy) : ParseType(ArgTy)) || 1479176472Skmacy ParseOptionalAttrs(Attrs, 0)) return true; 1480176472Skmacy 1481176472Skmacy if (ArgTy->isVoidTy()) 1482176472Skmacy return Error(TypeLoc, "argument can not have void type"); 1483167514Skmacy 1484167514Skmacy if (Lex.getKind() == lltok::LocalVar || 1485167514Skmacy Lex.getKind() == lltok::StringConstant) { // FIXME: REMOVE IN LLVM 3.0 1486167514Skmacy Name = Lex.getStrVal(); 1487167514Skmacy Lex.Lex(); 1488167514Skmacy } 1489167514Skmacy 1490167514Skmacy if (!FunctionType::isValidArgumentType(ArgTy)) 1491167514Skmacy return Error(TypeLoc, "invalid type for function argument"); 1492167514Skmacy 1493167514Skmacy ArgList.push_back(ArgInfo(TypeLoc, ArgTy, Attrs, Name)); 1494167514Skmacy 1495167514Skmacy while (EatIfPresent(lltok::comma)) { 1496167514Skmacy // Handle ... at end of arg list. 1497167514Skmacy if (EatIfPresent(lltok::dotdotdot)) { 1498167514Skmacy isVarArg = true; 1499167514Skmacy break; 1500167514Skmacy } 1501167514Skmacy 1502167514Skmacy // Otherwise must be an argument type. 1503167514Skmacy TypeLoc = Lex.getLoc(); 1504167514Skmacy if ((inType ? ParseTypeRec(ArgTy) : ParseType(ArgTy)) || 1505167514Skmacy ParseOptionalAttrs(Attrs, 0)) return true; 1506167514Skmacy 1507167514Skmacy if (ArgTy->isVoidTy()) 1508167514Skmacy return Error(TypeLoc, "argument can not have void type"); 1509167514Skmacy 1510167514Skmacy if (Lex.getKind() == lltok::LocalVar || 1511167514Skmacy Lex.getKind() == lltok::StringConstant) { // FIXME: REMOVE IN LLVM 3.0 1512167514Skmacy Name = Lex.getStrVal(); 1513167514Skmacy Lex.Lex(); 1514167514Skmacy } else { 1515167514Skmacy Name = ""; 1516167514Skmacy } 1517167514Skmacy 1518167514Skmacy if (!ArgTy->isFirstClassType() && !isa<OpaqueType>(ArgTy)) 1519167514Skmacy return Error(TypeLoc, "invalid type for function argument"); 1520167514Skmacy 1521167514Skmacy ArgList.push_back(ArgInfo(TypeLoc, ArgTy, Attrs, Name)); 1522167514Skmacy } 1523167514Skmacy } 1524167514Skmacy 1525167514Skmacy return ParseToken(lltok::rparen, "expected ')' at end of argument list"); 1526167514Skmacy} 1527167514Skmacy 1528167514Skmacy/// ParseFunctionType 1529167514Skmacy/// ::= Type ArgumentList OptionalAttrs 1530167514Skmacybool LLParser::ParseFunctionType(PATypeHolder &Result) { 1531167514Skmacy assert(Lex.getKind() == lltok::lparen); 1532167514Skmacy 1533167514Skmacy if (!FunctionType::isValidReturnType(Result)) 1534167514Skmacy return TokError("invalid function return type"); 1535167514Skmacy 1536167514Skmacy std::vector<ArgInfo> ArgList; 1537167514Skmacy bool isVarArg; 1538167514Skmacy unsigned Attrs; 1539167514Skmacy if (ParseArgumentList(ArgList, isVarArg, true) || 1540167514Skmacy // FIXME: Allow, but ignore attributes on function types! 1541167514Skmacy // FIXME: Remove in LLVM 3.0 1542167514Skmacy ParseOptionalAttrs(Attrs, 2)) 1543167514Skmacy return true; 1544167514Skmacy 1545167514Skmacy // Reject names on the arguments lists. 1546167514Skmacy for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { 1547167514Skmacy if (!ArgList[i].Name.empty()) 1548167514Skmacy return Error(ArgList[i].Loc, "argument name invalid in function type"); 1549167514Skmacy if (!ArgList[i].Attrs != 0) { 1550167514Skmacy // Allow but ignore attributes on function types; this permits 1551167514Skmacy // auto-upgrade. 1552167514Skmacy // FIXME: REJECT ATTRIBUTES ON FUNCTION TYPES in LLVM 3.0 1553167514Skmacy } 1554167514Skmacy } 1555167514Skmacy 1556167514Skmacy std::vector<const Type*> ArgListTy; 1557167514Skmacy for (unsigned i = 0, e = ArgList.size(); i != e; ++i) 1558167514Skmacy ArgListTy.push_back(ArgList[i].Type); 1559167514Skmacy 1560167514Skmacy Result = HandleUpRefs(FunctionType::get(Result.get(), 1561167514Skmacy ArgListTy, isVarArg)); 1562167514Skmacy return false; 1563167514Skmacy} 1564167514Skmacy 1565167514Skmacy/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere. 1566167514Skmacy/// TypeRec 1567167514Skmacy/// ::= '{' '}' 1568167514Skmacy/// ::= '{' TypeRec (',' TypeRec)* '}' 1569167514Skmacy/// ::= '<' '{' '}' '>' 1570167514Skmacy/// ::= '<' '{' TypeRec (',' TypeRec)* '}' '>' 1571167514Skmacybool LLParser::ParseStructType(PATypeHolder &Result, bool Packed) { 1572167514Skmacy assert(Lex.getKind() == lltok::lbrace); 1573167514Skmacy Lex.Lex(); // Consume the '{' 1574167514Skmacy 1575167514Skmacy if (EatIfPresent(lltok::rbrace)) { 1576167514Skmacy Result = StructType::get(Context, Packed); 1577167514Skmacy return false; 1578167514Skmacy } 1579167514Skmacy 1580167514Skmacy std::vector<PATypeHolder> ParamsList; 1581167514Skmacy LocTy EltTyLoc = Lex.getLoc(); 1582167514Skmacy if (ParseTypeRec(Result)) return true; 1583167514Skmacy ParamsList.push_back(Result); 1584167514Skmacy 1585167514Skmacy if (Result->isVoidTy()) 1586167514Skmacy return Error(EltTyLoc, "struct element can not have void type"); 1587167514Skmacy if (!StructType::isValidElementType(Result)) 1588167514Skmacy return Error(EltTyLoc, "invalid element type for struct"); 1589167514Skmacy 1590167514Skmacy while (EatIfPresent(lltok::comma)) { 1591167514Skmacy EltTyLoc = Lex.getLoc(); 1592167514Skmacy if (ParseTypeRec(Result)) return true; 1593167514Skmacy 1594167514Skmacy if (Result->isVoidTy()) 1595167514Skmacy return Error(EltTyLoc, "struct element can not have void type"); 1596167514Skmacy if (!StructType::isValidElementType(Result)) 1597167514Skmacy return Error(EltTyLoc, "invalid element type for struct"); 1598167514Skmacy 1599167514Skmacy ParamsList.push_back(Result); 1600167514Skmacy } 1601167514Skmacy 1602167514Skmacy if (ParseToken(lltok::rbrace, "expected '}' at end of struct")) 1603167514Skmacy return true; 1604167514Skmacy 1605167514Skmacy std::vector<const Type*> ParamsListTy; 1606167514Skmacy for (unsigned i = 0, e = ParamsList.size(); i != e; ++i) 1607167514Skmacy ParamsListTy.push_back(ParamsList[i].get()); 1608167514Skmacy Result = HandleUpRefs(StructType::get(Context, ParamsListTy, Packed)); 1609167514Skmacy return false; 1610167514Skmacy} 1611167514Skmacy 1612167514Skmacy/// ParseUnionType 1613167514Skmacy/// TypeRec 1614167514Skmacy/// ::= 'union' '{' TypeRec (',' TypeRec)* '}' 1615167514Skmacybool LLParser::ParseUnionType(PATypeHolder &Result) { 1616167514Skmacy assert(Lex.getKind() == lltok::kw_union); 1617167514Skmacy Lex.Lex(); // Consume the 'union' 1618167514Skmacy 1619167514Skmacy if (ParseToken(lltok::lbrace, "'{' expected after 'union'")) return true; 1620167514Skmacy 1621167514Skmacy SmallVector<PATypeHolder, 8> ParamsList; 1622167514Skmacy do { 1623167514Skmacy LocTy EltTyLoc = Lex.getLoc(); 1624167514Skmacy if (ParseTypeRec(Result)) return true; 1625167514Skmacy ParamsList.push_back(Result); 1626167514Skmacy 1627167514Skmacy if (Result->isVoidTy()) 1628167514Skmacy return Error(EltTyLoc, "union element can not have void type"); 1629167514Skmacy if (!UnionType::isValidElementType(Result)) 1630167514Skmacy return Error(EltTyLoc, "invalid element type for union"); 1631167514Skmacy 1632167514Skmacy } while (EatIfPresent(lltok::comma)) ; 1633167514Skmacy 1634167514Skmacy if (ParseToken(lltok::rbrace, "expected '}' at end of union")) 1635167514Skmacy return true; 1636167514Skmacy 1637167514Skmacy SmallVector<const Type*, 8> ParamsListTy; 1638167514Skmacy for (unsigned i = 0, e = ParamsList.size(); i != e; ++i) 1639167514Skmacy ParamsListTy.push_back(ParamsList[i].get()); 1640167514Skmacy Result = HandleUpRefs(UnionType::get(&ParamsListTy[0], ParamsListTy.size())); 1641167514Skmacy return false; 1642167514Skmacy} 1643167514Skmacy 1644167514Skmacy/// ParseArrayVectorType - Parse an array or vector type, assuming the first 1645167514Skmacy/// token has already been consumed. 1646167514Skmacy/// TypeRec 1647167514Skmacy/// ::= '[' APSINTVAL 'x' Types ']' 1648167514Skmacy/// ::= '<' APSINTVAL 'x' Types '>' 1649167514Skmacybool LLParser::ParseArrayVectorType(PATypeHolder &Result, bool isVector) { 1650167514Skmacy if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() || 1651167514Skmacy Lex.getAPSIntVal().getBitWidth() > 64) 1652167514Skmacy return TokError("expected number in address space"); 1653167514Skmacy 1654167514Skmacy LocTy SizeLoc = Lex.getLoc(); 1655167514Skmacy uint64_t Size = Lex.getAPSIntVal().getZExtValue(); 1656176472Skmacy Lex.Lex(); 1657176472Skmacy 1658176472Skmacy if (ParseToken(lltok::kw_x, "expected 'x' after element count")) 1659176472Skmacy return true; 1660176472Skmacy 1661176472Skmacy LocTy TypeLoc = Lex.getLoc(); 1662176472Skmacy PATypeHolder EltTy(Type::getVoidTy(Context)); 1663176472Skmacy if (ParseTypeRec(EltTy)) return true; 1664176472Skmacy 1665176472Skmacy if (EltTy->isVoidTy()) 1666176472Skmacy return Error(TypeLoc, "array and vector element type cannot be void"); 1667176472Skmacy 1668176472Skmacy if (ParseToken(isVector ? lltok::greater : lltok::rsquare, 1669176472Skmacy "expected end of sequential type")) 1670176472Skmacy return true; 1671176472Skmacy 1672176472Skmacy if (isVector) { 1673176472Skmacy if (Size == 0) 1674176472Skmacy return Error(SizeLoc, "zero element vector is illegal"); 1675176472Skmacy if ((unsigned)Size != Size) 1676176472Skmacy return Error(SizeLoc, "size too large for vector"); 1677176472Skmacy if (!VectorType::isValidElementType(EltTy)) 1678176472Skmacy return Error(TypeLoc, "vector element type must be fp or integer"); 1679176472Skmacy Result = VectorType::get(EltTy, unsigned(Size)); 1680176472Skmacy } else { 1681176472Skmacy if (!ArrayType::isValidElementType(EltTy)) 1682176472Skmacy return Error(TypeLoc, "invalid array element type"); 1683176472Skmacy Result = HandleUpRefs(ArrayType::get(EltTy, Size)); 1684176472Skmacy } 1685176472Skmacy return false; 1686176472Skmacy} 1687176472Skmacy 1688176472Skmacy//===----------------------------------------------------------------------===// 1689176472Skmacy// Function Semantic Analysis. 1690176472Skmacy//===----------------------------------------------------------------------===// 1691176472Skmacy 1692176472SkmacyLLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f, 1693176472Skmacy int functionNumber) 1694176472Skmacy : P(p), F(f), FunctionNumber(functionNumber) { 1695176472Skmacy 1696176472Skmacy // Insert unnamed arguments into the NumberedVals list. 1697176472Skmacy for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); 1698176472Skmacy AI != E; ++AI) 1699176472Skmacy if (!AI->hasName()) 1700176472Skmacy NumberedVals.push_back(AI); 1701176472Skmacy} 1702176472Skmacy 1703176472SkmacyLLParser::PerFunctionState::~PerFunctionState() { 1704176472Skmacy // If there were any forward referenced non-basicblock values, delete them. 1705176472Skmacy for (std::map<std::string, std::pair<Value*, LocTy> >::iterator 1706176472Skmacy I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I) 1707176472Skmacy if (!isa<BasicBlock>(I->second.first)) { 1708176472Skmacy I->second.first->replaceAllUsesWith( 1709176472Skmacy UndefValue::get(I->second.first->getType())); 1710176472Skmacy delete I->second.first; 1711176472Skmacy I->second.first = 0; 1712176472Skmacy } 1713176472Skmacy 1714176472Skmacy for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator 1715176472Skmacy I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I) 1716176472Skmacy if (!isa<BasicBlock>(I->second.first)) { 1717176472Skmacy I->second.first->replaceAllUsesWith( 1718176472Skmacy UndefValue::get(I->second.first->getType())); 1719176472Skmacy delete I->second.first; 1720176472Skmacy I->second.first = 0; 1721176472Skmacy } 1722176472Skmacy} 1723176472Skmacy 1724176472Skmacybool LLParser::PerFunctionState::FinishFunction() { 1725176472Skmacy // Check to see if someone took the address of labels in this block. 1726176472Skmacy if (!P.ForwardRefBlockAddresses.empty()) { 1727176472Skmacy ValID FunctionID; 1728176472Skmacy if (!F.getName().empty()) { 1729176472Skmacy FunctionID.Kind = ValID::t_GlobalName; 1730176472Skmacy FunctionID.StrVal = F.getName(); 1731176472Skmacy } else { 1732176472Skmacy FunctionID.Kind = ValID::t_GlobalID; 1733176472Skmacy FunctionID.UIntVal = FunctionNumber; 1734176472Skmacy } 1735176472Skmacy 1736176472Skmacy std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >::iterator 1737176472Skmacy FRBAI = P.ForwardRefBlockAddresses.find(FunctionID); 1738176472Skmacy if (FRBAI != P.ForwardRefBlockAddresses.end()) { 1739176472Skmacy // Resolve all these references. 1740176472Skmacy if (P.ResolveForwardRefBlockAddresses(&F, FRBAI->second, this)) 1741176472Skmacy return true; 1742176472Skmacy 1743176472Skmacy P.ForwardRefBlockAddresses.erase(FRBAI); 1744176472Skmacy } 1745176472Skmacy } 1746176472Skmacy 1747176472Skmacy if (!ForwardRefVals.empty()) 1748176472Skmacy return P.Error(ForwardRefVals.begin()->second.second, 1749176472Skmacy "use of undefined value '%" + ForwardRefVals.begin()->first + 1750176472Skmacy "'"); 1751176472Skmacy if (!ForwardRefValIDs.empty()) 1752176472Skmacy return P.Error(ForwardRefValIDs.begin()->second.second, 1753176472Skmacy "use of undefined value '%" + 1754176472Skmacy utostr(ForwardRefValIDs.begin()->first) + "'"); 1755176472Skmacy return false; 1756176472Skmacy} 1757176472Skmacy 1758176472Skmacy 1759176472Skmacy/// GetVal - Get a value with the specified name or ID, creating a 1760176472Skmacy/// forward reference record if needed. This can return null if the value 1761176472Skmacy/// exists but does not have the right type. 1762176472SkmacyValue *LLParser::PerFunctionState::GetVal(const std::string &Name, 1763176472Skmacy const Type *Ty, LocTy Loc) { 1764176472Skmacy // Look this name up in the normal function symbol table. 1765176472Skmacy Value *Val = F.getValueSymbolTable().lookup(Name); 1766176472Skmacy 1767176472Skmacy // If this is a forward reference for the value, see if we already created a 1768176472Skmacy // forward ref record. 1769176472Skmacy if (Val == 0) { 1770167514Skmacy std::map<std::string, std::pair<Value*, LocTy> >::iterator 1771167514Skmacy I = ForwardRefVals.find(Name); 1772167514Skmacy if (I != ForwardRefVals.end()) 1773167514Skmacy Val = I->second.first; 1774167514Skmacy } 1775167514Skmacy 1776167514Skmacy // If we have the value in the symbol table or fwd-ref table, return it. 1777167514Skmacy if (Val) { 1778167514Skmacy if (Val->getType() == Ty) return Val; 1779167514Skmacy if (Ty->isLabelTy()) 1780167514Skmacy P.Error(Loc, "'%" + Name + "' is not a basic block"); 1781167514Skmacy else 1782167514Skmacy P.Error(Loc, "'%" + Name + "' defined with type '" + 1783167514Skmacy Val->getType()->getDescription() + "'"); 1784167514Skmacy return 0; 1785167514Skmacy } 1786167514Skmacy 1787167514Skmacy // Don't make placeholders with invalid type. 1788167514Skmacy if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty) && !Ty->isLabelTy()) { 1789167514Skmacy P.Error(Loc, "invalid use of a non-first-class type"); 1790167514Skmacy return 0; 1791167514Skmacy } 1792167514Skmacy 1793167514Skmacy // Otherwise, create a new forward reference for this value and remember it. 1794167514Skmacy Value *FwdVal; 1795167514Skmacy if (Ty->isLabelTy()) 1796167514Skmacy FwdVal = BasicBlock::Create(F.getContext(), Name, &F); 1797167514Skmacy else 1798167514Skmacy FwdVal = new Argument(Ty, Name); 1799167514Skmacy 1800167514Skmacy ForwardRefVals[Name] = std::make_pair(FwdVal, Loc); 1801167514Skmacy return FwdVal; 1802167514Skmacy} 1803167514Skmacy 1804167514SkmacyValue *LLParser::PerFunctionState::GetVal(unsigned ID, const Type *Ty, 1805167514Skmacy LocTy Loc) { 1806167514Skmacy // Look this name up in the normal function symbol table. 1807167514Skmacy Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0; 1808167514Skmacy 1809167514Skmacy // If this is a forward reference for the value, see if we already created a 1810167514Skmacy // forward ref record. 1811167514Skmacy if (Val == 0) { 1812167514Skmacy std::map<unsigned, std::pair<Value*, LocTy> >::iterator 1813167514Skmacy I = ForwardRefValIDs.find(ID); 1814167514Skmacy if (I != ForwardRefValIDs.end()) 1815167514Skmacy Val = I->second.first; 1816167514Skmacy } 1817167514Skmacy 1818167514Skmacy // If we have the value in the symbol table or fwd-ref table, return it. 1819167514Skmacy if (Val) { 1820167514Skmacy if (Val->getType() == Ty) return Val; 1821167514Skmacy if (Ty->isLabelTy()) 1822167514Skmacy P.Error(Loc, "'%" + utostr(ID) + "' is not a basic block"); 1823167514Skmacy else 1824167514Skmacy P.Error(Loc, "'%" + utostr(ID) + "' defined with type '" + 1825167514Skmacy Val->getType()->getDescription() + "'"); 1826167514Skmacy return 0; 1827167514Skmacy } 1828167514Skmacy 1829167514Skmacy if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty) && !Ty->isLabelTy()) { 1830167514Skmacy P.Error(Loc, "invalid use of a non-first-class type"); 1831167514Skmacy return 0; 1832167514Skmacy } 1833167514Skmacy 1834167514Skmacy // Otherwise, create a new forward reference for this value and remember it. 1835167514Skmacy Value *FwdVal; 1836167514Skmacy if (Ty->isLabelTy()) 1837167514Skmacy FwdVal = BasicBlock::Create(F.getContext(), "", &F); 1838167514Skmacy else 1839167514Skmacy FwdVal = new Argument(Ty); 1840167514Skmacy 1841167514Skmacy ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc); 1842167514Skmacy return FwdVal; 1843167514Skmacy} 1844167514Skmacy 1845167514Skmacy/// SetInstName - After an instruction is parsed and inserted into its 1846167514Skmacy/// basic block, this installs its name. 1847167514Skmacybool LLParser::PerFunctionState::SetInstName(int NameID, 1848167514Skmacy const std::string &NameStr, 1849167514Skmacy LocTy NameLoc, Instruction *Inst) { 1850167514Skmacy // If this instruction has void type, it cannot have a name or ID specified. 1851167514Skmacy if (Inst->getType()->isVoidTy()) { 1852167514Skmacy if (NameID != -1 || !NameStr.empty()) 1853167514Skmacy return P.Error(NameLoc, "instructions returning void cannot have a name"); 1854167514Skmacy return false; 1855167514Skmacy } 1856167514Skmacy 1857167514Skmacy // If this was a numbered instruction, verify that the instruction is the 1858167514Skmacy // expected value and resolve any forward references. 1859167514Skmacy if (NameStr.empty()) { 1860167514Skmacy // If neither a name nor an ID was specified, just use the next ID. 1861167514Skmacy if (NameID == -1) 1862167514Skmacy NameID = NumberedVals.size(); 1863167514Skmacy 1864167514Skmacy if (unsigned(NameID) != NumberedVals.size()) 1865167514Skmacy return P.Error(NameLoc, "instruction expected to be numbered '%" + 1866167514Skmacy utostr(NumberedVals.size()) + "'"); 1867167514Skmacy 1868167514Skmacy std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI = 1869167514Skmacy ForwardRefValIDs.find(NameID); 1870167514Skmacy if (FI != ForwardRefValIDs.end()) { 1871167514Skmacy if (FI->second.first->getType() != Inst->getType()) 1872176472Skmacy return P.Error(NameLoc, "instruction forward referenced with type '" + 1873167514Skmacy FI->second.first->getType()->getDescription() + "'"); 1874176472Skmacy FI->second.first->replaceAllUsesWith(Inst); 1875176472Skmacy delete FI->second.first; 1876176472Skmacy ForwardRefValIDs.erase(FI); 1877176472Skmacy } 1878167514Skmacy 1879176472Skmacy NumberedVals.push_back(Inst); 1880176472Skmacy return false; 1881176472Skmacy } 1882167514Skmacy 1883176472Skmacy // Otherwise, the instruction had a name. Resolve forward refs and set it. 1884176472Skmacy std::map<std::string, std::pair<Value*, LocTy> >::iterator 1885176472Skmacy FI = ForwardRefVals.find(NameStr); 1886167514Skmacy if (FI != ForwardRefVals.end()) { 1887176472Skmacy if (FI->second.first->getType() != Inst->getType()) 1888176472Skmacy return P.Error(NameLoc, "instruction forward referenced with type '" + 1889176472Skmacy FI->second.first->getType()->getDescription() + "'"); 1890167514Skmacy FI->second.first->replaceAllUsesWith(Inst); 1891176472Skmacy delete FI->second.first; 1892176472Skmacy ForwardRefVals.erase(FI); 1893176472Skmacy } 1894167514Skmacy 1895176472Skmacy // Set the name on the instruction. 1896176472Skmacy Inst->setName(NameStr); 1897176472Skmacy 1898167514Skmacy if (Inst->getNameStr() != NameStr) 1899176472Skmacy return P.Error(NameLoc, "multiple definition of local value named '" + 1900176472Skmacy NameStr + "'"); 1901176472Skmacy return false; 1902167514Skmacy} 1903176472Skmacy 1904176472Skmacy/// GetBB - Get a basic block with the specified name or ID, creating a 1905176472Skmacy/// forward reference record if needed. 1906167514SkmacyBasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name, 1907167514Skmacy LocTy Loc) { 1908167514Skmacy return cast_or_null<BasicBlock>(GetVal(Name, 1909167514Skmacy Type::getLabelTy(F.getContext()), Loc)); 1910167514Skmacy} 1911167514Skmacy 1912167514SkmacyBasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) { 1913167514Skmacy return cast_or_null<BasicBlock>(GetVal(ID, 1914167514Skmacy Type::getLabelTy(F.getContext()), Loc)); 1915167514Skmacy} 1916167514Skmacy 1917167514Skmacy/// DefineBB - Define the specified basic block, which is either named or 1918167514Skmacy/// unnamed. If there is an error, this returns null otherwise it returns 1919167514Skmacy/// the block being defined. 1920167514SkmacyBasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name, 1921167514Skmacy LocTy Loc) { 1922167514Skmacy BasicBlock *BB; 1923167514Skmacy if (Name.empty()) 1924167514Skmacy BB = GetBB(NumberedVals.size(), Loc); 1925167514Skmacy else 1926167514Skmacy BB = GetBB(Name, Loc); 1927167514Skmacy if (BB == 0) return 0; // Already diagnosed error. 1928167514Skmacy 1929167514Skmacy // Move the block to the end of the function. Forward ref'd blocks are 1930167514Skmacy // inserted wherever they happen to be referenced. 1931167514Skmacy F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB); 1932167514Skmacy 1933167514Skmacy // Remove the block from forward ref sets. 1934167514Skmacy if (Name.empty()) { 1935167514Skmacy ForwardRefValIDs.erase(NumberedVals.size()); 1936167514Skmacy NumberedVals.push_back(BB); 1937167514Skmacy } else { 1938167514Skmacy // BB forward references are already in the function symbol table. 1939167514Skmacy ForwardRefVals.erase(Name); 1940167514Skmacy } 1941167514Skmacy 1942167514Skmacy return BB; 1943167514Skmacy} 1944167514Skmacy 1945167514Skmacy//===----------------------------------------------------------------------===// 1946167514Skmacy// Constants. 1947167514Skmacy//===----------------------------------------------------------------------===// 1948167514Skmacy 1949167514Skmacy/// ParseValID - Parse an abstract value that doesn't necessarily have a 1950167514Skmacy/// type implied. For example, if we parse "4" we don't know what integer type 1951167514Skmacy/// it has. The value will later be combined with its type and checked for 1952167514Skmacy/// sanity. PFS is used to convert function-local operands of metadata (since 1953167514Skmacy/// metadata operands are not just parsed here but also converted to values). 1954167514Skmacy/// PFS can be null when we are not parsing metadata values inside a function. 1955167514Skmacybool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) { 1956167514Skmacy ID.Loc = Lex.getLoc(); 1957167514Skmacy switch (Lex.getKind()) { 1958167514Skmacy default: return TokError("expected value token"); 1959167514Skmacy case lltok::GlobalID: // @42 1960167514Skmacy ID.UIntVal = Lex.getUIntVal(); 1961167514Skmacy ID.Kind = ValID::t_GlobalID; 1962167514Skmacy break; 1963167514Skmacy case lltok::GlobalVar: // @foo 1964167514Skmacy ID.StrVal = Lex.getStrVal(); 1965167514Skmacy ID.Kind = ValID::t_GlobalName; 1966167514Skmacy break; 1967167514Skmacy case lltok::LocalVarID: // %42 1968167514Skmacy ID.UIntVal = Lex.getUIntVal(); 1969167514Skmacy ID.Kind = ValID::t_LocalID; 1970167514Skmacy break; 1971167514Skmacy case lltok::LocalVar: // %foo 1972167514Skmacy case lltok::StringConstant: // "foo" - FIXME: REMOVE IN LLVM 3.0 1973167514Skmacy ID.StrVal = Lex.getStrVal(); 1974167514Skmacy ID.Kind = ValID::t_LocalName; 1975167514Skmacy break; 1976167514Skmacy case lltok::exclaim: // !{...} MDNode, !"foo" MDString 1977167514Skmacy Lex.Lex(); 1978167514Skmacy 1979167514Skmacy if (EatIfPresent(lltok::lbrace)) { 1980167514Skmacy SmallVector<Value*, 16> Elts; 1981167514Skmacy if (ParseMDNodeVector(Elts, PFS) || 1982167514Skmacy ParseToken(lltok::rbrace, "expected end of metadata node")) 1983167514Skmacy return true; 1984167514Skmacy 1985167514Skmacy ID.MDNodeVal = MDNode::get(Context, Elts.data(), Elts.size()); 1986167514Skmacy ID.Kind = ValID::t_MDNode; 1987167514Skmacy return false; 1988167514Skmacy } 1989167514Skmacy 1990167514Skmacy // Standalone metadata reference 1991167514Skmacy // !{ ..., !42, ... } 1992167514Skmacy if (Lex.getKind() == lltok::APSInt) { 1993167514Skmacy if (ParseMDNodeID(ID.MDNodeVal)) return true; 1994167514Skmacy ID.Kind = ValID::t_MDNode; 1995167514Skmacy return false; 1996167514Skmacy } 1997167514Skmacy 1998167514Skmacy // MDString: 1999167514Skmacy // ::= '!' STRINGCONSTANT 2000167514Skmacy if (ParseMDString(ID.MDStringVal)) return true; 2001167514Skmacy ID.Kind = ValID::t_MDString; 2002167514Skmacy return false; 2003167514Skmacy case lltok::APSInt: 2004167514Skmacy ID.APSIntVal = Lex.getAPSIntVal(); 2005167514Skmacy ID.Kind = ValID::t_APSInt; 2006167514Skmacy break; 2007167514Skmacy case lltok::APFloat: 2008167514Skmacy ID.APFloatVal = Lex.getAPFloatVal(); 2009167514Skmacy ID.Kind = ValID::t_APFloat; 2010167514Skmacy break; 2011167514Skmacy case lltok::kw_true: 2012167514Skmacy ID.ConstantVal = ConstantInt::getTrue(Context); 2013167514Skmacy ID.Kind = ValID::t_Constant; 2014167514Skmacy break; 2015167514Skmacy case lltok::kw_false: 2016167514Skmacy ID.ConstantVal = ConstantInt::getFalse(Context); 2017167514Skmacy ID.Kind = ValID::t_Constant; 2018167514Skmacy break; 2019167514Skmacy case lltok::kw_null: ID.Kind = ValID::t_Null; break; 2020167514Skmacy case lltok::kw_undef: ID.Kind = ValID::t_Undef; break; 2021167514Skmacy case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break; 2022167514Skmacy 2023167514Skmacy case lltok::lbrace: { 2024167514Skmacy // ValID ::= '{' ConstVector '}' 2025167514Skmacy Lex.Lex(); 2026167514Skmacy SmallVector<Constant*, 16> Elts; 2027167514Skmacy if (ParseGlobalValueVector(Elts) || 2028167514Skmacy ParseToken(lltok::rbrace, "expected end of struct constant")) 2029167514Skmacy return true; 2030167514Skmacy 2031167514Skmacy ID.ConstantVal = ConstantStruct::get(Context, Elts.data(), 2032167514Skmacy Elts.size(), false); 2033167514Skmacy ID.Kind = ValID::t_Constant; 2034167514Skmacy return false; 2035167514Skmacy } 2036167514Skmacy case lltok::less: { 2037176472Skmacy // ValID ::= '<' ConstVector '>' --> Vector. 2038167514Skmacy // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct. 2039176472Skmacy Lex.Lex(); 2040176472Skmacy bool isPackedStruct = EatIfPresent(lltok::lbrace); 2041176472Skmacy 2042176472Skmacy SmallVector<Constant*, 16> Elts; 2043167514Skmacy LocTy FirstEltLoc = Lex.getLoc(); 2044176472Skmacy if (ParseGlobalValueVector(Elts) || 2045176472Skmacy (isPackedStruct && 2046176472Skmacy ParseToken(lltok::rbrace, "expected end of packed struct")) || 2047176472Skmacy ParseToken(lltok::greater, "expected end of constant")) 2048167514Skmacy return true; 2049176472Skmacy 2050176472Skmacy if (isPackedStruct) { 2051176472Skmacy ID.ConstantVal = 2052176472Skmacy ConstantStruct::get(Context, Elts.data(), Elts.size(), true); 2053167514Skmacy ID.Kind = ValID::t_Constant; 2054167514Skmacy return false; 2055167514Skmacy } 2056167514Skmacy 2057167514Skmacy if (Elts.empty()) 2058167514Skmacy return Error(ID.Loc, "constant vector must not be empty"); 2059167514Skmacy 2060167514Skmacy if (!Elts[0]->getType()->isIntegerTy() && 2061167514Skmacy !Elts[0]->getType()->isFloatingPointTy()) 2062167514Skmacy return Error(FirstEltLoc, 2063167514Skmacy "vector elements must have integer or floating point type"); 2064167514Skmacy 2065167514Skmacy // Verify that all the vector elements have the same type. 2066167514Skmacy for (unsigned i = 1, e = Elts.size(); i != e; ++i) 2067167514Skmacy if (Elts[i]->getType() != Elts[0]->getType()) 2068167514Skmacy return Error(FirstEltLoc, 2069167514Skmacy "vector element #" + utostr(i) + 2070167514Skmacy " is not of type '" + Elts[0]->getType()->getDescription()); 2071167514Skmacy 2072167514Skmacy ID.ConstantVal = ConstantVector::get(Elts.data(), Elts.size()); 2073167514Skmacy ID.Kind = ValID::t_Constant; 2074167514Skmacy return false; 2075167514Skmacy } 2076167514Skmacy case lltok::lsquare: { // Array Constant 2077167514Skmacy Lex.Lex(); 2078167514Skmacy SmallVector<Constant*, 16> Elts; 2079167514Skmacy LocTy FirstEltLoc = Lex.getLoc(); 2080167514Skmacy if (ParseGlobalValueVector(Elts) || 2081167514Skmacy ParseToken(lltok::rsquare, "expected end of array constant")) 2082167514Skmacy return true; 2083167514Skmacy 2084167514Skmacy // Handle empty element. 2085167514Skmacy if (Elts.empty()) { 2086167514Skmacy // Use undef instead of an array because it's inconvenient to determine 2087167514Skmacy // the element type at this point, there being no elements to examine. 2088167514Skmacy ID.Kind = ValID::t_EmptyArray; 2089167514Skmacy return false; 2090167514Skmacy } 2091167514Skmacy 2092167514Skmacy if (!Elts[0]->getType()->isFirstClassType()) 2093167514Skmacy return Error(FirstEltLoc, "invalid array element type: " + 2094167514Skmacy Elts[0]->getType()->getDescription()); 2095167514Skmacy 2096167514Skmacy ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size()); 2097167514Skmacy 2098167514Skmacy // Verify all elements are correct type! 2099167514Skmacy for (unsigned i = 0, e = Elts.size(); i != e; ++i) { 2100167514Skmacy if (Elts[i]->getType() != Elts[0]->getType()) 2101167514Skmacy return Error(FirstEltLoc, 2102167514Skmacy "array element #" + utostr(i) + 2103167514Skmacy " is not of type '" +Elts[0]->getType()->getDescription()); 2104167514Skmacy } 2105167514Skmacy 2106167514Skmacy ID.ConstantVal = ConstantArray::get(ATy, Elts.data(), Elts.size()); 2107167514Skmacy ID.Kind = ValID::t_Constant; 2108167514Skmacy return false; 2109167514Skmacy } 2110167514Skmacy case lltok::kw_c: // c "foo" 2111167514Skmacy Lex.Lex(); 2112167514Skmacy ID.ConstantVal = ConstantArray::get(Context, Lex.getStrVal(), false); 2113167514Skmacy if (ParseToken(lltok::StringConstant, "expected string")) return true; 2114167514Skmacy ID.Kind = ValID::t_Constant; 2115167514Skmacy return false; 2116167514Skmacy 2117167514Skmacy case lltok::kw_asm: { 2118167514Skmacy // ValID ::= 'asm' SideEffect? AlignStack? STRINGCONSTANT ',' STRINGCONSTANT 2119167514Skmacy bool HasSideEffect, AlignStack; 2120167514Skmacy Lex.Lex(); 2121167514Skmacy if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) || 2122167514Skmacy ParseOptionalToken(lltok::kw_alignstack, AlignStack) || 2123167514Skmacy ParseStringConstant(ID.StrVal) || 2124167514Skmacy ParseToken(lltok::comma, "expected comma in inline asm expression") || 2125167514Skmacy ParseToken(lltok::StringConstant, "expected constraint string")) 2126167514Skmacy return true; 2127167514Skmacy ID.StrVal2 = Lex.getStrVal(); 2128167514Skmacy ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1); 2129167514Skmacy ID.Kind = ValID::t_InlineAsm; 2130167514Skmacy return false; 2131167514Skmacy } 2132167514Skmacy 2133167514Skmacy case lltok::kw_blockaddress: { 2134167514Skmacy // ValID ::= 'blockaddress' '(' @foo ',' %bar ')' 2135167514Skmacy Lex.Lex(); 2136167514Skmacy 2137167514Skmacy ValID Fn, Label; 2138167514Skmacy LocTy FnLoc, LabelLoc; 2139167514Skmacy 2140167514Skmacy if (ParseToken(lltok::lparen, "expected '(' in block address expression") || 2141167514Skmacy ParseValID(Fn) || 2142167514Skmacy ParseToken(lltok::comma, "expected comma in block address expression")|| 2143167514Skmacy ParseValID(Label) || 2144167514Skmacy ParseToken(lltok::rparen, "expected ')' in block address expression")) 2145167514Skmacy return true; 2146167514Skmacy 2147167514Skmacy if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName) 2148167514Skmacy return Error(Fn.Loc, "expected function name in blockaddress"); 2149167514Skmacy if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName) 2150167514Skmacy return Error(Label.Loc, "expected basic block name in blockaddress"); 2151167514Skmacy 2152167514Skmacy // Make a global variable as a placeholder for this reference. 2153167514Skmacy GlobalVariable *FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context), 2154167514Skmacy false, GlobalValue::InternalLinkage, 2155167514Skmacy 0, ""); 2156167514Skmacy ForwardRefBlockAddresses[Fn].push_back(std::make_pair(Label, FwdRef)); 2157167514Skmacy ID.ConstantVal = FwdRef; 2158167514Skmacy ID.Kind = ValID::t_Constant; 2159167514Skmacy return false; 2160167514Skmacy } 2161167514Skmacy 2162167514Skmacy case lltok::kw_trunc: 2163167514Skmacy case lltok::kw_zext: 2164167514Skmacy case lltok::kw_sext: 2165167514Skmacy case lltok::kw_fptrunc: 2166167514Skmacy case lltok::kw_fpext: 2167167514Skmacy case lltok::kw_bitcast: 2168167514Skmacy case lltok::kw_uitofp: 2169167514Skmacy case lltok::kw_sitofp: 2170167514Skmacy case lltok::kw_fptoui: 2171167514Skmacy case lltok::kw_fptosi: 2172167514Skmacy case lltok::kw_inttoptr: 2173167514Skmacy case lltok::kw_ptrtoint: { 2174167514Skmacy unsigned Opc = Lex.getUIntVal(); 2175167514Skmacy PATypeHolder DestTy(Type::getVoidTy(Context)); 2176167514Skmacy Constant *SrcVal; 2177167514Skmacy Lex.Lex(); 2178167514Skmacy if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") || 2179167514Skmacy ParseGlobalTypeAndValue(SrcVal) || 2180167514Skmacy ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") || 2181167514Skmacy ParseType(DestTy) || 2182167514Skmacy ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast")) 2183167514Skmacy return true; 2184167514Skmacy if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy)) 2185167514Skmacy return Error(ID.Loc, "invalid cast opcode for cast from '" + 2186167514Skmacy SrcVal->getType()->getDescription() + "' to '" + 2187167514Skmacy DestTy->getDescription() + "'"); 2188167514Skmacy ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc, 2189167514Skmacy SrcVal, DestTy); 2190167514Skmacy ID.Kind = ValID::t_Constant; 2191167514Skmacy return false; 2192167514Skmacy } 2193167514Skmacy case lltok::kw_extractvalue: { 2194167514Skmacy Lex.Lex(); 2195167514Skmacy Constant *Val; 2196167514Skmacy SmallVector<unsigned, 4> Indices; 2197167514Skmacy if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")|| 2198167514Skmacy ParseGlobalTypeAndValue(Val) || 2199167514Skmacy ParseIndexList(Indices) || 2200167514Skmacy ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr")) 2201167514Skmacy return true; 2202167514Skmacy 2203167514Skmacy if (!Val->getType()->isAggregateType()) 2204167514Skmacy return Error(ID.Loc, "extractvalue operand must be aggregate type"); 2205167514Skmacy if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(), 2206167514Skmacy Indices.end())) 2207176472Skmacy return Error(ID.Loc, "invalid indices for extractvalue"); 2208176472Skmacy ID.ConstantVal = 2209176472Skmacy ConstantExpr::getExtractValue(Val, Indices.data(), Indices.size()); 2210176472Skmacy ID.Kind = ValID::t_Constant; 2211176472Skmacy return false; 2212176472Skmacy } 2213176472Skmacy case lltok::kw_insertvalue: { 2214176472Skmacy Lex.Lex(); 2215176472Skmacy Constant *Val0, *Val1; 2216176472Skmacy SmallVector<unsigned, 4> Indices; 2217176472Skmacy if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")|| 2218176472Skmacy ParseGlobalTypeAndValue(Val0) || 2219176472Skmacy ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")|| 2220176472Skmacy ParseGlobalTypeAndValue(Val1) || 2221176472Skmacy ParseIndexList(Indices) || 2222176472Skmacy ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr")) 2223176472Skmacy return true; 2224176472Skmacy if (!Val0->getType()->isAggregateType()) 2225176472Skmacy return Error(ID.Loc, "insertvalue operand must be aggregate type"); 2226176472Skmacy if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(), 2227176472Skmacy Indices.end())) 2228176472Skmacy return Error(ID.Loc, "invalid indices for insertvalue"); 2229176472Skmacy ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, 2230176472Skmacy Indices.data(), Indices.size()); 2231176472Skmacy ID.Kind = ValID::t_Constant; 2232176472Skmacy return false; 2233176472Skmacy } 2234176472Skmacy case lltok::kw_icmp: 2235176472Skmacy case lltok::kw_fcmp: { 2236176472Skmacy unsigned PredVal, Opc = Lex.getUIntVal(); 2237176472Skmacy Constant *Val0, *Val1; 2238176472Skmacy Lex.Lex(); 2239176472Skmacy if (ParseCmpPredicate(PredVal, Opc) || 2240176472Skmacy ParseToken(lltok::lparen, "expected '(' in compare constantexpr") || 2241176472Skmacy ParseGlobalTypeAndValue(Val0) || 2242176472Skmacy ParseToken(lltok::comma, "expected comma in compare constantexpr") || 2243176472Skmacy ParseGlobalTypeAndValue(Val1) || 2244176472Skmacy ParseToken(lltok::rparen, "expected ')' in compare constantexpr")) 2245176472Skmacy return true; 2246176472Skmacy 2247176472Skmacy if (Val0->getType() != Val1->getType()) 2248176472Skmacy return Error(ID.Loc, "compare operands must have the same type"); 2249176472Skmacy 2250176472Skmacy CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal; 2251176472Skmacy 2252176472Skmacy if (Opc == Instruction::FCmp) { 2253176472Skmacy if (!Val0->getType()->isFPOrFPVectorTy()) 2254176472Skmacy return Error(ID.Loc, "fcmp requires floating point operands"); 2255167514Skmacy ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1); 2256167514Skmacy } else { 2257167514Skmacy assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!"); 2258167514Skmacy if (!Val0->getType()->isIntOrIntVectorTy() && 2259167514Skmacy !isa<PointerType>(Val0->getType())) 2260167514Skmacy return Error(ID.Loc, "icmp requires pointer or integer operands"); 2261167514Skmacy ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1); 2262167514Skmacy } 2263167514Skmacy ID.Kind = ValID::t_Constant; 2264167514Skmacy return false; 2265167514Skmacy } 2266167514Skmacy 2267167514Skmacy // Binary Operators. 2268167514Skmacy case lltok::kw_add: 2269167514Skmacy case lltok::kw_fadd: 2270167514Skmacy case lltok::kw_sub: 2271167514Skmacy case lltok::kw_fsub: 2272167514Skmacy case lltok::kw_mul: 2273167514Skmacy case lltok::kw_fmul: 2274167514Skmacy case lltok::kw_udiv: 2275167514Skmacy case lltok::kw_sdiv: 2276167514Skmacy case lltok::kw_fdiv: 2277167514Skmacy case lltok::kw_urem: 2278167514Skmacy case lltok::kw_srem: 2279167514Skmacy case lltok::kw_frem: { 2280167514Skmacy bool NUW = false; 2281167514Skmacy bool NSW = false; 2282167514Skmacy bool Exact = false; 2283167514Skmacy unsigned Opc = Lex.getUIntVal(); 2284167514Skmacy Constant *Val0, *Val1; 2285167514Skmacy Lex.Lex(); 2286167514Skmacy LocTy ModifierLoc = Lex.getLoc(); 2287167514Skmacy if (Opc == Instruction::Add || 2288167514Skmacy Opc == Instruction::Sub || 2289167514Skmacy Opc == Instruction::Mul) { 2290167514Skmacy if (EatIfPresent(lltok::kw_nuw)) 2291167514Skmacy NUW = true; 2292167514Skmacy if (EatIfPresent(lltok::kw_nsw)) { 2293167514Skmacy NSW = true; 2294167514Skmacy if (EatIfPresent(lltok::kw_nuw)) 2295167514Skmacy NUW = true; 2296167514Skmacy } 2297167514Skmacy } else if (Opc == Instruction::SDiv) { 2298167514Skmacy if (EatIfPresent(lltok::kw_exact)) 2299167514Skmacy Exact = true; 2300167514Skmacy } 2301167514Skmacy if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") || 2302167514Skmacy ParseGlobalTypeAndValue(Val0) || 2303167514Skmacy ParseToken(lltok::comma, "expected comma in binary constantexpr") || 2304167514Skmacy ParseGlobalTypeAndValue(Val1) || 2305167514Skmacy ParseToken(lltok::rparen, "expected ')' in binary constantexpr")) 2306167514Skmacy return true; 2307167514Skmacy if (Val0->getType() != Val1->getType()) 2308167514Skmacy return Error(ID.Loc, "operands of constexpr must have same type"); 2309167514Skmacy if (!Val0->getType()->isIntOrIntVectorTy()) { 2310167514Skmacy if (NUW) 2311167514Skmacy return Error(ModifierLoc, "nuw only applies to integer operations"); 2312167514Skmacy if (NSW) 2313167514Skmacy return Error(ModifierLoc, "nsw only applies to integer operations"); 2314167514Skmacy } 2315167514Skmacy // API compatibility: Accept either integer or floating-point types with 2316167514Skmacy // add, sub, and mul. 2317167514Skmacy if (!Val0->getType()->isIntOrIntVectorTy() && 2318167514Skmacy !Val0->getType()->isFPOrFPVectorTy()) 2319167514Skmacy return Error(ID.Loc,"constexpr requires integer, fp, or vector operands"); 2320167514Skmacy unsigned Flags = 0; 2321167514Skmacy if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap; 2322167514Skmacy if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap; 2323167514Skmacy if (Exact) Flags |= SDivOperator::IsExact; 2324167514Skmacy Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags); 2325167514Skmacy ID.ConstantVal = C; 2326167514Skmacy ID.Kind = ValID::t_Constant; 2327167514Skmacy return false; 2328167514Skmacy } 2329167514Skmacy 2330167514Skmacy // Logical Operations 2331167514Skmacy case lltok::kw_shl: 2332167514Skmacy case lltok::kw_lshr: 2333167514Skmacy case lltok::kw_ashr: 2334167514Skmacy case lltok::kw_and: 2335167514Skmacy case lltok::kw_or: 2336167514Skmacy case lltok::kw_xor: { 2337167514Skmacy unsigned Opc = Lex.getUIntVal(); 2338167514Skmacy Constant *Val0, *Val1; 2339167514Skmacy Lex.Lex(); 2340167514Skmacy if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") || 2341167514Skmacy ParseGlobalTypeAndValue(Val0) || 2342167514Skmacy ParseToken(lltok::comma, "expected comma in logical constantexpr") || 2343167514Skmacy ParseGlobalTypeAndValue(Val1) || 2344167514Skmacy ParseToken(lltok::rparen, "expected ')' in logical constantexpr")) 2345167514Skmacy return true; 2346167514Skmacy if (Val0->getType() != Val1->getType()) 2347167514Skmacy return Error(ID.Loc, "operands of constexpr must have same type"); 2348167514Skmacy if (!Val0->getType()->isIntOrIntVectorTy()) 2349167514Skmacy return Error(ID.Loc, 2350167514Skmacy "constexpr requires integer or integer vector operands"); 2351167514Skmacy ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1); 2352167514Skmacy ID.Kind = ValID::t_Constant; 2353167514Skmacy return false; 2354167514Skmacy } 2355167514Skmacy 2356167514Skmacy case lltok::kw_getelementptr: 2357167514Skmacy case lltok::kw_shufflevector: 2358167514Skmacy case lltok::kw_insertelement: 2359167514Skmacy case lltok::kw_extractelement: 2360167514Skmacy case lltok::kw_select: { 2361167514Skmacy unsigned Opc = Lex.getUIntVal(); 2362167514Skmacy SmallVector<Constant*, 16> Elts; 2363167514Skmacy bool InBounds = false; 2364167514Skmacy Lex.Lex(); 2365167514Skmacy if (Opc == Instruction::GetElementPtr) 2366167514Skmacy InBounds = EatIfPresent(lltok::kw_inbounds); 2367167514Skmacy if (ParseToken(lltok::lparen, "expected '(' in constantexpr") || 2368167514Skmacy ParseGlobalValueVector(Elts) || 2369167514Skmacy ParseToken(lltok::rparen, "expected ')' in constantexpr")) 2370167514Skmacy return true; 2371167514Skmacy 2372167514Skmacy if (Opc == Instruction::GetElementPtr) { 2373176472Skmacy if (Elts.size() == 0 || !isa<PointerType>(Elts[0]->getType())) 2374176472Skmacy return Error(ID.Loc, "getelementptr requires pointer operand"); 2375176472Skmacy 2376176472Skmacy if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), 2377167514Skmacy (Value**)(Elts.data() + 1), 2378167514Skmacy Elts.size() - 1)) 2379167514Skmacy return Error(ID.Loc, "invalid indices for getelementptr"); 2380167514Skmacy ID.ConstantVal = InBounds ? 2381176472Skmacy ConstantExpr::getInBoundsGetElementPtr(Elts[0], 2382167514Skmacy Elts.data() + 1, 2383176472Skmacy Elts.size() - 1) : 2384167514Skmacy ConstantExpr::getGetElementPtr(Elts[0], 2385167514Skmacy Elts.data() + 1, Elts.size() - 1); 2386167514Skmacy } else if (Opc == Instruction::Select) { 2387167514Skmacy if (Elts.size() != 3) 2388167514Skmacy return Error(ID.Loc, "expected three operands to select"); 2389167514Skmacy if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1], 2390167514Skmacy Elts[2])) 2391176472Skmacy return Error(ID.Loc, Reason); 2392176472Skmacy ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]); 2393176472Skmacy } else if (Opc == Instruction::ShuffleVector) { 2394176472Skmacy if (Elts.size() != 3) 2395167514Skmacy return Error(ID.Loc, "expected three operands to shufflevector"); 2396167514Skmacy if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2])) 2397167514Skmacy return Error(ID.Loc, "invalid operands to shufflevector"); 2398167514Skmacy ID.ConstantVal = 2399167514Skmacy ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]); 2400167514Skmacy } else if (Opc == Instruction::ExtractElement) { 2401167514Skmacy if (Elts.size() != 2) 2402167514Skmacy return Error(ID.Loc, "expected two operands to extractelement"); 2403167514Skmacy if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1])) 2404167514Skmacy return Error(ID.Loc, "invalid extractelement operands"); 2405167514Skmacy ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]); 2406167514Skmacy } else { 2407167514Skmacy assert(Opc == Instruction::InsertElement && "Unknown opcode"); 2408167514Skmacy if (Elts.size() != 3) 2409167514Skmacy return Error(ID.Loc, "expected three operands to insertelement"); 2410167514Skmacy if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2])) 2411167514Skmacy return Error(ID.Loc, "invalid insertelement operands"); 2412167514Skmacy ID.ConstantVal = 2413167514Skmacy ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]); 2414167514Skmacy } 2415167514Skmacy 2416167514Skmacy ID.Kind = ValID::t_Constant; 2417167514Skmacy return false; 2418167514Skmacy } 2419167514Skmacy } 2420167514Skmacy 2421167514Skmacy Lex.Lex(); 2422167514Skmacy return false; 2423167514Skmacy} 2424167514Skmacy 2425167514Skmacy/// ParseGlobalValue - Parse a global value with the specified type. 2426167514Skmacybool LLParser::ParseGlobalValue(const Type *Ty, Constant *&C) { 2427167514Skmacy C = 0; 2428167514Skmacy ValID ID; 2429167514Skmacy Value *V = NULL; 2430167514Skmacy bool Parsed = ParseValID(ID) || 2431167514Skmacy ConvertValIDToValue(Ty, ID, V, NULL); 2432167514Skmacy if (V && !(C = dyn_cast<Constant>(V))) 2433167514Skmacy return Error(ID.Loc, "global values must be constants"); 2434167514Skmacy return Parsed; 2435167514Skmacy} 2436167514Skmacy 2437176472Skmacybool LLParser::ParseGlobalTypeAndValue(Constant *&V) { 2438176472Skmacy PATypeHolder Type(Type::getVoidTy(Context)); 2439176472Skmacy return ParseType(Type) || 2440176472Skmacy ParseGlobalValue(Type, V); 2441167514Skmacy} 2442167514Skmacy 2443167514Skmacy/// ParseGlobalValueVector 2444176472Skmacy/// ::= /*empty*/ 2445167514Skmacy/// ::= TypeAndValue (',' TypeAndValue)* 2446176472Skmacybool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts) { 2447167514Skmacy // Empty list. 2448167514Skmacy if (Lex.getKind() == lltok::rbrace || 2449167514Skmacy Lex.getKind() == lltok::rsquare || 2450167514Skmacy Lex.getKind() == lltok::greater || 2451167514Skmacy Lex.getKind() == lltok::rparen) 2452167514Skmacy return false; 2453167514Skmacy 2454167514Skmacy Constant *C; 2455167514Skmacy if (ParseGlobalTypeAndValue(C)) return true; 2456167514Skmacy Elts.push_back(C); 2457176472Skmacy 2458167514Skmacy while (EatIfPresent(lltok::comma)) { 2459176472Skmacy if (ParseGlobalTypeAndValue(C)) return true; 2460167514Skmacy Elts.push_back(C); 2461167514Skmacy } 2462167514Skmacy 2463167514Skmacy return false; 2464167514Skmacy} 2465167514Skmacy 2466167514Skmacy 2467167514Skmacy//===----------------------------------------------------------------------===// 2468167514Skmacy// Function Parsing. 2469167514Skmacy//===----------------------------------------------------------------------===// 2470167514Skmacy 2471167514Skmacybool LLParser::ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V, 2472167514Skmacy PerFunctionState *PFS) { 2473167514Skmacy if (isa<FunctionType>(Ty)) 2474167514Skmacy return Error(ID.Loc, "functions are not values, refer to them as pointers"); 2475167514Skmacy 2476167514Skmacy switch (ID.Kind) { 2477167514Skmacy default: llvm_unreachable("Unknown ValID!"); 2478167514Skmacy case ValID::t_LocalID: 2479167514Skmacy if (!PFS) return Error(ID.Loc, "invalid use of function-local name"); 2480167514Skmacy V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc); 2481167514Skmacy return (V == 0); 2482167514Skmacy case ValID::t_LocalName: 2483167514Skmacy if (!PFS) return Error(ID.Loc, "invalid use of function-local name"); 2484167514Skmacy V = PFS->GetVal(ID.StrVal, Ty, ID.Loc); 2485167514Skmacy return (V == 0); 2486167514Skmacy case ValID::t_InlineAsm: { 2487167514Skmacy const PointerType *PTy = dyn_cast<PointerType>(Ty); 2488167514Skmacy const FunctionType *FTy = 2489167514Skmacy PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0; 2490167514Skmacy if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2)) 2491167514Skmacy return Error(ID.Loc, "invalid type for inline asm constraint string"); 2492167514Skmacy V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1, ID.UIntVal>>1); 2493167514Skmacy return false; 2494167514Skmacy } 2495167514Skmacy case ValID::t_MDNode: 2496167514Skmacy if (!Ty->isMetadataTy()) 2497167514Skmacy return Error(ID.Loc, "metadata value must have metadata type"); 2498167514Skmacy V = ID.MDNodeVal; 2499167514Skmacy return false; 2500167514Skmacy case ValID::t_MDString: 2501167514Skmacy if (!Ty->isMetadataTy()) 2502167514Skmacy return Error(ID.Loc, "metadata value must have metadata type"); 2503167514Skmacy V = ID.MDStringVal; 2504167514Skmacy return false; 2505167514Skmacy case ValID::t_GlobalName: 2506167514Skmacy V = GetGlobalVal(ID.StrVal, Ty, ID.Loc); 2507167514Skmacy return V == 0; 2508167514Skmacy case ValID::t_GlobalID: 2509167514Skmacy V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc); 2510167514Skmacy return V == 0; 2511167514Skmacy case ValID::t_APSInt: 2512167514Skmacy if (!isa<IntegerType>(Ty)) 2513167514Skmacy return Error(ID.Loc, "integer constant must have integer type"); 2514167514Skmacy ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits()); 2515167514Skmacy V = ConstantInt::get(Context, ID.APSIntVal); 2516167514Skmacy return false; 2517167514Skmacy case ValID::t_APFloat: 2518167514Skmacy if (!Ty->isFloatingPointTy() || 2519167514Skmacy !ConstantFP::isValueValidForType(Ty, ID.APFloatVal)) 2520167514Skmacy return Error(ID.Loc, "floating point constant invalid for type"); 2521167514Skmacy 2522167514Skmacy // The lexer has no type info, so builds all float and double FP constants 2523167514Skmacy // as double. Fix this here. Long double does not need this. 2524167514Skmacy if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble && 2525167514Skmacy Ty->isFloatTy()) { 2526167514Skmacy bool Ignored; 2527167514Skmacy ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, 2528167514Skmacy &Ignored); 2529167514Skmacy } 2530167514Skmacy V = ConstantFP::get(Context, ID.APFloatVal); 2531167514Skmacy 2532167514Skmacy if (V->getType() != Ty) 2533167514Skmacy return Error(ID.Loc, "floating point constant does not have type '" + 2534167514Skmacy Ty->getDescription() + "'"); 2535176472Skmacy 2536176472Skmacy return false; 2537176472Skmacy case ValID::t_Null: 2538176472Skmacy if (!isa<PointerType>(Ty)) 2539167514Skmacy return Error(ID.Loc, "null must be a pointer type"); 2540167514Skmacy V = ConstantPointerNull::get(cast<PointerType>(Ty)); 2541167514Skmacy return false; 2542167514Skmacy case ValID::t_Undef: 2543167514Skmacy // FIXME: LabelTy should not be a first-class type. 2544167514Skmacy if ((!Ty->isFirstClassType() || Ty->isLabelTy()) && 2545167514Skmacy !isa<OpaqueType>(Ty)) 2546167514Skmacy return Error(ID.Loc, "invalid type for undef constant"); 2547167514Skmacy V = UndefValue::get(Ty); 2548167514Skmacy return false; 2549167514Skmacy case ValID::t_EmptyArray: 2550176472Skmacy if (!isa<ArrayType>(Ty) || cast<ArrayType>(Ty)->getNumElements() != 0) 2551167514Skmacy return Error(ID.Loc, "invalid empty array initializer"); 2552176472Skmacy V = UndefValue::get(Ty); 2553167514Skmacy return false; 2554167514Skmacy case ValID::t_Zero: 2555176472Skmacy // FIXME: LabelTy should not be a first-class type. 2556167514Skmacy if (!Ty->isFirstClassType() || Ty->isLabelTy()) 2557176472Skmacy return Error(ID.Loc, "invalid type for null constant"); 2558167514Skmacy V = Constant::getNullValue(Ty); 2559167514Skmacy return false; 2560176472Skmacy case ValID::t_Constant: 2561167514Skmacy if (ID.ConstantVal->getType() != Ty) { 2562176472Skmacy // Allow a constant struct with a single member to be converted 2563167514Skmacy // to a union, if the union has a member which is the same type 2564167514Skmacy // as the struct member. 2565176472Skmacy if (const UnionType* utype = dyn_cast<UnionType>(Ty)) { 2566167514Skmacy return ParseUnionValue(utype, ID, V); 2567176472Skmacy } 2568167514Skmacy 2569167514Skmacy return Error(ID.Loc, "constant expression type mismatch"); 2570167514Skmacy } 2571167514Skmacy 2572167514Skmacy V = ID.ConstantVal; 2573167514Skmacy return false; 2574167514Skmacy } 2575167514Skmacy} 2576167514Skmacy 2577167514Skmacybool LLParser::ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS) { 2578167514Skmacy V = 0; 2579167514Skmacy ValID ID; 2580167514Skmacy return ParseValID(ID, &PFS) || 2581167514Skmacy ConvertValIDToValue(Ty, ID, V, &PFS); 2582167514Skmacy} 2583167514Skmacy 2584167514Skmacybool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState &PFS) { 2585167514Skmacy PATypeHolder T(Type::getVoidTy(Context)); 2586167514Skmacy return ParseType(T) || 2587167514Skmacy ParseValue(T, V, PFS); 2588167514Skmacy} 2589167514Skmacy 2590167514Skmacybool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc, 2591167514Skmacy PerFunctionState &PFS) { 2592167514Skmacy Value *V; 2593167514Skmacy Loc = Lex.getLoc(); 2594167514Skmacy if (ParseTypeAndValue(V, PFS)) return true; 2595167514Skmacy if (!isa<BasicBlock>(V)) 2596167514Skmacy return Error(Loc, "expected a basic block"); 2597167514Skmacy BB = cast<BasicBlock>(V); 2598167514Skmacy return false; 2599167514Skmacy} 2600167514Skmacy 2601167514Skmacybool LLParser::ParseUnionValue(const UnionType* utype, ValID &ID, Value *&V) { 2602167514Skmacy if (const StructType* stype = dyn_cast<StructType>(ID.ConstantVal->getType())) { 2603167514Skmacy if (stype->getNumContainedTypes() != 1) 2604167514Skmacy return Error(ID.Loc, "constant expression type mismatch"); 2605167514Skmacy int index = utype->getElementTypeIndex(stype->getContainedType(0)); 2606167514Skmacy if (index < 0) 2607167514Skmacy return Error(ID.Loc, "initializer type is not a member of the union"); 2608167514Skmacy 2609167514Skmacy V = ConstantUnion::get( 2610167514Skmacy utype, cast<Constant>(ID.ConstantVal->getOperand(0))); 2611167514Skmacy return false; 2612167514Skmacy } 2613167514Skmacy 2614167514Skmacy return Error(ID.Loc, "constant expression type mismatch"); 2615167514Skmacy} 2616167514Skmacy 2617167514Skmacy 2618167514Skmacy/// FunctionHeader 2619167514Skmacy/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs 2620167514Skmacy/// Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection 2621167514Skmacy/// OptionalAlign OptGC 2622167514Skmacybool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) { 2623167514Skmacy // Parse the linkage. 2624167514Skmacy LocTy LinkageLoc = Lex.getLoc(); 2625167514Skmacy unsigned Linkage; 2626167514Skmacy 2627167514Skmacy unsigned Visibility, RetAttrs; 2628167514Skmacy CallingConv::ID CC; 2629167514Skmacy PATypeHolder RetType(Type::getVoidTy(Context)); 2630167514Skmacy LocTy RetTypeLoc = Lex.getLoc(); 2631167514Skmacy if (ParseOptionalLinkage(Linkage) || 2632167514Skmacy ParseOptionalVisibility(Visibility) || 2633167514Skmacy ParseOptionalCallingConv(CC) || 2634167514Skmacy ParseOptionalAttrs(RetAttrs, 1) || 2635167514Skmacy ParseType(RetType, RetTypeLoc, true /*void allowed*/)) 2636167514Skmacy return true; 2637167514Skmacy 2638167514Skmacy // Verify that the linkage is ok. 2639167514Skmacy switch ((GlobalValue::LinkageTypes)Linkage) { 2640167514Skmacy case GlobalValue::ExternalLinkage: 2641167514Skmacy break; // always ok. 2642167514Skmacy case GlobalValue::DLLImportLinkage: 2643167514Skmacy case GlobalValue::ExternalWeakLinkage: 2644167514Skmacy if (isDefine) 2645167514Skmacy return Error(LinkageLoc, "invalid linkage for function definition"); 2646167514Skmacy break; 2647167514Skmacy case GlobalValue::PrivateLinkage: 2648167514Skmacy case GlobalValue::LinkerPrivateLinkage: 2649167514Skmacy case GlobalValue::InternalLinkage: 2650167514Skmacy case GlobalValue::AvailableExternallyLinkage: 2651167514Skmacy case GlobalValue::LinkOnceAnyLinkage: 2652167514Skmacy case GlobalValue::LinkOnceODRLinkage: 2653167514Skmacy case GlobalValue::WeakAnyLinkage: 2654167514Skmacy case GlobalValue::WeakODRLinkage: 2655167514Skmacy case GlobalValue::DLLExportLinkage: 2656167514Skmacy if (!isDefine) 2657167514Skmacy return Error(LinkageLoc, "invalid linkage for function declaration"); 2658167514Skmacy break; 2659167514Skmacy case GlobalValue::AppendingLinkage: 2660167514Skmacy case GlobalValue::CommonLinkage: 2661167514Skmacy return Error(LinkageLoc, "invalid function linkage type"); 2662167514Skmacy } 2663167514Skmacy 2664167514Skmacy if (!FunctionType::isValidReturnType(RetType) || 2665167514Skmacy isa<OpaqueType>(RetType)) 2666167514Skmacy return Error(RetTypeLoc, "invalid function return type"); 2667167514Skmacy 2668167514Skmacy LocTy NameLoc = Lex.getLoc(); 2669167514Skmacy 2670167514Skmacy std::string FunctionName; 2671167514Skmacy if (Lex.getKind() == lltok::GlobalVar) { 2672167514Skmacy FunctionName = Lex.getStrVal(); 2673167514Skmacy } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok. 2674167514Skmacy unsigned NameID = Lex.getUIntVal(); 2675167514Skmacy 2676167514Skmacy if (NameID != NumberedVals.size()) 2677167514Skmacy return TokError("function expected to be numbered '%" + 2678167514Skmacy utostr(NumberedVals.size()) + "'"); 2679167514Skmacy } else { 2680167514Skmacy return TokError("expected function name"); 2681167514Skmacy } 2682167514Skmacy 2683167514Skmacy Lex.Lex(); 2684167514Skmacy 2685167514Skmacy if (Lex.getKind() != lltok::lparen) 2686167514Skmacy return TokError("expected '(' in function argument list"); 2687167514Skmacy 2688167514Skmacy std::vector<ArgInfo> ArgList; 2689167514Skmacy bool isVarArg; 2690167514Skmacy unsigned FuncAttrs; 2691167514Skmacy std::string Section; 2692167514Skmacy unsigned Alignment; 2693167514Skmacy std::string GC; 2694167514Skmacy 2695167514Skmacy if (ParseArgumentList(ArgList, isVarArg, false) || 2696167514Skmacy ParseOptionalAttrs(FuncAttrs, 2) || 2697167514Skmacy (EatIfPresent(lltok::kw_section) && 2698167514Skmacy ParseStringConstant(Section)) || 2699167514Skmacy ParseOptionalAlignment(Alignment) || 2700167514Skmacy (EatIfPresent(lltok::kw_gc) && 2701167514Skmacy ParseStringConstant(GC))) 2702167514Skmacy return true; 2703167514Skmacy 2704167514Skmacy // If the alignment was parsed as an attribute, move to the alignment field. 2705167514Skmacy if (FuncAttrs & Attribute::Alignment) { 2706167514Skmacy Alignment = Attribute::getAlignmentFromAttrs(FuncAttrs); 2707167514Skmacy FuncAttrs &= ~Attribute::Alignment; 2708167514Skmacy } 2709167514Skmacy 2710167514Skmacy // Okay, if we got here, the function is syntactically valid. Convert types 2711167514Skmacy // and do semantic checks. 2712167514Skmacy std::vector<const Type*> ParamTypeList; 2713167514Skmacy SmallVector<AttributeWithIndex, 8> Attrs; 2714167514Skmacy // FIXME : In 3.0, stop accepting zext, sext and inreg as optional function 2715167514Skmacy // attributes. 2716167514Skmacy unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg; 2717167514Skmacy if (FuncAttrs & ObsoleteFuncAttrs) { 2718167514Skmacy RetAttrs |= FuncAttrs & ObsoleteFuncAttrs; 2719167514Skmacy FuncAttrs &= ~ObsoleteFuncAttrs; 2720167514Skmacy } 2721167514Skmacy 2722167514Skmacy if (RetAttrs != Attribute::None) 2723167514Skmacy Attrs.push_back(AttributeWithIndex::get(0, RetAttrs)); 2724167514Skmacy 2725167514Skmacy for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { 2726167514Skmacy ParamTypeList.push_back(ArgList[i].Type); 2727167514Skmacy if (ArgList[i].Attrs != Attribute::None) 2728167514Skmacy Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs)); 2729167514Skmacy } 2730167514Skmacy 2731167514Skmacy if (FuncAttrs != Attribute::None) 2732167514Skmacy Attrs.push_back(AttributeWithIndex::get(~0, FuncAttrs)); 2733167514Skmacy 2734167514Skmacy AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end()); 2735167514Skmacy 2736167514Skmacy if (PAL.paramHasAttr(1, Attribute::StructRet) && !RetType->isVoidTy()) 2737167514Skmacy return Error(RetTypeLoc, "functions with 'sret' argument must return void"); 2738167514Skmacy 2739167514Skmacy const FunctionType *FT = 2740167514Skmacy FunctionType::get(RetType, ParamTypeList, isVarArg); 2741167514Skmacy const PointerType *PFT = PointerType::getUnqual(FT); 2742167514Skmacy 2743167514Skmacy Fn = 0; 2744167514Skmacy if (!FunctionName.empty()) { 2745167514Skmacy // If this was a definition of a forward reference, remove the definition 2746167514Skmacy // from the forward reference table and fill in the forward ref. 2747167514Skmacy std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI = 2748167514Skmacy ForwardRefVals.find(FunctionName); 2749167514Skmacy if (FRVI != ForwardRefVals.end()) { 2750167514Skmacy Fn = M->getFunction(FunctionName); 2751167514Skmacy ForwardRefVals.erase(FRVI); 2752167514Skmacy } else if ((Fn = M->getFunction(FunctionName))) { 2753167514Skmacy // If this function already exists in the symbol table, then it is 2754167514Skmacy // multiply defined. We accept a few cases for old backwards compat. 2755167514Skmacy // FIXME: Remove this stuff for LLVM 3.0. 2756167514Skmacy if (Fn->getType() != PFT || Fn->getAttributes() != PAL || 2757167514Skmacy (!Fn->isDeclaration() && isDefine)) { 2758167514Skmacy // If the redefinition has different type or different attributes, 2759167514Skmacy // reject it. If both have bodies, reject it. 2760167514Skmacy return Error(NameLoc, "invalid redefinition of function '" + 2761167514Skmacy FunctionName + "'"); 2762167514Skmacy } else if (Fn->isDeclaration()) { 2763167514Skmacy // Make sure to strip off any argument names so we can't get conflicts. 2764167514Skmacy for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end(); 2765167514Skmacy AI != AE; ++AI) 2766167514Skmacy AI->setName(""); 2767167514Skmacy } 2768167514Skmacy } else if (M->getNamedValue(FunctionName)) { 2769167514Skmacy return Error(NameLoc, "redefinition of function '@" + FunctionName + "'"); 2770167514Skmacy } 2771167514Skmacy 2772167514Skmacy } else { 2773167514Skmacy // If this is a definition of a forward referenced function, make sure the 2774167514Skmacy // types agree. 2775167514Skmacy std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I 2776167514Skmacy = ForwardRefValIDs.find(NumberedVals.size()); 2777167514Skmacy if (I != ForwardRefValIDs.end()) { 2778167514Skmacy Fn = cast<Function>(I->second.first); 2779167514Skmacy if (Fn->getType() != PFT) 2780167514Skmacy return Error(NameLoc, "type of definition and forward reference of '@" + 2781167514Skmacy utostr(NumberedVals.size()) +"' disagree"); 2782167514Skmacy ForwardRefValIDs.erase(I); 2783167514Skmacy } 2784167514Skmacy } 2785167514Skmacy 2786167514Skmacy if (Fn == 0) 2787167514Skmacy Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M); 2788167514Skmacy else // Move the forward-reference to the correct spot in the module. 2789167514Skmacy M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn); 2790167514Skmacy 2791167514Skmacy if (FunctionName.empty()) 2792167514Skmacy NumberedVals.push_back(Fn); 2793167514Skmacy 2794167514Skmacy Fn->setLinkage((GlobalValue::LinkageTypes)Linkage); 2795167514Skmacy Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility); 2796167514Skmacy Fn->setCallingConv(CC); 2797167514Skmacy Fn->setAttributes(PAL); 2798167514Skmacy Fn->setAlignment(Alignment); 2799167514Skmacy Fn->setSection(Section); 2800167514Skmacy if (!GC.empty()) Fn->setGC(GC.c_str()); 2801167514Skmacy 2802167514Skmacy // Add all of the arguments we parsed to the function. 2803167514Skmacy Function::arg_iterator ArgIt = Fn->arg_begin(); 2804167514Skmacy for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) { 2805167514Skmacy // If we run out of arguments in the Function prototype, exit early. 2806167514Skmacy // FIXME: REMOVE THIS IN LLVM 3.0, this is just for the mismatch case above. 2807167514Skmacy if (ArgIt == Fn->arg_end()) break; 2808167514Skmacy 2809167514Skmacy // If the argument has a name, insert it into the argument symbol table. 2810167514Skmacy if (ArgList[i].Name.empty()) continue; 2811167514Skmacy 2812167514Skmacy // Set the name, if it conflicted, it will be auto-renamed. 2813167514Skmacy ArgIt->setName(ArgList[i].Name); 2814167514Skmacy 2815167514Skmacy if (ArgIt->getNameStr() != ArgList[i].Name) 2816167514Skmacy return Error(ArgList[i].Loc, "redefinition of argument '%" + 2817167514Skmacy ArgList[i].Name + "'"); 2818167514Skmacy } 2819167514Skmacy 2820167514Skmacy return false; 2821167514Skmacy} 2822167514Skmacy 2823167514Skmacy 2824167514Skmacy/// ParseFunctionBody 2825167514Skmacy/// ::= '{' BasicBlock+ '}' 2826167514Skmacy/// ::= 'begin' BasicBlock+ 'end' // FIXME: remove in LLVM 3.0 2827167514Skmacy/// 2828167514Skmacybool LLParser::ParseFunctionBody(Function &Fn) { 2829167514Skmacy if (Lex.getKind() != lltok::lbrace && Lex.getKind() != lltok::kw_begin) 2830167514Skmacy return TokError("expected '{' in function body"); 2831167514Skmacy Lex.Lex(); // eat the {. 2832167514Skmacy 2833167514Skmacy int FunctionNumber = -1; 2834167514Skmacy if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1; 2835167514Skmacy 2836167514Skmacy PerFunctionState PFS(*this, Fn, FunctionNumber); 2837167514Skmacy 2838167514Skmacy // We need at least one basic block. 2839167514Skmacy if (Lex.getKind() == lltok::rbrace || Lex.getKind() == lltok::kw_end) 2840167514Skmacy return TokError("function body requires at least one basic block"); 2841167514Skmacy 2842167514Skmacy while (Lex.getKind() != lltok::rbrace && Lex.getKind() != lltok::kw_end) 2843167514Skmacy if (ParseBasicBlock(PFS)) return true; 2844167514Skmacy 2845167514Skmacy // Eat the }. 2846167514Skmacy Lex.Lex(); 2847167514Skmacy 2848167514Skmacy // Verify function is ok. 2849167514Skmacy return PFS.FinishFunction(); 2850167514Skmacy} 2851167514Skmacy 2852167514Skmacy/// ParseBasicBlock 2853167514Skmacy/// ::= LabelStr? Instruction* 2854167514Skmacybool LLParser::ParseBasicBlock(PerFunctionState &PFS) { 2855167514Skmacy // If this basic block starts out with a name, remember it. 2856167514Skmacy std::string Name; 2857167514Skmacy LocTy NameLoc = Lex.getLoc(); 2858167514Skmacy if (Lex.getKind() == lltok::LabelStr) { 2859167514Skmacy Name = Lex.getStrVal(); 2860167514Skmacy Lex.Lex(); 2861167514Skmacy } 2862167514Skmacy 2863167514Skmacy BasicBlock *BB = PFS.DefineBB(Name, NameLoc); 2864167514Skmacy if (BB == 0) return true; 2865167514Skmacy 2866167514Skmacy std::string NameStr; 2867167514Skmacy 2868167514Skmacy // Parse the instructions in this block until we get a terminator. 2869167514Skmacy Instruction *Inst; 2870167514Skmacy SmallVector<std::pair<unsigned, MDNode *>, 4> MetadataOnInst; 2871167514Skmacy do { 2872167514Skmacy // This instruction may have three possibilities for a name: a) none 2873167514Skmacy // specified, b) name specified "%foo =", c) number specified: "%4 =". 2874167514Skmacy LocTy NameLoc = Lex.getLoc(); 2875167514Skmacy int NameID = -1; 2876167514Skmacy NameStr = ""; 2877167514Skmacy 2878167514Skmacy if (Lex.getKind() == lltok::LocalVarID) { 2879167514Skmacy NameID = Lex.getUIntVal(); 2880167514Skmacy Lex.Lex(); 2881167514Skmacy if (ParseToken(lltok::equal, "expected '=' after instruction id")) 2882167514Skmacy return true; 2883167514Skmacy } else if (Lex.getKind() == lltok::LocalVar || 2884167514Skmacy // FIXME: REMOVE IN LLVM 3.0 2885167514Skmacy Lex.getKind() == lltok::StringConstant) { 2886167514Skmacy NameStr = Lex.getStrVal(); 2887167514Skmacy Lex.Lex(); 2888167514Skmacy if (ParseToken(lltok::equal, "expected '=' after instruction name")) 2889167514Skmacy return true; 2890167514Skmacy } 2891167514Skmacy 2892167514Skmacy switch (ParseInstruction(Inst, BB, PFS)) { 2893167514Skmacy default: assert(0 && "Unknown ParseInstruction result!"); 2894167514Skmacy case InstError: return true; 2895167514Skmacy case InstNormal: 2896167514Skmacy // With a normal result, we check to see if the instruction is followed by 2897167514Skmacy // a comma and metadata. 2898167514Skmacy if (EatIfPresent(lltok::comma)) 2899167514Skmacy if (ParseInstructionMetadata(MetadataOnInst)) 2900167514Skmacy return true; 2901167514Skmacy break; 2902167514Skmacy case InstExtraComma: 2903167514Skmacy // If the instruction parser ate an extra comma at the end of it, it 2904167514Skmacy // *must* be followed by metadata. 2905167514Skmacy if (ParseInstructionMetadata(MetadataOnInst)) 2906167514Skmacy return true; 2907167514Skmacy break; 2908167514Skmacy } 2909167514Skmacy 2910167514Skmacy // Set metadata attached with this instruction. 2911167514Skmacy for (unsigned i = 0, e = MetadataOnInst.size(); i != e; ++i) 2912167514Skmacy Inst->setMetadata(MetadataOnInst[i].first, MetadataOnInst[i].second); 2913167514Skmacy MetadataOnInst.clear(); 2914167514Skmacy 2915167514Skmacy BB->getInstList().push_back(Inst); 2916167514Skmacy 2917167514Skmacy // Set the name on the instruction. 2918167514Skmacy if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true; 2919167514Skmacy } while (!isa<TerminatorInst>(Inst)); 2920167514Skmacy 2921167514Skmacy return false; 2922167514Skmacy} 2923167514Skmacy 2924167514Skmacy//===----------------------------------------------------------------------===// 2925167514Skmacy// Instruction Parsing. 2926167514Skmacy//===----------------------------------------------------------------------===// 2927167514Skmacy 2928167514Skmacy/// ParseInstruction - Parse one of the many different instructions. 2929167514Skmacy/// 2930167514Skmacyint LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB, 2931167514Skmacy PerFunctionState &PFS) { 2932167514Skmacy lltok::Kind Token = Lex.getKind(); 2933167514Skmacy if (Token == lltok::Eof) 2934167514Skmacy return TokError("found end of file when expecting more instructions"); 2935167514Skmacy LocTy Loc = Lex.getLoc(); 2936167514Skmacy unsigned KeywordVal = Lex.getUIntVal(); 2937167514Skmacy Lex.Lex(); // Eat the keyword. 2938167514Skmacy 2939167514Skmacy switch (Token) { 2940167514Skmacy default: return Error(Loc, "expected instruction opcode"); 2941167514Skmacy // Terminator Instructions. 2942167514Skmacy case lltok::kw_unwind: Inst = new UnwindInst(Context); return false; 2943167514Skmacy case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false; 2944167514Skmacy case lltok::kw_ret: return ParseRet(Inst, BB, PFS); 2945167514Skmacy case lltok::kw_br: return ParseBr(Inst, PFS); 2946167514Skmacy case lltok::kw_switch: return ParseSwitch(Inst, PFS); 2947167514Skmacy case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS); 2948167514Skmacy case lltok::kw_invoke: return ParseInvoke(Inst, PFS); 2949167514Skmacy // Binary Operators. 2950167514Skmacy case lltok::kw_add: 2951167514Skmacy case lltok::kw_sub: 2952167514Skmacy case lltok::kw_mul: { 2953167514Skmacy bool NUW = false; 2954167514Skmacy bool NSW = false; 2955167514Skmacy LocTy ModifierLoc = Lex.getLoc(); 2956167514Skmacy if (EatIfPresent(lltok::kw_nuw)) 2957167514Skmacy NUW = true; 2958167514Skmacy if (EatIfPresent(lltok::kw_nsw)) { 2959167514Skmacy NSW = true; 2960167514Skmacy if (EatIfPresent(lltok::kw_nuw)) 2961167514Skmacy NUW = true; 2962167514Skmacy } 2963167514Skmacy // API compatibility: Accept either integer or floating-point types. 2964167514Skmacy bool Result = ParseArithmetic(Inst, PFS, KeywordVal, 0); 2965167514Skmacy if (!Result) { 2966167514Skmacy if (!Inst->getType()->isIntOrIntVectorTy()) { 2967167514Skmacy if (NUW) 2968167514Skmacy return Error(ModifierLoc, "nuw only applies to integer operations"); 2969167514Skmacy if (NSW) 2970167514Skmacy return Error(ModifierLoc, "nsw only applies to integer operations"); 2971167514Skmacy } 2972167514Skmacy if (NUW) 2973167514Skmacy cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true); 2974167514Skmacy if (NSW) 2975167514Skmacy cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true); 2976167514Skmacy } 2977167514Skmacy return Result; 2978167514Skmacy } 2979167514Skmacy case lltok::kw_fadd: 2980167514Skmacy case lltok::kw_fsub: 2981167514Skmacy case lltok::kw_fmul: return ParseArithmetic(Inst, PFS, KeywordVal, 2); 2982167514Skmacy 2983167514Skmacy case lltok::kw_sdiv: { 2984167514Skmacy bool Exact = false; 2985167514Skmacy if (EatIfPresent(lltok::kw_exact)) 2986167514Skmacy Exact = true; 2987167514Skmacy bool Result = ParseArithmetic(Inst, PFS, KeywordVal, 1); 2988167514Skmacy if (!Result) 2989167514Skmacy if (Exact) 2990167514Skmacy cast<BinaryOperator>(Inst)->setIsExact(true); 2991167514Skmacy return Result; 2992167514Skmacy } 2993167514Skmacy 2994167514Skmacy case lltok::kw_udiv: 2995167514Skmacy case lltok::kw_urem: 2996167514Skmacy case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1); 2997167514Skmacy case lltok::kw_fdiv: 2998167514Skmacy case lltok::kw_frem: return ParseArithmetic(Inst, PFS, KeywordVal, 2); 2999167514Skmacy case lltok::kw_shl: 3000167514Skmacy case lltok::kw_lshr: 3001167514Skmacy case lltok::kw_ashr: 3002167514Skmacy case lltok::kw_and: 3003167514Skmacy case lltok::kw_or: 3004167514Skmacy case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal); 3005167514Skmacy case lltok::kw_icmp: 3006167514Skmacy case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal); 3007167514Skmacy // Casts. 3008167514Skmacy case lltok::kw_trunc: 3009167514Skmacy case lltok::kw_zext: 3010167514Skmacy case lltok::kw_sext: 3011167514Skmacy case lltok::kw_fptrunc: 3012167514Skmacy case lltok::kw_fpext: 3013167514Skmacy case lltok::kw_bitcast: 3014167514Skmacy case lltok::kw_uitofp: 3015167514Skmacy case lltok::kw_sitofp: 3016167514Skmacy case lltok::kw_fptoui: 3017167514Skmacy case lltok::kw_fptosi: 3018167514Skmacy case lltok::kw_inttoptr: 3019167514Skmacy case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal); 3020167514Skmacy // Other. 3021167514Skmacy case lltok::kw_select: return ParseSelect(Inst, PFS); 3022167514Skmacy case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS); 3023167514Skmacy case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS); 3024167514Skmacy case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS); 3025167514Skmacy case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS); 3026167514Skmacy case lltok::kw_phi: return ParsePHI(Inst, PFS); 3027167514Skmacy case lltok::kw_call: return ParseCall(Inst, PFS, false); 3028167514Skmacy case lltok::kw_tail: return ParseCall(Inst, PFS, true); 3029167514Skmacy // Memory. 3030167514Skmacy case lltok::kw_alloca: return ParseAlloc(Inst, PFS); 3031167514Skmacy case lltok::kw_malloc: return ParseAlloc(Inst, PFS, BB, false); 3032167514Skmacy case lltok::kw_free: return ParseFree(Inst, PFS, BB); 3033167514Skmacy case lltok::kw_load: return ParseLoad(Inst, PFS, false); 3034167514Skmacy case lltok::kw_store: return ParseStore(Inst, PFS, false); 3035167514Skmacy case lltok::kw_volatile: 3036167514Skmacy if (EatIfPresent(lltok::kw_load)) 3037167514Skmacy return ParseLoad(Inst, PFS, true); 3038167514Skmacy else if (EatIfPresent(lltok::kw_store)) 3039167514Skmacy return ParseStore(Inst, PFS, true); 3040167514Skmacy else 3041167514Skmacy return TokError("expected 'load' or 'store'"); 3042167514Skmacy case lltok::kw_getresult: return ParseGetResult(Inst, PFS); 3043167514Skmacy case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS); 3044167514Skmacy case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS); 3045167514Skmacy case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS); 3046167514Skmacy } 3047167514Skmacy} 3048167514Skmacy 3049167514Skmacy/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind. 3050167514Skmacybool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) { 3051167514Skmacy if (Opc == Instruction::FCmp) { 3052167514Skmacy switch (Lex.getKind()) { 3053167514Skmacy default: TokError("expected fcmp predicate (e.g. 'oeq')"); 3054167514Skmacy case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break; 3055167514Skmacy case lltok::kw_one: P = CmpInst::FCMP_ONE; break; 3056167514Skmacy case lltok::kw_olt: P = CmpInst::FCMP_OLT; break; 3057167514Skmacy case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break; 3058167514Skmacy case lltok::kw_ole: P = CmpInst::FCMP_OLE; break; 3059167514Skmacy case lltok::kw_oge: P = CmpInst::FCMP_OGE; break; 3060167514Skmacy case lltok::kw_ord: P = CmpInst::FCMP_ORD; break; 3061167514Skmacy case lltok::kw_uno: P = CmpInst::FCMP_UNO; break; 3062167514Skmacy case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break; 3063167514Skmacy case lltok::kw_une: P = CmpInst::FCMP_UNE; break; 3064167514Skmacy case lltok::kw_ult: P = CmpInst::FCMP_ULT; break; 3065167514Skmacy case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break; 3066167514Skmacy case lltok::kw_ule: P = CmpInst::FCMP_ULE; break; 3067167514Skmacy case lltok::kw_uge: P = CmpInst::FCMP_UGE; break; 3068167514Skmacy case lltok::kw_true: P = CmpInst::FCMP_TRUE; break; 3069167514Skmacy case lltok::kw_false: P = CmpInst::FCMP_FALSE; break; 3070167514Skmacy } 3071167514Skmacy } else { 3072167514Skmacy switch (Lex.getKind()) { 3073167514Skmacy default: TokError("expected icmp predicate (e.g. 'eq')"); 3074167514Skmacy case lltok::kw_eq: P = CmpInst::ICMP_EQ; break; 3075167514Skmacy case lltok::kw_ne: P = CmpInst::ICMP_NE; break; 3076167514Skmacy case lltok::kw_slt: P = CmpInst::ICMP_SLT; break; 3077167514Skmacy case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break; 3078167514Skmacy case lltok::kw_sle: P = CmpInst::ICMP_SLE; break; 3079167514Skmacy case lltok::kw_sge: P = CmpInst::ICMP_SGE; break; 3080167514Skmacy case lltok::kw_ult: P = CmpInst::ICMP_ULT; break; 3081167514Skmacy case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break; 3082167514Skmacy case lltok::kw_ule: P = CmpInst::ICMP_ULE; break; 3083167514Skmacy case lltok::kw_uge: P = CmpInst::ICMP_UGE; break; 3084167514Skmacy } 3085167514Skmacy } 3086167514Skmacy Lex.Lex(); 3087167514Skmacy return false; 3088167514Skmacy} 3089167514Skmacy 3090167514Skmacy//===----------------------------------------------------------------------===// 3091167514Skmacy// Terminator Instructions. 3092167514Skmacy//===----------------------------------------------------------------------===// 3093167514Skmacy 3094167514Skmacy/// ParseRet - Parse a return instruction. 3095167514Skmacy/// ::= 'ret' void (',' !dbg, !1)* 3096167514Skmacy/// ::= 'ret' TypeAndValue (',' !dbg, !1)* 3097167514Skmacy/// ::= 'ret' TypeAndValue (',' TypeAndValue)+ (',' !dbg, !1)* 3098167514Skmacy/// [[obsolete: LLVM 3.0]] 3099167514Skmacyint LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB, 3100167514Skmacy PerFunctionState &PFS) { 3101167514Skmacy PATypeHolder Ty(Type::getVoidTy(Context)); 3102167514Skmacy if (ParseType(Ty, true /*void allowed*/)) return true; 3103167514Skmacy 3104167514Skmacy if (Ty->isVoidTy()) { 3105167514Skmacy Inst = ReturnInst::Create(Context); 3106167514Skmacy return false; 3107167514Skmacy } 3108167514Skmacy 3109167514Skmacy Value *RV; 3110167514Skmacy if (ParseValue(Ty, RV, PFS)) return true; 3111167514Skmacy 3112167514Skmacy bool ExtraComma = false; 3113167514Skmacy if (EatIfPresent(lltok::comma)) { 3114167514Skmacy // Parse optional custom metadata, e.g. !dbg 3115167514Skmacy if (Lex.getKind() == lltok::MetadataVar) { 3116167514Skmacy ExtraComma = true; 3117167514Skmacy } else { 3118167514Skmacy // The normal case is one return value. 3119167514Skmacy // FIXME: LLVM 3.0 remove MRV support for 'ret i32 1, i32 2', requiring 3120167514Skmacy // use of 'ret {i32,i32} {i32 1, i32 2}' 3121167514Skmacy SmallVector<Value*, 8> RVs; 3122167514Skmacy RVs.push_back(RV); 3123176472Skmacy 3124176472Skmacy do { 3125176472Skmacy // If optional custom metadata, e.g. !dbg is seen then this is the 3126176472Skmacy // end of MRV. 3127176472Skmacy if (Lex.getKind() == lltok::MetadataVar) 3128176472Skmacy break; 3129176472Skmacy if (ParseTypeAndValue(RV, PFS)) return true; 3130176472Skmacy RVs.push_back(RV); 3131176472Skmacy } while (EatIfPresent(lltok::comma)); 3132176472Skmacy 3133176472Skmacy RV = UndefValue::get(PFS.getFunction().getReturnType()); 3134176472Skmacy for (unsigned i = 0, e = RVs.size(); i != e; ++i) { 3135176472Skmacy Instruction *I = InsertValueInst::Create(RV, RVs[i], i, "mrv"); 3136176472Skmacy BB->getInstList().push_back(I); 3137176472Skmacy RV = I; 3138176472Skmacy } 3139176472Skmacy } 3140176472Skmacy } 3141176472Skmacy 3142176472Skmacy Inst = ReturnInst::Create(Context, RV); 3143176472Skmacy return ExtraComma ? InstExtraComma : InstNormal; 3144176472Skmacy} 3145176472Skmacy 3146176472Skmacy 3147176472Skmacy/// ParseBr 3148176472Skmacy/// ::= 'br' TypeAndValue 3149176472Skmacy/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue 3150176472Skmacybool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) { 3151176472Skmacy LocTy Loc, Loc2; 3152176472Skmacy Value *Op0; 3153176472Skmacy BasicBlock *Op1, *Op2; 3154176472Skmacy if (ParseTypeAndValue(Op0, Loc, PFS)) return true; 3155176472Skmacy 3156176472Skmacy if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) { 3157176472Skmacy Inst = BranchInst::Create(BB); 3158176472Skmacy return false; 3159176472Skmacy } 3160176472Skmacy 3161176472Skmacy if (Op0->getType() != Type::getInt1Ty(Context)) 3162176472Skmacy return Error(Loc, "branch condition must have 'i1' type"); 3163176472Skmacy 3164176472Skmacy if (ParseToken(lltok::comma, "expected ',' after branch condition") || 3165176472Skmacy ParseTypeAndBasicBlock(Op1, Loc, PFS) || 3166176472Skmacy ParseToken(lltok::comma, "expected ',' after true destination") || 3167176472Skmacy ParseTypeAndBasicBlock(Op2, Loc2, PFS)) 3168176472Skmacy return true; 3169176472Skmacy 3170176472Skmacy Inst = BranchInst::Create(Op1, Op2, Op0); 3171167514Skmacy return false; 3172167514Skmacy} 3173167514Skmacy 3174167514Skmacy/// ParseSwitch 3175167514Skmacy/// Instruction 3176167514Skmacy/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']' 3177167514Skmacy/// JumpTable 3178167514Skmacy/// ::= (TypeAndValue ',' TypeAndValue)* 3179167514Skmacybool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) { 3180167514Skmacy LocTy CondLoc, BBLoc; 3181167514Skmacy Value *Cond; 3182167514Skmacy BasicBlock *DefaultBB; 3183167514Skmacy if (ParseTypeAndValue(Cond, CondLoc, PFS) || 3184167514Skmacy ParseToken(lltok::comma, "expected ',' after switch condition") || 3185167514Skmacy ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) || 3186167514Skmacy ParseToken(lltok::lsquare, "expected '[' with switch table")) 3187167514Skmacy return true; 3188167514Skmacy 3189167514Skmacy if (!isa<IntegerType>(Cond->getType())) 3190167514Skmacy return Error(CondLoc, "switch condition must have integer type"); 3191167514Skmacy 3192167514Skmacy // Parse the jump table pairs. 3193167514Skmacy SmallPtrSet<Value*, 32> SeenCases; 3194167514Skmacy SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table; 3195167514Skmacy while (Lex.getKind() != lltok::rsquare) { 3196167514Skmacy Value *Constant; 3197167514Skmacy BasicBlock *DestBB; 3198167514Skmacy 3199167514Skmacy if (ParseTypeAndValue(Constant, CondLoc, PFS) || 3200167514Skmacy ParseToken(lltok::comma, "expected ',' after case value") || 3201167514Skmacy ParseTypeAndBasicBlock(DestBB, PFS)) 3202167514Skmacy return true; 3203167514Skmacy 3204167514Skmacy if (!SeenCases.insert(Constant)) 3205167514Skmacy return Error(CondLoc, "duplicate case value in switch"); 3206167514Skmacy if (!isa<ConstantInt>(Constant)) 3207167514Skmacy return Error(CondLoc, "case value is not a constant integer"); 3208167514Skmacy 3209167514Skmacy Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB)); 3210167514Skmacy } 3211167514Skmacy 3212167514Skmacy Lex.Lex(); // Eat the ']'. 3213167514Skmacy 3214167514Skmacy SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size()); 3215167514Skmacy for (unsigned i = 0, e = Table.size(); i != e; ++i) 3216167514Skmacy SI->addCase(Table[i].first, Table[i].second); 3217167514Skmacy Inst = SI; 3218167514Skmacy return false; 3219167514Skmacy} 3220167514Skmacy 3221167514Skmacy/// ParseIndirectBr 3222167514Skmacy/// Instruction 3223167514Skmacy/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']' 3224167514Skmacybool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) { 3225167514Skmacy LocTy AddrLoc; 3226167514Skmacy Value *Address; 3227167514Skmacy if (ParseTypeAndValue(Address, AddrLoc, PFS) || 3228167514Skmacy ParseToken(lltok::comma, "expected ',' after indirectbr address") || 3229167514Skmacy ParseToken(lltok::lsquare, "expected '[' with indirectbr")) 3230167514Skmacy return true; 3231167514Skmacy 3232167514Skmacy if (!isa<PointerType>(Address->getType())) 3233167514Skmacy return Error(AddrLoc, "indirectbr address must have pointer type"); 3234167514Skmacy 3235167514Skmacy // Parse the destination list. 3236167514Skmacy SmallVector<BasicBlock*, 16> DestList; 3237167514Skmacy 3238167514Skmacy if (Lex.getKind() != lltok::rsquare) { 3239167514Skmacy BasicBlock *DestBB; 3240167514Skmacy if (ParseTypeAndBasicBlock(DestBB, PFS)) 3241167514Skmacy return true; 3242167514Skmacy DestList.push_back(DestBB); 3243167514Skmacy 3244167514Skmacy while (EatIfPresent(lltok::comma)) { 3245167514Skmacy if (ParseTypeAndBasicBlock(DestBB, PFS)) 3246167514Skmacy return true; 3247167514Skmacy DestList.push_back(DestBB); 3248167514Skmacy } 3249167514Skmacy } 3250167514Skmacy 3251167514Skmacy if (ParseToken(lltok::rsquare, "expected ']' at end of block list")) 3252167514Skmacy return true; 3253167514Skmacy 3254167514Skmacy IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size()); 3255167514Skmacy for (unsigned i = 0, e = DestList.size(); i != e; ++i) 3256167514Skmacy IBI->addDestination(DestList[i]); 3257167514Skmacy Inst = IBI; 3258167514Skmacy return false; 3259167514Skmacy} 3260167514Skmacy 3261167514Skmacy 3262167514Skmacy/// ParseInvoke 3263167514Skmacy/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList 3264167514Skmacy/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue 3265167514Skmacybool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) { 3266167514Skmacy LocTy CallLoc = Lex.getLoc(); 3267167514Skmacy unsigned RetAttrs, FnAttrs; 3268167514Skmacy CallingConv::ID CC; 3269167514Skmacy PATypeHolder RetType(Type::getVoidTy(Context)); 3270167514Skmacy LocTy RetTypeLoc; 3271167514Skmacy ValID CalleeID; 3272167514Skmacy SmallVector<ParamInfo, 16> ArgList; 3273167514Skmacy 3274167514Skmacy BasicBlock *NormalBB, *UnwindBB; 3275167514Skmacy if (ParseOptionalCallingConv(CC) || 3276167514Skmacy ParseOptionalAttrs(RetAttrs, 1) || 3277167514Skmacy ParseType(RetType, RetTypeLoc, true /*void allowed*/) || 3278167514Skmacy ParseValID(CalleeID) || 3279167514Skmacy ParseParameterList(ArgList, PFS) || 3280167514Skmacy ParseOptionalAttrs(FnAttrs, 2) || 3281167514Skmacy ParseToken(lltok::kw_to, "expected 'to' in invoke") || 3282167514Skmacy ParseTypeAndBasicBlock(NormalBB, PFS) || 3283167514Skmacy ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") || 3284167514Skmacy ParseTypeAndBasicBlock(UnwindBB, PFS)) 3285167514Skmacy return true; 3286167514Skmacy 3287167514Skmacy // If RetType is a non-function pointer type, then this is the short syntax 3288167514Skmacy // for the call, which means that RetType is just the return type. Infer the 3289167514Skmacy // rest of the function argument types from the arguments that are present. 3290167514Skmacy const PointerType *PFTy = 0; 3291167514Skmacy const FunctionType *Ty = 0; 3292167514Skmacy if (!(PFTy = dyn_cast<PointerType>(RetType)) || 3293167514Skmacy !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) { 3294167514Skmacy // Pull out the types of all of the arguments... 3295167514Skmacy std::vector<const Type*> ParamTypes; 3296167514Skmacy for (unsigned i = 0, e = ArgList.size(); i != e; ++i) 3297167514Skmacy ParamTypes.push_back(ArgList[i].V->getType()); 3298167514Skmacy 3299167514Skmacy if (!FunctionType::isValidReturnType(RetType)) 3300167514Skmacy return Error(RetTypeLoc, "Invalid result type for LLVM function"); 3301167514Skmacy 3302167514Skmacy Ty = FunctionType::get(RetType, ParamTypes, false); 3303167514Skmacy PFTy = PointerType::getUnqual(Ty); 3304167514Skmacy } 3305167514Skmacy 3306167514Skmacy // Look up the callee. 3307167514Skmacy Value *Callee; 3308167514Skmacy if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true; 3309167514Skmacy 3310167514Skmacy // FIXME: In LLVM 3.0, stop accepting zext, sext and inreg as optional 3311167514Skmacy // function attributes. 3312167514Skmacy unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg; 3313167514Skmacy if (FnAttrs & ObsoleteFuncAttrs) { 3314167514Skmacy RetAttrs |= FnAttrs & ObsoleteFuncAttrs; 3315167514Skmacy FnAttrs &= ~ObsoleteFuncAttrs; 3316167514Skmacy } 3317167514Skmacy 3318167514Skmacy // Set up the Attributes for the function. 3319167514Skmacy SmallVector<AttributeWithIndex, 8> Attrs; 3320167514Skmacy if (RetAttrs != Attribute::None) 3321167514Skmacy Attrs.push_back(AttributeWithIndex::get(0, RetAttrs)); 3322167514Skmacy 3323167514Skmacy SmallVector<Value*, 8> Args; 3324167514Skmacy 3325167514Skmacy // Loop through FunctionType's arguments and ensure they are specified 3326167514Skmacy // correctly. Also, gather any parameter attributes. 3327167514Skmacy FunctionType::param_iterator I = Ty->param_begin(); 3328167514Skmacy FunctionType::param_iterator E = Ty->param_end(); 3329167514Skmacy for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { 3330167514Skmacy const Type *ExpectedTy = 0; 3331167514Skmacy if (I != E) { 3332167514Skmacy ExpectedTy = *I++; 3333167514Skmacy } else if (!Ty->isVarArg()) { 3334167514Skmacy return Error(ArgList[i].Loc, "too many arguments specified"); 3335167514Skmacy } 3336167514Skmacy 3337167514Skmacy if (ExpectedTy && ExpectedTy != ArgList[i].V->getType()) 3338167514Skmacy return Error(ArgList[i].Loc, "argument is not of expected type '" + 3339167514Skmacy ExpectedTy->getDescription() + "'"); 3340167514Skmacy Args.push_back(ArgList[i].V); 3341167514Skmacy if (ArgList[i].Attrs != Attribute::None) 3342167514Skmacy Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs)); 3343167514Skmacy } 3344167514Skmacy 3345167514Skmacy if (I != E) 3346167514Skmacy return Error(CallLoc, "not enough parameters specified for call"); 3347167514Skmacy 3348167514Skmacy if (FnAttrs != Attribute::None) 3349167514Skmacy Attrs.push_back(AttributeWithIndex::get(~0, FnAttrs)); 3350167514Skmacy 3351167514Skmacy // Finish off the Attributes and check them 3352167514Skmacy AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end()); 3353167514Skmacy 3354167514Skmacy InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB, 3355167514Skmacy Args.begin(), Args.end()); 3356167514Skmacy II->setCallingConv(CC); 3357167514Skmacy II->setAttributes(PAL); 3358167514Skmacy Inst = II; 3359167514Skmacy return false; 3360167514Skmacy} 3361167514Skmacy 3362167514Skmacy 3363167514Skmacy 3364167514Skmacy//===----------------------------------------------------------------------===// 3365167514Skmacy// Binary Operators. 3366167514Skmacy//===----------------------------------------------------------------------===// 3367167514Skmacy 3368167514Skmacy/// ParseArithmetic 3369167514Skmacy/// ::= ArithmeticOps TypeAndValue ',' Value 3370167514Skmacy/// 3371167514Skmacy/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1, 3372167514Skmacy/// then any integer operand is allowed, if it is 2, any fp operand is allowed. 3373167514Skmacybool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS, 3374167514Skmacy unsigned Opc, unsigned OperandType) { 3375167514Skmacy LocTy Loc; Value *LHS, *RHS; 3376167514Skmacy if (ParseTypeAndValue(LHS, Loc, PFS) || 3377167514Skmacy ParseToken(lltok::comma, "expected ',' in arithmetic operation") || 3378167514Skmacy ParseValue(LHS->getType(), RHS, PFS)) 3379167514Skmacy return true; 3380167514Skmacy 3381167514Skmacy bool Valid; 3382167514Skmacy switch (OperandType) { 3383167514Skmacy default: llvm_unreachable("Unknown operand type!"); 3384167514Skmacy case 0: // int or FP. 3385167514Skmacy Valid = LHS->getType()->isIntOrIntVectorTy() || 3386167514Skmacy LHS->getType()->isFPOrFPVectorTy(); 3387167514Skmacy break; 3388167514Skmacy case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break; 3389167514Skmacy case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break; 3390167514Skmacy } 3391167514Skmacy 3392167514Skmacy if (!Valid) 3393167514Skmacy return Error(Loc, "invalid operand type for instruction"); 3394167514Skmacy 3395167514Skmacy Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); 3396167514Skmacy return false; 3397167514Skmacy} 3398167514Skmacy 3399167514Skmacy/// ParseLogical 3400167514Skmacy/// ::= ArithmeticOps TypeAndValue ',' Value { 3401167514Skmacybool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS, 3402167514Skmacy unsigned Opc) { 3403167514Skmacy LocTy Loc; Value *LHS, *RHS; 3404167514Skmacy if (ParseTypeAndValue(LHS, Loc, PFS) || 3405167514Skmacy ParseToken(lltok::comma, "expected ',' in logical operation") || 3406167514Skmacy ParseValue(LHS->getType(), RHS, PFS)) 3407167514Skmacy return true; 3408167514Skmacy 3409167514Skmacy if (!LHS->getType()->isIntOrIntVectorTy()) 3410167514Skmacy return Error(Loc,"instruction requires integer or integer vector operands"); 3411167514Skmacy 3412167514Skmacy Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); 3413167514Skmacy return false; 3414167514Skmacy} 3415167514Skmacy 3416167514Skmacy 3417167514Skmacy/// ParseCompare 3418167514Skmacy/// ::= 'icmp' IPredicates TypeAndValue ',' Value 3419167514Skmacy/// ::= 'fcmp' FPredicates TypeAndValue ',' Value 3420167514Skmacybool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS, 3421167514Skmacy unsigned Opc) { 3422167514Skmacy // Parse the integer/fp comparison predicate. 3423167514Skmacy LocTy Loc; 3424167514Skmacy unsigned Pred; 3425167514Skmacy Value *LHS, *RHS; 3426167514Skmacy if (ParseCmpPredicate(Pred, Opc) || 3427167514Skmacy ParseTypeAndValue(LHS, Loc, PFS) || 3428167514Skmacy ParseToken(lltok::comma, "expected ',' after compare value") || 3429167514Skmacy ParseValue(LHS->getType(), RHS, PFS)) 3430167514Skmacy return true; 3431167514Skmacy 3432167514Skmacy if (Opc == Instruction::FCmp) { 3433167514Skmacy if (!LHS->getType()->isFPOrFPVectorTy()) 3434167514Skmacy return Error(Loc, "fcmp requires floating point operands"); 3435167514Skmacy Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS); 3436167514Skmacy } else { 3437167514Skmacy assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!"); 3438167514Skmacy if (!LHS->getType()->isIntOrIntVectorTy() && 3439167514Skmacy !isa<PointerType>(LHS->getType())) 3440167514Skmacy return Error(Loc, "icmp requires integer operands"); 3441167514Skmacy Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS); 3442167514Skmacy } 3443167514Skmacy return false; 3444167514Skmacy} 3445167514Skmacy 3446167514Skmacy//===----------------------------------------------------------------------===// 3447167514Skmacy// Other Instructions. 3448167514Skmacy//===----------------------------------------------------------------------===// 3449167514Skmacy 3450167514Skmacy 3451167514Skmacy/// ParseCast 3452167514Skmacy/// ::= CastOpc TypeAndValue 'to' Type 3453167514Skmacybool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS, 3454167514Skmacy unsigned Opc) { 3455167514Skmacy LocTy Loc; Value *Op; 3456167514Skmacy PATypeHolder DestTy(Type::getVoidTy(Context)); 3457167514Skmacy if (ParseTypeAndValue(Op, Loc, PFS) || 3458167514Skmacy ParseToken(lltok::kw_to, "expected 'to' after cast value") || 3459167514Skmacy ParseType(DestTy)) 3460167514Skmacy return true; 3461167514Skmacy 3462167514Skmacy if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) { 3463167514Skmacy CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy); 3464167514Skmacy return Error(Loc, "invalid cast opcode for cast from '" + 3465167514Skmacy Op->getType()->getDescription() + "' to '" + 3466167514Skmacy DestTy->getDescription() + "'"); 3467167514Skmacy } 3468167514Skmacy Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy); 3469167514Skmacy return false; 3470167514Skmacy} 3471167514Skmacy 3472167514Skmacy/// ParseSelect 3473167514Skmacy/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue 3474176472Skmacybool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) { 3475176472Skmacy LocTy Loc; 3476176472Skmacy Value *Op0, *Op1, *Op2; 3477176472Skmacy if (ParseTypeAndValue(Op0, Loc, PFS) || 3478167514Skmacy ParseToken(lltok::comma, "expected ',' after select condition") || 3479167514Skmacy ParseTypeAndValue(Op1, PFS) || 3480167514Skmacy ParseToken(lltok::comma, "expected ',' after select value") || 3481167514Skmacy ParseTypeAndValue(Op2, PFS)) 3482167514Skmacy return true; 3483167514Skmacy 3484167514Skmacy if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2)) 3485167514Skmacy return Error(Loc, Reason); 3486167514Skmacy 3487167514Skmacy Inst = SelectInst::Create(Op0, Op1, Op2); 3488167514Skmacy return false; 3489167514Skmacy} 3490167514Skmacy 3491167514Skmacy/// ParseVA_Arg 3492167514Skmacy/// ::= 'va_arg' TypeAndValue ',' Type 3493167514Skmacybool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) { 3494167514Skmacy Value *Op; 3495167514Skmacy PATypeHolder EltTy(Type::getVoidTy(Context)); 3496167514Skmacy LocTy TypeLoc; 3497167514Skmacy if (ParseTypeAndValue(Op, PFS) || 3498167514Skmacy ParseToken(lltok::comma, "expected ',' after vaarg operand") || 3499167514Skmacy ParseType(EltTy, TypeLoc)) 3500167514Skmacy return true; 3501167514Skmacy 3502167514Skmacy if (!EltTy->isFirstClassType()) 3503167514Skmacy return Error(TypeLoc, "va_arg requires operand with first class type"); 3504167514Skmacy 3505167514Skmacy Inst = new VAArgInst(Op, EltTy); 3506167514Skmacy return false; 3507167514Skmacy} 3508167514Skmacy 3509167514Skmacy/// ParseExtractElement 3510167514Skmacy/// ::= 'extractelement' TypeAndValue ',' TypeAndValue 3511167514Skmacybool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) { 3512167514Skmacy LocTy Loc; 3513167514Skmacy Value *Op0, *Op1; 3514167514Skmacy if (ParseTypeAndValue(Op0, Loc, PFS) || 3515167514Skmacy ParseToken(lltok::comma, "expected ',' after extract value") || 3516167514Skmacy ParseTypeAndValue(Op1, PFS)) 3517167514Skmacy return true; 3518167514Skmacy 3519167514Skmacy if (!ExtractElementInst::isValidOperands(Op0, Op1)) 3520167514Skmacy return Error(Loc, "invalid extractelement operands"); 3521167514Skmacy 3522167514Skmacy Inst = ExtractElementInst::Create(Op0, Op1); 3523167514Skmacy return false; 3524176472Skmacy} 3525176472Skmacy 3526176472Skmacy/// ParseInsertElement 3527176472Skmacy/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue 3528176472Skmacybool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) { 3529176472Skmacy LocTy Loc; 3530176472Skmacy Value *Op0, *Op1, *Op2; 3531176472Skmacy if (ParseTypeAndValue(Op0, Loc, PFS) || 3532167514Skmacy ParseToken(lltok::comma, "expected ',' after insertelement value") || 3533167514Skmacy ParseTypeAndValue(Op1, PFS) || 3534167514Skmacy ParseToken(lltok::comma, "expected ',' after insertelement value") || 3535167514Skmacy ParseTypeAndValue(Op2, PFS)) 3536167514Skmacy return true; 3537167514Skmacy 3538167514Skmacy if (!InsertElementInst::isValidOperands(Op0, Op1, Op2)) 3539167514Skmacy return Error(Loc, "invalid insertelement operands"); 3540167514Skmacy 3541167514Skmacy Inst = InsertElementInst::Create(Op0, Op1, Op2); 3542167514Skmacy return false; 3543167514Skmacy} 3544167514Skmacy 3545167514Skmacy/// ParseShuffleVector 3546167514Skmacy/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue 3547167514Skmacybool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) { 3548167514Skmacy LocTy Loc; 3549167514Skmacy Value *Op0, *Op1, *Op2; 3550167514Skmacy if (ParseTypeAndValue(Op0, Loc, PFS) || 3551167514Skmacy ParseToken(lltok::comma, "expected ',' after shuffle mask") || 3552167514Skmacy ParseTypeAndValue(Op1, PFS) || 3553167514Skmacy ParseToken(lltok::comma, "expected ',' after shuffle value") || 3554167514Skmacy ParseTypeAndValue(Op2, PFS)) 3555167514Skmacy return true; 3556167514Skmacy 3557167514Skmacy if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2)) 3558167514Skmacy return Error(Loc, "invalid extractelement operands"); 3559167514Skmacy 3560167514Skmacy Inst = new ShuffleVectorInst(Op0, Op1, Op2); 3561167514Skmacy return false; 3562167514Skmacy} 3563167514Skmacy 3564167514Skmacy/// ParsePHI 3565167514Skmacy/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')* 3566167514Skmacyint LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) { 3567167514Skmacy PATypeHolder Ty(Type::getVoidTy(Context)); 3568176472Skmacy Value *Op0, *Op1; 3569167514Skmacy LocTy TypeLoc = Lex.getLoc(); 3570176472Skmacy 3571176472Skmacy if (ParseType(Ty) || 3572176472Skmacy ParseToken(lltok::lsquare, "expected '[' in phi value list") || 3573176472Skmacy ParseValue(Ty, Op0, PFS) || 3574167514Skmacy ParseToken(lltok::comma, "expected ',' after insertelement value") || 3575167514Skmacy ParseValue(Type::getLabelTy(Context), Op1, PFS) || 3576167514Skmacy ParseToken(lltok::rsquare, "expected ']' in phi value list")) 3577167514Skmacy return true; 3578167514Skmacy 3579167514Skmacy bool AteExtraComma = false; 3580167514Skmacy SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals; 3581167514Skmacy while (1) { 3582167514Skmacy PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1))); 3583167514Skmacy 3584167514Skmacy if (!EatIfPresent(lltok::comma)) 3585167514Skmacy break; 3586167514Skmacy 3587167514Skmacy if (Lex.getKind() == lltok::MetadataVar) { 3588167514Skmacy AteExtraComma = true; 3589167514Skmacy break; 3590167514Skmacy } 3591167514Skmacy 3592167514Skmacy if (ParseToken(lltok::lsquare, "expected '[' in phi value list") || 3593167514Skmacy ParseValue(Ty, Op0, PFS) || 3594167514Skmacy ParseToken(lltok::comma, "expected ',' after insertelement value") || 3595167514Skmacy ParseValue(Type::getLabelTy(Context), Op1, PFS) || 3596167514Skmacy ParseToken(lltok::rsquare, "expected ']' in phi value list")) 3597167514Skmacy return true; 3598167514Skmacy } 3599167514Skmacy 3600167514Skmacy if (!Ty->isFirstClassType()) 3601167514Skmacy return Error(TypeLoc, "phi node must have first class type"); 3602167514Skmacy 3603167514Skmacy PHINode *PN = PHINode::Create(Ty); 3604167514Skmacy PN->reserveOperandSpace(PHIVals.size()); 3605167514Skmacy for (unsigned i = 0, e = PHIVals.size(); i != e; ++i) 3606167514Skmacy PN->addIncoming(PHIVals[i].first, PHIVals[i].second); 3607167514Skmacy Inst = PN; 3608167514Skmacy return AteExtraComma ? InstExtraComma : InstNormal; 3609167514Skmacy} 3610167514Skmacy 3611167514Skmacy/// ParseCall 3612167514Skmacy/// ::= 'tail'? 'call' OptionalCallingConv OptionalAttrs Type Value 3613167514Skmacy/// ParameterList OptionalAttrs 3614167514Skmacybool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS, 3615167514Skmacy bool isTail) { 3616167514Skmacy unsigned RetAttrs, FnAttrs; 3617167514Skmacy CallingConv::ID CC; 3618167514Skmacy PATypeHolder RetType(Type::getVoidTy(Context)); 3619167514Skmacy LocTy RetTypeLoc; 3620167514Skmacy ValID CalleeID; 3621167514Skmacy SmallVector<ParamInfo, 16> ArgList; 3622167514Skmacy LocTy CallLoc = Lex.getLoc(); 3623167514Skmacy 3624167514Skmacy if ((isTail && ParseToken(lltok::kw_call, "expected 'tail call'")) || 3625167514Skmacy ParseOptionalCallingConv(CC) || 3626167514Skmacy ParseOptionalAttrs(RetAttrs, 1) || 3627167514Skmacy ParseType(RetType, RetTypeLoc, true /*void allowed*/) || 3628167514Skmacy ParseValID(CalleeID) || 3629167514Skmacy ParseParameterList(ArgList, PFS) || 3630167514Skmacy ParseOptionalAttrs(FnAttrs, 2)) 3631167514Skmacy return true; 3632167514Skmacy 3633167514Skmacy // If RetType is a non-function pointer type, then this is the short syntax 3634167514Skmacy // for the call, which means that RetType is just the return type. Infer the 3635167514Skmacy // rest of the function argument types from the arguments that are present. 3636167514Skmacy const PointerType *PFTy = 0; 3637167514Skmacy const FunctionType *Ty = 0; 3638167514Skmacy if (!(PFTy = dyn_cast<PointerType>(RetType)) || 3639167514Skmacy !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) { 3640167514Skmacy // Pull out the types of all of the arguments... 3641167514Skmacy std::vector<const Type*> ParamTypes; 3642167514Skmacy for (unsigned i = 0, e = ArgList.size(); i != e; ++i) 3643167514Skmacy ParamTypes.push_back(ArgList[i].V->getType()); 3644167514Skmacy 3645167514Skmacy if (!FunctionType::isValidReturnType(RetType)) 3646167514Skmacy return Error(RetTypeLoc, "Invalid result type for LLVM function"); 3647167514Skmacy 3648167514Skmacy Ty = FunctionType::get(RetType, ParamTypes, false); 3649167514Skmacy PFTy = PointerType::getUnqual(Ty); 3650167514Skmacy } 3651167514Skmacy 3652167514Skmacy // Look up the callee. 3653167514Skmacy Value *Callee; 3654167514Skmacy if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true; 3655167514Skmacy 3656167514Skmacy // FIXME: In LLVM 3.0, stop accepting zext, sext and inreg as optional 3657167514Skmacy // function attributes. 3658167514Skmacy unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg; 3659176472Skmacy if (FnAttrs & ObsoleteFuncAttrs) { 3660176472Skmacy RetAttrs |= FnAttrs & ObsoleteFuncAttrs; 3661176472Skmacy FnAttrs &= ~ObsoleteFuncAttrs; 3662176472Skmacy } 3663176472Skmacy 3664167514Skmacy // Set up the Attributes for the function. 3665167514Skmacy SmallVector<AttributeWithIndex, 8> Attrs; 3666167514Skmacy if (RetAttrs != Attribute::None) 3667167514Skmacy Attrs.push_back(AttributeWithIndex::get(0, RetAttrs)); 3668167514Skmacy 3669167514Skmacy SmallVector<Value*, 8> Args; 3670167514Skmacy 3671167514Skmacy // Loop through FunctionType's arguments and ensure they are specified 3672167514Skmacy // correctly. Also, gather any parameter attributes. 3673167514Skmacy FunctionType::param_iterator I = Ty->param_begin(); 3674167514Skmacy FunctionType::param_iterator E = Ty->param_end(); 3675167514Skmacy for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { 3676167514Skmacy const Type *ExpectedTy = 0; 3677167514Skmacy if (I != E) { 3678167514Skmacy ExpectedTy = *I++; 3679167514Skmacy } else if (!Ty->isVarArg()) { 3680167514Skmacy return Error(ArgList[i].Loc, "too many arguments specified"); 3681167514Skmacy } 3682167514Skmacy 3683167514Skmacy if (ExpectedTy && ExpectedTy != ArgList[i].V->getType()) 3684167514Skmacy return Error(ArgList[i].Loc, "argument is not of expected type '" + 3685167514Skmacy ExpectedTy->getDescription() + "'"); 3686167514Skmacy Args.push_back(ArgList[i].V); 3687167514Skmacy if (ArgList[i].Attrs != Attribute::None) 3688167514Skmacy Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs)); 3689167514Skmacy } 3690167514Skmacy 3691167514Skmacy if (I != E) 3692167514Skmacy return Error(CallLoc, "not enough parameters specified for call"); 3693167514Skmacy 3694167514Skmacy if (FnAttrs != Attribute::None) 3695167514Skmacy Attrs.push_back(AttributeWithIndex::get(~0, FnAttrs)); 3696167514Skmacy 3697167514Skmacy // Finish off the Attributes and check them 3698167514Skmacy AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end()); 3699167514Skmacy 3700167514Skmacy CallInst *CI = CallInst::Create(Callee, Args.begin(), Args.end()); 3701167514Skmacy CI->setTailCall(isTail); 3702167514Skmacy CI->setCallingConv(CC); 3703167514Skmacy CI->setAttributes(PAL); 3704167514Skmacy Inst = CI; 3705167514Skmacy return false; 3706167514Skmacy} 3707167514Skmacy 3708167514Skmacy//===----------------------------------------------------------------------===// 3709167514Skmacy// Memory Instructions. 3710167514Skmacy//===----------------------------------------------------------------------===// 3711167514Skmacy 3712167514Skmacy/// ParseAlloc 3713167514Skmacy/// ::= 'malloc' Type (',' TypeAndValue)? (',' OptionalInfo)? 3714167514Skmacy/// ::= 'alloca' Type (',' TypeAndValue)? (',' OptionalInfo)? 3715167514Skmacyint LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS, 3716167514Skmacy BasicBlock* BB, bool isAlloca) { 3717167514Skmacy PATypeHolder Ty(Type::getVoidTy(Context)); 3718167514Skmacy Value *Size = 0; 3719167514Skmacy LocTy SizeLoc; 3720167514Skmacy unsigned Alignment = 0; 3721167514Skmacy if (ParseType(Ty)) return true; 3722167514Skmacy 3723167514Skmacy bool AteExtraComma = false; 3724167514Skmacy if (EatIfPresent(lltok::comma)) { 3725167514Skmacy if (Lex.getKind() == lltok::kw_align) { 3726167514Skmacy if (ParseOptionalAlignment(Alignment)) return true; 3727167514Skmacy } else if (Lex.getKind() == lltok::MetadataVar) { 3728167514Skmacy AteExtraComma = true; 3729167514Skmacy } else { 3730167514Skmacy if (ParseTypeAndValue(Size, SizeLoc, PFS) || 3731167514Skmacy ParseOptionalCommaAlign(Alignment, AteExtraComma)) 3732167514Skmacy return true; 3733167514Skmacy } 3734167514Skmacy } 3735167514Skmacy 3736167514Skmacy if (Size && !Size->getType()->isIntegerTy(32)) 3737167514Skmacy return Error(SizeLoc, "element count must be i32"); 3738167514Skmacy 3739167514Skmacy if (isAlloca) { 3740167514Skmacy Inst = new AllocaInst(Ty, Size, Alignment); 3741167514Skmacy return AteExtraComma ? InstExtraComma : InstNormal; 3742167514Skmacy } 3743167514Skmacy 3744167514Skmacy // Autoupgrade old malloc instruction to malloc call. 3745167514Skmacy // FIXME: Remove in LLVM 3.0. 3746167514Skmacy const Type *IntPtrTy = Type::getInt32Ty(Context); 3747167514Skmacy Constant *AllocSize = ConstantExpr::getSizeOf(Ty); 3748167514Skmacy AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, IntPtrTy); 3749167514Skmacy if (!MallocF) 3750167514Skmacy // Prototype malloc as "void *(int32)". 3751167514Skmacy // This function is renamed as "malloc" in ValidateEndOfModule(). 3752167514Skmacy MallocF = cast<Function>( 3753167514Skmacy M->getOrInsertFunction("", Type::getInt8PtrTy(Context), IntPtrTy, NULL)); 3754167514Skmacy Inst = CallInst::CreateMalloc(BB, IntPtrTy, Ty, AllocSize, Size, MallocF); 3755167514Skmacyreturn AteExtraComma ? InstExtraComma : InstNormal; 3756167514Skmacy} 3757167514Skmacy 3758167514Skmacy/// ParseFree 3759167514Skmacy/// ::= 'free' TypeAndValue 3760167514Skmacybool LLParser::ParseFree(Instruction *&Inst, PerFunctionState &PFS, 3761167514Skmacy BasicBlock* BB) { 3762167514Skmacy Value *Val; LocTy Loc; 3763167514Skmacy if (ParseTypeAndValue(Val, Loc, PFS)) return true; 3764167514Skmacy if (!isa<PointerType>(Val->getType())) 3765167514Skmacy return Error(Loc, "operand to free must be a pointer"); 3766167514Skmacy Inst = CallInst::CreateFree(Val, BB); 3767167514Skmacy return false; 3768167514Skmacy} 3769167514Skmacy 3770176472Skmacy/// ParseLoad 3771176472Skmacy/// ::= 'volatile'? 'load' TypeAndValue (',' OptionalInfo)? 3772176472Skmacyint LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS, 3773176472Skmacy bool isVolatile) { 3774176472Skmacy Value *Val; LocTy Loc; 3775176472Skmacy unsigned Alignment = 0; 3776176472Skmacy bool AteExtraComma = false; 3777176472Skmacy if (ParseTypeAndValue(Val, Loc, PFS) || 3778176472Skmacy ParseOptionalCommaAlign(Alignment, AteExtraComma)) 3779176472Skmacy return true; 3780176472Skmacy 3781176472Skmacy if (!isa<PointerType>(Val->getType()) || 3782176472Skmacy !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType()) 3783176472Skmacy return Error(Loc, "load operand must be a pointer to a first class type"); 3784176472Skmacy 3785176472Skmacy Inst = new LoadInst(Val, "", isVolatile, Alignment); 3786176472Skmacy return AteExtraComma ? InstExtraComma : InstNormal; 3787176472Skmacy} 3788176472Skmacy 3789176472Skmacy/// ParseStore 3790167514Skmacy/// ::= 'volatile'? 'store' TypeAndValue ',' TypeAndValue (',' 'align' i32)? 3791167514Skmacyint LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS, 3792167514Skmacy bool isVolatile) { 3793167514Skmacy Value *Val, *Ptr; LocTy Loc, PtrLoc; 3794167514Skmacy unsigned Alignment = 0; 3795167514Skmacy bool AteExtraComma = false; 3796167514Skmacy if (ParseTypeAndValue(Val, Loc, PFS) || 3797167514Skmacy ParseToken(lltok::comma, "expected ',' after store operand") || 3798167514Skmacy ParseTypeAndValue(Ptr, PtrLoc, PFS) || 3799167514Skmacy ParseOptionalCommaAlign(Alignment, AteExtraComma)) 3800167514Skmacy return true; 3801167514Skmacy 3802167514Skmacy if (!isa<PointerType>(Ptr->getType())) 3803167514Skmacy return Error(PtrLoc, "store operand must be a pointer"); 3804167514Skmacy if (!Val->getType()->isFirstClassType()) 3805167514Skmacy return Error(Loc, "store operand must be a first class value"); 3806167514Skmacy if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType()) 3807167514Skmacy return Error(Loc, "stored value and pointer type do not match"); 3808167514Skmacy 3809167514Skmacy Inst = new StoreInst(Val, Ptr, isVolatile, Alignment); 3810167514Skmacy return AteExtraComma ? InstExtraComma : InstNormal; 3811167514Skmacy} 3812167514Skmacy 3813167514Skmacy/// ParseGetResult 3814167514Skmacy/// ::= 'getresult' TypeAndValue ',' i32 3815167514Skmacy/// FIXME: Remove support for getresult in LLVM 3.0 3816167514Skmacybool LLParser::ParseGetResult(Instruction *&Inst, PerFunctionState &PFS) { 3817167514Skmacy Value *Val; LocTy ValLoc, EltLoc; 3818167514Skmacy unsigned Element; 3819167514Skmacy if (ParseTypeAndValue(Val, ValLoc, PFS) || 3820167514Skmacy ParseToken(lltok::comma, "expected ',' after getresult operand") || 3821167514Skmacy ParseUInt32(Element, EltLoc)) 3822167514Skmacy return true; 3823167514Skmacy 3824167514Skmacy if (!isa<StructType>(Val->getType()) && !isa<ArrayType>(Val->getType())) 3825167514Skmacy return Error(ValLoc, "getresult inst requires an aggregate operand"); 3826167514Skmacy if (!ExtractValueInst::getIndexedType(Val->getType(), Element)) 3827167514Skmacy return Error(EltLoc, "invalid getresult index for value"); 3828167514Skmacy Inst = ExtractValueInst::Create(Val, Element); 3829167514Skmacy return false; 3830167514Skmacy} 3831167514Skmacy 3832167514Skmacy/// ParseGetElementPtr 3833167514Skmacy/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)* 3834167514Skmacyint LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) { 3835167514Skmacy Value *Ptr, *Val; LocTy Loc, EltLoc; 3836167514Skmacy 3837167514Skmacy bool InBounds = EatIfPresent(lltok::kw_inbounds); 3838167514Skmacy 3839167514Skmacy if (ParseTypeAndValue(Ptr, Loc, PFS)) return true; 3840167514Skmacy 3841167514Skmacy if (!isa<PointerType>(Ptr->getType())) 3842167514Skmacy return Error(Loc, "base of getelementptr must be a pointer"); 3843167514Skmacy 3844167514Skmacy SmallVector<Value*, 16> Indices; 3845167514Skmacy bool AteExtraComma = false; 3846167514Skmacy while (EatIfPresent(lltok::comma)) { 3847167514Skmacy if (Lex.getKind() == lltok::MetadataVar) { 3848167514Skmacy AteExtraComma = true; 3849167514Skmacy break; 3850167514Skmacy } 3851167514Skmacy if (ParseTypeAndValue(Val, EltLoc, PFS)) return true; 3852167514Skmacy if (!isa<IntegerType>(Val->getType())) 3853167514Skmacy return Error(EltLoc, "getelementptr index must be an integer"); 3854167514Skmacy Indices.push_back(Val); 3855167514Skmacy } 3856167514Skmacy 3857167514Skmacy if (!GetElementPtrInst::getIndexedType(Ptr->getType(), 3858167514Skmacy Indices.begin(), Indices.end())) 3859167514Skmacy return Error(Loc, "invalid getelementptr indices"); 3860167514Skmacy Inst = GetElementPtrInst::Create(Ptr, Indices.begin(), Indices.end()); 3861167514Skmacy if (InBounds) 3862167514Skmacy cast<GetElementPtrInst>(Inst)->setIsInBounds(true); 3863167514Skmacy return AteExtraComma ? InstExtraComma : InstNormal; 3864167514Skmacy} 3865167514Skmacy 3866167514Skmacy/// ParseExtractValue 3867167514Skmacy/// ::= 'extractvalue' TypeAndValue (',' uint32)+ 3868167514Skmacyint LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) { 3869167514Skmacy Value *Val; LocTy Loc; 3870167514Skmacy SmallVector<unsigned, 4> Indices; 3871167514Skmacy bool AteExtraComma; 3872167514Skmacy if (ParseTypeAndValue(Val, Loc, PFS) || 3873167514Skmacy ParseIndexList(Indices, AteExtraComma)) 3874167514Skmacy return true; 3875167514Skmacy 3876167514Skmacy if (!Val->getType()->isAggregateType()) 3877167514Skmacy return Error(Loc, "extractvalue operand must be aggregate type"); 3878167514Skmacy 3879167514Skmacy if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(), 3880167514Skmacy Indices.end())) 3881167514Skmacy return Error(Loc, "invalid indices for extractvalue"); 3882167514Skmacy Inst = ExtractValueInst::Create(Val, Indices.begin(), Indices.end()); 3883167514Skmacy return AteExtraComma ? InstExtraComma : InstNormal; 3884167514Skmacy} 3885167514Skmacy 3886167514Skmacy/// ParseInsertValue 3887176472Skmacy/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+ 3888167514Skmacyint LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) { 3889176472Skmacy Value *Val0, *Val1; LocTy Loc0, Loc1; 3890176472Skmacy SmallVector<unsigned, 4> Indices; 3891176472Skmacy bool AteExtraComma; 3892167514Skmacy if (ParseTypeAndValue(Val0, Loc0, PFS) || 3893176472Skmacy ParseToken(lltok::comma, "expected comma after insertvalue operand") || 3894176472Skmacy ParseTypeAndValue(Val1, Loc1, PFS) || 3895176472Skmacy ParseIndexList(Indices, AteExtraComma)) 3896167514Skmacy return true; 3897176472Skmacy 3898176472Skmacy if (!Val0->getType()->isAggregateType()) 3899176472Skmacy return Error(Loc0, "insertvalue operand must be aggregate type"); 3900167514Skmacy 3901176472Skmacy if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(), 3902176472Skmacy Indices.end())) 3903176472Skmacy return Error(Loc0, "invalid indices for insertvalue"); 3904167514Skmacy Inst = InsertValueInst::Create(Val0, Val1, Indices.begin(), Indices.end()); 3905167514Skmacy return AteExtraComma ? InstExtraComma : InstNormal; 3906167514Skmacy} 3907167514Skmacy 3908167514Skmacy//===----------------------------------------------------------------------===// 3909167514Skmacy// Embedded metadata. 3910167514Skmacy//===----------------------------------------------------------------------===// 3911167514Skmacy 3912167514Skmacy/// ParseMDNodeVector 3913167514Skmacy/// ::= Element (',' Element)* 3914167514Skmacy/// Element 3915167514Skmacy/// ::= 'null' | TypeAndValue 3916167514Skmacybool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts, 3917167514Skmacy PerFunctionState *PFS) { 3918167514Skmacy do { 3919167514Skmacy // Null is a special case since it is typeless. 3920167514Skmacy if (EatIfPresent(lltok::kw_null)) { 3921167514Skmacy Elts.push_back(0); 3922167514Skmacy continue; 3923167514Skmacy } 3924167514Skmacy 3925167514Skmacy Value *V = 0; 3926167514Skmacy PATypeHolder Ty(Type::getVoidTy(Context)); 3927167514Skmacy ValID ID; 3928167514Skmacy if (ParseType(Ty) || ParseValID(ID, PFS) || 3929176472Skmacy ConvertValIDToValue(Ty, ID, V, PFS)) 3930176472Skmacy return true; 3931176472Skmacy 3932167514Skmacy Elts.push_back(V); 3933167514Skmacy } while (EatIfPresent(lltok::comma)); 3934167514Skmacy 3935167514Skmacy return false; 3936167514Skmacy} 3937167514Skmacy