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