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 | eff2ab6 | 2009-07-29 00:34:02 +0000 | [diff] [blame] | 128 | case lltok::NamedMD: 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() { |
| 464 | assert(Lex.getKind() == lltok::NamedMD); |
| 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 | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 1028 | /// ParseOptionalDbgInfo |
| 1029 | /// ::= /* empty */ |
| 1030 | /// ::= 'dbg' !42 |
| 1031 | bool LLParser::ParseOptionalDbgInfo() { |
| 1032 | |
| 1033 | if (!EatIfPresent(lltok::kw_dbg)) |
| 1034 | return false; |
| 1035 | if (Lex.getKind() != lltok::Metadata) |
| 1036 | return TokError("Expected '!' here"); |
| 1037 | Lex.Lex(); |
| 1038 | MetadataBase *Node; |
| 1039 | if (ParseMDNode(Node)) return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1040 | |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 1041 | Metadata &TheMetadata = M->getContext().getMetadata(); |
| 1042 | unsigned MDDbgKind = TheMetadata.getMDKind("dbg"); |
| 1043 | if (!MDDbgKind) |
| 1044 | MDDbgKind = TheMetadata.RegisterMDKind("dbg"); |
| 1045 | MDsOnInst.push_back(std::make_pair(MDDbgKind, cast<MDNode>(Node))); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1046 | |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 1047 | return false; |
| 1048 | } |
| 1049 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1050 | /// ParseOptionalAlignment |
| 1051 | /// ::= /* empty */ |
| 1052 | /// ::= 'align' 4 |
| 1053 | bool LLParser::ParseOptionalAlignment(unsigned &Alignment) { |
| 1054 | Alignment = 0; |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1055 | if (!EatIfPresent(lltok::kw_align)) |
| 1056 | return false; |
Chris Lattner | 3fbb3ab | 2009-01-05 07:46:05 +0000 | [diff] [blame] | 1057 | LocTy AlignLoc = Lex.getLoc(); |
| 1058 | if (ParseUInt32(Alignment)) return true; |
| 1059 | if (!isPowerOf2_32(Alignment)) |
| 1060 | return Error(AlignLoc, "alignment is not a power of two"); |
| 1061 | return false; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 1064 | /// ParseOptionalInfo |
| 1065 | /// ::= OptionalInfo (',' OptionalInfo)+ |
| 1066 | bool LLParser::ParseOptionalInfo(unsigned &Alignment) { |
| 1067 | |
| 1068 | // FIXME: Handle customized metadata info attached with an instruction. |
| 1069 | do { |
| 1070 | if (Lex.getKind() == lltok::kw_dbg) { |
| 1071 | if (ParseOptionalDbgInfo()) return true; |
| 1072 | } else if (Lex.getKind() == lltok::kw_align) { |
| 1073 | if (ParseOptionalAlignment(Alignment)) return true; |
| 1074 | } else |
| 1075 | return true; |
| 1076 | } while (EatIfPresent(lltok::comma)); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1077 | |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 1078 | return false; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 1081 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1082 | /// ParseIndexList |
| 1083 | /// ::= (',' uint32)+ |
| 1084 | bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices) { |
| 1085 | if (Lex.getKind() != lltok::comma) |
| 1086 | return TokError("expected ',' as start of index list"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1087 | |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1088 | while (EatIfPresent(lltok::comma)) { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1089 | unsigned Idx; |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1090 | if (ParseUInt32(Idx)) return true; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1091 | Indices.push_back(Idx); |
| 1092 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1093 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1094 | return false; |
| 1095 | } |
| 1096 | |
| 1097 | //===----------------------------------------------------------------------===// |
| 1098 | // Type Parsing. |
| 1099 | //===----------------------------------------------------------------------===// |
| 1100 | |
| 1101 | /// ParseType - Parse and resolve a full type. |
Chris Lattner | a9a9e07 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 1102 | bool LLParser::ParseType(PATypeHolder &Result, bool AllowVoid) { |
| 1103 | LocTy TypeLoc = Lex.getLoc(); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1104 | if (ParseTypeRec(Result)) return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1105 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1106 | // Verify no unresolved uprefs. |
| 1107 | if (!UpRefs.empty()) |
| 1108 | return Error(UpRefs.back().Loc, "invalid unresolved type up reference"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1109 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1110 | if (!AllowVoid && Result.get() == Type::getVoidTy(Context)) |
Chris Lattner | a9a9e07 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 1111 | return Error(TypeLoc, "void type only allowed for function results"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1112 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1113 | return false; |
| 1114 | } |
| 1115 | |
| 1116 | /// HandleUpRefs - Every time we finish a new layer of types, this function is |
| 1117 | /// called. It loops through the UpRefs vector, which is a list of the |
| 1118 | /// currently active types. For each type, if the up-reference is contained in |
| 1119 | /// the newly completed type, we decrement the level count. When the level |
| 1120 | /// count reaches zero, the up-referenced type is the type that is passed in: |
| 1121 | /// thus we can complete the cycle. |
| 1122 | /// |
| 1123 | PATypeHolder LLParser::HandleUpRefs(const Type *ty) { |
| 1124 | // If Ty isn't abstract, or if there are no up-references in it, then there is |
| 1125 | // nothing to resolve here. |
| 1126 | if (!ty->isAbstract() || UpRefs.empty()) return ty; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1127 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1128 | PATypeHolder Ty(ty); |
| 1129 | #if 0 |
| 1130 | errs() << "Type '" << Ty->getDescription() |
| 1131 | << "' newly formed. Resolving upreferences.\n" |
| 1132 | << UpRefs.size() << " upreferences active!\n"; |
| 1133 | #endif |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1134 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1135 | // If we find any resolvable upreferences (i.e., those whose NestingLevel goes |
| 1136 | // to zero), we resolve them all together before we resolve them to Ty. At |
| 1137 | // the end of the loop, if there is anything to resolve to Ty, it will be in |
| 1138 | // this variable. |
| 1139 | OpaqueType *TypeToResolve = 0; |
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 | for (unsigned i = 0; i != UpRefs.size(); ++i) { |
| 1142 | // Determine if 'Ty' directly contains this up-references 'LastContainedTy'. |
| 1143 | bool ContainsType = |
| 1144 | std::find(Ty->subtype_begin(), Ty->subtype_end(), |
| 1145 | UpRefs[i].LastContainedTy) != Ty->subtype_end(); |
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 | #if 0 |
| 1148 | errs() << " UR#" << i << " - TypeContains(" << Ty->getDescription() << ", " |
| 1149 | << UpRefs[i].LastContainedTy->getDescription() << ") = " |
| 1150 | << (ContainsType ? "true" : "false") |
| 1151 | << " level=" << UpRefs[i].NestingLevel << "\n"; |
| 1152 | #endif |
| 1153 | if (!ContainsType) |
| 1154 | continue; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1155 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1156 | // Decrement level of upreference |
| 1157 | unsigned Level = --UpRefs[i].NestingLevel; |
| 1158 | UpRefs[i].LastContainedTy = Ty; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1159 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1160 | // If the Up-reference has a non-zero level, it shouldn't be resolved yet. |
| 1161 | if (Level != 0) |
| 1162 | continue; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1163 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1164 | #if 0 |
| 1165 | errs() << " * Resolving upreference for " << UpRefs[i].UpRefTy << "\n"; |
| 1166 | #endif |
| 1167 | if (!TypeToResolve) |
| 1168 | TypeToResolve = UpRefs[i].UpRefTy; |
| 1169 | else |
| 1170 | UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve); |
| 1171 | UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list. |
| 1172 | --i; // Do not skip the next element. |
| 1173 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1174 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1175 | if (TypeToResolve) |
| 1176 | TypeToResolve->refineAbstractTypeTo(Ty); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1177 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1178 | return Ty; |
| 1179 | } |
| 1180 | |
| 1181 | |
| 1182 | /// ParseTypeRec - The recursive function used to process the internal |
| 1183 | /// implementation details of types. |
| 1184 | bool LLParser::ParseTypeRec(PATypeHolder &Result) { |
| 1185 | switch (Lex.getKind()) { |
| 1186 | default: |
| 1187 | return TokError("expected type"); |
| 1188 | case lltok::Type: |
| 1189 | // TypeRec ::= 'float' | 'void' (etc) |
| 1190 | Result = Lex.getTyVal(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1191 | Lex.Lex(); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1192 | break; |
| 1193 | case lltok::kw_opaque: |
| 1194 | // TypeRec ::= 'opaque' |
Owen Anderson | 0e275dc | 2009-08-13 23:27:32 +0000 | [diff] [blame] | 1195 | Result = OpaqueType::get(Context); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1196 | Lex.Lex(); |
| 1197 | break; |
| 1198 | case lltok::lbrace: |
| 1199 | // TypeRec ::= '{' ... '}' |
| 1200 | if (ParseStructType(Result, false)) |
| 1201 | return true; |
| 1202 | break; |
| 1203 | case lltok::lsquare: |
| 1204 | // TypeRec ::= '[' ... ']' |
| 1205 | Lex.Lex(); // eat the lsquare. |
| 1206 | if (ParseArrayVectorType(Result, false)) |
| 1207 | return true; |
| 1208 | break; |
| 1209 | case lltok::less: // Either vector or packed struct. |
| 1210 | // TypeRec ::= '<' ... '>' |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1211 | Lex.Lex(); |
| 1212 | if (Lex.getKind() == lltok::lbrace) { |
| 1213 | if (ParseStructType(Result, true) || |
| 1214 | ParseToken(lltok::greater, "expected '>' at end of packed struct")) |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1215 | return true; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1216 | } else if (ParseArrayVectorType(Result, true)) |
| 1217 | return true; |
| 1218 | break; |
| 1219 | case lltok::LocalVar: |
| 1220 | case lltok::StringConstant: // FIXME: REMOVE IN LLVM 3.0 |
| 1221 | // TypeRec ::= %foo |
| 1222 | if (const Type *T = M->getTypeByName(Lex.getStrVal())) { |
| 1223 | Result = T; |
| 1224 | } else { |
Owen Anderson | 0e275dc | 2009-08-13 23:27:32 +0000 | [diff] [blame] | 1225 | Result = OpaqueType::get(Context); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1226 | ForwardRefTypes.insert(std::make_pair(Lex.getStrVal(), |
| 1227 | std::make_pair(Result, |
| 1228 | Lex.getLoc()))); |
| 1229 | M->addTypeName(Lex.getStrVal(), Result.get()); |
| 1230 | } |
| 1231 | Lex.Lex(); |
| 1232 | break; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1233 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1234 | case lltok::LocalVarID: |
| 1235 | // TypeRec ::= %4 |
| 1236 | if (Lex.getUIntVal() < NumberedTypes.size()) |
| 1237 | Result = NumberedTypes[Lex.getUIntVal()]; |
| 1238 | else { |
| 1239 | std::map<unsigned, std::pair<PATypeHolder, LocTy> >::iterator |
| 1240 | I = ForwardRefTypeIDs.find(Lex.getUIntVal()); |
| 1241 | if (I != ForwardRefTypeIDs.end()) |
| 1242 | Result = I->second.first; |
| 1243 | else { |
Owen Anderson | 0e275dc | 2009-08-13 23:27:32 +0000 | [diff] [blame] | 1244 | Result = OpaqueType::get(Context); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1245 | ForwardRefTypeIDs.insert(std::make_pair(Lex.getUIntVal(), |
| 1246 | std::make_pair(Result, |
| 1247 | Lex.getLoc()))); |
| 1248 | } |
| 1249 | } |
| 1250 | Lex.Lex(); |
| 1251 | break; |
| 1252 | case lltok::backslash: { |
| 1253 | // TypeRec ::= '\' 4 |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1254 | Lex.Lex(); |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1255 | unsigned Val; |
| 1256 | if (ParseUInt32(Val)) return true; |
Owen Anderson | 0e275dc | 2009-08-13 23:27:32 +0000 | [diff] [blame] | 1257 | OpaqueType *OT = OpaqueType::get(Context); //Use temporary placeholder. |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1258 | UpRefs.push_back(UpRefRecord(Lex.getLoc(), Val, OT)); |
| 1259 | Result = OT; |
| 1260 | break; |
| 1261 | } |
| 1262 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1263 | |
| 1264 | // Parse the type suffixes. |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1265 | while (1) { |
| 1266 | switch (Lex.getKind()) { |
| 1267 | // End of type. |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1268 | default: return false; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1269 | |
| 1270 | // TypeRec ::= TypeRec '*' |
| 1271 | case lltok::star: |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1272 | if (Result.get() == Type::getLabelTy(Context)) |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1273 | return TokError("basic block pointers are invalid"); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1274 | if (Result.get() == Type::getVoidTy(Context)) |
Dan Gohman | b9070d3 | 2009-02-09 17:41:21 +0000 | [diff] [blame] | 1275 | return TokError("pointers to void are invalid; use i8* instead"); |
Nick Lewycky | a5f54a0 | 2009-06-07 07:26:46 +0000 | [diff] [blame] | 1276 | if (!PointerType::isValidElementType(Result.get())) |
| 1277 | return TokError("pointer to this type is invalid"); |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1278 | Result = HandleUpRefs(PointerType::getUnqual(Result.get())); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1279 | Lex.Lex(); |
| 1280 | break; |
| 1281 | |
| 1282 | // TypeRec ::= TypeRec 'addrspace' '(' uint32 ')' '*' |
| 1283 | case lltok::kw_addrspace: { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1284 | if (Result.get() == Type::getLabelTy(Context)) |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1285 | return TokError("basic block pointers are invalid"); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1286 | if (Result.get() == Type::getVoidTy(Context)) |
Dan Gohman | b9070d3 | 2009-02-09 17:41:21 +0000 | [diff] [blame] | 1287 | return TokError("pointers to void are invalid; use i8* instead"); |
Nick Lewycky | a5f54a0 | 2009-06-07 07:26:46 +0000 | [diff] [blame] | 1288 | if (!PointerType::isValidElementType(Result.get())) |
| 1289 | return TokError("pointer to this type is invalid"); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1290 | unsigned AddrSpace; |
| 1291 | if (ParseOptionalAddrSpace(AddrSpace) || |
| 1292 | ParseToken(lltok::star, "expected '*' in address space")) |
| 1293 | return true; |
| 1294 | |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1295 | Result = HandleUpRefs(PointerType::get(Result.get(), AddrSpace)); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1296 | break; |
| 1297 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1298 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1299 | /// Types '(' ArgTypeListI ')' OptFuncAttrs |
| 1300 | case lltok::lparen: |
| 1301 | if (ParseFunctionType(Result)) |
| 1302 | return true; |
| 1303 | break; |
| 1304 | } |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | /// ParseParameterList |
| 1309 | /// ::= '(' ')' |
| 1310 | /// ::= '(' Arg (',' Arg)* ')' |
| 1311 | /// Arg |
| 1312 | /// ::= Type OptionalAttributes Value OptionalAttributes |
| 1313 | bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList, |
| 1314 | PerFunctionState &PFS) { |
| 1315 | if (ParseToken(lltok::lparen, "expected '(' in call")) |
| 1316 | return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1317 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1318 | while (Lex.getKind() != lltok::rparen) { |
| 1319 | // If this isn't the first argument, we need a comma. |
| 1320 | if (!ArgList.empty() && |
| 1321 | ParseToken(lltok::comma, "expected ',' in argument list")) |
| 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 | // Parse the argument. |
| 1325 | LocTy ArgLoc; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1326 | PATypeHolder ArgTy(Type::getVoidTy(Context)); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1327 | unsigned ArgAttrs1, ArgAttrs2; |
| 1328 | Value *V; |
| 1329 | if (ParseType(ArgTy, ArgLoc) || |
| 1330 | ParseOptionalAttrs(ArgAttrs1, 0) || |
| 1331 | ParseValue(ArgTy, V, PFS) || |
| 1332 | // FIXME: Should not allow attributes after the argument, remove this in |
| 1333 | // LLVM 3.0. |
Chris Lattner | ad9ad7c | 2009-03-25 06:36:36 +0000 | [diff] [blame] | 1334 | ParseOptionalAttrs(ArgAttrs2, 3)) |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1335 | return true; |
| 1336 | ArgList.push_back(ParamInfo(ArgLoc, V, ArgAttrs1|ArgAttrs2)); |
| 1337 | } |
| 1338 | |
| 1339 | Lex.Lex(); // Lex the ')'. |
| 1340 | return false; |
| 1341 | } |
| 1342 | |
| 1343 | |
| 1344 | |
Chris Lattner | dfd19dd | 2009-01-05 18:34:07 +0000 | [diff] [blame] | 1345 | /// ParseArgumentList - Parse the argument list for a function type or function |
| 1346 | /// prototype. If 'inType' is true then we are parsing a FunctionType. |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1347 | /// ::= '(' ArgTypeListI ')' |
| 1348 | /// ArgTypeListI |
| 1349 | /// ::= /*empty*/ |
| 1350 | /// ::= '...' |
| 1351 | /// ::= ArgTypeList ',' '...' |
| 1352 | /// ::= ArgType (',' ArgType)* |
Chris Lattner | dfd19dd | 2009-01-05 18:34:07 +0000 | [diff] [blame] | 1353 | /// |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1354 | bool LLParser::ParseArgumentList(std::vector<ArgInfo> &ArgList, |
Chris Lattner | dfd19dd | 2009-01-05 18:34:07 +0000 | [diff] [blame] | 1355 | bool &isVarArg, bool inType) { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1356 | isVarArg = false; |
| 1357 | assert(Lex.getKind() == lltok::lparen); |
| 1358 | Lex.Lex(); // eat the (. |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1359 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1360 | if (Lex.getKind() == lltok::rparen) { |
| 1361 | // empty |
| 1362 | } else if (Lex.getKind() == lltok::dotdotdot) { |
| 1363 | isVarArg = true; |
| 1364 | Lex.Lex(); |
| 1365 | } else { |
| 1366 | LocTy TypeLoc = Lex.getLoc(); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1367 | PATypeHolder ArgTy(Type::getVoidTy(Context)); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1368 | unsigned Attrs; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1369 | std::string Name; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1370 | |
Chris Lattner | dfd19dd | 2009-01-05 18:34:07 +0000 | [diff] [blame] | 1371 | // If we're parsing a type, use ParseTypeRec, because we allow recursive |
| 1372 | // types (such as a function returning a pointer to itself). If parsing a |
| 1373 | // function prototype, we require fully resolved types. |
| 1374 | if ((inType ? ParseTypeRec(ArgTy) : ParseType(ArgTy)) || |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1375 | ParseOptionalAttrs(Attrs, 0)) return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1376 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1377 | if (ArgTy == Type::getVoidTy(Context)) |
Chris Lattner | a9a9e07 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 1378 | return Error(TypeLoc, "argument can not have void type"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1379 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1380 | if (Lex.getKind() == lltok::LocalVar || |
| 1381 | Lex.getKind() == lltok::StringConstant) { // FIXME: REMOVE IN LLVM 3.0 |
| 1382 | Name = Lex.getStrVal(); |
| 1383 | Lex.Lex(); |
| 1384 | } |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1385 | |
Nick Lewycky | a5f54a0 | 2009-06-07 07:26:46 +0000 | [diff] [blame] | 1386 | if (!FunctionType::isValidArgumentType(ArgTy)) |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1387 | return Error(TypeLoc, "invalid type for function argument"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1388 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1389 | ArgList.push_back(ArgInfo(TypeLoc, ArgTy, Attrs, Name)); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1390 | |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1391 | while (EatIfPresent(lltok::comma)) { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1392 | // Handle ... at end of arg list. |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1393 | if (EatIfPresent(lltok::dotdotdot)) { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1394 | isVarArg = true; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1395 | break; |
| 1396 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1397 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1398 | // Otherwise must be an argument type. |
| 1399 | TypeLoc = Lex.getLoc(); |
Chris Lattner | a9a9e07 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 1400 | if ((inType ? ParseTypeRec(ArgTy) : ParseType(ArgTy)) || |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1401 | ParseOptionalAttrs(Attrs, 0)) return true; |
| 1402 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1403 | if (ArgTy == Type::getVoidTy(Context)) |
Chris Lattner | a9a9e07 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 1404 | return Error(TypeLoc, "argument can not have void type"); |
| 1405 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1406 | if (Lex.getKind() == lltok::LocalVar || |
| 1407 | Lex.getKind() == lltok::StringConstant) { // FIXME: REMOVE IN LLVM 3.0 |
| 1408 | Name = Lex.getStrVal(); |
| 1409 | Lex.Lex(); |
| 1410 | } else { |
| 1411 | Name = ""; |
| 1412 | } |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1413 | |
| 1414 | if (!ArgTy->isFirstClassType() && !isa<OpaqueType>(ArgTy)) |
| 1415 | return Error(TypeLoc, "invalid type for function argument"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1416 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1417 | ArgList.push_back(ArgInfo(TypeLoc, ArgTy, Attrs, Name)); |
| 1418 | } |
| 1419 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1420 | |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1421 | return ParseToken(lltok::rparen, "expected ')' at end of argument list"); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1422 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1423 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1424 | /// ParseFunctionType |
| 1425 | /// ::= Type ArgumentList OptionalAttrs |
| 1426 | bool LLParser::ParseFunctionType(PATypeHolder &Result) { |
| 1427 | assert(Lex.getKind() == lltok::lparen); |
| 1428 | |
Chris Lattner | d77d04c | 2009-01-05 08:04:33 +0000 | [diff] [blame] | 1429 | if (!FunctionType::isValidReturnType(Result)) |
| 1430 | return TokError("invalid function return type"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1431 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1432 | std::vector<ArgInfo> ArgList; |
| 1433 | bool isVarArg; |
| 1434 | unsigned Attrs; |
Chris Lattner | dfd19dd | 2009-01-05 18:34:07 +0000 | [diff] [blame] | 1435 | if (ParseArgumentList(ArgList, isVarArg, true) || |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1436 | // FIXME: Allow, but ignore attributes on function types! |
| 1437 | // FIXME: Remove in LLVM 3.0 |
| 1438 | ParseOptionalAttrs(Attrs, 2)) |
| 1439 | return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1440 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1441 | // Reject names on the arguments lists. |
| 1442 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { |
| 1443 | if (!ArgList[i].Name.empty()) |
| 1444 | return Error(ArgList[i].Loc, "argument name invalid in function type"); |
| 1445 | if (!ArgList[i].Attrs != 0) { |
| 1446 | // Allow but ignore attributes on function types; this permits |
| 1447 | // auto-upgrade. |
| 1448 | // FIXME: REJECT ATTRIBUTES ON FUNCTION TYPES in LLVM 3.0 |
| 1449 | } |
| 1450 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1451 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1452 | std::vector<const Type*> ArgListTy; |
| 1453 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) |
| 1454 | ArgListTy.push_back(ArgList[i].Type); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1455 | |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1456 | Result = HandleUpRefs(FunctionType::get(Result.get(), |
Owen Anderson | fba933c | 2009-07-01 23:57:11 +0000 | [diff] [blame] | 1457 | ArgListTy, isVarArg)); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1458 | return false; |
| 1459 | } |
| 1460 | |
| 1461 | /// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere. |
| 1462 | /// TypeRec |
| 1463 | /// ::= '{' '}' |
| 1464 | /// ::= '{' TypeRec (',' TypeRec)* '}' |
| 1465 | /// ::= '<' '{' '}' '>' |
| 1466 | /// ::= '<' '{' TypeRec (',' TypeRec)* '}' '>' |
| 1467 | bool LLParser::ParseStructType(PATypeHolder &Result, bool Packed) { |
| 1468 | assert(Lex.getKind() == lltok::lbrace); |
| 1469 | Lex.Lex(); // Consume the '{' |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1470 | |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1471 | if (EatIfPresent(lltok::rbrace)) { |
Owen Anderson | d7f2a6c | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 1472 | Result = StructType::get(Context, Packed); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1473 | return false; |
| 1474 | } |
| 1475 | |
| 1476 | std::vector<PATypeHolder> ParamsList; |
Chris Lattner | a9a9e07 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 1477 | LocTy EltTyLoc = Lex.getLoc(); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1478 | if (ParseTypeRec(Result)) return true; |
| 1479 | ParamsList.push_back(Result); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1480 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1481 | if (Result == Type::getVoidTy(Context)) |
Chris Lattner | a9a9e07 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 1482 | return Error(EltTyLoc, "struct element can not have void type"); |
Nick Lewycky | a5f54a0 | 2009-06-07 07:26:46 +0000 | [diff] [blame] | 1483 | if (!StructType::isValidElementType(Result)) |
| 1484 | return Error(EltTyLoc, "invalid element type for struct"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1485 | |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1486 | while (EatIfPresent(lltok::comma)) { |
Chris Lattner | a9a9e07 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 1487 | EltTyLoc = Lex.getLoc(); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1488 | if (ParseTypeRec(Result)) return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1489 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1490 | if (Result == Type::getVoidTy(Context)) |
Chris Lattner | a9a9e07 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 1491 | return Error(EltTyLoc, "struct element can not have void type"); |
Nick Lewycky | a5f54a0 | 2009-06-07 07:26:46 +0000 | [diff] [blame] | 1492 | if (!StructType::isValidElementType(Result)) |
| 1493 | return Error(EltTyLoc, "invalid element type for struct"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1494 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1495 | ParamsList.push_back(Result); |
| 1496 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1497 | |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1498 | if (ParseToken(lltok::rbrace, "expected '}' at end of struct")) |
| 1499 | return true; |
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 | std::vector<const Type*> ParamsListTy; |
| 1502 | for (unsigned i = 0, e = ParamsList.size(); i != e; ++i) |
| 1503 | ParamsListTy.push_back(ParamsList[i].get()); |
Owen Anderson | d7f2a6c | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 1504 | Result = HandleUpRefs(StructType::get(Context, ParamsListTy, Packed)); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1505 | return false; |
| 1506 | } |
| 1507 | |
| 1508 | /// ParseArrayVectorType - Parse an array or vector type, assuming the first |
| 1509 | /// token has already been consumed. |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1510 | /// TypeRec |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1511 | /// ::= '[' APSINTVAL 'x' Types ']' |
| 1512 | /// ::= '<' APSINTVAL 'x' Types '>' |
| 1513 | bool LLParser::ParseArrayVectorType(PATypeHolder &Result, bool isVector) { |
| 1514 | if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() || |
| 1515 | Lex.getAPSIntVal().getBitWidth() > 64) |
| 1516 | return TokError("expected number in address space"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1517 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1518 | LocTy SizeLoc = Lex.getLoc(); |
| 1519 | uint64_t Size = Lex.getAPSIntVal().getZExtValue(); |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1520 | Lex.Lex(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1521 | |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1522 | if (ParseToken(lltok::kw_x, "expected 'x' after element count")) |
| 1523 | return true; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1524 | |
| 1525 | LocTy TypeLoc = Lex.getLoc(); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1526 | PATypeHolder EltTy(Type::getVoidTy(Context)); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1527 | if (ParseTypeRec(EltTy)) return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1528 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1529 | if (EltTy == Type::getVoidTy(Context)) |
Chris Lattner | a9a9e07 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 1530 | return Error(TypeLoc, "array and vector element type cannot be void"); |
| 1531 | |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1532 | if (ParseToken(isVector ? lltok::greater : lltok::rsquare, |
| 1533 | "expected end of sequential type")) |
| 1534 | return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1535 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1536 | if (isVector) { |
Chris Lattner | 452e262 | 2009-02-28 18:12:41 +0000 | [diff] [blame] | 1537 | if (Size == 0) |
| 1538 | return Error(SizeLoc, "zero element vector is illegal"); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1539 | if ((unsigned)Size != Size) |
| 1540 | return Error(SizeLoc, "size too large for vector"); |
Nick Lewycky | a5f54a0 | 2009-06-07 07:26:46 +0000 | [diff] [blame] | 1541 | if (!VectorType::isValidElementType(EltTy)) |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1542 | return Error(TypeLoc, "vector element type must be fp or integer"); |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1543 | Result = VectorType::get(EltTy, unsigned(Size)); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1544 | } else { |
Nick Lewycky | a5f54a0 | 2009-06-07 07:26:46 +0000 | [diff] [blame] | 1545 | if (!ArrayType::isValidElementType(EltTy)) |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1546 | return Error(TypeLoc, "invalid array element type"); |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1547 | Result = HandleUpRefs(ArrayType::get(EltTy, Size)); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1548 | } |
| 1549 | return false; |
| 1550 | } |
| 1551 | |
| 1552 | //===----------------------------------------------------------------------===// |
| 1553 | // Function Semantic Analysis. |
| 1554 | //===----------------------------------------------------------------------===// |
| 1555 | |
| 1556 | LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f) |
| 1557 | : P(p), F(f) { |
| 1558 | |
| 1559 | // Insert unnamed arguments into the NumberedVals list. |
| 1560 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); |
| 1561 | AI != E; ++AI) |
| 1562 | if (!AI->hasName()) |
| 1563 | NumberedVals.push_back(AI); |
| 1564 | } |
| 1565 | |
| 1566 | LLParser::PerFunctionState::~PerFunctionState() { |
| 1567 | // If there were any forward referenced non-basicblock values, delete them. |
| 1568 | for (std::map<std::string, std::pair<Value*, LocTy> >::iterator |
| 1569 | I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I) |
| 1570 | if (!isa<BasicBlock>(I->second.first)) { |
Owen Anderson | b43eae7 | 2009-07-02 17:04:01 +0000 | [diff] [blame] | 1571 | I->second.first->replaceAllUsesWith( |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1572 | UndefValue::get(I->second.first->getType())); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1573 | delete I->second.first; |
| 1574 | I->second.first = 0; |
| 1575 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1576 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1577 | for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator |
| 1578 | I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I) |
| 1579 | if (!isa<BasicBlock>(I->second.first)) { |
Owen Anderson | b43eae7 | 2009-07-02 17:04:01 +0000 | [diff] [blame] | 1580 | I->second.first->replaceAllUsesWith( |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1581 | UndefValue::get(I->second.first->getType())); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1582 | delete I->second.first; |
| 1583 | I->second.first = 0; |
| 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | bool LLParser::PerFunctionState::VerifyFunctionComplete() { |
| 1588 | if (!ForwardRefVals.empty()) |
| 1589 | return P.Error(ForwardRefVals.begin()->second.second, |
| 1590 | "use of undefined value '%" + ForwardRefVals.begin()->first + |
| 1591 | "'"); |
| 1592 | if (!ForwardRefValIDs.empty()) |
| 1593 | return P.Error(ForwardRefValIDs.begin()->second.second, |
| 1594 | "use of undefined value '%" + |
| 1595 | utostr(ForwardRefValIDs.begin()->first) + "'"); |
| 1596 | return false; |
| 1597 | } |
| 1598 | |
| 1599 | |
| 1600 | /// GetVal - Get a value with the specified name or ID, creating a |
| 1601 | /// forward reference record if needed. This can return null if the value |
| 1602 | /// exists but does not have the right type. |
| 1603 | Value *LLParser::PerFunctionState::GetVal(const std::string &Name, |
| 1604 | const Type *Ty, LocTy Loc) { |
| 1605 | // Look this name up in the normal function symbol table. |
| 1606 | Value *Val = F.getValueSymbolTable().lookup(Name); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1607 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1608 | // If this is a forward reference for the value, see if we already created a |
| 1609 | // forward ref record. |
| 1610 | if (Val == 0) { |
| 1611 | std::map<std::string, std::pair<Value*, LocTy> >::iterator |
| 1612 | I = ForwardRefVals.find(Name); |
| 1613 | if (I != ForwardRefVals.end()) |
| 1614 | Val = I->second.first; |
| 1615 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1616 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1617 | // If we have the value in the symbol table or fwd-ref table, return it. |
| 1618 | if (Val) { |
| 1619 | if (Val->getType() == Ty) return Val; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1620 | if (Ty == Type::getLabelTy(F.getContext())) |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1621 | P.Error(Loc, "'%" + Name + "' is not a basic block"); |
| 1622 | else |
| 1623 | P.Error(Loc, "'%" + Name + "' defined with type '" + |
| 1624 | Val->getType()->getDescription() + "'"); |
| 1625 | return 0; |
| 1626 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1627 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1628 | // Don't make placeholders with invalid type. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1629 | if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty) && |
| 1630 | Ty != Type::getLabelTy(F.getContext())) { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1631 | P.Error(Loc, "invalid use of a non-first-class type"); |
| 1632 | return 0; |
| 1633 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1634 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1635 | // Otherwise, create a new forward reference for this value and remember it. |
| 1636 | Value *FwdVal; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1637 | if (Ty == Type::getLabelTy(F.getContext())) |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1638 | FwdVal = BasicBlock::Create(F.getContext(), Name, &F); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1639 | else |
| 1640 | FwdVal = new Argument(Ty, Name); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1641 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1642 | ForwardRefVals[Name] = std::make_pair(FwdVal, Loc); |
| 1643 | return FwdVal; |
| 1644 | } |
| 1645 | |
| 1646 | Value *LLParser::PerFunctionState::GetVal(unsigned ID, const Type *Ty, |
| 1647 | LocTy Loc) { |
| 1648 | // Look this name up in the normal function symbol table. |
| 1649 | Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1650 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1651 | // If this is a forward reference for the value, see if we already created a |
| 1652 | // forward ref record. |
| 1653 | if (Val == 0) { |
| 1654 | std::map<unsigned, std::pair<Value*, LocTy> >::iterator |
| 1655 | I = ForwardRefValIDs.find(ID); |
| 1656 | if (I != ForwardRefValIDs.end()) |
| 1657 | Val = I->second.first; |
| 1658 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1659 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1660 | // If we have the value in the symbol table or fwd-ref table, return it. |
| 1661 | if (Val) { |
| 1662 | if (Val->getType() == Ty) return Val; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1663 | if (Ty == Type::getLabelTy(F.getContext())) |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1664 | P.Error(Loc, "'%" + utostr(ID) + "' is not a basic block"); |
| 1665 | else |
| 1666 | P.Error(Loc, "'%" + utostr(ID) + "' defined with type '" + |
| 1667 | Val->getType()->getDescription() + "'"); |
| 1668 | return 0; |
| 1669 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1670 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1671 | if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty) && |
| 1672 | Ty != Type::getLabelTy(F.getContext())) { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1673 | P.Error(Loc, "invalid use of a non-first-class type"); |
| 1674 | return 0; |
| 1675 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1676 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1677 | // Otherwise, create a new forward reference for this value and remember it. |
| 1678 | Value *FwdVal; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1679 | if (Ty == Type::getLabelTy(F.getContext())) |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1680 | FwdVal = BasicBlock::Create(F.getContext(), "", &F); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1681 | else |
| 1682 | FwdVal = new Argument(Ty); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1683 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1684 | ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc); |
| 1685 | return FwdVal; |
| 1686 | } |
| 1687 | |
| 1688 | /// SetInstName - After an instruction is parsed and inserted into its |
| 1689 | /// basic block, this installs its name. |
| 1690 | bool LLParser::PerFunctionState::SetInstName(int NameID, |
| 1691 | const std::string &NameStr, |
| 1692 | LocTy NameLoc, Instruction *Inst) { |
| 1693 | // 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] | 1694 | if (Inst->getType() == Type::getVoidTy(F.getContext())) { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1695 | if (NameID != -1 || !NameStr.empty()) |
| 1696 | return P.Error(NameLoc, "instructions returning void cannot have a name"); |
| 1697 | return false; |
| 1698 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1699 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1700 | // If this was a numbered instruction, verify that the instruction is the |
| 1701 | // expected value and resolve any forward references. |
| 1702 | if (NameStr.empty()) { |
| 1703 | // If neither a name nor an ID was specified, just use the next ID. |
| 1704 | if (NameID == -1) |
| 1705 | NameID = NumberedVals.size(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1706 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1707 | if (unsigned(NameID) != NumberedVals.size()) |
| 1708 | return P.Error(NameLoc, "instruction expected to be numbered '%" + |
| 1709 | utostr(NumberedVals.size()) + "'"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1710 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1711 | std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI = |
| 1712 | ForwardRefValIDs.find(NameID); |
| 1713 | if (FI != ForwardRefValIDs.end()) { |
| 1714 | if (FI->second.first->getType() != Inst->getType()) |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1715 | return P.Error(NameLoc, "instruction forward referenced with type '" + |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1716 | FI->second.first->getType()->getDescription() + "'"); |
| 1717 | FI->second.first->replaceAllUsesWith(Inst); |
Nuno Lopes | 2b4f28e | 2009-09-02 15:02:57 +0000 | [diff] [blame] | 1718 | delete FI->second.first; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1719 | ForwardRefValIDs.erase(FI); |
| 1720 | } |
| 1721 | |
| 1722 | NumberedVals.push_back(Inst); |
| 1723 | return false; |
| 1724 | } |
| 1725 | |
| 1726 | // Otherwise, the instruction had a name. Resolve forward refs and set it. |
| 1727 | std::map<std::string, std::pair<Value*, LocTy> >::iterator |
| 1728 | FI = ForwardRefVals.find(NameStr); |
| 1729 | if (FI != ForwardRefVals.end()) { |
| 1730 | if (FI->second.first->getType() != Inst->getType()) |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1731 | return P.Error(NameLoc, "instruction forward referenced with type '" + |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1732 | FI->second.first->getType()->getDescription() + "'"); |
| 1733 | FI->second.first->replaceAllUsesWith(Inst); |
Nuno Lopes | 531552a | 2009-09-02 14:22:03 +0000 | [diff] [blame] | 1734 | delete FI->second.first; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1735 | ForwardRefVals.erase(FI); |
| 1736 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1737 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1738 | // Set the name on the instruction. |
| 1739 | Inst->setName(NameStr); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1740 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1741 | if (Inst->getNameStr() != NameStr) |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1742 | return P.Error(NameLoc, "multiple definition of local value named '" + |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1743 | NameStr + "'"); |
| 1744 | return false; |
| 1745 | } |
| 1746 | |
| 1747 | /// GetBB - Get a basic block with the specified name or ID, creating a |
| 1748 | /// forward reference record if needed. |
| 1749 | BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name, |
| 1750 | LocTy Loc) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1751 | return cast_or_null<BasicBlock>(GetVal(Name, |
| 1752 | Type::getLabelTy(F.getContext()), Loc)); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1756 | return cast_or_null<BasicBlock>(GetVal(ID, |
| 1757 | Type::getLabelTy(F.getContext()), Loc)); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | /// DefineBB - Define the specified basic block, which is either named or |
| 1761 | /// unnamed. If there is an error, this returns null otherwise it returns |
| 1762 | /// the block being defined. |
| 1763 | BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name, |
| 1764 | LocTy Loc) { |
| 1765 | BasicBlock *BB; |
| 1766 | if (Name.empty()) |
| 1767 | BB = GetBB(NumberedVals.size(), Loc); |
| 1768 | else |
| 1769 | BB = GetBB(Name, Loc); |
| 1770 | if (BB == 0) return 0; // Already diagnosed error. |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1771 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1772 | // Move the block to the end of the function. Forward ref'd blocks are |
| 1773 | // inserted wherever they happen to be referenced. |
| 1774 | F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1775 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1776 | // Remove the block from forward ref sets. |
| 1777 | if (Name.empty()) { |
| 1778 | ForwardRefValIDs.erase(NumberedVals.size()); |
| 1779 | NumberedVals.push_back(BB); |
| 1780 | } else { |
| 1781 | // BB forward references are already in the function symbol table. |
| 1782 | ForwardRefVals.erase(Name); |
| 1783 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1784 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1785 | return BB; |
| 1786 | } |
| 1787 | |
| 1788 | //===----------------------------------------------------------------------===// |
| 1789 | // Constants. |
| 1790 | //===----------------------------------------------------------------------===// |
| 1791 | |
| 1792 | /// ParseValID - Parse an abstract value that doesn't necessarily have a |
| 1793 | /// type implied. For example, if we parse "4" we don't know what integer type |
| 1794 | /// it has. The value will later be combined with its type and checked for |
| 1795 | /// sanity. |
| 1796 | bool LLParser::ParseValID(ValID &ID) { |
| 1797 | ID.Loc = Lex.getLoc(); |
| 1798 | switch (Lex.getKind()) { |
| 1799 | default: return TokError("expected value token"); |
| 1800 | case lltok::GlobalID: // @42 |
| 1801 | ID.UIntVal = Lex.getUIntVal(); |
| 1802 | ID.Kind = ValID::t_GlobalID; |
| 1803 | break; |
| 1804 | case lltok::GlobalVar: // @foo |
| 1805 | ID.StrVal = Lex.getStrVal(); |
| 1806 | ID.Kind = ValID::t_GlobalName; |
| 1807 | break; |
| 1808 | case lltok::LocalVarID: // %42 |
| 1809 | ID.UIntVal = Lex.getUIntVal(); |
| 1810 | ID.Kind = ValID::t_LocalID; |
| 1811 | break; |
| 1812 | case lltok::LocalVar: // %foo |
| 1813 | case lltok::StringConstant: // "foo" - FIXME: REMOVE IN LLVM 3.0 |
| 1814 | ID.StrVal = Lex.getStrVal(); |
| 1815 | ID.Kind = ValID::t_LocalName; |
| 1816 | break; |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 1817 | case lltok::Metadata: { // !{...} MDNode, !"foo" MDString |
Devang Patel | 104cf9e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 1818 | ID.Kind = ValID::t_Metadata; |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 1819 | Lex.Lex(); |
| 1820 | if (Lex.getKind() == lltok::lbrace) { |
Nick Lewycky | cb33799 | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 1821 | SmallVector<Value*, 16> Elts; |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 1822 | if (ParseMDNodeVector(Elts) || |
| 1823 | ParseToken(lltok::rbrace, "expected end of metadata node")) |
| 1824 | return true; |
Nick Lewycky | cb33799 | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 1825 | |
Owen Anderson | 647e301 | 2009-07-31 21:35:40 +0000 | [diff] [blame] | 1826 | ID.MetadataVal = MDNode::get(Context, Elts.data(), Elts.size()); |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 1827 | return false; |
| 1828 | } |
| 1829 | |
Devang Patel | 923078c | 2009-07-01 19:21:12 +0000 | [diff] [blame] | 1830 | // Standalone metadata reference |
| 1831 | // !{ ..., !42, ... } |
Devang Patel | 104cf9e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 1832 | if (!ParseMDNode(ID.MetadataVal)) |
Devang Patel | 923078c | 2009-07-01 19:21:12 +0000 | [diff] [blame] | 1833 | return false; |
Devang Patel | 256be96 | 2009-07-20 19:00:08 +0000 | [diff] [blame] | 1834 | |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 1835 | // MDString: |
| 1836 | // ::= '!' STRINGCONSTANT |
Devang Patel | e54abc9 | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1837 | if (ParseMDString(ID.MetadataVal)) return true; |
| 1838 | ID.Kind = ValID::t_Metadata; |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 1839 | return false; |
| 1840 | } |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1841 | case lltok::APSInt: |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1842 | ID.APSIntVal = Lex.getAPSIntVal(); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1843 | ID.Kind = ValID::t_APSInt; |
| 1844 | break; |
| 1845 | case lltok::APFloat: |
| 1846 | ID.APFloatVal = Lex.getAPFloatVal(); |
| 1847 | ID.Kind = ValID::t_APFloat; |
| 1848 | break; |
| 1849 | case lltok::kw_true: |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1850 | ID.ConstantVal = ConstantInt::getTrue(Context); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1851 | ID.Kind = ValID::t_Constant; |
| 1852 | break; |
| 1853 | case lltok::kw_false: |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1854 | ID.ConstantVal = ConstantInt::getFalse(Context); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1855 | ID.Kind = ValID::t_Constant; |
| 1856 | break; |
| 1857 | case lltok::kw_null: ID.Kind = ValID::t_Null; break; |
| 1858 | case lltok::kw_undef: ID.Kind = ValID::t_Undef; break; |
| 1859 | case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1860 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1861 | case lltok::lbrace: { |
| 1862 | // ValID ::= '{' ConstVector '}' |
| 1863 | Lex.Lex(); |
| 1864 | SmallVector<Constant*, 16> Elts; |
| 1865 | if (ParseGlobalValueVector(Elts) || |
| 1866 | ParseToken(lltok::rbrace, "expected end of struct constant")) |
| 1867 | return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1868 | |
Owen Anderson | d7f2a6c | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 1869 | ID.ConstantVal = ConstantStruct::get(Context, Elts.data(), |
| 1870 | Elts.size(), false); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1871 | ID.Kind = ValID::t_Constant; |
| 1872 | return false; |
| 1873 | } |
| 1874 | case lltok::less: { |
| 1875 | // ValID ::= '<' ConstVector '>' --> Vector. |
| 1876 | // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct. |
| 1877 | Lex.Lex(); |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1878 | bool isPackedStruct = EatIfPresent(lltok::lbrace); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1879 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1880 | SmallVector<Constant*, 16> Elts; |
| 1881 | LocTy FirstEltLoc = Lex.getLoc(); |
| 1882 | if (ParseGlobalValueVector(Elts) || |
| 1883 | (isPackedStruct && |
| 1884 | ParseToken(lltok::rbrace, "expected end of packed struct")) || |
| 1885 | ParseToken(lltok::greater, "expected end of constant")) |
| 1886 | return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1887 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1888 | if (isPackedStruct) { |
Owen Anderson | fba933c | 2009-07-01 23:57:11 +0000 | [diff] [blame] | 1889 | ID.ConstantVal = |
Owen Anderson | d7f2a6c | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 1890 | ConstantStruct::get(Context, Elts.data(), Elts.size(), true); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1891 | ID.Kind = ValID::t_Constant; |
| 1892 | return false; |
| 1893 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1894 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1895 | if (Elts.empty()) |
| 1896 | return Error(ID.Loc, "constant vector must not be empty"); |
| 1897 | |
| 1898 | if (!Elts[0]->getType()->isInteger() && |
| 1899 | !Elts[0]->getType()->isFloatingPoint()) |
| 1900 | return Error(FirstEltLoc, |
| 1901 | "vector elements must have integer or floating point type"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1902 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1903 | // Verify that all the vector elements have the same type. |
| 1904 | for (unsigned i = 1, e = Elts.size(); i != e; ++i) |
| 1905 | if (Elts[i]->getType() != Elts[0]->getType()) |
| 1906 | return Error(FirstEltLoc, |
| 1907 | "vector element #" + utostr(i) + |
| 1908 | " is not of type '" + Elts[0]->getType()->getDescription()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1909 | |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1910 | ID.ConstantVal = ConstantVector::get(Elts.data(), Elts.size()); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1911 | ID.Kind = ValID::t_Constant; |
| 1912 | return false; |
| 1913 | } |
| 1914 | case lltok::lsquare: { // Array Constant |
| 1915 | Lex.Lex(); |
| 1916 | SmallVector<Constant*, 16> Elts; |
| 1917 | LocTy FirstEltLoc = Lex.getLoc(); |
| 1918 | if (ParseGlobalValueVector(Elts) || |
| 1919 | ParseToken(lltok::rsquare, "expected end of array constant")) |
| 1920 | return true; |
| 1921 | |
| 1922 | // Handle empty element. |
| 1923 | if (Elts.empty()) { |
| 1924 | // Use undef instead of an array because it's inconvenient to determine |
| 1925 | // the element type at this point, there being no elements to examine. |
Chris Lattner | 081b505 | 2009-01-05 07:52:51 +0000 | [diff] [blame] | 1926 | ID.Kind = ValID::t_EmptyArray; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1927 | return false; |
| 1928 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1929 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1930 | if (!Elts[0]->getType()->isFirstClassType()) |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1931 | return Error(FirstEltLoc, "invalid array element type: " + |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1932 | Elts[0]->getType()->getDescription()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1933 | |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1934 | ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size()); |
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 | // Verify all elements are correct type! |
Chris Lattner | 6d6b3cc | 2009-01-02 08:49:06 +0000 | [diff] [blame] | 1937 | for (unsigned i = 0, e = Elts.size(); i != e; ++i) { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1938 | if (Elts[i]->getType() != Elts[0]->getType()) |
| 1939 | return Error(FirstEltLoc, |
| 1940 | "array element #" + utostr(i) + |
| 1941 | " is not of type '" +Elts[0]->getType()->getDescription()); |
| 1942 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1943 | |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 1944 | ID.ConstantVal = ConstantArray::get(ATy, Elts.data(), Elts.size()); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1945 | ID.Kind = ValID::t_Constant; |
| 1946 | return false; |
| 1947 | } |
| 1948 | case lltok::kw_c: // c "foo" |
| 1949 | Lex.Lex(); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1950 | ID.ConstantVal = ConstantArray::get(Context, Lex.getStrVal(), false); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1951 | if (ParseToken(lltok::StringConstant, "expected string")) return true; |
| 1952 | ID.Kind = ValID::t_Constant; |
| 1953 | return false; |
| 1954 | |
| 1955 | case lltok::kw_asm: { |
| 1956 | // ValID ::= 'asm' SideEffect? STRINGCONSTANT ',' STRINGCONSTANT |
| 1957 | bool HasSideEffect; |
| 1958 | Lex.Lex(); |
| 1959 | if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) || |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1960 | ParseStringConstant(ID.StrVal) || |
| 1961 | ParseToken(lltok::comma, "expected comma in inline asm expression") || |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1962 | ParseToken(lltok::StringConstant, "expected constraint string")) |
| 1963 | return true; |
| 1964 | ID.StrVal2 = Lex.getStrVal(); |
| 1965 | ID.UIntVal = HasSideEffect; |
| 1966 | ID.Kind = ValID::t_InlineAsm; |
| 1967 | return false; |
| 1968 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1969 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1970 | case lltok::kw_trunc: |
| 1971 | case lltok::kw_zext: |
| 1972 | case lltok::kw_sext: |
| 1973 | case lltok::kw_fptrunc: |
| 1974 | case lltok::kw_fpext: |
| 1975 | case lltok::kw_bitcast: |
| 1976 | case lltok::kw_uitofp: |
| 1977 | case lltok::kw_sitofp: |
| 1978 | case lltok::kw_fptoui: |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1979 | case lltok::kw_fptosi: |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1980 | case lltok::kw_inttoptr: |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1981 | case lltok::kw_ptrtoint: { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1982 | unsigned Opc = Lex.getUIntVal(); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1983 | PATypeHolder DestTy(Type::getVoidTy(Context)); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1984 | Constant *SrcVal; |
| 1985 | Lex.Lex(); |
| 1986 | if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") || |
| 1987 | ParseGlobalTypeAndValue(SrcVal) || |
Dan Gohman | 24b108b | 2009-06-15 21:52:11 +0000 | [diff] [blame] | 1988 | ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") || |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1989 | ParseType(DestTy) || |
| 1990 | ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast")) |
| 1991 | return true; |
| 1992 | if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy)) |
| 1993 | return Error(ID.Loc, "invalid cast opcode for cast from '" + |
| 1994 | SrcVal->getType()->getDescription() + "' to '" + |
| 1995 | DestTy->getDescription() + "'"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1996 | ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc, |
Owen Anderson | fba933c | 2009-07-01 23:57:11 +0000 | [diff] [blame] | 1997 | SrcVal, DestTy); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1998 | ID.Kind = ValID::t_Constant; |
| 1999 | return false; |
| 2000 | } |
| 2001 | case lltok::kw_extractvalue: { |
| 2002 | Lex.Lex(); |
| 2003 | Constant *Val; |
| 2004 | SmallVector<unsigned, 4> Indices; |
| 2005 | if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")|| |
| 2006 | ParseGlobalTypeAndValue(Val) || |
| 2007 | ParseIndexList(Indices) || |
| 2008 | ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr")) |
| 2009 | return true; |
| 2010 | if (!isa<StructType>(Val->getType()) && !isa<ArrayType>(Val->getType())) |
| 2011 | return Error(ID.Loc, "extractvalue operand must be array or struct"); |
| 2012 | if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(), |
| 2013 | Indices.end())) |
| 2014 | return Error(ID.Loc, "invalid indices for extractvalue"); |
Jay Foad | e3e51c0 | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2015 | ID.ConstantVal = |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2016 | ConstantExpr::getExtractValue(Val, Indices.data(), Indices.size()); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2017 | ID.Kind = ValID::t_Constant; |
| 2018 | return false; |
| 2019 | } |
| 2020 | case lltok::kw_insertvalue: { |
| 2021 | Lex.Lex(); |
| 2022 | Constant *Val0, *Val1; |
| 2023 | SmallVector<unsigned, 4> Indices; |
| 2024 | if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")|| |
| 2025 | ParseGlobalTypeAndValue(Val0) || |
| 2026 | ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")|| |
| 2027 | ParseGlobalTypeAndValue(Val1) || |
| 2028 | ParseIndexList(Indices) || |
| 2029 | ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr")) |
| 2030 | return true; |
| 2031 | if (!isa<StructType>(Val0->getType()) && !isa<ArrayType>(Val0->getType())) |
| 2032 | return Error(ID.Loc, "extractvalue operand must be array or struct"); |
| 2033 | if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(), |
| 2034 | Indices.end())) |
| 2035 | return Error(ID.Loc, "invalid indices for insertvalue"); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2036 | ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, |
Owen Anderson | fba933c | 2009-07-01 23:57:11 +0000 | [diff] [blame] | 2037 | Indices.data(), Indices.size()); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2038 | ID.Kind = ValID::t_Constant; |
| 2039 | return false; |
| 2040 | } |
| 2041 | case lltok::kw_icmp: |
Nick Lewycky | 7f6aa2b | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 2042 | case lltok::kw_fcmp: { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2043 | unsigned PredVal, Opc = Lex.getUIntVal(); |
| 2044 | Constant *Val0, *Val1; |
| 2045 | Lex.Lex(); |
| 2046 | if (ParseCmpPredicate(PredVal, Opc) || |
| 2047 | ParseToken(lltok::lparen, "expected '(' in compare constantexpr") || |
| 2048 | ParseGlobalTypeAndValue(Val0) || |
| 2049 | ParseToken(lltok::comma, "expected comma in compare constantexpr") || |
| 2050 | ParseGlobalTypeAndValue(Val1) || |
| 2051 | ParseToken(lltok::rparen, "expected ')' in compare constantexpr")) |
| 2052 | return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2053 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2054 | if (Val0->getType() != Val1->getType()) |
| 2055 | return Error(ID.Loc, "compare operands must have the same type"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2056 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2057 | CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2058 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2059 | if (Opc == Instruction::FCmp) { |
| 2060 | if (!Val0->getType()->isFPOrFPVector()) |
| 2061 | return Error(ID.Loc, "fcmp requires floating point operands"); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2062 | ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1); |
Nick Lewycky | 7f6aa2b | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 2063 | } else { |
| 2064 | assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!"); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2065 | if (!Val0->getType()->isIntOrIntVector() && |
| 2066 | !isa<PointerType>(Val0->getType())) |
| 2067 | return Error(ID.Loc, "icmp requires pointer or integer operands"); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2068 | ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2069 | } |
| 2070 | ID.Kind = ValID::t_Constant; |
| 2071 | return false; |
| 2072 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2073 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2074 | // Binary Operators. |
| 2075 | case lltok::kw_add: |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2076 | case lltok::kw_fadd: |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2077 | case lltok::kw_sub: |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2078 | case lltok::kw_fsub: |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2079 | case lltok::kw_mul: |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2080 | case lltok::kw_fmul: |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2081 | case lltok::kw_udiv: |
| 2082 | case lltok::kw_sdiv: |
| 2083 | case lltok::kw_fdiv: |
| 2084 | case lltok::kw_urem: |
| 2085 | case lltok::kw_srem: |
| 2086 | case lltok::kw_frem: { |
Dan Gohman | 59858cf | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 2087 | bool NUW = false; |
| 2088 | bool NSW = false; |
| 2089 | bool Exact = false; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2090 | unsigned Opc = Lex.getUIntVal(); |
| 2091 | Constant *Val0, *Val1; |
| 2092 | Lex.Lex(); |
Dan Gohman | 59858cf | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 2093 | LocTy ModifierLoc = Lex.getLoc(); |
| 2094 | if (Opc == Instruction::Add || |
| 2095 | Opc == Instruction::Sub || |
| 2096 | Opc == Instruction::Mul) { |
| 2097 | if (EatIfPresent(lltok::kw_nuw)) |
| 2098 | NUW = true; |
| 2099 | if (EatIfPresent(lltok::kw_nsw)) { |
| 2100 | NSW = true; |
| 2101 | if (EatIfPresent(lltok::kw_nuw)) |
| 2102 | NUW = true; |
| 2103 | } |
| 2104 | } else if (Opc == Instruction::SDiv) { |
| 2105 | if (EatIfPresent(lltok::kw_exact)) |
| 2106 | Exact = true; |
| 2107 | } |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2108 | if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") || |
| 2109 | ParseGlobalTypeAndValue(Val0) || |
| 2110 | ParseToken(lltok::comma, "expected comma in binary constantexpr") || |
| 2111 | ParseGlobalTypeAndValue(Val1) || |
| 2112 | ParseToken(lltok::rparen, "expected ')' in binary constantexpr")) |
| 2113 | return true; |
| 2114 | if (Val0->getType() != Val1->getType()) |
| 2115 | return Error(ID.Loc, "operands of constexpr must have same type"); |
Dan Gohman | 59858cf | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 2116 | if (!Val0->getType()->isIntOrIntVector()) { |
| 2117 | if (NUW) |
| 2118 | return Error(ModifierLoc, "nuw only applies to integer operations"); |
| 2119 | if (NSW) |
| 2120 | return Error(ModifierLoc, "nsw only applies to integer operations"); |
| 2121 | } |
| 2122 | // API compatibility: Accept either integer or floating-point types with |
| 2123 | // add, sub, and mul. |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2124 | if (!Val0->getType()->isIntOrIntVector() && |
| 2125 | !Val0->getType()->isFPOrFPVector()) |
| 2126 | return Error(ID.Loc,"constexpr requires integer, fp, or vector operands"); |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2127 | unsigned Flags = 0; |
| 2128 | if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap; |
| 2129 | if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap; |
| 2130 | if (Exact) Flags |= SDivOperator::IsExact; |
| 2131 | Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags); |
Dan Gohman | 59858cf | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 2132 | ID.ConstantVal = C; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2133 | ID.Kind = ValID::t_Constant; |
| 2134 | return false; |
| 2135 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2136 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2137 | // Logical Operations |
| 2138 | case lltok::kw_shl: |
| 2139 | case lltok::kw_lshr: |
| 2140 | case lltok::kw_ashr: |
| 2141 | case lltok::kw_and: |
| 2142 | case lltok::kw_or: |
| 2143 | case lltok::kw_xor: { |
| 2144 | unsigned Opc = Lex.getUIntVal(); |
| 2145 | Constant *Val0, *Val1; |
| 2146 | Lex.Lex(); |
| 2147 | if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") || |
| 2148 | ParseGlobalTypeAndValue(Val0) || |
| 2149 | ParseToken(lltok::comma, "expected comma in logical constantexpr") || |
| 2150 | ParseGlobalTypeAndValue(Val1) || |
| 2151 | ParseToken(lltok::rparen, "expected ')' in logical constantexpr")) |
| 2152 | return true; |
| 2153 | if (Val0->getType() != Val1->getType()) |
| 2154 | return Error(ID.Loc, "operands of constexpr must have same type"); |
| 2155 | if (!Val0->getType()->isIntOrIntVector()) |
| 2156 | return Error(ID.Loc, |
| 2157 | "constexpr requires integer or integer vector operands"); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2158 | ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2159 | ID.Kind = ValID::t_Constant; |
| 2160 | return false; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2163 | case lltok::kw_getelementptr: |
| 2164 | case lltok::kw_shufflevector: |
| 2165 | case lltok::kw_insertelement: |
| 2166 | case lltok::kw_extractelement: |
| 2167 | case lltok::kw_select: { |
| 2168 | unsigned Opc = Lex.getUIntVal(); |
| 2169 | SmallVector<Constant*, 16> Elts; |
Dan Gohman | dd8004d | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 2170 | bool InBounds = false; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2171 | Lex.Lex(); |
Dan Gohman | dd8004d | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 2172 | if (Opc == Instruction::GetElementPtr) |
Dan Gohman | dcb40a3 | 2009-07-29 15:58:36 +0000 | [diff] [blame] | 2173 | InBounds = EatIfPresent(lltok::kw_inbounds); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2174 | if (ParseToken(lltok::lparen, "expected '(' in constantexpr") || |
| 2175 | ParseGlobalValueVector(Elts) || |
| 2176 | ParseToken(lltok::rparen, "expected ')' in constantexpr")) |
| 2177 | return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2178 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2179 | if (Opc == Instruction::GetElementPtr) { |
| 2180 | if (Elts.size() == 0 || !isa<PointerType>(Elts[0]->getType())) |
| 2181 | return Error(ID.Loc, "getelementptr requires pointer operand"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2182 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2183 | if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), |
Eli Friedman | 4e9bac3 | 2009-07-24 21:56:17 +0000 | [diff] [blame] | 2184 | (Value**)(Elts.data() + 1), |
| 2185 | Elts.size() - 1)) |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2186 | return Error(ID.Loc, "invalid indices for getelementptr"); |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2187 | ID.ConstantVal = InBounds ? |
| 2188 | ConstantExpr::getInBoundsGetElementPtr(Elts[0], |
| 2189 | Elts.data() + 1, |
| 2190 | Elts.size() - 1) : |
| 2191 | ConstantExpr::getGetElementPtr(Elts[0], |
| 2192 | Elts.data() + 1, Elts.size() - 1); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2193 | } else if (Opc == Instruction::Select) { |
| 2194 | if (Elts.size() != 3) |
| 2195 | return Error(ID.Loc, "expected three operands to select"); |
| 2196 | if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1], |
| 2197 | Elts[2])) |
| 2198 | return Error(ID.Loc, Reason); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2199 | ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2200 | } else if (Opc == Instruction::ShuffleVector) { |
| 2201 | if (Elts.size() != 3) |
| 2202 | return Error(ID.Loc, "expected three operands to shufflevector"); |
| 2203 | if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2])) |
| 2204 | return Error(ID.Loc, "invalid operands to shufflevector"); |
Owen Anderson | fba933c | 2009-07-01 23:57:11 +0000 | [diff] [blame] | 2205 | ID.ConstantVal = |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2206 | ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2207 | } else if (Opc == Instruction::ExtractElement) { |
| 2208 | if (Elts.size() != 2) |
| 2209 | return Error(ID.Loc, "expected two operands to extractelement"); |
| 2210 | if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1])) |
| 2211 | return Error(ID.Loc, "invalid extractelement operands"); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2212 | ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2213 | } else { |
| 2214 | assert(Opc == Instruction::InsertElement && "Unknown opcode"); |
| 2215 | if (Elts.size() != 3) |
| 2216 | return Error(ID.Loc, "expected three operands to insertelement"); |
| 2217 | if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2])) |
| 2218 | return Error(ID.Loc, "invalid insertelement operands"); |
Owen Anderson | fba933c | 2009-07-01 23:57:11 +0000 | [diff] [blame] | 2219 | ID.ConstantVal = |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2220 | ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2221 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2222 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2223 | ID.Kind = ValID::t_Constant; |
| 2224 | return false; |
| 2225 | } |
| 2226 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2227 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2228 | Lex.Lex(); |
| 2229 | return false; |
| 2230 | } |
| 2231 | |
| 2232 | /// ParseGlobalValue - Parse a global value with the specified type. |
| 2233 | bool LLParser::ParseGlobalValue(const Type *Ty, Constant *&V) { |
| 2234 | V = 0; |
| 2235 | ValID ID; |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2236 | return ParseValID(ID) || |
| 2237 | ConvertGlobalValIDToValue(Ty, ID, V); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2238 | } |
| 2239 | |
| 2240 | /// ConvertGlobalValIDToValue - Apply a type to a ValID to get a fully resolved |
| 2241 | /// constant. |
| 2242 | bool LLParser::ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, |
| 2243 | Constant *&V) { |
| 2244 | if (isa<FunctionType>(Ty)) |
| 2245 | 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] | 2246 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2247 | switch (ID.Kind) { |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2248 | default: llvm_unreachable("Unknown ValID!"); |
Devang Patel | e54abc9 | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 2249 | case ValID::t_Metadata: |
| 2250 | return Error(ID.Loc, "invalid use of metadata"); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2251 | case ValID::t_LocalID: |
| 2252 | case ValID::t_LocalName: |
| 2253 | return Error(ID.Loc, "invalid use of function-local name"); |
| 2254 | case ValID::t_InlineAsm: |
| 2255 | return Error(ID.Loc, "inline asm can only be an operand of call/invoke"); |
| 2256 | case ValID::t_GlobalName: |
| 2257 | V = GetGlobalVal(ID.StrVal, Ty, ID.Loc); |
| 2258 | return V == 0; |
| 2259 | case ValID::t_GlobalID: |
| 2260 | V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc); |
| 2261 | return V == 0; |
| 2262 | case ValID::t_APSInt: |
| 2263 | if (!isa<IntegerType>(Ty)) |
| 2264 | return Error(ID.Loc, "integer constant must have integer type"); |
| 2265 | ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits()); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 2266 | V = ConstantInt::get(Context, ID.APSIntVal); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2267 | return false; |
| 2268 | case ValID::t_APFloat: |
| 2269 | if (!Ty->isFloatingPoint() || |
| 2270 | !ConstantFP::isValueValidForType(Ty, ID.APFloatVal)) |
| 2271 | return Error(ID.Loc, "floating point constant invalid for type"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2272 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2273 | // The lexer has no type info, so builds all float and double FP constants |
| 2274 | // as double. Fix this here. Long double does not need this. |
| 2275 | if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble && |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2276 | Ty == Type::getFloatTy(Context)) { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2277 | bool Ignored; |
| 2278 | ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, |
| 2279 | &Ignored); |
| 2280 | } |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2281 | V = ConstantFP::get(Context, ID.APFloatVal); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2282 | |
Chris Lattner | 959873d | 2009-01-05 18:24:23 +0000 | [diff] [blame] | 2283 | if (V->getType() != Ty) |
| 2284 | return Error(ID.Loc, "floating point constant does not have type '" + |
| 2285 | Ty->getDescription() + "'"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2286 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2287 | return false; |
| 2288 | case ValID::t_Null: |
| 2289 | if (!isa<PointerType>(Ty)) |
| 2290 | return Error(ID.Loc, "null must be a pointer type"); |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2291 | V = ConstantPointerNull::get(cast<PointerType>(Ty)); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2292 | return false; |
| 2293 | case ValID::t_Undef: |
Chris Lattner | e67c1aa | 2009-01-05 08:13:38 +0000 | [diff] [blame] | 2294 | // FIXME: LabelTy should not be a first-class type. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2295 | if ((!Ty->isFirstClassType() || Ty == Type::getLabelTy(Context)) && |
Chris Lattner | 0b61635 | 2009-01-05 18:12:21 +0000 | [diff] [blame] | 2296 | !isa<OpaqueType>(Ty)) |
Chris Lattner | e67c1aa | 2009-01-05 08:13:38 +0000 | [diff] [blame] | 2297 | return Error(ID.Loc, "invalid type for undef constant"); |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2298 | V = UndefValue::get(Ty); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2299 | return false; |
Chris Lattner | 081b505 | 2009-01-05 07:52:51 +0000 | [diff] [blame] | 2300 | case ValID::t_EmptyArray: |
| 2301 | if (!isa<ArrayType>(Ty) || cast<ArrayType>(Ty)->getNumElements() != 0) |
| 2302 | return Error(ID.Loc, "invalid empty array initializer"); |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2303 | V = UndefValue::get(Ty); |
Chris Lattner | 081b505 | 2009-01-05 07:52:51 +0000 | [diff] [blame] | 2304 | return false; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2305 | case ValID::t_Zero: |
Chris Lattner | e67c1aa | 2009-01-05 08:13:38 +0000 | [diff] [blame] | 2306 | // FIXME: LabelTy should not be a first-class type. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2307 | if (!Ty->isFirstClassType() || Ty == Type::getLabelTy(Context)) |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2308 | return Error(ID.Loc, "invalid type for null constant"); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 2309 | V = Constant::getNullValue(Ty); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2310 | return false; |
| 2311 | case ValID::t_Constant: |
| 2312 | if (ID.ConstantVal->getType() != Ty) |
| 2313 | return Error(ID.Loc, "constant expression type mismatch"); |
| 2314 | V = ID.ConstantVal; |
| 2315 | return false; |
| 2316 | } |
| 2317 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2318 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2319 | bool LLParser::ParseGlobalTypeAndValue(Constant *&V) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2320 | PATypeHolder Type(Type::getVoidTy(Context)); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2321 | return ParseType(Type) || |
| 2322 | ParseGlobalValue(Type, V); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2323 | } |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2324 | |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2325 | /// ParseGlobalValueVector |
| 2326 | /// ::= /*empty*/ |
| 2327 | /// ::= TypeAndValue (',' TypeAndValue)* |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2328 | bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts) { |
| 2329 | // Empty list. |
| 2330 | if (Lex.getKind() == lltok::rbrace || |
| 2331 | Lex.getKind() == lltok::rsquare || |
| 2332 | Lex.getKind() == lltok::greater || |
| 2333 | Lex.getKind() == lltok::rparen) |
| 2334 | return false; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2335 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2336 | Constant *C; |
| 2337 | if (ParseGlobalTypeAndValue(C)) return true; |
| 2338 | Elts.push_back(C); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2339 | |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2340 | while (EatIfPresent(lltok::comma)) { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2341 | if (ParseGlobalTypeAndValue(C)) return true; |
| 2342 | Elts.push_back(C); |
| 2343 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2344 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2345 | return false; |
| 2346 | } |
| 2347 | |
| 2348 | |
| 2349 | //===----------------------------------------------------------------------===// |
| 2350 | // Function Parsing. |
| 2351 | //===----------------------------------------------------------------------===// |
| 2352 | |
| 2353 | bool LLParser::ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V, |
| 2354 | PerFunctionState &PFS) { |
| 2355 | if (ID.Kind == ValID::t_LocalID) |
| 2356 | V = PFS.GetVal(ID.UIntVal, Ty, ID.Loc); |
| 2357 | else if (ID.Kind == ValID::t_LocalName) |
| 2358 | V = PFS.GetVal(ID.StrVal, Ty, ID.Loc); |
Steve Naroff | b0adcdb | 2009-01-05 18:48:47 +0000 | [diff] [blame] | 2359 | else if (ID.Kind == ValID::t_InlineAsm) { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2360 | const PointerType *PTy = dyn_cast<PointerType>(Ty); |
| 2361 | const FunctionType *FTy = |
| 2362 | PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0; |
| 2363 | if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2)) |
| 2364 | return Error(ID.Loc, "invalid type for inline asm constraint string"); |
| 2365 | V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal); |
| 2366 | return false; |
Devang Patel | e54abc9 | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 2367 | } else if (ID.Kind == ValID::t_Metadata) { |
| 2368 | V = ID.MetadataVal; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2369 | } else { |
| 2370 | Constant *C; |
| 2371 | if (ConvertGlobalValIDToValue(Ty, ID, C)) return true; |
| 2372 | V = C; |
| 2373 | return false; |
| 2374 | } |
| 2375 | |
| 2376 | return V == 0; |
| 2377 | } |
| 2378 | |
| 2379 | bool LLParser::ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS) { |
| 2380 | V = 0; |
| 2381 | ValID ID; |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2382 | return ParseValID(ID) || |
| 2383 | ConvertValIDToValue(Ty, ID, V, PFS); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
| 2386 | bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState &PFS) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2387 | PATypeHolder T(Type::getVoidTy(Context)); |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2388 | return ParseType(T) || |
| 2389 | ParseValue(T, V, PFS); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2390 | } |
| 2391 | |
| 2392 | /// FunctionHeader |
| 2393 | /// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs |
| 2394 | /// Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection |
| 2395 | /// OptionalAlign OptGC |
| 2396 | bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) { |
| 2397 | // Parse the linkage. |
| 2398 | LocTy LinkageLoc = Lex.getLoc(); |
| 2399 | unsigned Linkage; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2400 | |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 2401 | unsigned Visibility, RetAttrs; |
| 2402 | CallingConv::ID CC; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2403 | PATypeHolder RetType(Type::getVoidTy(Context)); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2404 | LocTy RetTypeLoc = Lex.getLoc(); |
| 2405 | if (ParseOptionalLinkage(Linkage) || |
| 2406 | ParseOptionalVisibility(Visibility) || |
| 2407 | ParseOptionalCallingConv(CC) || |
| 2408 | ParseOptionalAttrs(RetAttrs, 1) || |
Chris Lattner | a9a9e07 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 2409 | ParseType(RetType, RetTypeLoc, true /*void allowed*/)) |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2410 | return true; |
| 2411 | |
| 2412 | // Verify that the linkage is ok. |
| 2413 | switch ((GlobalValue::LinkageTypes)Linkage) { |
| 2414 | case GlobalValue::ExternalLinkage: |
| 2415 | break; // always ok. |
| 2416 | case GlobalValue::DLLImportLinkage: |
Duncan Sands | 5f4ee1f | 2009-03-11 08:08:06 +0000 | [diff] [blame] | 2417 | case GlobalValue::ExternalWeakLinkage: |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2418 | if (isDefine) |
| 2419 | return Error(LinkageLoc, "invalid linkage for function definition"); |
| 2420 | break; |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 2421 | case GlobalValue::PrivateLinkage: |
Bill Wendling | 3d10a5a | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 2422 | case GlobalValue::LinkerPrivateLinkage: |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2423 | case GlobalValue::InternalLinkage: |
Nick Lewycky | 55f64db | 2009-04-13 07:02:02 +0000 | [diff] [blame] | 2424 | case GlobalValue::AvailableExternallyLinkage: |
Duncan Sands | 667d4b8 | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 2425 | case GlobalValue::LinkOnceAnyLinkage: |
| 2426 | case GlobalValue::LinkOnceODRLinkage: |
| 2427 | case GlobalValue::WeakAnyLinkage: |
| 2428 | case GlobalValue::WeakODRLinkage: |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2429 | case GlobalValue::DLLExportLinkage: |
| 2430 | if (!isDefine) |
| 2431 | return Error(LinkageLoc, "invalid linkage for function declaration"); |
| 2432 | break; |
| 2433 | case GlobalValue::AppendingLinkage: |
| 2434 | case GlobalValue::GhostLinkage: |
Duncan Sands | 4dc2b39 | 2009-03-11 20:14:15 +0000 | [diff] [blame] | 2435 | case GlobalValue::CommonLinkage: |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2436 | return Error(LinkageLoc, "invalid function linkage type"); |
| 2437 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2438 | |
Chris Lattner | 99bb315 | 2009-01-05 08:00:30 +0000 | [diff] [blame] | 2439 | if (!FunctionType::isValidReturnType(RetType) || |
| 2440 | isa<OpaqueType>(RetType)) |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2441 | return Error(RetTypeLoc, "invalid function return type"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2442 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2443 | LocTy NameLoc = Lex.getLoc(); |
Chris Lattner | f570e62 | 2009-02-18 21:48:13 +0000 | [diff] [blame] | 2444 | |
| 2445 | std::string FunctionName; |
| 2446 | if (Lex.getKind() == lltok::GlobalVar) { |
| 2447 | FunctionName = Lex.getStrVal(); |
| 2448 | } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok. |
| 2449 | unsigned NameID = Lex.getUIntVal(); |
| 2450 | |
| 2451 | if (NameID != NumberedVals.size()) |
| 2452 | return TokError("function expected to be numbered '%" + |
| 2453 | utostr(NumberedVals.size()) + "'"); |
| 2454 | } else { |
| 2455 | return TokError("expected function name"); |
| 2456 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2457 | |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2458 | Lex.Lex(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2459 | |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2460 | if (Lex.getKind() != lltok::lparen) |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2461 | return TokError("expected '(' in function argument list"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2462 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2463 | std::vector<ArgInfo> ArgList; |
| 2464 | bool isVarArg; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2465 | unsigned FuncAttrs; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2466 | std::string Section; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2467 | unsigned Alignment; |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2468 | std::string GC; |
| 2469 | |
Chris Lattner | dfd19dd | 2009-01-05 18:34:07 +0000 | [diff] [blame] | 2470 | if (ParseArgumentList(ArgList, isVarArg, false) || |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2471 | ParseOptionalAttrs(FuncAttrs, 2) || |
| 2472 | (EatIfPresent(lltok::kw_section) && |
| 2473 | ParseStringConstant(Section)) || |
| 2474 | ParseOptionalAlignment(Alignment) || |
| 2475 | (EatIfPresent(lltok::kw_gc) && |
| 2476 | ParseStringConstant(GC))) |
| 2477 | return true; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2478 | |
| 2479 | // If the alignment was parsed as an attribute, move to the alignment field. |
| 2480 | if (FuncAttrs & Attribute::Alignment) { |
| 2481 | Alignment = Attribute::getAlignmentFromAttrs(FuncAttrs); |
| 2482 | FuncAttrs &= ~Attribute::Alignment; |
| 2483 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2484 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2485 | // Okay, if we got here, the function is syntactically valid. Convert types |
| 2486 | // and do semantic checks. |
| 2487 | std::vector<const Type*> ParamTypeList; |
| 2488 | SmallVector<AttributeWithIndex, 8> Attrs; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2489 | // 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] | 2490 | // attributes. |
| 2491 | unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg; |
| 2492 | if (FuncAttrs & ObsoleteFuncAttrs) { |
| 2493 | RetAttrs |= FuncAttrs & ObsoleteFuncAttrs; |
| 2494 | FuncAttrs &= ~ObsoleteFuncAttrs; |
| 2495 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2496 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2497 | if (RetAttrs != Attribute::None) |
| 2498 | Attrs.push_back(AttributeWithIndex::get(0, RetAttrs)); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2499 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2500 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { |
| 2501 | ParamTypeList.push_back(ArgList[i].Type); |
| 2502 | if (ArgList[i].Attrs != Attribute::None) |
| 2503 | Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs)); |
| 2504 | } |
| 2505 | |
| 2506 | if (FuncAttrs != Attribute::None) |
| 2507 | Attrs.push_back(AttributeWithIndex::get(~0, FuncAttrs)); |
| 2508 | |
| 2509 | AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2510 | |
Chris Lattner | a9a9e07 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 2511 | if (PAL.paramHasAttr(1, Attribute::StructRet) && |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2512 | RetType != Type::getVoidTy(Context)) |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2513 | return Error(RetTypeLoc, "functions with 'sret' argument must return void"); |
| 2514 | |
Owen Anderson | fba933c | 2009-07-01 23:57:11 +0000 | [diff] [blame] | 2515 | const FunctionType *FT = |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 2516 | FunctionType::get(RetType, ParamTypeList, isVarArg); |
| 2517 | const PointerType *PFT = PointerType::getUnqual(FT); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2518 | |
| 2519 | Fn = 0; |
| 2520 | if (!FunctionName.empty()) { |
| 2521 | // If this was a definition of a forward reference, remove the definition |
| 2522 | // from the forward reference table and fill in the forward ref. |
| 2523 | std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI = |
| 2524 | ForwardRefVals.find(FunctionName); |
| 2525 | if (FRVI != ForwardRefVals.end()) { |
| 2526 | Fn = M->getFunction(FunctionName); |
| 2527 | ForwardRefVals.erase(FRVI); |
| 2528 | } else if ((Fn = M->getFunction(FunctionName))) { |
| 2529 | // If this function already exists in the symbol table, then it is |
| 2530 | // multiply defined. We accept a few cases for old backwards compat. |
| 2531 | // FIXME: Remove this stuff for LLVM 3.0. |
| 2532 | if (Fn->getType() != PFT || Fn->getAttributes() != PAL || |
| 2533 | (!Fn->isDeclaration() && isDefine)) { |
| 2534 | // If the redefinition has different type or different attributes, |
| 2535 | // reject it. If both have bodies, reject it. |
| 2536 | return Error(NameLoc, "invalid redefinition of function '" + |
| 2537 | FunctionName + "'"); |
| 2538 | } else if (Fn->isDeclaration()) { |
| 2539 | // Make sure to strip off any argument names so we can't get conflicts. |
| 2540 | for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end(); |
| 2541 | AI != AE; ++AI) |
| 2542 | AI->setName(""); |
| 2543 | } |
| 2544 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2545 | |
Dan Gohman | 4190554 | 2009-08-29 23:37:49 +0000 | [diff] [blame] | 2546 | } else { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2547 | // If this is a definition of a forward referenced function, make sure the |
| 2548 | // types agree. |
| 2549 | std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I |
| 2550 | = ForwardRefValIDs.find(NumberedVals.size()); |
| 2551 | if (I != ForwardRefValIDs.end()) { |
| 2552 | Fn = cast<Function>(I->second.first); |
| 2553 | if (Fn->getType() != PFT) |
| 2554 | return Error(NameLoc, "type of definition and forward reference of '@" + |
| 2555 | utostr(NumberedVals.size()) +"' disagree"); |
| 2556 | ForwardRefValIDs.erase(I); |
| 2557 | } |
| 2558 | } |
| 2559 | |
| 2560 | if (Fn == 0) |
| 2561 | Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M); |
| 2562 | else // Move the forward-reference to the correct spot in the module. |
| 2563 | M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn); |
| 2564 | |
| 2565 | if (FunctionName.empty()) |
| 2566 | NumberedVals.push_back(Fn); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2567 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2568 | Fn->setLinkage((GlobalValue::LinkageTypes)Linkage); |
| 2569 | Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility); |
| 2570 | Fn->setCallingConv(CC); |
| 2571 | Fn->setAttributes(PAL); |
| 2572 | Fn->setAlignment(Alignment); |
| 2573 | Fn->setSection(Section); |
| 2574 | if (!GC.empty()) Fn->setGC(GC.c_str()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2575 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2576 | // Add all of the arguments we parsed to the function. |
| 2577 | Function::arg_iterator ArgIt = Fn->arg_begin(); |
| 2578 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) { |
| 2579 | // If the argument has a name, insert it into the argument symbol table. |
| 2580 | if (ArgList[i].Name.empty()) continue; |
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 | // Set the name, if it conflicted, it will be auto-renamed. |
| 2583 | ArgIt->setName(ArgList[i].Name); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2584 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2585 | if (ArgIt->getNameStr() != ArgList[i].Name) |
| 2586 | return Error(ArgList[i].Loc, "redefinition of argument '%" + |
| 2587 | ArgList[i].Name + "'"); |
| 2588 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2589 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2590 | return false; |
| 2591 | } |
| 2592 | |
| 2593 | |
| 2594 | /// ParseFunctionBody |
| 2595 | /// ::= '{' BasicBlock+ '}' |
| 2596 | /// ::= 'begin' BasicBlock+ 'end' // FIXME: remove in LLVM 3.0 |
| 2597 | /// |
| 2598 | bool LLParser::ParseFunctionBody(Function &Fn) { |
| 2599 | if (Lex.getKind() != lltok::lbrace && Lex.getKind() != lltok::kw_begin) |
| 2600 | return TokError("expected '{' in function body"); |
| 2601 | Lex.Lex(); // eat the {. |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2602 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2603 | PerFunctionState PFS(*this, Fn); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2604 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2605 | while (Lex.getKind() != lltok::rbrace && Lex.getKind() != lltok::kw_end) |
| 2606 | if (ParseBasicBlock(PFS)) return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2607 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2608 | // Eat the }. |
| 2609 | Lex.Lex(); |
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 | // Verify function is ok. |
| 2612 | return PFS.VerifyFunctionComplete(); |
| 2613 | } |
| 2614 | |
| 2615 | /// ParseBasicBlock |
| 2616 | /// ::= LabelStr? Instruction* |
| 2617 | bool LLParser::ParseBasicBlock(PerFunctionState &PFS) { |
| 2618 | // If this basic block starts out with a name, remember it. |
| 2619 | std::string Name; |
| 2620 | LocTy NameLoc = Lex.getLoc(); |
| 2621 | if (Lex.getKind() == lltok::LabelStr) { |
| 2622 | Name = Lex.getStrVal(); |
| 2623 | Lex.Lex(); |
| 2624 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2625 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2626 | BasicBlock *BB = PFS.DefineBB(Name, NameLoc); |
| 2627 | if (BB == 0) return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2628 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2629 | std::string NameStr; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2630 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2631 | // Parse the instructions in this block until we get a terminator. |
| 2632 | Instruction *Inst; |
| 2633 | do { |
| 2634 | // This instruction may have three possibilities for a name: a) none |
| 2635 | // specified, b) name specified "%foo =", c) number specified: "%4 =". |
| 2636 | LocTy NameLoc = Lex.getLoc(); |
| 2637 | int NameID = -1; |
| 2638 | NameStr = ""; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2639 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2640 | if (Lex.getKind() == lltok::LocalVarID) { |
| 2641 | NameID = Lex.getUIntVal(); |
| 2642 | Lex.Lex(); |
| 2643 | if (ParseToken(lltok::equal, "expected '=' after instruction id")) |
| 2644 | return true; |
| 2645 | } else if (Lex.getKind() == lltok::LocalVar || |
| 2646 | // FIXME: REMOVE IN LLVM 3.0 |
| 2647 | Lex.getKind() == lltok::StringConstant) { |
| 2648 | NameStr = Lex.getStrVal(); |
| 2649 | Lex.Lex(); |
| 2650 | if (ParseToken(lltok::equal, "expected '=' after instruction name")) |
| 2651 | return true; |
| 2652 | } |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 2653 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2654 | if (ParseInstruction(Inst, BB, PFS)) return true; |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 2655 | if (EatIfPresent(lltok::comma)) |
| 2656 | ParseOptionalDbgInfo(); |
| 2657 | |
| 2658 | // Set metadata attached with this instruction. |
| 2659 | Metadata &TheMetadata = M->getContext().getMetadata(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2660 | for (SmallVector<std::pair<MDKindID, MDNode *>, 2>::iterator |
| 2661 | MDI = MDsOnInst.begin(), MDE = MDsOnInst.end(); MDI != MDE; ++MDI) |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 2662 | TheMetadata.setMD(MDI->first, MDI->second, Inst); |
| 2663 | MDsOnInst.clear(); |
| 2664 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2665 | BB->getInstList().push_back(Inst); |
| 2666 | |
| 2667 | // Set the name on the instruction. |
| 2668 | if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true; |
| 2669 | } while (!isa<TerminatorInst>(Inst)); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2670 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2671 | return false; |
| 2672 | } |
| 2673 | |
| 2674 | //===----------------------------------------------------------------------===// |
| 2675 | // Instruction Parsing. |
| 2676 | //===----------------------------------------------------------------------===// |
| 2677 | |
| 2678 | /// ParseInstruction - Parse one of the many different instructions. |
| 2679 | /// |
| 2680 | bool LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB, |
| 2681 | PerFunctionState &PFS) { |
| 2682 | lltok::Kind Token = Lex.getKind(); |
| 2683 | if (Token == lltok::Eof) |
| 2684 | return TokError("found end of file when expecting more instructions"); |
| 2685 | LocTy Loc = Lex.getLoc(); |
Chris Lattner | f6f0bdf | 2009-03-01 00:53:13 +0000 | [diff] [blame] | 2686 | unsigned KeywordVal = Lex.getUIntVal(); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2687 | Lex.Lex(); // Eat the keyword. |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2688 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2689 | switch (Token) { |
| 2690 | default: return Error(Loc, "expected instruction opcode"); |
| 2691 | // Terminator Instructions. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2692 | case lltok::kw_unwind: Inst = new UnwindInst(Context); return false; |
| 2693 | case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2694 | case lltok::kw_ret: return ParseRet(Inst, BB, PFS); |
| 2695 | case lltok::kw_br: return ParseBr(Inst, PFS); |
| 2696 | case lltok::kw_switch: return ParseSwitch(Inst, PFS); |
| 2697 | case lltok::kw_invoke: return ParseInvoke(Inst, PFS); |
| 2698 | // Binary Operators. |
| 2699 | case lltok::kw_add: |
| 2700 | case lltok::kw_sub: |
Dan Gohman | 59858cf | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 2701 | case lltok::kw_mul: { |
| 2702 | bool NUW = false; |
| 2703 | bool NSW = false; |
| 2704 | LocTy ModifierLoc = Lex.getLoc(); |
| 2705 | if (EatIfPresent(lltok::kw_nuw)) |
| 2706 | NUW = true; |
| 2707 | if (EatIfPresent(lltok::kw_nsw)) { |
| 2708 | NSW = true; |
| 2709 | if (EatIfPresent(lltok::kw_nuw)) |
| 2710 | NUW = true; |
| 2711 | } |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2712 | // API compatibility: Accept either integer or floating-point types. |
Dan Gohman | 59858cf | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 2713 | bool Result = ParseArithmetic(Inst, PFS, KeywordVal, 0); |
| 2714 | if (!Result) { |
| 2715 | if (!Inst->getType()->isIntOrIntVector()) { |
| 2716 | if (NUW) |
| 2717 | return Error(ModifierLoc, "nuw only applies to integer operations"); |
| 2718 | if (NSW) |
| 2719 | return Error(ModifierLoc, "nsw only applies to integer operations"); |
| 2720 | } |
| 2721 | if (NUW) |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2722 | cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true); |
Dan Gohman | 59858cf | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 2723 | if (NSW) |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2724 | cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true); |
Dan Gohman | 59858cf | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 2725 | } |
| 2726 | return Result; |
| 2727 | } |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2728 | case lltok::kw_fadd: |
| 2729 | case lltok::kw_fsub: |
| 2730 | case lltok::kw_fmul: return ParseArithmetic(Inst, PFS, KeywordVal, 2); |
| 2731 | |
Dan Gohman | 59858cf | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 2732 | case lltok::kw_sdiv: { |
| 2733 | bool Exact = false; |
| 2734 | if (EatIfPresent(lltok::kw_exact)) |
| 2735 | Exact = true; |
| 2736 | bool Result = ParseArithmetic(Inst, PFS, KeywordVal, 1); |
| 2737 | if (!Result) |
| 2738 | if (Exact) |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2739 | cast<BinaryOperator>(Inst)->setIsExact(true); |
Dan Gohman | 59858cf | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 2740 | return Result; |
| 2741 | } |
| 2742 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2743 | case lltok::kw_udiv: |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2744 | case lltok::kw_urem: |
Chris Lattner | f6f0bdf | 2009-03-01 00:53:13 +0000 | [diff] [blame] | 2745 | case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1); |
Chris Lattner | e914b59 | 2009-01-05 08:24:46 +0000 | [diff] [blame] | 2746 | case lltok::kw_fdiv: |
Chris Lattner | f6f0bdf | 2009-03-01 00:53:13 +0000 | [diff] [blame] | 2747 | case lltok::kw_frem: return ParseArithmetic(Inst, PFS, KeywordVal, 2); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2748 | case lltok::kw_shl: |
| 2749 | case lltok::kw_lshr: |
| 2750 | case lltok::kw_ashr: |
| 2751 | case lltok::kw_and: |
| 2752 | case lltok::kw_or: |
Chris Lattner | f6f0bdf | 2009-03-01 00:53:13 +0000 | [diff] [blame] | 2753 | case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2754 | case lltok::kw_icmp: |
Nick Lewycky | 7f6aa2b | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 2755 | case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2756 | // Casts. |
| 2757 | case lltok::kw_trunc: |
| 2758 | case lltok::kw_zext: |
| 2759 | case lltok::kw_sext: |
| 2760 | case lltok::kw_fptrunc: |
| 2761 | case lltok::kw_fpext: |
| 2762 | case lltok::kw_bitcast: |
| 2763 | case lltok::kw_uitofp: |
| 2764 | case lltok::kw_sitofp: |
| 2765 | case lltok::kw_fptoui: |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2766 | case lltok::kw_fptosi: |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2767 | case lltok::kw_inttoptr: |
Chris Lattner | f6f0bdf | 2009-03-01 00:53:13 +0000 | [diff] [blame] | 2768 | case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2769 | // Other. |
| 2770 | case lltok::kw_select: return ParseSelect(Inst, PFS); |
Chris Lattner | 0088a5c | 2009-01-05 08:18:44 +0000 | [diff] [blame] | 2771 | case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2772 | case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS); |
| 2773 | case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS); |
| 2774 | case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS); |
| 2775 | case lltok::kw_phi: return ParsePHI(Inst, PFS); |
| 2776 | case lltok::kw_call: return ParseCall(Inst, PFS, false); |
| 2777 | case lltok::kw_tail: return ParseCall(Inst, PFS, true); |
| 2778 | // Memory. |
| 2779 | case lltok::kw_alloca: |
Chris Lattner | f6f0bdf | 2009-03-01 00:53:13 +0000 | [diff] [blame] | 2780 | case lltok::kw_malloc: return ParseAlloc(Inst, PFS, KeywordVal); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2781 | case lltok::kw_free: return ParseFree(Inst, PFS); |
| 2782 | case lltok::kw_load: return ParseLoad(Inst, PFS, false); |
| 2783 | case lltok::kw_store: return ParseStore(Inst, PFS, false); |
| 2784 | case lltok::kw_volatile: |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2785 | if (EatIfPresent(lltok::kw_load)) |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2786 | return ParseLoad(Inst, PFS, true); |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2787 | else if (EatIfPresent(lltok::kw_store)) |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2788 | return ParseStore(Inst, PFS, true); |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2789 | else |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2790 | return TokError("expected 'load' or 'store'"); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2791 | case lltok::kw_getresult: return ParseGetResult(Inst, PFS); |
| 2792 | case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS); |
| 2793 | case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS); |
| 2794 | case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS); |
| 2795 | } |
| 2796 | } |
| 2797 | |
| 2798 | /// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind. |
| 2799 | bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) { |
Nick Lewycky | 7f6aa2b | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 2800 | if (Opc == Instruction::FCmp) { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2801 | switch (Lex.getKind()) { |
| 2802 | default: TokError("expected fcmp predicate (e.g. 'oeq')"); |
| 2803 | case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break; |
| 2804 | case lltok::kw_one: P = CmpInst::FCMP_ONE; break; |
| 2805 | case lltok::kw_olt: P = CmpInst::FCMP_OLT; break; |
| 2806 | case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break; |
| 2807 | case lltok::kw_ole: P = CmpInst::FCMP_OLE; break; |
| 2808 | case lltok::kw_oge: P = CmpInst::FCMP_OGE; break; |
| 2809 | case lltok::kw_ord: P = CmpInst::FCMP_ORD; break; |
| 2810 | case lltok::kw_uno: P = CmpInst::FCMP_UNO; break; |
| 2811 | case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break; |
| 2812 | case lltok::kw_une: P = CmpInst::FCMP_UNE; break; |
| 2813 | case lltok::kw_ult: P = CmpInst::FCMP_ULT; break; |
| 2814 | case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break; |
| 2815 | case lltok::kw_ule: P = CmpInst::FCMP_ULE; break; |
| 2816 | case lltok::kw_uge: P = CmpInst::FCMP_UGE; break; |
| 2817 | case lltok::kw_true: P = CmpInst::FCMP_TRUE; break; |
| 2818 | case lltok::kw_false: P = CmpInst::FCMP_FALSE; break; |
| 2819 | } |
| 2820 | } else { |
| 2821 | switch (Lex.getKind()) { |
| 2822 | default: TokError("expected icmp predicate (e.g. 'eq')"); |
| 2823 | case lltok::kw_eq: P = CmpInst::ICMP_EQ; break; |
| 2824 | case lltok::kw_ne: P = CmpInst::ICMP_NE; break; |
| 2825 | case lltok::kw_slt: P = CmpInst::ICMP_SLT; break; |
| 2826 | case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break; |
| 2827 | case lltok::kw_sle: P = CmpInst::ICMP_SLE; break; |
| 2828 | case lltok::kw_sge: P = CmpInst::ICMP_SGE; break; |
| 2829 | case lltok::kw_ult: P = CmpInst::ICMP_ULT; break; |
| 2830 | case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break; |
| 2831 | case lltok::kw_ule: P = CmpInst::ICMP_ULE; break; |
| 2832 | case lltok::kw_uge: P = CmpInst::ICMP_UGE; break; |
| 2833 | } |
| 2834 | } |
| 2835 | Lex.Lex(); |
| 2836 | return false; |
| 2837 | } |
| 2838 | |
| 2839 | //===----------------------------------------------------------------------===// |
| 2840 | // Terminator Instructions. |
| 2841 | //===----------------------------------------------------------------------===// |
| 2842 | |
| 2843 | /// ParseRet - Parse a return instruction. |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 2844 | /// ::= 'ret' void (',' 'dbg' !1) |
| 2845 | /// ::= 'ret' TypeAndValue (',' 'dbg' !1) |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2846 | /// ::= 'ret' TypeAndValue (',' TypeAndValue)+ (',' 'dbg' !1) |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 2847 | /// [[obsolete: LLVM 3.0]] |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2848 | bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB, |
| 2849 | PerFunctionState &PFS) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2850 | PATypeHolder Ty(Type::getVoidTy(Context)); |
Chris Lattner | a9a9e07 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 2851 | if (ParseType(Ty, true /*void allowed*/)) return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2852 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2853 | if (Ty == Type::getVoidTy(Context)) { |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 2854 | if (EatIfPresent(lltok::comma)) |
| 2855 | if (ParseOptionalDbgInfo()) return true; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2856 | Inst = ReturnInst::Create(Context); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2857 | return false; |
| 2858 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2859 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2860 | Value *RV; |
| 2861 | if (ParseValue(Ty, RV, PFS)) return true; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2862 | |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 2863 | if (EatIfPresent(lltok::comma)) { |
| 2864 | // Parse optional 'dbg' |
| 2865 | if (Lex.getKind() == lltok::kw_dbg) { |
| 2866 | if (ParseOptionalDbgInfo()) return true; |
| 2867 | } else { |
| 2868 | // The normal case is one return value. |
| 2869 | // FIXME: LLVM 3.0 remove MRV support for 'ret i32 1, i32 2', requiring use |
| 2870 | // of 'ret {i32,i32} {i32 1, i32 2}' |
| 2871 | SmallVector<Value*, 8> RVs; |
| 2872 | RVs.push_back(RV); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2873 | |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 2874 | do { |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2875 | // If optional 'dbg' is seen then this is the end of MRV. |
| 2876 | if (Lex.getKind() == lltok::kw_dbg) |
| 2877 | break; |
| 2878 | if (ParseTypeAndValue(RV, PFS)) return true; |
| 2879 | RVs.push_back(RV); |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 2880 | } while (EatIfPresent(lltok::comma)); |
| 2881 | |
| 2882 | RV = UndefValue::get(PFS.getFunction().getReturnType()); |
| 2883 | for (unsigned i = 0, e = RVs.size(); i != e; ++i) { |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2884 | Instruction *I = InsertValueInst::Create(RV, RVs[i], i, "mrv"); |
| 2885 | BB->getInstList().push_back(I); |
| 2886 | RV = I; |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 2887 | } |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2888 | } |
| 2889 | } |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 2890 | if (EatIfPresent(lltok::comma)) |
| 2891 | if (ParseOptionalDbgInfo()) return true; |
| 2892 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2893 | Inst = ReturnInst::Create(Context, RV); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2894 | return false; |
| 2895 | } |
| 2896 | |
| 2897 | |
| 2898 | /// ParseBr |
| 2899 | /// ::= 'br' TypeAndValue |
| 2900 | /// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue |
| 2901 | bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) { |
| 2902 | LocTy Loc, Loc2; |
| 2903 | Value *Op0, *Op1, *Op2; |
| 2904 | if (ParseTypeAndValue(Op0, Loc, PFS)) return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2905 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2906 | if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) { |
| 2907 | Inst = BranchInst::Create(BB); |
| 2908 | return false; |
| 2909 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2910 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2911 | if (Op0->getType() != Type::getInt1Ty(Context)) |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2912 | return Error(Loc, "branch condition must have 'i1' type"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2913 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2914 | if (ParseToken(lltok::comma, "expected ',' after branch condition") || |
| 2915 | ParseTypeAndValue(Op1, Loc, PFS) || |
| 2916 | ParseToken(lltok::comma, "expected ',' after true destination") || |
| 2917 | ParseTypeAndValue(Op2, Loc2, PFS)) |
| 2918 | return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2919 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2920 | if (!isa<BasicBlock>(Op1)) |
| 2921 | return Error(Loc, "true destination of branch must be a basic block"); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2922 | if (!isa<BasicBlock>(Op2)) |
| 2923 | return Error(Loc2, "true destination of branch must be a basic block"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2924 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2925 | Inst = BranchInst::Create(cast<BasicBlock>(Op1), cast<BasicBlock>(Op2), Op0); |
| 2926 | return false; |
| 2927 | } |
| 2928 | |
| 2929 | /// ParseSwitch |
| 2930 | /// Instruction |
| 2931 | /// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']' |
| 2932 | /// JumpTable |
| 2933 | /// ::= (TypeAndValue ',' TypeAndValue)* |
| 2934 | bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) { |
| 2935 | LocTy CondLoc, BBLoc; |
| 2936 | Value *Cond, *DefaultBB; |
| 2937 | if (ParseTypeAndValue(Cond, CondLoc, PFS) || |
| 2938 | ParseToken(lltok::comma, "expected ',' after switch condition") || |
| 2939 | ParseTypeAndValue(DefaultBB, BBLoc, PFS) || |
| 2940 | ParseToken(lltok::lsquare, "expected '[' with switch table")) |
| 2941 | return true; |
| 2942 | |
| 2943 | if (!isa<IntegerType>(Cond->getType())) |
| 2944 | return Error(CondLoc, "switch condition must have integer type"); |
| 2945 | if (!isa<BasicBlock>(DefaultBB)) |
| 2946 | return Error(BBLoc, "default destination must be a basic block"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2947 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2948 | // Parse the jump table pairs. |
| 2949 | SmallPtrSet<Value*, 32> SeenCases; |
| 2950 | SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table; |
| 2951 | while (Lex.getKind() != lltok::rsquare) { |
| 2952 | Value *Constant, *DestBB; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2953 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2954 | if (ParseTypeAndValue(Constant, CondLoc, PFS) || |
| 2955 | ParseToken(lltok::comma, "expected ',' after case value") || |
| 2956 | ParseTypeAndValue(DestBB, BBLoc, PFS)) |
| 2957 | return true; |
| 2958 | |
| 2959 | if (!SeenCases.insert(Constant)) |
| 2960 | return Error(CondLoc, "duplicate case value in switch"); |
| 2961 | if (!isa<ConstantInt>(Constant)) |
| 2962 | return Error(CondLoc, "case value is not a constant integer"); |
| 2963 | if (!isa<BasicBlock>(DestBB)) |
| 2964 | return Error(BBLoc, "case destination is not a basic block"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2965 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2966 | Table.push_back(std::make_pair(cast<ConstantInt>(Constant), |
| 2967 | cast<BasicBlock>(DestBB))); |
| 2968 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2969 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2970 | Lex.Lex(); // Eat the ']'. |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2971 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2972 | SwitchInst *SI = SwitchInst::Create(Cond, cast<BasicBlock>(DefaultBB), |
| 2973 | Table.size()); |
| 2974 | for (unsigned i = 0, e = Table.size(); i != e; ++i) |
| 2975 | SI->addCase(Table[i].first, Table[i].second); |
| 2976 | Inst = SI; |
| 2977 | return false; |
| 2978 | } |
| 2979 | |
| 2980 | /// ParseInvoke |
| 2981 | /// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList |
| 2982 | /// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue |
| 2983 | bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) { |
| 2984 | LocTy CallLoc = Lex.getLoc(); |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 2985 | unsigned RetAttrs, FnAttrs; |
| 2986 | CallingConv::ID CC; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2987 | PATypeHolder RetType(Type::getVoidTy(Context)); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2988 | LocTy RetTypeLoc; |
| 2989 | ValID CalleeID; |
| 2990 | SmallVector<ParamInfo, 16> ArgList; |
| 2991 | |
| 2992 | Value *NormalBB, *UnwindBB; |
| 2993 | if (ParseOptionalCallingConv(CC) || |
| 2994 | ParseOptionalAttrs(RetAttrs, 1) || |
Chris Lattner | a9a9e07 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 2995 | ParseType(RetType, RetTypeLoc, true /*void allowed*/) || |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2996 | ParseValID(CalleeID) || |
| 2997 | ParseParameterList(ArgList, PFS) || |
| 2998 | ParseOptionalAttrs(FnAttrs, 2) || |
| 2999 | ParseToken(lltok::kw_to, "expected 'to' in invoke") || |
| 3000 | ParseTypeAndValue(NormalBB, PFS) || |
| 3001 | ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") || |
| 3002 | ParseTypeAndValue(UnwindBB, PFS)) |
| 3003 | return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3004 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3005 | if (!isa<BasicBlock>(NormalBB)) |
| 3006 | return Error(CallLoc, "normal destination is not a basic block"); |
| 3007 | if (!isa<BasicBlock>(UnwindBB)) |
| 3008 | return Error(CallLoc, "unwind destination is not a basic block"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3009 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3010 | // If RetType is a non-function pointer type, then this is the short syntax |
| 3011 | // for the call, which means that RetType is just the return type. Infer the |
| 3012 | // rest of the function argument types from the arguments that are present. |
| 3013 | const PointerType *PFTy = 0; |
| 3014 | const FunctionType *Ty = 0; |
| 3015 | if (!(PFTy = dyn_cast<PointerType>(RetType)) || |
| 3016 | !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) { |
| 3017 | // Pull out the types of all of the arguments... |
| 3018 | std::vector<const Type*> ParamTypes; |
| 3019 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) |
| 3020 | ParamTypes.push_back(ArgList[i].V->getType()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3021 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3022 | if (!FunctionType::isValidReturnType(RetType)) |
| 3023 | return Error(RetTypeLoc, "Invalid result type for LLVM function"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3024 | |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 3025 | Ty = FunctionType::get(RetType, ParamTypes, false); |
| 3026 | PFTy = PointerType::getUnqual(Ty); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3027 | } |
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 | // Look up the callee. |
| 3030 | Value *Callee; |
| 3031 | if (ConvertValIDToValue(PFTy, CalleeID, Callee, PFS)) return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3032 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3033 | // FIXME: In LLVM 3.0, stop accepting zext, sext and inreg as optional |
| 3034 | // function attributes. |
| 3035 | unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg; |
| 3036 | if (FnAttrs & ObsoleteFuncAttrs) { |
| 3037 | RetAttrs |= FnAttrs & ObsoleteFuncAttrs; |
| 3038 | FnAttrs &= ~ObsoleteFuncAttrs; |
| 3039 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3040 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3041 | // Set up the Attributes for the function. |
| 3042 | SmallVector<AttributeWithIndex, 8> Attrs; |
| 3043 | if (RetAttrs != Attribute::None) |
| 3044 | Attrs.push_back(AttributeWithIndex::get(0, RetAttrs)); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3045 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3046 | SmallVector<Value*, 8> Args; |
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 | // Loop through FunctionType's arguments and ensure they are specified |
| 3049 | // correctly. Also, gather any parameter attributes. |
| 3050 | FunctionType::param_iterator I = Ty->param_begin(); |
| 3051 | FunctionType::param_iterator E = Ty->param_end(); |
| 3052 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { |
| 3053 | const Type *ExpectedTy = 0; |
| 3054 | if (I != E) { |
| 3055 | ExpectedTy = *I++; |
| 3056 | } else if (!Ty->isVarArg()) { |
| 3057 | return Error(ArgList[i].Loc, "too many arguments specified"); |
| 3058 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3059 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3060 | if (ExpectedTy && ExpectedTy != ArgList[i].V->getType()) |
| 3061 | return Error(ArgList[i].Loc, "argument is not of expected type '" + |
| 3062 | ExpectedTy->getDescription() + "'"); |
| 3063 | Args.push_back(ArgList[i].V); |
| 3064 | if (ArgList[i].Attrs != Attribute::None) |
| 3065 | Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs)); |
| 3066 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3067 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3068 | if (I != E) |
| 3069 | return Error(CallLoc, "not enough parameters specified for call"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3070 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3071 | if (FnAttrs != Attribute::None) |
| 3072 | Attrs.push_back(AttributeWithIndex::get(~0, FnAttrs)); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3073 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3074 | // Finish off the Attributes and check them |
| 3075 | AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3076 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3077 | InvokeInst *II = InvokeInst::Create(Callee, cast<BasicBlock>(NormalBB), |
| 3078 | cast<BasicBlock>(UnwindBB), |
| 3079 | Args.begin(), Args.end()); |
| 3080 | II->setCallingConv(CC); |
| 3081 | II->setAttributes(PAL); |
| 3082 | Inst = II; |
| 3083 | return false; |
| 3084 | } |
| 3085 | |
| 3086 | |
| 3087 | |
| 3088 | //===----------------------------------------------------------------------===// |
| 3089 | // Binary Operators. |
| 3090 | //===----------------------------------------------------------------------===// |
| 3091 | |
| 3092 | /// ParseArithmetic |
Chris Lattner | e914b59 | 2009-01-05 08:24:46 +0000 | [diff] [blame] | 3093 | /// ::= ArithmeticOps TypeAndValue ',' Value |
| 3094 | /// |
| 3095 | /// If OperandType is 0, then any FP or integer operand is allowed. If it is 1, |
| 3096 | /// 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] | 3097 | bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS, |
Chris Lattner | e914b59 | 2009-01-05 08:24:46 +0000 | [diff] [blame] | 3098 | unsigned Opc, unsigned OperandType) { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3099 | LocTy Loc; Value *LHS, *RHS; |
| 3100 | if (ParseTypeAndValue(LHS, Loc, PFS) || |
| 3101 | ParseToken(lltok::comma, "expected ',' in arithmetic operation") || |
| 3102 | ParseValue(LHS->getType(), RHS, PFS)) |
| 3103 | return true; |
| 3104 | |
Chris Lattner | e914b59 | 2009-01-05 08:24:46 +0000 | [diff] [blame] | 3105 | bool Valid; |
| 3106 | switch (OperandType) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3107 | default: llvm_unreachable("Unknown operand type!"); |
Chris Lattner | e914b59 | 2009-01-05 08:24:46 +0000 | [diff] [blame] | 3108 | case 0: // int or FP. |
| 3109 | Valid = LHS->getType()->isIntOrIntVector() || |
| 3110 | LHS->getType()->isFPOrFPVector(); |
| 3111 | break; |
| 3112 | case 1: Valid = LHS->getType()->isIntOrIntVector(); break; |
| 3113 | case 2: Valid = LHS->getType()->isFPOrFPVector(); break; |
| 3114 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3115 | |
Chris Lattner | e914b59 | 2009-01-05 08:24:46 +0000 | [diff] [blame] | 3116 | if (!Valid) |
| 3117 | return Error(Loc, "invalid operand type for instruction"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3118 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3119 | Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); |
| 3120 | return false; |
| 3121 | } |
| 3122 | |
| 3123 | /// ParseLogical |
| 3124 | /// ::= ArithmeticOps TypeAndValue ',' Value { |
| 3125 | bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS, |
| 3126 | unsigned Opc) { |
| 3127 | LocTy Loc; Value *LHS, *RHS; |
| 3128 | if (ParseTypeAndValue(LHS, Loc, PFS) || |
| 3129 | ParseToken(lltok::comma, "expected ',' in logical operation") || |
| 3130 | ParseValue(LHS->getType(), RHS, PFS)) |
| 3131 | return true; |
| 3132 | |
| 3133 | if (!LHS->getType()->isIntOrIntVector()) |
| 3134 | return Error(Loc,"instruction requires integer or integer vector operands"); |
| 3135 | |
| 3136 | Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); |
| 3137 | return false; |
| 3138 | } |
| 3139 | |
| 3140 | |
| 3141 | /// ParseCompare |
| 3142 | /// ::= 'icmp' IPredicates TypeAndValue ',' Value |
| 3143 | /// ::= 'fcmp' FPredicates TypeAndValue ',' Value |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3144 | bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS, |
| 3145 | unsigned Opc) { |
| 3146 | // Parse the integer/fp comparison predicate. |
| 3147 | LocTy Loc; |
| 3148 | unsigned Pred; |
| 3149 | Value *LHS, *RHS; |
| 3150 | if (ParseCmpPredicate(Pred, Opc) || |
| 3151 | ParseTypeAndValue(LHS, Loc, PFS) || |
| 3152 | ParseToken(lltok::comma, "expected ',' after compare value") || |
| 3153 | ParseValue(LHS->getType(), RHS, PFS)) |
| 3154 | return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3155 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3156 | if (Opc == Instruction::FCmp) { |
| 3157 | if (!LHS->getType()->isFPOrFPVector()) |
| 3158 | return Error(Loc, "fcmp requires floating point operands"); |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3159 | Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS); |
Nick Lewycky | 7f6aa2b | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 3160 | } else { |
| 3161 | assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!"); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3162 | if (!LHS->getType()->isIntOrIntVector() && |
| 3163 | !isa<PointerType>(LHS->getType())) |
| 3164 | return Error(Loc, "icmp requires integer operands"); |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3165 | Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3166 | } |
| 3167 | return false; |
| 3168 | } |
| 3169 | |
| 3170 | //===----------------------------------------------------------------------===// |
| 3171 | // Other Instructions. |
| 3172 | //===----------------------------------------------------------------------===// |
| 3173 | |
| 3174 | |
| 3175 | /// ParseCast |
| 3176 | /// ::= CastOpc TypeAndValue 'to' Type |
| 3177 | bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS, |
| 3178 | unsigned Opc) { |
| 3179 | LocTy Loc; Value *Op; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3180 | PATypeHolder DestTy(Type::getVoidTy(Context)); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3181 | if (ParseTypeAndValue(Op, Loc, PFS) || |
| 3182 | ParseToken(lltok::kw_to, "expected 'to' after cast value") || |
| 3183 | ParseType(DestTy)) |
| 3184 | return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3185 | |
Chris Lattner | f6f0bdf | 2009-03-01 00:53:13 +0000 | [diff] [blame] | 3186 | if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) { |
| 3187 | CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3188 | return Error(Loc, "invalid cast opcode for cast from '" + |
| 3189 | Op->getType()->getDescription() + "' to '" + |
| 3190 | DestTy->getDescription() + "'"); |
Chris Lattner | f6f0bdf | 2009-03-01 00:53:13 +0000 | [diff] [blame] | 3191 | } |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3192 | Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy); |
| 3193 | return false; |
| 3194 | } |
| 3195 | |
| 3196 | /// ParseSelect |
| 3197 | /// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue |
| 3198 | bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) { |
| 3199 | LocTy Loc; |
| 3200 | Value *Op0, *Op1, *Op2; |
| 3201 | if (ParseTypeAndValue(Op0, Loc, PFS) || |
| 3202 | ParseToken(lltok::comma, "expected ',' after select condition") || |
| 3203 | ParseTypeAndValue(Op1, PFS) || |
| 3204 | ParseToken(lltok::comma, "expected ',' after select value") || |
| 3205 | ParseTypeAndValue(Op2, PFS)) |
| 3206 | return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3207 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3208 | if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2)) |
| 3209 | return Error(Loc, Reason); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3210 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3211 | Inst = SelectInst::Create(Op0, Op1, Op2); |
| 3212 | return false; |
| 3213 | } |
| 3214 | |
Chris Lattner | 0088a5c | 2009-01-05 08:18:44 +0000 | [diff] [blame] | 3215 | /// ParseVA_Arg |
| 3216 | /// ::= 'va_arg' TypeAndValue ',' Type |
| 3217 | bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) { |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3218 | Value *Op; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3219 | PATypeHolder EltTy(Type::getVoidTy(Context)); |
Chris Lattner | 0088a5c | 2009-01-05 08:18:44 +0000 | [diff] [blame] | 3220 | LocTy TypeLoc; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3221 | if (ParseTypeAndValue(Op, PFS) || |
| 3222 | ParseToken(lltok::comma, "expected ',' after vaarg operand") || |
Chris Lattner | 0088a5c | 2009-01-05 08:18:44 +0000 | [diff] [blame] | 3223 | ParseType(EltTy, TypeLoc)) |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3224 | return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3225 | |
Chris Lattner | 0088a5c | 2009-01-05 08:18:44 +0000 | [diff] [blame] | 3226 | if (!EltTy->isFirstClassType()) |
| 3227 | return Error(TypeLoc, "va_arg requires operand with first class type"); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3228 | |
| 3229 | Inst = new VAArgInst(Op, EltTy); |
| 3230 | return false; |
| 3231 | } |
| 3232 | |
| 3233 | /// ParseExtractElement |
| 3234 | /// ::= 'extractelement' TypeAndValue ',' TypeAndValue |
| 3235 | bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) { |
| 3236 | LocTy Loc; |
| 3237 | Value *Op0, *Op1; |
| 3238 | if (ParseTypeAndValue(Op0, Loc, PFS) || |
| 3239 | ParseToken(lltok::comma, "expected ',' after extract value") || |
| 3240 | ParseTypeAndValue(Op1, PFS)) |
| 3241 | return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3242 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3243 | if (!ExtractElementInst::isValidOperands(Op0, Op1)) |
| 3244 | return Error(Loc, "invalid extractelement operands"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3245 | |
Eric Christopher | a3500da | 2009-07-25 02:28:41 +0000 | [diff] [blame] | 3246 | Inst = ExtractElementInst::Create(Op0, Op1); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3247 | return false; |
| 3248 | } |
| 3249 | |
| 3250 | /// ParseInsertElement |
| 3251 | /// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue |
| 3252 | bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) { |
| 3253 | LocTy Loc; |
| 3254 | Value *Op0, *Op1, *Op2; |
| 3255 | if (ParseTypeAndValue(Op0, Loc, PFS) || |
| 3256 | ParseToken(lltok::comma, "expected ',' after insertelement value") || |
| 3257 | ParseTypeAndValue(Op1, PFS) || |
| 3258 | ParseToken(lltok::comma, "expected ',' after insertelement value") || |
| 3259 | ParseTypeAndValue(Op2, PFS)) |
| 3260 | return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3261 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3262 | if (!InsertElementInst::isValidOperands(Op0, Op1, Op2)) |
Eric Christopher | 0aaf4e9 | 2009-07-23 01:01:32 +0000 | [diff] [blame] | 3263 | return Error(Loc, "invalid insertelement operands"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3264 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3265 | Inst = InsertElementInst::Create(Op0, Op1, Op2); |
| 3266 | return false; |
| 3267 | } |
| 3268 | |
| 3269 | /// ParseShuffleVector |
| 3270 | /// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue |
| 3271 | bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) { |
| 3272 | LocTy Loc; |
| 3273 | Value *Op0, *Op1, *Op2; |
| 3274 | if (ParseTypeAndValue(Op0, Loc, PFS) || |
| 3275 | ParseToken(lltok::comma, "expected ',' after shuffle mask") || |
| 3276 | ParseTypeAndValue(Op1, PFS) || |
| 3277 | ParseToken(lltok::comma, "expected ',' after shuffle value") || |
| 3278 | ParseTypeAndValue(Op2, PFS)) |
| 3279 | return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3280 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3281 | if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2)) |
| 3282 | return Error(Loc, "invalid extractelement operands"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3283 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3284 | Inst = new ShuffleVectorInst(Op0, Op1, Op2); |
| 3285 | return false; |
| 3286 | } |
| 3287 | |
| 3288 | /// ParsePHI |
| 3289 | /// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Valueß ']')* |
| 3290 | bool LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3291 | PATypeHolder Ty(Type::getVoidTy(Context)); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3292 | Value *Op0, *Op1; |
| 3293 | LocTy TypeLoc = Lex.getLoc(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3294 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3295 | if (ParseType(Ty) || |
| 3296 | ParseToken(lltok::lsquare, "expected '[' in phi value list") || |
| 3297 | ParseValue(Ty, Op0, PFS) || |
| 3298 | ParseToken(lltok::comma, "expected ',' after insertelement value") || |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3299 | ParseValue(Type::getLabelTy(Context), Op1, PFS) || |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3300 | ParseToken(lltok::rsquare, "expected ']' in phi value list")) |
| 3301 | return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3302 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3303 | SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals; |
| 3304 | while (1) { |
| 3305 | PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1))); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3306 | |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 3307 | if (!EatIfPresent(lltok::comma)) |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3308 | break; |
| 3309 | |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 3310 | if (ParseToken(lltok::lsquare, "expected '[' in phi value list") || |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3311 | ParseValue(Ty, Op0, PFS) || |
| 3312 | ParseToken(lltok::comma, "expected ',' after insertelement value") || |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3313 | ParseValue(Type::getLabelTy(Context), Op1, PFS) || |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3314 | ParseToken(lltok::rsquare, "expected ']' in phi value list")) |
| 3315 | return true; |
| 3316 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3317 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3318 | if (!Ty->isFirstClassType()) |
| 3319 | return Error(TypeLoc, "phi node must have first class type"); |
| 3320 | |
| 3321 | PHINode *PN = PHINode::Create(Ty); |
| 3322 | PN->reserveOperandSpace(PHIVals.size()); |
| 3323 | for (unsigned i = 0, e = PHIVals.size(); i != e; ++i) |
| 3324 | PN->addIncoming(PHIVals[i].first, PHIVals[i].second); |
| 3325 | Inst = PN; |
| 3326 | return false; |
| 3327 | } |
| 3328 | |
| 3329 | /// ParseCall |
| 3330 | /// ::= 'tail'? 'call' OptionalCallingConv OptionalAttrs Type Value |
| 3331 | /// ParameterList OptionalAttrs |
| 3332 | bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS, |
| 3333 | bool isTail) { |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 3334 | unsigned RetAttrs, FnAttrs; |
| 3335 | CallingConv::ID CC; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3336 | PATypeHolder RetType(Type::getVoidTy(Context)); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3337 | LocTy RetTypeLoc; |
| 3338 | ValID CalleeID; |
| 3339 | SmallVector<ParamInfo, 16> ArgList; |
| 3340 | LocTy CallLoc = Lex.getLoc(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3341 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3342 | if ((isTail && ParseToken(lltok::kw_call, "expected 'tail call'")) || |
| 3343 | ParseOptionalCallingConv(CC) || |
| 3344 | ParseOptionalAttrs(RetAttrs, 1) || |
Chris Lattner | a9a9e07 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 3345 | ParseType(RetType, RetTypeLoc, true /*void allowed*/) || |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3346 | ParseValID(CalleeID) || |
| 3347 | ParseParameterList(ArgList, PFS) || |
| 3348 | ParseOptionalAttrs(FnAttrs, 2)) |
| 3349 | return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3350 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3351 | // If RetType is a non-function pointer type, then this is the short syntax |
| 3352 | // for the call, which means that RetType is just the return type. Infer the |
| 3353 | // rest of the function argument types from the arguments that are present. |
| 3354 | const PointerType *PFTy = 0; |
| 3355 | const FunctionType *Ty = 0; |
| 3356 | if (!(PFTy = dyn_cast<PointerType>(RetType)) || |
| 3357 | !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) { |
| 3358 | // Pull out the types of all of the arguments... |
| 3359 | std::vector<const Type*> ParamTypes; |
| 3360 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) |
| 3361 | ParamTypes.push_back(ArgList[i].V->getType()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3362 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3363 | if (!FunctionType::isValidReturnType(RetType)) |
| 3364 | return Error(RetTypeLoc, "Invalid result type for LLVM function"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3365 | |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 3366 | Ty = FunctionType::get(RetType, ParamTypes, false); |
| 3367 | PFTy = PointerType::getUnqual(Ty); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3368 | } |
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 | // Look up the callee. |
| 3371 | Value *Callee; |
| 3372 | if (ConvertValIDToValue(PFTy, CalleeID, Callee, PFS)) return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3373 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3374 | // FIXME: In LLVM 3.0, stop accepting zext, sext and inreg as optional |
| 3375 | // function attributes. |
| 3376 | unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg; |
| 3377 | if (FnAttrs & ObsoleteFuncAttrs) { |
| 3378 | RetAttrs |= FnAttrs & ObsoleteFuncAttrs; |
| 3379 | FnAttrs &= ~ObsoleteFuncAttrs; |
| 3380 | } |
| 3381 | |
| 3382 | // Set up the Attributes for the function. |
| 3383 | SmallVector<AttributeWithIndex, 8> Attrs; |
| 3384 | if (RetAttrs != Attribute::None) |
| 3385 | Attrs.push_back(AttributeWithIndex::get(0, RetAttrs)); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3386 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3387 | SmallVector<Value*, 8> Args; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3388 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3389 | // Loop through FunctionType's arguments and ensure they are specified |
| 3390 | // correctly. Also, gather any parameter attributes. |
| 3391 | FunctionType::param_iterator I = Ty->param_begin(); |
| 3392 | FunctionType::param_iterator E = Ty->param_end(); |
| 3393 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { |
| 3394 | const Type *ExpectedTy = 0; |
| 3395 | if (I != E) { |
| 3396 | ExpectedTy = *I++; |
| 3397 | } else if (!Ty->isVarArg()) { |
| 3398 | return Error(ArgList[i].Loc, "too many arguments specified"); |
| 3399 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3400 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3401 | if (ExpectedTy && ExpectedTy != ArgList[i].V->getType()) |
| 3402 | return Error(ArgList[i].Loc, "argument is not of expected type '" + |
| 3403 | ExpectedTy->getDescription() + "'"); |
| 3404 | Args.push_back(ArgList[i].V); |
| 3405 | if (ArgList[i].Attrs != Attribute::None) |
| 3406 | Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs)); |
| 3407 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3408 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3409 | if (I != E) |
| 3410 | return Error(CallLoc, "not enough parameters specified for call"); |
| 3411 | |
| 3412 | if (FnAttrs != Attribute::None) |
| 3413 | Attrs.push_back(AttributeWithIndex::get(~0, FnAttrs)); |
| 3414 | |
| 3415 | // Finish off the Attributes and check them |
| 3416 | AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3417 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3418 | CallInst *CI = CallInst::Create(Callee, Args.begin(), Args.end()); |
| 3419 | CI->setTailCall(isTail); |
| 3420 | CI->setCallingConv(CC); |
| 3421 | CI->setAttributes(PAL); |
| 3422 | Inst = CI; |
| 3423 | return false; |
| 3424 | } |
| 3425 | |
| 3426 | //===----------------------------------------------------------------------===// |
| 3427 | // Memory Instructions. |
| 3428 | //===----------------------------------------------------------------------===// |
| 3429 | |
| 3430 | /// ParseAlloc |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 3431 | /// ::= 'malloc' Type (',' TypeAndValue)? (',' OptionalInfo)? |
| 3432 | /// ::= 'alloca' Type (',' TypeAndValue)? (',' OptionalInfo)? |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3433 | bool LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS, |
| 3434 | unsigned Opc) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3435 | PATypeHolder Ty(Type::getVoidTy(Context)); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3436 | Value *Size = 0; |
Chris Lattner | eeb4a84 | 2009-07-02 23:08:13 +0000 | [diff] [blame] | 3437 | LocTy SizeLoc; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3438 | unsigned Alignment = 0; |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 3439 | if (ParseType(Ty)) return true; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3440 | |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 3441 | if (EatIfPresent(lltok::comma)) { |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 3442 | if (Lex.getKind() == lltok::kw_align || Lex.getKind() == lltok::kw_dbg) { |
| 3443 | if (ParseOptionalInfo(Alignment)) return true; |
| 3444 | } else { |
| 3445 | if (ParseTypeAndValue(Size, SizeLoc, PFS)) return true; |
| 3446 | if (EatIfPresent(lltok::comma)) |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3447 | if (ParseOptionalInfo(Alignment)) return true; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3448 | } |
| 3449 | } |
| 3450 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3451 | if (Size && Size->getType() != Type::getInt32Ty(Context)) |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3452 | return Error(SizeLoc, "element count must be i32"); |
| 3453 | |
| 3454 | if (Opc == Instruction::Malloc) |
Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 3455 | Inst = new MallocInst(Ty, Size, Alignment); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3456 | else |
Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 3457 | Inst = new AllocaInst(Ty, Size, Alignment); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3458 | return false; |
| 3459 | } |
| 3460 | |
| 3461 | /// ParseFree |
| 3462 | /// ::= 'free' TypeAndValue |
| 3463 | bool LLParser::ParseFree(Instruction *&Inst, PerFunctionState &PFS) { |
| 3464 | Value *Val; LocTy Loc; |
| 3465 | if (ParseTypeAndValue(Val, Loc, PFS)) return true; |
| 3466 | if (!isa<PointerType>(Val->getType())) |
| 3467 | return Error(Loc, "operand to free must be a pointer"); |
| 3468 | Inst = new FreeInst(Val); |
| 3469 | return false; |
| 3470 | } |
| 3471 | |
| 3472 | /// ParseLoad |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 3473 | /// ::= 'volatile'? 'load' TypeAndValue (',' OptionalInfo)? |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3474 | bool LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS, |
| 3475 | bool isVolatile) { |
| 3476 | Value *Val; LocTy Loc; |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 3477 | unsigned Alignment = 0; |
| 3478 | if (ParseTypeAndValue(Val, Loc, PFS)) return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3479 | |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 3480 | if (EatIfPresent(lltok::comma)) |
| 3481 | if (ParseOptionalInfo(Alignment)) return true; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3482 | |
| 3483 | if (!isa<PointerType>(Val->getType()) || |
| 3484 | !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType()) |
| 3485 | 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] | 3486 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3487 | Inst = new LoadInst(Val, "", isVolatile, Alignment); |
| 3488 | return false; |
| 3489 | } |
| 3490 | |
| 3491 | /// ParseStore |
Dan Gohman | a119de8 | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 3492 | /// ::= 'volatile'? 'store' TypeAndValue ',' TypeAndValue (',' 'align' i32)? |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3493 | bool LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS, |
| 3494 | bool isVolatile) { |
| 3495 | Value *Val, *Ptr; LocTy Loc, PtrLoc; |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 3496 | unsigned Alignment = 0; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3497 | if (ParseTypeAndValue(Val, Loc, PFS) || |
| 3498 | ParseToken(lltok::comma, "expected ',' after store operand") || |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 3499 | ParseTypeAndValue(Ptr, PtrLoc, PFS)) |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3500 | return true; |
Devang Patel | f633a06 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 3501 | |
| 3502 | if (EatIfPresent(lltok::comma)) |
| 3503 | if (ParseOptionalInfo(Alignment)) return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3504 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3505 | if (!isa<PointerType>(Ptr->getType())) |
| 3506 | return Error(PtrLoc, "store operand must be a pointer"); |
| 3507 | if (!Val->getType()->isFirstClassType()) |
| 3508 | return Error(Loc, "store operand must be a first class value"); |
| 3509 | if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType()) |
| 3510 | return Error(Loc, "stored value and pointer type do not match"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3511 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3512 | Inst = new StoreInst(Val, Ptr, isVolatile, Alignment); |
| 3513 | return false; |
| 3514 | } |
| 3515 | |
| 3516 | /// ParseGetResult |
Dan Gohman | a119de8 | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 3517 | /// ::= 'getresult' TypeAndValue ',' i32 |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3518 | /// FIXME: Remove support for getresult in LLVM 3.0 |
| 3519 | bool LLParser::ParseGetResult(Instruction *&Inst, PerFunctionState &PFS) { |
| 3520 | Value *Val; LocTy ValLoc, EltLoc; |
| 3521 | unsigned Element; |
| 3522 | if (ParseTypeAndValue(Val, ValLoc, PFS) || |
| 3523 | ParseToken(lltok::comma, "expected ',' after getresult operand") || |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 3524 | ParseUInt32(Element, EltLoc)) |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3525 | return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3526 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3527 | if (!isa<StructType>(Val->getType()) && !isa<ArrayType>(Val->getType())) |
| 3528 | return Error(ValLoc, "getresult inst requires an aggregate operand"); |
| 3529 | if (!ExtractValueInst::getIndexedType(Val->getType(), Element)) |
| 3530 | return Error(EltLoc, "invalid getresult index for value"); |
| 3531 | Inst = ExtractValueInst::Create(Val, Element); |
| 3532 | return false; |
| 3533 | } |
| 3534 | |
| 3535 | /// ParseGetElementPtr |
Dan Gohman | dd8004d | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 3536 | /// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)* |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3537 | bool LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) { |
| 3538 | Value *Ptr, *Val; LocTy Loc, EltLoc; |
Dan Gohman | dd8004d | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 3539 | |
Dan Gohman | dcb40a3 | 2009-07-29 15:58:36 +0000 | [diff] [blame] | 3540 | bool InBounds = EatIfPresent(lltok::kw_inbounds); |
Dan Gohman | dd8004d | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 3541 | |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 3542 | if (ParseTypeAndValue(Ptr, Loc, PFS)) return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3543 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3544 | if (!isa<PointerType>(Ptr->getType())) |
| 3545 | return Error(Loc, "base of getelementptr must be a pointer"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3546 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3547 | SmallVector<Value*, 16> Indices; |
Chris Lattner | 3ed88ef | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 3548 | while (EatIfPresent(lltok::comma)) { |
| 3549 | if (ParseTypeAndValue(Val, EltLoc, PFS)) return true; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3550 | if (!isa<IntegerType>(Val->getType())) |
| 3551 | return Error(EltLoc, "getelementptr index must be an integer"); |
| 3552 | Indices.push_back(Val); |
| 3553 | } |
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 | if (!GetElementPtrInst::getIndexedType(Ptr->getType(), |
| 3556 | Indices.begin(), Indices.end())) |
| 3557 | return Error(Loc, "invalid getelementptr indices"); |
| 3558 | Inst = GetElementPtrInst::Create(Ptr, Indices.begin(), Indices.end()); |
Dan Gohman | dd8004d | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 3559 | if (InBounds) |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 3560 | cast<GetElementPtrInst>(Inst)->setIsInBounds(true); |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3561 | return false; |
| 3562 | } |
| 3563 | |
| 3564 | /// ParseExtractValue |
| 3565 | /// ::= 'extractvalue' TypeAndValue (',' uint32)+ |
| 3566 | bool LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) { |
| 3567 | Value *Val; LocTy Loc; |
| 3568 | SmallVector<unsigned, 4> Indices; |
| 3569 | if (ParseTypeAndValue(Val, Loc, PFS) || |
| 3570 | ParseIndexList(Indices)) |
| 3571 | return true; |
| 3572 | |
| 3573 | if (!isa<StructType>(Val->getType()) && !isa<ArrayType>(Val->getType())) |
| 3574 | return Error(Loc, "extractvalue operand must be array or struct"); |
| 3575 | |
| 3576 | if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(), |
| 3577 | Indices.end())) |
| 3578 | return Error(Loc, "invalid indices for extractvalue"); |
| 3579 | Inst = ExtractValueInst::Create(Val, Indices.begin(), Indices.end()); |
| 3580 | return false; |
| 3581 | } |
| 3582 | |
| 3583 | /// ParseInsertValue |
| 3584 | /// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+ |
| 3585 | bool LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) { |
| 3586 | Value *Val0, *Val1; LocTy Loc0, Loc1; |
| 3587 | SmallVector<unsigned, 4> Indices; |
| 3588 | if (ParseTypeAndValue(Val0, Loc0, PFS) || |
| 3589 | ParseToken(lltok::comma, "expected comma after insertvalue operand") || |
| 3590 | ParseTypeAndValue(Val1, Loc1, PFS) || |
| 3591 | ParseIndexList(Indices)) |
| 3592 | return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3593 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3594 | if (!isa<StructType>(Val0->getType()) && !isa<ArrayType>(Val0->getType())) |
| 3595 | return Error(Loc0, "extractvalue operand must be array or struct"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3596 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3597 | if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(), |
| 3598 | Indices.end())) |
| 3599 | return Error(Loc0, "invalid indices for insertvalue"); |
| 3600 | Inst = InsertValueInst::Create(Val0, Val1, Indices.begin(), Indices.end()); |
| 3601 | return false; |
| 3602 | } |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 3603 | |
| 3604 | //===----------------------------------------------------------------------===// |
| 3605 | // Embedded metadata. |
| 3606 | //===----------------------------------------------------------------------===// |
| 3607 | |
| 3608 | /// ParseMDNodeVector |
Nick Lewycky | cb33799 | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 3609 | /// ::= Element (',' Element)* |
| 3610 | /// Element |
| 3611 | /// ::= 'null' | TypeAndValue |
| 3612 | bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts) { |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 3613 | assert(Lex.getKind() == lltok::lbrace); |
| 3614 | Lex.Lex(); |
| 3615 | do { |
Devang Patel | db5e900 | 2009-07-23 01:36:16 +0000 | [diff] [blame] | 3616 | Value *V = 0; |
Nick Lewycky | cb33799 | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 3617 | if (Lex.getKind() == lltok::kw_null) { |
| 3618 | Lex.Lex(); |
| 3619 | V = 0; |
| 3620 | } else { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3621 | PATypeHolder Ty(Type::getVoidTy(Context)); |
Devang Patel | e54abc9 | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 3622 | if (ParseType(Ty)) return true; |
| 3623 | if (Lex.getKind() == lltok::Metadata) { |
| 3624 | Lex.Lex(); |
Devang Patel | 104cf9e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 3625 | MetadataBase *Node = 0; |
Devang Patel | e54abc9 | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 3626 | if (!ParseMDNode(Node)) |
| 3627 | V = Node; |
| 3628 | else { |
| 3629 | MetadataBase *MDS = 0; |
| 3630 | if (ParseMDString(MDS)) return true; |
| 3631 | V = MDS; |
| 3632 | } |
| 3633 | } else { |
| 3634 | Constant *C; |
| 3635 | if (ParseGlobalValue(Ty, C)) return true; |
| 3636 | V = C; |
| 3637 | } |
Nick Lewycky | cb33799 | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 3638 | } |
| 3639 | Elts.push_back(V); |
Nick Lewycky | 21cc446 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 3640 | } while (EatIfPresent(lltok::comma)); |
| 3641 | |
| 3642 | return false; |
| 3643 | } |