LLParser.cpp revision b4bd16fc5e3dd416cf68e73cff93544d364159d6
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" 21df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/Module.h" 22df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/ValueSymbolTable.h" 23df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/ADT/SmallPtrSet.h" 24df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/ADT/StringExtras.h" 25df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/Support/raw_ostream.h" 26df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerusing namespace llvm; 27df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 28df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnernamespace llvm { 293ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner /// ValID - Represents a reference of a definition of some sort with no type. 303ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner /// There are several cases where we have to parse the value but where the 313ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner /// type can depend on later context. This may either be a numeric reference 323ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner /// or a symbolic (%var) reference. This is just a discriminated union. 33df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner struct ValID { 34df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner enum { 35df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner t_LocalID, t_GlobalID, // ID in UIntVal. 36df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner t_LocalName, t_GlobalName, // Name in StrVal. 37df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner t_APSInt, t_APFloat, // Value in APSIntVal/APFloatVal. 38df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner t_Null, t_Undef, t_Zero, // No value. 39081b505f77b183b7b029818218245cf4986e8948Chris Lattner t_EmptyArray, // No value: [] 40df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner t_Constant, // Value in ConstantVal. 41df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner t_InlineAsm // Value in StrVal/StrVal2/UIntVal. 42df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } Kind; 43df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 44df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LLParser::LocTy Loc; 45df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned UIntVal; 46df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::string StrVal, StrVal2; 47df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner APSInt APSIntVal; 48df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner APFloat APFloatVal; 49df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *ConstantVal; 50df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ValID() : APFloatVal(0.0) {} 51df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner }; 52df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 53df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 543ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// Run: module ::= toplevelentity* 55ad7d1e2085a489dbedc65b99bab811771ead1aabChris Lattnerbool LLParser::Run() { 563ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner // Prime the lexer. 573ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner Lex.Lex(); 583ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner 59ad7d1e2085a489dbedc65b99bab811771ead1aabChris Lattner return ParseTopLevelEntities() || 60ad7d1e2085a489dbedc65b99bab811771ead1aabChris Lattner ValidateEndOfModule(); 61df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 62df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 63df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ValidateEndOfModule - Do final validity and sanity checks at the end of the 64df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// module. 65df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ValidateEndOfModule() { 66df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ForwardRefTypes.empty()) 67df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ForwardRefTypes.begin()->second.second, 68df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "use of undefined type named '" + 69df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefTypes.begin()->first + "'"); 70df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ForwardRefTypeIDs.empty()) 71df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ForwardRefTypeIDs.begin()->second.second, 72df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "use of undefined type '%" + 73df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner utostr(ForwardRefTypeIDs.begin()->first) + "'"); 74df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 75df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ForwardRefVals.empty()) 76df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ForwardRefVals.begin()->second.second, 77df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "use of undefined value '@" + ForwardRefVals.begin()->first + 78df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "'"); 79df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 80df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ForwardRefValIDs.empty()) 81df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ForwardRefValIDs.begin()->second.second, 82df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "use of undefined value '@" + 83df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner utostr(ForwardRefValIDs.begin()->first) + "'"); 84df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 85df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Look for intrinsic functions and CallInst that need to be upgraded 86df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ) 87df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove 88df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 89df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 90df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 91df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 92df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 93df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Top-Level Entities 94df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 95df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 96df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseTopLevelEntities() { 97df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner while (1) { 98df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Lex.getKind()) { 99df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: return TokError("expected top-level entity"); 100df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::Eof: return false; 101df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner //case lltok::kw_define: 102df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_declare: if (ParseDeclare()) return true; break; 103df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_define: if (ParseDefine()) return true; break; 104df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_module: if (ParseModuleAsm()) return true; break; 105df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_target: if (ParseTargetDefinition()) return true; break; 106df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_deplibs: if (ParseDepLibs()) return true; break; 107df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_type: if (ParseUnnamedType()) return true; break; 108df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::StringConstant: // FIXME: REMOVE IN LLVM 3.0 109df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::LocalVar: if (ParseNamedType()) return true; break; 110df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break; 111df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 112df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // The Global variable production with no name can have many different 113df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // optional leading prefixes, the production is: 114df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalThreadLocal 115df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // OptionalAddrSpace ('constant'|'global') ... 116bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola case lltok::kw_private: // OptionalLinkage 117df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_internal: // OptionalLinkage 118df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_weak: // OptionalLinkage 119df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_linkonce: // OptionalLinkage 120df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_appending: // OptionalLinkage 121df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_dllexport: // OptionalLinkage 122df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_common: // OptionalLinkage 123df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_dllimport: // OptionalLinkage 124df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_extern_weak: // OptionalLinkage 125df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_external: { // OptionalLinkage 126df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Linkage, Visibility; 127df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseOptionalLinkage(Linkage) || 128df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalVisibility(Visibility) || 129df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobal("", 0, Linkage, true, Visibility)) 130df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 131df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 132df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 133df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_default: // OptionalVisibility 134df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_hidden: // OptionalVisibility 135df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_protected: { // OptionalVisibility 136df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Visibility; 137df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseOptionalVisibility(Visibility) || 138df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobal("", 0, 0, false, Visibility)) 139df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 140df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 141df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 142df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 143df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_thread_local: // OptionalThreadLocal 144df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_addrspace: // OptionalAddrSpace 145df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_constant: // GlobalType 146df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_global: // GlobalType 147df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseGlobal("", 0, 0, false, 0)) return true; 148df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 149df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 150df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 151df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 152df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 153df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 154df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// toplevelentity 155df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'module' 'asm' STRINGCONSTANT 156df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseModuleAsm() { 157df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Lex.getKind() == lltok::kw_module); 158df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 159df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1603ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner std::string AsmStr; 1613ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseToken(lltok::kw_asm, "expected 'module asm'") || 1623ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseStringConstant(AsmStr)) return true; 163df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 164df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const std::string &AsmSoFar = M->getModuleInlineAsm(); 165df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (AsmSoFar.empty()) 1663ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner M->setModuleInlineAsm(AsmStr); 167df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else 1683ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner M->setModuleInlineAsm(AsmSoFar+"\n"+AsmStr); 169df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 170df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 171df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 172df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// toplevelentity 173df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'target' 'triple' '=' STRINGCONSTANT 174df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'target' 'datalayout' '=' STRINGCONSTANT 175df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseTargetDefinition() { 176df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Lex.getKind() == lltok::kw_target); 1773ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner std::string Str; 178df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Lex.Lex()) { 179df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: return TokError("unknown target property"); 180df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_triple: 181df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1823ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseToken(lltok::equal, "expected '=' after target triple") || 1833ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseStringConstant(Str)) 184df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1853ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner M->setTargetTriple(Str); 186df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 187df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_datalayout: 188df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1893ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseToken(lltok::equal, "expected '=' after target datalayout") || 1903ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseStringConstant(Str)) 191df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1923ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner M->setDataLayout(Str); 193df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 194df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 195df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 196df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 197df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// toplevelentity 198df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'deplibs' '=' '[' ']' 199df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']' 200df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseDepLibs() { 201df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Lex.getKind() == lltok::kw_deplibs); 2023ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner Lex.Lex(); 2033ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseToken(lltok::equal, "expected '=' after deplibs") || 2043ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseToken(lltok::lsquare, "expected '=' after deplibs")) 2053ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return true; 206df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2073ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (EatIfPresent(lltok::rsquare)) 208df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 209df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2103ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner std::string Str; 2113ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseStringConstant(Str)) return true; 2123ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner M->addLibrary(Str); 213df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2143ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner while (EatIfPresent(lltok::comma)) { 2153ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseStringConstant(Str)) return true; 2163ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner M->addLibrary(Str); 217df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 218df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2193ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return ParseToken(lltok::rsquare, "expected ']' at end of list"); 220df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 221df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 222df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// toplevelentity 223df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'type' type 224df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseUnnamedType() { 225df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Lex.getKind() == lltok::kw_type); 226df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy TypeLoc = Lex.getLoc(); 227df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); // eat kw_type 228df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 229df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder Ty(Type::VoidTy); 230df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseType(Ty)) return true; 231df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 232df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned TypeID = NumberedTypes.size(); 233df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 234df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // We don't allow assigning names to void type 235df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Ty == Type::VoidTy) 236df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(TypeLoc, "can't assign name to the void type"); 237df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 238df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // See if this type was previously referenced. 239df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<unsigned, std::pair<PATypeHolder, LocTy> >::iterator 240df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FI = ForwardRefTypeIDs.find(TypeID); 241df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FI != ForwardRefTypeIDs.end()) { 242c38daba25b66f9a0386fc28861db66e62663310dChris Lattner if (FI->second.first.get() == Ty) 243c38daba25b66f9a0386fc28861db66e62663310dChris Lattner return Error(TypeLoc, "self referential type is invalid"); 244c38daba25b66f9a0386fc28861db66e62663310dChris Lattner 245df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner cast<DerivedType>(FI->second.first.get())->refineAbstractTypeTo(Ty); 246df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Ty = FI->second.first.get(); 247df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefTypeIDs.erase(FI); 248df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 249df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 250df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner NumberedTypes.push_back(Ty); 251df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 252df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 253df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 254df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 255df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// toplevelentity 256df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= LocalVar '=' 'type' type 257df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseNamedType() { 258df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::string Name = Lex.getStrVal(); 259df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy NameLoc = Lex.getLoc(); 2603ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner Lex.Lex(); // eat LocalVar. 261df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 262df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder Ty(Type::VoidTy); 2633ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner 2643ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseToken(lltok::equal, "expected '=' after name") || 2653ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseToken(lltok::kw_type, "expected 'type' after name") || 2663ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseType(Ty)) 2673ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return true; 268df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 269df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // We don't allow assigning names to void type 270df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Ty == Type::VoidTy) 271df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(NameLoc, "can't assign name '" + Name + "' to the void type"); 272df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 273df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Set the type name, checking for conflicts as we do so. 274df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner bool AlreadyExists = M->addTypeName(Name, Ty); 275df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!AlreadyExists) return false; 276df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 277df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // See if this type is a forward reference. We need to eagerly resolve 278df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // types to allow recursive type redefinitions below. 279df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<std::string, std::pair<PATypeHolder, LocTy> >::iterator 280df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FI = ForwardRefTypes.find(Name); 281df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FI != ForwardRefTypes.end()) { 282c38daba25b66f9a0386fc28861db66e62663310dChris Lattner if (FI->second.first.get() == Ty) 283c38daba25b66f9a0386fc28861db66e62663310dChris Lattner return Error(NameLoc, "self referential type is invalid"); 284c38daba25b66f9a0386fc28861db66e62663310dChris Lattner 285df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner cast<DerivedType>(FI->second.first.get())->refineAbstractTypeTo(Ty); 286df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Ty = FI->second.first.get(); 287df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefTypes.erase(FI); 288df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 289df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 290df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Inserting a name that is already defined, get the existing name. 291df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const Type *Existing = M->getTypeByName(Name); 292df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Existing && "Conflict but no matching type?!"); 293df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 294df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Otherwise, this is an attempt to redefine a type. That's okay if 295df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // the redefinition is identical to the original. 296df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: REMOVE REDEFINITIONS IN LLVM 3.0 297df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Existing == Ty) return false; 298df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 299df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Any other kind of (non-equivalent) redefinition is an error. 300df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(NameLoc, "redefinition of type named '" + Name + "' of type '" + 301df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Ty->getDescription() + "'"); 302df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 303df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 304df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 305df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// toplevelentity 306df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'declare' FunctionHeader 307df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseDeclare() { 308df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Lex.getKind() == lltok::kw_declare); 309df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 310df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 311df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Function *F; 312df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return ParseFunctionHeader(F, false); 313df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 314df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 315df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// toplevelentity 316df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'define' FunctionHeader '{' ... 317df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseDefine() { 318df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Lex.getKind() == lltok::kw_define); 319df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 320df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 321df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Function *F; 3223ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return ParseFunctionHeader(F, true) || 3233ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseFunctionBody(*F); 324df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 325df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3263ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ParseGlobalType 3273ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ::= 'constant' 3283ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ::= 'global' 329df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseGlobalType(bool &IsConstant) { 330df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() == lltok::kw_constant) 331df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner IsConstant = true; 332df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else if (Lex.getKind() == lltok::kw_global) 333df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner IsConstant = false; 334df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else 335df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("expected 'global' or 'constant'"); 336df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 337df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 338df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 339df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 340df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseNamedGlobal: 341df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// GlobalVar '=' OptionalVisibility ALIAS ... 342df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// GlobalVar '=' OptionalLinkage OptionalVisibility ... -> global variable 343df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseNamedGlobal() { 344df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Lex.getKind() == lltok::GlobalVar); 345df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy NameLoc = Lex.getLoc(); 346df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::string Name = Lex.getStrVal(); 347df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 348df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 349df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner bool HasLinkage; 350df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Linkage, Visibility; 351df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::equal, "expected '=' in global variable") || 352df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalLinkage(Linkage, HasLinkage) || 353df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalVisibility(Visibility)) 354df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 355df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 356df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (HasLinkage || Lex.getKind() != lltok::kw_alias) 357df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility); 358df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return ParseAlias(Name, NameLoc, Visibility); 359df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 360df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 361df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseAlias: 362df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= GlobalVar '=' OptionalVisibility 'alias' OptionalLinkage Aliasee 363df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// Aliasee 364df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= TypeAndValue | 'bitcast' '(' TypeAndValue 'to' Type ')' 365df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// 366df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// Everything through visibility has already been parsed. 367df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// 368df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc, 369df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Visibility) { 370df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Lex.getKind() == lltok::kw_alias); 371df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 372df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Linkage; 373df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy LinkageLoc = Lex.getLoc(); 374df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseOptionalLinkage(Linkage)) 375df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 376df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 377df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Linkage != GlobalValue::ExternalLinkage && 378df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Linkage != GlobalValue::WeakLinkage && 379bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola Linkage != GlobalValue::InternalLinkage && 380bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola Linkage != GlobalValue::PrivateLinkage) 381df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(LinkageLoc, "invalid linkage type for alias"); 382df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 383df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *Aliasee; 384df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy AliaseeLoc = Lex.getLoc(); 385df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() != lltok::kw_bitcast) { 386df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseGlobalTypeAndValue(Aliasee)) return true; 387df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 388df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // The bitcast dest type is not present, it is implied by the dest type. 389df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ValID ID; 390df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseValID(ID)) return true; 391df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ID.Kind != ValID::t_Constant) 392df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(AliaseeLoc, "invalid aliasee"); 393df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Aliasee = ID.ConstantVal; 394df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 395df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 396df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<PointerType>(Aliasee->getType())) 397df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(AliaseeLoc, "alias must have pointer type"); 398df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 399df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Okay, create the alias but do not insert it into the module yet. 400df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GlobalAlias* GA = new GlobalAlias(Aliasee->getType(), 401df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner (GlobalValue::LinkageTypes)Linkage, Name, 402df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Aliasee); 403df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GA->setVisibility((GlobalValue::VisibilityTypes)Visibility); 404df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 405df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // See if this value already exists in the symbol table. If so, it is either 406df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // a redefinition or a definition of a forward reference. 407df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (GlobalValue *Val = 408df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name))) { 409df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // See if this was a redefinition. If so, there is no entry in 410df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // ForwardRefVals. 411df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator 412df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I = ForwardRefVals.find(Name); 413df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I == ForwardRefVals.end()) 414df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(NameLoc, "redefinition of global named '@" + Name + "'"); 415df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 416df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Otherwise, this was a definition of forward ref. Verify that types 417df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // agree. 418df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val->getType() != GA->getType()) 419df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(NameLoc, 420df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "forward reference and definition of alias have different types"); 421df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 422df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If they agree, just RAUW the old value with the alias and remove the 423df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // forward ref info. 424df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Val->replaceAllUsesWith(GA); 425df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Val->eraseFromParent(); 426df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefVals.erase(I); 427df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 428df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 429df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Insert into the module, we know its name won't collide now. 430df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner M->getAliasList().push_back(GA); 431df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(GA->getNameStr() == Name && "Should not be a name conflict!"); 432df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 433df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 434df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 435df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 436df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseGlobal 437df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalThreadLocal 438df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// OptionalAddrSpace GlobalType Type Const 439df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= OptionalLinkage OptionalVisibility OptionalThreadLocal 440df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// OptionalAddrSpace GlobalType Type Const 441df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// 442df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// Everything through visibility has been parsed already. 443df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// 444df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc, 445df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Linkage, bool HasLinkage, 446df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Visibility) { 447df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned AddrSpace; 448df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner bool ThreadLocal, IsConstant; 449df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy TyLoc; 450df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 451df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder Ty(Type::VoidTy); 452df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseOptionalToken(lltok::kw_thread_local, ThreadLocal) || 453df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalAddrSpace(AddrSpace) || 454df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalType(IsConstant) || 455df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseType(Ty, TyLoc)) 456df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 457df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 458df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If the linkage is specified and is external, then no initializer is 459df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // present. 460df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *Init = 0; 461df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!HasLinkage || (Linkage != GlobalValue::DLLImportLinkage && 462df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Linkage != GlobalValue::ExternalWeakLinkage && 463df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Linkage != GlobalValue::ExternalLinkage)) { 464df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseGlobalValue(Ty, Init)) 465df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 466df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 467df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 468b4bd16fc5e3dd416cf68e73cff93544d364159d6Chris Lattner if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || Ty == Type::VoidTy) 469df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(TyLoc, "invald type for global variable"); 470df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 471df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GlobalVariable *GV = 0; 472df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 473df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // See if the global was forward referenced, if so, use the global. 47491dad87ddc1e69ac5fa8953a583de4185e1dd0d7Chris Lattner if (!Name.empty()) { 47591dad87ddc1e69ac5fa8953a583de4185e1dd0d7Chris Lattner if ((GV = M->getGlobalVariable(Name, true)) && 47691dad87ddc1e69ac5fa8953a583de4185e1dd0d7Chris Lattner !ForwardRefVals.erase(Name)) 477df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(NameLoc, "redefinition of global '@" + Name + "'"); 478df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 479df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator 480df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I = ForwardRefValIDs.find(NumberedVals.size()); 481df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I != ForwardRefValIDs.end()) { 482df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GV = cast<GlobalVariable>(I->second.first); 483df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefValIDs.erase(I); 484df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 485df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 486df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 487df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (GV == 0) { 488df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GV = new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage, 0, Name, 489df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner M, false, AddrSpace); 490df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 491df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (GV->getType()->getElementType() != Ty) 492df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(TyLoc, 493df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "forward reference and definition of global have different types"); 494df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 495df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Move the forward-reference to the correct spot in the module. 496df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV); 497df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 498df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 499df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Name.empty()) 500df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner NumberedVals.push_back(GV); 501df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 502df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Set the parsed properties on the global. 503df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Init) 504df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GV->setInitializer(Init); 505df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GV->setConstant(IsConstant); 506df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GV->setLinkage((GlobalValue::LinkageTypes)Linkage); 507df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GV->setVisibility((GlobalValue::VisibilityTypes)Visibility); 508df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GV->setThreadLocal(ThreadLocal); 509df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 510df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Parse attributes on the global. 511df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner while (Lex.getKind() == lltok::comma) { 512df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 513df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 514df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() == lltok::kw_section) { 515df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 516df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GV->setSection(Lex.getStrVal()); 517df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::StringConstant, "expected global section string")) 518df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 519df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (Lex.getKind() == lltok::kw_align) { 520df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Alignment; 521df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseOptionalAlignment(Alignment)) return true; 522df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GV->setAlignment(Alignment); 523df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 524df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner TokError("unknown global variable property!"); 525df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 526df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 527df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 528df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 529df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 530df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 531df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 532df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 533df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// GlobalValue Reference/Resolution Routines. 534df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 535df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 536df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// GetGlobalVal - Get a value with the specified name or ID, creating a 537df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// forward reference record if needed. This can return null if the value 538df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// exists but does not have the right type. 539df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerGlobalValue *LLParser::GetGlobalVal(const std::string &Name, const Type *Ty, 540df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc) { 541df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const PointerType *PTy = dyn_cast<PointerType>(Ty); 542df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (PTy == 0) { 543df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Error(Loc, "global variable reference must have pointer type"); 544df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return 0; 545df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 546df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 547df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Look this name up in the normal function symbol table. 548df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GlobalValue *Val = 549df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name)); 550df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 551df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If this is a forward reference for the value, see if we already created a 552df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // forward ref record. 553df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val == 0) { 554df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator 555df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I = ForwardRefVals.find(Name); 556df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I != ForwardRefVals.end()) 557df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Val = I->second.first; 558df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 559df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 560df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If we have the value in the symbol table or fwd-ref table, return it. 561df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val) { 562df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val->getType() == Ty) return Val; 563df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Error(Loc, "'@" + Name + "' defined with type '" + 564df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Val->getType()->getDescription() + "'"); 565df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return 0; 566df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 567df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 568df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Otherwise, create a new forward reference for this value and remember it. 569df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GlobalValue *FwdVal; 5701e407c338f6944242facb1e0c32903162cc366f0Chris Lattner if (const FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType())) { 5711e407c338f6944242facb1e0c32903162cc366f0Chris Lattner // Function types can return opaque but functions can't. 5721e407c338f6944242facb1e0c32903162cc366f0Chris Lattner if (isa<OpaqueType>(FT->getReturnType())) { 5731e407c338f6944242facb1e0c32903162cc366f0Chris Lattner Error(Loc, "function may not return opaque type"); 5741e407c338f6944242facb1e0c32903162cc366f0Chris Lattner return 0; 5751e407c338f6944242facb1e0c32903162cc366f0Chris Lattner } 5761e407c338f6944242facb1e0c32903162cc366f0Chris Lattner 577df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M); 5781e407c338f6944242facb1e0c32903162cc366f0Chris Lattner } else { 579df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FwdVal = new GlobalVariable(PTy->getElementType(), false, 580df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GlobalValue::ExternalWeakLinkage, 0, Name, M); 5811e407c338f6944242facb1e0c32903162cc366f0Chris Lattner } 582df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 583df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefVals[Name] = std::make_pair(FwdVal, Loc); 584df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return FwdVal; 585df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 586df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 587df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerGlobalValue *LLParser::GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc) { 588df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const PointerType *PTy = dyn_cast<PointerType>(Ty); 589df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (PTy == 0) { 590df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Error(Loc, "global variable reference must have pointer type"); 591df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return 0; 592df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 593df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 594df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0; 595df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 596df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If this is a forward reference for the value, see if we already created a 597df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // forward ref record. 598df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val == 0) { 599df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator 600df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I = ForwardRefValIDs.find(ID); 601df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I != ForwardRefValIDs.end()) 602df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Val = I->second.first; 603df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 604df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 605df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If we have the value in the symbol table or fwd-ref table, return it. 606df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val) { 607df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val->getType() == Ty) return Val; 608df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Error(Loc, "'@" + utostr(ID) + "' defined with type '" + 609df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Val->getType()->getDescription() + "'"); 610df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return 0; 611df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 612df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 613df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Otherwise, create a new forward reference for this value and remember it. 614df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GlobalValue *FwdVal; 615830703b1bd88511e29eec76b1fd146524bc9c5a1Chris Lattner if (const FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType())) { 616830703b1bd88511e29eec76b1fd146524bc9c5a1Chris Lattner // Function types can return opaque but functions can't. 617830703b1bd88511e29eec76b1fd146524bc9c5a1Chris Lattner if (isa<OpaqueType>(FT->getReturnType())) { 6180d8484f2edb1069522f5c3de6467433e0fbe9323Chris Lattner Error(Loc, "function may not return opaque type"); 619830703b1bd88511e29eec76b1fd146524bc9c5a1Chris Lattner return 0; 620830703b1bd88511e29eec76b1fd146524bc9c5a1Chris Lattner } 621df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M); 622830703b1bd88511e29eec76b1fd146524bc9c5a1Chris Lattner } else { 623df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FwdVal = new GlobalVariable(PTy->getElementType(), false, 624df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner GlobalValue::ExternalWeakLinkage, 0, "", M); 625830703b1bd88511e29eec76b1fd146524bc9c5a1Chris Lattner } 626df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 627df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc); 628df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return FwdVal; 629df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 630df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 631df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 632df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 633df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Helper Routines. 634df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 635df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 636df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseToken - If the current token has the specified kind, eat it and return 637df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// success. Otherwise, emit the specified error and return failure. 638df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) { 639df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() != T) 640df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError(ErrMsg); 641df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 642df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 643df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 644df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 6453ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ParseStringConstant 6463ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ::= StringConstant 6473ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattnerbool LLParser::ParseStringConstant(std::string &Result) { 6483ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (Lex.getKind() != lltok::StringConstant) 6493ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return TokError("expected string constant"); 6503ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner Result = Lex.getStrVal(); 6513ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner Lex.Lex(); 6523ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return false; 6533ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner} 6543ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner 6553ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ParseUInt32 6563ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ::= uint32 6573ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattnerbool LLParser::ParseUInt32(unsigned &Val) { 658df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned()) 659df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("expected integer"); 660df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1); 661df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val64 != unsigned(Val64)) 662df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("expected 32-bit integer (too large)"); 663df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Val = Val64; 664df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 665df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 666df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 667df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 668df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 669df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseOptionalAddrSpace 670df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// := /*empty*/ 671df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// := 'addrspace' '(' uint32 ')' 672df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) { 673df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner AddrSpace = 0; 6743ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (!EatIfPresent(lltok::kw_addrspace)) 675df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 676df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return ParseToken(lltok::lparen, "expected '(' in address space") || 6773ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseUInt32(AddrSpace) || 678df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rparen, "expected ')' in address space"); 679df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 680df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 681df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseOptionalAttrs - Parse a potentially empty attribute list. AttrKind 682df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// indicates what kind of attribute list this is: 0: function arg, 1: result, 683df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// 2: function attr. 684df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind) { 685df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs = Attribute::None; 686df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy AttrLoc = Lex.getLoc(); 687df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 688df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner while (1) { 689df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Lex.getKind()) { 690df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sext: 691df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_zext: 692df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Treat these as signext/zeroext unless they are function attrs. 693df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: REMOVE THIS IN LLVM 3.0 694df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (AttrKind != 2) { 695df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() == lltok::kw_sext) 696df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs |= Attribute::SExt; 697df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else 698df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs |= Attribute::ZExt; 699df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 700df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 701df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FALL THROUGH. 702df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: // End of attributes. 703df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (AttrKind != 2 && (Attrs & Attribute::FunctionOnly)) 704df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(AttrLoc, "invalid use of function-only attribute"); 705df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 706df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (AttrKind != 0 && (Attrs & Attribute::ParameterOnly)) 707df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(AttrLoc, "invalid use of parameter-only attribute"); 708df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 709df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 710df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_zeroext: Attrs |= Attribute::ZExt; break; 711df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_signext: Attrs |= Attribute::SExt; break; 712df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_inreg: Attrs |= Attribute::InReg; break; 713df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sret: Attrs |= Attribute::StructRet; break; 714df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_noalias: Attrs |= Attribute::NoAlias; break; 715df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_nocapture: Attrs |= Attribute::NoCapture; break; 716df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_byval: Attrs |= Attribute::ByVal; break; 717df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_nest: Attrs |= Attribute::Nest; break; 718df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 719df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_noreturn: Attrs |= Attribute::NoReturn; break; 720df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_nounwind: Attrs |= Attribute::NoUnwind; break; 721df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_noinline: Attrs |= Attribute::NoInline; break; 722df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_readnone: Attrs |= Attribute::ReadNone; break; 723df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_readonly: Attrs |= Attribute::ReadOnly; break; 724df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_alwaysinline: Attrs |= Attribute::AlwaysInline; break; 725df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_optsize: Attrs |= Attribute::OptimizeForSize; break; 726df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ssp: Attrs |= Attribute::StackProtect; break; 727df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sspreq: Attrs |= Attribute::StackProtectReq; break; 728df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 729df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 730df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_align: { 731df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Alignment; 732df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseOptionalAlignment(Alignment)) 733df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 734df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs |= Attribute::constructAlignmentFromInt(Alignment); 735df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner continue; 736df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 737df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 738df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 739df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 740df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 741df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 742df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseOptionalLinkage 743df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= /*empty*/ 744bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola/// ::= 'private' 745df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'internal' 746df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'weak' 747df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'linkonce' 748df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'appending' 749df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'dllexport' 750df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'common' 751df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'dllimport' 752df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'extern_weak' 753df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'external' 754df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) { 755df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner HasLinkage = false; 756df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Lex.getKind()) { 757df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: Res = GlobalValue::ExternalLinkage; return false; 758bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break; 759df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break; 760df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_weak: Res = GlobalValue::WeakLinkage; break; 761df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_linkonce: Res = GlobalValue::LinkOnceLinkage; break; 762df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break; 763df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_dllexport: Res = GlobalValue::DLLExportLinkage; break; 764df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_common: Res = GlobalValue::CommonLinkage; break; 765df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_dllimport: Res = GlobalValue::DLLImportLinkage; break; 766df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break; 767df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break; 768df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 769df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 770df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner HasLinkage = true; 771df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 772df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 773df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 774df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseOptionalVisibility 775df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= /*empty*/ 776df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'default' 777df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'hidden' 778df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'protected' 779df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// 780df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseOptionalVisibility(unsigned &Res) { 781df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Lex.getKind()) { 782df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: Res = GlobalValue::DefaultVisibility; return false; 783df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break; 784df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break; 785df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break; 786df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 787df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 788df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 789df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 790df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 791df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseOptionalCallingConv 792df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= /*empty*/ 793df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'ccc' 794df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'fastcc' 795df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'coldcc' 796df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'x86_stdcallcc' 797df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'x86_fastcallcc' 798df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'cc' UINT 799df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// 800df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseOptionalCallingConv(unsigned &CC) { 801df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Lex.getKind()) { 802df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: CC = CallingConv::C; return false; 803df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ccc: CC = CallingConv::C; break; 804df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_fastcc: CC = CallingConv::Fast; break; 805df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_coldcc: CC = CallingConv::Cold; break; 806df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break; 807df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break; 8083ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner case lltok::kw_cc: Lex.Lex(); return ParseUInt32(CC); 809df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 810df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 811df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 812df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 813df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 814df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseOptionalAlignment 815df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= /* empty */ 816df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'align' 4 817df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseOptionalAlignment(unsigned &Alignment) { 818df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Alignment = 0; 8193ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (!EatIfPresent(lltok::kw_align)) 8203ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return false; 8213fbb3ab14d86f752a933b5af4e6728c5e57e1155Chris Lattner LocTy AlignLoc = Lex.getLoc(); 8223fbb3ab14d86f752a933b5af4e6728c5e57e1155Chris Lattner if (ParseUInt32(Alignment)) return true; 8233fbb3ab14d86f752a933b5af4e6728c5e57e1155Chris Lattner if (!isPowerOf2_32(Alignment)) 8243fbb3ab14d86f752a933b5af4e6728c5e57e1155Chris Lattner return Error(AlignLoc, "alignment is not a power of two"); 8253fbb3ab14d86f752a933b5af4e6728c5e57e1155Chris Lattner return false; 826df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 827df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 828df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseOptionalCommaAlignment 829df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= /* empty */ 830df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= ',' 'align' 4 831df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseOptionalCommaAlignment(unsigned &Alignment) { 832df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Alignment = 0; 8333ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (!EatIfPresent(lltok::comma)) 834df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 835df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return ParseToken(lltok::kw_align, "expected 'align'") || 8363ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseUInt32(Alignment); 837df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 838df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 839df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseIndexList 840df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= (',' uint32)+ 841df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices) { 842df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() != lltok::comma) 843df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("expected ',' as start of index list"); 844df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 8453ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner while (EatIfPresent(lltok::comma)) { 846df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Idx; 8473ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseUInt32(Idx)) return true; 848df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Indices.push_back(Idx); 849df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 850df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 851df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 852df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 853df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 854df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 855df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Type Parsing. 856df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 857df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 858df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseType - Parse and resolve a full type. 859df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseType(PATypeHolder &Result) { 860df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeRec(Result)) return true; 861df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 862df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Verify no unresolved uprefs. 863df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!UpRefs.empty()) 864df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(UpRefs.back().Loc, "invalid unresolved type up reference"); 865df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 866df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 867df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 868df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 869df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// HandleUpRefs - Every time we finish a new layer of types, this function is 870df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// called. It loops through the UpRefs vector, which is a list of the 871df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// currently active types. For each type, if the up-reference is contained in 872df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// the newly completed type, we decrement the level count. When the level 873df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// count reaches zero, the up-referenced type is the type that is passed in: 874df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// thus we can complete the cycle. 875df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// 876df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerPATypeHolder LLParser::HandleUpRefs(const Type *ty) { 877df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If Ty isn't abstract, or if there are no up-references in it, then there is 878df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // nothing to resolve here. 879df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ty->isAbstract() || UpRefs.empty()) return ty; 880df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 881df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder Ty(ty); 882df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#if 0 883df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner errs() << "Type '" << Ty->getDescription() 884df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner << "' newly formed. Resolving upreferences.\n" 885df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner << UpRefs.size() << " upreferences active!\n"; 886df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#endif 887df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 888df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If we find any resolvable upreferences (i.e., those whose NestingLevel goes 889df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // to zero), we resolve them all together before we resolve them to Ty. At 890df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // the end of the loop, if there is anything to resolve to Ty, it will be in 891df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // this variable. 892df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner OpaqueType *TypeToResolve = 0; 893df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 894df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0; i != UpRefs.size(); ++i) { 895df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Determine if 'Ty' directly contains this up-references 'LastContainedTy'. 896df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner bool ContainsType = 897df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::find(Ty->subtype_begin(), Ty->subtype_end(), 898df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner UpRefs[i].LastContainedTy) != Ty->subtype_end(); 899df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 900df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#if 0 901df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner errs() << " UR#" << i << " - TypeContains(" << Ty->getDescription() << ", " 902df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner << UpRefs[i].LastContainedTy->getDescription() << ") = " 903df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner << (ContainsType ? "true" : "false") 904df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner << " level=" << UpRefs[i].NestingLevel << "\n"; 905df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#endif 906df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ContainsType) 907df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner continue; 908df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 909df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Decrement level of upreference 910df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Level = --UpRefs[i].NestingLevel; 911df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner UpRefs[i].LastContainedTy = Ty; 912df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 913df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If the Up-reference has a non-zero level, it shouldn't be resolved yet. 914df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Level != 0) 915df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner continue; 916df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 917df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#if 0 918df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner errs() << " * Resolving upreference for " << UpRefs[i].UpRefTy << "\n"; 919df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#endif 920df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!TypeToResolve) 921df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner TypeToResolve = UpRefs[i].UpRefTy; 922df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else 923df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve); 924df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list. 925df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner --i; // Do not skip the next element. 926df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 927df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 928df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (TypeToResolve) 929df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner TypeToResolve->refineAbstractTypeTo(Ty); 930df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 931df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Ty; 932df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 933df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 934df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 935df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseTypeRec - The recursive function used to process the internal 936df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// implementation details of types. 937df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseTypeRec(PATypeHolder &Result) { 938df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Lex.getKind()) { 939df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: 940df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("expected type"); 941df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::Type: 942df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // TypeRec ::= 'float' | 'void' (etc) 943df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Result = Lex.getTyVal(); 944df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 945df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 946df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_opaque: 947df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // TypeRec ::= 'opaque' 948df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Result = OpaqueType::get(); 949df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 950df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 951df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::lbrace: 952df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // TypeRec ::= '{' ... '}' 953df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseStructType(Result, false)) 954df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 955df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 956df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::lsquare: 957df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // TypeRec ::= '[' ... ']' 958df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); // eat the lsquare. 959df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseArrayVectorType(Result, false)) 960df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 961df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 962df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::less: // Either vector or packed struct. 963df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // TypeRec ::= '<' ... '>' 9643ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner Lex.Lex(); 9653ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (Lex.getKind() == lltok::lbrace) { 9663ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseStructType(Result, true) || 9673ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseToken(lltok::greater, "expected '>' at end of packed struct")) 968df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 969df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (ParseArrayVectorType(Result, true)) 970df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 971df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 972df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::LocalVar: 973df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::StringConstant: // FIXME: REMOVE IN LLVM 3.0 974df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // TypeRec ::= %foo 975df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (const Type *T = M->getTypeByName(Lex.getStrVal())) { 976df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Result = T; 977df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 978df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Result = OpaqueType::get(); 979df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefTypes.insert(std::make_pair(Lex.getStrVal(), 980df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::make_pair(Result, 981df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.getLoc()))); 982df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner M->addTypeName(Lex.getStrVal(), Result.get()); 983df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 984df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 985df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 986df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 987df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::LocalVarID: 988df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // TypeRec ::= %4 989df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getUIntVal() < NumberedTypes.size()) 990df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Result = NumberedTypes[Lex.getUIntVal()]; 991df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else { 992df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<unsigned, std::pair<PATypeHolder, LocTy> >::iterator 993df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I = ForwardRefTypeIDs.find(Lex.getUIntVal()); 994df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I != ForwardRefTypeIDs.end()) 995df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Result = I->second.first; 996df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else { 997df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Result = OpaqueType::get(); 998df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefTypeIDs.insert(std::make_pair(Lex.getUIntVal(), 999df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::make_pair(Result, 1000df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.getLoc()))); 1001df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1002df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1003df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1004df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1005df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::backslash: { 1006df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // TypeRec ::= '\' 4 1007df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 10083ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner unsigned Val; 10093ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseUInt32(Val)) return true; 1010df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder. 1011df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner UpRefs.push_back(UpRefRecord(Lex.getLoc(), Val, OT)); 1012df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Result = OT; 1013df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1014df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1015df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1016df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1017df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Parse the type suffixes. 1018df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner while (1) { 1019df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Lex.getKind()) { 1020df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // End of type. 1021df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: return false; 1022df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1023df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // TypeRec ::= TypeRec '*' 1024df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::star: 1025df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Result.get() == Type::LabelTy) 1026df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("basic block pointers are invalid"); 1027b4bd16fc5e3dd416cf68e73cff93544d364159d6Chris Lattner if (Result.get() == Type::VoidTy) 1028b4bd16fc5e3dd416cf68e73cff93544d364159d6Chris Lattner return TokError("pointers to void are invalid, use i8* instead"); 1029df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Result = HandleUpRefs(PointerType::getUnqual(Result.get())); 1030df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1031df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1032df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1033df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // TypeRec ::= TypeRec 'addrspace' '(' uint32 ')' '*' 1034df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_addrspace: { 1035df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Result.get() == Type::LabelTy) 1036df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("basic block pointers are invalid"); 1037b4bd16fc5e3dd416cf68e73cff93544d364159d6Chris Lattner if (Result.get() == Type::VoidTy) 1038b4bd16fc5e3dd416cf68e73cff93544d364159d6Chris Lattner return TokError("pointers to void are invalid, use i8* instead"); 1039df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned AddrSpace; 1040df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseOptionalAddrSpace(AddrSpace) || 1041df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::star, "expected '*' in address space")) 1042df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1043df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1044df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Result = HandleUpRefs(PointerType::get(Result.get(), AddrSpace)); 1045df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1046df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1047df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1048df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner /// Types '(' ArgTypeListI ')' OptFuncAttrs 1049df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::lparen: 1050df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseFunctionType(Result)) 1051df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1052df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1053df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1054df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1055df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1056df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1057df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseParameterList 1058df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= '(' ')' 1059df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= '(' Arg (',' Arg)* ')' 1060df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// Arg 1061df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= Type OptionalAttributes Value OptionalAttributes 1062df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList, 1063df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PerFunctionState &PFS) { 1064df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::lparen, "expected '(' in call")) 1065df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1066df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1067df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner while (Lex.getKind() != lltok::rparen) { 1068df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If this isn't the first argument, we need a comma. 1069df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ArgList.empty() && 1070df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' in argument list")) 1071df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1072df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1073df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Parse the argument. 1074df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy ArgLoc; 1075df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder ArgTy(Type::VoidTy); 1076df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned ArgAttrs1, ArgAttrs2; 1077df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *V; 1078df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseType(ArgTy, ArgLoc) || 1079df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalAttrs(ArgAttrs1, 0) || 1080df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseValue(ArgTy, V, PFS) || 1081df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: Should not allow attributes after the argument, remove this in 1082df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // LLVM 3.0. 1083df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalAttrs(ArgAttrs2, 0)) 1084df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1085df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ArgList.push_back(ParamInfo(ArgLoc, V, ArgAttrs1|ArgAttrs2)); 1086df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1087df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1088df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); // Lex the ')'. 1089df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1090df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1091df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1092df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1093df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1094dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner/// ParseArgumentList - Parse the argument list for a function type or function 1095dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner/// prototype. If 'inType' is true then we are parsing a FunctionType. 1096df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= '(' ArgTypeListI ')' 1097df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ArgTypeListI 1098df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= /*empty*/ 1099df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= '...' 1100df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= ArgTypeList ',' '...' 1101df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= ArgType (',' ArgType)* 1102dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner/// 1103df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseArgumentList(std::vector<ArgInfo> &ArgList, 1104dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner bool &isVarArg, bool inType) { 1105df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner isVarArg = false; 1106df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Lex.getKind() == lltok::lparen); 1107df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); // eat the (. 1108df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1109df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() == lltok::rparen) { 1110df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // empty 1111df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (Lex.getKind() == lltok::dotdotdot) { 1112df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner isVarArg = true; 1113df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1114df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 1115df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy TypeLoc = Lex.getLoc(); 1116df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder ArgTy(Type::VoidTy); 1117df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Attrs; 1118df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::string Name; 11193ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner 1120dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner // If we're parsing a type, use ParseTypeRec, because we allow recursive 1121dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner // types (such as a function returning a pointer to itself). If parsing a 1122dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner // function prototype, we require fully resolved types. 1123dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner if ((inType ? ParseTypeRec(ArgTy) : ParseType(ArgTy)) || 11243ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseOptionalAttrs(Attrs, 0)) return true; 11253ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner 1126df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() == lltok::LocalVar || 1127df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.getKind() == lltok::StringConstant) { // FIXME: REMOVE IN LLVM 3.0 1128df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Name = Lex.getStrVal(); 1129df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1130df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 11313ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner 11323ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (!ArgTy->isFirstClassType() && !isa<OpaqueType>(ArgTy)) 11333ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return Error(TypeLoc, "invalid type for function argument"); 1134df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1135df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ArgList.push_back(ArgInfo(TypeLoc, ArgTy, Attrs, Name)); 1136df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 11373ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner while (EatIfPresent(lltok::comma)) { 1138df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Handle ... at end of arg list. 11393ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (EatIfPresent(lltok::dotdotdot)) { 1140df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner isVarArg = true; 1141df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1142df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1143df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1144df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Otherwise must be an argument type. 1145df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner TypeLoc = Lex.getLoc(); 11463ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseTypeRec(ArgTy) || 11473ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseOptionalAttrs(Attrs, 0)) return true; 11483ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner 1149df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() == lltok::LocalVar || 1150df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.getKind() == lltok::StringConstant) { // FIXME: REMOVE IN LLVM 3.0 1151df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Name = Lex.getStrVal(); 1152df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1153df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 1154df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Name = ""; 1155df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 11563ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner 11573ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (!ArgTy->isFirstClassType() && !isa<OpaqueType>(ArgTy)) 11583ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return Error(TypeLoc, "invalid type for function argument"); 1159df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1160df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ArgList.push_back(ArgInfo(TypeLoc, ArgTy, Attrs, Name)); 1161df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1162df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1163df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 11643ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return ParseToken(lltok::rparen, "expected ')' at end of argument list"); 1165df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1166df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1167df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseFunctionType 1168df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= Type ArgumentList OptionalAttrs 1169df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseFunctionType(PATypeHolder &Result) { 1170df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Lex.getKind() == lltok::lparen); 1171df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1172d77d04c323f856b07c6e46450e7578dd09c0165fChris Lattner if (!FunctionType::isValidReturnType(Result)) 1173d77d04c323f856b07c6e46450e7578dd09c0165fChris Lattner return TokError("invalid function return type"); 1174d77d04c323f856b07c6e46450e7578dd09c0165fChris Lattner 1175df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::vector<ArgInfo> ArgList; 1176df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner bool isVarArg; 1177df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Attrs; 1178dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner if (ParseArgumentList(ArgList, isVarArg, true) || 1179df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: Allow, but ignore attributes on function types! 1180df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: Remove in LLVM 3.0 1181df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalAttrs(Attrs, 2)) 1182df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1183df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1184df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Reject names on the arguments lists. 1185df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { 1186df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ArgList[i].Name.empty()) 1187df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ArgList[i].Loc, "argument name invalid in function type"); 1188df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ArgList[i].Attrs != 0) { 1189df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Allow but ignore attributes on function types; this permits 1190df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // auto-upgrade. 1191df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: REJECT ATTRIBUTES ON FUNCTION TYPES in LLVM 3.0 1192df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1193df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1194df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1195df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::vector<const Type*> ArgListTy; 1196df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = ArgList.size(); i != e; ++i) 1197df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ArgListTy.push_back(ArgList[i].Type); 1198df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1199df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Result = HandleUpRefs(FunctionType::get(Result.get(), ArgListTy, isVarArg)); 1200df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1201df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1202df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1203df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere. 1204df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// TypeRec 1205df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= '{' '}' 1206df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= '{' TypeRec (',' TypeRec)* '}' 1207df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= '<' '{' '}' '>' 1208df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= '<' '{' TypeRec (',' TypeRec)* '}' '>' 1209df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseStructType(PATypeHolder &Result, bool Packed) { 1210df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Lex.getKind() == lltok::lbrace); 1211df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); // Consume the '{' 1212df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 12133ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (EatIfPresent(lltok::rbrace)) { 1214df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Result = StructType::get(std::vector<const Type*>(), Packed); 1215df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1216df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1217df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1218df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::vector<PATypeHolder> ParamsList; 1219df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeRec(Result)) return true; 1220df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParamsList.push_back(Result); 1221df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 12223ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner while (EatIfPresent(lltok::comma)) { 1223df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeRec(Result)) return true; 1224df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParamsList.push_back(Result); 1225df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1226df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 12273ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseToken(lltok::rbrace, "expected '}' at end of struct")) 12283ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return true; 1229df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1230df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::vector<const Type*> ParamsListTy; 1231df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = ParamsList.size(); i != e; ++i) 1232df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParamsListTy.push_back(ParamsList[i].get()); 1233df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Result = HandleUpRefs(StructType::get(ParamsListTy, Packed)); 1234df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1235df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1236df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1237df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseArrayVectorType - Parse an array or vector type, assuming the first 1238df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// token has already been consumed. 1239df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// TypeRec 1240df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= '[' APSINTVAL 'x' Types ']' 1241df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= '<' APSINTVAL 'x' Types '>' 1242df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseArrayVectorType(PATypeHolder &Result, bool isVector) { 1243df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() || 1244df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.getAPSIntVal().getBitWidth() > 64) 1245df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("expected number in address space"); 1246df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1247df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy SizeLoc = Lex.getLoc(); 1248df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner uint64_t Size = Lex.getAPSIntVal().getZExtValue(); 12493ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner Lex.Lex(); 12503ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner 12513ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseToken(lltok::kw_x, "expected 'x' after element count")) 12523ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return true; 1253df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1254df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy TypeLoc = Lex.getLoc(); 1255df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder EltTy(Type::VoidTy); 1256df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeRec(EltTy)) return true; 1257df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 12583ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseToken(isVector ? lltok::greater : lltok::rsquare, 12593ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner "expected end of sequential type")) 12603ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return true; 1261df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1262df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (isVector) { 1263df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if ((unsigned)Size != Size) 1264df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(SizeLoc, "size too large for vector"); 1265df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!EltTy->isFloatingPoint() && !EltTy->isInteger()) 1266df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(TypeLoc, "vector element type must be fp or integer"); 1267df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Result = VectorType::get(EltTy, unsigned(Size)); 1268df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 1269df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!EltTy->isFirstClassType() && !isa<OpaqueType>(EltTy)) 1270df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(TypeLoc, "invalid array element type"); 1271df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Result = HandleUpRefs(ArrayType::get(EltTy, Size)); 1272df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1273df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1274df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1275df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1276df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 1277df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Function Semantic Analysis. 1278df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 1279df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1280df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerLLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f) 1281df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner : P(p), F(f) { 1282df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1283df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Insert unnamed arguments into the NumberedVals list. 1284df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); 1285df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner AI != E; ++AI) 1286df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!AI->hasName()) 1287df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner NumberedVals.push_back(AI); 1288df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1289df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1290df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerLLParser::PerFunctionState::~PerFunctionState() { 1291df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If there were any forward referenced non-basicblock values, delete them. 1292df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (std::map<std::string, std::pair<Value*, LocTy> >::iterator 1293df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I) 1294df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<BasicBlock>(I->second.first)) { 1295df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I->second.first->replaceAllUsesWith(UndefValue::get(I->second.first 1296df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ->getType())); 1297df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner delete I->second.first; 1298df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I->second.first = 0; 1299df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1300df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1301df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator 1302df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I) 1303df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<BasicBlock>(I->second.first)) { 1304df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I->second.first->replaceAllUsesWith(UndefValue::get(I->second.first 1305df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ->getType())); 1306df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner delete I->second.first; 1307df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I->second.first = 0; 1308df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1309df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1310df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1311df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::PerFunctionState::VerifyFunctionComplete() { 1312df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ForwardRefVals.empty()) 1313df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return P.Error(ForwardRefVals.begin()->second.second, 1314df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "use of undefined value '%" + ForwardRefVals.begin()->first + 1315df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "'"); 1316df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ForwardRefValIDs.empty()) 1317df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return P.Error(ForwardRefValIDs.begin()->second.second, 1318df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "use of undefined value '%" + 1319df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner utostr(ForwardRefValIDs.begin()->first) + "'"); 1320df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1321df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1322df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1323df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1324df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// GetVal - Get a value with the specified name or ID, creating a 1325df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// forward reference record if needed. This can return null if the value 1326df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// exists but does not have the right type. 1327df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerValue *LLParser::PerFunctionState::GetVal(const std::string &Name, 1328df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const Type *Ty, LocTy Loc) { 1329df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Look this name up in the normal function symbol table. 1330df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Val = F.getValueSymbolTable().lookup(Name); 1331df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1332df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If this is a forward reference for the value, see if we already created a 1333df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // forward ref record. 1334df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val == 0) { 1335df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<std::string, std::pair<Value*, LocTy> >::iterator 1336df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I = ForwardRefVals.find(Name); 1337df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I != ForwardRefVals.end()) 1338df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Val = I->second.first; 1339df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1340df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1341df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If we have the value in the symbol table or fwd-ref table, return it. 1342df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val) { 1343df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val->getType() == Ty) return Val; 1344df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Ty == Type::LabelTy) 1345df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner P.Error(Loc, "'%" + Name + "' is not a basic block"); 1346df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else 1347df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner P.Error(Loc, "'%" + Name + "' defined with type '" + 1348df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Val->getType()->getDescription() + "'"); 1349df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return 0; 1350df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1351df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1352df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Don't make placeholders with invalid type. 1353df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty) && Ty != Type::LabelTy) { 1354df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner P.Error(Loc, "invalid use of a non-first-class type"); 1355df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return 0; 1356df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1357df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1358df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Otherwise, create a new forward reference for this value and remember it. 1359df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *FwdVal; 1360df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Ty == Type::LabelTy) 1361df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FwdVal = BasicBlock::Create(Name, &F); 1362df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else 1363df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FwdVal = new Argument(Ty, Name); 1364df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1365df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefVals[Name] = std::make_pair(FwdVal, Loc); 1366df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return FwdVal; 1367df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1368df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1369df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerValue *LLParser::PerFunctionState::GetVal(unsigned ID, const Type *Ty, 1370df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc) { 1371df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Look this name up in the normal function symbol table. 1372df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0; 1373df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1374df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If this is a forward reference for the value, see if we already created a 1375df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // forward ref record. 1376df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val == 0) { 1377df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<unsigned, std::pair<Value*, LocTy> >::iterator 1378df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner I = ForwardRefValIDs.find(ID); 1379df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I != ForwardRefValIDs.end()) 1380df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Val = I->second.first; 1381df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1382df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1383df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If we have the value in the symbol table or fwd-ref table, return it. 1384df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val) { 1385df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val->getType() == Ty) return Val; 1386df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Ty == Type::LabelTy) 1387df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner P.Error(Loc, "'%" + utostr(ID) + "' is not a basic block"); 1388df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else 1389df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner P.Error(Loc, "'%" + utostr(ID) + "' defined with type '" + 1390df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Val->getType()->getDescription() + "'"); 1391df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return 0; 1392df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1393df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1394df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty) && Ty != Type::LabelTy) { 1395df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner P.Error(Loc, "invalid use of a non-first-class type"); 1396df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return 0; 1397df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1398df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1399df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Otherwise, create a new forward reference for this value and remember it. 1400df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *FwdVal; 1401df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Ty == Type::LabelTy) 1402df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FwdVal = BasicBlock::Create("", &F); 1403df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else 1404df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FwdVal = new Argument(Ty); 1405df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1406df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc); 1407df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return FwdVal; 1408df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1409df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1410df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// SetInstName - After an instruction is parsed and inserted into its 1411df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// basic block, this installs its name. 1412df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::PerFunctionState::SetInstName(int NameID, 1413df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const std::string &NameStr, 1414df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy NameLoc, Instruction *Inst) { 1415df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If this instruction has void type, it cannot have a name or ID specified. 1416df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Inst->getType() == Type::VoidTy) { 1417df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (NameID != -1 || !NameStr.empty()) 1418df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return P.Error(NameLoc, "instructions returning void cannot have a name"); 1419df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1420df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1421df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1422df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If this was a numbered instruction, verify that the instruction is the 1423df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // expected value and resolve any forward references. 1424df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (NameStr.empty()) { 1425df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If neither a name nor an ID was specified, just use the next ID. 1426df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (NameID == -1) 1427df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner NameID = NumberedVals.size(); 1428df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1429df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (unsigned(NameID) != NumberedVals.size()) 1430df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return P.Error(NameLoc, "instruction expected to be numbered '%" + 1431df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner utostr(NumberedVals.size()) + "'"); 1432df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1433df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI = 1434df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefValIDs.find(NameID); 1435df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FI != ForwardRefValIDs.end()) { 1436df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FI->second.first->getType() != Inst->getType()) 1437df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return P.Error(NameLoc, "instruction forward referenced with type '" + 1438df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FI->second.first->getType()->getDescription() + "'"); 1439df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FI->second.first->replaceAllUsesWith(Inst); 1440df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefValIDs.erase(FI); 1441df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1442df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1443df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner NumberedVals.push_back(Inst); 1444df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1445df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1446df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1447df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Otherwise, the instruction had a name. Resolve forward refs and set it. 1448df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<std::string, std::pair<Value*, LocTy> >::iterator 1449df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FI = ForwardRefVals.find(NameStr); 1450df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FI != ForwardRefVals.end()) { 1451df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FI->second.first->getType() != Inst->getType()) 1452df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return P.Error(NameLoc, "instruction forward referenced with type '" + 1453df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FI->second.first->getType()->getDescription() + "'"); 1454df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FI->second.first->replaceAllUsesWith(Inst); 1455df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefVals.erase(FI); 1456df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1457df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1458df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Set the name on the instruction. 1459df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst->setName(NameStr); 1460df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1461df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Inst->getNameStr() != NameStr) 1462df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return P.Error(NameLoc, "multiple definition of local value named '" + 1463df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner NameStr + "'"); 1464df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1465df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1466df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1467df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// GetBB - Get a basic block with the specified name or ID, creating a 1468df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// forward reference record if needed. 1469df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerBasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name, 1470df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc) { 1471df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return cast_or_null<BasicBlock>(GetVal(Name, Type::LabelTy, Loc)); 1472df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1473df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1474df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerBasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) { 1475df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return cast_or_null<BasicBlock>(GetVal(ID, Type::LabelTy, Loc)); 1476df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1477df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1478df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// DefineBB - Define the specified basic block, which is either named or 1479df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// unnamed. If there is an error, this returns null otherwise it returns 1480df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// the block being defined. 1481df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerBasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name, 1482df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc) { 1483df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner BasicBlock *BB; 1484df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Name.empty()) 1485df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner BB = GetBB(NumberedVals.size(), Loc); 1486df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else 1487df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner BB = GetBB(Name, Loc); 1488df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (BB == 0) return 0; // Already diagnosed error. 1489df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1490df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Move the block to the end of the function. Forward ref'd blocks are 1491df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // inserted wherever they happen to be referenced. 1492df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB); 1493df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1494df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Remove the block from forward ref sets. 1495df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Name.empty()) { 1496df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefValIDs.erase(NumberedVals.size()); 1497df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner NumberedVals.push_back(BB); 1498df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 1499df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // BB forward references are already in the function symbol table. 1500df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefVals.erase(Name); 1501df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1502df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1503df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return BB; 1504df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1505df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1506df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 1507df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Constants. 1508df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 1509df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1510df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseValID - Parse an abstract value that doesn't necessarily have a 1511df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// type implied. For example, if we parse "4" we don't know what integer type 1512df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// it has. The value will later be combined with its type and checked for 1513df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// sanity. 1514df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseValID(ValID &ID) { 1515df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Loc = Lex.getLoc(); 1516df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Lex.getKind()) { 1517df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: return TokError("expected value token"); 1518df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::GlobalID: // @42 1519df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.UIntVal = Lex.getUIntVal(); 1520df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_GlobalID; 1521df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1522df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::GlobalVar: // @foo 1523df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.StrVal = Lex.getStrVal(); 1524df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_GlobalName; 1525df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1526df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::LocalVarID: // %42 1527df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.UIntVal = Lex.getUIntVal(); 1528df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_LocalID; 1529df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1530df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::LocalVar: // %foo 1531df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::StringConstant: // "foo" - FIXME: REMOVE IN LLVM 3.0 1532df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.StrVal = Lex.getStrVal(); 1533df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_LocalName; 1534df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1535df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::APSInt: 1536df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.APSIntVal = Lex.getAPSIntVal(); 1537df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_APSInt; 1538df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1539df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::APFloat: 1540df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.APFloatVal = Lex.getAPFloatVal(); 1541df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_APFloat; 1542df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1543df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_true: 1544df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.ConstantVal = ConstantInt::getTrue(); 1545df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1546df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1547df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_false: 1548df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.ConstantVal = ConstantInt::getFalse(); 1549df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1550df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 1551df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_null: ID.Kind = ValID::t_Null; break; 1552df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_undef: ID.Kind = ValID::t_Undef; break; 1553df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break; 1554df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1555df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::lbrace: { 1556df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // ValID ::= '{' ConstVector '}' 1557df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1558df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<Constant*, 16> Elts; 1559df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseGlobalValueVector(Elts) || 1560df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rbrace, "expected end of struct constant")) 1561df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1562df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1563df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.ConstantVal = ConstantStruct::get(&Elts[0], Elts.size(), false); 1564df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1565df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1566df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1567df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::less: { 1568df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // ValID ::= '<' ConstVector '>' --> Vector. 1569df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct. 1570df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 15713ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner bool isPackedStruct = EatIfPresent(lltok::lbrace); 1572df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1573df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<Constant*, 16> Elts; 1574df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy FirstEltLoc = Lex.getLoc(); 1575df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseGlobalValueVector(Elts) || 1576df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner (isPackedStruct && 1577df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rbrace, "expected end of packed struct")) || 1578df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::greater, "expected end of constant")) 1579df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1580df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1581df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (isPackedStruct) { 1582df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.ConstantVal = ConstantStruct::get(&Elts[0], Elts.size(), true); 1583df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1584df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1585df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1586df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1587df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Elts.empty()) 1588df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "constant vector must not be empty"); 1589df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1590df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!Elts[0]->getType()->isInteger() && 1591df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner !Elts[0]->getType()->isFloatingPoint()) 1592df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(FirstEltLoc, 1593df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "vector elements must have integer or floating point type"); 1594df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1595df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Verify that all the vector elements have the same type. 1596df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 1, e = Elts.size(); i != e; ++i) 1597df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Elts[i]->getType() != Elts[0]->getType()) 1598df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(FirstEltLoc, 1599df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "vector element #" + utostr(i) + 1600df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner " is not of type '" + Elts[0]->getType()->getDescription()); 1601df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1602df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.ConstantVal = ConstantVector::get(&Elts[0], Elts.size()); 1603df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1604df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1605df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1606df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::lsquare: { // Array Constant 1607df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1608df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<Constant*, 16> Elts; 1609df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy FirstEltLoc = Lex.getLoc(); 1610df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseGlobalValueVector(Elts) || 1611df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rsquare, "expected end of array constant")) 1612df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1613df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1614df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Handle empty element. 1615df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Elts.empty()) { 1616df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Use undef instead of an array because it's inconvenient to determine 1617df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // the element type at this point, there being no elements to examine. 1618081b505f77b183b7b029818218245cf4986e8948Chris Lattner ID.Kind = ValID::t_EmptyArray; 1619df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1620df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1621df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1622df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!Elts[0]->getType()->isFirstClassType()) 1623df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(FirstEltLoc, "invalid array element type: " + 1624df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Elts[0]->getType()->getDescription()); 1625df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1626df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size()); 1627df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1628df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Verify all elements are correct type! 16296d6b3cc89b9d1911cff4931dadfe670f94fcbb3bChris Lattner for (unsigned i = 0, e = Elts.size(); i != e; ++i) { 1630df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Elts[i]->getType() != Elts[0]->getType()) 1631df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(FirstEltLoc, 1632df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "array element #" + utostr(i) + 1633df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner " is not of type '" +Elts[0]->getType()->getDescription()); 1634df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1635df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1636df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.ConstantVal = ConstantArray::get(ATy, &Elts[0], Elts.size()); 1637df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1638df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1639df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1640df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_c: // c "foo" 1641df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1642df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.ConstantVal = ConstantArray::get(Lex.getStrVal(), false); 1643df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::StringConstant, "expected string")) return true; 1644df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1645df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1646df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1647df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_asm: { 1648df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // ValID ::= 'asm' SideEffect? STRINGCONSTANT ',' STRINGCONSTANT 1649df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner bool HasSideEffect; 1650df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1651df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) || 16523ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseStringConstant(ID.StrVal) || 16533ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseToken(lltok::comma, "expected comma in inline asm expression") || 1654df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::StringConstant, "expected constraint string")) 1655df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1656df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.StrVal2 = Lex.getStrVal(); 1657df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.UIntVal = HasSideEffect; 1658df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_InlineAsm; 1659df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1660df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1661df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1662df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_trunc: 1663df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_zext: 1664df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sext: 1665df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_fptrunc: 1666df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_fpext: 1667df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_bitcast: 1668df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_uitofp: 1669df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sitofp: 1670df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_fptoui: 1671df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_fptosi: 1672df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_inttoptr: 1673df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ptrtoint: { 1674df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Opc = Lex.getUIntVal(); 1675df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder DestTy(Type::VoidTy); 1676df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *SrcVal; 1677df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1678df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") || 1679df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalTypeAndValue(SrcVal) || 1680df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::kw_to, "expected 'to' int constantexpr cast") || 1681df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseType(DestTy) || 1682df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast")) 1683df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1684df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy)) 1685df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "invalid cast opcode for cast from '" + 1686df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SrcVal->getType()->getDescription() + "' to '" + 1687df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner DestTy->getDescription() + "'"); 1688df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc, SrcVal, 1689df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner DestTy); 1690df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1691df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1692df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1693df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_extractvalue: { 1694df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1695df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *Val; 1696df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<unsigned, 4> Indices; 1697df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")|| 1698df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalTypeAndValue(Val) || 1699df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseIndexList(Indices) || 1700df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr")) 1701df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1702df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<StructType>(Val->getType()) && !isa<ArrayType>(Val->getType())) 1703df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "extractvalue operand must be array or struct"); 1704df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(), 1705df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Indices.end())) 1706df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "invalid indices for extractvalue"); 1707df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.ConstantVal = ConstantExpr::getExtractValue(Val, 1708df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner &Indices[0], Indices.size()); 1709df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1710df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1711df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1712df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_insertvalue: { 1713df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1714df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *Val0, *Val1; 1715df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<unsigned, 4> Indices; 1716df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")|| 1717df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalTypeAndValue(Val0) || 1718df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")|| 1719df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalTypeAndValue(Val1) || 1720df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseIndexList(Indices) || 1721df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr")) 1722df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1723df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<StructType>(Val0->getType()) && !isa<ArrayType>(Val0->getType())) 1724df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "extractvalue operand must be array or struct"); 1725df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(), 1726df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Indices.end())) 1727df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "invalid indices for insertvalue"); 1728df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, 1729df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner &Indices[0], Indices.size()); 1730df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1731df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1732df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1733df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_icmp: 1734df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_fcmp: 1735df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_vicmp: 1736df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_vfcmp: { 1737df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned PredVal, Opc = Lex.getUIntVal(); 1738df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *Val0, *Val1; 1739df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1740df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseCmpPredicate(PredVal, Opc) || 1741df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::lparen, "expected '(' in compare constantexpr") || 1742df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalTypeAndValue(Val0) || 1743df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected comma in compare constantexpr") || 1744df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalTypeAndValue(Val1) || 1745df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rparen, "expected ')' in compare constantexpr")) 1746df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1747df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1748df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val0->getType() != Val1->getType()) 1749df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "compare operands must have the same type"); 1750df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1751df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal; 1752df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1753df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Opc == Instruction::FCmp) { 1754df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!Val0->getType()->isFPOrFPVector()) 1755df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "fcmp requires floating point operands"); 1756df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1); 1757df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (Opc == Instruction::ICmp) { 1758df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!Val0->getType()->isIntOrIntVector() && 1759df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner !isa<PointerType>(Val0->getType())) 1760df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "icmp requires pointer or integer operands"); 1761df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1); 1762df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (Opc == Instruction::VFCmp) { 1763df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: REMOVE VFCMP Support 1764d0f9c730d4f0dcf008da0814ebcd6da90e4ba995Chris Lattner if (!Val0->getType()->isFPOrFPVector() || 1765d0f9c730d4f0dcf008da0814ebcd6da90e4ba995Chris Lattner !isa<VectorType>(Val0->getType())) 1766d0f9c730d4f0dcf008da0814ebcd6da90e4ba995Chris Lattner return Error(ID.Loc, "vfcmp requires vector floating point operands"); 1767df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.ConstantVal = ConstantExpr::getVFCmp(Pred, Val0, Val1); 1768df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (Opc == Instruction::VICmp) { 1769d0f9c730d4f0dcf008da0814ebcd6da90e4ba995Chris Lattner // FIXME: REMOVE VICMP Support 1770d0f9c730d4f0dcf008da0814ebcd6da90e4ba995Chris Lattner if (!Val0->getType()->isIntOrIntVector() || 1771d0f9c730d4f0dcf008da0814ebcd6da90e4ba995Chris Lattner !isa<VectorType>(Val0->getType())) 1772d0f9c730d4f0dcf008da0814ebcd6da90e4ba995Chris Lattner return Error(ID.Loc, "vicmp requires vector floating point operands"); 1773df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.ConstantVal = ConstantExpr::getVICmp(Pred, Val0, Val1); 1774df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1775df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1776df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1777df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1778df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1779df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Binary Operators. 1780df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_add: 1781df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sub: 1782df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_mul: 1783df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_udiv: 1784df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sdiv: 1785df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_fdiv: 1786df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_urem: 1787df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_srem: 1788df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_frem: { 1789df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Opc = Lex.getUIntVal(); 1790df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *Val0, *Val1; 1791df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1792df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") || 1793df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalTypeAndValue(Val0) || 1794df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected comma in binary constantexpr") || 1795df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalTypeAndValue(Val1) || 1796df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rparen, "expected ')' in binary constantexpr")) 1797df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1798df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val0->getType() != Val1->getType()) 1799df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "operands of constexpr must have same type"); 1800df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!Val0->getType()->isIntOrIntVector() && 1801df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner !Val0->getType()->isFPOrFPVector()) 1802df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc,"constexpr requires integer, fp, or vector operands"); 1803df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1); 1804df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1805df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1806df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1807df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1808df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Logical Operations 1809df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_shl: 1810df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_lshr: 1811df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ashr: 1812df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_and: 1813df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_or: 1814df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_xor: { 1815df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Opc = Lex.getUIntVal(); 1816df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *Val0, *Val1; 1817df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1818df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") || 1819df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalTypeAndValue(Val0) || 1820df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected comma in logical constantexpr") || 1821df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalTypeAndValue(Val1) || 1822df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rparen, "expected ')' in logical constantexpr")) 1823df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1824df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Val0->getType() != Val1->getType()) 1825df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "operands of constexpr must have same type"); 1826df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!Val0->getType()->isIntOrIntVector()) 1827df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, 1828df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner "constexpr requires integer or integer vector operands"); 1829df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1); 1830df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1831df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1832df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1833df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1834df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_getelementptr: 1835df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_shufflevector: 1836df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_insertelement: 1837df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_extractelement: 1838df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_select: { 1839df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Opc = Lex.getUIntVal(); 1840df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<Constant*, 16> Elts; 1841df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1842df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::lparen, "expected '(' in constantexpr") || 1843df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalValueVector(Elts) || 1844df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rparen, "expected ')' in constantexpr")) 1845df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 1846df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1847df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Opc == Instruction::GetElementPtr) { 1848df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Elts.size() == 0 || !isa<PointerType>(Elts[0]->getType())) 1849df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "getelementptr requires pointer operand"); 1850df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1851df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), 1852df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner (Value**)&Elts[1], Elts.size()-1)) 1853df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "invalid indices for getelementptr"); 1854df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], 1855df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner &Elts[1], Elts.size()-1); 1856df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (Opc == Instruction::Select) { 1857df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Elts.size() != 3) 1858df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "expected three operands to select"); 1859df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1], 1860df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Elts[2])) 1861df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, Reason); 1862df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]); 1863df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (Opc == Instruction::ShuffleVector) { 1864df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Elts.size() != 3) 1865df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "expected three operands to shufflevector"); 1866df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2])) 1867df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "invalid operands to shufflevector"); 1868df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.ConstantVal = ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]); 1869df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (Opc == Instruction::ExtractElement) { 1870df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Elts.size() != 2) 1871df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "expected two operands to extractelement"); 1872df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1])) 1873df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "invalid extractelement operands"); 1874df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]); 1875df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 1876df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner assert(Opc == Instruction::InsertElement && "Unknown opcode"); 1877df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Elts.size() != 3) 1878df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "expected three operands to insertelement"); 1879df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2])) 1880df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "invalid insertelement operands"); 1881df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.ConstantVal = ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]); 1882df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1883df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1884df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.Kind = ValID::t_Constant; 1885df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1886df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1887df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1888df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1889df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 1890df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1891df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1892df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1893df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseGlobalValue - Parse a global value with the specified type. 1894df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseGlobalValue(const Type *Ty, Constant *&V) { 1895df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = 0; 1896df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ValID ID; 18973ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return ParseValID(ID) || 18983ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ConvertGlobalValIDToValue(Ty, ID, V); 1899df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1900df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1901df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ConvertGlobalValIDToValue - Apply a type to a ValID to get a fully resolved 1902df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// constant. 1903df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, 1904df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *&V) { 1905df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (isa<FunctionType>(Ty)) 1906df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "functions are not values, refer to them as pointers"); 1907df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1908df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (ID.Kind) { 1909df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: assert(0 && "Unknown ValID!"); 1910df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case ValID::t_LocalID: 1911df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case ValID::t_LocalName: 1912df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "invalid use of function-local name"); 1913df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case ValID::t_InlineAsm: 1914df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "inline asm can only be an operand of call/invoke"); 1915df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case ValID::t_GlobalName: 1916df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = GetGlobalVal(ID.StrVal, Ty, ID.Loc); 1917df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return V == 0; 1918df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case ValID::t_GlobalID: 1919df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc); 1920df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return V == 0; 1921df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case ValID::t_APSInt: 1922df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<IntegerType>(Ty)) 1923df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "integer constant must have integer type"); 1924df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits()); 1925df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = ConstantInt::get(ID.APSIntVal); 1926df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1927df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case ValID::t_APFloat: 1928df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!Ty->isFloatingPoint() || 1929df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner !ConstantFP::isValueValidForType(Ty, ID.APFloatVal)) 1930df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "floating point constant invalid for type"); 1931df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1932df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // The lexer has no type info, so builds all float and double FP constants 1933df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // as double. Fix this here. Long double does not need this. 1934df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble && 1935df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Ty == Type::FloatTy) { 1936df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner bool Ignored; 1937df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, 1938df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner &Ignored); 1939df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1940df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = ConstantFP::get(ID.APFloatVal); 1941959873d2da763c0e3b3c67c39aab179fc43c5a52Chris Lattner 1942959873d2da763c0e3b3c67c39aab179fc43c5a52Chris Lattner if (V->getType() != Ty) 1943959873d2da763c0e3b3c67c39aab179fc43c5a52Chris Lattner return Error(ID.Loc, "floating point constant does not have type '" + 1944959873d2da763c0e3b3c67c39aab179fc43c5a52Chris Lattner Ty->getDescription() + "'"); 1945959873d2da763c0e3b3c67c39aab179fc43c5a52Chris Lattner 1946df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1947df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case ValID::t_Null: 1948df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<PointerType>(Ty)) 1949df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "null must be a pointer type"); 1950df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = ConstantPointerNull::get(cast<PointerType>(Ty)); 1951df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1952df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case ValID::t_Undef: 1953e67c1aa3986cf98f2021a31a6ad502857c757b7bChris Lattner // FIXME: LabelTy should not be a first-class type. 19540b6163506359223fb0f74d856099ee4d5e768ebeChris Lattner if ((!Ty->isFirstClassType() || Ty == Type::LabelTy) && 19550b6163506359223fb0f74d856099ee4d5e768ebeChris Lattner !isa<OpaqueType>(Ty)) 1956e67c1aa3986cf98f2021a31a6ad502857c757b7bChris Lattner return Error(ID.Loc, "invalid type for undef constant"); 1957df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = UndefValue::get(Ty); 1958df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1959081b505f77b183b7b029818218245cf4986e8948Chris Lattner case ValID::t_EmptyArray: 1960081b505f77b183b7b029818218245cf4986e8948Chris Lattner if (!isa<ArrayType>(Ty) || cast<ArrayType>(Ty)->getNumElements() != 0) 1961081b505f77b183b7b029818218245cf4986e8948Chris Lattner return Error(ID.Loc, "invalid empty array initializer"); 1962081b505f77b183b7b029818218245cf4986e8948Chris Lattner V = UndefValue::get(Ty); 1963081b505f77b183b7b029818218245cf4986e8948Chris Lattner return false; 1964df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case ValID::t_Zero: 1965e67c1aa3986cf98f2021a31a6ad502857c757b7bChris Lattner // FIXME: LabelTy should not be a first-class type. 1966e67c1aa3986cf98f2021a31a6ad502857c757b7bChris Lattner if (!Ty->isFirstClassType() || Ty == Type::LabelTy) 1967df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "invalid type for null constant"); 1968df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = Constant::getNullValue(Ty); 1969df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1970df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case ValID::t_Constant: 1971df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ID.ConstantVal->getType() != Ty) 1972df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "constant expression type mismatch"); 1973df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = ID.ConstantVal; 1974df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1975df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 1976df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1977df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1978df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseGlobalTypeAndValue(Constant *&V) { 1979df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder Type(Type::VoidTy); 1980df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return ParseType(Type) || 1981df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseGlobalValue(Type, V); 1982df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 1983df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 19843ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ParseGlobalValueVector 19853ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ::= /*empty*/ 19863ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ::= TypeAndValue (',' TypeAndValue)* 1987df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts) { 1988df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Empty list. 1989df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() == lltok::rbrace || 1990df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.getKind() == lltok::rsquare || 1991df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.getKind() == lltok::greater || 1992df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.getKind() == lltok::rparen) 1993df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 1994df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 1995df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *C; 1996df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseGlobalTypeAndValue(C)) return true; 1997df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Elts.push_back(C); 1998df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 19993ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner while (EatIfPresent(lltok::comma)) { 2000df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseGlobalTypeAndValue(C)) return true; 2001df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Elts.push_back(C); 2002df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2003df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2004df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2005df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2006df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2007df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2008df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 2009df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Function Parsing. 2010df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 2011df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2012df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V, 2013df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PerFunctionState &PFS) { 2014df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ID.Kind == ValID::t_LocalID) 2015df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = PFS.GetVal(ID.UIntVal, Ty, ID.Loc); 2016df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else if (ID.Kind == ValID::t_LocalName) 2017df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = PFS.GetVal(ID.StrVal, Ty, ID.Loc); 2018b0adcdb7e53dcd636a7eafba44d941ac23006473Steve Naroff else if (ID.Kind == ValID::t_InlineAsm) { 2019df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const PointerType *PTy = dyn_cast<PointerType>(Ty); 2020df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const FunctionType *FTy = 2021df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0; 2022df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2)) 2023df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ID.Loc, "invalid type for inline asm constraint string"); 2024df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal); 2025df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2026df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 2027df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Constant *C; 2028df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ConvertGlobalValIDToValue(Ty, ID, C)) return true; 2029df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = C; 2030df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2031df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2032df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2033df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return V == 0; 2034df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2035df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2036df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS) { 2037df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner V = 0; 2038df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ValID ID; 20393ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return ParseValID(ID) || 20403ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ConvertValIDToValue(Ty, ID, V, PFS); 2041df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2042df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2043df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState &PFS) { 2044df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder T(Type::VoidTy); 20453ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return ParseType(T) || 20463ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseValue(T, V, PFS); 2047df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2048df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2049df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// FunctionHeader 2050df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs 2051df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection 2052df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// OptionalAlign OptGC 2053df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) { 2054df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Parse the linkage. 2055df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy LinkageLoc = Lex.getLoc(); 2056df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Linkage; 2057df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2058df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Visibility, CC, RetAttrs; 2059df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder RetType(Type::VoidTy); 2060df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy RetTypeLoc = Lex.getLoc(); 2061df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseOptionalLinkage(Linkage) || 2062df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalVisibility(Visibility) || 2063df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalCallingConv(CC) || 2064df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalAttrs(RetAttrs, 1) || 2065df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseType(RetType, RetTypeLoc)) 2066df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2067df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2068df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Verify that the linkage is ok. 2069df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch ((GlobalValue::LinkageTypes)Linkage) { 2070df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case GlobalValue::ExternalLinkage: 2071df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; // always ok. 2072df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case GlobalValue::DLLImportLinkage: 2073df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case GlobalValue::ExternalWeakLinkage: 2074df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (isDefine) 2075df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(LinkageLoc, "invalid linkage for function definition"); 2076df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 2077bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola case GlobalValue::PrivateLinkage: 2078df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case GlobalValue::InternalLinkage: 2079df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case GlobalValue::LinkOnceLinkage: 2080df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case GlobalValue::WeakLinkage: 2081df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case GlobalValue::DLLExportLinkage: 2082df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isDefine) 2083df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(LinkageLoc, "invalid linkage for function declaration"); 2084df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 2085df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case GlobalValue::AppendingLinkage: 2086df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case GlobalValue::GhostLinkage: 2087df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case GlobalValue::CommonLinkage: 2088df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(LinkageLoc, "invalid function linkage type"); 2089df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2090df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 209199bb315f32a5f82f9e4978345ee39177db973242Chris Lattner if (!FunctionType::isValidReturnType(RetType) || 209299bb315f32a5f82f9e4978345ee39177db973242Chris Lattner isa<OpaqueType>(RetType)) 2093df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(RetTypeLoc, "invalid function return type"); 2094df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2095df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() != lltok::GlobalVar) 2096df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("expected function name"); 2097df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2098df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy NameLoc = Lex.getLoc(); 2099df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::string FunctionName = Lex.getStrVal(); 21003ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner Lex.Lex(); 2101df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 21023ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (Lex.getKind() != lltok::lparen) 2103df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("expected '(' in function argument list"); 2104df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2105df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::vector<ArgInfo> ArgList; 2106df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner bool isVarArg; 2107df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned FuncAttrs; 2108df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::string Section; 2109df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Alignment; 21103ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner std::string GC; 21113ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner 2112dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner if (ParseArgumentList(ArgList, isVarArg, false) || 21133ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseOptionalAttrs(FuncAttrs, 2) || 21143ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner (EatIfPresent(lltok::kw_section) && 21153ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseStringConstant(Section)) || 21163ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseOptionalAlignment(Alignment) || 21173ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner (EatIfPresent(lltok::kw_gc) && 21183ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseStringConstant(GC))) 21193ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return true; 2120df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2121df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If the alignment was parsed as an attribute, move to the alignment field. 2122df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FuncAttrs & Attribute::Alignment) { 2123df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Alignment = Attribute::getAlignmentFromAttrs(FuncAttrs); 2124df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FuncAttrs &= ~Attribute::Alignment; 2125df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2126df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2127df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Okay, if we got here, the function is syntactically valid. Convert types 2128df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // and do semantic checks. 2129df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::vector<const Type*> ParamTypeList; 2130df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<AttributeWithIndex, 8> Attrs; 2131df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME : In 3.0, stop accepting zext, sext and inreg as optional function 2132df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // attributes. 2133df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg; 2134df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FuncAttrs & ObsoleteFuncAttrs) { 2135df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner RetAttrs |= FuncAttrs & ObsoleteFuncAttrs; 2136df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FuncAttrs &= ~ObsoleteFuncAttrs; 2137df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2138df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2139df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (RetAttrs != Attribute::None) 2140df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs.push_back(AttributeWithIndex::get(0, RetAttrs)); 2141df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2142df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { 2143df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParamTypeList.push_back(ArgList[i].Type); 2144df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ArgList[i].Attrs != Attribute::None) 2145df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs)); 2146df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2147df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2148df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FuncAttrs != Attribute::None) 2149df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs.push_back(AttributeWithIndex::get(~0, FuncAttrs)); 2150df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2151df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end()); 2152df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2153df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const FunctionType *FT = FunctionType::get(RetType, ParamTypeList, isVarArg); 2154df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const PointerType *PFT = PointerType::getUnqual(FT); 2155df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2156df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Fn = 0; 2157df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!FunctionName.empty()) { 2158df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If this was a definition of a forward reference, remove the definition 2159df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // from the forward reference table and fill in the forward ref. 2160df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI = 2161df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefVals.find(FunctionName); 2162df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FRVI != ForwardRefVals.end()) { 2163df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Fn = M->getFunction(FunctionName); 2164df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefVals.erase(FRVI); 2165df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if ((Fn = M->getFunction(FunctionName))) { 2166df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If this function already exists in the symbol table, then it is 2167df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // multiply defined. We accept a few cases for old backwards compat. 2168df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: Remove this stuff for LLVM 3.0. 2169df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Fn->getType() != PFT || Fn->getAttributes() != PAL || 2170df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner (!Fn->isDeclaration() && isDefine)) { 2171df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If the redefinition has different type or different attributes, 2172df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // reject it. If both have bodies, reject it. 2173df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(NameLoc, "invalid redefinition of function '" + 2174df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FunctionName + "'"); 2175df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (Fn->isDeclaration()) { 2176df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Make sure to strip off any argument names so we can't get conflicts. 2177df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end(); 2178df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner AI != AE; ++AI) 2179df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner AI->setName(""); 2180df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2181df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2182df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2183df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (FunctionName.empty()) { 2184df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If this is a definition of a forward referenced function, make sure the 2185df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // types agree. 2186df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I 2187df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner = ForwardRefValIDs.find(NumberedVals.size()); 2188df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I != ForwardRefValIDs.end()) { 2189df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Fn = cast<Function>(I->second.first); 2190df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Fn->getType() != PFT) 2191df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(NameLoc, "type of definition and forward reference of '@" + 2192df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner utostr(NumberedVals.size()) +"' disagree"); 2193df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ForwardRefValIDs.erase(I); 2194df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2195df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2196df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2197df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Fn == 0) 2198df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M); 2199df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else // Move the forward-reference to the correct spot in the module. 2200df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn); 2201df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2202df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FunctionName.empty()) 2203df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner NumberedVals.push_back(Fn); 2204df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2205df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Fn->setLinkage((GlobalValue::LinkageTypes)Linkage); 2206df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility); 2207df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Fn->setCallingConv(CC); 2208df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Fn->setAttributes(PAL); 2209df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Fn->setAlignment(Alignment); 2210df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Fn->setSection(Section); 2211df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!GC.empty()) Fn->setGC(GC.c_str()); 2212df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2213df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Add all of the arguments we parsed to the function. 2214df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Function::arg_iterator ArgIt = Fn->arg_begin(); 2215df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) { 2216df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If the argument has a name, insert it into the argument symbol table. 2217df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ArgList[i].Name.empty()) continue; 2218df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2219df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Set the name, if it conflicted, it will be auto-renamed. 2220df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ArgIt->setName(ArgList[i].Name); 2221df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2222df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ArgIt->getNameStr() != ArgList[i].Name) 2223df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ArgList[i].Loc, "redefinition of argument '%" + 2224df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ArgList[i].Name + "'"); 2225df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2226df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2227df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2228df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2229df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2230df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2231df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseFunctionBody 2232df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= '{' BasicBlock+ '}' 2233df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'begin' BasicBlock+ 'end' // FIXME: remove in LLVM 3.0 2234df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// 2235df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseFunctionBody(Function &Fn) { 2236df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() != lltok::lbrace && Lex.getKind() != lltok::kw_begin) 2237df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("expected '{' in function body"); 2238df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); // eat the {. 2239df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2240df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PerFunctionState PFS(*this, Fn); 2241df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2242df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner while (Lex.getKind() != lltok::rbrace && Lex.getKind() != lltok::kw_end) 2243df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseBasicBlock(PFS)) return true; 2244df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2245df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Eat the }. 2246df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 2247df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2248df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Verify function is ok. 2249df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return PFS.VerifyFunctionComplete(); 2250df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2251df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2252df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseBasicBlock 2253df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= LabelStr? Instruction* 2254df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseBasicBlock(PerFunctionState &PFS) { 2255df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If this basic block starts out with a name, remember it. 2256df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::string Name; 2257df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy NameLoc = Lex.getLoc(); 2258df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() == lltok::LabelStr) { 2259df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Name = Lex.getStrVal(); 2260df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 2261df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2262df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2263df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner BasicBlock *BB = PFS.DefineBB(Name, NameLoc); 2264df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (BB == 0) return true; 2265df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2266df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::string NameStr; 2267df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2268df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Parse the instructions in this block until we get a terminator. 2269df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Instruction *Inst; 2270df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner do { 2271df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // This instruction may have three possibilities for a name: a) none 2272df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // specified, b) name specified "%foo =", c) number specified: "%4 =". 2273df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy NameLoc = Lex.getLoc(); 2274df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner int NameID = -1; 2275df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner NameStr = ""; 2276df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2277df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() == lltok::LocalVarID) { 2278df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner NameID = Lex.getUIntVal(); 2279df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 2280df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::equal, "expected '=' after instruction id")) 2281df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2282df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (Lex.getKind() == lltok::LocalVar || 2283df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: REMOVE IN LLVM 3.0 2284df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.getKind() == lltok::StringConstant) { 2285df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner NameStr = Lex.getStrVal(); 2286df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 2287df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::equal, "expected '=' after instruction name")) 2288df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2289df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2290df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2291df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseInstruction(Inst, BB, PFS)) return true; 2292df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2293df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner BB->getInstList().push_back(Inst); 2294df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2295df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Set the name on the instruction. 2296df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true; 2297df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } while (!isa<TerminatorInst>(Inst)); 2298df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2299df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2300df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2301df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2302df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 2303df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Instruction Parsing. 2304df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 2305df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2306df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseInstruction - Parse one of the many different instructions. 2307df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// 2308df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB, 2309df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PerFunctionState &PFS) { 2310df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner lltok::Kind Token = Lex.getKind(); 2311df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Token == lltok::Eof) 2312df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("found end of file when expecting more instructions"); 2313df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc = Lex.getLoc(); 2314df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); // Eat the keyword. 2315df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2316df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Token) { 2317df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: return Error(Loc, "expected instruction opcode"); 2318df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Terminator Instructions. 2319df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_unwind: Inst = new UnwindInst(); return false; 2320df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_unreachable: Inst = new UnreachableInst(); return false; 2321df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ret: return ParseRet(Inst, BB, PFS); 2322df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_br: return ParseBr(Inst, PFS); 2323df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_switch: return ParseSwitch(Inst, PFS); 2324df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_invoke: return ParseInvoke(Inst, PFS); 2325df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Binary Operators. 2326df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_add: 2327df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sub: 2328e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner case lltok::kw_mul: return ParseArithmetic(Inst, PFS, Lex.getUIntVal(), 0); 2329e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner 2330df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_udiv: 2331df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sdiv: 2332df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_urem: 2333e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner case lltok::kw_srem: return ParseArithmetic(Inst, PFS, Lex.getUIntVal(), 1); 2334e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner case lltok::kw_fdiv: 2335e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner case lltok::kw_frem: return ParseArithmetic(Inst, PFS, Lex.getUIntVal(), 2); 2336df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_shl: 2337df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_lshr: 2338df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ashr: 2339df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_and: 2340df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_or: 2341df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_xor: return ParseLogical(Inst, PFS, Lex.getUIntVal()); 2342df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_icmp: 2343df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_fcmp: 2344df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_vicmp: 2345df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_vfcmp: return ParseCompare(Inst, PFS, Lex.getUIntVal()); 2346df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Casts. 2347df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_trunc: 2348df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_zext: 2349df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sext: 2350df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_fptrunc: 2351df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_fpext: 2352df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_bitcast: 2353df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_uitofp: 2354df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sitofp: 2355df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_fptoui: 2356df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_fptosi: 2357df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_inttoptr: 2358df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, Lex.getUIntVal()); 2359df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Other. 2360df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_select: return ParseSelect(Inst, PFS); 23610088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS); 2362df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS); 2363df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS); 2364df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS); 2365df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_phi: return ParsePHI(Inst, PFS); 2366df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_call: return ParseCall(Inst, PFS, false); 2367df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_tail: return ParseCall(Inst, PFS, true); 2368df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Memory. 2369df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_alloca: 2370df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_malloc: return ParseAlloc(Inst, PFS, Lex.getUIntVal()); 2371df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_free: return ParseFree(Inst, PFS); 2372df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_load: return ParseLoad(Inst, PFS, false); 2373df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_store: return ParseStore(Inst, PFS, false); 2374df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_volatile: 23753ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (EatIfPresent(lltok::kw_load)) 2376df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return ParseLoad(Inst, PFS, true); 23773ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner else if (EatIfPresent(lltok::kw_store)) 2378df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return ParseStore(Inst, PFS, true); 23793ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner else 2380df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return TokError("expected 'load' or 'store'"); 2381df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_getresult: return ParseGetResult(Inst, PFS); 2382df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS); 2383df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS); 2384df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS); 2385df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2386df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2387df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2388df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind. 2389df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) { 2390df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: REMOVE vicmp/vfcmp! 2391df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Opc == Instruction::FCmp || Opc == Instruction::VFCmp) { 2392df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Lex.getKind()) { 2393df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: TokError("expected fcmp predicate (e.g. 'oeq')"); 2394df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break; 2395df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_one: P = CmpInst::FCMP_ONE; break; 2396df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_olt: P = CmpInst::FCMP_OLT; break; 2397df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break; 2398df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ole: P = CmpInst::FCMP_OLE; break; 2399df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_oge: P = CmpInst::FCMP_OGE; break; 2400df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ord: P = CmpInst::FCMP_ORD; break; 2401df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_uno: P = CmpInst::FCMP_UNO; break; 2402df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break; 2403df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_une: P = CmpInst::FCMP_UNE; break; 2404df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ult: P = CmpInst::FCMP_ULT; break; 2405df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break; 2406df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ule: P = CmpInst::FCMP_ULE; break; 2407df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_uge: P = CmpInst::FCMP_UGE; break; 2408df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_true: P = CmpInst::FCMP_TRUE; break; 2409df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_false: P = CmpInst::FCMP_FALSE; break; 2410df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2411df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else { 2412df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner switch (Lex.getKind()) { 2413df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner default: TokError("expected icmp predicate (e.g. 'eq')"); 2414df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_eq: P = CmpInst::ICMP_EQ; break; 2415df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ne: P = CmpInst::ICMP_NE; break; 2416df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_slt: P = CmpInst::ICMP_SLT; break; 2417df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break; 2418df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sle: P = CmpInst::ICMP_SLE; break; 2419df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_sge: P = CmpInst::ICMP_SGE; break; 2420df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ult: P = CmpInst::ICMP_ULT; break; 2421df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break; 2422df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_ule: P = CmpInst::ICMP_ULE; break; 2423df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner case lltok::kw_uge: P = CmpInst::ICMP_UGE; break; 2424df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2425df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2426df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); 2427df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2428df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2429df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2430df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 2431df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Terminator Instructions. 2432df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 2433df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2434df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseRet - Parse a return instruction. 2435df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'ret' void 2436df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'ret' TypeAndValue 2437df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'ret' TypeAndValue (',' TypeAndValue)+ [[obsolete: LLVM 3.0]] 2438df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB, 2439df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PerFunctionState &PFS) { 2440df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder Ty(Type::VoidTy); 2441df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseType(Ty)) return true; 2442df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2443df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Ty == Type::VoidTy) { 2444df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = ReturnInst::Create(); 2445df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2446df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2447df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2448df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *RV; 2449df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseValue(Ty, RV, PFS)) return true; 2450df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2451df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // The normal case is one return value. 2452df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() == lltok::comma) { 2453df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: LLVM 3.0 remove MRV support for 'ret i32 1, i32 2', requiring use 2454df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // of 'ret {i32,i32} {i32 1, i32 2}' 2455df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<Value*, 8> RVs; 2456df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner RVs.push_back(RV); 2457df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 24583ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner while (EatIfPresent(lltok::comma)) { 2459df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(RV, PFS)) return true; 2460df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner RVs.push_back(RV); 2461df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2462df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2463df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner RV = UndefValue::get(PFS.getFunction().getReturnType()); 2464df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = RVs.size(); i != e; ++i) { 2465df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Instruction *I = InsertValueInst::Create(RV, RVs[i], i, "mrv"); 2466df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner BB->getInstList().push_back(I); 2467df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner RV = I; 2468df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2469df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2470df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = ReturnInst::Create(RV); 2471df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2472df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2473df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2474df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2475df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseBr 2476df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'br' TypeAndValue 2477df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue 2478df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) { 2479df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc, Loc2; 2480df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Op0, *Op1, *Op2; 2481df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Op0, Loc, PFS)) return true; 2482df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2483df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) { 2484df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = BranchInst::Create(BB); 2485df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2486df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2487df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2488df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Op0->getType() != Type::Int1Ty) 2489df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "branch condition must have 'i1' type"); 2490df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2491df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseToken(lltok::comma, "expected ',' after branch condition") || 2492df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(Op1, Loc, PFS) || 2493df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after true destination") || 2494df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(Op2, Loc2, PFS)) 2495df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2496df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2497df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<BasicBlock>(Op1)) 2498df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "true destination of branch must be a basic block"); 2499df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<BasicBlock>(Op2)) 2500df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc2, "true destination of branch must be a basic block"); 2501df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2502df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = BranchInst::Create(cast<BasicBlock>(Op1), cast<BasicBlock>(Op2), Op0); 2503df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2504df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2505df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2506df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseSwitch 2507df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// Instruction 2508df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']' 2509df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// JumpTable 2510df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= (TypeAndValue ',' TypeAndValue)* 2511df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) { 2512df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy CondLoc, BBLoc; 2513df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Cond, *DefaultBB; 2514df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Cond, CondLoc, PFS) || 2515df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after switch condition") || 2516df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(DefaultBB, BBLoc, PFS) || 2517df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::lsquare, "expected '[' with switch table")) 2518df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2519df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2520df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<IntegerType>(Cond->getType())) 2521df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(CondLoc, "switch condition must have integer type"); 2522df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<BasicBlock>(DefaultBB)) 2523df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(BBLoc, "default destination must be a basic block"); 2524df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2525df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Parse the jump table pairs. 2526df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallPtrSet<Value*, 32> SeenCases; 2527df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table; 2528df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner while (Lex.getKind() != lltok::rsquare) { 2529df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Constant, *DestBB; 2530df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2531df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Constant, CondLoc, PFS) || 2532df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after case value") || 2533df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(DestBB, BBLoc, PFS)) 2534df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2535df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2536df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!SeenCases.insert(Constant)) 2537df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(CondLoc, "duplicate case value in switch"); 2538df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<ConstantInt>(Constant)) 2539df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(CondLoc, "case value is not a constant integer"); 2540df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<BasicBlock>(DestBB)) 2541df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(BBLoc, "case destination is not a basic block"); 2542df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2543df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Table.push_back(std::make_pair(cast<ConstantInt>(Constant), 2544df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner cast<BasicBlock>(DestBB))); 2545df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2546df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2547df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Lex.Lex(); // Eat the ']'. 2548df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2549df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SwitchInst *SI = SwitchInst::Create(Cond, cast<BasicBlock>(DefaultBB), 2550df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Table.size()); 2551df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = Table.size(); i != e; ++i) 2552df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SI->addCase(Table[i].first, Table[i].second); 2553df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = SI; 2554df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2555df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2556df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2557df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseInvoke 2558df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList 2559df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue 2560df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) { 2561df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy CallLoc = Lex.getLoc(); 2562df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned CC, RetAttrs, FnAttrs; 2563df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder RetType(Type::VoidTy); 2564df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy RetTypeLoc; 2565df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ValID CalleeID; 2566df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<ParamInfo, 16> ArgList; 2567df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2568df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *NormalBB, *UnwindBB; 2569df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseOptionalCallingConv(CC) || 2570df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalAttrs(RetAttrs, 1) || 2571df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseType(RetType, RetTypeLoc) || 2572df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseValID(CalleeID) || 2573df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseParameterList(ArgList, PFS) || 2574df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalAttrs(FnAttrs, 2) || 2575df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::kw_to, "expected 'to' in invoke") || 2576df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(NormalBB, PFS) || 2577df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") || 2578df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(UnwindBB, PFS)) 2579df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2580df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2581df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<BasicBlock>(NormalBB)) 2582df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(CallLoc, "normal destination is not a basic block"); 2583df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<BasicBlock>(UnwindBB)) 2584df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(CallLoc, "unwind destination is not a basic block"); 2585df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2586df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If RetType is a non-function pointer type, then this is the short syntax 2587df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // for the call, which means that RetType is just the return type. Infer the 2588df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // rest of the function argument types from the arguments that are present. 2589df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const PointerType *PFTy = 0; 2590df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const FunctionType *Ty = 0; 2591df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!(PFTy = dyn_cast<PointerType>(RetType)) || 2592df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) { 2593df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Pull out the types of all of the arguments... 2594df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::vector<const Type*> ParamTypes; 2595df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = ArgList.size(); i != e; ++i) 2596df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParamTypes.push_back(ArgList[i].V->getType()); 2597df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2598df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!FunctionType::isValidReturnType(RetType)) 2599df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(RetTypeLoc, "Invalid result type for LLVM function"); 2600df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2601df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Ty = FunctionType::get(RetType, ParamTypes, false); 2602df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PFTy = PointerType::getUnqual(Ty); 2603df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2604df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2605df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Look up the callee. 2606df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Callee; 2607df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ConvertValIDToValue(PFTy, CalleeID, Callee, PFS)) return true; 2608df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2609df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: In LLVM 3.0, stop accepting zext, sext and inreg as optional 2610df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // function attributes. 2611df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg; 2612df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FnAttrs & ObsoleteFuncAttrs) { 2613df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner RetAttrs |= FnAttrs & ObsoleteFuncAttrs; 2614df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FnAttrs &= ~ObsoleteFuncAttrs; 2615df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2616df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2617df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Set up the Attributes for the function. 2618df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<AttributeWithIndex, 8> Attrs; 2619df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (RetAttrs != Attribute::None) 2620df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs.push_back(AttributeWithIndex::get(0, RetAttrs)); 2621df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2622df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<Value*, 8> Args; 2623df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2624df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Loop through FunctionType's arguments and ensure they are specified 2625df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // correctly. Also, gather any parameter attributes. 2626df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FunctionType::param_iterator I = Ty->param_begin(); 2627df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FunctionType::param_iterator E = Ty->param_end(); 2628df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { 2629df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const Type *ExpectedTy = 0; 2630df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I != E) { 2631df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ExpectedTy = *I++; 2632df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (!Ty->isVarArg()) { 2633df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ArgList[i].Loc, "too many arguments specified"); 2634df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2635df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2636df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ExpectedTy && ExpectedTy != ArgList[i].V->getType()) 2637df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ArgList[i].Loc, "argument is not of expected type '" + 2638df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ExpectedTy->getDescription() + "'"); 2639df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Args.push_back(ArgList[i].V); 2640df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ArgList[i].Attrs != Attribute::None) 2641df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs)); 2642df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2643df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2644df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I != E) 2645df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(CallLoc, "not enough parameters specified for call"); 2646df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2647df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FnAttrs != Attribute::None) 2648df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs.push_back(AttributeWithIndex::get(~0, FnAttrs)); 2649df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2650df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Finish off the Attributes and check them 2651df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end()); 2652df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2653df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner InvokeInst *II = InvokeInst::Create(Callee, cast<BasicBlock>(NormalBB), 2654df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner cast<BasicBlock>(UnwindBB), 2655df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Args.begin(), Args.end()); 2656df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner II->setCallingConv(CC); 2657df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner II->setAttributes(PAL); 2658df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = II; 2659df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2660df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2661df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2662df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2663df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2664df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 2665df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Binary Operators. 2666df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 2667df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2668df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseArithmetic 2669e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner/// ::= ArithmeticOps TypeAndValue ',' Value 2670e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner/// 2671e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1, 2672e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner/// then any integer operand is allowed, if it is 2, any fp operand is allowed. 2673df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS, 2674e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner unsigned Opc, unsigned OperandType) { 2675df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc; Value *LHS, *RHS; 2676df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(LHS, Loc, PFS) || 2677df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' in arithmetic operation") || 2678df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseValue(LHS->getType(), RHS, PFS)) 2679df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2680df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2681e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner bool Valid; 2682e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner switch (OperandType) { 2683e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner default: assert(0 && "Unknown operand type!"); 2684e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner case 0: // int or FP. 2685e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner Valid = LHS->getType()->isIntOrIntVector() || 2686e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner LHS->getType()->isFPOrFPVector(); 2687e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner break; 2688e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner case 1: Valid = LHS->getType()->isIntOrIntVector(); break; 2689e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner case 2: Valid = LHS->getType()->isFPOrFPVector(); break; 2690e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner } 2691e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner 2692e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner if (!Valid) 2693e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner return Error(Loc, "invalid operand type for instruction"); 2694df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2695df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); 2696df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2697df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2698df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2699df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseLogical 2700df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= ArithmeticOps TypeAndValue ',' Value { 2701df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS, 2702df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Opc) { 2703df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc; Value *LHS, *RHS; 2704df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(LHS, Loc, PFS) || 2705df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' in logical operation") || 2706df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseValue(LHS->getType(), RHS, PFS)) 2707df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2708df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2709df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!LHS->getType()->isIntOrIntVector()) 2710df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc,"instruction requires integer or integer vector operands"); 2711df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2712df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); 2713df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2714df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2715df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2716df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2717df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseCompare 2718df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'icmp' IPredicates TypeAndValue ',' Value 2719df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'fcmp' FPredicates TypeAndValue ',' Value 2720df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'vicmp' IPredicates TypeAndValue ',' Value 2721df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'vfcmp' FPredicates TypeAndValue ',' Value 2722df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS, 2723df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Opc) { 2724df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Parse the integer/fp comparison predicate. 2725df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc; 2726df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Pred; 2727df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *LHS, *RHS; 2728df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseCmpPredicate(Pred, Opc) || 2729df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(LHS, Loc, PFS) || 2730df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after compare value") || 2731df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseValue(LHS->getType(), RHS, PFS)) 2732df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2733df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2734df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Opc == Instruction::FCmp) { 2735df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!LHS->getType()->isFPOrFPVector()) 2736df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "fcmp requires floating point operands"); 2737df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS); 2738df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (Opc == Instruction::ICmp) { 2739df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!LHS->getType()->isIntOrIntVector() && 2740df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner !isa<PointerType>(LHS->getType())) 2741df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "icmp requires integer operands"); 2742df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS); 2743df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (Opc == Instruction::VFCmp) { 27444a1c4a414ca8ab087dd365eb2e5421ee8535c7aeChris Lattner if (!LHS->getType()->isFPOrFPVector() || !isa<VectorType>(LHS->getType())) 27454a1c4a414ca8ab087dd365eb2e5421ee8535c7aeChris Lattner return Error(Loc, "vfcmp requires vector floating point operands"); 2746df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = new VFCmpInst(CmpInst::Predicate(Pred), LHS, RHS); 2747df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (Opc == Instruction::VICmp) { 27484a1c4a414ca8ab087dd365eb2e5421ee8535c7aeChris Lattner if (!LHS->getType()->isIntOrIntVector() || !isa<VectorType>(LHS->getType())) 27494a1c4a414ca8ab087dd365eb2e5421ee8535c7aeChris Lattner return Error(Loc, "vicmp requires vector floating point operands"); 2750df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = new VICmpInst(CmpInst::Predicate(Pred), LHS, RHS); 2751df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2752df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2753df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2754df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2755df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 2756df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Other Instructions. 2757df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 2758df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2759df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2760df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseCast 2761df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= CastOpc TypeAndValue 'to' Type 2762df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS, 2763df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Opc) { 2764df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc; Value *Op; 2765df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder DestTy(Type::VoidTy); 2766df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Op, Loc, PFS) || 2767df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::kw_to, "expected 'to' after cast value") || 2768df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseType(DestTy)) 2769df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2770df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2771df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) 2772df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "invalid cast opcode for cast from '" + 2773df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Op->getType()->getDescription() + "' to '" + 2774df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner DestTy->getDescription() + "'"); 2775df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy); 2776df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2777df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2778df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2779df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseSelect 2780df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue 2781df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) { 2782df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc; 2783df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Op0, *Op1, *Op2; 2784df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Op0, Loc, PFS) || 2785df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after select condition") || 2786df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(Op1, PFS) || 2787df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after select value") || 2788df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(Op2, PFS)) 2789df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2790df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2791df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2)) 2792df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, Reason); 2793df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2794df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = SelectInst::Create(Op0, Op1, Op2); 2795df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2796df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2797df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 27980088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner/// ParseVA_Arg 27990088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner/// ::= 'va_arg' TypeAndValue ',' Type 28000088a5cc722b9e32820d31c70175a7650ad4772dChris Lattnerbool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) { 2801df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Op; 2802df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder EltTy(Type::VoidTy); 28030088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner LocTy TypeLoc; 2804df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Op, PFS) || 2805df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after vaarg operand") || 28060088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner ParseType(EltTy, TypeLoc)) 2807df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 28080088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner 28090088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner if (!EltTy->isFirstClassType()) 28100088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner return Error(TypeLoc, "va_arg requires operand with first class type"); 2811df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2812df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = new VAArgInst(Op, EltTy); 2813df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2814df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2815df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2816df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseExtractElement 2817df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'extractelement' TypeAndValue ',' TypeAndValue 2818df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) { 2819df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc; 2820df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Op0, *Op1; 2821df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Op0, Loc, PFS) || 2822df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after extract value") || 2823df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(Op1, PFS)) 2824df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2825df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2826df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ExtractElementInst::isValidOperands(Op0, Op1)) 2827df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "invalid extractelement operands"); 2828df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2829df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = new ExtractElementInst(Op0, Op1); 2830df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2831df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2832df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2833df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseInsertElement 2834df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue 2835df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) { 2836df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc; 2837df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Op0, *Op1, *Op2; 2838df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Op0, Loc, PFS) || 2839df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after insertelement value") || 2840df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(Op1, PFS) || 2841df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after insertelement value") || 2842df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(Op2, PFS)) 2843df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2844df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2845df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!InsertElementInst::isValidOperands(Op0, Op1, Op2)) 2846df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "invalid extractelement operands"); 2847df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2848df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = InsertElementInst::Create(Op0, Op1, Op2); 2849df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2850df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2851df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2852df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseShuffleVector 2853df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue 2854df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) { 2855df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy Loc; 2856df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Op0, *Op1, *Op2; 2857df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Op0, Loc, PFS) || 2858df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after shuffle mask") || 2859df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(Op1, PFS) || 2860df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after shuffle value") || 2861df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(Op2, PFS)) 2862df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2863df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2864df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2)) 2865df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "invalid extractelement operands"); 2866df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2867df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = new ShuffleVectorInst(Op0, Op1, Op2); 2868df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2869df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2870df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2871df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParsePHI 2872df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Valueß ']')* 2873df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) { 2874df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder Ty(Type::VoidTy); 2875df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Op0, *Op1; 2876df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy TypeLoc = Lex.getLoc(); 2877df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2878df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseType(Ty) || 2879df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::lsquare, "expected '[' in phi value list") || 2880df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseValue(Ty, Op0, PFS) || 2881df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after insertelement value") || 2882df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseValue(Type::LabelTy, Op1, PFS) || 2883df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rsquare, "expected ']' in phi value list")) 2884df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2885df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2886df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals; 2887df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner while (1) { 2888df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1))); 2889df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 28903ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (!EatIfPresent(lltok::comma)) 2891df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner break; 2892df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 28933ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseToken(lltok::lsquare, "expected '[' in phi value list") || 2894df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseValue(Ty, Op0, PFS) || 2895df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after insertelement value") || 2896df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseValue(Type::LabelTy, Op1, PFS) || 2897df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::rsquare, "expected ']' in phi value list")) 2898df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2899df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2900df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2901df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!Ty->isFirstClassType()) 2902df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(TypeLoc, "phi node must have first class type"); 2903df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2904df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PHINode *PN = PHINode::Create(Ty); 2905df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PN->reserveOperandSpace(PHIVals.size()); 2906df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = PHIVals.size(); i != e; ++i) 2907df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PN->addIncoming(PHIVals[i].first, PHIVals[i].second); 2908df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = PN; 2909df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 2910df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 2911df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2912df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseCall 2913df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'tail'? 'call' OptionalCallingConv OptionalAttrs Type Value 2914df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParameterList OptionalAttrs 2915df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS, 2916df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner bool isTail) { 2917df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned CC, RetAttrs, FnAttrs; 2918df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder RetType(Type::VoidTy); 2919df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy RetTypeLoc; 2920df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ValID CalleeID; 2921df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<ParamInfo, 16> ArgList; 2922df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy CallLoc = Lex.getLoc(); 2923df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2924df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if ((isTail && ParseToken(lltok::kw_call, "expected 'tail call'")) || 2925df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalCallingConv(CC) || 2926df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalAttrs(RetAttrs, 1) || 2927df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseType(RetType, RetTypeLoc) || 2928df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseValID(CalleeID) || 2929df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseParameterList(ArgList, PFS) || 2930df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalAttrs(FnAttrs, 2)) 2931df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 2932df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2933df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // If RetType is a non-function pointer type, then this is the short syntax 2934df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // for the call, which means that RetType is just the return type. Infer the 2935df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // rest of the function argument types from the arguments that are present. 2936df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const PointerType *PFTy = 0; 2937df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const FunctionType *Ty = 0; 2938df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!(PFTy = dyn_cast<PointerType>(RetType)) || 2939df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) { 2940df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Pull out the types of all of the arguments... 2941df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner std::vector<const Type*> ParamTypes; 2942df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = ArgList.size(); i != e; ++i) 2943df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParamTypes.push_back(ArgList[i].V->getType()); 2944df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2945df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!FunctionType::isValidReturnType(RetType)) 2946df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(RetTypeLoc, "Invalid result type for LLVM function"); 2947df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2948df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Ty = FunctionType::get(RetType, ParamTypes, false); 2949df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PFTy = PointerType::getUnqual(Ty); 2950df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2951df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2952df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Look up the callee. 2953df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Callee; 2954df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ConvertValIDToValue(PFTy, CalleeID, Callee, PFS)) return true; 2955df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2956df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // FIXME: In LLVM 3.0, stop accepting zext, sext and inreg as optional 2957df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // function attributes. 2958df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg; 2959df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FnAttrs & ObsoleteFuncAttrs) { 2960df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner RetAttrs |= FnAttrs & ObsoleteFuncAttrs; 2961df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FnAttrs &= ~ObsoleteFuncAttrs; 2962df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2963df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2964df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Set up the Attributes for the function. 2965df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<AttributeWithIndex, 8> Attrs; 2966df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (RetAttrs != Attribute::None) 2967df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs.push_back(AttributeWithIndex::get(0, RetAttrs)); 2968df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2969df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<Value*, 8> Args; 2970df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2971df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Loop through FunctionType's arguments and ensure they are specified 2972df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // correctly. Also, gather any parameter attributes. 2973df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FunctionType::param_iterator I = Ty->param_begin(); 2974df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner FunctionType::param_iterator E = Ty->param_end(); 2975df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { 2976df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner const Type *ExpectedTy = 0; 2977df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I != E) { 2978df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ExpectedTy = *I++; 2979df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } else if (!Ty->isVarArg()) { 2980df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ArgList[i].Loc, "too many arguments specified"); 2981df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2982df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2983df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ExpectedTy && ExpectedTy != ArgList[i].V->getType()) 2984df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ArgList[i].Loc, "argument is not of expected type '" + 2985df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ExpectedTy->getDescription() + "'"); 2986df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Args.push_back(ArgList[i].V); 2987df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ArgList[i].Attrs != Attribute::None) 2988df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs)); 2989df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 2990df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2991df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (I != E) 2992df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(CallLoc, "not enough parameters specified for call"); 2993df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2994df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (FnAttrs != Attribute::None) 2995df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Attrs.push_back(AttributeWithIndex::get(~0, FnAttrs)); 2996df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 2997df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner // Finish off the Attributes and check them 2998df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end()); 2999df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3000df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner CallInst *CI = CallInst::Create(Callee, Args.begin(), Args.end()); 3001df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner CI->setTailCall(isTail); 3002df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner CI->setCallingConv(CC); 3003df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner CI->setAttributes(PAL); 3004df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = CI; 3005df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3006df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3007df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3008df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 3009df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Memory Instructions. 3010df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===// 3011df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3012df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseAlloc 3013df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'malloc' Type (',' TypeAndValue)? (',' OptionalAlignment)? 3014df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'alloca' Type (',' TypeAndValue)? (',' OptionalAlignment)? 3015df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS, 3016df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Opc) { 3017df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner PATypeHolder Ty(Type::VoidTy); 3018df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Size = 0; 3019df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner LocTy SizeLoc = 0; 3020df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Alignment = 0; 30213ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseType(Ty)) return true; 3022df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 30233ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (EatIfPresent(lltok::comma)) { 3024df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Lex.getKind() == lltok::kw_align) { 3025df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseOptionalAlignment(Alignment)) return true; 30263ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner } else if (ParseTypeAndValue(Size, SizeLoc, PFS) || 30273ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseOptionalCommaAlignment(Alignment)) { 30283ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner return true; 3029df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 3030df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 3031df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3032df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Size && Size->getType() != Type::Int32Ty) 3033df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(SizeLoc, "element count must be i32"); 3034df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3035df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (Opc == Instruction::Malloc) 3036df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = new MallocInst(Ty, Size, Alignment); 3037df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner else 3038df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = new AllocaInst(Ty, Size, Alignment); 3039df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3040df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3041df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3042df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseFree 3043df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'free' TypeAndValue 3044df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseFree(Instruction *&Inst, PerFunctionState &PFS) { 3045df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Val; LocTy Loc; 3046df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Val, Loc, PFS)) return true; 3047df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<PointerType>(Val->getType())) 3048df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "operand to free must be a pointer"); 3049df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = new FreeInst(Val); 3050df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3051df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3052df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3053df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseLoad 3054df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'volatile'? 'load' TypeAndValue (',' 'align' uint)? 3055df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS, 3056df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner bool isVolatile) { 3057df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Val; LocTy Loc; 3058df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Alignment; 3059df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Val, Loc, PFS) || 3060df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalCommaAlignment(Alignment)) 3061df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 3062df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3063df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<PointerType>(Val->getType()) || 3064df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType()) 3065df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "load operand must be a pointer to a first class type"); 3066df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3067df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = new LoadInst(Val, "", isVolatile, Alignment); 3068df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3069df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3070df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3071df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseStore 3072df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'volatile'? 'store' TypeAndValue ',' TypeAndValue (',' 'align' uint)? 3073df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS, 3074df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner bool isVolatile) { 3075df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Val, *Ptr; LocTy Loc, PtrLoc; 3076df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Alignment; 3077df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Val, Loc, PFS) || 3078df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after store operand") || 3079df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(Ptr, PtrLoc, PFS) || 3080df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseOptionalCommaAlignment(Alignment)) 3081df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 3082df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3083df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<PointerType>(Ptr->getType())) 3084df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(PtrLoc, "store operand must be a pointer"); 3085df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!Val->getType()->isFirstClassType()) 3086df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "store operand must be a first class value"); 3087df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType()) 3088df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "stored value and pointer type do not match"); 3089df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3090df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = new StoreInst(Val, Ptr, isVolatile, Alignment); 3091df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3092df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3093df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3094df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseGetResult 3095df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'getresult' TypeAndValue ',' uint 3096df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// FIXME: Remove support for getresult in LLVM 3.0 3097df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseGetResult(Instruction *&Inst, PerFunctionState &PFS) { 3098df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Val; LocTy ValLoc, EltLoc; 3099df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner unsigned Element; 3100df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Val, ValLoc, PFS) || 3101df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected ',' after getresult operand") || 31023ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner ParseUInt32(Element, EltLoc)) 3103df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 3104df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3105df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<StructType>(Val->getType()) && !isa<ArrayType>(Val->getType())) 3106df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(ValLoc, "getresult inst requires an aggregate operand"); 3107df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ExtractValueInst::getIndexedType(Val->getType(), Element)) 3108df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(EltLoc, "invalid getresult index for value"); 3109df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = ExtractValueInst::Create(Val, Element); 3110df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3111df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3112df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3113df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseGetElementPtr 3114df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'getelementptr' TypeAndValue (',' TypeAndValue)* 3115df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) { 3116df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Ptr, *Val; LocTy Loc, EltLoc; 31173ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseTypeAndValue(Ptr, Loc, PFS)) return true; 3118df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3119df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<PointerType>(Ptr->getType())) 3120df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "base of getelementptr must be a pointer"); 3121df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3122df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<Value*, 16> Indices; 31233ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner while (EatIfPresent(lltok::comma)) { 31243ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner if (ParseTypeAndValue(Val, EltLoc, PFS)) return true; 3125df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<IntegerType>(Val->getType())) 3126df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(EltLoc, "getelementptr index must be an integer"); 3127df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Indices.push_back(Val); 3128df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner } 3129df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3130df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!GetElementPtrInst::getIndexedType(Ptr->getType(), 3131df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Indices.begin(), Indices.end())) 3132df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "invalid getelementptr indices"); 3133df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = GetElementPtrInst::Create(Ptr, Indices.begin(), Indices.end()); 3134df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3135df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3136df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3137df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseExtractValue 3138df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'extractvalue' TypeAndValue (',' uint32)+ 3139df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) { 3140df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Val; LocTy Loc; 3141df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<unsigned, 4> Indices; 3142df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Val, Loc, PFS) || 3143df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseIndexList(Indices)) 3144df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 3145df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3146df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<StructType>(Val->getType()) && !isa<ArrayType>(Val->getType())) 3147df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "extractvalue operand must be array or struct"); 3148df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3149df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(), 3150df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Indices.end())) 3151df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc, "invalid indices for extractvalue"); 3152df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = ExtractValueInst::Create(Val, Indices.begin(), Indices.end()); 3153df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3154df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3155df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3156df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseInsertValue 3157df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+ 3158df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) { 3159df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Value *Val0, *Val1; LocTy Loc0, Loc1; 3160df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner SmallVector<unsigned, 4> Indices; 3161df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (ParseTypeAndValue(Val0, Loc0, PFS) || 3162df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseToken(lltok::comma, "expected comma after insertvalue operand") || 3163df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseTypeAndValue(Val1, Loc1, PFS) || 3164df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner ParseIndexList(Indices)) 3165df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return true; 3166df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3167df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!isa<StructType>(Val0->getType()) && !isa<ArrayType>(Val0->getType())) 3168df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc0, "extractvalue operand must be array or struct"); 3169df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner 3170df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(), 3171df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Indices.end())) 3172df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return Error(Loc0, "invalid indices for insertvalue"); 3173df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner Inst = InsertValueInst::Create(Val0, Val1, Indices.begin(), Indices.end()); 3174df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner return false; 3175df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner} 3176