LLParser.cpp revision 647e3016de18d2fc8b0f233a0b356809e3fdcc54
1df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===-- LLParser.cpp - Parser Class ---------------------------------------===// 2df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// 3df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// The LLVM Compiler Infrastructure 4df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// 5df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// This file is distributed under the University of Illinois Open Source 6df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// License. See LICENSE.TXT for details. 7df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// 8df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 9df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// 10df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// This file defines the parser class for .ll files. 11df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// 12df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 13df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 14df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "LLParser.h" 15df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/AutoUpgrade.h" 16df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/CallingConv.h" 17df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/Constants.h" 18df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/DerivedTypes.h" 19df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/InlineAsm.h" 20df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/Instructions.h" 21fba933c823a2862067e0696bd5032ab18185bd77Owen Anderson#include "llvm/LLVMContext.h" 220a9f7b9c3ebe7d0ec033462e1a7c9101279956f9Devang Patel#include "llvm/Metadata.h" 23df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/Module.h" 241224c386981f7948f298ed9ad444c40609570f2eDan Gohman#include "llvm/Operator.h" 25df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/ValueSymbolTable.h" 26df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/ADT/SmallPtrSet.h" 27df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/ADT/StringExtras.h" 28c25e7581b9b8088910da31702d4ca21c4734c6d7Torok Edwin#include "llvm/Support/ErrorHandling.h" 29df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/Support/raw_ostream.h" 30df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerusing namespace llvm; 31df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 32df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnernamespace llvm { 333ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner /// ValID - Represents a reference of a definition of some sort with no type. 343ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner /// There are several cases where we have to parse the value but where the 353ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner /// type can depend on later context. This may either be a numeric reference 363ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner /// or a symbolic (%var) reference. This is just a discriminated union. 37df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner struct ValID { 38df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner enum { 39df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner t_LocalID, t_GlobalID, // ID in UIntVal. 40df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner t_LocalName, t_GlobalName, // Name in StrVal. 41df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner t_APSInt, t_APFloat, // Value in APSIntVal/APFloatVal. 42df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner t_Null, t_Undef, t_Zero, // No value. 43081b505f77b183b7b029818218245cf4986e8948Chris Lattner t_EmptyArray, // No value: [] 44df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner t_Constant, // Value in ConstantVal. 45e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel t_InlineAsm, // Value in StrVal/StrVal2/UIntVal. 46e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel t_Metadata // Value in MetadataVal. 47df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } Kind; 48df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 49df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LLParser::LocTy Loc; 50df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned UIntVal; 51df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::string StrVal, StrVal2; 52df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner APSInt APSIntVal; 53df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner APFloat APFloatVal; 54df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *ConstantVal; 55e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel MetadataBase *MetadataVal; 56df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ValID() : APFloatVal(0.0) {} 57df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner }; 58df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 59df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 603ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// Run: module ::= toplevelentity* 61ad7d1e2085a489dbedc65b99bab811771ead1aabChris Lattnerbool LLParser::Run() { 623ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner // Prime the lexer. 633ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner Lex.Lex(); 643ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner 65ad7d1e2085a489dbedc65b99bab811771ead1aabChris Lattner return ParseTopLevelEntities() || 66ad7d1e2085a489dbedc65b99bab811771ead1aabChris Lattner ValidateEndOfModule(); 67df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 68df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 69df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ValidateEndOfModule - Do final validity and sanity checks at the end of the 70df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// module. 71df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ValidateEndOfModule() { 72df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ForwardRefTypes.empty()) 73df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ForwardRefTypes.begin()->second.second, 74df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "use of undefined type named '" + 75df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefTypes.begin()->first + "'"); 76df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ForwardRefTypeIDs.empty()) 77df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ForwardRefTypeIDs.begin()->second.second, 78df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "use of undefined type '%" + 79df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner utostr(ForwardRefTypeIDs.begin()->first) + "'"); 80df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 81df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ForwardRefVals.empty()) 82df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ForwardRefVals.begin()->second.second, 83df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "use of undefined value '@" + ForwardRefVals.begin()->first + 84df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "'"); 85df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 86df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ForwardRefValIDs.empty()) 87df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ForwardRefValIDs.begin()->second.second, 88df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "use of undefined value '@" + 89df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner utostr(ForwardRefValIDs.begin()->first) + "'"); 90df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 911c7eea60d3cbada2bae54c42fa178078a3ace9b3Devang Patel if (!ForwardRefMDNodes.empty()) 921c7eea60d3cbada2bae54c42fa178078a3ace9b3Devang Patel return Error(ForwardRefMDNodes.begin()->second.second, 931c7eea60d3cbada2bae54c42fa178078a3ace9b3Devang Patel "use of undefined metadata '!" + 941c7eea60d3cbada2bae54c42fa178078a3ace9b3Devang Patel utostr(ForwardRefMDNodes.begin()->first) + "'"); 951c7eea60d3cbada2bae54c42fa178078a3ace9b3Devang Patel 961c7eea60d3cbada2bae54c42fa178078a3ace9b3Devang Patel 97df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Look for intrinsic functions and CallInst that need to be upgraded 98df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ) 99df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove 100df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 101df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 102df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 103df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 104df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 105df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Top-Level Entities 106df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 107df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 108df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseTopLevelEntities() { 109df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner while (1) { 110df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Lex.getKind()) { 111df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: return TokError("expected top-level entity"); 112df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::Eof: return false; 113df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner //case lltok::kw_define: 114df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_declare: if (ParseDeclare()) return true; break; 115df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_define: if (ParseDefine()) return true; break; 116df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_module: if (ParseModuleAsm()) return true; break; 117df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_target: if (ParseTargetDefinition()) return true; break; 118df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_deplibs: if (ParseDepLibs()) return true; break; 119df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_type: if (ParseUnnamedType()) return true; break; 120df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::StringConstant: // FIXME: REMOVE IN LLVM 3.0 121df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::LocalVar: if (ParseNamedType()) return true; break; 122df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break; 123923078c65d5a37a4f135705300c9feea49487de5Devang Patel case lltok::Metadata: if (ParseStandaloneMetadata()) return true; break; 124eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel case lltok::NamedMD: if (ParseNamedMetadata()) return true; break; 125df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 126df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // The Global variable production with no name can have many different 127df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // optional leading prefixes, the production is: 128df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalThreadLocal 129df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // OptionalAddrSpace ('constant'|'global') ... 1303d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_private : // OptionalLinkage 1313d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_linker_private: // OptionalLinkage 1323d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_internal: // OptionalLinkage 1333d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_weak: // OptionalLinkage 1343d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_weak_odr: // OptionalLinkage 1353d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_linkonce: // OptionalLinkage 1363d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_linkonce_odr: // OptionalLinkage 1373d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_appending: // OptionalLinkage 1383d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_dllexport: // OptionalLinkage 1393d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_common: // OptionalLinkage 1403d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_dllimport: // OptionalLinkage 1413d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_extern_weak: // OptionalLinkage 1423d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_external: { // OptionalLinkage 143df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Linkage, Visibility; 144df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseOptionalLinkage(Linkage) || 145df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalVisibility(Visibility) || 146eeb4a84ac8d91fb1d5a7c484a1c7047409faee30Chris Lattner ParseGlobal("", SMLoc(), Linkage, true, Visibility)) 147df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 148df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 149df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 150df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_default: // OptionalVisibility 151df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_hidden: // OptionalVisibility 152df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_protected: { // OptionalVisibility 153df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Visibility; 154df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseOptionalVisibility(Visibility) || 155eeb4a84ac8d91fb1d5a7c484a1c7047409faee30Chris Lattner ParseGlobal("", SMLoc(), 0, false, Visibility)) 156df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 157df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 158df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 159df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 160df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_thread_local: // OptionalThreadLocal 161df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_addrspace: // OptionalAddrSpace 162df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_constant: // GlobalType 163df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_global: // GlobalType 164eeb4a84ac8d91fb1d5a7c484a1c7047409faee30Chris Lattner if (ParseGlobal("", SMLoc(), 0, false, 0)) return true; 165df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 166df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 167df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 168df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 169df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 170df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 171df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// toplevelentity 172df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'module' 'asm' STRINGCONSTANT 173df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseModuleAsm() { 174df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Lex.getKind() == lltok::kw_module); 175df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 176df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1773ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner std::string AsmStr; 1783ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseToken(lltok::kw_asm, "expected 'module asm'") || 1793ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseStringConstant(AsmStr)) return true; 180df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 181df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const std::string &AsmSoFar = M->getModuleInlineAsm(); 182df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (AsmSoFar.empty()) 1833ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner M->setModuleInlineAsm(AsmStr); 184df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else 1853ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner M->setModuleInlineAsm(AsmSoFar+"\n"+AsmStr); 186df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 187df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 188df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 189df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// toplevelentity 190df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'target' 'triple' '=' STRINGCONSTANT 191df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'target' 'datalayout' '=' STRINGCONSTANT 192df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseTargetDefinition() { 193df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Lex.getKind() == lltok::kw_target); 1943ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner std::string Str; 195df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Lex.Lex()) { 196df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: return TokError("unknown target property"); 197df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_triple: 198df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1993ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseToken(lltok::equal, "expected '=' after target triple") || 2003ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseStringConstant(Str)) 201df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2023ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner M->setTargetTriple(Str); 203df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 204df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_datalayout: 205df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 2063ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseToken(lltok::equal, "expected '=' after target datalayout") || 2073ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseStringConstant(Str)) 208df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2093ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner M->setDataLayout(Str); 210df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 211df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 212df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 213df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 214df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// toplevelentity 215df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'deplibs' '=' '[' ']' 216df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']' 217df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseDepLibs() { 218df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Lex.getKind() == lltok::kw_deplibs); 2193ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner Lex.Lex(); 2203ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseToken(lltok::equal, "expected '=' after deplibs") || 2213ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseToken(lltok::lsquare, "expected '=' after deplibs")) 2223ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return true; 223df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2243ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (EatIfPresent(lltok::rsquare)) 225df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 226df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2273ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner std::string Str; 2283ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseStringConstant(Str)) return true; 2293ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner M->addLibrary(Str); 230df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2313ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner while (EatIfPresent(lltok::comma)) { 2323ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseStringConstant(Str)) return true; 2333ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner M->addLibrary(Str); 234df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 235df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2363ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return ParseToken(lltok::rsquare, "expected ']' at end of list"); 237df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 238df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 239df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// toplevelentity 240df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'type' type 241df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseUnnamedType() { 242df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Lex.getKind() == lltok::kw_type); 243df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy TypeLoc = Lex.getLoc(); 244df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); // eat kw_type 245df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 246df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder Ty(Type::VoidTy); 247df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseType(Ty)) return true; 248df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 249df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned TypeID = NumberedTypes.size(); 250df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 251df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // See if this type was previously referenced. 252df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<unsigned, std::pair<PATypeHolder, LocTy> >::iterator 253df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FI = ForwardRefTypeIDs.find(TypeID); 254df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FI != ForwardRefTypeIDs.end()) { 255c38daba25b66f9a0386fc28861db66e62663310dChris Lattner if (FI->second.first.get() == Ty) 256c38daba25b66f9a0386fc28861db66e62663310dChris Lattner return Error(TypeLoc, "self referential type is invalid"); 257c38daba25b66f9a0386fc28861db66e62663310dChris Lattner 258df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner cast<DerivedType>(FI->second.first.get())->refineAbstractTypeTo(Ty); 259df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Ty = FI->second.first.get(); 260df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefTypeIDs.erase(FI); 261df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 262df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 263df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner NumberedTypes.push_back(Ty); 264df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 265df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 266df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 267df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 268df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// toplevelentity 269df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= LocalVar '=' 'type' type 270df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseNamedType() { 271df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::string Name = Lex.getStrVal(); 272df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy NameLoc = Lex.getLoc(); 2733ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner Lex.Lex(); // eat LocalVar. 274df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 275df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder Ty(Type::VoidTy); 2763ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner 2773ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseToken(lltok::equal, "expected '=' after name") || 2783ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseToken(lltok::kw_type, "expected 'type' after name") || 2793ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseType(Ty)) 2803ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return true; 281df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 282df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Set the type name, checking for conflicts as we do so. 283df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner bool AlreadyExists = M->addTypeName(Name, Ty); 284df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!AlreadyExists) return false; 285df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 286df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // See if this type is a forward reference. We need to eagerly resolve 287df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // types to allow recursive type redefinitions below. 288df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<std::string, std::pair<PATypeHolder, LocTy> >::iterator 289df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FI = ForwardRefTypes.find(Name); 290df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FI != ForwardRefTypes.end()) { 291c38daba25b66f9a0386fc28861db66e62663310dChris Lattner if (FI->second.first.get() == Ty) 292c38daba25b66f9a0386fc28861db66e62663310dChris Lattner return Error(NameLoc, "self referential type is invalid"); 293c38daba25b66f9a0386fc28861db66e62663310dChris Lattner 294df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner cast<DerivedType>(FI->second.first.get())->refineAbstractTypeTo(Ty); 295df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Ty = FI->second.first.get(); 296df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefTypes.erase(FI); 297df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 298df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 299df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Inserting a name that is already defined, get the existing name. 300df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const Type *Existing = M->getTypeByName(Name); 301df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Existing && "Conflict but no matching type?!"); 302df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 303df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Otherwise, this is an attempt to redefine a type. That's okay if 304df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // the redefinition is identical to the original. 305df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: REMOVE REDEFINITIONS IN LLVM 3.0 306df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Existing == Ty) return false; 307df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 308df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Any other kind of (non-equivalent) redefinition is an error. 309df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(NameLoc, "redefinition of type named '" + Name + "' of type '" + 310df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Ty->getDescription() + "'"); 311df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 312df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 313df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 314df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// toplevelentity 315df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'declare' FunctionHeader 316df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseDeclare() { 317df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Lex.getKind() == lltok::kw_declare); 318df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 319df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 320df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Function *F; 321df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return ParseFunctionHeader(F, false); 322df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 323df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 324df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// toplevelentity 325df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'define' FunctionHeader '{' ... 326df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseDefine() { 327df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Lex.getKind() == lltok::kw_define); 328df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 329df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 330df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Function *F; 3313ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return ParseFunctionHeader(F, true) || 3323ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseFunctionBody(*F); 333df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 334df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3353ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ParseGlobalType 3363ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ::= 'constant' 3373ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ::= 'global' 338df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseGlobalType(bool &IsConstant) { 339df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() == lltok::kw_constant) 340df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner IsConstant = true; 341df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else if (Lex.getKind() == lltok::kw_global) 342df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner IsConstant = false; 34335b5107951582f58bf9d6f786fdddbf7811420fdDuncan Sands else { 34435b5107951582f58bf9d6f786fdddbf7811420fdDuncan Sands IsConstant = false; 345df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("expected 'global' or 'constant'"); 34635b5107951582f58bf9d6f786fdddbf7811420fdDuncan Sands } 347df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 348df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 349df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 350df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 351df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseNamedGlobal: 352df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// GlobalVar '=' OptionalVisibility ALIAS ... 353df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// GlobalVar '=' OptionalLinkage OptionalVisibility ... -> global variable 354df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseNamedGlobal() { 355df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Lex.getKind() == lltok::GlobalVar); 356df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy NameLoc = Lex.getLoc(); 357df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::string Name = Lex.getStrVal(); 358df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 359df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 360df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner bool HasLinkage; 361df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Linkage, Visibility; 362df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::equal, "expected '=' in global variable") || 363df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalLinkage(Linkage, HasLinkage) || 364df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalVisibility(Visibility)) 365df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 366df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 367df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (HasLinkage || Lex.getKind() != lltok::kw_alias) 368df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility); 369df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return ParseAlias(Name, NameLoc, Visibility); 370df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 371df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 372256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel// MDString: 373256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel// ::= '!' STRINGCONSTANT 374e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patelbool LLParser::ParseMDString(MetadataBase *&MDS) { 375256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel std::string Str; 376256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel if (ParseStringConstant(Str)) return true; 377647e3016de18d2fc8b0f233a0b356809e3fdcc54Owen Anderson MDS = MDString::get(Context, Str); 378256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel return false; 379256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel} 380256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel 381256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel// MDNode: 382256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel// ::= '!' MDNodeNumber 383104cf9e02b0ed94d4173869a598af6c6972a8660Devang Patelbool LLParser::ParseMDNode(MetadataBase *&Node) { 384256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel // !{ ..., !42, ... } 385256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel unsigned MID = 0; 386256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel if (ParseUInt32(MID)) return true; 387256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel 388256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel // Check existing MDNode. 389104cf9e02b0ed94d4173869a598af6c6972a8660Devang Patel std::map<unsigned, MetadataBase *>::iterator I = MetadataCache.find(MID); 390256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel if (I != MetadataCache.end()) { 391256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel Node = I->second; 392256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel return false; 393256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel } 394256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel 395256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel // Check known forward references. 396104cf9e02b0ed94d4173869a598af6c6972a8660Devang Patel std::map<unsigned, std::pair<MetadataBase *, LocTy> >::iterator 397256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel FI = ForwardRefMDNodes.find(MID); 398256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel if (FI != ForwardRefMDNodes.end()) { 399256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel Node = FI->second.first; 400256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel return false; 401256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel } 402256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel 403256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel // Create MDNode forward reference 404256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel SmallVector<Value *, 1> Elts; 405256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel std::string FwdRefName = "llvm.mdnode.fwdref." + utostr(MID); 406647e3016de18d2fc8b0f233a0b356809e3fdcc54Owen Anderson Elts.push_back(MDString::get(Context, FwdRefName)); 407647e3016de18d2fc8b0f233a0b356809e3fdcc54Owen Anderson MDNode *FwdNode = MDNode::get(Context, Elts.data(), Elts.size()); 408256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc()); 409256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel Node = FwdNode; 410256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel return false; 411256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel} 412256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel 413eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel///ParseNamedMetadata: 414eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel/// !foo = !{ !1, !2 } 415eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patelbool LLParser::ParseNamedMetadata() { 416eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel assert(Lex.getKind() == lltok::NamedMD); 417eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel Lex.Lex(); 418eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel std::string Name = Lex.getStrVal(); 419eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel 420eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel if (ParseToken(lltok::equal, "expected '=' here")) 421eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel return true; 422eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel 423eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel if (Lex.getKind() != lltok::Metadata) 424eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel return TokError("Expected '!' here"); 425eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel Lex.Lex(); 426eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel 427eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel if (Lex.getKind() != lltok::lbrace) 428eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel return TokError("Expected '{' here"); 429eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel Lex.Lex(); 430eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel SmallVector<MetadataBase *, 8> Elts; 431eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel do { 432eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel if (Lex.getKind() != lltok::Metadata) 433eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel return TokError("Expected '!' here"); 434eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel Lex.Lex(); 435eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel MetadataBase *N = 0; 436eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel if (ParseMDNode(N)) return true; 437eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel Elts.push_back(N); 438eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel } while (EatIfPresent(lltok::comma)); 439eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel 440eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel if (ParseToken(lltok::rbrace, "expected end of metadata node")) 441eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel return true; 442eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel 4435316bf0252ce9797e90a233645b6580695403837Devang Patel NamedMDNode::Create(Name, Elts.data(), Elts.size(), M); 444eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel return false; 445eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel} 446eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel 447923078c65d5a37a4f135705300c9feea49487de5Devang Patel/// ParseStandaloneMetadata: 448923078c65d5a37a4f135705300c9feea49487de5Devang Patel/// !42 = !{...} 449923078c65d5a37a4f135705300c9feea49487de5Devang Patelbool LLParser::ParseStandaloneMetadata() { 450923078c65d5a37a4f135705300c9feea49487de5Devang Patel assert(Lex.getKind() == lltok::Metadata); 451923078c65d5a37a4f135705300c9feea49487de5Devang Patel Lex.Lex(); 452923078c65d5a37a4f135705300c9feea49487de5Devang Patel unsigned MetadataID = 0; 453923078c65d5a37a4f135705300c9feea49487de5Devang Patel if (ParseUInt32(MetadataID)) 454923078c65d5a37a4f135705300c9feea49487de5Devang Patel return true; 455923078c65d5a37a4f135705300c9feea49487de5Devang Patel if (MetadataCache.find(MetadataID) != MetadataCache.end()) 456923078c65d5a37a4f135705300c9feea49487de5Devang Patel return TokError("Metadata id is already used"); 457923078c65d5a37a4f135705300c9feea49487de5Devang Patel if (ParseToken(lltok::equal, "expected '=' here")) 458923078c65d5a37a4f135705300c9feea49487de5Devang Patel return true; 459923078c65d5a37a4f135705300c9feea49487de5Devang Patel 460923078c65d5a37a4f135705300c9feea49487de5Devang Patel LocTy TyLoc; 461923078c65d5a37a4f135705300c9feea49487de5Devang Patel PATypeHolder Ty(Type::VoidTy); 4622214c94bb70524ff3150c6763c2f9ac0c1bb844bDevang Patel if (ParseType(Ty, TyLoc)) 463923078c65d5a37a4f135705300c9feea49487de5Devang Patel return true; 464923078c65d5a37a4f135705300c9feea49487de5Devang Patel 465104cf9e02b0ed94d4173869a598af6c6972a8660Devang Patel if (Lex.getKind() != lltok::Metadata) 466104cf9e02b0ed94d4173869a598af6c6972a8660Devang Patel return TokError("Expected metadata here"); 467104cf9e02b0ed94d4173869a598af6c6972a8660Devang Patel 468104cf9e02b0ed94d4173869a598af6c6972a8660Devang Patel Lex.Lex(); 469104cf9e02b0ed94d4173869a598af6c6972a8660Devang Patel if (Lex.getKind() != lltok::lbrace) 470104cf9e02b0ed94d4173869a598af6c6972a8660Devang Patel return TokError("Expected '{' here"); 471923078c65d5a37a4f135705300c9feea49487de5Devang Patel 472104cf9e02b0ed94d4173869a598af6c6972a8660Devang Patel SmallVector<Value *, 16> Elts; 473104cf9e02b0ed94d4173869a598af6c6972a8660Devang Patel if (ParseMDNodeVector(Elts) 47430d3b911c97b28ddeda6aba15128cab55cd2cfb2Benjamin Kramer || ParseToken(lltok::rbrace, "expected end of metadata node")) 475104cf9e02b0ed94d4173869a598af6c6972a8660Devang Patel return true; 476104cf9e02b0ed94d4173869a598af6c6972a8660Devang Patel 477647e3016de18d2fc8b0f233a0b356809e3fdcc54Owen Anderson MDNode *Init = MDNode::get(Context, Elts.data(), Elts.size()); 478923078c65d5a37a4f135705300c9feea49487de5Devang Patel MetadataCache[MetadataID] = Init; 479104cf9e02b0ed94d4173869a598af6c6972a8660Devang Patel std::map<unsigned, std::pair<MetadataBase *, LocTy> >::iterator 4801c7eea60d3cbada2bae54c42fa178078a3ace9b3Devang Patel FI = ForwardRefMDNodes.find(MetadataID); 4811c7eea60d3cbada2bae54c42fa178078a3ace9b3Devang Patel if (FI != ForwardRefMDNodes.end()) { 482104cf9e02b0ed94d4173869a598af6c6972a8660Devang Patel MDNode *FwdNode = cast<MDNode>(FI->second.first); 4831c7eea60d3cbada2bae54c42fa178078a3ace9b3Devang Patel FwdNode->replaceAllUsesWith(Init); 4841c7eea60d3cbada2bae54c42fa178078a3ace9b3Devang Patel ForwardRefMDNodes.erase(FI); 4851c7eea60d3cbada2bae54c42fa178078a3ace9b3Devang Patel } 4861c7eea60d3cbada2bae54c42fa178078a3ace9b3Devang Patel 487923078c65d5a37a4f135705300c9feea49487de5Devang Patel return false; 488923078c65d5a37a4f135705300c9feea49487de5Devang Patel} 489923078c65d5a37a4f135705300c9feea49487de5Devang Patel 490df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseAlias: 491df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= GlobalVar '=' OptionalVisibility 'alias' OptionalLinkage Aliasee 492df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// Aliasee 493040f758e3abc75bdf6ddb1ce7bea1e1ed1293ccfChris Lattner/// ::= TypeAndValue 494040f758e3abc75bdf6ddb1ce7bea1e1ed1293ccfChris Lattner/// ::= 'bitcast' '(' TypeAndValue 'to' Type ')' 495dd8004dc73d091ccb3927dbbc3b41639a3738ae3Dan Gohman/// ::= 'getelementptr' 'inbounds'? '(' ... ')' 496df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// 497df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// Everything through visibility has already been parsed. 498df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// 499df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc, 500df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Visibility) { 501df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Lex.getKind() == lltok::kw_alias); 502df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 503df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Linkage; 504df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy LinkageLoc = Lex.getLoc(); 505df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseOptionalLinkage(Linkage)) 506df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 507df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 508df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Linkage != GlobalValue::ExternalLinkage && 509667d4b8de6dea70195ff12ef39a4deebffa2f5c7Duncan Sands Linkage != GlobalValue::WeakAnyLinkage && 510667d4b8de6dea70195ff12ef39a4deebffa2f5c7Duncan Sands Linkage != GlobalValue::WeakODRLinkage && 511bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola Linkage != GlobalValue::InternalLinkage && 5123d10a5a75794356a0a568ce283713adc3a963200Bill Wendling Linkage != GlobalValue::PrivateLinkage && 5133d10a5a75794356a0a568ce283713adc3a963200Bill Wendling Linkage != GlobalValue::LinkerPrivateLinkage) 514df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(LinkageLoc, "invalid linkage type for alias"); 515df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 516df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *Aliasee; 517df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy AliaseeLoc = Lex.getLoc(); 518040f758e3abc75bdf6ddb1ce7bea1e1ed1293ccfChris Lattner if (Lex.getKind() != lltok::kw_bitcast && 519040f758e3abc75bdf6ddb1ce7bea1e1ed1293ccfChris Lattner Lex.getKind() != lltok::kw_getelementptr) { 520df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseGlobalTypeAndValue(Aliasee)) return true; 521df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 522df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // The bitcast dest type is not present, it is implied by the dest type. 523df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ValID ID; 524df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseValID(ID)) return true; 525df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ID.Kind != ValID::t_Constant) 526df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(AliaseeLoc, "invalid aliasee"); 527df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Aliasee = ID.ConstantVal; 528df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 529df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 530df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<PointerType>(Aliasee->getType())) 531df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(AliaseeLoc, "alias must have pointer type"); 532df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 533df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Okay, create the alias but do not insert it into the module yet. 534df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), 535df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner (GlobalValue::LinkageTypes)Linkage, Name, 536df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Aliasee); 537df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GA->setVisibility((GlobalValue::VisibilityTypes)Visibility); 538df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 539df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // See if this value already exists in the symbol table. If so, it is either 540df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // a redefinition or a definition of a forward reference. 541df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (GlobalValue *Val = 542df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name))) { 543df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // See if this was a redefinition. If so, there is no entry in 544df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // ForwardRefVals. 545df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator 546df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I = ForwardRefVals.find(Name); 547df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I == ForwardRefVals.end()) 548df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(NameLoc, "redefinition of global named '@" + Name + "'"); 549df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 550df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Otherwise, this was a definition of forward ref. Verify that types 551df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // agree. 552df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val->getType() != GA->getType()) 553df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(NameLoc, 554df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "forward reference and definition of alias have different types"); 555df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 556df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If they agree, just RAUW the old value with the alias and remove the 557df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // forward ref info. 558df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Val->replaceAllUsesWith(GA); 559df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Val->eraseFromParent(); 560df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefVals.erase(I); 561df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 562df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 563df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Insert into the module, we know its name won't collide now. 564df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner M->getAliasList().push_back(GA); 565df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(GA->getNameStr() == Name && "Should not be a name conflict!"); 566df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 567df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 568df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 569df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 570df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseGlobal 571df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalThreadLocal 572df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// OptionalAddrSpace GlobalType Type Const 573df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= OptionalLinkage OptionalVisibility OptionalThreadLocal 574df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// OptionalAddrSpace GlobalType Type Const 575df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// 576df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// Everything through visibility has been parsed already. 577df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// 578df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc, 579df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Linkage, bool HasLinkage, 580df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Visibility) { 581df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned AddrSpace; 582df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner bool ThreadLocal, IsConstant; 583df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy TyLoc; 584df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 585df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder Ty(Type::VoidTy); 586df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseOptionalToken(lltok::kw_thread_local, ThreadLocal) || 587df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalAddrSpace(AddrSpace) || 588df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalType(IsConstant) || 589df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseType(Ty, TyLoc)) 590df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 591df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 592df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If the linkage is specified and is external, then no initializer is 593df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // present. 594df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *Init = 0; 595df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!HasLinkage || (Linkage != GlobalValue::DLLImportLinkage && 5965f4ee1fc5d00ae55c30fa2ce450c69be4c6d6e63Duncan Sands Linkage != GlobalValue::ExternalWeakLinkage && 597df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Linkage != GlobalValue::ExternalLinkage)) { 598df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseGlobalValue(Ty, Init)) 599df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 600df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 601df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 602a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner if (isa<FunctionType>(Ty) || Ty == Type::LabelTy) 6034a2f112a5c774cb1ab3fd890a88ba1db9709f561Chris Lattner return Error(TyLoc, "invalid type for global variable"); 604df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 605df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GlobalVariable *GV = 0; 606df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 607df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // See if the global was forward referenced, if so, use the global. 60891dad87ddc1e69ac5fa8953a583de4185e1dd0d7Chris Lattner if (!Name.empty()) { 60991dad87ddc1e69ac5fa8953a583de4185e1dd0d7Chris Lattner if ((GV = M->getGlobalVariable(Name, true)) && 61091dad87ddc1e69ac5fa8953a583de4185e1dd0d7Chris Lattner !ForwardRefVals.erase(Name)) 611df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(NameLoc, "redefinition of global '@" + Name + "'"); 612df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 613df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator 614df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I = ForwardRefValIDs.find(NumberedVals.size()); 615df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I != ForwardRefValIDs.end()) { 616df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GV = cast<GlobalVariable>(I->second.first); 617df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefValIDs.erase(I); 618df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 619df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 620df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 621df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (GV == 0) { 622e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, 0, 623e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson Name, 0, false, AddrSpace); 624df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 625df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (GV->getType()->getElementType() != Ty) 626df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(TyLoc, 627df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "forward reference and definition of global have different types"); 628df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 629df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Move the forward-reference to the correct spot in the module. 630df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV); 631df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 632df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 633df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Name.empty()) 634df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner NumberedVals.push_back(GV); 635df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 636df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Set the parsed properties on the global. 637df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Init) 638df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GV->setInitializer(Init); 639df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GV->setConstant(IsConstant); 640df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GV->setLinkage((GlobalValue::LinkageTypes)Linkage); 641df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GV->setVisibility((GlobalValue::VisibilityTypes)Visibility); 642df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GV->setThreadLocal(ThreadLocal); 643df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 644df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Parse attributes on the global. 645df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner while (Lex.getKind() == lltok::comma) { 646df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 647df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 648df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() == lltok::kw_section) { 649df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 650df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GV->setSection(Lex.getStrVal()); 651df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::StringConstant, "expected global section string")) 652df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 653df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (Lex.getKind() == lltok::kw_align) { 654df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Alignment; 655df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseOptionalAlignment(Alignment)) return true; 656df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GV->setAlignment(Alignment); 657df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 658df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner TokError("unknown global variable property!"); 659df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 660df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 661df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 662df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 663df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 664df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 665df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 666df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 667df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// GlobalValue Reference/Resolution Routines. 668df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 669df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 670df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// GetGlobalVal - Get a value with the specified name or ID, creating a 671df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// forward reference record if needed. This can return null if the value 672df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// exists but does not have the right type. 673df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerGlobalValue *LLParser::GetGlobalVal(const std::string &Name, const Type *Ty, 674df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc) { 675df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const PointerType *PTy = dyn_cast<PointerType>(Ty); 676df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (PTy == 0) { 677df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Error(Loc, "global variable reference must have pointer type"); 678df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return 0; 679df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 680df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 681df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Look this name up in the normal function symbol table. 682df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GlobalValue *Val = 683df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name)); 684df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 685df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If this is a forward reference for the value, see if we already created a 686df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // forward ref record. 687df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val == 0) { 688df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator 689df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I = ForwardRefVals.find(Name); 690df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I != ForwardRefVals.end()) 691df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Val = I->second.first; 692df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 693df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 694df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If we have the value in the symbol table or fwd-ref table, return it. 695df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val) { 696df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val->getType() == Ty) return Val; 697df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Error(Loc, "'@" + Name + "' defined with type '" + 698df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Val->getType()->getDescription() + "'"); 699df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return 0; 700df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 701df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 702df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Otherwise, create a new forward reference for this value and remember it. 703df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GlobalValue *FwdVal; 7041e407c338f6944242facb1e0c32903162cc366f0Chris Lattner if (const FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType())) { 7051e407c338f6944242facb1e0c32903162cc366f0Chris Lattner // Function types can return opaque but functions can't. 7061e407c338f6944242facb1e0c32903162cc366f0Chris Lattner if (isa<OpaqueType>(FT->getReturnType())) { 7071e407c338f6944242facb1e0c32903162cc366f0Chris Lattner Error(Loc, "function may not return opaque type"); 7081e407c338f6944242facb1e0c32903162cc366f0Chris Lattner return 0; 7091e407c338f6944242facb1e0c32903162cc366f0Chris Lattner } 7101e407c338f6944242facb1e0c32903162cc366f0Chris Lattner 7115f4ee1fc5d00ae55c30fa2ce450c69be4c6d6e63Duncan Sands FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M); 7121e407c338f6944242facb1e0c32903162cc366f0Chris Lattner } else { 713e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson FwdVal = new GlobalVariable(*M, PTy->getElementType(), false, 714e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson GlobalValue::ExternalWeakLinkage, 0, Name); 7151e407c338f6944242facb1e0c32903162cc366f0Chris Lattner } 716df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 717df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefVals[Name] = std::make_pair(FwdVal, Loc); 718df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return FwdVal; 719df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 720df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 721df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerGlobalValue *LLParser::GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc) { 722df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const PointerType *PTy = dyn_cast<PointerType>(Ty); 723df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (PTy == 0) { 724df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Error(Loc, "global variable reference must have pointer type"); 725df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return 0; 726df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 727df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 728df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0; 729df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 730df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If this is a forward reference for the value, see if we already created a 731df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // forward ref record. 732df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val == 0) { 733df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator 734df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I = ForwardRefValIDs.find(ID); 735df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I != ForwardRefValIDs.end()) 736df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Val = I->second.first; 737df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 738df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 739df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If we have the value in the symbol table or fwd-ref table, return it. 740df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val) { 741df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val->getType() == Ty) return Val; 742df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Error(Loc, "'@" + utostr(ID) + "' defined with type '" + 743df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Val->getType()->getDescription() + "'"); 744df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return 0; 745df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 746df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 747df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Otherwise, create a new forward reference for this value and remember it. 748df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GlobalValue *FwdVal; 749830703b1bd88511e29eec76b1fd146524bc9c5a1Chris Lattner if (const FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType())) { 750830703b1bd88511e29eec76b1fd146524bc9c5a1Chris Lattner // Function types can return opaque but functions can't. 751830703b1bd88511e29eec76b1fd146524bc9c5a1Chris Lattner if (isa<OpaqueType>(FT->getReturnType())) { 7520d8484f2edb1069522f5c3de6467433e0fbe9323Chris Lattner Error(Loc, "function may not return opaque type"); 753830703b1bd88511e29eec76b1fd146524bc9c5a1Chris Lattner return 0; 754830703b1bd88511e29eec76b1fd146524bc9c5a1Chris Lattner } 7555f4ee1fc5d00ae55c30fa2ce450c69be4c6d6e63Duncan Sands FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M); 756830703b1bd88511e29eec76b1fd146524bc9c5a1Chris Lattner } else { 757e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson FwdVal = new GlobalVariable(*M, PTy->getElementType(), false, 758e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson GlobalValue::ExternalWeakLinkage, 0, ""); 759830703b1bd88511e29eec76b1fd146524bc9c5a1Chris Lattner } 760df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 761df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc); 762df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return FwdVal; 763df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 764df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 765df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 766df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 767df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Helper Routines. 768df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 769df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 770df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseToken - If the current token has the specified kind, eat it and return 771df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// success. Otherwise, emit the specified error and return failure. 772df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) { 773df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() != T) 774df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError(ErrMsg); 775df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 776df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 777df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 778df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 7793ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ParseStringConstant 7803ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ::= StringConstant 7813ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattnerbool LLParser::ParseStringConstant(std::string &Result) { 7823ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (Lex.getKind() != lltok::StringConstant) 7833ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return TokError("expected string constant"); 7843ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner Result = Lex.getStrVal(); 7853ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner Lex.Lex(); 7863ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return false; 7873ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner} 7883ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner 7893ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ParseUInt32 7903ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ::= uint32 7913ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattnerbool LLParser::ParseUInt32(unsigned &Val) { 792df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned()) 793df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("expected integer"); 794df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1); 795df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val64 != unsigned(Val64)) 796df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("expected 32-bit integer (too large)"); 797df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Val = Val64; 798df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 799df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 800df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 801df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 802df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 803df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseOptionalAddrSpace 804df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// := /*empty*/ 805df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// := 'addrspace' '(' uint32 ')' 806df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) { 807df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner AddrSpace = 0; 8083ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (!EatIfPresent(lltok::kw_addrspace)) 809df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 810df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return ParseToken(lltok::lparen, "expected '(' in address space") || 8113ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseUInt32(AddrSpace) || 812df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rparen, "expected ')' in address space"); 813df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 814df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 815df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseOptionalAttrs - Parse a potentially empty attribute list. AttrKind 816df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// indicates what kind of attribute list this is: 0: function arg, 1: result, 817df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// 2: function attr. 818ad9ad7c827b585c018288c89d611d8c97d8d2e7dChris Lattner/// 3: function arg after value: FIXME: REMOVE IN LLVM 3.0 819df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind) { 820df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs = Attribute::None; 821df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy AttrLoc = Lex.getLoc(); 822df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 823df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner while (1) { 824df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Lex.getKind()) { 825df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sext: 826df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_zext: 827ad9ad7c827b585c018288c89d611d8c97d8d2e7dChris Lattner // Treat these as signext/zeroext if they occur in the argument list after 828ad9ad7c827b585c018288c89d611d8c97d8d2e7dChris Lattner // the value, as in "call i8 @foo(i8 10 sext)". If they occur before the 829ad9ad7c827b585c018288c89d611d8c97d8d2e7dChris Lattner // value, as in "call i8 @foo(i8 sext (" then it is part of a constant 830ad9ad7c827b585c018288c89d611d8c97d8d2e7dChris Lattner // expr. 831df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: REMOVE THIS IN LLVM 3.0 832ad9ad7c827b585c018288c89d611d8c97d8d2e7dChris Lattner if (AttrKind == 3) { 833df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() == lltok::kw_sext) 834df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs |= Attribute::SExt; 835df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else 836df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs |= Attribute::ZExt; 837df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 838df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 839df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FALL THROUGH. 840df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: // End of attributes. 841df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (AttrKind != 2 && (Attrs & Attribute::FunctionOnly)) 842df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(AttrLoc, "invalid use of function-only attribute"); 843df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 844ad9ad7c827b585c018288c89d611d8c97d8d2e7dChris Lattner if (AttrKind != 0 && AttrKind != 3 && (Attrs & Attribute::ParameterOnly)) 845df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(AttrLoc, "invalid use of parameter-only attribute"); 846df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 847df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 848578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel case lltok::kw_zeroext: Attrs |= Attribute::ZExt; break; 849578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel case lltok::kw_signext: Attrs |= Attribute::SExt; break; 850578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel case lltok::kw_inreg: Attrs |= Attribute::InReg; break; 851578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel case lltok::kw_sret: Attrs |= Attribute::StructRet; break; 852578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel case lltok::kw_noalias: Attrs |= Attribute::NoAlias; break; 853578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel case lltok::kw_nocapture: Attrs |= Attribute::NoCapture; break; 854578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel case lltok::kw_byval: Attrs |= Attribute::ByVal; break; 855578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel case lltok::kw_nest: Attrs |= Attribute::Nest; break; 856578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel 857578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel case lltok::kw_noreturn: Attrs |= Attribute::NoReturn; break; 858578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel case lltok::kw_nounwind: Attrs |= Attribute::NoUnwind; break; 859578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel case lltok::kw_noinline: Attrs |= Attribute::NoInline; break; 860578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel case lltok::kw_readnone: Attrs |= Attribute::ReadNone; break; 861578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel case lltok::kw_readonly: Attrs |= Attribute::ReadOnly; break; 862578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel case lltok::kw_alwaysinline: Attrs |= Attribute::AlwaysInline; break; 863578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel case lltok::kw_optsize: Attrs |= Attribute::OptimizeForSize; break; 864578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel case lltok::kw_ssp: Attrs |= Attribute::StackProtect; break; 865578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel case lltok::kw_sspreq: Attrs |= Attribute::StackProtectReq; break; 866578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel case lltok::kw_noredzone: Attrs |= Attribute::NoRedZone; break; 867578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel case lltok::kw_noimplicitfloat: Attrs |= Attribute::NoImplicitFloat; break; 868c5ec8a78ea898087ad361e5b755f74a76150e5fdAnton Korobeynikov case lltok::kw_naked: Attrs |= Attribute::Naked; break; 869df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 870df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_align: { 871df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Alignment; 872df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseOptionalAlignment(Alignment)) 873df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 874df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs |= Attribute::constructAlignmentFromInt(Alignment); 875df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner continue; 876df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 877df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 878df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 879df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 880df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 881df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 882df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseOptionalLinkage 883df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= /*empty*/ 884bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola/// ::= 'private' 8853d10a5a75794356a0a568ce283713adc3a963200Bill Wendling/// ::= 'linker_private' 886df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'internal' 887df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'weak' 888667d4b8de6dea70195ff12ef39a4deebffa2f5c7Duncan Sands/// ::= 'weak_odr' 889df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'linkonce' 890667d4b8de6dea70195ff12ef39a4deebffa2f5c7Duncan Sands/// ::= 'linkonce_odr' 891df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'appending' 892df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'dllexport' 893df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'common' 894df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'dllimport' 895df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'extern_weak' 896df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'external' 897df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) { 898df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner HasLinkage = false; 899df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Lex.getKind()) { 9003d10a5a75794356a0a568ce283713adc3a963200Bill Wendling default: Res=GlobalValue::ExternalLinkage; return false; 9013d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break; 9023d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_linker_private: Res = GlobalValue::LinkerPrivateLinkage; break; 9033d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break; 9043d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break; 9053d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break; 9063d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break; 9073d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break; 908266c7bbbbcc4b326dea82e577de1a415d6acc23eChris Lattner case lltok::kw_available_externally: 909266c7bbbbcc4b326dea82e577de1a415d6acc23eChris Lattner Res = GlobalValue::AvailableExternallyLinkage; 910266c7bbbbcc4b326dea82e577de1a415d6acc23eChris Lattner break; 9113d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break; 9123d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_dllexport: Res = GlobalValue::DLLExportLinkage; break; 9133d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_common: Res = GlobalValue::CommonLinkage; break; 9143d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_dllimport: Res = GlobalValue::DLLImportLinkage; break; 9153d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break; 9163d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break; 917df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 918df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 919df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner HasLinkage = true; 920df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 921df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 922df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 923df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseOptionalVisibility 924df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= /*empty*/ 925df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'default' 926df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'hidden' 927df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'protected' 928df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// 929df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseOptionalVisibility(unsigned &Res) { 930df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Lex.getKind()) { 931df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: Res = GlobalValue::DefaultVisibility; return false; 932df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break; 933df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break; 934df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break; 935df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 936df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 937df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 938df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 939df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 940df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseOptionalCallingConv 941df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= /*empty*/ 942df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'ccc' 943df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'fastcc' 944df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'coldcc' 945df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'x86_stdcallcc' 946df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'x86_fastcallcc' 947385f5a99ecc7fee48a7539bc63d3e1d3b5089c0dAnton Korobeynikov/// ::= 'arm_apcscc' 948385f5a99ecc7fee48a7539bc63d3e1d3b5089c0dAnton Korobeynikov/// ::= 'arm_aapcscc' 949385f5a99ecc7fee48a7539bc63d3e1d3b5089c0dAnton Korobeynikov/// ::= 'arm_aapcs_vfpcc' 950df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'cc' UINT 951385f5a99ecc7fee48a7539bc63d3e1d3b5089c0dAnton Korobeynikov/// 952df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseOptionalCallingConv(unsigned &CC) { 953df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Lex.getKind()) { 954df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: CC = CallingConv::C; return false; 955df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ccc: CC = CallingConv::C; break; 956df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_fastcc: CC = CallingConv::Fast; break; 957df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_coldcc: CC = CallingConv::Cold; break; 958df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break; 959df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break; 960385f5a99ecc7fee48a7539bc63d3e1d3b5089c0dAnton Korobeynikov case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break; 961385f5a99ecc7fee48a7539bc63d3e1d3b5089c0dAnton Korobeynikov case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break; 962385f5a99ecc7fee48a7539bc63d3e1d3b5089c0dAnton Korobeynikov case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break; 9633ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner case lltok::kw_cc: Lex.Lex(); return ParseUInt32(CC); 964df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 965df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 966df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 967df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 968df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 969df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseOptionalAlignment 970df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= /* empty */ 971df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'align' 4 972df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseOptionalAlignment(unsigned &Alignment) { 973df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Alignment = 0; 9743ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (!EatIfPresent(lltok::kw_align)) 9753ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return false; 9763fbb3ab14d86f752a933b5af4e6728c5e57e1155Chris Lattner LocTy AlignLoc = Lex.getLoc(); 9773fbb3ab14d86f752a933b5af4e6728c5e57e1155Chris Lattner if (ParseUInt32(Alignment)) return true; 9783fbb3ab14d86f752a933b5af4e6728c5e57e1155Chris Lattner if (!isPowerOf2_32(Alignment)) 9793fbb3ab14d86f752a933b5af4e6728c5e57e1155Chris Lattner return Error(AlignLoc, "alignment is not a power of two"); 9803fbb3ab14d86f752a933b5af4e6728c5e57e1155Chris Lattner return false; 981df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 982df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 983df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseOptionalCommaAlignment 984df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= /* empty */ 985df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= ',' 'align' 4 986df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseOptionalCommaAlignment(unsigned &Alignment) { 987df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Alignment = 0; 9883ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (!EatIfPresent(lltok::comma)) 989df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 990df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return ParseToken(lltok::kw_align, "expected 'align'") || 9913ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseUInt32(Alignment); 992df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 993df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 994df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseIndexList 995df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= (',' uint32)+ 996df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices) { 997df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() != lltok::comma) 998df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("expected ',' as start of index list"); 999df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 10003ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner while (EatIfPresent(lltok::comma)) { 1001df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Idx; 10023ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseUInt32(Idx)) return true; 1003df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Indices.push_back(Idx); 1004df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1005df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1006df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1007df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1008df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1009df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 1010df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Type Parsing. 1011df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 1012df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1013df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseType - Parse and resolve a full type. 1014a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattnerbool LLParser::ParseType(PATypeHolder &Result, bool AllowVoid) { 1015a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner LocTy TypeLoc = Lex.getLoc(); 1016df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeRec(Result)) return true; 1017df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1018df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Verify no unresolved uprefs. 1019df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!UpRefs.empty()) 1020df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(UpRefs.back().Loc, "invalid unresolved type up reference"); 1021df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1022a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner if (!AllowVoid && Result.get() == Type::VoidTy) 1023a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner return Error(TypeLoc, "void type only allowed for function results"); 1024a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner 1025df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1026df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1027df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1028df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// HandleUpRefs - Every time we finish a new layer of types, this function is 1029df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// called. It loops through the UpRefs vector, which is a list of the 1030df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// currently active types. For each type, if the up-reference is contained in 1031df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// the newly completed type, we decrement the level count. When the level 1032df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// count reaches zero, the up-referenced type is the type that is passed in: 1033df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// thus we can complete the cycle. 1034df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// 1035df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerPATypeHolder LLParser::HandleUpRefs(const Type *ty) { 1036df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If Ty isn't abstract, or if there are no up-references in it, then there is 1037df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // nothing to resolve here. 1038df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ty->isAbstract() || UpRefs.empty()) return ty; 1039df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1040df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder Ty(ty); 1041df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#if 0 1042df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner errs() << "Type '" << Ty->getDescription() 1043df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner << "' newly formed. Resolving upreferences.\n" 1044df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner << UpRefs.size() << " upreferences active!\n"; 1045df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#endif 1046df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1047df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If we find any resolvable upreferences (i.e., those whose NestingLevel goes 1048df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // to zero), we resolve them all together before we resolve them to Ty. At 1049df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // the end of the loop, if there is anything to resolve to Ty, it will be in 1050df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // this variable. 1051df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner OpaqueType *TypeToResolve = 0; 1052df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1053df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0; i != UpRefs.size(); ++i) { 1054df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Determine if 'Ty' directly contains this up-references 'LastContainedTy'. 1055df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner bool ContainsType = 1056df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::find(Ty->subtype_begin(), Ty->subtype_end(), 1057df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner UpRefs[i].LastContainedTy) != Ty->subtype_end(); 1058df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1059df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#if 0 1060df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner errs() << " UR#" << i << " - TypeContains(" << Ty->getDescription() << ", " 1061df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner << UpRefs[i].LastContainedTy->getDescription() << ") = " 1062df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner << (ContainsType ? "true" : "false") 1063df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner << " level=" << UpRefs[i].NestingLevel << "\n"; 1064df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#endif 1065df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ContainsType) 1066df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner continue; 1067df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1068df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Decrement level of upreference 1069df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Level = --UpRefs[i].NestingLevel; 1070df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner UpRefs[i].LastContainedTy = Ty; 1071df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1072df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If the Up-reference has a non-zero level, it shouldn't be resolved yet. 1073df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Level != 0) 1074df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner continue; 1075df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1076df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#if 0 1077df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner errs() << " * Resolving upreference for " << UpRefs[i].UpRefTy << "\n"; 1078df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#endif 1079df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!TypeToResolve) 1080df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner TypeToResolve = UpRefs[i].UpRefTy; 1081df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else 1082df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve); 1083df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list. 1084df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner --i; // Do not skip the next element. 1085df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1086df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1087df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (TypeToResolve) 1088df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner TypeToResolve->refineAbstractTypeTo(Ty); 1089df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1090df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Ty; 1091df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1092df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1093df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1094df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseTypeRec - The recursive function used to process the internal 1095df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// implementation details of types. 1096df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseTypeRec(PATypeHolder &Result) { 1097df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Lex.getKind()) { 1098df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: 1099df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("expected type"); 1100df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::Type: 1101df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // TypeRec ::= 'float' | 'void' (etc) 1102df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Result = Lex.getTyVal(); 1103df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1104df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1105df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_opaque: 1106df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // TypeRec ::= 'opaque' 1107debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson Result = OpaqueType::get(); 1108df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1109df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1110df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::lbrace: 1111df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // TypeRec ::= '{' ... '}' 1112df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseStructType(Result, false)) 1113df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1114df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1115df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::lsquare: 1116df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // TypeRec ::= '[' ... ']' 1117df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); // eat the lsquare. 1118df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseArrayVectorType(Result, false)) 1119df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1120df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1121df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::less: // Either vector or packed struct. 1122df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // TypeRec ::= '<' ... '>' 11233ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner Lex.Lex(); 11243ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (Lex.getKind() == lltok::lbrace) { 11253ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseStructType(Result, true) || 11263ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseToken(lltok::greater, "expected '>' at end of packed struct")) 1127df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1128df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (ParseArrayVectorType(Result, true)) 1129df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1130df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1131df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::LocalVar: 1132df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::StringConstant: // FIXME: REMOVE IN LLVM 3.0 1133df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // TypeRec ::= %foo 1134df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (const Type *T = M->getTypeByName(Lex.getStrVal())) { 1135df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Result = T; 1136df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 1137debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson Result = OpaqueType::get(); 1138df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefTypes.insert(std::make_pair(Lex.getStrVal(), 1139df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::make_pair(Result, 1140df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.getLoc()))); 1141df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner M->addTypeName(Lex.getStrVal(), Result.get()); 1142df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1143df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1144df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1145df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1146df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::LocalVarID: 1147df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // TypeRec ::= %4 1148df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getUIntVal() < NumberedTypes.size()) 1149df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Result = NumberedTypes[Lex.getUIntVal()]; 1150df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else { 1151df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<unsigned, std::pair<PATypeHolder, LocTy> >::iterator 1152df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I = ForwardRefTypeIDs.find(Lex.getUIntVal()); 1153df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I != ForwardRefTypeIDs.end()) 1154df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Result = I->second.first; 1155df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else { 1156debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson Result = OpaqueType::get(); 1157df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefTypeIDs.insert(std::make_pair(Lex.getUIntVal(), 1158df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::make_pair(Result, 1159df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.getLoc()))); 1160df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1161df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1162df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1163df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1164df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::backslash: { 1165df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // TypeRec ::= '\' 4 1166df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 11673ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner unsigned Val; 11683ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseUInt32(Val)) return true; 1169debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson OpaqueType *OT = OpaqueType::get(); //Use temporary placeholder. 1170df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner UpRefs.push_back(UpRefRecord(Lex.getLoc(), Val, OT)); 1171df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Result = OT; 1172df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1173df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1174df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1175df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1176df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Parse the type suffixes. 1177df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner while (1) { 1178df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Lex.getKind()) { 1179df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // End of type. 1180df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: return false; 1181df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1182df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // TypeRec ::= TypeRec '*' 1183df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::star: 1184df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Result.get() == Type::LabelTy) 1185df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("basic block pointers are invalid"); 1186b4bd16fc5e3dd416cf68e73cff93544d364159d6Chris Lattner if (Result.get() == Type::VoidTy) 1187b9070d3f6309929e47503e2790a368592b4bd201Dan Gohman return TokError("pointers to void are invalid; use i8* instead"); 1188a5f54a06b09acf5c4ed3571039f262128226145bNick Lewycky if (!PointerType::isValidElementType(Result.get())) 1189a5f54a06b09acf5c4ed3571039f262128226145bNick Lewycky return TokError("pointer to this type is invalid"); 1190debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson Result = HandleUpRefs(PointerType::getUnqual(Result.get())); 1191df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1192df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1193df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1194df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // TypeRec ::= TypeRec 'addrspace' '(' uint32 ')' '*' 1195df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_addrspace: { 1196df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Result.get() == Type::LabelTy) 1197df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("basic block pointers are invalid"); 1198b4bd16fc5e3dd416cf68e73cff93544d364159d6Chris Lattner if (Result.get() == Type::VoidTy) 1199b9070d3f6309929e47503e2790a368592b4bd201Dan Gohman return TokError("pointers to void are invalid; use i8* instead"); 1200a5f54a06b09acf5c4ed3571039f262128226145bNick Lewycky if (!PointerType::isValidElementType(Result.get())) 1201a5f54a06b09acf5c4ed3571039f262128226145bNick Lewycky return TokError("pointer to this type is invalid"); 1202df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned AddrSpace; 1203df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseOptionalAddrSpace(AddrSpace) || 1204df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::star, "expected '*' in address space")) 1205df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1206df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1207debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson Result = HandleUpRefs(PointerType::get(Result.get(), AddrSpace)); 1208df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1209df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1210df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1211df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner /// Types '(' ArgTypeListI ')' OptFuncAttrs 1212df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::lparen: 1213df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseFunctionType(Result)) 1214df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1215df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1216df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1217df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1218df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1219df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1220df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseParameterList 1221df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= '(' ')' 1222df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= '(' Arg (',' Arg)* ')' 1223df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// Arg 1224df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= Type OptionalAttributes Value OptionalAttributes 1225df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList, 1226df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PerFunctionState &PFS) { 1227df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::lparen, "expected '(' in call")) 1228df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1229df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1230df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner while (Lex.getKind() != lltok::rparen) { 1231df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If this isn't the first argument, we need a comma. 1232df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ArgList.empty() && 1233df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' in argument list")) 1234df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1235df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1236df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Parse the argument. 1237df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy ArgLoc; 1238df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder ArgTy(Type::VoidTy); 1239df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned ArgAttrs1, ArgAttrs2; 1240df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *V; 1241df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseType(ArgTy, ArgLoc) || 1242df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalAttrs(ArgAttrs1, 0) || 1243df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseValue(ArgTy, V, PFS) || 1244df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: Should not allow attributes after the argument, remove this in 1245df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // LLVM 3.0. 1246ad9ad7c827b585c018288c89d611d8c97d8d2e7dChris Lattner ParseOptionalAttrs(ArgAttrs2, 3)) 1247df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1248df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ArgList.push_back(ParamInfo(ArgLoc, V, ArgAttrs1|ArgAttrs2)); 1249df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1250df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1251df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); // Lex the ')'. 1252df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1253df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1254df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1255df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1256df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1257dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner/// ParseArgumentList - Parse the argument list for a function type or function 1258dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner/// prototype. If 'inType' is true then we are parsing a FunctionType. 1259df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= '(' ArgTypeListI ')' 1260df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ArgTypeListI 1261df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= /*empty*/ 1262df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= '...' 1263df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= ArgTypeList ',' '...' 1264df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= ArgType (',' ArgType)* 1265dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner/// 1266df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseArgumentList(std::vector<ArgInfo> &ArgList, 1267dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner bool &isVarArg, bool inType) { 1268df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner isVarArg = false; 1269df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Lex.getKind() == lltok::lparen); 1270df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); // eat the (. 1271df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1272df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() == lltok::rparen) { 1273df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // empty 1274df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (Lex.getKind() == lltok::dotdotdot) { 1275df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner isVarArg = true; 1276df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1277df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 1278df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy TypeLoc = Lex.getLoc(); 1279df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder ArgTy(Type::VoidTy); 1280df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Attrs; 1281df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::string Name; 12823ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner 1283dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner // If we're parsing a type, use ParseTypeRec, because we allow recursive 1284dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner // types (such as a function returning a pointer to itself). If parsing a 1285dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner // function prototype, we require fully resolved types. 1286dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner if ((inType ? ParseTypeRec(ArgTy) : ParseType(ArgTy)) || 12873ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseOptionalAttrs(Attrs, 0)) return true; 12883ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner 1289a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner if (ArgTy == Type::VoidTy) 1290a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner return Error(TypeLoc, "argument can not have void type"); 1291a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner 1292df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() == lltok::LocalVar || 1293df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.getKind() == lltok::StringConstant) { // FIXME: REMOVE IN LLVM 3.0 1294df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Name = Lex.getStrVal(); 1295df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1296df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 12973ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner 1298a5f54a06b09acf5c4ed3571039f262128226145bNick Lewycky if (!FunctionType::isValidArgumentType(ArgTy)) 12993ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return Error(TypeLoc, "invalid type for function argument"); 1300df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1301df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ArgList.push_back(ArgInfo(TypeLoc, ArgTy, Attrs, Name)); 1302df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 13033ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner while (EatIfPresent(lltok::comma)) { 1304df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Handle ... at end of arg list. 13053ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (EatIfPresent(lltok::dotdotdot)) { 1306df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner isVarArg = true; 1307df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1308df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1309df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1310df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Otherwise must be an argument type. 1311df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner TypeLoc = Lex.getLoc(); 1312a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner if ((inType ? ParseTypeRec(ArgTy) : ParseType(ArgTy)) || 13133ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseOptionalAttrs(Attrs, 0)) return true; 13143ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner 1315a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner if (ArgTy == Type::VoidTy) 1316a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner return Error(TypeLoc, "argument can not have void type"); 1317a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner 1318df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() == lltok::LocalVar || 1319df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.getKind() == lltok::StringConstant) { // FIXME: REMOVE IN LLVM 3.0 1320df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Name = Lex.getStrVal(); 1321df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1322df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 1323df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Name = ""; 1324df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 13253ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner 13263ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (!ArgTy->isFirstClassType() && !isa<OpaqueType>(ArgTy)) 13273ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return Error(TypeLoc, "invalid type for function argument"); 1328df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1329df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ArgList.push_back(ArgInfo(TypeLoc, ArgTy, Attrs, Name)); 1330df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1331df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1332df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 13333ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return ParseToken(lltok::rparen, "expected ')' at end of argument list"); 1334df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1335df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1336df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseFunctionType 1337df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= Type ArgumentList OptionalAttrs 1338df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseFunctionType(PATypeHolder &Result) { 1339df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Lex.getKind() == lltok::lparen); 1340df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1341d77d04c323f856b07c6e46450e7578dd09c0165fChris Lattner if (!FunctionType::isValidReturnType(Result)) 1342d77d04c323f856b07c6e46450e7578dd09c0165fChris Lattner return TokError("invalid function return type"); 1343d77d04c323f856b07c6e46450e7578dd09c0165fChris Lattner 1344df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::vector<ArgInfo> ArgList; 1345df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner bool isVarArg; 1346df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Attrs; 1347dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner if (ParseArgumentList(ArgList, isVarArg, true) || 1348df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: Allow, but ignore attributes on function types! 1349df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: Remove in LLVM 3.0 1350df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalAttrs(Attrs, 2)) 1351df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1352df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1353df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Reject names on the arguments lists. 1354df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { 1355df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ArgList[i].Name.empty()) 1356df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ArgList[i].Loc, "argument name invalid in function type"); 1357df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ArgList[i].Attrs != 0) { 1358df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Allow but ignore attributes on function types; this permits 1359df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // auto-upgrade. 1360df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: REJECT ATTRIBUTES ON FUNCTION TYPES in LLVM 3.0 1361df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1362df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1363df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1364df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::vector<const Type*> ArgListTy; 1365df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = ArgList.size(); i != e; ++i) 1366df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ArgListTy.push_back(ArgList[i].Type); 1367df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1368debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson Result = HandleUpRefs(FunctionType::get(Result.get(), 1369fba933c823a2862067e0696bd5032ab18185bd77Owen Anderson ArgListTy, isVarArg)); 1370df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1371df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1372df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1373df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere. 1374df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// TypeRec 1375df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= '{' '}' 1376df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= '{' TypeRec (',' TypeRec)* '}' 1377df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= '<' '{' '}' '>' 1378df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= '<' '{' TypeRec (',' TypeRec)* '}' '>' 1379df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseStructType(PATypeHolder &Result, bool Packed) { 1380df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Lex.getKind() == lltok::lbrace); 1381df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); // Consume the '{' 1382df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 13833ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (EatIfPresent(lltok::rbrace)) { 1384debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson Result = StructType::get(Packed); 1385df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1386df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1387df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1388df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::vector<PATypeHolder> ParamsList; 1389a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner LocTy EltTyLoc = Lex.getLoc(); 1390df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeRec(Result)) return true; 1391df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParamsList.push_back(Result); 1392df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1393a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner if (Result == Type::VoidTy) 1394a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner return Error(EltTyLoc, "struct element can not have void type"); 1395a5f54a06b09acf5c4ed3571039f262128226145bNick Lewycky if (!StructType::isValidElementType(Result)) 1396a5f54a06b09acf5c4ed3571039f262128226145bNick Lewycky return Error(EltTyLoc, "invalid element type for struct"); 1397a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner 13983ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner while (EatIfPresent(lltok::comma)) { 1399a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner EltTyLoc = Lex.getLoc(); 1400df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeRec(Result)) return true; 1401a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner 1402a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner if (Result == Type::VoidTy) 1403a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner return Error(EltTyLoc, "struct element can not have void type"); 1404a5f54a06b09acf5c4ed3571039f262128226145bNick Lewycky if (!StructType::isValidElementType(Result)) 1405a5f54a06b09acf5c4ed3571039f262128226145bNick Lewycky return Error(EltTyLoc, "invalid element type for struct"); 1406a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner 1407df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParamsList.push_back(Result); 1408df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1409df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 14103ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseToken(lltok::rbrace, "expected '}' at end of struct")) 14113ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return true; 1412df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1413df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::vector<const Type*> ParamsListTy; 1414df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = ParamsList.size(); i != e; ++i) 1415df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParamsListTy.push_back(ParamsList[i].get()); 1416debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson Result = HandleUpRefs(StructType::get(ParamsListTy, Packed)); 1417df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1418df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1419df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1420df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseArrayVectorType - Parse an array or vector type, assuming the first 1421df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// token has already been consumed. 1422df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// TypeRec 1423df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= '[' APSINTVAL 'x' Types ']' 1424df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= '<' APSINTVAL 'x' Types '>' 1425df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseArrayVectorType(PATypeHolder &Result, bool isVector) { 1426df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() || 1427df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.getAPSIntVal().getBitWidth() > 64) 1428df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("expected number in address space"); 1429df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1430df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy SizeLoc = Lex.getLoc(); 1431df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner uint64_t Size = Lex.getAPSIntVal().getZExtValue(); 14323ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner Lex.Lex(); 14333ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner 14343ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseToken(lltok::kw_x, "expected 'x' after element count")) 14353ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return true; 1436df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1437df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy TypeLoc = Lex.getLoc(); 1438df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder EltTy(Type::VoidTy); 1439df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeRec(EltTy)) return true; 1440df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1441a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner if (EltTy == Type::VoidTy) 1442a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner return Error(TypeLoc, "array and vector element type cannot be void"); 1443a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner 14443ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseToken(isVector ? lltok::greater : lltok::rsquare, 14453ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner "expected end of sequential type")) 14463ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return true; 1447df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1448df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (isVector) { 1449452e2627dc1de4fb5a2b1c9ac999f1b3396f82c7Chris Lattner if (Size == 0) 1450452e2627dc1de4fb5a2b1c9ac999f1b3396f82c7Chris Lattner return Error(SizeLoc, "zero element vector is illegal"); 1451df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if ((unsigned)Size != Size) 1452df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(SizeLoc, "size too large for vector"); 1453a5f54a06b09acf5c4ed3571039f262128226145bNick Lewycky if (!VectorType::isValidElementType(EltTy)) 1454df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(TypeLoc, "vector element type must be fp or integer"); 1455debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson Result = VectorType::get(EltTy, unsigned(Size)); 1456df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 1457a5f54a06b09acf5c4ed3571039f262128226145bNick Lewycky if (!ArrayType::isValidElementType(EltTy)) 1458df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(TypeLoc, "invalid array element type"); 1459debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson Result = HandleUpRefs(ArrayType::get(EltTy, Size)); 1460df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1461df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1462df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1463df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1464df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 1465df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Function Semantic Analysis. 1466df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 1467df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1468df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerLLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f) 1469df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner : P(p), F(f) { 1470df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1471df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Insert unnamed arguments into the NumberedVals list. 1472df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); 1473df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner AI != E; ++AI) 1474df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!AI->hasName()) 1475df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner NumberedVals.push_back(AI); 1476df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1477df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1478df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerLLParser::PerFunctionState::~PerFunctionState() { 1479df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If there were any forward referenced non-basicblock values, delete them. 1480df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (std::map<std::string, std::pair<Value*, LocTy> >::iterator 1481df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I) 1482df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<BasicBlock>(I->second.first)) { 1483b43eae745eb9a7c46fe76b0ed8053b782ca67536Owen Anderson I->second.first->replaceAllUsesWith( 14849e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson UndefValue::get(I->second.first->getType())); 1485df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner delete I->second.first; 1486df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I->second.first = 0; 1487df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1488df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1489df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator 1490df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I) 1491df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<BasicBlock>(I->second.first)) { 1492b43eae745eb9a7c46fe76b0ed8053b782ca67536Owen Anderson I->second.first->replaceAllUsesWith( 14939e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson UndefValue::get(I->second.first->getType())); 1494df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner delete I->second.first; 1495df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I->second.first = 0; 1496df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1497df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1498df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1499df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::PerFunctionState::VerifyFunctionComplete() { 1500df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ForwardRefVals.empty()) 1501df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return P.Error(ForwardRefVals.begin()->second.second, 1502df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "use of undefined value '%" + ForwardRefVals.begin()->first + 1503df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "'"); 1504df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ForwardRefValIDs.empty()) 1505df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return P.Error(ForwardRefValIDs.begin()->second.second, 1506df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "use of undefined value '%" + 1507df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner utostr(ForwardRefValIDs.begin()->first) + "'"); 1508df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1509df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1510df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1511df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1512df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// GetVal - Get a value with the specified name or ID, creating a 1513df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// forward reference record if needed. This can return null if the value 1514df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// exists but does not have the right type. 1515df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerValue *LLParser::PerFunctionState::GetVal(const std::string &Name, 1516df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const Type *Ty, LocTy Loc) { 1517df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Look this name up in the normal function symbol table. 1518df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Val = F.getValueSymbolTable().lookup(Name); 1519df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1520df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If this is a forward reference for the value, see if we already created a 1521df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // forward ref record. 1522df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val == 0) { 1523df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<std::string, std::pair<Value*, LocTy> >::iterator 1524df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I = ForwardRefVals.find(Name); 1525df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I != ForwardRefVals.end()) 1526df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Val = I->second.first; 1527df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1528df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1529df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If we have the value in the symbol table or fwd-ref table, return it. 1530df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val) { 1531df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val->getType() == Ty) return Val; 1532df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Ty == Type::LabelTy) 1533df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner P.Error(Loc, "'%" + Name + "' is not a basic block"); 1534df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else 1535df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner P.Error(Loc, "'%" + Name + "' defined with type '" + 1536df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Val->getType()->getDescription() + "'"); 1537df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return 0; 1538df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1539df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1540df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Don't make placeholders with invalid type. 1541df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty) && Ty != Type::LabelTy) { 1542df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner P.Error(Loc, "invalid use of a non-first-class type"); 1543df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return 0; 1544df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1545df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1546df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Otherwise, create a new forward reference for this value and remember it. 1547df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *FwdVal; 1548df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Ty == Type::LabelTy) 1549df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FwdVal = BasicBlock::Create(Name, &F); 1550df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else 1551df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FwdVal = new Argument(Ty, Name); 1552df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1553df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefVals[Name] = std::make_pair(FwdVal, Loc); 1554df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return FwdVal; 1555df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1556df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1557df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerValue *LLParser::PerFunctionState::GetVal(unsigned ID, const Type *Ty, 1558df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc) { 1559df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Look this name up in the normal function symbol table. 1560df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0; 1561df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1562df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If this is a forward reference for the value, see if we already created a 1563df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // forward ref record. 1564df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val == 0) { 1565df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<unsigned, std::pair<Value*, LocTy> >::iterator 1566df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I = ForwardRefValIDs.find(ID); 1567df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I != ForwardRefValIDs.end()) 1568df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Val = I->second.first; 1569df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1570df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1571df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If we have the value in the symbol table or fwd-ref table, return it. 1572df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val) { 1573df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val->getType() == Ty) return Val; 1574df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Ty == Type::LabelTy) 1575df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner P.Error(Loc, "'%" + utostr(ID) + "' is not a basic block"); 1576df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else 1577df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner P.Error(Loc, "'%" + utostr(ID) + "' defined with type '" + 1578df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Val->getType()->getDescription() + "'"); 1579df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return 0; 1580df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1581df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1582df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty) && Ty != Type::LabelTy) { 1583df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner P.Error(Loc, "invalid use of a non-first-class type"); 1584df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return 0; 1585df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1586df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1587df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Otherwise, create a new forward reference for this value and remember it. 1588df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *FwdVal; 1589df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Ty == Type::LabelTy) 1590df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FwdVal = BasicBlock::Create("", &F); 1591df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else 1592df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FwdVal = new Argument(Ty); 1593df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1594df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc); 1595df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return FwdVal; 1596df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1597df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1598df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// SetInstName - After an instruction is parsed and inserted into its 1599df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// basic block, this installs its name. 1600df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::PerFunctionState::SetInstName(int NameID, 1601df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const std::string &NameStr, 1602df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy NameLoc, Instruction *Inst) { 1603df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If this instruction has void type, it cannot have a name or ID specified. 1604df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Inst->getType() == Type::VoidTy) { 1605df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (NameID != -1 || !NameStr.empty()) 1606df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return P.Error(NameLoc, "instructions returning void cannot have a name"); 1607df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1608df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1609df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1610df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If this was a numbered instruction, verify that the instruction is the 1611df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // expected value and resolve any forward references. 1612df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (NameStr.empty()) { 1613df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If neither a name nor an ID was specified, just use the next ID. 1614df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (NameID == -1) 1615df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner NameID = NumberedVals.size(); 1616df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1617df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (unsigned(NameID) != NumberedVals.size()) 1618df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return P.Error(NameLoc, "instruction expected to be numbered '%" + 1619df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner utostr(NumberedVals.size()) + "'"); 1620df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1621df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI = 1622df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefValIDs.find(NameID); 1623df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FI != ForwardRefValIDs.end()) { 1624df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FI->second.first->getType() != Inst->getType()) 1625df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return P.Error(NameLoc, "instruction forward referenced with type '" + 1626df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FI->second.first->getType()->getDescription() + "'"); 1627df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FI->second.first->replaceAllUsesWith(Inst); 1628df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefValIDs.erase(FI); 1629df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1630df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1631df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner NumberedVals.push_back(Inst); 1632df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1633df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1634df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1635df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Otherwise, the instruction had a name. Resolve forward refs and set it. 1636df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<std::string, std::pair<Value*, LocTy> >::iterator 1637df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FI = ForwardRefVals.find(NameStr); 1638df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FI != ForwardRefVals.end()) { 1639df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FI->second.first->getType() != Inst->getType()) 1640df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return P.Error(NameLoc, "instruction forward referenced with type '" + 1641df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FI->second.first->getType()->getDescription() + "'"); 1642df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FI->second.first->replaceAllUsesWith(Inst); 1643df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefVals.erase(FI); 1644df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1645df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1646df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Set the name on the instruction. 1647df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst->setName(NameStr); 1648df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1649df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Inst->getNameStr() != NameStr) 1650df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return P.Error(NameLoc, "multiple definition of local value named '" + 1651df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner NameStr + "'"); 1652df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1653df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1654df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1655df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// GetBB - Get a basic block with the specified name or ID, creating a 1656df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// forward reference record if needed. 1657df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerBasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name, 1658df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc) { 1659df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return cast_or_null<BasicBlock>(GetVal(Name, Type::LabelTy, Loc)); 1660df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1661df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1662df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerBasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) { 1663df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return cast_or_null<BasicBlock>(GetVal(ID, Type::LabelTy, Loc)); 1664df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1665df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1666df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// DefineBB - Define the specified basic block, which is either named or 1667df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// unnamed. If there is an error, this returns null otherwise it returns 1668df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// the block being defined. 1669df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerBasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name, 1670df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc) { 1671df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner BasicBlock *BB; 1672df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Name.empty()) 1673df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner BB = GetBB(NumberedVals.size(), Loc); 1674df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else 1675df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner BB = GetBB(Name, Loc); 1676df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (BB == 0) return 0; // Already diagnosed error. 1677df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1678df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Move the block to the end of the function. Forward ref'd blocks are 1679df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // inserted wherever they happen to be referenced. 1680df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB); 1681df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1682df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Remove the block from forward ref sets. 1683df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Name.empty()) { 1684df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefValIDs.erase(NumberedVals.size()); 1685df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner NumberedVals.push_back(BB); 1686df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 1687df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // BB forward references are already in the function symbol table. 1688df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefVals.erase(Name); 1689df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1690df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1691df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return BB; 1692df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1693df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1694df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 1695df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Constants. 1696df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 1697df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1698df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseValID - Parse an abstract value that doesn't necessarily have a 1699df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// type implied. For example, if we parse "4" we don't know what integer type 1700df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// it has. The value will later be combined with its type and checked for 1701df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// sanity. 1702df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseValID(ValID &ID) { 1703df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Loc = Lex.getLoc(); 1704df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Lex.getKind()) { 1705df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: return TokError("expected value token"); 1706df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::GlobalID: // @42 1707df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.UIntVal = Lex.getUIntVal(); 1708df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_GlobalID; 1709df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1710df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::GlobalVar: // @foo 1711df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.StrVal = Lex.getStrVal(); 1712df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_GlobalName; 1713df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1714df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::LocalVarID: // %42 1715df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.UIntVal = Lex.getUIntVal(); 1716df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_LocalID; 1717df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1718df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::LocalVar: // %foo 1719df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::StringConstant: // "foo" - FIXME: REMOVE IN LLVM 3.0 1720df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.StrVal = Lex.getStrVal(); 1721df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_LocalName; 1722df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 172321cc4460efa104e8591b05a90f20130291614344Nick Lewycky case lltok::Metadata: { // !{...} MDNode, !"foo" MDString 1724104cf9e02b0ed94d4173869a598af6c6972a8660Devang Patel ID.Kind = ValID::t_Metadata; 172521cc4460efa104e8591b05a90f20130291614344Nick Lewycky Lex.Lex(); 172621cc4460efa104e8591b05a90f20130291614344Nick Lewycky if (Lex.getKind() == lltok::lbrace) { 1727cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky SmallVector<Value*, 16> Elts; 172821cc4460efa104e8591b05a90f20130291614344Nick Lewycky if (ParseMDNodeVector(Elts) || 172921cc4460efa104e8591b05a90f20130291614344Nick Lewycky ParseToken(lltok::rbrace, "expected end of metadata node")) 173021cc4460efa104e8591b05a90f20130291614344Nick Lewycky return true; 1731cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky 1732647e3016de18d2fc8b0f233a0b356809e3fdcc54Owen Anderson ID.MetadataVal = MDNode::get(Context, Elts.data(), Elts.size()); 173321cc4460efa104e8591b05a90f20130291614344Nick Lewycky return false; 173421cc4460efa104e8591b05a90f20130291614344Nick Lewycky } 173521cc4460efa104e8591b05a90f20130291614344Nick Lewycky 1736923078c65d5a37a4f135705300c9feea49487de5Devang Patel // Standalone metadata reference 1737923078c65d5a37a4f135705300c9feea49487de5Devang Patel // !{ ..., !42, ... } 1738104cf9e02b0ed94d4173869a598af6c6972a8660Devang Patel if (!ParseMDNode(ID.MetadataVal)) 1739923078c65d5a37a4f135705300c9feea49487de5Devang Patel return false; 1740256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel 174121cc4460efa104e8591b05a90f20130291614344Nick Lewycky // MDString: 174221cc4460efa104e8591b05a90f20130291614344Nick Lewycky // ::= '!' STRINGCONSTANT 1743e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel if (ParseMDString(ID.MetadataVal)) return true; 1744e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel ID.Kind = ValID::t_Metadata; 174521cc4460efa104e8591b05a90f20130291614344Nick Lewycky return false; 174621cc4460efa104e8591b05a90f20130291614344Nick Lewycky } 1747df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::APSInt: 1748df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.APSIntVal = Lex.getAPSIntVal(); 1749df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_APSInt; 1750df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1751df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::APFloat: 1752df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.APFloatVal = Lex.getAPFloatVal(); 1753df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_APFloat; 1754df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1755df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_true: 17565defacc6e605f4651c6300237cef8e9bb2eb6d0eOwen Anderson ID.ConstantVal = ConstantInt::getTrue(Context); 1757df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1758df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1759df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_false: 17605defacc6e605f4651c6300237cef8e9bb2eb6d0eOwen Anderson ID.ConstantVal = ConstantInt::getFalse(Context); 1761df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1762df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1763df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_null: ID.Kind = ValID::t_Null; break; 1764df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_undef: ID.Kind = ValID::t_Undef; break; 1765df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break; 1766df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1767df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::lbrace: { 1768df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // ValID ::= '{' ConstVector '}' 1769df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1770df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<Constant*, 16> Elts; 1771df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseGlobalValueVector(Elts) || 1772df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rbrace, "expected end of struct constant")) 1773df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1774df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 17758fa3338ed2400c1352b137613d2c2c70d1ead695Owen Anderson ID.ConstantVal = ConstantStruct::get(Elts.data(), Elts.size(), false); 1776df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1777df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1778df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1779df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::less: { 1780df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // ValID ::= '<' ConstVector '>' --> Vector. 1781df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct. 1782df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 17833ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner bool isPackedStruct = EatIfPresent(lltok::lbrace); 1784df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1785df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<Constant*, 16> Elts; 1786df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy FirstEltLoc = Lex.getLoc(); 1787df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseGlobalValueVector(Elts) || 1788df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner (isPackedStruct && 1789df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rbrace, "expected end of packed struct")) || 1790df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::greater, "expected end of constant")) 1791df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1792df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1793df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (isPackedStruct) { 1794fba933c823a2862067e0696bd5032ab18185bd77Owen Anderson ID.ConstantVal = 17958fa3338ed2400c1352b137613d2c2c70d1ead695Owen Anderson ConstantStruct::get(Elts.data(), Elts.size(), true); 1796df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1797df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1798df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1799df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1800df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Elts.empty()) 1801df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "constant vector must not be empty"); 1802df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1803df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!Elts[0]->getType()->isInteger() && 1804df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner !Elts[0]->getType()->isFloatingPoint()) 1805df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(FirstEltLoc, 1806df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "vector elements must have integer or floating point type"); 1807df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1808df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Verify that all the vector elements have the same type. 1809df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 1, e = Elts.size(); i != e; ++i) 1810df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Elts[i]->getType() != Elts[0]->getType()) 1811df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(FirstEltLoc, 1812df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "vector element #" + utostr(i) + 1813df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner " is not of type '" + Elts[0]->getType()->getDescription()); 1814df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1815af7ec975870f92245f1f1484ac80a1e2db6a0afaOwen Anderson ID.ConstantVal = ConstantVector::get(Elts.data(), Elts.size()); 1816df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1817df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1818df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1819df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::lsquare: { // Array Constant 1820df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1821df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<Constant*, 16> Elts; 1822df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy FirstEltLoc = Lex.getLoc(); 1823df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseGlobalValueVector(Elts) || 1824df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rsquare, "expected end of array constant")) 1825df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1826df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1827df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Handle empty element. 1828df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Elts.empty()) { 1829df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Use undef instead of an array because it's inconvenient to determine 1830df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // the element type at this point, there being no elements to examine. 1831081b505f77b183b7b029818218245cf4986e8948Chris Lattner ID.Kind = ValID::t_EmptyArray; 1832df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1833df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1834df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1835df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!Elts[0]->getType()->isFirstClassType()) 1836df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(FirstEltLoc, "invalid array element type: " + 1837df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Elts[0]->getType()->getDescription()); 1838df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1839debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size()); 1840df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1841df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Verify all elements are correct type! 18426d6b3cc89b9d1911cff4931dadfe670f94fcbb3bChris Lattner for (unsigned i = 0, e = Elts.size(); i != e; ++i) { 1843df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Elts[i]->getType() != Elts[0]->getType()) 1844df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(FirstEltLoc, 1845df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "array element #" + utostr(i) + 1846df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner " is not of type '" +Elts[0]->getType()->getDescription()); 1847df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 184821cc4460efa104e8591b05a90f20130291614344Nick Lewycky 18491fd7096407d5e598ed3366a1141548e71273f1c5Owen Anderson ID.ConstantVal = ConstantArray::get(ATy, Elts.data(), Elts.size()); 1850df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1851df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1852df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1853df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_c: // c "foo" 1854df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 18551fd7096407d5e598ed3366a1141548e71273f1c5Owen Anderson ID.ConstantVal = ConstantArray::get(Lex.getStrVal(), false); 1856df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::StringConstant, "expected string")) return true; 1857df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1858df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1859df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1860df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_asm: { 1861df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // ValID ::= 'asm' SideEffect? STRINGCONSTANT ',' STRINGCONSTANT 1862df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner bool HasSideEffect; 1863df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1864df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) || 18653ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseStringConstant(ID.StrVal) || 18663ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseToken(lltok::comma, "expected comma in inline asm expression") || 1867df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::StringConstant, "expected constraint string")) 1868df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1869df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.StrVal2 = Lex.getStrVal(); 1870df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.UIntVal = HasSideEffect; 1871df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_InlineAsm; 1872df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1873df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1874df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1875df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_trunc: 1876df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_zext: 1877df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sext: 1878df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_fptrunc: 1879df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_fpext: 1880df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_bitcast: 1881df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_uitofp: 1882df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sitofp: 1883df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_fptoui: 1884df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_fptosi: 1885df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_inttoptr: 1886df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ptrtoint: { 1887df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Opc = Lex.getUIntVal(); 1888df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder DestTy(Type::VoidTy); 1889df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *SrcVal; 1890df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1891df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") || 1892df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalTypeAndValue(SrcVal) || 189324b108bc8ca75922d4cb8e7f197934b8269d4fc3Dan Gohman ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") || 1894df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseType(DestTy) || 1895df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast")) 1896df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1897df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy)) 1898df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "invalid cast opcode for cast from '" + 1899df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SrcVal->getType()->getDescription() + "' to '" + 1900df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner DestTy->getDescription() + "'"); 1901baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc, 1902fba933c823a2862067e0696bd5032ab18185bd77Owen Anderson SrcVal, DestTy); 1903df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1904df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1905df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1906df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_extractvalue: { 1907df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1908df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *Val; 1909df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<unsigned, 4> Indices; 1910df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")|| 1911df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalTypeAndValue(Val) || 1912df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseIndexList(Indices) || 1913df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr")) 1914df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1915df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<StructType>(Val->getType()) && !isa<ArrayType>(Val->getType())) 1916df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "extractvalue operand must be array or struct"); 1917df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(), 1918df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Indices.end())) 1919df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "invalid indices for extractvalue"); 1920e3e51c0038bd6ba2add82e2246e97edec0ab2204Jay Foad ID.ConstantVal = 1921baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson ConstantExpr::getExtractValue(Val, Indices.data(), Indices.size()); 1922df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1923df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1924df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1925df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_insertvalue: { 1926df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1927df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *Val0, *Val1; 1928df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<unsigned, 4> Indices; 1929df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")|| 1930df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalTypeAndValue(Val0) || 1931df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")|| 1932df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalTypeAndValue(Val1) || 1933df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseIndexList(Indices) || 1934df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr")) 1935df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1936df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<StructType>(Val0->getType()) && !isa<ArrayType>(Val0->getType())) 1937df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "extractvalue operand must be array or struct"); 1938df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(), 1939df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Indices.end())) 1940df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "invalid indices for insertvalue"); 1941baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, 1942fba933c823a2862067e0696bd5032ab18185bd77Owen Anderson Indices.data(), Indices.size()); 1943df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1944df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1945df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1946df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_icmp: 19477f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky case lltok::kw_fcmp: { 1948df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned PredVal, Opc = Lex.getUIntVal(); 1949df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *Val0, *Val1; 1950df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1951df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseCmpPredicate(PredVal, Opc) || 1952df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::lparen, "expected '(' in compare constantexpr") || 1953df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalTypeAndValue(Val0) || 1954df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected comma in compare constantexpr") || 1955df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalTypeAndValue(Val1) || 1956df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rparen, "expected ')' in compare constantexpr")) 1957df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1958df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1959df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val0->getType() != Val1->getType()) 1960df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "compare operands must have the same type"); 1961df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1962df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal; 1963df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1964df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Opc == Instruction::FCmp) { 1965df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!Val0->getType()->isFPOrFPVector()) 1966df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "fcmp requires floating point operands"); 1967baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1); 19687f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky } else { 19697f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!"); 1970df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!Val0->getType()->isIntOrIntVector() && 1971df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner !isa<PointerType>(Val0->getType())) 1972df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "icmp requires pointer or integer operands"); 1973baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1); 1974df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1975df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1976df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1977df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1978df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1979df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Binary Operators. 1980df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_add: 1981ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman case lltok::kw_fadd: 1982df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sub: 1983ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman case lltok::kw_fsub: 1984df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_mul: 1985ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman case lltok::kw_fmul: 1986df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_udiv: 1987df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sdiv: 1988df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_fdiv: 1989df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_urem: 1990df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_srem: 1991df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_frem: { 199259858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman bool NUW = false; 199359858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman bool NSW = false; 199459858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman bool Exact = false; 1995df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Opc = Lex.getUIntVal(); 1996df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *Val0, *Val1; 1997df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 199859858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman LocTy ModifierLoc = Lex.getLoc(); 199959858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (Opc == Instruction::Add || 200059858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman Opc == Instruction::Sub || 200159858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman Opc == Instruction::Mul) { 200259858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (EatIfPresent(lltok::kw_nuw)) 200359858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman NUW = true; 200459858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (EatIfPresent(lltok::kw_nsw)) { 200559858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman NSW = true; 200659858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (EatIfPresent(lltok::kw_nuw)) 200759858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman NUW = true; 200859858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman } 200959858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman } else if (Opc == Instruction::SDiv) { 201059858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (EatIfPresent(lltok::kw_exact)) 201159858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman Exact = true; 201259858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman } 2013df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") || 2014df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalTypeAndValue(Val0) || 2015df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected comma in binary constantexpr") || 2016df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalTypeAndValue(Val1) || 2017df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rparen, "expected ')' in binary constantexpr")) 2018df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2019df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val0->getType() != Val1->getType()) 2020df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "operands of constexpr must have same type"); 202159858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (!Val0->getType()->isIntOrIntVector()) { 202259858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (NUW) 202359858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman return Error(ModifierLoc, "nuw only applies to integer operations"); 202459858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (NSW) 202559858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman return Error(ModifierLoc, "nsw only applies to integer operations"); 202659858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman } 202759858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman // API compatibility: Accept either integer or floating-point types with 202859858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman // add, sub, and mul. 2029df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!Val0->getType()->isIntOrIntVector() && 2030df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner !Val0->getType()->isFPOrFPVector()) 2031df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc,"constexpr requires integer, fp, or vector operands"); 2032baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson Constant *C = ConstantExpr::get(Opc, Val0, Val1); 203359858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (NUW) 203459858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman cast<OverflowingBinaryOperator>(C)->setHasNoUnsignedOverflow(true); 203559858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (NSW) 203659858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman cast<OverflowingBinaryOperator>(C)->setHasNoSignedOverflow(true); 203759858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (Exact) 203859858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman cast<SDivOperator>(C)->setIsExact(true); 203959858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman ID.ConstantVal = C; 2040df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 2041df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2042df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2043df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2044df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Logical Operations 2045df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_shl: 2046df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_lshr: 2047df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ashr: 2048df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_and: 2049df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_or: 2050df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_xor: { 2051df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Opc = Lex.getUIntVal(); 2052df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *Val0, *Val1; 2053df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 2054df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") || 2055df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalTypeAndValue(Val0) || 2056df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected comma in logical constantexpr") || 2057df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalTypeAndValue(Val1) || 2058df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rparen, "expected ')' in logical constantexpr")) 2059df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2060df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val0->getType() != Val1->getType()) 2061df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "operands of constexpr must have same type"); 2062df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!Val0->getType()->isIntOrIntVector()) 2063df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, 2064df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "constexpr requires integer or integer vector operands"); 2065baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1); 2066df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 2067df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2068df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2069df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2070df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_getelementptr: 2071df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_shufflevector: 2072df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_insertelement: 2073df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_extractelement: 2074df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_select: { 2075df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Opc = Lex.getUIntVal(); 2076df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<Constant*, 16> Elts; 2077dd8004dc73d091ccb3927dbbc3b41639a3738ae3Dan Gohman bool InBounds = false; 2078df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 2079dd8004dc73d091ccb3927dbbc3b41639a3738ae3Dan Gohman if (Opc == Instruction::GetElementPtr) 2080dcb40a3c1c747c88c4e634e34d1b4d7533a86adaDan Gohman InBounds = EatIfPresent(lltok::kw_inbounds); 2081df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::lparen, "expected '(' in constantexpr") || 2082df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalValueVector(Elts) || 2083df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rparen, "expected ')' in constantexpr")) 2084df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2085df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2086df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Opc == Instruction::GetElementPtr) { 2087df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Elts.size() == 0 || !isa<PointerType>(Elts[0]->getType())) 2088df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "getelementptr requires pointer operand"); 2089df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2090df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), 20914e9bac3769cb668aad20f1ac2d6a35d9ba452f3aEli Friedman (Value**)(Elts.data() + 1), 20924e9bac3769cb668aad20f1ac2d6a35d9ba452f3aEli Friedman Elts.size() - 1)) 2093df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "invalid indices for getelementptr"); 2094baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], 20954e9bac3769cb668aad20f1ac2d6a35d9ba452f3aEli Friedman Elts.data() + 1, Elts.size() - 1); 2096dd8004dc73d091ccb3927dbbc3b41639a3738ae3Dan Gohman if (InBounds) 2097dd8004dc73d091ccb3927dbbc3b41639a3738ae3Dan Gohman cast<GEPOperator>(ID.ConstantVal)->setIsInBounds(true); 2098df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (Opc == Instruction::Select) { 2099df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Elts.size() != 3) 2100df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "expected three operands to select"); 2101df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1], 2102df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Elts[2])) 2103df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, Reason); 2104baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]); 2105df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (Opc == Instruction::ShuffleVector) { 2106df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Elts.size() != 3) 2107df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "expected three operands to shufflevector"); 2108df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2])) 2109df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "invalid operands to shufflevector"); 2110fba933c823a2862067e0696bd5032ab18185bd77Owen Anderson ID.ConstantVal = 2111baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]); 2112df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (Opc == Instruction::ExtractElement) { 2113df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Elts.size() != 2) 2114df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "expected two operands to extractelement"); 2115df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1])) 2116df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "invalid extractelement operands"); 2117baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]); 2118df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 2119df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Opc == Instruction::InsertElement && "Unknown opcode"); 2120df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Elts.size() != 3) 2121df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "expected three operands to insertelement"); 2122df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2])) 2123df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "invalid insertelement operands"); 2124fba933c823a2862067e0696bd5032ab18185bd77Owen Anderson ID.ConstantVal = 2125baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]); 2126df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2127df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2128df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 2129df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2130df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2131df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2132df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2133df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 2134df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2135df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2136df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2137df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseGlobalValue - Parse a global value with the specified type. 2138df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseGlobalValue(const Type *Ty, Constant *&V) { 2139df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = 0; 2140df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ValID ID; 21413ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return ParseValID(ID) || 21423ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ConvertGlobalValIDToValue(Ty, ID, V); 2143df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2144df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2145df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ConvertGlobalValIDToValue - Apply a type to a ValID to get a fully resolved 2146df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// constant. 2147df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, 2148df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *&V) { 2149df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (isa<FunctionType>(Ty)) 2150df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "functions are not values, refer to them as pointers"); 2151df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2152df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (ID.Kind) { 2153e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel default: llvm_unreachable("Unknown ValID!"); 2154e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel case ValID::t_Metadata: 2155e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel return Error(ID.Loc, "invalid use of metadata"); 2156df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case ValID::t_LocalID: 2157df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case ValID::t_LocalName: 2158df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "invalid use of function-local name"); 2159df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case ValID::t_InlineAsm: 2160df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "inline asm can only be an operand of call/invoke"); 2161df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case ValID::t_GlobalName: 2162df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = GetGlobalVal(ID.StrVal, Ty, ID.Loc); 2163df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return V == 0; 2164df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case ValID::t_GlobalID: 2165df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc); 2166df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return V == 0; 2167df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case ValID::t_APSInt: 2168df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<IntegerType>(Ty)) 2169df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "integer constant must have integer type"); 2170df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits()); 2171eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson V = ConstantInt::get(Context, ID.APSIntVal); 2172df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2173df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case ValID::t_APFloat: 2174df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!Ty->isFloatingPoint() || 2175df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner !ConstantFP::isValueValidForType(Ty, ID.APFloatVal)) 2176df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "floating point constant invalid for type"); 2177df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2178df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // The lexer has no type info, so builds all float and double FP constants 2179df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // as double. Fix this here. Long double does not need this. 2180df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble && 2181df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Ty == Type::FloatTy) { 2182df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner bool Ignored; 2183df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, 2184df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner &Ignored); 2185df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 21866f83c9c6ef0e7f79825a0a8f22941815e4b684c7Owen Anderson V = ConstantFP::get(Context, ID.APFloatVal); 2187959873d2da763c0e3b3c67c39aab179fc43c5a52Chris Lattner 2188959873d2da763c0e3b3c67c39aab179fc43c5a52Chris Lattner if (V->getType() != Ty) 2189959873d2da763c0e3b3c67c39aab179fc43c5a52Chris Lattner return Error(ID.Loc, "floating point constant does not have type '" + 2190959873d2da763c0e3b3c67c39aab179fc43c5a52Chris Lattner Ty->getDescription() + "'"); 2191959873d2da763c0e3b3c67c39aab179fc43c5a52Chris Lattner 2192df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2193df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case ValID::t_Null: 2194df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<PointerType>(Ty)) 2195df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "null must be a pointer type"); 21969e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson V = ConstantPointerNull::get(cast<PointerType>(Ty)); 2197df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2198df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case ValID::t_Undef: 2199e67c1aa3986cf98f2021a31a6ad502857c757b7bChris Lattner // FIXME: LabelTy should not be a first-class type. 22000b6163506359223fb0f74d856099ee4d5e768ebeChris Lattner if ((!Ty->isFirstClassType() || Ty == Type::LabelTy) && 22010b6163506359223fb0f74d856099ee4d5e768ebeChris Lattner !isa<OpaqueType>(Ty)) 2202e67c1aa3986cf98f2021a31a6ad502857c757b7bChris Lattner return Error(ID.Loc, "invalid type for undef constant"); 22039e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson V = UndefValue::get(Ty); 2204df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2205081b505f77b183b7b029818218245cf4986e8948Chris Lattner case ValID::t_EmptyArray: 2206081b505f77b183b7b029818218245cf4986e8948Chris Lattner if (!isa<ArrayType>(Ty) || cast<ArrayType>(Ty)->getNumElements() != 0) 2207081b505f77b183b7b029818218245cf4986e8948Chris Lattner return Error(ID.Loc, "invalid empty array initializer"); 22089e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson V = UndefValue::get(Ty); 2209081b505f77b183b7b029818218245cf4986e8948Chris Lattner return false; 2210df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case ValID::t_Zero: 2211e67c1aa3986cf98f2021a31a6ad502857c757b7bChris Lattner // FIXME: LabelTy should not be a first-class type. 2212e67c1aa3986cf98f2021a31a6ad502857c757b7bChris Lattner if (!Ty->isFirstClassType() || Ty == Type::LabelTy) 2213df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "invalid type for null constant"); 2214a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson V = Constant::getNullValue(Ty); 2215df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2216df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case ValID::t_Constant: 2217df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ID.ConstantVal->getType() != Ty) 2218df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "constant expression type mismatch"); 2219df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = ID.ConstantVal; 2220df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2221df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2222df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2223df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2224df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseGlobalTypeAndValue(Constant *&V) { 2225df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder Type(Type::VoidTy); 2226df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return ParseType(Type) || 2227df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalValue(Type, V); 2228df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2229df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 22303ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ParseGlobalValueVector 22313ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ::= /*empty*/ 22323ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ::= TypeAndValue (',' TypeAndValue)* 2233df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts) { 2234df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Empty list. 2235df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() == lltok::rbrace || 2236df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.getKind() == lltok::rsquare || 2237df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.getKind() == lltok::greater || 2238df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.getKind() == lltok::rparen) 2239df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2240df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2241df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *C; 2242df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseGlobalTypeAndValue(C)) return true; 2243df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Elts.push_back(C); 2244df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 22453ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner while (EatIfPresent(lltok::comma)) { 2246df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseGlobalTypeAndValue(C)) return true; 2247df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Elts.push_back(C); 2248df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2249df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2250df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2251df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2252df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2253df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2254df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 2255df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Function Parsing. 2256df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 2257df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2258df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V, 2259df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PerFunctionState &PFS) { 2260df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ID.Kind == ValID::t_LocalID) 2261df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = PFS.GetVal(ID.UIntVal, Ty, ID.Loc); 2262df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else if (ID.Kind == ValID::t_LocalName) 2263df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = PFS.GetVal(ID.StrVal, Ty, ID.Loc); 2264b0adcdb7e53dcd636a7eafba44d941ac23006473Steve Naroff else if (ID.Kind == ValID::t_InlineAsm) { 2265df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const PointerType *PTy = dyn_cast<PointerType>(Ty); 2266df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const FunctionType *FTy = 2267df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0; 2268df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2)) 2269df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "invalid type for inline asm constraint string"); 2270df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal); 2271df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2272e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel } else if (ID.Kind == ValID::t_Metadata) { 2273e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel V = ID.MetadataVal; 2274df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 2275df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *C; 2276df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ConvertGlobalValIDToValue(Ty, ID, C)) return true; 2277df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = C; 2278df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2279df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2280df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2281df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return V == 0; 2282df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2283df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2284df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS) { 2285df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = 0; 2286df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ValID ID; 22873ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return ParseValID(ID) || 22883ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ConvertValIDToValue(Ty, ID, V, PFS); 2289df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2290df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2291df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState &PFS) { 2292df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder T(Type::VoidTy); 22933ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return ParseType(T) || 22943ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseValue(T, V, PFS); 2295df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2296df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2297df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// FunctionHeader 2298df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs 2299df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection 2300df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// OptionalAlign OptGC 2301df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) { 2302df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Parse the linkage. 2303df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy LinkageLoc = Lex.getLoc(); 2304df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Linkage; 2305df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2306df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Visibility, CC, RetAttrs; 2307df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder RetType(Type::VoidTy); 2308df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy RetTypeLoc = Lex.getLoc(); 2309df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseOptionalLinkage(Linkage) || 2310df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalVisibility(Visibility) || 2311df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalCallingConv(CC) || 2312df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalAttrs(RetAttrs, 1) || 2313a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner ParseType(RetType, RetTypeLoc, true /*void allowed*/)) 2314df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2315df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2316df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Verify that the linkage is ok. 2317df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch ((GlobalValue::LinkageTypes)Linkage) { 2318df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case GlobalValue::ExternalLinkage: 2319df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; // always ok. 2320df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case GlobalValue::DLLImportLinkage: 23215f4ee1fc5d00ae55c30fa2ce450c69be4c6d6e63Duncan Sands case GlobalValue::ExternalWeakLinkage: 2322df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (isDefine) 2323df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(LinkageLoc, "invalid linkage for function definition"); 2324df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 2325bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola case GlobalValue::PrivateLinkage: 23263d10a5a75794356a0a568ce283713adc3a963200Bill Wendling case GlobalValue::LinkerPrivateLinkage: 2327df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case GlobalValue::InternalLinkage: 232855f64dbb398cc8d967d1e32a8bb2190a60f5b61eNick Lewycky case GlobalValue::AvailableExternallyLinkage: 2329667d4b8de6dea70195ff12ef39a4deebffa2f5c7Duncan Sands case GlobalValue::LinkOnceAnyLinkage: 2330667d4b8de6dea70195ff12ef39a4deebffa2f5c7Duncan Sands case GlobalValue::LinkOnceODRLinkage: 2331667d4b8de6dea70195ff12ef39a4deebffa2f5c7Duncan Sands case GlobalValue::WeakAnyLinkage: 2332667d4b8de6dea70195ff12ef39a4deebffa2f5c7Duncan Sands case GlobalValue::WeakODRLinkage: 2333df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case GlobalValue::DLLExportLinkage: 2334df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isDefine) 2335df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(LinkageLoc, "invalid linkage for function declaration"); 2336df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 2337df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case GlobalValue::AppendingLinkage: 2338df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case GlobalValue::GhostLinkage: 23394dc2b39bf89d7c87868008ef8a0f807e0419aca6Duncan Sands case GlobalValue::CommonLinkage: 2340df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(LinkageLoc, "invalid function linkage type"); 2341df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2342df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 234399bb315f32a5f82f9e4978345ee39177db973242Chris Lattner if (!FunctionType::isValidReturnType(RetType) || 234499bb315f32a5f82f9e4978345ee39177db973242Chris Lattner isa<OpaqueType>(RetType)) 2345df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(RetTypeLoc, "invalid function return type"); 2346df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2347f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner LocTy NameLoc = Lex.getLoc(); 2348f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner 2349f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner std::string FunctionName; 2350f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner if (Lex.getKind() == lltok::GlobalVar) { 2351f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner FunctionName = Lex.getStrVal(); 2352f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok. 2353f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner unsigned NameID = Lex.getUIntVal(); 2354f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner 2355f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner if (NameID != NumberedVals.size()) 2356f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner return TokError("function expected to be numbered '%" + 2357f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner utostr(NumberedVals.size()) + "'"); 2358f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner } else { 2359df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("expected function name"); 2360f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner } 2361df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 23623ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner Lex.Lex(); 2363df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 23643ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (Lex.getKind() != lltok::lparen) 2365df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("expected '(' in function argument list"); 2366df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2367df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::vector<ArgInfo> ArgList; 2368df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner bool isVarArg; 2369df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned FuncAttrs; 2370df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::string Section; 2371df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Alignment; 23723ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner std::string GC; 23733ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner 2374dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner if (ParseArgumentList(ArgList, isVarArg, false) || 23753ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseOptionalAttrs(FuncAttrs, 2) || 23763ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner (EatIfPresent(lltok::kw_section) && 23773ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseStringConstant(Section)) || 23783ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseOptionalAlignment(Alignment) || 23793ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner (EatIfPresent(lltok::kw_gc) && 23803ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseStringConstant(GC))) 23813ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return true; 2382df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2383df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If the alignment was parsed as an attribute, move to the alignment field. 2384df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FuncAttrs & Attribute::Alignment) { 2385df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Alignment = Attribute::getAlignmentFromAttrs(FuncAttrs); 2386df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FuncAttrs &= ~Attribute::Alignment; 2387df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2388df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2389df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Okay, if we got here, the function is syntactically valid. Convert types 2390df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // and do semantic checks. 2391df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::vector<const Type*> ParamTypeList; 2392df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<AttributeWithIndex, 8> Attrs; 2393df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME : In 3.0, stop accepting zext, sext and inreg as optional function 2394df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // attributes. 2395df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg; 2396df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FuncAttrs & ObsoleteFuncAttrs) { 2397df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner RetAttrs |= FuncAttrs & ObsoleteFuncAttrs; 2398df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FuncAttrs &= ~ObsoleteFuncAttrs; 2399df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2400df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2401df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (RetAttrs != Attribute::None) 2402df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs.push_back(AttributeWithIndex::get(0, RetAttrs)); 2403df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2404df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { 2405df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParamTypeList.push_back(ArgList[i].Type); 2406df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ArgList[i].Attrs != Attribute::None) 2407df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs)); 2408df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2409df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2410df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FuncAttrs != Attribute::None) 2411df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs.push_back(AttributeWithIndex::get(~0, FuncAttrs)); 2412df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2413df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end()); 2414df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2415a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner if (PAL.paramHasAttr(1, Attribute::StructRet) && 2416a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner RetType != Type::VoidTy) 2417a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner return Error(RetTypeLoc, "functions with 'sret' argument must return void"); 2418a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner 2419fba933c823a2862067e0696bd5032ab18185bd77Owen Anderson const FunctionType *FT = 2420debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson FunctionType::get(RetType, ParamTypeList, isVarArg); 2421debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson const PointerType *PFT = PointerType::getUnqual(FT); 2422df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2423df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Fn = 0; 2424df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!FunctionName.empty()) { 2425df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If this was a definition of a forward reference, remove the definition 2426df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // from the forward reference table and fill in the forward ref. 2427df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI = 2428df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefVals.find(FunctionName); 2429df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FRVI != ForwardRefVals.end()) { 2430df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Fn = M->getFunction(FunctionName); 2431df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefVals.erase(FRVI); 2432df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if ((Fn = M->getFunction(FunctionName))) { 2433df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If this function already exists in the symbol table, then it is 2434df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // multiply defined. We accept a few cases for old backwards compat. 2435df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: Remove this stuff for LLVM 3.0. 2436df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Fn->getType() != PFT || Fn->getAttributes() != PAL || 2437df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner (!Fn->isDeclaration() && isDefine)) { 2438df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If the redefinition has different type or different attributes, 2439df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // reject it. If both have bodies, reject it. 2440df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(NameLoc, "invalid redefinition of function '" + 2441df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FunctionName + "'"); 2442df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (Fn->isDeclaration()) { 2443df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Make sure to strip off any argument names so we can't get conflicts. 2444df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end(); 2445df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner AI != AE; ++AI) 2446df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner AI->setName(""); 2447df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2448df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2449df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2450df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (FunctionName.empty()) { 2451df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If this is a definition of a forward referenced function, make sure the 2452df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // types agree. 2453df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I 2454df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner = ForwardRefValIDs.find(NumberedVals.size()); 2455df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I != ForwardRefValIDs.end()) { 2456df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Fn = cast<Function>(I->second.first); 2457df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Fn->getType() != PFT) 2458df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(NameLoc, "type of definition and forward reference of '@" + 2459df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner utostr(NumberedVals.size()) +"' disagree"); 2460df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefValIDs.erase(I); 2461df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2462df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2463df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2464df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Fn == 0) 2465df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M); 2466df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else // Move the forward-reference to the correct spot in the module. 2467df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn); 2468df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2469df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FunctionName.empty()) 2470df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner NumberedVals.push_back(Fn); 2471df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2472df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Fn->setLinkage((GlobalValue::LinkageTypes)Linkage); 2473df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility); 2474df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Fn->setCallingConv(CC); 2475df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Fn->setAttributes(PAL); 2476df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Fn->setAlignment(Alignment); 2477df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Fn->setSection(Section); 2478df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!GC.empty()) Fn->setGC(GC.c_str()); 2479df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2480df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Add all of the arguments we parsed to the function. 2481df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Function::arg_iterator ArgIt = Fn->arg_begin(); 2482df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) { 2483df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If the argument has a name, insert it into the argument symbol table. 2484df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ArgList[i].Name.empty()) continue; 2485df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2486df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Set the name, if it conflicted, it will be auto-renamed. 2487df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ArgIt->setName(ArgList[i].Name); 2488df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2489df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ArgIt->getNameStr() != ArgList[i].Name) 2490df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ArgList[i].Loc, "redefinition of argument '%" + 2491df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ArgList[i].Name + "'"); 2492df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2493df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2494df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2495df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2496df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2497df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2498df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseFunctionBody 2499df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= '{' BasicBlock+ '}' 2500df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'begin' BasicBlock+ 'end' // FIXME: remove in LLVM 3.0 2501df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// 2502df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseFunctionBody(Function &Fn) { 2503df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() != lltok::lbrace && Lex.getKind() != lltok::kw_begin) 2504df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("expected '{' in function body"); 2505df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); // eat the {. 2506df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2507df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PerFunctionState PFS(*this, Fn); 2508df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2509df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner while (Lex.getKind() != lltok::rbrace && Lex.getKind() != lltok::kw_end) 2510df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseBasicBlock(PFS)) return true; 2511df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2512df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Eat the }. 2513df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 2514df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2515df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Verify function is ok. 2516df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return PFS.VerifyFunctionComplete(); 2517df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2518df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2519df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseBasicBlock 2520df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= LabelStr? Instruction* 2521df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseBasicBlock(PerFunctionState &PFS) { 2522df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If this basic block starts out with a name, remember it. 2523df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::string Name; 2524df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy NameLoc = Lex.getLoc(); 2525df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() == lltok::LabelStr) { 2526df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Name = Lex.getStrVal(); 2527df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 2528df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2529df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2530df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner BasicBlock *BB = PFS.DefineBB(Name, NameLoc); 2531df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (BB == 0) return true; 2532df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2533df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::string NameStr; 2534df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2535df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Parse the instructions in this block until we get a terminator. 2536df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Instruction *Inst; 2537df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner do { 2538df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // This instruction may have three possibilities for a name: a) none 2539df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // specified, b) name specified "%foo =", c) number specified: "%4 =". 2540df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy NameLoc = Lex.getLoc(); 2541df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner int NameID = -1; 2542df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner NameStr = ""; 2543df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2544df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() == lltok::LocalVarID) { 2545df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner NameID = Lex.getUIntVal(); 2546df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 2547df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::equal, "expected '=' after instruction id")) 2548df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2549df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (Lex.getKind() == lltok::LocalVar || 2550df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: REMOVE IN LLVM 3.0 2551df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.getKind() == lltok::StringConstant) { 2552df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner NameStr = Lex.getStrVal(); 2553df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 2554df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::equal, "expected '=' after instruction name")) 2555df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2556df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2557df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2558df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseInstruction(Inst, BB, PFS)) return true; 2559df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2560df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner BB->getInstList().push_back(Inst); 2561df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2562df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Set the name on the instruction. 2563df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true; 2564df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } while (!isa<TerminatorInst>(Inst)); 2565df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2566df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2567df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2568df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2569df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 2570df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Instruction Parsing. 2571df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 2572df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2573df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseInstruction - Parse one of the many different instructions. 2574df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// 2575df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB, 2576df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PerFunctionState &PFS) { 2577df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner lltok::Kind Token = Lex.getKind(); 2578df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Token == lltok::Eof) 2579df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("found end of file when expecting more instructions"); 2580df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc = Lex.getLoc(); 2581f6f0bdfec336b596b095b4db8126149bbb33fec1Chris Lattner unsigned KeywordVal = Lex.getUIntVal(); 2582df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); // Eat the keyword. 2583df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2584df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Token) { 2585df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: return Error(Loc, "expected instruction opcode"); 2586df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Terminator Instructions. 2587df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_unwind: Inst = new UnwindInst(); return false; 2588df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_unreachable: Inst = new UnreachableInst(); return false; 2589df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ret: return ParseRet(Inst, BB, PFS); 2590df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_br: return ParseBr(Inst, PFS); 2591df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_switch: return ParseSwitch(Inst, PFS); 2592df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_invoke: return ParseInvoke(Inst, PFS); 2593df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Binary Operators. 2594df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_add: 2595df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sub: 259659858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman case lltok::kw_mul: { 259759858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman bool NUW = false; 259859858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman bool NSW = false; 259959858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman LocTy ModifierLoc = Lex.getLoc(); 260059858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (EatIfPresent(lltok::kw_nuw)) 260159858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman NUW = true; 260259858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (EatIfPresent(lltok::kw_nsw)) { 260359858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman NSW = true; 260459858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (EatIfPresent(lltok::kw_nuw)) 260559858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman NUW = true; 260659858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman } 2607ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman // API compatibility: Accept either integer or floating-point types. 260859858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman bool Result = ParseArithmetic(Inst, PFS, KeywordVal, 0); 260959858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (!Result) { 261059858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (!Inst->getType()->isIntOrIntVector()) { 261159858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (NUW) 261259858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman return Error(ModifierLoc, "nuw only applies to integer operations"); 261359858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (NSW) 261459858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman return Error(ModifierLoc, "nsw only applies to integer operations"); 261559858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman } 261659858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (NUW) 261759858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman cast<OverflowingBinaryOperator>(Inst)->setHasNoUnsignedOverflow(true); 261859858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (NSW) 261959858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman cast<OverflowingBinaryOperator>(Inst)->setHasNoSignedOverflow(true); 262059858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman } 262159858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman return Result; 262259858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman } 2623ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman case lltok::kw_fadd: 2624ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman case lltok::kw_fsub: 2625ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman case lltok::kw_fmul: return ParseArithmetic(Inst, PFS, KeywordVal, 2); 2626ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman 262759858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman case lltok::kw_sdiv: { 262859858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman bool Exact = false; 262959858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (EatIfPresent(lltok::kw_exact)) 263059858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman Exact = true; 263159858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman bool Result = ParseArithmetic(Inst, PFS, KeywordVal, 1); 263259858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (!Result) 263359858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman if (Exact) 263459858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman cast<SDivOperator>(Inst)->setIsExact(true); 263559858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman return Result; 263659858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman } 263759858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman 2638df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_udiv: 2639df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_urem: 2640f6f0bdfec336b596b095b4db8126149bbb33fec1Chris Lattner case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1); 2641e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner case lltok::kw_fdiv: 2642f6f0bdfec336b596b095b4db8126149bbb33fec1Chris Lattner case lltok::kw_frem: return ParseArithmetic(Inst, PFS, KeywordVal, 2); 2643df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_shl: 2644df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_lshr: 2645df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ashr: 2646df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_and: 2647df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_or: 2648f6f0bdfec336b596b095b4db8126149bbb33fec1Chris Lattner case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal); 2649df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_icmp: 26507f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal); 2651df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Casts. 2652df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_trunc: 2653df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_zext: 2654df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sext: 2655df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_fptrunc: 2656df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_fpext: 2657df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_bitcast: 2658df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_uitofp: 2659df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sitofp: 2660df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_fptoui: 2661df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_fptosi: 2662df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_inttoptr: 2663f6f0bdfec336b596b095b4db8126149bbb33fec1Chris Lattner case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal); 2664df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Other. 2665df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_select: return ParseSelect(Inst, PFS); 26660088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS); 2667df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS); 2668df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS); 2669df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS); 2670df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_phi: return ParsePHI(Inst, PFS); 2671df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_call: return ParseCall(Inst, PFS, false); 2672df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_tail: return ParseCall(Inst, PFS, true); 2673df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Memory. 2674df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_alloca: 2675f6f0bdfec336b596b095b4db8126149bbb33fec1Chris Lattner case lltok::kw_malloc: return ParseAlloc(Inst, PFS, KeywordVal); 2676df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_free: return ParseFree(Inst, PFS); 2677df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_load: return ParseLoad(Inst, PFS, false); 2678df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_store: return ParseStore(Inst, PFS, false); 2679df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_volatile: 26803ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (EatIfPresent(lltok::kw_load)) 2681df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return ParseLoad(Inst, PFS, true); 26823ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner else if (EatIfPresent(lltok::kw_store)) 2683df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return ParseStore(Inst, PFS, true); 26843ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner else 2685df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("expected 'load' or 'store'"); 2686df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_getresult: return ParseGetResult(Inst, PFS); 2687df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS); 2688df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS); 2689df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS); 2690df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2691df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2692df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2693df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind. 2694df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) { 26957f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky if (Opc == Instruction::FCmp) { 2696df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Lex.getKind()) { 2697df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: TokError("expected fcmp predicate (e.g. 'oeq')"); 2698df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break; 2699df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_one: P = CmpInst::FCMP_ONE; break; 2700df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_olt: P = CmpInst::FCMP_OLT; break; 2701df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break; 2702df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ole: P = CmpInst::FCMP_OLE; break; 2703df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_oge: P = CmpInst::FCMP_OGE; break; 2704df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ord: P = CmpInst::FCMP_ORD; break; 2705df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_uno: P = CmpInst::FCMP_UNO; break; 2706df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break; 2707df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_une: P = CmpInst::FCMP_UNE; break; 2708df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ult: P = CmpInst::FCMP_ULT; break; 2709df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break; 2710df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ule: P = CmpInst::FCMP_ULE; break; 2711df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_uge: P = CmpInst::FCMP_UGE; break; 2712df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_true: P = CmpInst::FCMP_TRUE; break; 2713df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_false: P = CmpInst::FCMP_FALSE; break; 2714df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2715df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 2716df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Lex.getKind()) { 2717df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: TokError("expected icmp predicate (e.g. 'eq')"); 2718df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_eq: P = CmpInst::ICMP_EQ; break; 2719df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ne: P = CmpInst::ICMP_NE; break; 2720df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_slt: P = CmpInst::ICMP_SLT; break; 2721df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break; 2722df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sle: P = CmpInst::ICMP_SLE; break; 2723df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sge: P = CmpInst::ICMP_SGE; break; 2724df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ult: P = CmpInst::ICMP_ULT; break; 2725df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break; 2726df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ule: P = CmpInst::ICMP_ULE; break; 2727df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_uge: P = CmpInst::ICMP_UGE; break; 2728df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2729df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2730df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 2731df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2732df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2733df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2734df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 2735df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Terminator Instructions. 2736df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 2737df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2738df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseRet - Parse a return instruction. 2739df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'ret' void 2740df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'ret' TypeAndValue 2741df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'ret' TypeAndValue (',' TypeAndValue)+ [[obsolete: LLVM 3.0]] 2742df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB, 2743df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PerFunctionState &PFS) { 2744df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder Ty(Type::VoidTy); 2745a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner if (ParseType(Ty, true /*void allowed*/)) return true; 2746df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2747df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Ty == Type::VoidTy) { 2748df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = ReturnInst::Create(); 2749df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2750df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2751df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2752df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *RV; 2753df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseValue(Ty, RV, PFS)) return true; 2754df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2755df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // The normal case is one return value. 2756df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() == lltok::comma) { 2757df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: LLVM 3.0 remove MRV support for 'ret i32 1, i32 2', requiring use 2758df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // of 'ret {i32,i32} {i32 1, i32 2}' 2759df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<Value*, 8> RVs; 2760df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner RVs.push_back(RV); 2761df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 27623ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner while (EatIfPresent(lltok::comma)) { 2763df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(RV, PFS)) return true; 2764df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner RVs.push_back(RV); 2765df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2766df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 27679e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson RV = UndefValue::get(PFS.getFunction().getReturnType()); 2768df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = RVs.size(); i != e; ++i) { 2769df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Instruction *I = InsertValueInst::Create(RV, RVs[i], i, "mrv"); 2770df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner BB->getInstList().push_back(I); 2771df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner RV = I; 2772df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2773df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2774df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = ReturnInst::Create(RV); 2775df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2776df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2777df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2778df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2779df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseBr 2780df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'br' TypeAndValue 2781df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue 2782df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) { 2783df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc, Loc2; 2784df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Op0, *Op1, *Op2; 2785df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Op0, Loc, PFS)) return true; 2786df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2787df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) { 2788df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = BranchInst::Create(BB); 2789df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2790df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2791df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2792df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Op0->getType() != Type::Int1Ty) 2793df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "branch condition must have 'i1' type"); 2794df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2795df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::comma, "expected ',' after branch condition") || 2796df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(Op1, Loc, PFS) || 2797df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after true destination") || 2798df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(Op2, Loc2, PFS)) 2799df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2800df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2801df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<BasicBlock>(Op1)) 2802df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "true destination of branch must be a basic block"); 2803df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<BasicBlock>(Op2)) 2804df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc2, "true destination of branch must be a basic block"); 2805df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2806df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = BranchInst::Create(cast<BasicBlock>(Op1), cast<BasicBlock>(Op2), Op0); 2807df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2808df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2809df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2810df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseSwitch 2811df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// Instruction 2812df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']' 2813df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// JumpTable 2814df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= (TypeAndValue ',' TypeAndValue)* 2815df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) { 2816df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy CondLoc, BBLoc; 2817df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Cond, *DefaultBB; 2818df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Cond, CondLoc, PFS) || 2819df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after switch condition") || 2820df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(DefaultBB, BBLoc, PFS) || 2821df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::lsquare, "expected '[' with switch table")) 2822df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2823df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2824df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<IntegerType>(Cond->getType())) 2825df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(CondLoc, "switch condition must have integer type"); 2826df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<BasicBlock>(DefaultBB)) 2827df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(BBLoc, "default destination must be a basic block"); 2828df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2829df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Parse the jump table pairs. 2830df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallPtrSet<Value*, 32> SeenCases; 2831df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table; 2832df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner while (Lex.getKind() != lltok::rsquare) { 2833df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Constant, *DestBB; 2834df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2835df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Constant, CondLoc, PFS) || 2836df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after case value") || 2837df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(DestBB, BBLoc, PFS)) 2838df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2839df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2840df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!SeenCases.insert(Constant)) 2841df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(CondLoc, "duplicate case value in switch"); 2842df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<ConstantInt>(Constant)) 2843df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(CondLoc, "case value is not a constant integer"); 2844df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<BasicBlock>(DestBB)) 2845df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(BBLoc, "case destination is not a basic block"); 2846df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2847df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Table.push_back(std::make_pair(cast<ConstantInt>(Constant), 2848df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner cast<BasicBlock>(DestBB))); 2849df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2850df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2851df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); // Eat the ']'. 2852df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2853df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SwitchInst *SI = SwitchInst::Create(Cond, cast<BasicBlock>(DefaultBB), 2854df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Table.size()); 2855df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = Table.size(); i != e; ++i) 2856df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SI->addCase(Table[i].first, Table[i].second); 2857df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = SI; 2858df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2859df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2860df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2861df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseInvoke 2862df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList 2863df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue 2864df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) { 2865df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy CallLoc = Lex.getLoc(); 2866df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned CC, RetAttrs, FnAttrs; 2867df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder RetType(Type::VoidTy); 2868df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy RetTypeLoc; 2869df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ValID CalleeID; 2870df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<ParamInfo, 16> ArgList; 2871df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2872df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *NormalBB, *UnwindBB; 2873df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseOptionalCallingConv(CC) || 2874df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalAttrs(RetAttrs, 1) || 2875a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner ParseType(RetType, RetTypeLoc, true /*void allowed*/) || 2876df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseValID(CalleeID) || 2877df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseParameterList(ArgList, PFS) || 2878df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalAttrs(FnAttrs, 2) || 2879df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::kw_to, "expected 'to' in invoke") || 2880df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(NormalBB, PFS) || 2881df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") || 2882df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(UnwindBB, PFS)) 2883df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2884df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2885df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<BasicBlock>(NormalBB)) 2886df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(CallLoc, "normal destination is not a basic block"); 2887df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<BasicBlock>(UnwindBB)) 2888df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(CallLoc, "unwind destination is not a basic block"); 2889df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2890df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If RetType is a non-function pointer type, then this is the short syntax 2891df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // for the call, which means that RetType is just the return type. Infer the 2892df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // rest of the function argument types from the arguments that are present. 2893df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const PointerType *PFTy = 0; 2894df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const FunctionType *Ty = 0; 2895df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!(PFTy = dyn_cast<PointerType>(RetType)) || 2896df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) { 2897df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Pull out the types of all of the arguments... 2898df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::vector<const Type*> ParamTypes; 2899df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = ArgList.size(); i != e; ++i) 2900df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParamTypes.push_back(ArgList[i].V->getType()); 2901df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2902df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!FunctionType::isValidReturnType(RetType)) 2903df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(RetTypeLoc, "Invalid result type for LLVM function"); 2904df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2905debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson Ty = FunctionType::get(RetType, ParamTypes, false); 2906debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson PFTy = PointerType::getUnqual(Ty); 2907df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2908df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2909df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Look up the callee. 2910df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Callee; 2911df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ConvertValIDToValue(PFTy, CalleeID, Callee, PFS)) return true; 2912df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2913df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: In LLVM 3.0, stop accepting zext, sext and inreg as optional 2914df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // function attributes. 2915df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg; 2916df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FnAttrs & ObsoleteFuncAttrs) { 2917df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner RetAttrs |= FnAttrs & ObsoleteFuncAttrs; 2918df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FnAttrs &= ~ObsoleteFuncAttrs; 2919df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2920df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2921df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Set up the Attributes for the function. 2922df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<AttributeWithIndex, 8> Attrs; 2923df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (RetAttrs != Attribute::None) 2924df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs.push_back(AttributeWithIndex::get(0, RetAttrs)); 2925df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2926df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<Value*, 8> Args; 2927df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2928df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Loop through FunctionType's arguments and ensure they are specified 2929df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // correctly. Also, gather any parameter attributes. 2930df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FunctionType::param_iterator I = Ty->param_begin(); 2931df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FunctionType::param_iterator E = Ty->param_end(); 2932df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { 2933df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const Type *ExpectedTy = 0; 2934df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I != E) { 2935df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ExpectedTy = *I++; 2936df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (!Ty->isVarArg()) { 2937df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ArgList[i].Loc, "too many arguments specified"); 2938df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2939df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2940df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ExpectedTy && ExpectedTy != ArgList[i].V->getType()) 2941df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ArgList[i].Loc, "argument is not of expected type '" + 2942df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ExpectedTy->getDescription() + "'"); 2943df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Args.push_back(ArgList[i].V); 2944df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ArgList[i].Attrs != Attribute::None) 2945df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs)); 2946df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2947df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2948df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I != E) 2949df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(CallLoc, "not enough parameters specified for call"); 2950df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2951df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FnAttrs != Attribute::None) 2952df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs.push_back(AttributeWithIndex::get(~0, FnAttrs)); 2953df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2954df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Finish off the Attributes and check them 2955df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end()); 2956df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2957df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner InvokeInst *II = InvokeInst::Create(Callee, cast<BasicBlock>(NormalBB), 2958df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner cast<BasicBlock>(UnwindBB), 2959df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Args.begin(), Args.end()); 2960df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner II->setCallingConv(CC); 2961df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner II->setAttributes(PAL); 2962df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = II; 2963df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2964df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2965df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2966df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2967df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2968df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 2969df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Binary Operators. 2970df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 2971df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2972df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseArithmetic 2973e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner/// ::= ArithmeticOps TypeAndValue ',' Value 2974e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner/// 2975e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1, 2976e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner/// then any integer operand is allowed, if it is 2, any fp operand is allowed. 2977df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS, 2978e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner unsigned Opc, unsigned OperandType) { 2979df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc; Value *LHS, *RHS; 2980df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(LHS, Loc, PFS) || 2981df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' in arithmetic operation") || 2982df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseValue(LHS->getType(), RHS, PFS)) 2983df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2984df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2985e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner bool Valid; 2986e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner switch (OperandType) { 2987c23197a26f34f559ea9797de51e187087c039c42Torok Edwin default: llvm_unreachable("Unknown operand type!"); 2988e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner case 0: // int or FP. 2989e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner Valid = LHS->getType()->isIntOrIntVector() || 2990e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner LHS->getType()->isFPOrFPVector(); 2991e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner break; 2992e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner case 1: Valid = LHS->getType()->isIntOrIntVector(); break; 2993e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner case 2: Valid = LHS->getType()->isFPOrFPVector(); break; 2994e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner } 2995e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner 2996e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner if (!Valid) 2997e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner return Error(Loc, "invalid operand type for instruction"); 2998df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2999df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); 3000df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3001df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3002df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3003df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseLogical 3004df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= ArithmeticOps TypeAndValue ',' Value { 3005df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS, 3006df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Opc) { 3007df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc; Value *LHS, *RHS; 3008df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(LHS, Loc, PFS) || 3009df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' in logical operation") || 3010df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseValue(LHS->getType(), RHS, PFS)) 3011df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 3012df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3013df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!LHS->getType()->isIntOrIntVector()) 3014df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc,"instruction requires integer or integer vector operands"); 3015df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3016df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); 3017df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3018df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3019df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3020df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3021df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseCompare 3022df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'icmp' IPredicates TypeAndValue ',' Value 3023df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'fcmp' FPredicates TypeAndValue ',' Value 3024df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS, 3025df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Opc) { 3026df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Parse the integer/fp comparison predicate. 3027df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc; 3028df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Pred; 3029df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *LHS, *RHS; 3030df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseCmpPredicate(Pred, Opc) || 3031df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(LHS, Loc, PFS) || 3032df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after compare value") || 3033df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseValue(LHS->getType(), RHS, PFS)) 3034df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 3035df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3036df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Opc == Instruction::FCmp) { 3037df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!LHS->getType()->isFPOrFPVector()) 3038df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "fcmp requires floating point operands"); 3039333c40096561218bc3597cf153c0a3895274414cOwen Anderson Inst = new FCmpInst(Context, CmpInst::Predicate(Pred), LHS, RHS); 30407f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky } else { 30417f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!"); 3042df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!LHS->getType()->isIntOrIntVector() && 3043df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner !isa<PointerType>(LHS->getType())) 3044df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "icmp requires integer operands"); 3045333c40096561218bc3597cf153c0a3895274414cOwen Anderson Inst = new ICmpInst(Context, CmpInst::Predicate(Pred), LHS, RHS); 3046df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 3047df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3048df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3049df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3050df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 3051df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Other Instructions. 3052df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 3053df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3054df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3055df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseCast 3056df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= CastOpc TypeAndValue 'to' Type 3057df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS, 3058df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Opc) { 3059df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc; Value *Op; 3060df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder DestTy(Type::VoidTy); 3061df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Op, Loc, PFS) || 3062df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::kw_to, "expected 'to' after cast value") || 3063df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseType(DestTy)) 3064df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 3065df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3066f6f0bdfec336b596b095b4db8126149bbb33fec1Chris Lattner if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) { 3067f6f0bdfec336b596b095b4db8126149bbb33fec1Chris Lattner CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy); 3068df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "invalid cast opcode for cast from '" + 3069df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Op->getType()->getDescription() + "' to '" + 3070df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner DestTy->getDescription() + "'"); 3071f6f0bdfec336b596b095b4db8126149bbb33fec1Chris Lattner } 3072df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy); 3073df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3074df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3075df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3076df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseSelect 3077df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue 3078df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) { 3079df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc; 3080df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Op0, *Op1, *Op2; 3081df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Op0, Loc, PFS) || 3082df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after select condition") || 3083df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(Op1, PFS) || 3084df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after select value") || 3085df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(Op2, PFS)) 3086df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 3087df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3088df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2)) 3089df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, Reason); 3090df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3091df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = SelectInst::Create(Op0, Op1, Op2); 3092df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3093df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3094df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 30950088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner/// ParseVA_Arg 30960088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner/// ::= 'va_arg' TypeAndValue ',' Type 30970088a5cc722b9e32820d31c70175a7650ad4772dChris Lattnerbool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) { 3098df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Op; 3099df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder EltTy(Type::VoidTy); 31000088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner LocTy TypeLoc; 3101df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Op, PFS) || 3102df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after vaarg operand") || 31030088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner ParseType(EltTy, TypeLoc)) 3104df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 31050088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner 31060088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner if (!EltTy->isFirstClassType()) 31070088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner return Error(TypeLoc, "va_arg requires operand with first class type"); 3108df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3109df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = new VAArgInst(Op, EltTy); 3110df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3111df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3112df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3113df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseExtractElement 3114df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'extractelement' TypeAndValue ',' TypeAndValue 3115df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) { 3116df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc; 3117df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Op0, *Op1; 3118df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Op0, Loc, PFS) || 3119df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after extract value") || 3120df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(Op1, PFS)) 3121df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 3122df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3123df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ExtractElementInst::isValidOperands(Op0, Op1)) 3124df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "invalid extractelement operands"); 3125df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3126a3500da5592aee83675d6714d4f1e9d5ad96d1f2Eric Christopher Inst = ExtractElementInst::Create(Op0, Op1); 3127df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3128df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3129df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3130df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseInsertElement 3131df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue 3132df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) { 3133df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc; 3134df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Op0, *Op1, *Op2; 3135df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Op0, Loc, PFS) || 3136df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after insertelement value") || 3137df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(Op1, PFS) || 3138df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after insertelement value") || 3139df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(Op2, PFS)) 3140df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 3141df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3142df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!InsertElementInst::isValidOperands(Op0, Op1, Op2)) 31430aaf4e91c2961585c160af0cc87f040301aafbd2Eric Christopher return Error(Loc, "invalid insertelement operands"); 3144df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3145df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = InsertElementInst::Create(Op0, Op1, Op2); 3146df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3147df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3148df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3149df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseShuffleVector 3150df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue 3151df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) { 3152df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc; 3153df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Op0, *Op1, *Op2; 3154df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Op0, Loc, PFS) || 3155df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after shuffle mask") || 3156df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(Op1, PFS) || 3157df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after shuffle value") || 3158df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(Op2, PFS)) 3159df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 3160df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3161df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2)) 3162df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "invalid extractelement operands"); 3163df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3164df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = new ShuffleVectorInst(Op0, Op1, Op2); 3165df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3166df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3167df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3168df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParsePHI 3169df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Valueß ']')* 3170df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) { 3171df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder Ty(Type::VoidTy); 3172df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Op0, *Op1; 3173df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy TypeLoc = Lex.getLoc(); 3174df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3175df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseType(Ty) || 3176df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::lsquare, "expected '[' in phi value list") || 3177df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseValue(Ty, Op0, PFS) || 3178df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after insertelement value") || 3179df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseValue(Type::LabelTy, Op1, PFS) || 3180df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rsquare, "expected ']' in phi value list")) 3181df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 3182df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3183df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals; 3184df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner while (1) { 3185df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1))); 3186df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 31873ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (!EatIfPresent(lltok::comma)) 3188df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 3189df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 31903ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseToken(lltok::lsquare, "expected '[' in phi value list") || 3191df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseValue(Ty, Op0, PFS) || 3192df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after insertelement value") || 3193df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseValue(Type::LabelTy, Op1, PFS) || 3194df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rsquare, "expected ']' in phi value list")) 3195df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 3196df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 3197df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3198df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!Ty->isFirstClassType()) 3199df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(TypeLoc, "phi node must have first class type"); 3200df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3201df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PHINode *PN = PHINode::Create(Ty); 3202df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PN->reserveOperandSpace(PHIVals.size()); 3203df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = PHIVals.size(); i != e; ++i) 3204df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PN->addIncoming(PHIVals[i].first, PHIVals[i].second); 3205df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = PN; 3206df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3207df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3208df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3209df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseCall 3210df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'tail'? 'call' OptionalCallingConv OptionalAttrs Type Value 3211df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParameterList OptionalAttrs 3212df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS, 3213df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner bool isTail) { 3214df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned CC, RetAttrs, FnAttrs; 3215df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder RetType(Type::VoidTy); 3216df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy RetTypeLoc; 3217df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ValID CalleeID; 3218df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<ParamInfo, 16> ArgList; 3219df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy CallLoc = Lex.getLoc(); 3220df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3221df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if ((isTail && ParseToken(lltok::kw_call, "expected 'tail call'")) || 3222df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalCallingConv(CC) || 3223df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalAttrs(RetAttrs, 1) || 3224a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner ParseType(RetType, RetTypeLoc, true /*void allowed*/) || 3225df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseValID(CalleeID) || 3226df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseParameterList(ArgList, PFS) || 3227df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalAttrs(FnAttrs, 2)) 3228df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 3229df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3230df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If RetType is a non-function pointer type, then this is the short syntax 3231df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // for the call, which means that RetType is just the return type. Infer the 3232df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // rest of the function argument types from the arguments that are present. 3233df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const PointerType *PFTy = 0; 3234df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const FunctionType *Ty = 0; 3235df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!(PFTy = dyn_cast<PointerType>(RetType)) || 3236df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) { 3237df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Pull out the types of all of the arguments... 3238df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::vector<const Type*> ParamTypes; 3239df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = ArgList.size(); i != e; ++i) 3240df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParamTypes.push_back(ArgList[i].V->getType()); 3241df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3242df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!FunctionType::isValidReturnType(RetType)) 3243df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(RetTypeLoc, "Invalid result type for LLVM function"); 3244df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3245debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson Ty = FunctionType::get(RetType, ParamTypes, false); 3246debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson PFTy = PointerType::getUnqual(Ty); 3247df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 3248df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3249df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Look up the callee. 3250df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Callee; 3251df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ConvertValIDToValue(PFTy, CalleeID, Callee, PFS)) return true; 3252df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3253df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: In LLVM 3.0, stop accepting zext, sext and inreg as optional 3254df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // function attributes. 3255df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg; 3256df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FnAttrs & ObsoleteFuncAttrs) { 3257df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner RetAttrs |= FnAttrs & ObsoleteFuncAttrs; 3258df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FnAttrs &= ~ObsoleteFuncAttrs; 3259df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 3260df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3261df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Set up the Attributes for the function. 3262df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<AttributeWithIndex, 8> Attrs; 3263df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (RetAttrs != Attribute::None) 3264df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs.push_back(AttributeWithIndex::get(0, RetAttrs)); 3265df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3266df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<Value*, 8> Args; 3267df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3268df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Loop through FunctionType's arguments and ensure they are specified 3269df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // correctly. Also, gather any parameter attributes. 3270df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FunctionType::param_iterator I = Ty->param_begin(); 3271df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FunctionType::param_iterator E = Ty->param_end(); 3272df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { 3273df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const Type *ExpectedTy = 0; 3274df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I != E) { 3275df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ExpectedTy = *I++; 3276df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (!Ty->isVarArg()) { 3277df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ArgList[i].Loc, "too many arguments specified"); 3278df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 3279df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3280df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ExpectedTy && ExpectedTy != ArgList[i].V->getType()) 3281df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ArgList[i].Loc, "argument is not of expected type '" + 3282df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ExpectedTy->getDescription() + "'"); 3283df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Args.push_back(ArgList[i].V); 3284df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ArgList[i].Attrs != Attribute::None) 3285df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs)); 3286df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 3287df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3288df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I != E) 3289df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(CallLoc, "not enough parameters specified for call"); 3290df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3291df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FnAttrs != Attribute::None) 3292df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs.push_back(AttributeWithIndex::get(~0, FnAttrs)); 3293df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3294df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Finish off the Attributes and check them 3295df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end()); 3296df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3297df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner CallInst *CI = CallInst::Create(Callee, Args.begin(), Args.end()); 3298df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner CI->setTailCall(isTail); 3299df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner CI->setCallingConv(CC); 3300df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner CI->setAttributes(PAL); 3301df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = CI; 3302df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3303df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3304df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3305df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 3306df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Memory Instructions. 3307df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 3308df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3309df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseAlloc 3310df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'malloc' Type (',' TypeAndValue)? (',' OptionalAlignment)? 3311df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'alloca' Type (',' TypeAndValue)? (',' OptionalAlignment)? 3312df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS, 3313df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Opc) { 3314df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder Ty(Type::VoidTy); 3315df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Size = 0; 3316eeb4a84ac8d91fb1d5a7c484a1c7047409faee30Chris Lattner LocTy SizeLoc; 3317df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Alignment = 0; 33183ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseType(Ty)) return true; 3319df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 33203ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (EatIfPresent(lltok::comma)) { 3321df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() == lltok::kw_align) { 3322df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseOptionalAlignment(Alignment)) return true; 33233ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner } else if (ParseTypeAndValue(Size, SizeLoc, PFS) || 33243ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseOptionalCommaAlignment(Alignment)) { 33253ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return true; 3326df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 3327df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 3328df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3329df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Size && Size->getType() != Type::Int32Ty) 3330df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(SizeLoc, "element count must be i32"); 3331df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3332df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Opc == Instruction::Malloc) 333350dead06ffc107edb7e84857baaeeb09039c631cOwen Anderson Inst = new MallocInst(Ty, Size, Alignment); 3334df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else 333550dead06ffc107edb7e84857baaeeb09039c631cOwen Anderson Inst = new AllocaInst(Ty, Size, Alignment); 3336df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3337df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3338df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3339df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseFree 3340df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'free' TypeAndValue 3341df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseFree(Instruction *&Inst, PerFunctionState &PFS) { 3342df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Val; LocTy Loc; 3343df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Val, Loc, PFS)) return true; 3344df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<PointerType>(Val->getType())) 3345df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "operand to free must be a pointer"); 3346df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = new FreeInst(Val); 3347df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3348df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3349df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3350df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseLoad 3351a119de86a064414622562cfe32953de7f9b0ee40Dan Gohman/// ::= 'volatile'? 'load' TypeAndValue (',' 'align' i32)? 3352df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS, 3353df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner bool isVolatile) { 3354df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Val; LocTy Loc; 3355df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Alignment; 3356df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Val, Loc, PFS) || 3357df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalCommaAlignment(Alignment)) 3358df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 3359df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3360df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<PointerType>(Val->getType()) || 3361df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType()) 3362df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "load operand must be a pointer to a first class type"); 3363df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3364df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = new LoadInst(Val, "", isVolatile, Alignment); 3365df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3366df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3367df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3368df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseStore 3369a119de86a064414622562cfe32953de7f9b0ee40Dan Gohman/// ::= 'volatile'? 'store' TypeAndValue ',' TypeAndValue (',' 'align' i32)? 3370df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS, 3371df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner bool isVolatile) { 3372df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Val, *Ptr; LocTy Loc, PtrLoc; 3373df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Alignment; 3374df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Val, Loc, PFS) || 3375df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after store operand") || 3376df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(Ptr, PtrLoc, PFS) || 3377df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalCommaAlignment(Alignment)) 3378df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 3379df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3380df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<PointerType>(Ptr->getType())) 3381df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(PtrLoc, "store operand must be a pointer"); 3382df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!Val->getType()->isFirstClassType()) 3383df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "store operand must be a first class value"); 3384df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType()) 3385df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "stored value and pointer type do not match"); 3386df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3387df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = new StoreInst(Val, Ptr, isVolatile, Alignment); 3388df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3389df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3390df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3391df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseGetResult 3392a119de86a064414622562cfe32953de7f9b0ee40Dan Gohman/// ::= 'getresult' TypeAndValue ',' i32 3393df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// FIXME: Remove support for getresult in LLVM 3.0 3394df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseGetResult(Instruction *&Inst, PerFunctionState &PFS) { 3395df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Val; LocTy ValLoc, EltLoc; 3396df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Element; 3397df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Val, ValLoc, PFS) || 3398df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after getresult operand") || 33993ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseUInt32(Element, EltLoc)) 3400df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 3401df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3402df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<StructType>(Val->getType()) && !isa<ArrayType>(Val->getType())) 3403df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ValLoc, "getresult inst requires an aggregate operand"); 3404df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ExtractValueInst::getIndexedType(Val->getType(), Element)) 3405df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(EltLoc, "invalid getresult index for value"); 3406df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = ExtractValueInst::Create(Val, Element); 3407df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3408df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3409df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3410df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseGetElementPtr 3411dd8004dc73d091ccb3927dbbc3b41639a3738ae3Dan Gohman/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)* 3412df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) { 3413df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Ptr, *Val; LocTy Loc, EltLoc; 3414dd8004dc73d091ccb3927dbbc3b41639a3738ae3Dan Gohman 3415dcb40a3c1c747c88c4e634e34d1b4d7533a86adaDan Gohman bool InBounds = EatIfPresent(lltok::kw_inbounds); 3416dd8004dc73d091ccb3927dbbc3b41639a3738ae3Dan Gohman 34173ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseTypeAndValue(Ptr, Loc, PFS)) return true; 3418df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3419df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<PointerType>(Ptr->getType())) 3420df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "base of getelementptr must be a pointer"); 3421df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3422df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<Value*, 16> Indices; 34233ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner while (EatIfPresent(lltok::comma)) { 34243ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseTypeAndValue(Val, EltLoc, PFS)) return true; 3425df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<IntegerType>(Val->getType())) 3426df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(EltLoc, "getelementptr index must be an integer"); 3427df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Indices.push_back(Val); 3428df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 3429df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3430df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!GetElementPtrInst::getIndexedType(Ptr->getType(), 3431df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Indices.begin(), Indices.end())) 3432df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "invalid getelementptr indices"); 3433df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = GetElementPtrInst::Create(Ptr, Indices.begin(), Indices.end()); 3434dd8004dc73d091ccb3927dbbc3b41639a3738ae3Dan Gohman if (InBounds) 3435dd8004dc73d091ccb3927dbbc3b41639a3738ae3Dan Gohman cast<GEPOperator>(Inst)->setIsInBounds(true); 3436df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3437df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3438df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3439df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseExtractValue 3440df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'extractvalue' TypeAndValue (',' uint32)+ 3441df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) { 3442df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Val; LocTy Loc; 3443df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<unsigned, 4> Indices; 3444df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Val, Loc, PFS) || 3445df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseIndexList(Indices)) 3446df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 3447df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3448df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<StructType>(Val->getType()) && !isa<ArrayType>(Val->getType())) 3449df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "extractvalue operand must be array or struct"); 3450df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3451df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(), 3452df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Indices.end())) 3453df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "invalid indices for extractvalue"); 3454df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = ExtractValueInst::Create(Val, Indices.begin(), Indices.end()); 3455df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3456df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3457df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3458df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseInsertValue 3459df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+ 3460df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) { 3461df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Val0, *Val1; LocTy Loc0, Loc1; 3462df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<unsigned, 4> Indices; 3463df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Val0, Loc0, PFS) || 3464df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected comma after insertvalue operand") || 3465df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(Val1, Loc1, PFS) || 3466df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseIndexList(Indices)) 3467df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 3468df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3469df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<StructType>(Val0->getType()) && !isa<ArrayType>(Val0->getType())) 3470df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc0, "extractvalue operand must be array or struct"); 3471df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3472df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(), 3473df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Indices.end())) 3474df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc0, "invalid indices for insertvalue"); 3475df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = InsertValueInst::Create(Val0, Val1, Indices.begin(), Indices.end()); 3476df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3477df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 347821cc4460efa104e8591b05a90f20130291614344Nick Lewycky 347921cc4460efa104e8591b05a90f20130291614344Nick Lewycky//===----------------------------------------------------------------------===// 348021cc4460efa104e8591b05a90f20130291614344Nick Lewycky// Embedded metadata. 348121cc4460efa104e8591b05a90f20130291614344Nick Lewycky//===----------------------------------------------------------------------===// 348221cc4460efa104e8591b05a90f20130291614344Nick Lewycky 348321cc4460efa104e8591b05a90f20130291614344Nick Lewycky/// ParseMDNodeVector 3484cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky/// ::= Element (',' Element)* 3485cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky/// Element 3486cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky/// ::= 'null' | TypeAndValue 3487cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewyckybool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts) { 348821cc4460efa104e8591b05a90f20130291614344Nick Lewycky assert(Lex.getKind() == lltok::lbrace); 348921cc4460efa104e8591b05a90f20130291614344Nick Lewycky Lex.Lex(); 349021cc4460efa104e8591b05a90f20130291614344Nick Lewycky do { 3491db5e90078ea1034cf992e7d173517d1001f089cdDevang Patel Value *V = 0; 3492cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky if (Lex.getKind() == lltok::kw_null) { 3493cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky Lex.Lex(); 3494cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky V = 0; 3495cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky } else { 3496e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel PATypeHolder Ty(Type::VoidTy); 3497e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel if (ParseType(Ty)) return true; 3498e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel if (Lex.getKind() == lltok::Metadata) { 3499e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel Lex.Lex(); 3500104cf9e02b0ed94d4173869a598af6c6972a8660Devang Patel MetadataBase *Node = 0; 3501e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel if (!ParseMDNode(Node)) 3502e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel V = Node; 3503e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel else { 3504e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel MetadataBase *MDS = 0; 3505e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel if (ParseMDString(MDS)) return true; 3506e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel V = MDS; 3507e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel } 3508e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel } else { 3509e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel Constant *C; 3510e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel if (ParseGlobalValue(Ty, C)) return true; 3511e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel V = C; 3512e54abc90fe9942ef3902040a7ac475ce0c369dc9Devang Patel } 3513cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky } 3514cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky Elts.push_back(V); 351521cc4460efa104e8591b05a90f20130291614344Nick Lewycky } while (EatIfPresent(lltok::comma)); 351621cc4460efa104e8591b05a90f20130291614344Nick Lewycky 351721cc4460efa104e8591b05a90f20130291614344Nick Lewycky return false; 351821cc4460efa104e8591b05a90f20130291614344Nick Lewycky} 3519