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