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