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