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