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