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