Chris Lattner | ac161bf | 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" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallPtrSet.h" |
Chandler Carruth | 9106521 | 2014-03-05 10:34:14 +0000 | [diff] [blame] | 16 | #include "llvm/IR/AutoUpgrade.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/CallingConv.h" |
| 18 | #include "llvm/IR/Constants.h" |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 19 | #include "llvm/IR/DebugInfoMetadata.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/DerivedTypes.h" |
| 21 | #include "llvm/IR/InlineAsm.h" |
| 22 | #include "llvm/IR/Instructions.h" |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 23 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Module.h" |
| 25 | #include "llvm/IR/Operator.h" |
| 26 | #include "llvm/IR/ValueSymbolTable.h" |
Duncan P. N. Exon Smith | 9748607 | 2015-02-03 21:56:01 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Dwarf.h" |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
Duncan P. N. Exon Smith | 1716990 | 2014-08-19 00:13:19 +0000 | [diff] [blame] | 29 | #include "llvm/Support/SaveAndRestore.h" |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
| 31 | using namespace llvm; |
| 32 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 33 | static std::string getTypeString(Type *T) { |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 34 | std::string Result; |
| 35 | raw_string_ostream Tmp(Result); |
| 36 | Tmp << *T; |
| 37 | return Tmp.str(); |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 40 | /// Run: module ::= toplevelentity* |
Chris Lattner | ad6f335 | 2009-01-04 20:44:11 +0000 | [diff] [blame] | 41 | bool LLParser::Run() { |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 42 | // Prime the lexer. |
| 43 | Lex.Lex(); |
| 44 | |
Chris Lattner | ad6f335 | 2009-01-04 20:44:11 +0000 | [diff] [blame] | 45 | return ParseTopLevelEntities() || |
| 46 | ValidateEndOfModule(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /// ValidateEndOfModule - Do final validity and sanity checks at the end of the |
| 50 | /// module. |
| 51 | bool LLParser::ValidateEndOfModule() { |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 52 | for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++) |
| 53 | UpgradeInstWithTBAATag(InstsWithTBAATag[I]); |
| 54 | |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 55 | // Handle any function attribute group forward references. |
| 56 | for (std::map<Value*, std::vector<unsigned> >::iterator |
| 57 | I = ForwardRefAttrGroups.begin(), E = ForwardRefAttrGroups.end(); |
| 58 | I != E; ++I) { |
| 59 | Value *V = I->first; |
| 60 | std::vector<unsigned> &Vec = I->second; |
| 61 | AttrBuilder B; |
| 62 | |
| 63 | for (std::vector<unsigned>::iterator VI = Vec.begin(), VE = Vec.end(); |
| 64 | VI != VE; ++VI) |
| 65 | B.merge(NumberedAttrBuilders[*VI]); |
| 66 | |
| 67 | if (Function *Fn = dyn_cast<Function>(V)) { |
| 68 | AttributeSet AS = Fn->getAttributes(); |
| 69 | AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex); |
| 70 | AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex, |
| 71 | AS.getFnAttributes()); |
| 72 | |
| 73 | FnAttrs.merge(B); |
| 74 | |
| 75 | // If the alignment was parsed as an attribute, move to the alignment |
| 76 | // field. |
| 77 | if (FnAttrs.hasAlignmentAttr()) { |
| 78 | Fn->setAlignment(FnAttrs.getAlignment()); |
| 79 | FnAttrs.removeAttribute(Attribute::Alignment); |
| 80 | } |
| 81 | |
| 82 | AS = AS.addAttributes(Context, AttributeSet::FunctionIndex, |
| 83 | AttributeSet::get(Context, |
| 84 | AttributeSet::FunctionIndex, |
| 85 | FnAttrs)); |
| 86 | Fn->setAttributes(AS); |
| 87 | } else if (CallInst *CI = dyn_cast<CallInst>(V)) { |
| 88 | AttributeSet AS = CI->getAttributes(); |
| 89 | AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex); |
| 90 | AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex, |
| 91 | AS.getFnAttributes()); |
Bill Wendling | 59dce37 | 2013-02-12 10:13:06 +0000 | [diff] [blame] | 92 | FnAttrs.merge(B); |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 93 | AS = AS.addAttributes(Context, AttributeSet::FunctionIndex, |
| 94 | AttributeSet::get(Context, |
| 95 | AttributeSet::FunctionIndex, |
| 96 | FnAttrs)); |
| 97 | CI->setAttributes(AS); |
| 98 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) { |
| 99 | AttributeSet AS = II->getAttributes(); |
| 100 | AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex); |
| 101 | AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex, |
| 102 | AS.getFnAttributes()); |
Bill Wendling | 59dce37 | 2013-02-12 10:13:06 +0000 | [diff] [blame] | 103 | FnAttrs.merge(B); |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 104 | AS = AS.addAttributes(Context, AttributeSet::FunctionIndex, |
| 105 | AttributeSet::get(Context, |
| 106 | AttributeSet::FunctionIndex, |
| 107 | FnAttrs)); |
| 108 | II->setAttributes(AS); |
| 109 | } else { |
| 110 | llvm_unreachable("invalid object with forward attribute group reference"); |
| 111 | } |
| 112 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 113 | |
Duncan P. N. Exon Smith | 1716990 | 2014-08-19 00:13:19 +0000 | [diff] [blame] | 114 | // If there are entries in ForwardRefBlockAddresses at this point, the |
| 115 | // function was never defined. |
| 116 | if (!ForwardRefBlockAddresses.empty()) |
| 117 | return Error(ForwardRefBlockAddresses.begin()->first.Loc, |
| 118 | "expected function name in blockaddress"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 119 | |
David Majnemer | 19b5105 | 2015-02-11 07:43:56 +0000 | [diff] [blame] | 120 | for (const auto &NT : NumberedTypes) |
| 121 | if (NT.second.second.isValid()) |
| 122 | return Error(NT.second.second, |
| 123 | "use of undefined type '%" + Twine(NT.first) + "'"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 124 | |
| 125 | for (StringMap<std::pair<Type*, LocTy> >::iterator I = |
| 126 | NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) |
| 127 | if (I->second.second.isValid()) |
| 128 | return Error(I->second.second, |
| 129 | "use of undefined type named '" + I->getKey() + "'"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 130 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 131 | if (!ForwardRefComdats.empty()) |
| 132 | return Error(ForwardRefComdats.begin()->second, |
| 133 | "use of undefined comdat '$" + |
| 134 | ForwardRefComdats.begin()->first + "'"); |
| 135 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 136 | if (!ForwardRefVals.empty()) |
| 137 | return Error(ForwardRefVals.begin()->second.second, |
| 138 | "use of undefined value '@" + ForwardRefVals.begin()->first + |
| 139 | "'"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 140 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 141 | if (!ForwardRefValIDs.empty()) |
| 142 | return Error(ForwardRefValIDs.begin()->second.second, |
| 143 | "use of undefined value '@" + |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 144 | Twine(ForwardRefValIDs.begin()->first) + "'"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 145 | |
Devang Patel | d254115 | 2009-07-08 19:23:54 +0000 | [diff] [blame] | 146 | if (!ForwardRefMDNodes.empty()) |
| 147 | return Error(ForwardRefMDNodes.begin()->second.second, |
| 148 | "use of undefined metadata '!" + |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 149 | Twine(ForwardRefMDNodes.begin()->first) + "'"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 150 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 151 | // Resolve metadata cycles. |
David Majnemer | 19b5105 | 2015-02-11 07:43:56 +0000 | [diff] [blame] | 152 | for (auto &N : NumberedMetadata) { |
| 153 | if (N.second && !N.second->isResolved()) |
| 154 | N.second->resolveCycles(); |
| 155 | } |
Devang Patel | d254115 | 2009-07-08 19:23:54 +0000 | [diff] [blame] | 156 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 157 | // Look for intrinsic functions and CallInst that need to be upgraded |
| 158 | for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ) |
| 159 | UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 160 | |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 161 | UpgradeDebugInfo(*M); |
| 162 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 163 | return false; |
| 164 | } |
| 165 | |
| 166 | //===----------------------------------------------------------------------===// |
| 167 | // Top-Level Entities |
| 168 | //===----------------------------------------------------------------------===// |
| 169 | |
| 170 | bool LLParser::ParseTopLevelEntities() { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 171 | while (1) { |
| 172 | switch (Lex.getKind()) { |
| 173 | default: return TokError("expected top-level entity"); |
| 174 | case lltok::Eof: return false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 175 | case lltok::kw_declare: if (ParseDeclare()) return true; break; |
| 176 | case lltok::kw_define: if (ParseDefine()) return true; break; |
| 177 | case lltok::kw_module: if (ParseModuleAsm()) return true; break; |
| 178 | case lltok::kw_target: if (ParseTargetDefinition()) return true; break; |
Bill Wendling | 706d3d6 | 2012-11-28 08:41:48 +0000 | [diff] [blame] | 179 | case lltok::kw_deplibs: if (ParseDepLibs()) return true; break; |
Dan Gohman | 466876b | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 180 | case lltok::LocalVarID: if (ParseUnnamedType()) return true; break; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 181 | case lltok::LocalVar: if (ParseNamedType()) return true; break; |
Dan Gohman | 466876b | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 182 | case lltok::GlobalID: if (ParseUnnamedGlobal()) return true; break; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 183 | case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break; |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 184 | case lltok::ComdatVar: if (parseComdat()) return true; break; |
Chris Lattner | 1eed2d6 | 2009-12-30 04:56:59 +0000 | [diff] [blame] | 185 | case lltok::exclaim: if (ParseStandaloneMetadata()) return true; break; |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 186 | case lltok::MetadataVar:if (ParseNamedMetadata()) return true; break; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 187 | |
| 188 | // The Global variable production with no name can have many different |
| 189 | // optional leading prefixes, the production is: |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 190 | // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass |
| 191 | // OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr |
Rafael Espindola | 45e6c19 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 192 | // ('constant'|'global') ... |
Bill Wendling | 03bcd6e | 2010-07-01 21:55:59 +0000 | [diff] [blame] | 193 | case lltok::kw_private: // OptionalLinkage |
Bill Wendling | 03bcd6e | 2010-07-01 21:55:59 +0000 | [diff] [blame] | 194 | case lltok::kw_internal: // OptionalLinkage |
| 195 | case lltok::kw_weak: // OptionalLinkage |
| 196 | case lltok::kw_weak_odr: // OptionalLinkage |
| 197 | case lltok::kw_linkonce: // OptionalLinkage |
| 198 | case lltok::kw_linkonce_odr: // OptionalLinkage |
| 199 | case lltok::kw_appending: // OptionalLinkage |
Bill Wendling | 03bcd6e | 2010-07-01 21:55:59 +0000 | [diff] [blame] | 200 | case lltok::kw_common: // OptionalLinkage |
Bill Wendling | 03bcd6e | 2010-07-01 21:55:59 +0000 | [diff] [blame] | 201 | case lltok::kw_extern_weak: // OptionalLinkage |
Rafael Espindola | 52b7442 | 2014-06-03 20:00:20 +0000 | [diff] [blame] | 202 | case lltok::kw_external: // OptionalLinkage |
| 203 | case lltok::kw_default: // OptionalVisibility |
| 204 | case lltok::kw_hidden: // OptionalVisibility |
| 205 | case lltok::kw_protected: // OptionalVisibility |
Rafael Espindola | 63e92fb | 2014-06-03 20:07:32 +0000 | [diff] [blame] | 206 | case lltok::kw_dllimport: // OptionalDLLStorageClass |
| 207 | case lltok::kw_dllexport: // OptionalDLLStorageClass |
Rafael Espindola | 52b7442 | 2014-06-03 20:00:20 +0000 | [diff] [blame] | 208 | case lltok::kw_thread_local: // OptionalThreadLocal |
| 209 | case lltok::kw_addrspace: // OptionalAddrSpace |
| 210 | case lltok::kw_constant: // GlobalType |
| 211 | case lltok::kw_global: { // GlobalType |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 212 | unsigned Linkage, Visibility, DLLStorageClass; |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 213 | bool UnnamedAddr; |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 214 | GlobalVariable::ThreadLocalMode TLM; |
Rafael Espindola | 52b7442 | 2014-06-03 20:00:20 +0000 | [diff] [blame] | 215 | bool HasLinkage; |
| 216 | if (ParseOptionalLinkage(Linkage, HasLinkage) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 217 | ParseOptionalVisibility(Visibility) || |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 218 | ParseOptionalDLLStorageClass(DLLStorageClass) || |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 219 | ParseOptionalThreadLocal(TLM) || |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 220 | parseOptionalUnnamedAddr(UnnamedAddr) || |
Rafael Espindola | 52b7442 | 2014-06-03 20:00:20 +0000 | [diff] [blame] | 221 | ParseGlobal("", SMLoc(), Linkage, HasLinkage, Visibility, |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 222 | DLLStorageClass, TLM, UnnamedAddr)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 223 | return true; |
| 224 | break; |
| 225 | } |
Bill Wendling | a7c3877 | 2013-02-09 15:48:49 +0000 | [diff] [blame] | 226 | |
| 227 | case lltok::kw_attributes: if (ParseUnnamedAttrGrp()) return true; break; |
Duncan P. N. Exon Smith | 0a448fb | 2014-08-19 21:30:15 +0000 | [diff] [blame] | 228 | case lltok::kw_uselistorder: if (ParseUseListOrder()) return true; break; |
| 229 | case lltok::kw_uselistorder_bb: |
| 230 | if (ParseUseListOrderBB()) return true; break; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | |
| 236 | /// toplevelentity |
| 237 | /// ::= 'module' 'asm' STRINGCONSTANT |
| 238 | bool LLParser::ParseModuleAsm() { |
| 239 | assert(Lex.getKind() == lltok::kw_module); |
| 240 | Lex.Lex(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 241 | |
| 242 | std::string AsmStr; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 243 | if (ParseToken(lltok::kw_asm, "expected 'module asm'") || |
| 244 | ParseStringConstant(AsmStr)) return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 245 | |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 246 | M->appendModuleInlineAsm(AsmStr); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 247 | return false; |
| 248 | } |
| 249 | |
| 250 | /// toplevelentity |
| 251 | /// ::= 'target' 'triple' '=' STRINGCONSTANT |
| 252 | /// ::= 'target' 'datalayout' '=' STRINGCONSTANT |
| 253 | bool LLParser::ParseTargetDefinition() { |
| 254 | assert(Lex.getKind() == lltok::kw_target); |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 255 | std::string Str; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 256 | switch (Lex.Lex()) { |
| 257 | default: return TokError("unknown target property"); |
| 258 | case lltok::kw_triple: |
| 259 | Lex.Lex(); |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 260 | if (ParseToken(lltok::equal, "expected '=' after target triple") || |
| 261 | ParseStringConstant(Str)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 262 | return true; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 263 | M->setTargetTriple(Str); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 264 | return false; |
| 265 | case lltok::kw_datalayout: |
| 266 | Lex.Lex(); |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 267 | if (ParseToken(lltok::equal, "expected '=' after target datalayout") || |
| 268 | ParseStringConstant(Str)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 269 | return true; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 270 | M->setDataLayout(Str); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 271 | return false; |
| 272 | } |
| 273 | } |
| 274 | |
Bill Wendling | 706d3d6 | 2012-11-28 08:41:48 +0000 | [diff] [blame] | 275 | /// toplevelentity |
| 276 | /// ::= 'deplibs' '=' '[' ']' |
| 277 | /// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']' |
| 278 | /// FIXME: Remove in 4.0. Currently parse, but ignore. |
| 279 | bool LLParser::ParseDepLibs() { |
| 280 | assert(Lex.getKind() == lltok::kw_deplibs); |
| 281 | Lex.Lex(); |
| 282 | if (ParseToken(lltok::equal, "expected '=' after deplibs") || |
| 283 | ParseToken(lltok::lsquare, "expected '=' after deplibs")) |
| 284 | return true; |
| 285 | |
| 286 | if (EatIfPresent(lltok::rsquare)) |
| 287 | return false; |
| 288 | |
| 289 | do { |
| 290 | std::string Str; |
| 291 | if (ParseStringConstant(Str)) return true; |
| 292 | } while (EatIfPresent(lltok::comma)); |
| 293 | |
| 294 | return ParseToken(lltok::rsquare, "expected ']' at end of list"); |
| 295 | } |
| 296 | |
Dan Gohman | 466876b | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 297 | /// ParseUnnamedType: |
Dan Gohman | 466876b | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 298 | /// ::= LocalVarID '=' 'type' type |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 299 | bool LLParser::ParseUnnamedType() { |
Chris Lattner | 0703736 | 2011-06-18 23:51:31 +0000 | [diff] [blame] | 300 | LocTy TypeLoc = Lex.getLoc(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 301 | unsigned TypeID = Lex.getUIntVal(); |
Chris Lattner | 8936d2b | 2011-06-19 00:03:46 +0000 | [diff] [blame] | 302 | Lex.Lex(); // eat LocalVarID; |
| 303 | |
| 304 | if (ParseToken(lltok::equal, "expected '=' after name") || |
| 305 | ParseToken(lltok::kw_type, "expected 'type' after '='")) |
| 306 | return true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 307 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 308 | Type *Result = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 309 | if (ParseStructDefinition(TypeLoc, "", |
| 310 | NumberedTypes[TypeID], Result)) return true; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 311 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 312 | if (!isa<StructType>(Result)) { |
| 313 | std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID]; |
| 314 | if (Entry.first) |
| 315 | return Error(TypeLoc, "non-struct types may not be recursive"); |
| 316 | Entry.first = Result; |
| 317 | Entry.second = SMLoc(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 318 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 319 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 320 | return false; |
| 321 | } |
| 322 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 323 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 324 | /// toplevelentity |
| 325 | /// ::= LocalVar '=' 'type' type |
| 326 | bool LLParser::ParseNamedType() { |
| 327 | std::string Name = Lex.getStrVal(); |
| 328 | LocTy NameLoc = Lex.getLoc(); |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 329 | Lex.Lex(); // eat LocalVar. |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 330 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 331 | if (ParseToken(lltok::equal, "expected '=' after name") || |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 332 | ParseToken(lltok::kw_type, "expected 'type' after name")) |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 333 | return true; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 334 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 335 | Type *Result = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 336 | if (ParseStructDefinition(NameLoc, Name, |
| 337 | NamedTypes[Name], Result)) return true; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 338 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 339 | if (!isa<StructType>(Result)) { |
| 340 | std::pair<Type*, LocTy> &Entry = NamedTypes[Name]; |
| 341 | if (Entry.first) |
| 342 | return Error(NameLoc, "non-struct types may not be recursive"); |
| 343 | Entry.first = Result; |
| 344 | Entry.second = SMLoc(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 345 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 346 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 347 | return false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | |
| 351 | /// toplevelentity |
| 352 | /// ::= 'declare' FunctionHeader |
| 353 | bool LLParser::ParseDeclare() { |
| 354 | assert(Lex.getKind() == lltok::kw_declare); |
| 355 | Lex.Lex(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 356 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 357 | Function *F; |
| 358 | return ParseFunctionHeader(F, false); |
| 359 | } |
| 360 | |
| 361 | /// toplevelentity |
| 362 | /// ::= 'define' FunctionHeader '{' ... |
| 363 | bool LLParser::ParseDefine() { |
| 364 | assert(Lex.getKind() == lltok::kw_define); |
| 365 | Lex.Lex(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 366 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 367 | Function *F; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 368 | return ParseFunctionHeader(F, true) || |
| 369 | ParseFunctionBody(*F); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 372 | /// ParseGlobalType |
| 373 | /// ::= 'constant' |
| 374 | /// ::= 'global' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 375 | bool LLParser::ParseGlobalType(bool &IsConstant) { |
| 376 | if (Lex.getKind() == lltok::kw_constant) |
| 377 | IsConstant = true; |
| 378 | else if (Lex.getKind() == lltok::kw_global) |
| 379 | IsConstant = false; |
Duncan Sands | d1de45a | 2009-02-10 16:24:55 +0000 | [diff] [blame] | 380 | else { |
| 381 | IsConstant = false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 382 | return TokError("expected 'global' or 'constant'"); |
Duncan Sands | d1de45a | 2009-02-10 16:24:55 +0000 | [diff] [blame] | 383 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 384 | Lex.Lex(); |
| 385 | return false; |
| 386 | } |
| 387 | |
Dan Gohman | 466876b | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 388 | /// ParseUnnamedGlobal: |
| 389 | /// OptionalVisibility ALIAS ... |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 390 | /// OptionalLinkage OptionalVisibility OptionalDLLStorageClass |
| 391 | /// ... -> global variable |
Dan Gohman | 466876b | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 392 | /// GlobalID '=' OptionalVisibility ALIAS ... |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 393 | /// GlobalID '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass |
| 394 | /// ... -> global variable |
Dan Gohman | 466876b | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 395 | bool LLParser::ParseUnnamedGlobal() { |
| 396 | unsigned VarID = NumberedVals.size(); |
| 397 | std::string Name; |
| 398 | LocTy NameLoc = Lex.getLoc(); |
| 399 | |
| 400 | // Handle the GlobalID form. |
| 401 | if (Lex.getKind() == lltok::GlobalID) { |
| 402 | if (Lex.getUIntVal() != VarID) |
| 403 | return Error(Lex.getLoc(), "variable expected to be numbered '%" + |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 404 | Twine(VarID) + "'"); |
Dan Gohman | 466876b | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 405 | Lex.Lex(); // eat GlobalID; |
| 406 | |
| 407 | if (ParseToken(lltok::equal, "expected '=' after name")) |
| 408 | return true; |
| 409 | } |
| 410 | |
| 411 | bool HasLinkage; |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 412 | unsigned Linkage, Visibility, DLLStorageClass; |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 413 | GlobalVariable::ThreadLocalMode TLM; |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 414 | bool UnnamedAddr; |
Dan Gohman | 466876b | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 415 | if (ParseOptionalLinkage(Linkage, HasLinkage) || |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 416 | ParseOptionalVisibility(Visibility) || |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 417 | ParseOptionalDLLStorageClass(DLLStorageClass) || |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 418 | ParseOptionalThreadLocal(TLM) || |
| 419 | parseOptionalUnnamedAddr(UnnamedAddr)) |
Dan Gohman | 466876b | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 420 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 421 | |
Rafael Espindola | 464fe02 | 2014-07-30 22:51:54 +0000 | [diff] [blame] | 422 | if (Lex.getKind() != lltok::kw_alias) |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 423 | return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility, |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 424 | DLLStorageClass, TLM, UnnamedAddr); |
Rafael Espindola | 464fe02 | 2014-07-30 22:51:54 +0000 | [diff] [blame] | 425 | return ParseAlias(Name, NameLoc, Linkage, Visibility, DLLStorageClass, TLM, |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 426 | UnnamedAddr); |
Dan Gohman | 466876b | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 429 | /// ParseNamedGlobal: |
| 430 | /// GlobalVar '=' OptionalVisibility ALIAS ... |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 431 | /// GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass |
| 432 | /// ... -> global variable |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 433 | bool LLParser::ParseNamedGlobal() { |
| 434 | assert(Lex.getKind() == lltok::GlobalVar); |
| 435 | LocTy NameLoc = Lex.getLoc(); |
| 436 | std::string Name = Lex.getStrVal(); |
| 437 | Lex.Lex(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 438 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 439 | bool HasLinkage; |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 440 | unsigned Linkage, Visibility, DLLStorageClass; |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 441 | GlobalVariable::ThreadLocalMode TLM; |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 442 | bool UnnamedAddr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 443 | if (ParseToken(lltok::equal, "expected '=' in global variable") || |
| 444 | ParseOptionalLinkage(Linkage, HasLinkage) || |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 445 | ParseOptionalVisibility(Visibility) || |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 446 | ParseOptionalDLLStorageClass(DLLStorageClass) || |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 447 | ParseOptionalThreadLocal(TLM) || |
| 448 | parseOptionalUnnamedAddr(UnnamedAddr)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 449 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 450 | |
Rafael Espindola | 464fe02 | 2014-07-30 22:51:54 +0000 | [diff] [blame] | 451 | if (Lex.getKind() != lltok::kw_alias) |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 452 | return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility, |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 453 | DLLStorageClass, TLM, UnnamedAddr); |
Rafael Espindola | 464fe02 | 2014-07-30 22:51:54 +0000 | [diff] [blame] | 454 | |
| 455 | return ParseAlias(Name, NameLoc, Linkage, Visibility, DLLStorageClass, TLM, |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 456 | UnnamedAddr); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 457 | } |
| 458 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 459 | bool LLParser::parseComdat() { |
| 460 | assert(Lex.getKind() == lltok::ComdatVar); |
| 461 | std::string Name = Lex.getStrVal(); |
| 462 | LocTy NameLoc = Lex.getLoc(); |
| 463 | Lex.Lex(); |
| 464 | |
| 465 | if (ParseToken(lltok::equal, "expected '=' here")) |
| 466 | return true; |
| 467 | |
| 468 | if (ParseToken(lltok::kw_comdat, "expected comdat keyword")) |
| 469 | return TokError("expected comdat type"); |
| 470 | |
| 471 | Comdat::SelectionKind SK; |
| 472 | switch (Lex.getKind()) { |
| 473 | default: |
| 474 | return TokError("unknown selection kind"); |
| 475 | case lltok::kw_any: |
| 476 | SK = Comdat::Any; |
| 477 | break; |
| 478 | case lltok::kw_exactmatch: |
| 479 | SK = Comdat::ExactMatch; |
| 480 | break; |
| 481 | case lltok::kw_largest: |
| 482 | SK = Comdat::Largest; |
| 483 | break; |
| 484 | case lltok::kw_noduplicates: |
| 485 | SK = Comdat::NoDuplicates; |
| 486 | break; |
| 487 | case lltok::kw_samesize: |
| 488 | SK = Comdat::SameSize; |
| 489 | break; |
| 490 | } |
| 491 | Lex.Lex(); |
| 492 | |
| 493 | // See if the comdat was forward referenced, if so, use the comdat. |
| 494 | Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable(); |
| 495 | Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name); |
| 496 | if (I != ComdatSymTab.end() && !ForwardRefComdats.erase(Name)) |
| 497 | return Error(NameLoc, "redefinition of comdat '$" + Name + "'"); |
| 498 | |
| 499 | Comdat *C; |
| 500 | if (I != ComdatSymTab.end()) |
| 501 | C = &I->second; |
| 502 | else |
| 503 | C = M->getOrInsertComdat(Name); |
| 504 | C->setSelectionKind(SK); |
| 505 | |
| 506 | return false; |
| 507 | } |
| 508 | |
Devang Patel | 8ff0f8d | 2009-07-20 19:00:08 +0000 | [diff] [blame] | 509 | // MDString: |
| 510 | // ::= '!' STRINGCONSTANT |
Chris Lattner | 1797fc7 | 2009-12-29 21:53:55 +0000 | [diff] [blame] | 511 | bool LLParser::ParseMDString(MDString *&Result) { |
Devang Patel | 8ff0f8d | 2009-07-20 19:00:08 +0000 | [diff] [blame] | 512 | std::string Str; |
| 513 | if (ParseStringConstant(Str)) return true; |
Eli Bendersky | 5d5e18d | 2014-06-25 15:41:00 +0000 | [diff] [blame] | 514 | llvm::UpgradeMDStringConstant(Str); |
Chris Lattner | 1797fc7 | 2009-12-29 21:53:55 +0000 | [diff] [blame] | 515 | Result = MDString::get(Context, Str); |
Devang Patel | 8ff0f8d | 2009-07-20 19:00:08 +0000 | [diff] [blame] | 516 | return false; |
| 517 | } |
| 518 | |
| 519 | // MDNode: |
| 520 | // ::= '!' MDNodeNumber |
Chris Lattner | 6dac02a | 2009-12-30 04:15:23 +0000 | [diff] [blame] | 521 | bool LLParser::ParseMDNodeID(MDNode *&Result) { |
Devang Patel | 8ff0f8d | 2009-07-20 19:00:08 +0000 | [diff] [blame] | 522 | // !{ ..., !42, ... } |
| 523 | unsigned MID = 0; |
Duncan P. N. Exon Smith | a8d9a02 | 2015-01-12 21:14:38 +0000 | [diff] [blame] | 524 | if (ParseUInt32(MID)) |
| 525 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 526 | |
Chris Lattner | 8eff015 | 2010-04-01 05:14:45 +0000 | [diff] [blame] | 527 | // If not a forward reference, just return it now. |
David Majnemer | 19b5105 | 2015-02-11 07:43:56 +0000 | [diff] [blame] | 528 | if (NumberedMetadata.count(MID)) { |
Duncan P. N. Exon Smith | a8d9a02 | 2015-01-12 21:14:38 +0000 | [diff] [blame] | 529 | Result = NumberedMetadata[MID]; |
| 530 | return false; |
| 531 | } |
Devang Patel | 8ff0f8d | 2009-07-20 19:00:08 +0000 | [diff] [blame] | 532 | |
Chris Lattner | 8eff015 | 2010-04-01 05:14:45 +0000 | [diff] [blame] | 533 | // Otherwise, create MDNode forward reference. |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 534 | auto &FwdRef = ForwardRefMDNodes[MID]; |
| 535 | FwdRef = std::make_pair(MDTuple::getTemporary(Context, None), Lex.getLoc()); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 536 | |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 537 | Result = FwdRef.first.get(); |
| 538 | NumberedMetadata[MID].reset(Result); |
Devang Patel | 8ff0f8d | 2009-07-20 19:00:08 +0000 | [diff] [blame] | 539 | return false; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 540 | } |
Devang Patel | 8ff0f8d | 2009-07-20 19:00:08 +0000 | [diff] [blame] | 541 | |
Chris Lattner | f2fe7ff | 2009-12-29 22:35:39 +0000 | [diff] [blame] | 542 | /// ParseNamedMetadata: |
Devang Patel | be62697 | 2009-07-29 00:34:02 +0000 | [diff] [blame] | 543 | /// !foo = !{ !1, !2 } |
| 544 | bool LLParser::ParseNamedMetadata() { |
Chris Lattner | eafe4de | 2009-12-30 05:02:06 +0000 | [diff] [blame] | 545 | assert(Lex.getKind() == lltok::MetadataVar); |
Devang Patel | be62697 | 2009-07-29 00:34:02 +0000 | [diff] [blame] | 546 | std::string Name = Lex.getStrVal(); |
Chris Lattner | eafe4de | 2009-12-30 05:02:06 +0000 | [diff] [blame] | 547 | Lex.Lex(); |
Devang Patel | be62697 | 2009-07-29 00:34:02 +0000 | [diff] [blame] | 548 | |
Chris Lattner | f2fe7ff | 2009-12-29 22:35:39 +0000 | [diff] [blame] | 549 | if (ParseToken(lltok::equal, "expected '=' here") || |
Chris Lattner | 1eed2d6 | 2009-12-30 04:56:59 +0000 | [diff] [blame] | 550 | ParseToken(lltok::exclaim, "Expected '!' here") || |
Chris Lattner | f2fe7ff | 2009-12-29 22:35:39 +0000 | [diff] [blame] | 551 | ParseToken(lltok::lbrace, "Expected '{' here")) |
Devang Patel | be62697 | 2009-07-29 00:34:02 +0000 | [diff] [blame] | 552 | return true; |
| 553 | |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 554 | NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name); |
Dan Gohman | afd69cf | 2010-07-13 19:42:44 +0000 | [diff] [blame] | 555 | if (Lex.getKind() != lltok::rbrace) |
| 556 | do { |
Dan Gohman | afd69cf | 2010-07-13 19:42:44 +0000 | [diff] [blame] | 557 | if (ParseToken(lltok::exclaim, "Expected '!' here")) |
| 558 | return true; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 559 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 560 | MDNode *N = nullptr; |
Dan Gohman | afd69cf | 2010-07-13 19:42:44 +0000 | [diff] [blame] | 561 | if (ParseMDNodeID(N)) return true; |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 562 | NMD->addOperand(N); |
Dan Gohman | afd69cf | 2010-07-13 19:42:44 +0000 | [diff] [blame] | 563 | } while (EatIfPresent(lltok::comma)); |
Devang Patel | be62697 | 2009-07-29 00:34:02 +0000 | [diff] [blame] | 564 | |
| 565 | if (ParseToken(lltok::rbrace, "expected end of metadata node")) |
| 566 | return true; |
| 567 | |
Devang Patel | be62697 | 2009-07-29 00:34:02 +0000 | [diff] [blame] | 568 | return false; |
| 569 | } |
| 570 | |
Devang Patel | 39e64d4 | 2009-07-01 19:21:12 +0000 | [diff] [blame] | 571 | /// ParseStandaloneMetadata: |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 572 | /// !42 = !{...} |
Devang Patel | 39e64d4 | 2009-07-01 19:21:12 +0000 | [diff] [blame] | 573 | bool LLParser::ParseStandaloneMetadata() { |
Chris Lattner | 1eed2d6 | 2009-12-30 04:56:59 +0000 | [diff] [blame] | 574 | assert(Lex.getKind() == lltok::exclaim); |
Devang Patel | 39e64d4 | 2009-07-01 19:21:12 +0000 | [diff] [blame] | 575 | Lex.Lex(); |
| 576 | unsigned MetadataID = 0; |
Devang Patel | 39e64d4 | 2009-07-01 19:21:12 +0000 | [diff] [blame] | 577 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 578 | MDNode *Init; |
Chris Lattner | 278bc95 | 2009-12-29 22:40:21 +0000 | [diff] [blame] | 579 | if (ParseUInt32(MetadataID) || |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 580 | ParseToken(lltok::equal, "expected '=' here")) |
| 581 | return true; |
| 582 | |
| 583 | // Detect common error, from old metadata syntax. |
| 584 | if (Lex.getKind() == lltok::Type) |
| 585 | return TokError("unexpected type in metadata definition"); |
| 586 | |
Duncan P. N. Exon Smith | 090a19b | 2015-01-08 22:38:29 +0000 | [diff] [blame] | 587 | bool IsDistinct = EatIfPresent(lltok::kw_distinct); |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 588 | if (Lex.getKind() == lltok::MetadataVar) { |
| 589 | if (ParseSpecializedMDNode(Init, IsDistinct)) |
| 590 | return true; |
| 591 | } else if (ParseToken(lltok::exclaim, "Expected '!' here") || |
| 592 | ParseMDTuple(Init, IsDistinct)) |
Devang Patel | e059ba6e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 593 | return true; |
| 594 | |
Chris Lattner | fc58af2 | 2009-12-30 04:51:58 +0000 | [diff] [blame] | 595 | // See if this was forward referenced, if so, handle it. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 596 | auto FI = ForwardRefMDNodes.find(MetadataID); |
Devang Patel | d254115 | 2009-07-08 19:23:54 +0000 | [diff] [blame] | 597 | if (FI != ForwardRefMDNodes.end()) { |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 598 | FI->second.first->replaceAllUsesWith(Init); |
Devang Patel | d254115 | 2009-07-08 19:23:54 +0000 | [diff] [blame] | 599 | ForwardRefMDNodes.erase(FI); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 600 | |
Chris Lattner | fc58af2 | 2009-12-30 04:51:58 +0000 | [diff] [blame] | 601 | assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work"); |
| 602 | } else { |
David Majnemer | 19b5105 | 2015-02-11 07:43:56 +0000 | [diff] [blame] | 603 | if (NumberedMetadata.count(MetadataID)) |
Chris Lattner | fc58af2 | 2009-12-30 04:51:58 +0000 | [diff] [blame] | 604 | return TokError("Metadata id is already used"); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 605 | NumberedMetadata[MetadataID].reset(Init); |
Devang Patel | d254115 | 2009-07-08 19:23:54 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Devang Patel | 39e64d4 | 2009-07-01 19:21:12 +0000 | [diff] [blame] | 608 | return false; |
| 609 | } |
| 610 | |
Duncan P. N. Exon Smith | b80de10 | 2014-05-07 22:57:20 +0000 | [diff] [blame] | 611 | static bool isValidVisibilityForLinkage(unsigned V, unsigned L) { |
| 612 | return !GlobalValue::isLocalLinkage((GlobalValue::LinkageTypes)L) || |
| 613 | (GlobalValue::VisibilityTypes)V == GlobalValue::DefaultVisibility; |
| 614 | } |
| 615 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 616 | /// ParseAlias: |
Rafael Espindola | 464fe02 | 2014-07-30 22:51:54 +0000 | [diff] [blame] | 617 | /// ::= GlobalVar '=' OptionalLinkage OptionalVisibility |
| 618 | /// OptionalDLLStorageClass OptionalThreadLocal |
| 619 | /// OptionalUnNammedAddr 'alias' Aliasee |
Rafael Espindola | 6b23863 | 2014-05-16 19:35:39 +0000 | [diff] [blame] | 620 | /// |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 621 | /// Aliasee |
Chris Lattner | 4c73d7a | 2009-04-25 21:26:00 +0000 | [diff] [blame] | 622 | /// ::= TypeAndValue |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 623 | /// |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 624 | /// Everything through OptionalUnNammedAddr has already been parsed. |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 625 | /// |
Rafael Espindola | 464fe02 | 2014-07-30 22:51:54 +0000 | [diff] [blame] | 626 | bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc, unsigned L, |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 627 | unsigned Visibility, unsigned DLLStorageClass, |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 628 | GlobalVariable::ThreadLocalMode TLM, |
| 629 | bool UnnamedAddr) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 630 | assert(Lex.getKind() == lltok::kw_alias); |
| 631 | Lex.Lex(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 632 | |
Rafael Espindola | 7852705 | 2013-10-06 15:10:43 +0000 | [diff] [blame] | 633 | GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L; |
| 634 | |
Rafael Espindola | caa4356 | 2013-10-09 16:07:32 +0000 | [diff] [blame] | 635 | if(!GlobalAlias::isValidLinkage(Linkage)) |
Rafael Espindola | 464fe02 | 2014-07-30 22:51:54 +0000 | [diff] [blame] | 636 | return Error(NameLoc, "invalid linkage type for alias"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 637 | |
Duncan P. N. Exon Smith | b80de10 | 2014-05-07 22:57:20 +0000 | [diff] [blame] | 638 | if (!isValidVisibilityForLinkage(Visibility, L)) |
Rafael Espindola | 464fe02 | 2014-07-30 22:51:54 +0000 | [diff] [blame] | 639 | return Error(NameLoc, |
Duncan P. N. Exon Smith | b80de10 | 2014-05-07 22:57:20 +0000 | [diff] [blame] | 640 | "symbol with local linkage must have default visibility"); |
| 641 | |
Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 642 | Constant *Aliasee; |
| 643 | LocTy AliaseeLoc = Lex.getLoc(); |
| 644 | if (Lex.getKind() != lltok::kw_bitcast && |
| 645 | Lex.getKind() != lltok::kw_getelementptr && |
| 646 | Lex.getKind() != lltok::kw_addrspacecast && |
| 647 | Lex.getKind() != lltok::kw_inttoptr) { |
| 648 | if (ParseGlobalTypeAndValue(Aliasee)) |
Rafael Espindola | 6b23863 | 2014-05-16 19:35:39 +0000 | [diff] [blame] | 649 | return true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 650 | } else { |
Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 651 | // The bitcast dest type is not present, it is implied by the dest type. |
| 652 | ValID ID; |
| 653 | if (ParseValID(ID)) |
| 654 | return true; |
| 655 | if (ID.Kind != ValID::t_Constant) |
| 656 | return Error(AliaseeLoc, "invalid aliasee"); |
| 657 | Aliasee = ID.ConstantVal; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 658 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 659 | |
Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 660 | Type *AliaseeType = Aliasee->getType(); |
| 661 | auto *PTy = dyn_cast<PointerType>(AliaseeType); |
| 662 | if (!PTy) |
| 663 | return Error(AliaseeLoc, "An alias must have pointer type"); |
| 664 | Type *Ty = PTy->getElementType(); |
| 665 | unsigned AddrSpace = PTy->getAddressSpace(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 666 | |
| 667 | // Okay, create the alias but do not insert it into the module yet. |
Rafael Espindola | 4fe0094 | 2014-05-16 13:34:04 +0000 | [diff] [blame] | 668 | std::unique_ptr<GlobalAlias> GA( |
Rafael Espindola | f1bedd374 | 2014-05-17 21:29:57 +0000 | [diff] [blame] | 669 | GlobalAlias::create(Ty, AddrSpace, (GlobalValue::LinkageTypes)Linkage, |
| 670 | Name, Aliasee, /*Parent*/ nullptr)); |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 671 | GA->setThreadLocalMode(TLM); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 672 | GA->setVisibility((GlobalValue::VisibilityTypes)Visibility); |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 673 | GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass); |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 674 | GA->setUnnamedAddr(UnnamedAddr); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 675 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 676 | // See if this value already exists in the symbol table. If so, it is either |
| 677 | // a redefinition or a definition of a forward reference. |
Chris Lattner | e38317f | 2009-10-25 23:22:50 +0000 | [diff] [blame] | 678 | if (GlobalValue *Val = M->getNamedValue(Name)) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 679 | // See if this was a redefinition. If so, there is no entry in |
| 680 | // ForwardRefVals. |
| 681 | std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator |
| 682 | I = ForwardRefVals.find(Name); |
| 683 | if (I == ForwardRefVals.end()) |
| 684 | return Error(NameLoc, "redefinition of global named '@" + Name + "'"); |
| 685 | |
| 686 | // Otherwise, this was a definition of forward ref. Verify that types |
| 687 | // agree. |
| 688 | if (Val->getType() != GA->getType()) |
| 689 | return Error(NameLoc, |
| 690 | "forward reference and definition of alias have different types"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 691 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 692 | // If they agree, just RAUW the old value with the alias and remove the |
| 693 | // forward ref info. |
Rafael Espindola | aa27382 | 2014-05-09 21:49:17 +0000 | [diff] [blame] | 694 | Val->replaceAllUsesWith(GA.get()); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 695 | Val->eraseFromParent(); |
| 696 | ForwardRefVals.erase(I); |
| 697 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 698 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 699 | // Insert into the module, we know its name won't collide now. |
Rafael Espindola | aa27382 | 2014-05-09 21:49:17 +0000 | [diff] [blame] | 700 | M->getAliasList().push_back(GA.get()); |
Benjamin Kramer | 1dc34b4 | 2010-10-16 11:28:23 +0000 | [diff] [blame] | 701 | assert(GA->getName() == Name && "Should not be a name conflict!"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 702 | |
Rafael Espindola | aa27382 | 2014-05-09 21:49:17 +0000 | [diff] [blame] | 703 | // The module owns this now |
| 704 | GA.release(); |
| 705 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 706 | return false; |
| 707 | } |
| 708 | |
| 709 | /// ParseGlobal |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 710 | /// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 711 | /// OptionalThreadLocal OptionalUnNammedAddr OptionalAddrSpace |
Michael Gottesman | 27e7ef3 | 2013-02-05 05:57:38 +0000 | [diff] [blame] | 712 | /// OptionalExternallyInitialized GlobalType Type Const |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 713 | /// ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 714 | /// OptionalThreadLocal OptionalUnNammedAddr OptionalAddrSpace |
Michael Gottesman | 27e7ef3 | 2013-02-05 05:57:38 +0000 | [diff] [blame] | 715 | /// OptionalExternallyInitialized GlobalType Type Const |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 716 | /// |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 717 | /// Everything up to and including OptionalUnNammedAddr has been parsed |
David Majnemer | c4ab61c | 2014-03-09 06:41:58 +0000 | [diff] [blame] | 718 | /// already. |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 719 | /// |
| 720 | bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc, |
| 721 | unsigned Linkage, bool HasLinkage, |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 722 | unsigned Visibility, unsigned DLLStorageClass, |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 723 | GlobalVariable::ThreadLocalMode TLM, |
| 724 | bool UnnamedAddr) { |
Duncan P. N. Exon Smith | b80de10 | 2014-05-07 22:57:20 +0000 | [diff] [blame] | 725 | if (!isValidVisibilityForLinkage(Visibility, Linkage)) |
| 726 | return Error(NameLoc, |
| 727 | "symbol with local linkage must have default visibility"); |
| 728 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 729 | unsigned AddrSpace; |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 730 | bool IsConstant, IsExternallyInitialized; |
Michael Gottesman | 27e7ef3 | 2013-02-05 05:57:38 +0000 | [diff] [blame] | 731 | LocTy IsExternallyInitializedLoc; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 732 | LocTy TyLoc; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 733 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 734 | Type *Ty = nullptr; |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 735 | if (ParseOptionalAddrSpace(AddrSpace) || |
Michael Gottesman | 27e7ef3 | 2013-02-05 05:57:38 +0000 | [diff] [blame] | 736 | ParseOptionalToken(lltok::kw_externally_initialized, |
| 737 | IsExternallyInitialized, |
| 738 | &IsExternallyInitializedLoc) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 739 | ParseGlobalType(IsConstant) || |
| 740 | ParseType(Ty, TyLoc)) |
| 741 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 742 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 743 | // If the linkage is specified and is external, then no initializer is |
| 744 | // present. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 745 | Constant *Init = nullptr; |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 746 | if (!HasLinkage || (Linkage != GlobalValue::ExternalWeakLinkage && |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 747 | Linkage != GlobalValue::ExternalLinkage)) { |
| 748 | if (ParseGlobalValue(Ty, Init)) |
| 749 | return true; |
| 750 | } |
| 751 | |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 752 | if (Ty->isFunctionTy() || Ty->isLabelTy()) |
Chris Lattner | c9e1b48 | 2009-02-08 20:00:15 +0000 | [diff] [blame] | 753 | return Error(TyLoc, "invalid type for global variable"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 754 | |
David Majnemer | 598bd05 | 2014-12-09 05:56:09 +0000 | [diff] [blame] | 755 | GlobalValue *GVal = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 756 | |
| 757 | // See if the global was forward referenced, if so, use the global. |
Chris Lattner | 1f386b8 | 2009-02-02 07:24:28 +0000 | [diff] [blame] | 758 | if (!Name.empty()) { |
David Majnemer | 598bd05 | 2014-12-09 05:56:09 +0000 | [diff] [blame] | 759 | GVal = M->getNamedValue(Name); |
| 760 | if (GVal) { |
Chris Lattner | e38317f | 2009-10-25 23:22:50 +0000 | [diff] [blame] | 761 | if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal)) |
| 762 | return Error(NameLoc, "redefinition of global '@" + Name + "'"); |
Chris Lattner | e38317f | 2009-10-25 23:22:50 +0000 | [diff] [blame] | 763 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 764 | } else { |
| 765 | std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator |
| 766 | I = ForwardRefValIDs.find(NumberedVals.size()); |
| 767 | if (I != ForwardRefValIDs.end()) { |
David Majnemer | 598bd05 | 2014-12-09 05:56:09 +0000 | [diff] [blame] | 768 | GVal = I->second.first; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 769 | ForwardRefValIDs.erase(I); |
| 770 | } |
| 771 | } |
| 772 | |
David Majnemer | 598bd05 | 2014-12-09 05:56:09 +0000 | [diff] [blame] | 773 | GlobalVariable *GV; |
| 774 | if (!GVal) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 775 | GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, nullptr, |
| 776 | Name, nullptr, GlobalVariable::NotThreadLocal, |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 777 | AddrSpace); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 778 | } else { |
David Majnemer | 598bd05 | 2014-12-09 05:56:09 +0000 | [diff] [blame] | 779 | if (GVal->getType()->getElementType() != Ty) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 780 | return Error(TyLoc, |
| 781 | "forward reference and definition of global have different types"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 782 | |
David Majnemer | 598bd05 | 2014-12-09 05:56:09 +0000 | [diff] [blame] | 783 | GV = cast<GlobalVariable>(GVal); |
| 784 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 785 | // Move the forward-reference to the correct spot in the module. |
| 786 | M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV); |
| 787 | } |
| 788 | |
| 789 | if (Name.empty()) |
| 790 | NumberedVals.push_back(GV); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 791 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 792 | // Set the parsed properties on the global. |
| 793 | if (Init) |
| 794 | GV->setInitializer(Init); |
| 795 | GV->setConstant(IsConstant); |
| 796 | GV->setLinkage((GlobalValue::LinkageTypes)Linkage); |
| 797 | GV->setVisibility((GlobalValue::VisibilityTypes)Visibility); |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 798 | GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass); |
Michael Gottesman | 27e7ef3 | 2013-02-05 05:57:38 +0000 | [diff] [blame] | 799 | GV->setExternallyInitialized(IsExternallyInitialized); |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 800 | GV->setThreadLocalMode(TLM); |
Rafael Espindola | 45e6c19 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 801 | GV->setUnnamedAddr(UnnamedAddr); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 802 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 803 | // Parse attributes on the global. |
| 804 | while (Lex.getKind() == lltok::comma) { |
| 805 | Lex.Lex(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 806 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 807 | if (Lex.getKind() == lltok::kw_section) { |
| 808 | Lex.Lex(); |
| 809 | GV->setSection(Lex.getStrVal()); |
| 810 | if (ParseToken(lltok::StringConstant, "expected global section string")) |
| 811 | return true; |
| 812 | } else if (Lex.getKind() == lltok::kw_align) { |
| 813 | unsigned Alignment; |
| 814 | if (ParseOptionalAlignment(Alignment)) return true; |
| 815 | GV->setAlignment(Alignment); |
| 816 | } else { |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 817 | Comdat *C; |
Rafael Espindola | 83a362c | 2015-01-06 22:55:16 +0000 | [diff] [blame] | 818 | if (parseOptionalComdat(Name, C)) |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 819 | return true; |
| 820 | if (C) |
| 821 | GV->setComdat(C); |
| 822 | else |
| 823 | return TokError("unknown global variable property!"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 824 | } |
| 825 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 826 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 827 | return false; |
| 828 | } |
| 829 | |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 830 | /// ParseUnnamedAttrGrp |
Bill Wendling | a7c3877 | 2013-02-09 15:48:49 +0000 | [diff] [blame] | 831 | /// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}' |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 832 | bool LLParser::ParseUnnamedAttrGrp() { |
Bill Wendling | a7c3877 | 2013-02-09 15:48:49 +0000 | [diff] [blame] | 833 | assert(Lex.getKind() == lltok::kw_attributes); |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 834 | LocTy AttrGrpLoc = Lex.getLoc(); |
Bill Wendling | a7c3877 | 2013-02-09 15:48:49 +0000 | [diff] [blame] | 835 | Lex.Lex(); |
| 836 | |
David Majnemer | b39e22b | 2014-12-09 18:33:57 +0000 | [diff] [blame] | 837 | if (Lex.getKind() != lltok::AttrGrpID) |
| 838 | return TokError("expected attribute group id"); |
| 839 | |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 840 | unsigned VarID = Lex.getUIntVal(); |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 841 | std::vector<unsigned> unused; |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 842 | LocTy BuiltinLoc; |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 843 | Lex.Lex(); |
| 844 | |
| 845 | if (ParseToken(lltok::equal, "expected '=' here") || |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 846 | ParseToken(lltok::lbrace, "expected '{' here") || |
Bill Wendling | 09bd1f7 | 2013-02-22 00:12:35 +0000 | [diff] [blame] | 847 | ParseFnAttributeValuePairs(NumberedAttrBuilders[VarID], unused, true, |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 848 | BuiltinLoc) || |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 849 | ParseToken(lltok::rbrace, "expected end of attribute group")) |
| 850 | return true; |
| 851 | |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 852 | if (!NumberedAttrBuilders[VarID].hasAttributes()) |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 853 | return Error(AttrGrpLoc, "attribute group has no attributes"); |
| 854 | |
| 855 | return false; |
| 856 | } |
| 857 | |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 858 | /// ParseFnAttributeValuePairs |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 859 | /// ::= <attr> | <attr> '=' <value> |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 860 | bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B, |
| 861 | std::vector<unsigned> &FwdRefAttrGrps, |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 862 | bool inAttrGrp, LocTy &BuiltinLoc) { |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 863 | bool HaveError = false; |
| 864 | |
| 865 | B.clear(); |
| 866 | |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 867 | while (true) { |
| 868 | lltok::Kind Token = Lex.getKind(); |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 869 | if (Token == lltok::kw_builtin) |
| 870 | BuiltinLoc = Lex.getLoc(); |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 871 | switch (Token) { |
| 872 | default: |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 873 | if (!inAttrGrp) return HaveError; |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 874 | return Error(Lex.getLoc(), "unterminated attribute group"); |
| 875 | case lltok::rbrace: |
| 876 | // Finished. |
| 877 | return false; |
| 878 | |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 879 | case lltok::AttrGrpID: { |
| 880 | // Allow a function to reference an attribute group: |
| 881 | // |
| 882 | // define void @foo() #1 { ... } |
| 883 | if (inAttrGrp) |
| 884 | HaveError |= |
| 885 | Error(Lex.getLoc(), |
| 886 | "cannot have an attribute group reference in an attribute group"); |
| 887 | |
| 888 | unsigned AttrGrpNum = Lex.getUIntVal(); |
| 889 | if (inAttrGrp) break; |
| 890 | |
| 891 | // Save the reference to the attribute group. We'll fill it in later. |
| 892 | FwdRefAttrGrps.push_back(AttrGrpNum); |
| 893 | break; |
| 894 | } |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 895 | // Target-dependent attributes: |
| 896 | case lltok::StringConstant: { |
| 897 | std::string Attr = Lex.getStrVal(); |
| 898 | Lex.Lex(); |
| 899 | std::string Val; |
| 900 | if (EatIfPresent(lltok::equal) && |
| 901 | ParseStringConstant(Val)) |
| 902 | return true; |
| 903 | |
| 904 | B.addAttribute(Attr, Val); |
Bill Wendling | b1ea980 | 2013-02-10 10:12:50 +0000 | [diff] [blame] | 905 | continue; |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | // Target-independent attributes: |
| 909 | case lltok::kw_align: { |
Bill Wendling | c62789f | 2013-04-18 18:30:16 +0000 | [diff] [blame] | 910 | // As a hack, we allow function alignment to be initially parsed as an |
| 911 | // attribute on a function declaration/definition or added to an attribute |
| 912 | // group and later moved to the alignment field. |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 913 | unsigned Alignment; |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 914 | if (inAttrGrp) { |
Bill Wendling | 44b08bf | 2013-02-10 23:15:51 +0000 | [diff] [blame] | 915 | Lex.Lex(); |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 916 | if (ParseToken(lltok::equal, "expected '=' here") || |
| 917 | ParseUInt32(Alignment)) |
| 918 | return true; |
| 919 | } else { |
| 920 | if (ParseOptionalAlignment(Alignment)) |
| 921 | return true; |
| 922 | } |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 923 | B.addAlignmentAttr(Alignment); |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 924 | continue; |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 925 | } |
| 926 | case lltok::kw_alignstack: { |
| 927 | unsigned Alignment; |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 928 | if (inAttrGrp) { |
Bill Wendling | 44b08bf | 2013-02-10 23:15:51 +0000 | [diff] [blame] | 929 | Lex.Lex(); |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 930 | if (ParseToken(lltok::equal, "expected '=' here") || |
| 931 | ParseUInt32(Alignment)) |
| 932 | return true; |
| 933 | } else { |
| 934 | if (ParseOptionalStackAlignment(Alignment)) |
| 935 | return true; |
| 936 | } |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 937 | B.addStackAlignmentAttr(Alignment); |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 938 | continue; |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 939 | } |
Kostya Serebryany | cf880b9 | 2013-02-26 06:58:09 +0000 | [diff] [blame] | 940 | case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break; |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 941 | case lltok::kw_builtin: B.addAttribute(Attribute::Builtin); break; |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 942 | case lltok::kw_cold: B.addAttribute(Attribute::Cold); break; |
Kostya Serebryany | cf880b9 | 2013-02-26 06:58:09 +0000 | [diff] [blame] | 943 | case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break; |
Tom Roeder | 44cb65f | 2014-06-05 19:29:43 +0000 | [diff] [blame] | 944 | case lltok::kw_jumptable: B.addAttribute(Attribute::JumpTable); break; |
Kostya Serebryany | cf880b9 | 2013-02-26 06:58:09 +0000 | [diff] [blame] | 945 | case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break; |
| 946 | case lltok::kw_naked: B.addAttribute(Attribute::Naked); break; |
| 947 | case lltok::kw_nobuiltin: B.addAttribute(Attribute::NoBuiltin); break; |
| 948 | case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break; |
| 949 | case lltok::kw_noimplicitfloat: B.addAttribute(Attribute::NoImplicitFloat); break; |
| 950 | case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break; |
| 951 | case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break; |
| 952 | case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break; |
| 953 | case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break; |
| 954 | case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break; |
Andrea Di Biagio | 377496b | 2013-08-23 11:53:55 +0000 | [diff] [blame] | 955 | case lltok::kw_optnone: B.addAttribute(Attribute::OptimizeNone); break; |
Kostya Serebryany | cf880b9 | 2013-02-26 06:58:09 +0000 | [diff] [blame] | 956 | case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break; |
| 957 | case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break; |
| 958 | case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break; |
| 959 | case lltok::kw_returns_twice: B.addAttribute(Attribute::ReturnsTwice); break; |
| 960 | case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break; |
| 961 | case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break; |
| 962 | case lltok::kw_sspstrong: B.addAttribute(Attribute::StackProtectStrong); break; |
| 963 | case lltok::kw_sanitize_address: B.addAttribute(Attribute::SanitizeAddress); break; |
| 964 | case lltok::kw_sanitize_thread: B.addAttribute(Attribute::SanitizeThread); break; |
| 965 | case lltok::kw_sanitize_memory: B.addAttribute(Attribute::SanitizeMemory); break; |
| 966 | case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break; |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 967 | |
| 968 | // Error handling. |
| 969 | case lltok::kw_inreg: |
| 970 | case lltok::kw_signext: |
| 971 | case lltok::kw_zeroext: |
| 972 | HaveError |= |
| 973 | Error(Lex.getLoc(), |
| 974 | "invalid use of attribute on a function"); |
| 975 | break; |
| 976 | case lltok::kw_byval: |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 977 | case lltok::kw_dereferenceable: |
Reid Kleckner | a534a38 | 2013-12-19 02:14:12 +0000 | [diff] [blame] | 978 | case lltok::kw_inalloca: |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 979 | case lltok::kw_nest: |
| 980 | case lltok::kw_noalias: |
| 981 | case lltok::kw_nocapture: |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 982 | case lltok::kw_nonnull: |
Stephen Lin | b8bd232 | 2013-04-20 05:14:40 +0000 | [diff] [blame] | 983 | case lltok::kw_returned: |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 984 | case lltok::kw_sret: |
| 985 | HaveError |= |
| 986 | Error(Lex.getLoc(), |
| 987 | "invalid use of parameter-only attribute on a function"); |
| 988 | break; |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | Lex.Lex(); |
| 992 | } |
| 993 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 994 | |
| 995 | //===----------------------------------------------------------------------===// |
| 996 | // GlobalValue Reference/Resolution Routines. |
| 997 | //===----------------------------------------------------------------------===// |
| 998 | |
| 999 | /// GetGlobalVal - Get a value with the specified name or ID, creating a |
| 1000 | /// forward reference record if needed. This can return null if the value |
| 1001 | /// exists but does not have the right type. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1002 | GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty, |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1003 | LocTy Loc) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1004 | PointerType *PTy = dyn_cast<PointerType>(Ty); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1005 | if (!PTy) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1006 | Error(Loc, "global variable reference must have pointer type"); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1007 | return nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1008 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1009 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1010 | // Look this name up in the normal function symbol table. |
| 1011 | GlobalValue *Val = |
| 1012 | cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name)); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1013 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1014 | // If this is a forward reference for the value, see if we already created a |
| 1015 | // forward ref record. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1016 | if (!Val) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1017 | std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator |
| 1018 | I = ForwardRefVals.find(Name); |
| 1019 | if (I != ForwardRefVals.end()) |
| 1020 | Val = I->second.first; |
| 1021 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1022 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1023 | // If we have the value in the symbol table or fwd-ref table, return it. |
| 1024 | if (Val) { |
| 1025 | if (Val->getType() == Ty) return Val; |
| 1026 | Error(Loc, "'@" + Name + "' defined with type '" + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 1027 | getTypeString(Val->getType()) + "'"); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1028 | return nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1029 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1030 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1031 | // Otherwise, create a new forward reference for this value and remember it. |
| 1032 | GlobalValue *FwdVal; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1033 | if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType())) |
Duncan Sands | e288105 | 2009-03-11 08:08:06 +0000 | [diff] [blame] | 1034 | FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1035 | else |
Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 1036 | FwdVal = new GlobalVariable(*M, PTy->getElementType(), false, |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1037 | GlobalValue::ExternalWeakLinkage, nullptr, Name, |
| 1038 | nullptr, GlobalVariable::NotThreadLocal, |
Justin Holewinski | 898a0a0 | 2012-11-16 21:03:47 +0000 | [diff] [blame] | 1039 | PTy->getAddressSpace()); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1040 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1041 | ForwardRefVals[Name] = std::make_pair(FwdVal, Loc); |
| 1042 | return FwdVal; |
| 1043 | } |
| 1044 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1045 | GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) { |
| 1046 | PointerType *PTy = dyn_cast<PointerType>(Ty); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1047 | if (!PTy) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1048 | Error(Loc, "global variable reference must have pointer type"); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1049 | return nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1050 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1051 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1052 | GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1053 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1054 | // If this is a forward reference for the value, see if we already created a |
| 1055 | // forward ref record. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1056 | if (!Val) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1057 | std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator |
| 1058 | I = ForwardRefValIDs.find(ID); |
| 1059 | if (I != ForwardRefValIDs.end()) |
| 1060 | Val = I->second.first; |
| 1061 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1062 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1063 | // If we have the value in the symbol table or fwd-ref table, return it. |
| 1064 | if (Val) { |
| 1065 | if (Val->getType() == Ty) return Val; |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 1066 | Error(Loc, "'@" + Twine(ID) + "' defined with type '" + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 1067 | getTypeString(Val->getType()) + "'"); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1068 | return nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1069 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1070 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1071 | // Otherwise, create a new forward reference for this value and remember it. |
| 1072 | GlobalValue *FwdVal; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1073 | if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType())) |
Duncan Sands | e288105 | 2009-03-11 08:08:06 +0000 | [diff] [blame] | 1074 | FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1075 | else |
Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 1076 | FwdVal = new GlobalVariable(*M, PTy->getElementType(), false, |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1077 | GlobalValue::ExternalWeakLinkage, nullptr, ""); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1078 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1079 | ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc); |
| 1080 | return FwdVal; |
| 1081 | } |
| 1082 | |
| 1083 | |
| 1084 | //===----------------------------------------------------------------------===// |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1085 | // Comdat Reference/Resolution Routines. |
| 1086 | //===----------------------------------------------------------------------===// |
| 1087 | |
| 1088 | Comdat *LLParser::getComdat(const std::string &Name, LocTy Loc) { |
| 1089 | // Look this name up in the comdat symbol table. |
| 1090 | Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable(); |
| 1091 | Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name); |
| 1092 | if (I != ComdatSymTab.end()) |
| 1093 | return &I->second; |
| 1094 | |
| 1095 | // Otherwise, create a new forward reference for this value and remember it. |
| 1096 | Comdat *C = M->getOrInsertComdat(Name); |
| 1097 | ForwardRefComdats[Name] = Loc; |
| 1098 | return C; |
| 1099 | } |
| 1100 | |
| 1101 | |
| 1102 | //===----------------------------------------------------------------------===// |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1103 | // Helper Routines. |
| 1104 | //===----------------------------------------------------------------------===// |
| 1105 | |
| 1106 | /// ParseToken - If the current token has the specified kind, eat it and return |
| 1107 | /// success. Otherwise, emit the specified error and return failure. |
| 1108 | bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) { |
| 1109 | if (Lex.getKind() != T) |
| 1110 | return TokError(ErrMsg); |
| 1111 | Lex.Lex(); |
| 1112 | return false; |
| 1113 | } |
| 1114 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1115 | /// ParseStringConstant |
| 1116 | /// ::= StringConstant |
| 1117 | bool LLParser::ParseStringConstant(std::string &Result) { |
| 1118 | if (Lex.getKind() != lltok::StringConstant) |
| 1119 | return TokError("expected string constant"); |
| 1120 | Result = Lex.getStrVal(); |
| 1121 | Lex.Lex(); |
| 1122 | return false; |
| 1123 | } |
| 1124 | |
| 1125 | /// ParseUInt32 |
| 1126 | /// ::= uint32 |
| 1127 | bool LLParser::ParseUInt32(unsigned &Val) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1128 | if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned()) |
| 1129 | return TokError("expected integer"); |
| 1130 | uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1); |
| 1131 | if (Val64 != unsigned(Val64)) |
| 1132 | return TokError("expected 32-bit integer (too large)"); |
| 1133 | Val = Val64; |
| 1134 | Lex.Lex(); |
| 1135 | return false; |
| 1136 | } |
| 1137 | |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1138 | /// ParseUInt64 |
| 1139 | /// ::= uint64 |
| 1140 | bool LLParser::ParseUInt64(uint64_t &Val) { |
| 1141 | if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned()) |
| 1142 | return TokError("expected integer"); |
| 1143 | Val = Lex.getAPSIntVal().getLimitedValue(); |
| 1144 | Lex.Lex(); |
| 1145 | return false; |
| 1146 | } |
| 1147 | |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 1148 | /// ParseTLSModel |
| 1149 | /// := 'localdynamic' |
| 1150 | /// := 'initialexec' |
| 1151 | /// := 'localexec' |
| 1152 | bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) { |
| 1153 | switch (Lex.getKind()) { |
| 1154 | default: |
| 1155 | return TokError("expected localdynamic, initialexec or localexec"); |
| 1156 | case lltok::kw_localdynamic: |
| 1157 | TLM = GlobalVariable::LocalDynamicTLSModel; |
| 1158 | break; |
| 1159 | case lltok::kw_initialexec: |
| 1160 | TLM = GlobalVariable::InitialExecTLSModel; |
| 1161 | break; |
| 1162 | case lltok::kw_localexec: |
| 1163 | TLM = GlobalVariable::LocalExecTLSModel; |
| 1164 | break; |
| 1165 | } |
| 1166 | |
| 1167 | Lex.Lex(); |
| 1168 | return false; |
| 1169 | } |
| 1170 | |
| 1171 | /// ParseOptionalThreadLocal |
| 1172 | /// := /*empty*/ |
| 1173 | /// := 'thread_local' |
| 1174 | /// := 'thread_local' '(' tlsmodel ')' |
| 1175 | bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) { |
| 1176 | TLM = GlobalVariable::NotThreadLocal; |
| 1177 | if (!EatIfPresent(lltok::kw_thread_local)) |
| 1178 | return false; |
| 1179 | |
| 1180 | TLM = GlobalVariable::GeneralDynamicTLSModel; |
| 1181 | if (Lex.getKind() == lltok::lparen) { |
| 1182 | Lex.Lex(); |
| 1183 | return ParseTLSModel(TLM) || |
| 1184 | ParseToken(lltok::rparen, "expected ')' after thread local model"); |
| 1185 | } |
| 1186 | return false; |
| 1187 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1188 | |
| 1189 | /// ParseOptionalAddrSpace |
| 1190 | /// := /*empty*/ |
| 1191 | /// := 'addrspace' '(' uint32 ')' |
| 1192 | bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) { |
| 1193 | AddrSpace = 0; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1194 | if (!EatIfPresent(lltok::kw_addrspace)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1195 | return false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1196 | return ParseToken(lltok::lparen, "expected '(' in address space") || |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1197 | ParseUInt32(AddrSpace) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1198 | ParseToken(lltok::rparen, "expected ')' in address space"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1199 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1200 | |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 1201 | /// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes. |
| 1202 | bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) { |
| 1203 | bool HaveError = false; |
| 1204 | |
| 1205 | B.clear(); |
| 1206 | |
| 1207 | while (1) { |
| 1208 | lltok::Kind Token = Lex.getKind(); |
| 1209 | switch (Token) { |
| 1210 | default: // End of attributes. |
| 1211 | return HaveError; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1212 | case lltok::kw_align: { |
| 1213 | unsigned Alignment; |
| 1214 | if (ParseOptionalAlignment(Alignment)) |
| 1215 | return true; |
Bill Wendling | 68d2401 | 2012-10-08 22:20:14 +0000 | [diff] [blame] | 1216 | B.addAlignmentAttr(Alignment); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1217 | continue; |
| 1218 | } |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1219 | case lltok::kw_byval: B.addAttribute(Attribute::ByVal); break; |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1220 | case lltok::kw_dereferenceable: { |
| 1221 | uint64_t Bytes; |
| 1222 | if (ParseOptionalDereferenceableBytes(Bytes)) |
| 1223 | return true; |
| 1224 | B.addDereferenceableAttr(Bytes); |
| 1225 | continue; |
| 1226 | } |
Reid Kleckner | a534a38 | 2013-12-19 02:14:12 +0000 | [diff] [blame] | 1227 | case lltok::kw_inalloca: B.addAttribute(Attribute::InAlloca); break; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1228 | case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break; |
| 1229 | case lltok::kw_nest: B.addAttribute(Attribute::Nest); break; |
| 1230 | case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break; |
| 1231 | case lltok::kw_nocapture: B.addAttribute(Attribute::NoCapture); break; |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 1232 | case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break; |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1233 | case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break; |
| 1234 | case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break; |
Stephen Lin | b8bd232 | 2013-04-20 05:14:40 +0000 | [diff] [blame] | 1235 | case lltok::kw_returned: B.addAttribute(Attribute::Returned); break; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1236 | case lltok::kw_signext: B.addAttribute(Attribute::SExt); break; |
| 1237 | case lltok::kw_sret: B.addAttribute(Attribute::StructRet); break; |
| 1238 | case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break; |
Charles Davis | be5557e | 2010-02-12 00:31:15 +0000 | [diff] [blame] | 1239 | |
Stephen Lin | 7577ed5 | 2013-04-20 13:16:13 +0000 | [diff] [blame] | 1240 | case lltok::kw_alignstack: |
| 1241 | case lltok::kw_alwaysinline: |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 1242 | case lltok::kw_builtin: |
Stephen Lin | 7577ed5 | 2013-04-20 13:16:13 +0000 | [diff] [blame] | 1243 | case lltok::kw_inlinehint: |
Tom Roeder | 44cb65f | 2014-06-05 19:29:43 +0000 | [diff] [blame] | 1244 | case lltok::kw_jumptable: |
Stephen Lin | 7577ed5 | 2013-04-20 13:16:13 +0000 | [diff] [blame] | 1245 | case lltok::kw_minsize: |
| 1246 | case lltok::kw_naked: |
| 1247 | case lltok::kw_nobuiltin: |
| 1248 | case lltok::kw_noduplicate: |
| 1249 | case lltok::kw_noimplicitfloat: |
| 1250 | case lltok::kw_noinline: |
| 1251 | case lltok::kw_nonlazybind: |
| 1252 | case lltok::kw_noredzone: |
| 1253 | case lltok::kw_noreturn: |
| 1254 | case lltok::kw_nounwind: |
Andrea Di Biagio | 377496b | 2013-08-23 11:53:55 +0000 | [diff] [blame] | 1255 | case lltok::kw_optnone: |
Stephen Lin | 7577ed5 | 2013-04-20 13:16:13 +0000 | [diff] [blame] | 1256 | case lltok::kw_optsize: |
Stephen Lin | 7577ed5 | 2013-04-20 13:16:13 +0000 | [diff] [blame] | 1257 | case lltok::kw_returns_twice: |
| 1258 | case lltok::kw_sanitize_address: |
| 1259 | case lltok::kw_sanitize_memory: |
| 1260 | case lltok::kw_sanitize_thread: |
| 1261 | case lltok::kw_ssp: |
| 1262 | case lltok::kw_sspreq: |
| 1263 | case lltok::kw_sspstrong: |
| 1264 | case lltok::kw_uwtable: |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 1265 | HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute"); |
| 1266 | break; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1267 | } |
Bill Wendling | 0be9c40 | 2012-09-28 22:30:18 +0000 | [diff] [blame] | 1268 | |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 1269 | Lex.Lex(); |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | /// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes. |
| 1274 | bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) { |
| 1275 | bool HaveError = false; |
| 1276 | |
| 1277 | B.clear(); |
| 1278 | |
| 1279 | while (1) { |
| 1280 | lltok::Kind Token = Lex.getKind(); |
Bill Wendling | 0be9c40 | 2012-09-28 22:30:18 +0000 | [diff] [blame] | 1281 | switch (Token) { |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 1282 | default: // End of attributes. |
| 1283 | return HaveError; |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1284 | case lltok::kw_dereferenceable: { |
| 1285 | uint64_t Bytes; |
| 1286 | if (ParseOptionalDereferenceableBytes(Bytes)) |
| 1287 | return true; |
| 1288 | B.addDereferenceableAttr(Bytes); |
| 1289 | continue; |
| 1290 | } |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1291 | case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break; |
| 1292 | case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break; |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 1293 | case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1294 | case lltok::kw_signext: B.addAttribute(Attribute::SExt); break; |
| 1295 | case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break; |
Bill Wendling | 0be9c40 | 2012-09-28 22:30:18 +0000 | [diff] [blame] | 1296 | |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 1297 | // Error handling. |
Stephen Lin | 7577ed5 | 2013-04-20 13:16:13 +0000 | [diff] [blame] | 1298 | case lltok::kw_align: |
Chandler Carruth | 9f6b59a | 2013-04-09 19:46:46 +0000 | [diff] [blame] | 1299 | case lltok::kw_byval: |
Reid Kleckner | a534a38 | 2013-12-19 02:14:12 +0000 | [diff] [blame] | 1300 | case lltok::kw_inalloca: |
Chandler Carruth | 9f6b59a | 2013-04-09 19:46:46 +0000 | [diff] [blame] | 1301 | case lltok::kw_nest: |
| 1302 | case lltok::kw_nocapture: |
Stephen Lin | b8bd232 | 2013-04-20 05:14:40 +0000 | [diff] [blame] | 1303 | case lltok::kw_returned: |
Chandler Carruth | 9f6b59a | 2013-04-09 19:46:46 +0000 | [diff] [blame] | 1304 | case lltok::kw_sret: |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 1305 | HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute"); |
Bill Wendling | 0be9c40 | 2012-09-28 22:30:18 +0000 | [diff] [blame] | 1306 | break; |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 1307 | |
Chandler Carruth | 9f6b59a | 2013-04-09 19:46:46 +0000 | [diff] [blame] | 1308 | case lltok::kw_alignstack: |
| 1309 | case lltok::kw_alwaysinline: |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 1310 | case lltok::kw_builtin: |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 1311 | case lltok::kw_cold: |
Chandler Carruth | 9f6b59a | 2013-04-09 19:46:46 +0000 | [diff] [blame] | 1312 | case lltok::kw_inlinehint: |
Tom Roeder | 44cb65f | 2014-06-05 19:29:43 +0000 | [diff] [blame] | 1313 | case lltok::kw_jumptable: |
Chandler Carruth | 9f6b59a | 2013-04-09 19:46:46 +0000 | [diff] [blame] | 1314 | case lltok::kw_minsize: |
| 1315 | case lltok::kw_naked: |
| 1316 | case lltok::kw_nobuiltin: |
| 1317 | case lltok::kw_noduplicate: |
| 1318 | case lltok::kw_noimplicitfloat: |
| 1319 | case lltok::kw_noinline: |
| 1320 | case lltok::kw_nonlazybind: |
| 1321 | case lltok::kw_noredzone: |
| 1322 | case lltok::kw_noreturn: |
| 1323 | case lltok::kw_nounwind: |
Andrea Di Biagio | 377496b | 2013-08-23 11:53:55 +0000 | [diff] [blame] | 1324 | case lltok::kw_optnone: |
Chandler Carruth | 9f6b59a | 2013-04-09 19:46:46 +0000 | [diff] [blame] | 1325 | case lltok::kw_optsize: |
Chandler Carruth | 9f6b59a | 2013-04-09 19:46:46 +0000 | [diff] [blame] | 1326 | case lltok::kw_returns_twice: |
| 1327 | case lltok::kw_sanitize_address: |
| 1328 | case lltok::kw_sanitize_memory: |
| 1329 | case lltok::kw_sanitize_thread: |
| 1330 | case lltok::kw_ssp: |
| 1331 | case lltok::kw_sspreq: |
| 1332 | case lltok::kw_sspstrong: |
| 1333 | case lltok::kw_uwtable: |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 1334 | HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute"); |
Bill Wendling | 0be9c40 | 2012-09-28 22:30:18 +0000 | [diff] [blame] | 1335 | break; |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1336 | |
| 1337 | case lltok::kw_readnone: |
| 1338 | case lltok::kw_readonly: |
| 1339 | HaveError |= Error(Lex.getLoc(), "invalid use of attribute on return type"); |
Bill Wendling | 0be9c40 | 2012-09-28 22:30:18 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1342 | Lex.Lex(); |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | /// ParseOptionalLinkage |
| 1347 | /// ::= /*empty*/ |
Rafael Espindola | 6de96a1 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 1348 | /// ::= 'private' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1349 | /// ::= 'internal' |
| 1350 | /// ::= 'weak' |
Duncan Sands | 12da8ce | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 1351 | /// ::= 'weak_odr' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1352 | /// ::= 'linkonce' |
Duncan Sands | 12da8ce | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 1353 | /// ::= 'linkonce_odr' |
Bill Wendling | 03bcd6e | 2010-07-01 21:55:59 +0000 | [diff] [blame] | 1354 | /// ::= 'available_externally' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1355 | /// ::= 'appending' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1356 | /// ::= 'common' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1357 | /// ::= 'extern_weak' |
| 1358 | /// ::= 'external' |
| 1359 | bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) { |
| 1360 | HasLinkage = false; |
| 1361 | switch (Lex.getKind()) { |
Bill Wendling | a3c6f6b | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 1362 | default: Res=GlobalValue::ExternalLinkage; return false; |
| 1363 | case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break; |
Bill Wendling | a3c6f6b | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 1364 | case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break; |
| 1365 | case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break; |
| 1366 | case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break; |
| 1367 | case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break; |
| 1368 | case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break; |
Chris Lattner | 184f1be | 2009-04-13 05:44:34 +0000 | [diff] [blame] | 1369 | case lltok::kw_available_externally: |
| 1370 | Res = GlobalValue::AvailableExternallyLinkage; |
| 1371 | break; |
Bill Wendling | a3c6f6b | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 1372 | case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break; |
Bill Wendling | a3c6f6b | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 1373 | case lltok::kw_common: Res = GlobalValue::CommonLinkage; break; |
Bill Wendling | a3c6f6b | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 1374 | case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break; |
| 1375 | case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1376 | } |
| 1377 | Lex.Lex(); |
| 1378 | HasLinkage = true; |
| 1379 | return false; |
| 1380 | } |
| 1381 | |
| 1382 | /// ParseOptionalVisibility |
| 1383 | /// ::= /*empty*/ |
| 1384 | /// ::= 'default' |
| 1385 | /// ::= 'hidden' |
| 1386 | /// ::= 'protected' |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1387 | /// |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1388 | bool LLParser::ParseOptionalVisibility(unsigned &Res) { |
| 1389 | switch (Lex.getKind()) { |
| 1390 | default: Res = GlobalValue::DefaultVisibility; return false; |
| 1391 | case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break; |
| 1392 | case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break; |
| 1393 | case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break; |
| 1394 | } |
| 1395 | Lex.Lex(); |
| 1396 | return false; |
| 1397 | } |
| 1398 | |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 1399 | /// ParseOptionalDLLStorageClass |
| 1400 | /// ::= /*empty*/ |
| 1401 | /// ::= 'dllimport' |
| 1402 | /// ::= 'dllexport' |
| 1403 | /// |
| 1404 | bool LLParser::ParseOptionalDLLStorageClass(unsigned &Res) { |
| 1405 | switch (Lex.getKind()) { |
| 1406 | default: Res = GlobalValue::DefaultStorageClass; return false; |
| 1407 | case lltok::kw_dllimport: Res = GlobalValue::DLLImportStorageClass; break; |
| 1408 | case lltok::kw_dllexport: Res = GlobalValue::DLLExportStorageClass; break; |
| 1409 | } |
| 1410 | Lex.Lex(); |
| 1411 | return false; |
| 1412 | } |
| 1413 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1414 | /// ParseOptionalCallingConv |
| 1415 | /// ::= /*empty*/ |
| 1416 | /// ::= 'ccc' |
| 1417 | /// ::= 'fastcc' |
Reid Kleckner | 35fc363 | 2014-12-01 21:04:44 +0000 | [diff] [blame] | 1418 | /// ::= 'intel_ocl_bicc' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1419 | /// ::= 'coldcc' |
| 1420 | /// ::= 'x86_stdcallcc' |
| 1421 | /// ::= 'x86_fastcallcc' |
Anton Korobeynikov | 8f35fab | 2010-05-16 09:08:45 +0000 | [diff] [blame] | 1422 | /// ::= 'x86_thiscallcc' |
Reid Kleckner | 9ccce99 | 2014-10-28 01:29:26 +0000 | [diff] [blame] | 1423 | /// ::= 'x86_vectorcallcc' |
Anton Korobeynikov | a8fd40b | 2009-06-16 18:50:49 +0000 | [diff] [blame] | 1424 | /// ::= 'arm_apcscc' |
| 1425 | /// ::= 'arm_aapcscc' |
| 1426 | /// ::= 'arm_aapcs_vfpcc' |
Anton Korobeynikov | 27a0ecf | 2009-12-07 02:27:35 +0000 | [diff] [blame] | 1427 | /// ::= 'msp430_intrcc' |
Che-Liang Chiou | 2994790 | 2010-09-25 07:46:17 +0000 | [diff] [blame] | 1428 | /// ::= 'ptx_kernel' |
| 1429 | /// ::= 'ptx_device' |
Micah Villmow | 48c8ddc | 2012-10-01 17:01:31 +0000 | [diff] [blame] | 1430 | /// ::= 'spir_func' |
| 1431 | /// ::= 'spir_kernel' |
Charles Davis | e8f297c | 2013-07-12 06:02:35 +0000 | [diff] [blame] | 1432 | /// ::= 'x86_64_sysvcc' |
| 1433 | /// ::= 'x86_64_win64cc' |
Andrew Trick | a3a11de | 2013-10-31 22:12:01 +0000 | [diff] [blame] | 1434 | /// ::= 'webkit_jscc' |
Juergen Ributzka | 9969d3e | 2013-11-08 23:28:16 +0000 | [diff] [blame] | 1435 | /// ::= 'anyregcc' |
Juergen Ributzka | e625013 | 2014-01-17 19:47:03 +0000 | [diff] [blame] | 1436 | /// ::= 'preserve_mostcc' |
| 1437 | /// ::= 'preserve_allcc' |
Reid Kleckner | 35fc363 | 2014-12-01 21:04:44 +0000 | [diff] [blame] | 1438 | /// ::= 'ghccc' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1439 | /// ::= 'cc' UINT |
Anton Korobeynikov | a8fd40b | 2009-06-16 18:50:49 +0000 | [diff] [blame] | 1440 | /// |
Alexey Samsonov | 17a9cff | 2014-09-10 18:00:17 +0000 | [diff] [blame] | 1441 | bool LLParser::ParseOptionalCallingConv(unsigned &CC) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1442 | switch (Lex.getKind()) { |
| 1443 | default: CC = CallingConv::C; return false; |
| 1444 | case lltok::kw_ccc: CC = CallingConv::C; break; |
| 1445 | case lltok::kw_fastcc: CC = CallingConv::Fast; break; |
| 1446 | case lltok::kw_coldcc: CC = CallingConv::Cold; break; |
| 1447 | case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break; |
| 1448 | case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break; |
Anton Korobeynikov | 8f35fab | 2010-05-16 09:08:45 +0000 | [diff] [blame] | 1449 | case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break; |
Reid Kleckner | 9ccce99 | 2014-10-28 01:29:26 +0000 | [diff] [blame] | 1450 | case lltok::kw_x86_vectorcallcc:CC = CallingConv::X86_VectorCall; break; |
Anton Korobeynikov | a8fd40b | 2009-06-16 18:50:49 +0000 | [diff] [blame] | 1451 | case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break; |
| 1452 | case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break; |
| 1453 | case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break; |
Anton Korobeynikov | 27a0ecf | 2009-12-07 02:27:35 +0000 | [diff] [blame] | 1454 | case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break; |
Che-Liang Chiou | 2994790 | 2010-09-25 07:46:17 +0000 | [diff] [blame] | 1455 | case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break; |
| 1456 | case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break; |
Micah Villmow | 48c8ddc | 2012-10-01 17:01:31 +0000 | [diff] [blame] | 1457 | case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break; |
| 1458 | case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break; |
Elena Demikhovsky | d6afb03 | 2012-10-24 14:46:16 +0000 | [diff] [blame] | 1459 | case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break; |
Charles Davis | e8f297c | 2013-07-12 06:02:35 +0000 | [diff] [blame] | 1460 | case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break; |
| 1461 | case lltok::kw_x86_64_win64cc: CC = CallingConv::X86_64_Win64; break; |
Andrew Trick | a3a11de | 2013-10-31 22:12:01 +0000 | [diff] [blame] | 1462 | case lltok::kw_webkit_jscc: CC = CallingConv::WebKit_JS; break; |
Juergen Ributzka | 9969d3e | 2013-11-08 23:28:16 +0000 | [diff] [blame] | 1463 | case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break; |
Juergen Ributzka | e625013 | 2014-01-17 19:47:03 +0000 | [diff] [blame] | 1464 | case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break; |
| 1465 | case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break; |
Reid Kleckner | 35fc363 | 2014-12-01 21:04:44 +0000 | [diff] [blame] | 1466 | case lltok::kw_ghccc: CC = CallingConv::GHC; break; |
Sandeep Patel | 68c5f47 | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 1467 | case lltok::kw_cc: { |
Sandeep Patel | 68c5f47 | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 1468 | Lex.Lex(); |
Alexey Samsonov | 17a9cff | 2014-09-10 18:00:17 +0000 | [diff] [blame] | 1469 | return ParseUInt32(CC); |
Sandeep Patel | 68c5f47 | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 1470 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1471 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1472 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1473 | Lex.Lex(); |
| 1474 | return false; |
| 1475 | } |
| 1476 | |
Chris Lattner | 5c42763 | 2009-12-30 05:31:19 +0000 | [diff] [blame] | 1477 | /// ParseInstructionMetadata |
Chris Lattner | 596760d | 2009-12-29 21:25:40 +0000 | [diff] [blame] | 1478 | /// ::= !dbg !42 (',' !dbg !57)* |
Dan Gohman | 338d9a4 | 2010-08-24 02:05:17 +0000 | [diff] [blame] | 1479 | bool LLParser::ParseInstructionMetadata(Instruction *Inst, |
| 1480 | PerFunctionState *PFS) { |
Chris Lattner | 5c42763 | 2009-12-30 05:31:19 +0000 | [diff] [blame] | 1481 | do { |
| 1482 | if (Lex.getKind() != lltok::MetadataVar) |
| 1483 | return TokError("expected metadata after comma"); |
Devang Patel | ba4a6fd | 2009-09-29 00:01:14 +0000 | [diff] [blame] | 1484 | |
Chris Lattner | 596760d | 2009-12-29 21:25:40 +0000 | [diff] [blame] | 1485 | std::string Name = Lex.getStrVal(); |
Benjamin Kramer | b3bd019 | 2011-12-06 11:50:26 +0000 | [diff] [blame] | 1486 | unsigned MDK = M->getMDKindID(Name); |
Chris Lattner | 596760d | 2009-12-29 21:25:40 +0000 | [diff] [blame] | 1487 | Lex.Lex(); |
Chris Lattner | 8d58f2f | 2009-10-19 05:31:10 +0000 | [diff] [blame] | 1488 | |
Duncan P. N. Exon Smith | f825dae | 2015-01-12 22:26:48 +0000 | [diff] [blame] | 1489 | MDNode *N; |
| 1490 | if (ParseMDNode(N)) |
Chris Lattner | 1eed2d6 | 2009-12-30 04:56:59 +0000 | [diff] [blame] | 1491 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1492 | |
Duncan P. N. Exon Smith | f825dae | 2015-01-12 22:26:48 +0000 | [diff] [blame] | 1493 | Inst->setMetadata(MDK, N); |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 1494 | if (MDK == LLVMContext::MD_tbaa) |
| 1495 | InstsWithTBAATag.push_back(Inst); |
| 1496 | |
Chris Lattner | 596760d | 2009-12-29 21:25:40 +0000 | [diff] [blame] | 1497 | // If this is the end of the list, we're done. |
Chris Lattner | 5c42763 | 2009-12-30 05:31:19 +0000 | [diff] [blame] | 1498 | } while (EatIfPresent(lltok::comma)); |
| 1499 | return false; |
Devang Patel | ea8a4b9 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1502 | /// ParseOptionalAlignment |
| 1503 | /// ::= /* empty */ |
| 1504 | /// ::= 'align' 4 |
| 1505 | bool LLParser::ParseOptionalAlignment(unsigned &Alignment) { |
| 1506 | Alignment = 0; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1507 | if (!EatIfPresent(lltok::kw_align)) |
| 1508 | return false; |
Chris Lattner | d11f514 | 2009-01-05 07:46:05 +0000 | [diff] [blame] | 1509 | LocTy AlignLoc = Lex.getLoc(); |
| 1510 | if (ParseUInt32(Alignment)) return true; |
| 1511 | if (!isPowerOf2_32(Alignment)) |
| 1512 | return Error(AlignLoc, "alignment is not a power of two"); |
Dan Gohman | d566d2c | 2010-07-30 21:07:05 +0000 | [diff] [blame] | 1513 | if (Alignment > Value::MaximumAlignment) |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1514 | return Error(AlignLoc, "huge alignments are not supported yet"); |
Chris Lattner | d11f514 | 2009-01-05 07:46:05 +0000 | [diff] [blame] | 1515 | return false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1518 | /// ParseOptionalDereferenceableBytes |
| 1519 | /// ::= /* empty */ |
| 1520 | /// ::= 'dereferenceable' '(' 4 ')' |
| 1521 | bool LLParser::ParseOptionalDereferenceableBytes(uint64_t &Bytes) { |
| 1522 | Bytes = 0; |
| 1523 | if (!EatIfPresent(lltok::kw_dereferenceable)) |
| 1524 | return false; |
| 1525 | LocTy ParenLoc = Lex.getLoc(); |
| 1526 | if (!EatIfPresent(lltok::lparen)) |
| 1527 | return Error(ParenLoc, "expected '('"); |
| 1528 | LocTy DerefLoc = Lex.getLoc(); |
| 1529 | if (ParseUInt64(Bytes)) return true; |
| 1530 | ParenLoc = Lex.getLoc(); |
| 1531 | if (!EatIfPresent(lltok::rparen)) |
| 1532 | return Error(ParenLoc, "expected ')'"); |
| 1533 | if (!Bytes) |
| 1534 | return Error(DerefLoc, "dereferenceable bytes must be non-zero"); |
| 1535 | return false; |
| 1536 | } |
| 1537 | |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 1538 | /// ParseOptionalCommaAlign |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1539 | /// ::= |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 1540 | /// ::= ',' align 4 |
| 1541 | /// |
| 1542 | /// This returns with AteExtraComma set to true if it ate an excess comma at the |
| 1543 | /// end. |
| 1544 | bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment, |
| 1545 | bool &AteExtraComma) { |
| 1546 | AteExtraComma = false; |
| 1547 | while (EatIfPresent(lltok::comma)) { |
| 1548 | // Metadata at the end is an early exit. |
Chris Lattner | eafe4de | 2009-12-30 05:02:06 +0000 | [diff] [blame] | 1549 | if (Lex.getKind() == lltok::MetadataVar) { |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 1550 | AteExtraComma = true; |
| 1551 | return false; |
| 1552 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1553 | |
Chris Lattner | 95b0ff4 | 2010-04-23 00:50:50 +0000 | [diff] [blame] | 1554 | if (Lex.getKind() != lltok::kw_align) |
| 1555 | return Error(Lex.getLoc(), "expected metadata or 'align'"); |
Duncan Sands | 4c5c858 | 2010-10-21 16:07:10 +0000 | [diff] [blame] | 1556 | |
Chris Lattner | 95b0ff4 | 2010-04-23 00:50:50 +0000 | [diff] [blame] | 1557 | if (ParseOptionalAlignment(Alignment)) return true; |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 1558 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1559 | |
Devang Patel | ea8a4b9 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 1560 | return false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1563 | /// ParseScopeAndOrdering |
| 1564 | /// if isAtomic: ::= 'singlethread'? AtomicOrdering |
| 1565 | /// else: ::= |
| 1566 | /// |
| 1567 | /// This sets Scope and Ordering to the parsed values. |
| 1568 | bool LLParser::ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope, |
| 1569 | AtomicOrdering &Ordering) { |
| 1570 | if (!isAtomic) |
| 1571 | return false; |
| 1572 | |
| 1573 | Scope = CrossThread; |
| 1574 | if (EatIfPresent(lltok::kw_singlethread)) |
| 1575 | Scope = SingleThread; |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1576 | |
| 1577 | return ParseOrdering(Ordering); |
| 1578 | } |
| 1579 | |
| 1580 | /// ParseOrdering |
| 1581 | /// ::= AtomicOrdering |
| 1582 | /// |
| 1583 | /// This sets Ordering to the parsed value. |
| 1584 | bool LLParser::ParseOrdering(AtomicOrdering &Ordering) { |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1585 | switch (Lex.getKind()) { |
| 1586 | default: return TokError("Expected ordering on atomic instruction"); |
| 1587 | case lltok::kw_unordered: Ordering = Unordered; break; |
| 1588 | case lltok::kw_monotonic: Ordering = Monotonic; break; |
| 1589 | case lltok::kw_acquire: Ordering = Acquire; break; |
| 1590 | case lltok::kw_release: Ordering = Release; break; |
| 1591 | case lltok::kw_acq_rel: Ordering = AcquireRelease; break; |
| 1592 | case lltok::kw_seq_cst: Ordering = SequentiallyConsistent; break; |
| 1593 | } |
| 1594 | Lex.Lex(); |
| 1595 | return false; |
| 1596 | } |
| 1597 | |
Charles Davis | be5557e | 2010-02-12 00:31:15 +0000 | [diff] [blame] | 1598 | /// ParseOptionalStackAlignment |
| 1599 | /// ::= /* empty */ |
| 1600 | /// ::= 'alignstack' '(' 4 ')' |
| 1601 | bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) { |
| 1602 | Alignment = 0; |
| 1603 | if (!EatIfPresent(lltok::kw_alignstack)) |
| 1604 | return false; |
| 1605 | LocTy ParenLoc = Lex.getLoc(); |
| 1606 | if (!EatIfPresent(lltok::lparen)) |
| 1607 | return Error(ParenLoc, "expected '('"); |
| 1608 | LocTy AlignLoc = Lex.getLoc(); |
| 1609 | if (ParseUInt32(Alignment)) return true; |
| 1610 | ParenLoc = Lex.getLoc(); |
| 1611 | if (!EatIfPresent(lltok::rparen)) |
| 1612 | return Error(ParenLoc, "expected ')'"); |
| 1613 | if (!isPowerOf2_32(Alignment)) |
| 1614 | return Error(AlignLoc, "stack alignment is not a power of two"); |
| 1615 | return false; |
| 1616 | } |
Devang Patel | ea8a4b9 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 1617 | |
Chris Lattner | 28f1eeb | 2009-12-30 05:14:00 +0000 | [diff] [blame] | 1618 | /// ParseIndexList - This parses the index list for an insert/extractvalue |
| 1619 | /// instruction. This sets AteExtraComma in the case where we eat an extra |
| 1620 | /// comma at the end of the line and find that it is followed by metadata. |
| 1621 | /// Clients that don't allow metadata can call the version of this function that |
| 1622 | /// only takes one argument. |
| 1623 | /// |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1624 | /// ParseIndexList |
| 1625 | /// ::= (',' uint32)+ |
Chris Lattner | 28f1eeb | 2009-12-30 05:14:00 +0000 | [diff] [blame] | 1626 | /// |
| 1627 | bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices, |
| 1628 | bool &AteExtraComma) { |
| 1629 | AteExtraComma = false; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1630 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1631 | if (Lex.getKind() != lltok::comma) |
| 1632 | return TokError("expected ',' as start of index list"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1633 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1634 | while (EatIfPresent(lltok::comma)) { |
Chris Lattner | 28f1eeb | 2009-12-30 05:14:00 +0000 | [diff] [blame] | 1635 | if (Lex.getKind() == lltok::MetadataVar) { |
| 1636 | AteExtraComma = true; |
| 1637 | return false; |
| 1638 | } |
Nick Lewycky | 83e4711 | 2010-09-29 23:32:20 +0000 | [diff] [blame] | 1639 | unsigned Idx = 0; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1640 | if (ParseUInt32(Idx)) return true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1641 | Indices.push_back(Idx); |
| 1642 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1643 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1644 | return false; |
| 1645 | } |
| 1646 | |
| 1647 | //===----------------------------------------------------------------------===// |
| 1648 | // Type Parsing. |
| 1649 | //===----------------------------------------------------------------------===// |
| 1650 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1651 | /// ParseType - Parse a type. |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 1652 | bool LLParser::ParseType(Type *&Result, const Twine &Msg, bool AllowVoid) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1653 | SMLoc TypeLoc = Lex.getLoc(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1654 | switch (Lex.getKind()) { |
| 1655 | default: |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 1656 | return TokError(Msg); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1657 | case lltok::Type: |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1658 | // Type ::= 'float' | 'void' (etc) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1659 | Result = Lex.getTyVal(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1660 | Lex.Lex(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1661 | break; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1662 | case lltok::lbrace: |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1663 | // Type ::= StructType |
| 1664 | if (ParseAnonStructType(Result, false)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1665 | return true; |
| 1666 | break; |
| 1667 | case lltok::lsquare: |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1668 | // Type ::= '[' ... ']' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1669 | Lex.Lex(); // eat the lsquare. |
| 1670 | if (ParseArrayVectorType(Result, false)) |
| 1671 | return true; |
| 1672 | break; |
| 1673 | case lltok::less: // Either vector or packed struct. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1674 | // Type ::= '<' ... '>' |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1675 | Lex.Lex(); |
| 1676 | if (Lex.getKind() == lltok::lbrace) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1677 | if (ParseAnonStructType(Result, true) || |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1678 | ParseToken(lltok::greater, "expected '>' at end of packed struct")) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1679 | return true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1680 | } else if (ParseArrayVectorType(Result, true)) |
| 1681 | return true; |
| 1682 | break; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1683 | case lltok::LocalVar: { |
| 1684 | // Type ::= %foo |
| 1685 | std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()]; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1686 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1687 | // If the type hasn't been defined yet, create a forward definition and |
| 1688 | // remember where that forward def'n was seen (in case it never is defined). |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1689 | if (!Entry.first) { |
Chris Lattner | 335d399 | 2011-08-12 18:06:37 +0000 | [diff] [blame] | 1690 | Entry.first = StructType::create(Context, Lex.getStrVal()); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1691 | Entry.second = Lex.getLoc(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1692 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1693 | Result = Entry.first; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1694 | Lex.Lex(); |
| 1695 | break; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1696 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1697 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1698 | case lltok::LocalVarID: { |
| 1699 | // Type ::= %4 |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1700 | std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()]; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1701 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1702 | // If the type hasn't been defined yet, create a forward definition and |
| 1703 | // remember where that forward def'n was seen (in case it never is defined). |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1704 | if (!Entry.first) { |
Chris Lattner | 335d399 | 2011-08-12 18:06:37 +0000 | [diff] [blame] | 1705 | Entry.first = StructType::create(Context); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1706 | Entry.second = Lex.getLoc(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1707 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1708 | Result = Entry.first; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1709 | Lex.Lex(); |
| 1710 | break; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1711 | } |
| 1712 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1713 | |
| 1714 | // Parse the type suffixes. |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1715 | while (1) { |
| 1716 | switch (Lex.getKind()) { |
| 1717 | // End of type. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1718 | default: |
| 1719 | if (!AllowVoid && Result->isVoidTy()) |
| 1720 | return Error(TypeLoc, "void type only allowed for function results"); |
| 1721 | return false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1722 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1723 | // Type ::= Type '*' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1724 | case lltok::star: |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1725 | if (Result->isLabelTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1726 | return TokError("basic block pointers are invalid"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1727 | if (Result->isVoidTy()) |
| 1728 | return TokError("pointers to void are invalid - use i8* instead"); |
| 1729 | if (!PointerType::isValidElementType(Result)) |
Nick Lewycky | 0aa6a74 | 2009-06-07 07:26:46 +0000 | [diff] [blame] | 1730 | return TokError("pointer to this type is invalid"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1731 | Result = PointerType::getUnqual(Result); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1732 | Lex.Lex(); |
| 1733 | break; |
| 1734 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1735 | // Type ::= Type 'addrspace' '(' uint32 ')' '*' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1736 | case lltok::kw_addrspace: { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1737 | if (Result->isLabelTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1738 | return TokError("basic block pointers are invalid"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1739 | if (Result->isVoidTy()) |
Dan Gohman | 9280a68 | 2009-02-09 17:41:21 +0000 | [diff] [blame] | 1740 | return TokError("pointers to void are invalid; use i8* instead"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1741 | if (!PointerType::isValidElementType(Result)) |
Nick Lewycky | 0aa6a74 | 2009-06-07 07:26:46 +0000 | [diff] [blame] | 1742 | return TokError("pointer to this type is invalid"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1743 | unsigned AddrSpace; |
| 1744 | if (ParseOptionalAddrSpace(AddrSpace) || |
| 1745 | ParseToken(lltok::star, "expected '*' in address space")) |
| 1746 | return true; |
| 1747 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1748 | Result = PointerType::get(Result, AddrSpace); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1749 | break; |
| 1750 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1751 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1752 | /// Types '(' ArgTypeListI ')' OptFuncAttrs |
| 1753 | case lltok::lparen: |
| 1754 | if (ParseFunctionType(Result)) |
| 1755 | return true; |
| 1756 | break; |
| 1757 | } |
| 1758 | } |
| 1759 | } |
| 1760 | |
| 1761 | /// ParseParameterList |
| 1762 | /// ::= '(' ')' |
| 1763 | /// ::= '(' Arg (',' Arg)* ')' |
| 1764 | /// Arg |
| 1765 | /// ::= Type OptionalAttributes Value OptionalAttributes |
| 1766 | bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList, |
Reid Kleckner | 8349864 | 2014-08-26 00:33:28 +0000 | [diff] [blame] | 1767 | PerFunctionState &PFS, bool IsMustTailCall, |
| 1768 | bool InVarArgsFunc) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1769 | if (ParseToken(lltok::lparen, "expected '(' in call")) |
| 1770 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1771 | |
Bill Wendling | fe0021a | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 1772 | unsigned AttrIndex = 1; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1773 | while (Lex.getKind() != lltok::rparen) { |
| 1774 | // If this isn't the first argument, we need a comma. |
| 1775 | if (!ArgList.empty() && |
| 1776 | ParseToken(lltok::comma, "expected ',' in argument list")) |
| 1777 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1778 | |
Reid Kleckner | 8349864 | 2014-08-26 00:33:28 +0000 | [diff] [blame] | 1779 | // Parse an ellipsis if this is a musttail call in a variadic function. |
| 1780 | if (Lex.getKind() == lltok::dotdotdot) { |
| 1781 | const char *Msg = "unexpected ellipsis in argument list for "; |
| 1782 | if (!IsMustTailCall) |
| 1783 | return TokError(Twine(Msg) + "non-musttail call"); |
| 1784 | if (!InVarArgsFunc) |
| 1785 | return TokError(Twine(Msg) + "musttail call in non-varargs function"); |
| 1786 | Lex.Lex(); // Lex the '...', it is purely for readability. |
| 1787 | return ParseToken(lltok::rparen, "expected ')' at end of argument list"); |
| 1788 | } |
| 1789 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1790 | // Parse the argument. |
| 1791 | LocTy ArgLoc; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1792 | Type *ArgTy = nullptr; |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 1793 | AttrBuilder ArgAttrs; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1794 | Value *V; |
Victor Hernandez | fa23223 | 2009-12-03 23:40:58 +0000 | [diff] [blame] | 1795 | if (ParseType(ArgTy, ArgLoc)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1796 | return true; |
Victor Hernandez | fa23223 | 2009-12-03 23:40:58 +0000 | [diff] [blame] | 1797 | |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 1798 | if (ArgTy->isMetadataTy()) { |
| 1799 | if (ParseMetadataAsValue(V, PFS)) |
| 1800 | return true; |
| 1801 | } else { |
| 1802 | // Otherwise, handle normal operands. |
| 1803 | if (ParseOptionalParamAttrs(ArgAttrs) || ParseValue(ArgTy, V, PFS)) |
| 1804 | return true; |
| 1805 | } |
Bill Wendling | fe0021a | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 1806 | ArgList.push_back(ParamInfo(ArgLoc, V, AttributeSet::get(V->getContext(), |
| 1807 | AttrIndex++, |
| 1808 | ArgAttrs))); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
Reid Kleckner | 8349864 | 2014-08-26 00:33:28 +0000 | [diff] [blame] | 1811 | if (IsMustTailCall && InVarArgsFunc) |
| 1812 | return TokError("expected '...' at end of argument list for musttail call " |
| 1813 | "in varargs function"); |
| 1814 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1815 | Lex.Lex(); // Lex the ')'. |
| 1816 | return false; |
| 1817 | } |
| 1818 | |
| 1819 | |
| 1820 | |
Chris Lattner | 2ed06b4 | 2009-01-05 18:34:07 +0000 | [diff] [blame] | 1821 | /// ParseArgumentList - Parse the argument list for a function type or function |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1822 | /// prototype. |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1823 | /// ::= '(' ArgTypeListI ')' |
| 1824 | /// ArgTypeListI |
| 1825 | /// ::= /*empty*/ |
| 1826 | /// ::= '...' |
| 1827 | /// ::= ArgTypeList ',' '...' |
| 1828 | /// ::= ArgType (',' ArgType)* |
Chris Lattner | 2ed06b4 | 2009-01-05 18:34:07 +0000 | [diff] [blame] | 1829 | /// |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1830 | bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, |
| 1831 | bool &isVarArg){ |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1832 | isVarArg = false; |
| 1833 | assert(Lex.getKind() == lltok::lparen); |
| 1834 | Lex.Lex(); // eat the (. |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1835 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1836 | if (Lex.getKind() == lltok::rparen) { |
| 1837 | // empty |
| 1838 | } else if (Lex.getKind() == lltok::dotdotdot) { |
| 1839 | isVarArg = true; |
| 1840 | Lex.Lex(); |
| 1841 | } else { |
| 1842 | LocTy TypeLoc = Lex.getLoc(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1843 | Type *ArgTy = nullptr; |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 1844 | AttrBuilder Attrs; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1845 | std::string Name; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1846 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1847 | if (ParseType(ArgTy) || |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 1848 | ParseOptionalParamAttrs(Attrs)) return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1849 | |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1850 | if (ArgTy->isVoidTy()) |
Chris Lattner | f880ca2 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 1851 | return Error(TypeLoc, "argument can not have void type"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1852 | |
Chris Lattner | def1949 | 2011-06-17 06:36:20 +0000 | [diff] [blame] | 1853 | if (Lex.getKind() == lltok::LocalVar) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1854 | Name = Lex.getStrVal(); |
| 1855 | Lex.Lex(); |
| 1856 | } |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1857 | |
Nick Lewycky | 0aa6a74 | 2009-06-07 07:26:46 +0000 | [diff] [blame] | 1858 | if (!FunctionType::isValidArgumentType(ArgTy)) |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1859 | return Error(TypeLoc, "invalid type for function argument"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1860 | |
Bill Wendling | fe0021a | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 1861 | unsigned AttrIndex = 1; |
Bill Wendling | d079a44 | 2012-10-15 04:46:55 +0000 | [diff] [blame] | 1862 | ArgList.push_back(ArgInfo(TypeLoc, ArgTy, |
Bill Wendling | fe0021a | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 1863 | AttributeSet::get(ArgTy->getContext(), |
| 1864 | AttrIndex++, Attrs), Name)); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1865 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1866 | while (EatIfPresent(lltok::comma)) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1867 | // Handle ... at end of arg list. |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1868 | if (EatIfPresent(lltok::dotdotdot)) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1869 | isVarArg = true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1870 | break; |
| 1871 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1872 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1873 | // Otherwise must be an argument type. |
| 1874 | TypeLoc = Lex.getLoc(); |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 1875 | if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) return true; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1876 | |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1877 | if (ArgTy->isVoidTy()) |
Chris Lattner | f880ca2 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 1878 | return Error(TypeLoc, "argument can not have void type"); |
| 1879 | |
Chris Lattner | def1949 | 2011-06-17 06:36:20 +0000 | [diff] [blame] | 1880 | if (Lex.getKind() == lltok::LocalVar) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1881 | Name = Lex.getStrVal(); |
| 1882 | Lex.Lex(); |
| 1883 | } else { |
| 1884 | Name = ""; |
| 1885 | } |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1886 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1887 | if (!ArgTy->isFirstClassType()) |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1888 | return Error(TypeLoc, "invalid type for function argument"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1889 | |
Bill Wendling | d079a44 | 2012-10-15 04:46:55 +0000 | [diff] [blame] | 1890 | ArgList.push_back(ArgInfo(TypeLoc, ArgTy, |
Bill Wendling | fe0021a | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 1891 | AttributeSet::get(ArgTy->getContext(), |
| 1892 | AttrIndex++, Attrs), |
Bill Wendling | d079a44 | 2012-10-15 04:46:55 +0000 | [diff] [blame] | 1893 | Name)); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1894 | } |
| 1895 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1896 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1897 | return ParseToken(lltok::rparen, "expected ')' at end of argument list"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1898 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1899 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1900 | /// ParseFunctionType |
| 1901 | /// ::= Type ArgumentList OptionalAttrs |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1902 | bool LLParser::ParseFunctionType(Type *&Result) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1903 | assert(Lex.getKind() == lltok::lparen); |
| 1904 | |
Chris Lattner | ce473c7 | 2009-01-05 08:04:33 +0000 | [diff] [blame] | 1905 | if (!FunctionType::isValidReturnType(Result)) |
| 1906 | return TokError("invalid function return type"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1907 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1908 | SmallVector<ArgInfo, 8> ArgList; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1909 | bool isVarArg; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1910 | if (ParseArgumentList(ArgList, isVarArg)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1911 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1912 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1913 | // Reject names on the arguments lists. |
| 1914 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { |
| 1915 | if (!ArgList[i].Name.empty()) |
| 1916 | return Error(ArgList[i].Loc, "argument name invalid in function type"); |
Bill Wendling | fe0021a | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 1917 | if (ArgList[i].Attrs.hasAttributes(i + 1)) |
Chris Lattner | 6bc5c89 | 2011-06-17 17:37:13 +0000 | [diff] [blame] | 1918 | return Error(ArgList[i].Loc, |
| 1919 | "argument attributes invalid in function type"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1920 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1921 | |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 1922 | SmallVector<Type*, 16> ArgListTy; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1923 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1924 | ArgListTy.push_back(ArgList[i].Ty); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1925 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1926 | Result = FunctionType::get(Result, ArgListTy, isVarArg); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1927 | return false; |
| 1928 | } |
| 1929 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1930 | /// ParseAnonStructType - Parse an anonymous struct type, which is inlined into |
| 1931 | /// other structs. |
| 1932 | bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) { |
| 1933 | SmallVector<Type*, 8> Elts; |
| 1934 | if (ParseStructBody(Elts)) return true; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1935 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1936 | Result = StructType::get(Context, Elts, Packed); |
| 1937 | return false; |
| 1938 | } |
| 1939 | |
| 1940 | /// ParseStructDefinition - Parse a struct in a 'type' definition. |
| 1941 | bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name, |
| 1942 | std::pair<Type*, LocTy> &Entry, |
| 1943 | Type *&ResultTy) { |
| 1944 | // If the type was already defined, diagnose the redefinition. |
| 1945 | if (Entry.first && !Entry.second.isValid()) |
| 1946 | return Error(TypeLoc, "redefinition of type"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1947 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1948 | // If we have opaque, just return without filling in the definition for the |
| 1949 | // struct. This counts as a definition as far as the .ll file goes. |
| 1950 | if (EatIfPresent(lltok::kw_opaque)) { |
| 1951 | // This type is being defined, so clear the location to indicate this. |
| 1952 | Entry.second = SMLoc(); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1953 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1954 | // If this type number has never been uttered, create it. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1955 | if (!Entry.first) |
Chris Lattner | 335d399 | 2011-08-12 18:06:37 +0000 | [diff] [blame] | 1956 | Entry.first = StructType::create(Context, Name); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1957 | ResultTy = Entry.first; |
| 1958 | return false; |
| 1959 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1960 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1961 | // If the type starts with '<', then it is either a packed struct or a vector. |
| 1962 | bool isPacked = EatIfPresent(lltok::less); |
| 1963 | |
| 1964 | // If we don't have a struct, then we have a random type alias, which we |
| 1965 | // accept for compatibility with old files. These types are not allowed to be |
| 1966 | // forward referenced and not allowed to be recursive. |
| 1967 | if (Lex.getKind() != lltok::lbrace) { |
| 1968 | if (Entry.first) |
| 1969 | return Error(TypeLoc, "forward references to non-struct type"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1970 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1971 | ResultTy = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1972 | if (isPacked) |
| 1973 | return ParseArrayVectorType(ResultTy, true); |
| 1974 | return ParseType(ResultTy); |
| 1975 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1976 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1977 | // This type is being defined, so clear the location to indicate this. |
| 1978 | Entry.second = SMLoc(); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1979 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1980 | // If this type number has never been uttered, create it. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1981 | if (!Entry.first) |
Chris Lattner | 335d399 | 2011-08-12 18:06:37 +0000 | [diff] [blame] | 1982 | Entry.first = StructType::create(Context, Name); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1983 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1984 | StructType *STy = cast<StructType>(Entry.first); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1985 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1986 | SmallVector<Type*, 8> Body; |
| 1987 | if (ParseStructBody(Body) || |
| 1988 | (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct"))) |
| 1989 | return true; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1990 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1991 | STy->setBody(Body, isPacked); |
| 1992 | ResultTy = STy; |
| 1993 | return false; |
| 1994 | } |
| 1995 | |
| 1996 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1997 | /// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1998 | /// StructType |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1999 | /// ::= '{' '}' |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2000 | /// ::= '{' Type (',' Type)* '}' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2001 | /// ::= '<' '{' '}' '>' |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2002 | /// ::= '<' '{' Type (',' Type)* '}' '>' |
| 2003 | bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2004 | assert(Lex.getKind() == lltok::lbrace); |
| 2005 | Lex.Lex(); // Consume the '{' |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2006 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2007 | // Handle the empty struct. |
| 2008 | if (EatIfPresent(lltok::rbrace)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2009 | return false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2010 | |
Chris Lattner | f880ca2 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 2011 | LocTy EltTyLoc = Lex.getLoc(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2012 | Type *Ty = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2013 | if (ParseType(Ty)) return true; |
| 2014 | Body.push_back(Ty); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2015 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2016 | if (!StructType::isValidElementType(Ty)) |
Nick Lewycky | 0aa6a74 | 2009-06-07 07:26:46 +0000 | [diff] [blame] | 2017 | return Error(EltTyLoc, "invalid element type for struct"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2018 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2019 | while (EatIfPresent(lltok::comma)) { |
Chris Lattner | f880ca2 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 2020 | EltTyLoc = Lex.getLoc(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2021 | if (ParseType(Ty)) return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2022 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2023 | if (!StructType::isValidElementType(Ty)) |
Nick Lewycky | 0aa6a74 | 2009-06-07 07:26:46 +0000 | [diff] [blame] | 2024 | return Error(EltTyLoc, "invalid element type for struct"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2025 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2026 | Body.push_back(Ty); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2027 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2028 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2029 | return ParseToken(lltok::rbrace, "expected '}' at end of struct"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | /// ParseArrayVectorType - Parse an array or vector type, assuming the first |
| 2033 | /// token has already been consumed. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2034 | /// Type |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2035 | /// ::= '[' APSINTVAL 'x' Types ']' |
| 2036 | /// ::= '<' APSINTVAL 'x' Types '>' |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2037 | bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2038 | if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() || |
| 2039 | Lex.getAPSIntVal().getBitWidth() > 64) |
| 2040 | return TokError("expected number in address space"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2041 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2042 | LocTy SizeLoc = Lex.getLoc(); |
| 2043 | uint64_t Size = Lex.getAPSIntVal().getZExtValue(); |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2044 | Lex.Lex(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2045 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2046 | if (ParseToken(lltok::kw_x, "expected 'x' after element count")) |
| 2047 | return true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2048 | |
| 2049 | LocTy TypeLoc = Lex.getLoc(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2050 | Type *EltTy = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2051 | if (ParseType(EltTy)) return true; |
Chris Lattner | f880ca2 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 2052 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2053 | if (ParseToken(isVector ? lltok::greater : lltok::rsquare, |
| 2054 | "expected end of sequential type")) |
| 2055 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2056 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2057 | if (isVector) { |
Chris Lattner | bb1fe8a | 2009-02-28 18:12:41 +0000 | [diff] [blame] | 2058 | if (Size == 0) |
| 2059 | return Error(SizeLoc, "zero element vector is illegal"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2060 | if ((unsigned)Size != Size) |
| 2061 | return Error(SizeLoc, "size too large for vector"); |
Nick Lewycky | 0aa6a74 | 2009-06-07 07:26:46 +0000 | [diff] [blame] | 2062 | if (!VectorType::isValidElementType(EltTy)) |
Duncan Sands | e6beec6 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 2063 | return Error(TypeLoc, "invalid vector element type"); |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 2064 | Result = VectorType::get(EltTy, unsigned(Size)); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2065 | } else { |
Nick Lewycky | 0aa6a74 | 2009-06-07 07:26:46 +0000 | [diff] [blame] | 2066 | if (!ArrayType::isValidElementType(EltTy)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2067 | return Error(TypeLoc, "invalid array element type"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2068 | Result = ArrayType::get(EltTy, Size); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2069 | } |
| 2070 | return false; |
| 2071 | } |
| 2072 | |
| 2073 | //===----------------------------------------------------------------------===// |
| 2074 | // Function Semantic Analysis. |
| 2075 | //===----------------------------------------------------------------------===// |
| 2076 | |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 2077 | LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f, |
| 2078 | int functionNumber) |
| 2079 | : P(p), F(f), FunctionNumber(functionNumber) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2080 | |
| 2081 | // Insert unnamed arguments into the NumberedVals list. |
| 2082 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); |
| 2083 | AI != E; ++AI) |
| 2084 | if (!AI->hasName()) |
| 2085 | NumberedVals.push_back(AI); |
| 2086 | } |
| 2087 | |
| 2088 | LLParser::PerFunctionState::~PerFunctionState() { |
| 2089 | // If there were any forward referenced non-basicblock values, delete them. |
| 2090 | for (std::map<std::string, std::pair<Value*, LocTy> >::iterator |
| 2091 | I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I) |
| 2092 | if (!isa<BasicBlock>(I->second.first)) { |
Owen Anderson | 09063ce | 2009-07-02 17:04:01 +0000 | [diff] [blame] | 2093 | I->second.first->replaceAllUsesWith( |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2094 | UndefValue::get(I->second.first->getType())); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2095 | delete I->second.first; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2096 | I->second.first = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2097 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2098 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2099 | for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator |
| 2100 | I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I) |
| 2101 | if (!isa<BasicBlock>(I->second.first)) { |
Owen Anderson | 09063ce | 2009-07-02 17:04:01 +0000 | [diff] [blame] | 2102 | I->second.first->replaceAllUsesWith( |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2103 | UndefValue::get(I->second.first->getType())); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2104 | delete I->second.first; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2105 | I->second.first = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2106 | } |
| 2107 | } |
| 2108 | |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 2109 | bool LLParser::PerFunctionState::FinishFunction() { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2110 | if (!ForwardRefVals.empty()) |
| 2111 | return P.Error(ForwardRefVals.begin()->second.second, |
| 2112 | "use of undefined value '%" + ForwardRefVals.begin()->first + |
| 2113 | "'"); |
| 2114 | if (!ForwardRefValIDs.empty()) |
| 2115 | return P.Error(ForwardRefValIDs.begin()->second.second, |
| 2116 | "use of undefined value '%" + |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 2117 | Twine(ForwardRefValIDs.begin()->first) + "'"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2118 | return false; |
| 2119 | } |
| 2120 | |
| 2121 | |
| 2122 | /// GetVal - Get a value with the specified name or ID, creating a |
| 2123 | /// forward reference record if needed. This can return null if the value |
| 2124 | /// exists but does not have the right type. |
| 2125 | Value *LLParser::PerFunctionState::GetVal(const std::string &Name, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2126 | Type *Ty, LocTy Loc) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2127 | // Look this name up in the normal function symbol table. |
| 2128 | Value *Val = F.getValueSymbolTable().lookup(Name); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2129 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2130 | // If this is a forward reference for the value, see if we already created a |
| 2131 | // forward ref record. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2132 | if (!Val) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2133 | std::map<std::string, std::pair<Value*, LocTy> >::iterator |
| 2134 | I = ForwardRefVals.find(Name); |
| 2135 | if (I != ForwardRefVals.end()) |
| 2136 | Val = I->second.first; |
| 2137 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2138 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2139 | // If we have the value in the symbol table or fwd-ref table, return it. |
| 2140 | if (Val) { |
| 2141 | if (Val->getType() == Ty) return Val; |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 2142 | if (Ty->isLabelTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2143 | P.Error(Loc, "'%" + Name + "' is not a basic block"); |
| 2144 | else |
| 2145 | P.Error(Loc, "'%" + Name + "' defined with type '" + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 2146 | getTypeString(Val->getType()) + "'"); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2147 | return nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2148 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2149 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2150 | // Don't make placeholders with invalid type. |
Duncan P. N. Exon Smith | 6a6e9cb | 2014-08-05 18:22:58 +0000 | [diff] [blame] | 2151 | if (!Ty->isFirstClassType()) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2152 | P.Error(Loc, "invalid use of a non-first-class type"); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2153 | return nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2154 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2155 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2156 | // Otherwise, create a new forward reference for this value and remember it. |
| 2157 | Value *FwdVal; |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 2158 | if (Ty->isLabelTy()) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2159 | FwdVal = BasicBlock::Create(F.getContext(), Name, &F); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2160 | else |
| 2161 | FwdVal = new Argument(Ty, Name); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2162 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2163 | ForwardRefVals[Name] = std::make_pair(FwdVal, Loc); |
| 2164 | return FwdVal; |
| 2165 | } |
| 2166 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2167 | Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty, |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2168 | LocTy Loc) { |
| 2169 | // Look this name up in the normal function symbol table. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2170 | Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2171 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2172 | // If this is a forward reference for the value, see if we already created a |
| 2173 | // forward ref record. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2174 | if (!Val) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2175 | std::map<unsigned, std::pair<Value*, LocTy> >::iterator |
| 2176 | I = ForwardRefValIDs.find(ID); |
| 2177 | if (I != ForwardRefValIDs.end()) |
| 2178 | Val = I->second.first; |
| 2179 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2180 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2181 | // If we have the value in the symbol table or fwd-ref table, return it. |
| 2182 | if (Val) { |
| 2183 | if (Val->getType() == Ty) return Val; |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 2184 | if (Ty->isLabelTy()) |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 2185 | P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2186 | else |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 2187 | P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 2188 | getTypeString(Val->getType()) + "'"); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2189 | return nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2190 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2191 | |
Duncan P. N. Exon Smith | 6a6e9cb | 2014-08-05 18:22:58 +0000 | [diff] [blame] | 2192 | if (!Ty->isFirstClassType()) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2193 | P.Error(Loc, "invalid use of a non-first-class type"); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2194 | return nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2195 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2196 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2197 | // Otherwise, create a new forward reference for this value and remember it. |
| 2198 | Value *FwdVal; |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 2199 | if (Ty->isLabelTy()) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2200 | FwdVal = BasicBlock::Create(F.getContext(), "", &F); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2201 | else |
| 2202 | FwdVal = new Argument(Ty); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2203 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2204 | ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc); |
| 2205 | return FwdVal; |
| 2206 | } |
| 2207 | |
| 2208 | /// SetInstName - After an instruction is parsed and inserted into its |
| 2209 | /// basic block, this installs its name. |
| 2210 | bool LLParser::PerFunctionState::SetInstName(int NameID, |
| 2211 | const std::string &NameStr, |
| 2212 | LocTy NameLoc, Instruction *Inst) { |
| 2213 | // If this instruction has void type, it cannot have a name or ID specified. |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 2214 | if (Inst->getType()->isVoidTy()) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2215 | if (NameID != -1 || !NameStr.empty()) |
| 2216 | return P.Error(NameLoc, "instructions returning void cannot have a name"); |
| 2217 | return false; |
| 2218 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2219 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2220 | // If this was a numbered instruction, verify that the instruction is the |
| 2221 | // expected value and resolve any forward references. |
| 2222 | if (NameStr.empty()) { |
| 2223 | // If neither a name nor an ID was specified, just use the next ID. |
| 2224 | if (NameID == -1) |
| 2225 | NameID = NumberedVals.size(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2226 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2227 | if (unsigned(NameID) != NumberedVals.size()) |
| 2228 | return P.Error(NameLoc, "instruction expected to be numbered '%" + |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 2229 | Twine(NumberedVals.size()) + "'"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2230 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2231 | std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI = |
| 2232 | ForwardRefValIDs.find(NameID); |
| 2233 | if (FI != ForwardRefValIDs.end()) { |
| 2234 | if (FI->second.first->getType() != Inst->getType()) |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2235 | return P.Error(NameLoc, "instruction forward referenced with type '" + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 2236 | getTypeString(FI->second.first->getType()) + "'"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2237 | FI->second.first->replaceAllUsesWith(Inst); |
Nuno Lopes | 2baa6a3 | 2009-09-02 15:02:57 +0000 | [diff] [blame] | 2238 | delete FI->second.first; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2239 | ForwardRefValIDs.erase(FI); |
| 2240 | } |
| 2241 | |
| 2242 | NumberedVals.push_back(Inst); |
| 2243 | return false; |
| 2244 | } |
| 2245 | |
| 2246 | // Otherwise, the instruction had a name. Resolve forward refs and set it. |
| 2247 | std::map<std::string, std::pair<Value*, LocTy> >::iterator |
| 2248 | FI = ForwardRefVals.find(NameStr); |
| 2249 | if (FI != ForwardRefVals.end()) { |
| 2250 | if (FI->second.first->getType() != Inst->getType()) |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2251 | return P.Error(NameLoc, "instruction forward referenced with type '" + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 2252 | getTypeString(FI->second.first->getType()) + "'"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2253 | FI->second.first->replaceAllUsesWith(Inst); |
Nuno Lopes | 2fcee70 | 2009-09-02 14:22:03 +0000 | [diff] [blame] | 2254 | delete FI->second.first; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2255 | ForwardRefVals.erase(FI); |
| 2256 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2257 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2258 | // Set the name on the instruction. |
| 2259 | Inst->setName(NameStr); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2260 | |
Benjamin Kramer | 1dc34b4 | 2010-10-16 11:28:23 +0000 | [diff] [blame] | 2261 | if (Inst->getName() != NameStr) |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2262 | return P.Error(NameLoc, "multiple definition of local value named '" + |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2263 | NameStr + "'"); |
| 2264 | return false; |
| 2265 | } |
| 2266 | |
| 2267 | /// GetBB - Get a basic block with the specified name or ID, creating a |
| 2268 | /// forward reference record if needed. |
| 2269 | BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name, |
| 2270 | LocTy Loc) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2271 | return cast_or_null<BasicBlock>(GetVal(Name, |
| 2272 | Type::getLabelTy(F.getContext()), Loc)); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
| 2275 | BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2276 | return cast_or_null<BasicBlock>(GetVal(ID, |
| 2277 | Type::getLabelTy(F.getContext()), Loc)); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2278 | } |
| 2279 | |
| 2280 | /// DefineBB - Define the specified basic block, which is either named or |
| 2281 | /// unnamed. If there is an error, this returns null otherwise it returns |
| 2282 | /// the block being defined. |
| 2283 | BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name, |
| 2284 | LocTy Loc) { |
| 2285 | BasicBlock *BB; |
| 2286 | if (Name.empty()) |
| 2287 | BB = GetBB(NumberedVals.size(), Loc); |
| 2288 | else |
| 2289 | BB = GetBB(Name, Loc); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2290 | if (!BB) return nullptr; // Already diagnosed error. |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2291 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2292 | // Move the block to the end of the function. Forward ref'd blocks are |
| 2293 | // inserted wherever they happen to be referenced. |
| 2294 | F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2295 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2296 | // Remove the block from forward ref sets. |
| 2297 | if (Name.empty()) { |
| 2298 | ForwardRefValIDs.erase(NumberedVals.size()); |
| 2299 | NumberedVals.push_back(BB); |
| 2300 | } else { |
| 2301 | // BB forward references are already in the function symbol table. |
| 2302 | ForwardRefVals.erase(Name); |
| 2303 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2304 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2305 | return BB; |
| 2306 | } |
| 2307 | |
| 2308 | //===----------------------------------------------------------------------===// |
| 2309 | // Constants. |
| 2310 | //===----------------------------------------------------------------------===// |
| 2311 | |
| 2312 | /// ParseValID - Parse an abstract value that doesn't necessarily have a |
| 2313 | /// type implied. For example, if we parse "4" we don't know what integer type |
| 2314 | /// it has. The value will later be combined with its type and checked for |
Victor Hernandez | b8fd152 | 2010-01-10 07:14:18 +0000 | [diff] [blame] | 2315 | /// sanity. PFS is used to convert function-local operands of metadata (since |
| 2316 | /// metadata operands are not just parsed here but also converted to values). |
| 2317 | /// PFS can be null when we are not parsing metadata values inside a function. |
Victor Hernandez | dc6e65a | 2010-01-05 22:22:14 +0000 | [diff] [blame] | 2318 | bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2319 | ID.Loc = Lex.getLoc(); |
| 2320 | switch (Lex.getKind()) { |
| 2321 | default: return TokError("expected value token"); |
| 2322 | case lltok::GlobalID: // @42 |
| 2323 | ID.UIntVal = Lex.getUIntVal(); |
| 2324 | ID.Kind = ValID::t_GlobalID; |
| 2325 | break; |
| 2326 | case lltok::GlobalVar: // @foo |
| 2327 | ID.StrVal = Lex.getStrVal(); |
| 2328 | ID.Kind = ValID::t_GlobalName; |
| 2329 | break; |
| 2330 | case lltok::LocalVarID: // %42 |
| 2331 | ID.UIntVal = Lex.getUIntVal(); |
| 2332 | ID.Kind = ValID::t_LocalID; |
| 2333 | break; |
| 2334 | case lltok::LocalVar: // %foo |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2335 | ID.StrVal = Lex.getStrVal(); |
| 2336 | ID.Kind = ValID::t_LocalName; |
| 2337 | break; |
| 2338 | case lltok::APSInt: |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2339 | ID.APSIntVal = Lex.getAPSIntVal(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2340 | ID.Kind = ValID::t_APSInt; |
| 2341 | break; |
| 2342 | case lltok::APFloat: |
| 2343 | ID.APFloatVal = Lex.getAPFloatVal(); |
| 2344 | ID.Kind = ValID::t_APFloat; |
| 2345 | break; |
| 2346 | case lltok::kw_true: |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 2347 | ID.ConstantVal = ConstantInt::getTrue(Context); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2348 | ID.Kind = ValID::t_Constant; |
| 2349 | break; |
| 2350 | case lltok::kw_false: |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 2351 | ID.ConstantVal = ConstantInt::getFalse(Context); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2352 | ID.Kind = ValID::t_Constant; |
| 2353 | break; |
| 2354 | case lltok::kw_null: ID.Kind = ValID::t_Null; break; |
| 2355 | case lltok::kw_undef: ID.Kind = ValID::t_Undef; break; |
| 2356 | case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2357 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2358 | case lltok::lbrace: { |
| 2359 | // ValID ::= '{' ConstVector '}' |
| 2360 | Lex.Lex(); |
| 2361 | SmallVector<Constant*, 16> Elts; |
| 2362 | if (ParseGlobalValueVector(Elts) || |
| 2363 | ParseToken(lltok::rbrace, "expected end of struct constant")) |
| 2364 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2365 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2366 | ID.ConstantStructElts = new Constant*[Elts.size()]; |
| 2367 | ID.UIntVal = Elts.size(); |
| 2368 | memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0])); |
| 2369 | ID.Kind = ValID::t_ConstantStruct; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2370 | return false; |
| 2371 | } |
| 2372 | case lltok::less: { |
| 2373 | // ValID ::= '<' ConstVector '>' --> Vector. |
| 2374 | // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct. |
| 2375 | Lex.Lex(); |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2376 | bool isPackedStruct = EatIfPresent(lltok::lbrace); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2377 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2378 | SmallVector<Constant*, 16> Elts; |
| 2379 | LocTy FirstEltLoc = Lex.getLoc(); |
| 2380 | if (ParseGlobalValueVector(Elts) || |
| 2381 | (isPackedStruct && |
| 2382 | ParseToken(lltok::rbrace, "expected end of packed struct")) || |
| 2383 | ParseToken(lltok::greater, "expected end of constant")) |
| 2384 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2385 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2386 | if (isPackedStruct) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2387 | ID.ConstantStructElts = new Constant*[Elts.size()]; |
| 2388 | memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0])); |
| 2389 | ID.UIntVal = Elts.size(); |
| 2390 | ID.Kind = ValID::t_PackedConstantStruct; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2391 | return false; |
| 2392 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2393 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2394 | if (Elts.empty()) |
| 2395 | return Error(ID.Loc, "constant vector must not be empty"); |
| 2396 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2397 | if (!Elts[0]->getType()->isIntegerTy() && |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 2398 | !Elts[0]->getType()->isFloatingPointTy() && |
| 2399 | !Elts[0]->getType()->isPointerTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2400 | return Error(FirstEltLoc, |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 2401 | "vector elements must have integer, pointer or floating point type"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2402 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2403 | // Verify that all the vector elements have the same type. |
| 2404 | for (unsigned i = 1, e = Elts.size(); i != e; ++i) |
| 2405 | if (Elts[i]->getType() != Elts[0]->getType()) |
| 2406 | return Error(FirstEltLoc, |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 2407 | "vector element #" + Twine(i) + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 2408 | " is not of type '" + getTypeString(Elts[0]->getType())); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2409 | |
Chris Lattner | 6922931 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 2410 | ID.ConstantVal = ConstantVector::get(Elts); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2411 | ID.Kind = ValID::t_Constant; |
| 2412 | return false; |
| 2413 | } |
| 2414 | case lltok::lsquare: { // Array Constant |
| 2415 | Lex.Lex(); |
| 2416 | SmallVector<Constant*, 16> Elts; |
| 2417 | LocTy FirstEltLoc = Lex.getLoc(); |
| 2418 | if (ParseGlobalValueVector(Elts) || |
| 2419 | ParseToken(lltok::rsquare, "expected end of array constant")) |
| 2420 | return true; |
| 2421 | |
| 2422 | // Handle empty element. |
| 2423 | if (Elts.empty()) { |
| 2424 | // Use undef instead of an array because it's inconvenient to determine |
| 2425 | // the element type at this point, there being no elements to examine. |
Chris Lattner | 998fa0a | 2009-01-05 07:52:51 +0000 | [diff] [blame] | 2426 | ID.Kind = ValID::t_EmptyArray; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2427 | return false; |
| 2428 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2429 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2430 | if (!Elts[0]->getType()->isFirstClassType()) |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2431 | return Error(FirstEltLoc, "invalid array element type: " + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 2432 | getTypeString(Elts[0]->getType())); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2433 | |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 2434 | ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size()); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2435 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2436 | // Verify all elements are correct type! |
Chris Lattner | 59d0e3b | 2009-01-02 08:49:06 +0000 | [diff] [blame] | 2437 | for (unsigned i = 0, e = Elts.size(); i != e; ++i) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2438 | if (Elts[i]->getType() != Elts[0]->getType()) |
| 2439 | return Error(FirstEltLoc, |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 2440 | "array element #" + Twine(i) + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 2441 | " is not of type '" + getTypeString(Elts[0]->getType())); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2442 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2443 | |
Jay Foad | 83be361 | 2011-06-22 09:24:39 +0000 | [diff] [blame] | 2444 | ID.ConstantVal = ConstantArray::get(ATy, Elts); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2445 | ID.Kind = ValID::t_Constant; |
| 2446 | return false; |
| 2447 | } |
| 2448 | case lltok::kw_c: // c "foo" |
| 2449 | Lex.Lex(); |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2450 | ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(), |
| 2451 | false); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2452 | if (ParseToken(lltok::StringConstant, "expected string")) return true; |
| 2453 | ID.Kind = ValID::t_Constant; |
| 2454 | return false; |
| 2455 | |
| 2456 | case lltok::kw_asm: { |
Chad Rosier | 6f9c385 | 2013-02-14 20:44:07 +0000 | [diff] [blame] | 2457 | // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ',' |
| 2458 | // STRINGCONSTANT |
Chad Rosier | d8c7610 | 2012-09-05 19:00:49 +0000 | [diff] [blame] | 2459 | bool HasSideEffect, AlignStack, AsmDialect; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2460 | Lex.Lex(); |
| 2461 | if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) || |
Dale Johannesen | 1cfb958 | 2009-10-21 23:28:00 +0000 | [diff] [blame] | 2462 | ParseOptionalToken(lltok::kw_alignstack, AlignStack) || |
Chad Rosier | d8c7610 | 2012-09-05 19:00:49 +0000 | [diff] [blame] | 2463 | ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) || |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2464 | ParseStringConstant(ID.StrVal) || |
| 2465 | ParseToken(lltok::comma, "expected comma in inline asm expression") || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2466 | ParseToken(lltok::StringConstant, "expected constraint string")) |
| 2467 | return true; |
| 2468 | ID.StrVal2 = Lex.getStrVal(); |
Chad Rosier | f42fad6 | 2012-09-05 00:08:17 +0000 | [diff] [blame] | 2469 | ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) | |
Chad Rosier | d8c7610 | 2012-09-05 19:00:49 +0000 | [diff] [blame] | 2470 | (unsigned(AsmDialect)<<2); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2471 | ID.Kind = ValID::t_InlineAsm; |
| 2472 | return false; |
| 2473 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2474 | |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 2475 | case lltok::kw_blockaddress: { |
| 2476 | // ValID ::= 'blockaddress' '(' @foo ',' %bar ')' |
| 2477 | Lex.Lex(); |
| 2478 | |
| 2479 | ValID Fn, Label; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2480 | |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 2481 | if (ParseToken(lltok::lparen, "expected '(' in block address expression") || |
| 2482 | ParseValID(Fn) || |
| 2483 | ParseToken(lltok::comma, "expected comma in block address expression")|| |
| 2484 | ParseValID(Label) || |
| 2485 | ParseToken(lltok::rparen, "expected ')' in block address expression")) |
| 2486 | return true; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2487 | |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 2488 | if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName) |
| 2489 | return Error(Fn.Loc, "expected function name in blockaddress"); |
Chris Lattner | aa99c94 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 2490 | if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName) |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 2491 | return Error(Label.Loc, "expected basic block name in blockaddress"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2492 | |
Duncan P. N. Exon Smith | 1716990 | 2014-08-19 00:13:19 +0000 | [diff] [blame] | 2493 | // Try to find the function (but skip it if it's forward-referenced). |
| 2494 | GlobalValue *GV = nullptr; |
| 2495 | if (Fn.Kind == ValID::t_GlobalID) { |
| 2496 | if (Fn.UIntVal < NumberedVals.size()) |
| 2497 | GV = NumberedVals[Fn.UIntVal]; |
| 2498 | } else if (!ForwardRefVals.count(Fn.StrVal)) { |
| 2499 | GV = M->getNamedValue(Fn.StrVal); |
| 2500 | } |
| 2501 | Function *F = nullptr; |
| 2502 | if (GV) { |
| 2503 | // Confirm that it's actually a function with a definition. |
| 2504 | if (!isa<Function>(GV)) |
| 2505 | return Error(Fn.Loc, "expected function name in blockaddress"); |
| 2506 | F = cast<Function>(GV); |
| 2507 | if (F->isDeclaration()) |
| 2508 | return Error(Fn.Loc, "cannot take blockaddress inside a declaration"); |
| 2509 | } |
| 2510 | |
| 2511 | if (!F) { |
| 2512 | // Make a global variable as a placeholder for this reference. |
| 2513 | GlobalValue *&FwdRef = ForwardRefBlockAddresses[Fn][Label]; |
| 2514 | if (!FwdRef) |
| 2515 | FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context), false, |
| 2516 | GlobalValue::InternalLinkage, nullptr, ""); |
| 2517 | ID.ConstantVal = FwdRef; |
| 2518 | ID.Kind = ValID::t_Constant; |
| 2519 | return false; |
| 2520 | } |
| 2521 | |
| 2522 | // We found the function; now find the basic block. Don't use PFS, since we |
| 2523 | // might be inside a constant expression. |
| 2524 | BasicBlock *BB; |
| 2525 | if (BlockAddressPFS && F == &BlockAddressPFS->getFunction()) { |
| 2526 | if (Label.Kind == ValID::t_LocalID) |
| 2527 | BB = BlockAddressPFS->GetBB(Label.UIntVal, Label.Loc); |
| 2528 | else |
| 2529 | BB = BlockAddressPFS->GetBB(Label.StrVal, Label.Loc); |
| 2530 | if (!BB) |
| 2531 | return Error(Label.Loc, "referenced value is not a basic block"); |
| 2532 | } else { |
| 2533 | if (Label.Kind == ValID::t_LocalID) |
| 2534 | return Error(Label.Loc, "cannot take address of numeric label after " |
| 2535 | "the function is defined"); |
| 2536 | BB = dyn_cast_or_null<BasicBlock>( |
| 2537 | F->getValueSymbolTable().lookup(Label.StrVal)); |
| 2538 | if (!BB) |
| 2539 | return Error(Label.Loc, "referenced value is not a basic block"); |
| 2540 | } |
| 2541 | |
| 2542 | ID.ConstantVal = BlockAddress::get(F, BB); |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 2543 | ID.Kind = ValID::t_Constant; |
| 2544 | return false; |
| 2545 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2546 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2547 | case lltok::kw_trunc: |
| 2548 | case lltok::kw_zext: |
| 2549 | case lltok::kw_sext: |
| 2550 | case lltok::kw_fptrunc: |
| 2551 | case lltok::kw_fpext: |
| 2552 | case lltok::kw_bitcast: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2553 | case lltok::kw_addrspacecast: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2554 | case lltok::kw_uitofp: |
| 2555 | case lltok::kw_sitofp: |
| 2556 | case lltok::kw_fptoui: |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2557 | case lltok::kw_fptosi: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2558 | case lltok::kw_inttoptr: |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2559 | case lltok::kw_ptrtoint: { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2560 | unsigned Opc = Lex.getUIntVal(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2561 | Type *DestTy = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2562 | Constant *SrcVal; |
| 2563 | Lex.Lex(); |
| 2564 | if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") || |
| 2565 | ParseGlobalTypeAndValue(SrcVal) || |
Dan Gohman | 0b5d042 | 2009-06-15 21:52:11 +0000 | [diff] [blame] | 2566 | ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2567 | ParseType(DestTy) || |
| 2568 | ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast")) |
| 2569 | return true; |
| 2570 | if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy)) |
| 2571 | return Error(ID.Loc, "invalid cast opcode for cast from '" + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 2572 | getTypeString(SrcVal->getType()) + "' to '" + |
| 2573 | getTypeString(DestTy) + "'"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2574 | ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc, |
Owen Anderson | 02a9da3 | 2009-07-01 23:57:11 +0000 | [diff] [blame] | 2575 | SrcVal, DestTy); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2576 | ID.Kind = ValID::t_Constant; |
| 2577 | return false; |
| 2578 | } |
| 2579 | case lltok::kw_extractvalue: { |
| 2580 | Lex.Lex(); |
| 2581 | Constant *Val; |
| 2582 | SmallVector<unsigned, 4> Indices; |
| 2583 | if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")|| |
| 2584 | ParseGlobalTypeAndValue(Val) || |
| 2585 | ParseIndexList(Indices) || |
| 2586 | ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr")) |
| 2587 | return true; |
Devang Patel | 1cb5116 | 2009-11-03 19:06:07 +0000 | [diff] [blame] | 2588 | |
Chris Lattner | 392be58 | 2010-02-12 20:49:41 +0000 | [diff] [blame] | 2589 | if (!Val->getType()->isAggregateType()) |
| 2590 | return Error(ID.Loc, "extractvalue operand must be aggregate type"); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2591 | if (!ExtractValueInst::getIndexedType(Val->getType(), Indices)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2592 | return Error(ID.Loc, "invalid indices for extractvalue"); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2593 | ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2594 | ID.Kind = ValID::t_Constant; |
| 2595 | return false; |
| 2596 | } |
| 2597 | case lltok::kw_insertvalue: { |
| 2598 | Lex.Lex(); |
| 2599 | Constant *Val0, *Val1; |
| 2600 | SmallVector<unsigned, 4> Indices; |
| 2601 | if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")|| |
| 2602 | ParseGlobalTypeAndValue(Val0) || |
| 2603 | ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")|| |
| 2604 | ParseGlobalTypeAndValue(Val1) || |
| 2605 | ParseIndexList(Indices) || |
| 2606 | ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr")) |
| 2607 | return true; |
Chris Lattner | 392be58 | 2010-02-12 20:49:41 +0000 | [diff] [blame] | 2608 | if (!Val0->getType()->isAggregateType()) |
| 2609 | return Error(ID.Loc, "insertvalue operand must be aggregate type"); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2610 | if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2611 | return Error(ID.Loc, "invalid indices for insertvalue"); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2612 | ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2613 | ID.Kind = ValID::t_Constant; |
| 2614 | return false; |
| 2615 | } |
| 2616 | case lltok::kw_icmp: |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 2617 | case lltok::kw_fcmp: { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2618 | unsigned PredVal, Opc = Lex.getUIntVal(); |
| 2619 | Constant *Val0, *Val1; |
| 2620 | Lex.Lex(); |
| 2621 | if (ParseCmpPredicate(PredVal, Opc) || |
| 2622 | ParseToken(lltok::lparen, "expected '(' in compare constantexpr") || |
| 2623 | ParseGlobalTypeAndValue(Val0) || |
| 2624 | ParseToken(lltok::comma, "expected comma in compare constantexpr") || |
| 2625 | ParseGlobalTypeAndValue(Val1) || |
| 2626 | ParseToken(lltok::rparen, "expected ')' in compare constantexpr")) |
| 2627 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2628 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2629 | if (Val0->getType() != Val1->getType()) |
| 2630 | return Error(ID.Loc, "compare operands must have the same type"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2631 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2632 | CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2633 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2634 | if (Opc == Instruction::FCmp) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2635 | if (!Val0->getType()->isFPOrFPVectorTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2636 | return Error(ID.Loc, "fcmp requires floating point operands"); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2637 | ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1); |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 2638 | } else { |
| 2639 | assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2640 | if (!Val0->getType()->isIntOrIntVectorTy() && |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 2641 | !Val0->getType()->getScalarType()->isPointerTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2642 | return Error(ID.Loc, "icmp requires pointer or integer operands"); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2643 | ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2644 | } |
| 2645 | ID.Kind = ValID::t_Constant; |
| 2646 | return false; |
| 2647 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2648 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2649 | // Binary Operators. |
| 2650 | case lltok::kw_add: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2651 | case lltok::kw_fadd: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2652 | case lltok::kw_sub: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2653 | case lltok::kw_fsub: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2654 | case lltok::kw_mul: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2655 | case lltok::kw_fmul: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2656 | case lltok::kw_udiv: |
| 2657 | case lltok::kw_sdiv: |
| 2658 | case lltok::kw_fdiv: |
| 2659 | case lltok::kw_urem: |
| 2660 | case lltok::kw_srem: |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2661 | case lltok::kw_frem: |
| 2662 | case lltok::kw_shl: |
| 2663 | case lltok::kw_lshr: |
| 2664 | case lltok::kw_ashr: { |
Dan Gohman | 9c7f808 | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 2665 | bool NUW = false; |
| 2666 | bool NSW = false; |
| 2667 | bool Exact = false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2668 | unsigned Opc = Lex.getUIntVal(); |
| 2669 | Constant *Val0, *Val1; |
| 2670 | Lex.Lex(); |
Dan Gohman | 9c7f808 | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 2671 | LocTy ModifierLoc = Lex.getLoc(); |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2672 | if (Opc == Instruction::Add || Opc == Instruction::Sub || |
| 2673 | Opc == Instruction::Mul || Opc == Instruction::Shl) { |
Dan Gohman | 9c7f808 | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 2674 | if (EatIfPresent(lltok::kw_nuw)) |
| 2675 | NUW = true; |
| 2676 | if (EatIfPresent(lltok::kw_nsw)) { |
| 2677 | NSW = true; |
| 2678 | if (EatIfPresent(lltok::kw_nuw)) |
| 2679 | NUW = true; |
| 2680 | } |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2681 | } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv || |
| 2682 | Opc == Instruction::LShr || Opc == Instruction::AShr) { |
Dan Gohman | 9c7f808 | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 2683 | if (EatIfPresent(lltok::kw_exact)) |
| 2684 | Exact = true; |
| 2685 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2686 | if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") || |
| 2687 | ParseGlobalTypeAndValue(Val0) || |
| 2688 | ParseToken(lltok::comma, "expected comma in binary constantexpr") || |
| 2689 | ParseGlobalTypeAndValue(Val1) || |
| 2690 | ParseToken(lltok::rparen, "expected ')' in binary constantexpr")) |
| 2691 | return true; |
| 2692 | if (Val0->getType() != Val1->getType()) |
| 2693 | return Error(ID.Loc, "operands of constexpr must have same type"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2694 | if (!Val0->getType()->isIntOrIntVectorTy()) { |
Dan Gohman | 9c7f808 | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 2695 | if (NUW) |
| 2696 | return Error(ModifierLoc, "nuw only applies to integer operations"); |
| 2697 | if (NSW) |
| 2698 | return Error(ModifierLoc, "nsw only applies to integer operations"); |
| 2699 | } |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 2700 | // Check that the type is valid for the operator. |
| 2701 | switch (Opc) { |
| 2702 | case Instruction::Add: |
| 2703 | case Instruction::Sub: |
| 2704 | case Instruction::Mul: |
| 2705 | case Instruction::UDiv: |
| 2706 | case Instruction::SDiv: |
| 2707 | case Instruction::URem: |
| 2708 | case Instruction::SRem: |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2709 | case Instruction::Shl: |
| 2710 | case Instruction::AShr: |
| 2711 | case Instruction::LShr: |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 2712 | if (!Val0->getType()->isIntOrIntVectorTy()) |
| 2713 | return Error(ID.Loc, "constexpr requires integer operands"); |
| 2714 | break; |
| 2715 | case Instruction::FAdd: |
| 2716 | case Instruction::FSub: |
| 2717 | case Instruction::FMul: |
| 2718 | case Instruction::FDiv: |
| 2719 | case Instruction::FRem: |
| 2720 | if (!Val0->getType()->isFPOrFPVectorTy()) |
| 2721 | return Error(ID.Loc, "constexpr requires fp operands"); |
| 2722 | break; |
| 2723 | default: llvm_unreachable("Unknown binary operator!"); |
| 2724 | } |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2725 | unsigned Flags = 0; |
| 2726 | if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap; |
| 2727 | if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap; |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 2728 | if (Exact) Flags |= PossiblyExactOperator::IsExact; |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2729 | Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags); |
Dan Gohman | 9c7f808 | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 2730 | ID.ConstantVal = C; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2731 | ID.Kind = ValID::t_Constant; |
| 2732 | return false; |
| 2733 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2734 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2735 | // Logical Operations |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2736 | case lltok::kw_and: |
| 2737 | case lltok::kw_or: |
| 2738 | case lltok::kw_xor: { |
| 2739 | unsigned Opc = Lex.getUIntVal(); |
| 2740 | Constant *Val0, *Val1; |
| 2741 | Lex.Lex(); |
| 2742 | if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") || |
| 2743 | ParseGlobalTypeAndValue(Val0) || |
| 2744 | ParseToken(lltok::comma, "expected comma in logical constantexpr") || |
| 2745 | ParseGlobalTypeAndValue(Val1) || |
| 2746 | ParseToken(lltok::rparen, "expected ')' in logical constantexpr")) |
| 2747 | return true; |
| 2748 | if (Val0->getType() != Val1->getType()) |
| 2749 | return Error(ID.Loc, "operands of constexpr must have same type"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2750 | if (!Val0->getType()->isIntOrIntVectorTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2751 | return Error(ID.Loc, |
| 2752 | "constexpr requires integer or integer vector operands"); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2753 | ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2754 | ID.Kind = ValID::t_Constant; |
| 2755 | return false; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2758 | case lltok::kw_getelementptr: |
| 2759 | case lltok::kw_shufflevector: |
| 2760 | case lltok::kw_insertelement: |
| 2761 | case lltok::kw_extractelement: |
| 2762 | case lltok::kw_select: { |
| 2763 | unsigned Opc = Lex.getUIntVal(); |
| 2764 | SmallVector<Constant*, 16> Elts; |
Dan Gohman | 1639c39 | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 2765 | bool InBounds = false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2766 | Lex.Lex(); |
Dan Gohman | 1639c39 | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 2767 | if (Opc == Instruction::GetElementPtr) |
Dan Gohman | 16cbbe4 | 2009-07-29 15:58:36 +0000 | [diff] [blame] | 2768 | InBounds = EatIfPresent(lltok::kw_inbounds); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2769 | if (ParseToken(lltok::lparen, "expected '(' in constantexpr") || |
| 2770 | ParseGlobalValueVector(Elts) || |
| 2771 | ParseToken(lltok::rparen, "expected ')' in constantexpr")) |
| 2772 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2773 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2774 | if (Opc == Instruction::GetElementPtr) { |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 2775 | if (Elts.size() == 0 || |
| 2776 | !Elts[0]->getType()->getScalarType()->isPointerTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2777 | return Error(ID.Loc, "getelementptr requires pointer operand"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2778 | |
Jay Foad | ed8db7d | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 2779 | ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end()); |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 2780 | if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), Indices)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2781 | return Error(ID.Loc, "invalid indices for getelementptr"); |
Jay Foad | 2f5fc8c | 2011-07-21 15:15:37 +0000 | [diff] [blame] | 2782 | ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], Indices, |
| 2783 | InBounds); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2784 | } else if (Opc == Instruction::Select) { |
| 2785 | if (Elts.size() != 3) |
| 2786 | return Error(ID.Loc, "expected three operands to select"); |
| 2787 | if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1], |
| 2788 | Elts[2])) |
| 2789 | return Error(ID.Loc, Reason); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2790 | ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2791 | } else if (Opc == Instruction::ShuffleVector) { |
| 2792 | if (Elts.size() != 3) |
| 2793 | return Error(ID.Loc, "expected three operands to shufflevector"); |
| 2794 | if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2])) |
| 2795 | return Error(ID.Loc, "invalid operands to shufflevector"); |
Owen Anderson | 02a9da3 | 2009-07-01 23:57:11 +0000 | [diff] [blame] | 2796 | ID.ConstantVal = |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2797 | ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2798 | } else if (Opc == Instruction::ExtractElement) { |
| 2799 | if (Elts.size() != 2) |
| 2800 | return Error(ID.Loc, "expected two operands to extractelement"); |
| 2801 | if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1])) |
| 2802 | return Error(ID.Loc, "invalid extractelement operands"); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2803 | ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2804 | } else { |
| 2805 | assert(Opc == Instruction::InsertElement && "Unknown opcode"); |
| 2806 | if (Elts.size() != 3) |
| 2807 | return Error(ID.Loc, "expected three operands to insertelement"); |
| 2808 | if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2])) |
| 2809 | return Error(ID.Loc, "invalid insertelement operands"); |
Owen Anderson | 02a9da3 | 2009-07-01 23:57:11 +0000 | [diff] [blame] | 2810 | ID.ConstantVal = |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2811 | ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2812 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2813 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2814 | ID.Kind = ValID::t_Constant; |
| 2815 | return false; |
| 2816 | } |
| 2817 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2818 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2819 | Lex.Lex(); |
| 2820 | return false; |
| 2821 | } |
| 2822 | |
| 2823 | /// ParseGlobalValue - Parse a global value with the specified type. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2824 | bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2825 | C = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2826 | ValID ID; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2827 | Value *V = nullptr; |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 2828 | bool Parsed = ParseValID(ID) || |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2829 | ConvertValIDToValue(Ty, ID, V, nullptr); |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 2830 | if (V && !(C = dyn_cast<Constant>(V))) |
| 2831 | return Error(ID.Loc, "global values must be constants"); |
| 2832 | return Parsed; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 2835 | bool LLParser::ParseGlobalTypeAndValue(Constant *&V) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2836 | Type *Ty = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2837 | return ParseType(Ty) || |
| 2838 | ParseGlobalValue(Ty, V); |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
Rafael Espindola | 83a362c | 2015-01-06 22:55:16 +0000 | [diff] [blame] | 2841 | bool LLParser::parseOptionalComdat(StringRef GlobalName, Comdat *&C) { |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 2842 | C = nullptr; |
Rafael Espindola | 83a362c | 2015-01-06 22:55:16 +0000 | [diff] [blame] | 2843 | |
| 2844 | LocTy KwLoc = Lex.getLoc(); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 2845 | if (!EatIfPresent(lltok::kw_comdat)) |
| 2846 | return false; |
Rafael Espindola | 83a362c | 2015-01-06 22:55:16 +0000 | [diff] [blame] | 2847 | |
| 2848 | if (EatIfPresent(lltok::lparen)) { |
| 2849 | if (Lex.getKind() != lltok::ComdatVar) |
| 2850 | return TokError("expected comdat variable"); |
| 2851 | C = getComdat(Lex.getStrVal(), Lex.getLoc()); |
| 2852 | Lex.Lex(); |
| 2853 | if (ParseToken(lltok::rparen, "expected ')' after comdat var")) |
| 2854 | return true; |
| 2855 | } else { |
| 2856 | if (GlobalName.empty()) |
| 2857 | return TokError("comdat cannot be unnamed"); |
| 2858 | C = getComdat(GlobalName, KwLoc); |
| 2859 | } |
| 2860 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 2861 | return false; |
| 2862 | } |
| 2863 | |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 2864 | /// ParseGlobalValueVector |
| 2865 | /// ::= /*empty*/ |
| 2866 | /// ::= TypeAndValue (',' TypeAndValue)* |
Duncan P. N. Exon Smith | 1716990 | 2014-08-19 00:13:19 +0000 | [diff] [blame] | 2867 | bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts) { |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 2868 | // Empty list. |
| 2869 | if (Lex.getKind() == lltok::rbrace || |
| 2870 | Lex.getKind() == lltok::rsquare || |
| 2871 | Lex.getKind() == lltok::greater || |
| 2872 | Lex.getKind() == lltok::rparen) |
| 2873 | return false; |
| 2874 | |
| 2875 | Constant *C; |
| 2876 | if (ParseGlobalTypeAndValue(C)) return true; |
| 2877 | Elts.push_back(C); |
| 2878 | |
| 2879 | while (EatIfPresent(lltok::comma)) { |
| 2880 | if (ParseGlobalTypeAndValue(C)) return true; |
| 2881 | Elts.push_back(C); |
| 2882 | } |
| 2883 | |
| 2884 | return false; |
| 2885 | } |
| 2886 | |
Duncan P. N. Exon Smith | 58ef9d1 | 2015-01-12 21:23:11 +0000 | [diff] [blame] | 2887 | bool LLParser::ParseMDTuple(MDNode *&MD, bool IsDistinct) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 2888 | SmallVector<Metadata *, 16> Elts; |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 2889 | if (ParseMDNodeVector(Elts)) |
Dan Gohman | c828c54 | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 2890 | return true; |
| 2891 | |
Duncan P. N. Exon Smith | 0b31dd1 | 2015-01-12 22:27:39 +0000 | [diff] [blame] | 2892 | MD = (IsDistinct ? MDTuple::getDistinct : MDTuple::get)(Context, Elts); |
Dan Gohman | c828c54 | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 2893 | return false; |
| 2894 | } |
| 2895 | |
Duncan P. N. Exon Smith | f825dae | 2015-01-12 22:26:48 +0000 | [diff] [blame] | 2896 | /// MDNode: |
| 2897 | /// ::= !{ ... } |
| 2898 | /// ::= !7 |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 2899 | /// ::= !MDLocation(...) |
Duncan P. N. Exon Smith | f825dae | 2015-01-12 22:26:48 +0000 | [diff] [blame] | 2900 | bool LLParser::ParseMDNode(MDNode *&N) { |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 2901 | if (Lex.getKind() == lltok::MetadataVar) |
| 2902 | return ParseSpecializedMDNode(N); |
| 2903 | |
Duncan P. N. Exon Smith | f825dae | 2015-01-12 22:26:48 +0000 | [diff] [blame] | 2904 | return ParseToken(lltok::exclaim, "expected '!' here") || |
| 2905 | ParseMDNodeTail(N); |
| 2906 | } |
| 2907 | |
| 2908 | bool LLParser::ParseMDNodeTail(MDNode *&N) { |
| 2909 | // !{ ... } |
| 2910 | if (Lex.getKind() == lltok::lbrace) |
| 2911 | return ParseMDTuple(N); |
| 2912 | |
| 2913 | // !42 |
| 2914 | return ParseMDNodeID(N); |
| 2915 | } |
| 2916 | |
Duncan P. N. Exon Smith | 077c031 | 2015-02-04 22:05:21 +0000 | [diff] [blame] | 2917 | namespace { |
| 2918 | |
| 2919 | /// Structure to represent an optional metadata field. |
| 2920 | template <class FieldTy> struct MDFieldImpl { |
| 2921 | typedef MDFieldImpl ImplTy; |
| 2922 | FieldTy Val; |
| 2923 | bool Seen; |
| 2924 | |
| 2925 | void assign(FieldTy Val) { |
| 2926 | Seen = true; |
| 2927 | this->Val = std::move(Val); |
| 2928 | } |
| 2929 | |
| 2930 | explicit MDFieldImpl(FieldTy Default) |
| 2931 | : Val(std::move(Default)), Seen(false) {} |
| 2932 | }; |
Duncan P. N. Exon Smith | c7363f1 | 2015-02-13 01:10:38 +0000 | [diff] [blame] | 2933 | |
Duncan P. N. Exon Smith | 077c031 | 2015-02-04 22:05:21 +0000 | [diff] [blame] | 2934 | struct MDUnsignedField : public MDFieldImpl<uint64_t> { |
| 2935 | uint64_t Max; |
| 2936 | |
| 2937 | MDUnsignedField(uint64_t Default = 0, uint64_t Max = UINT64_MAX) |
| 2938 | : ImplTy(Default), Max(Max) {} |
| 2939 | }; |
Duncan P. N. Exon Smith | 9c93dc6 | 2015-02-04 22:59:18 +0000 | [diff] [blame] | 2940 | struct LineField : public MDUnsignedField { |
Duncan P. N. Exon Smith | af677eb | 2015-02-06 22:50:13 +0000 | [diff] [blame] | 2941 | LineField() : MDUnsignedField(0, UINT32_MAX) {} |
Duncan P. N. Exon Smith | 9c93dc6 | 2015-02-04 22:59:18 +0000 | [diff] [blame] | 2942 | }; |
| 2943 | struct ColumnField : public MDUnsignedField { |
| 2944 | ColumnField() : MDUnsignedField(0, UINT16_MAX) {} |
| 2945 | }; |
Duncan P. N. Exon Smith | 077c031 | 2015-02-04 22:05:21 +0000 | [diff] [blame] | 2946 | struct DwarfTagField : public MDUnsignedField { |
Duncan P. N. Exon Smith | a81f7e1 | 2015-02-06 22:29:35 +0000 | [diff] [blame] | 2947 | DwarfTagField() : MDUnsignedField(0, dwarf::DW_TAG_hi_user) {} |
Duncan P. N. Exon Smith | 077c031 | 2015-02-04 22:05:21 +0000 | [diff] [blame] | 2948 | }; |
Duncan P. N. Exon Smith | cd6636c | 2015-02-13 01:17:35 +0000 | [diff] [blame] | 2949 | struct DwarfAttEncodingField : public MDUnsignedField { |
| 2950 | DwarfAttEncodingField() : MDUnsignedField(0, dwarf::DW_ATE_hi_user) {} |
| 2951 | }; |
Duncan P. N. Exon Smith | aece2dc | 2015-02-13 01:21:25 +0000 | [diff] [blame] | 2952 | struct DwarfLangField : public MDUnsignedField { |
| 2953 | DwarfLangField() : MDUnsignedField(0, dwarf::DW_LANG_hi_user) {} |
| 2954 | }; |
Duncan P. N. Exon Smith | c7363f1 | 2015-02-13 01:10:38 +0000 | [diff] [blame] | 2955 | |
| 2956 | struct MDSignedField : public MDFieldImpl<int64_t> { |
| 2957 | int64_t Min; |
| 2958 | int64_t Max; |
| 2959 | |
| 2960 | MDSignedField(int64_t Default = 0) |
| 2961 | : ImplTy(Default), Min(INT64_MIN), Max(INT64_MAX) {} |
| 2962 | MDSignedField(int64_t Default, int64_t Min, int64_t Max) |
| 2963 | : ImplTy(Default), Min(Min), Max(Max) {} |
| 2964 | }; |
| 2965 | |
Duncan P. N. Exon Smith | aece2dc | 2015-02-13 01:21:25 +0000 | [diff] [blame] | 2966 | struct MDBoolField : public MDFieldImpl<bool> { |
| 2967 | MDBoolField(bool Default = false) : ImplTy(Default) {} |
| 2968 | }; |
Duncan P. N. Exon Smith | 077c031 | 2015-02-04 22:05:21 +0000 | [diff] [blame] | 2969 | struct MDField : public MDFieldImpl<Metadata *> { |
| 2970 | MDField() : ImplTy(nullptr) {} |
| 2971 | }; |
| 2972 | struct MDStringField : public MDFieldImpl<std::string> { |
| 2973 | MDStringField() : ImplTy(std::string()) {} |
| 2974 | }; |
| 2975 | struct MDFieldList : public MDFieldImpl<SmallVector<Metadata *, 4>> { |
| 2976 | MDFieldList() : ImplTy(SmallVector<Metadata *, 4>()) {} |
| 2977 | }; |
| 2978 | |
| 2979 | } // end namespace |
| 2980 | |
Duncan P. N. Exon Smith | 2f09c46 | 2015-02-04 22:13:28 +0000 | [diff] [blame] | 2981 | namespace llvm { |
| 2982 | |
Duncan P. N. Exon Smith | 077c031 | 2015-02-04 22:05:21 +0000 | [diff] [blame] | 2983 | template <> |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 2984 | bool LLParser::ParseMDField(LocTy Loc, StringRef Name, |
Duncan P. N. Exon Smith | 39b10c2 | 2015-02-04 21:57:52 +0000 | [diff] [blame] | 2985 | MDUnsignedField &Result) { |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 2986 | if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned()) |
| 2987 | return TokError("expected unsigned integer"); |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 2988 | |
Duncan P. N. Exon Smith | 39b10c2 | 2015-02-04 21:57:52 +0000 | [diff] [blame] | 2989 | auto &U = Lex.getAPSIntVal(); |
| 2990 | if (U.ugt(Result.Max)) |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 2991 | return TokError("value for '" + Name + "' too large, limit is " + |
| 2992 | Twine(Result.Max)); |
Duncan P. N. Exon Smith | 39b10c2 | 2015-02-04 21:57:52 +0000 | [diff] [blame] | 2993 | Result.assign(U.getZExtValue()); |
| 2994 | assert(Result.Val <= Result.Max && "Expected value in range"); |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 2995 | Lex.Lex(); |
| 2996 | return false; |
| 2997 | } |
| 2998 | |
Duncan P. N. Exon Smith | 077c031 | 2015-02-04 22:05:21 +0000 | [diff] [blame] | 2999 | template <> |
Duncan P. N. Exon Smith | 9c93dc6 | 2015-02-04 22:59:18 +0000 | [diff] [blame] | 3000 | bool LLParser::ParseMDField(LocTy Loc, StringRef Name, LineField &Result) { |
| 3001 | return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result)); |
| 3002 | } |
| 3003 | template <> |
| 3004 | bool LLParser::ParseMDField(LocTy Loc, StringRef Name, ColumnField &Result) { |
| 3005 | return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result)); |
| 3006 | } |
| 3007 | |
| 3008 | template <> |
Duncan P. N. Exon Smith | 9748607 | 2015-02-03 21:56:01 +0000 | [diff] [blame] | 3009 | bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DwarfTagField &Result) { |
| 3010 | if (Lex.getKind() == lltok::APSInt) |
Duncan P. N. Exon Smith | 39b10c2 | 2015-02-04 21:57:52 +0000 | [diff] [blame] | 3011 | return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result)); |
Duncan P. N. Exon Smith | 9748607 | 2015-02-03 21:56:01 +0000 | [diff] [blame] | 3012 | |
Duncan P. N. Exon Smith | 9748607 | 2015-02-03 21:56:01 +0000 | [diff] [blame] | 3013 | if (Lex.getKind() != lltok::DwarfTag) |
| 3014 | return TokError("expected DWARF tag"); |
| 3015 | |
| 3016 | unsigned Tag = dwarf::getTag(Lex.getStrVal()); |
| 3017 | if (Tag == dwarf::DW_TAG_invalid) |
| 3018 | return TokError("invalid DWARF tag" + Twine(" '") + Lex.getStrVal() + "'"); |
Duncan P. N. Exon Smith | fad631a | 2015-02-04 22:02:18 +0000 | [diff] [blame] | 3019 | assert(Tag <= Result.Max && "Expected valid DWARF tag"); |
Duncan P. N. Exon Smith | 9748607 | 2015-02-03 21:56:01 +0000 | [diff] [blame] | 3020 | |
| 3021 | Result.assign(Tag); |
| 3022 | Lex.Lex(); |
| 3023 | return false; |
| 3024 | } |
| 3025 | |
Duncan P. N. Exon Smith | 077c031 | 2015-02-04 22:05:21 +0000 | [diff] [blame] | 3026 | template <> |
Duncan P. N. Exon Smith | aece2dc | 2015-02-13 01:21:25 +0000 | [diff] [blame] | 3027 | bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DwarfLangField &Result) { |
| 3028 | if (Lex.getKind() == lltok::APSInt) |
| 3029 | return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result)); |
| 3030 | |
| 3031 | if (Lex.getKind() != lltok::DwarfLang) |
| 3032 | return TokError("expected DWARF language"); |
| 3033 | |
| 3034 | unsigned Lang = dwarf::getLanguage(Lex.getStrVal()); |
| 3035 | if (!Lang) |
| 3036 | return TokError("invalid DWARF language" + Twine(" '") + Lex.getStrVal() + |
| 3037 | "'"); |
| 3038 | assert(Lang <= Result.Max && "Expected valid DWARF language"); |
| 3039 | Result.assign(Lang); |
| 3040 | Lex.Lex(); |
| 3041 | return false; |
| 3042 | } |
| 3043 | |
| 3044 | template <> |
Duncan P. N. Exon Smith | c7363f1 | 2015-02-13 01:10:38 +0000 | [diff] [blame] | 3045 | bool LLParser::ParseMDField(LocTy Loc, StringRef Name, |
Duncan P. N. Exon Smith | cd6636c | 2015-02-13 01:17:35 +0000 | [diff] [blame] | 3046 | DwarfAttEncodingField &Result) { |
| 3047 | if (Lex.getKind() == lltok::APSInt) |
| 3048 | return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result)); |
| 3049 | |
| 3050 | if (Lex.getKind() != lltok::DwarfAttEncoding) |
| 3051 | return TokError("expected DWARF type attribute encoding"); |
| 3052 | |
| 3053 | unsigned Encoding = dwarf::getAttributeEncoding(Lex.getStrVal()); |
| 3054 | if (!Encoding) |
| 3055 | return TokError("invalid DWARF type attribute encoding" + Twine(" '") + |
| 3056 | Lex.getStrVal() + "'"); |
| 3057 | assert(Encoding <= Result.Max && "Expected valid DWARF language"); |
| 3058 | Result.assign(Encoding); |
| 3059 | Lex.Lex(); |
| 3060 | return false; |
| 3061 | } |
| 3062 | |
| 3063 | template <> |
| 3064 | bool LLParser::ParseMDField(LocTy Loc, StringRef Name, |
Duncan P. N. Exon Smith | c7363f1 | 2015-02-13 01:10:38 +0000 | [diff] [blame] | 3065 | MDSignedField &Result) { |
| 3066 | if (Lex.getKind() != lltok::APSInt) |
| 3067 | return TokError("expected signed integer"); |
| 3068 | |
| 3069 | auto &S = Lex.getAPSIntVal(); |
| 3070 | if (S < Result.Min) |
| 3071 | return TokError("value for '" + Name + "' too small, limit is " + |
| 3072 | Twine(Result.Min)); |
| 3073 | if (S > Result.Max) |
| 3074 | return TokError("value for '" + Name + "' too large, limit is " + |
| 3075 | Twine(Result.Max)); |
| 3076 | Result.assign(S.getExtValue()); |
| 3077 | assert(Result.Val >= Result.Min && "Expected value in range"); |
| 3078 | assert(Result.Val <= Result.Max && "Expected value in range"); |
| 3079 | Lex.Lex(); |
| 3080 | return false; |
| 3081 | } |
| 3082 | |
| 3083 | template <> |
Duncan P. N. Exon Smith | aece2dc | 2015-02-13 01:21:25 +0000 | [diff] [blame] | 3084 | bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDBoolField &Result) { |
| 3085 | switch (Lex.getKind()) { |
| 3086 | default: |
| 3087 | return TokError("expected 'true' or 'false'"); |
| 3088 | case lltok::kw_true: |
| 3089 | Result.assign(true); |
| 3090 | break; |
| 3091 | case lltok::kw_false: |
| 3092 | Result.assign(false); |
| 3093 | break; |
| 3094 | } |
| 3095 | Lex.Lex(); |
| 3096 | return false; |
| 3097 | } |
| 3098 | |
| 3099 | template <> |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 3100 | bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDField &Result) { |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 3101 | Metadata *MD; |
| 3102 | if (ParseMetadata(MD, nullptr)) |
| 3103 | return true; |
| 3104 | |
| 3105 | Result.assign(MD); |
| 3106 | return false; |
| 3107 | } |
| 3108 | |
Duncan P. N. Exon Smith | 077c031 | 2015-02-04 22:05:21 +0000 | [diff] [blame] | 3109 | template <> |
Duncan P. N. Exon Smith | 4e4aa70 | 2015-02-03 21:54:14 +0000 | [diff] [blame] | 3110 | bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDStringField &Result) { |
| 3111 | std::string S; |
| 3112 | if (ParseStringConstant(S)) |
| 3113 | return true; |
| 3114 | |
| 3115 | Result.assign(std::move(S)); |
| 3116 | return false; |
| 3117 | } |
| 3118 | |
Duncan P. N. Exon Smith | 077c031 | 2015-02-04 22:05:21 +0000 | [diff] [blame] | 3119 | template <> |
Duncan P. N. Exon Smith | 4e4aa70 | 2015-02-03 21:54:14 +0000 | [diff] [blame] | 3120 | bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDFieldList &Result) { |
| 3121 | SmallVector<Metadata *, 4> MDs; |
| 3122 | if (ParseMDNodeVector(MDs)) |
| 3123 | return true; |
| 3124 | |
| 3125 | Result.assign(std::move(MDs)); |
| 3126 | return false; |
| 3127 | } |
| 3128 | |
Duncan P. N. Exon Smith | 2f09c46 | 2015-02-04 22:13:28 +0000 | [diff] [blame] | 3129 | } // end namespace llvm |
| 3130 | |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 3131 | template <class ParserTy> |
Duncan P. N. Exon Smith | 66ca92e | 2015-01-19 23:39:32 +0000 | [diff] [blame] | 3132 | bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) { |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 3133 | do { |
| 3134 | if (Lex.getKind() != lltok::LabelStr) |
| 3135 | return TokError("expected field label here"); |
| 3136 | |
| 3137 | if (parseField()) |
| 3138 | return true; |
| 3139 | } while (EatIfPresent(lltok::comma)); |
| 3140 | |
Duncan P. N. Exon Smith | 66ca92e | 2015-01-19 23:39:32 +0000 | [diff] [blame] | 3141 | return false; |
| 3142 | } |
| 3143 | |
| 3144 | template <class ParserTy> |
| 3145 | bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) { |
| 3146 | assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name"); |
| 3147 | Lex.Lex(); |
| 3148 | |
| 3149 | if (ParseToken(lltok::lparen, "expected '(' here")) |
| 3150 | return true; |
| 3151 | if (Lex.getKind() != lltok::rparen) |
| 3152 | if (ParseMDFieldsImplBody(parseField)) |
| 3153 | return true; |
| 3154 | |
Duncan P. N. Exon Smith | 13890af | 2015-01-19 23:32:36 +0000 | [diff] [blame] | 3155 | ClosingLoc = Lex.getLoc(); |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 3156 | return ParseToken(lltok::rparen, "expected ')' here"); |
| 3157 | } |
| 3158 | |
Duncan P. N. Exon Smith | a747728 | 2015-01-20 02:42:29 +0000 | [diff] [blame] | 3159 | template <class FieldTy> |
| 3160 | bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) { |
| 3161 | if (Result.Seen) |
| 3162 | return TokError("field '" + Name + "' cannot be specified more than once"); |
| 3163 | |
| 3164 | LocTy Loc = Lex.getLoc(); |
| 3165 | Lex.Lex(); |
| 3166 | return ParseMDField(Loc, Name, Result); |
| 3167 | } |
| 3168 | |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 3169 | bool LLParser::ParseSpecializedMDNode(MDNode *&N, bool IsDistinct) { |
| 3170 | assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name"); |
Duncan P. N. Exon Smith | ed458fa | 2015-02-10 01:08:16 +0000 | [diff] [blame] | 3171 | |
| 3172 | #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \ |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 3173 | if (Lex.getStrVal() == #CLASS) \ |
| 3174 | return Parse##CLASS(N, IsDistinct); |
Duncan P. N. Exon Smith | ed458fa | 2015-02-10 01:08:16 +0000 | [diff] [blame] | 3175 | #include "llvm/IR/Metadata.def" |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 3176 | |
| 3177 | return TokError("expected metadata type"); |
| 3178 | } |
| 3179 | |
Duncan P. N. Exon Smith | 2a6b5fc | 2015-01-19 23:44:41 +0000 | [diff] [blame] | 3180 | #define DECLARE_FIELD(NAME, TYPE, INIT) TYPE NAME INIT |
| 3181 | #define NOP_FIELD(NAME, TYPE, INIT) |
| 3182 | #define REQUIRE_FIELD(NAME, TYPE, INIT) \ |
| 3183 | if (!NAME.Seen) \ |
| 3184 | return Error(ClosingLoc, "missing required field '" #NAME "'"); |
| 3185 | #define PARSE_MD_FIELD(NAME, TYPE, DEFAULT) \ |
Duncan P. N. Exon Smith | a747728 | 2015-01-20 02:42:29 +0000 | [diff] [blame] | 3186 | if (Lex.getStrVal() == #NAME) \ |
| 3187 | return ParseMDField(#NAME, NAME); |
Duncan P. N. Exon Smith | 2a6b5fc | 2015-01-19 23:44:41 +0000 | [diff] [blame] | 3188 | #define PARSE_MD_FIELDS() \ |
| 3189 | VISIT_MD_FIELDS(DECLARE_FIELD, DECLARE_FIELD) \ |
| 3190 | do { \ |
| 3191 | LocTy ClosingLoc; \ |
| 3192 | if (ParseMDFieldsImpl([&]() -> bool { \ |
| 3193 | VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD) \ |
| 3194 | return TokError(Twine("invalid field '") + Lex.getStrVal() + "'"); \ |
| 3195 | }, ClosingLoc)) \ |
| 3196 | return true; \ |
| 3197 | VISIT_MD_FIELDS(NOP_FIELD, REQUIRE_FIELD) \ |
| 3198 | } while (false) |
Duncan P. N. Exon Smith | 4e4aa70 | 2015-02-03 21:54:14 +0000 | [diff] [blame] | 3199 | #define GET_OR_DISTINCT(CLASS, ARGS) \ |
| 3200 | (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS) |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 3201 | |
| 3202 | /// ParseMDLocationFields: |
| 3203 | /// ::= !MDLocation(line: 43, column: 8, scope: !5, inlinedAt: !6) |
| 3204 | bool LLParser::ParseMDLocation(MDNode *&Result, bool IsDistinct) { |
Duncan P. N. Exon Smith | 2a6b5fc | 2015-01-19 23:44:41 +0000 | [diff] [blame] | 3205 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
Duncan P. N. Exon Smith | 9c93dc6 | 2015-02-04 22:59:18 +0000 | [diff] [blame] | 3206 | OPTIONAL(line, LineField, ); \ |
| 3207 | OPTIONAL(column, ColumnField, ); \ |
Duncan P. N. Exon Smith | 2a6b5fc | 2015-01-19 23:44:41 +0000 | [diff] [blame] | 3208 | REQUIRED(scope, MDField, ); \ |
| 3209 | OPTIONAL(inlinedAt, MDField, ); |
| 3210 | PARSE_MD_FIELDS(); |
| 3211 | #undef VISIT_MD_FIELDS |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 3212 | |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 3213 | auto get = (IsDistinct ? MDLocation::getDistinct : MDLocation::get); |
| 3214 | Result = get(Context, line.Val, column.Val, scope.Val, inlinedAt.Val); |
| 3215 | return false; |
| 3216 | } |
Duncan P. N. Exon Smith | 4e4aa70 | 2015-02-03 21:54:14 +0000 | [diff] [blame] | 3217 | |
| 3218 | /// ParseGenericDebugNode: |
| 3219 | /// ::= !GenericDebugNode(tag: 15, header: "...", operands: {...}) |
| 3220 | bool LLParser::ParseGenericDebugNode(MDNode *&Result, bool IsDistinct) { |
| 3221 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
Duncan P. N. Exon Smith | 9748607 | 2015-02-03 21:56:01 +0000 | [diff] [blame] | 3222 | REQUIRED(tag, DwarfTagField, ); \ |
Duncan P. N. Exon Smith | 4e4aa70 | 2015-02-03 21:54:14 +0000 | [diff] [blame] | 3223 | OPTIONAL(header, MDStringField, ); \ |
| 3224 | OPTIONAL(operands, MDFieldList, ); |
| 3225 | PARSE_MD_FIELDS(); |
| 3226 | #undef VISIT_MD_FIELDS |
| 3227 | |
| 3228 | Result = GET_OR_DISTINCT(GenericDebugNode, |
| 3229 | (Context, tag.Val, header.Val, operands.Val)); |
| 3230 | return false; |
| 3231 | } |
Duncan P. N. Exon Smith | ed458fa | 2015-02-10 01:08:16 +0000 | [diff] [blame] | 3232 | |
Duncan P. N. Exon Smith | c7363f1 | 2015-02-13 01:10:38 +0000 | [diff] [blame] | 3233 | /// ParseMDSubrange: |
| 3234 | /// ::= !MDSubrange(count: 30, lowerBound: 2) |
Duncan P. N. Exon Smith | ed458fa | 2015-02-10 01:08:16 +0000 | [diff] [blame] | 3235 | bool LLParser::ParseMDSubrange(MDNode *&Result, bool IsDistinct) { |
Duncan P. N. Exon Smith | c7363f1 | 2015-02-13 01:10:38 +0000 | [diff] [blame] | 3236 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 3237 | REQUIRED(count, MDUnsignedField, (0, UINT64_MAX >> 1)); \ |
| 3238 | OPTIONAL(lowerBound, MDSignedField, ); |
| 3239 | PARSE_MD_FIELDS(); |
| 3240 | #undef VISIT_MD_FIELDS |
| 3241 | |
| 3242 | Result = GET_OR_DISTINCT(MDSubrange, (Context, count.Val, lowerBound.Val)); |
| 3243 | return false; |
Duncan P. N. Exon Smith | ed458fa | 2015-02-10 01:08:16 +0000 | [diff] [blame] | 3244 | } |
Duncan P. N. Exon Smith | c7363f1 | 2015-02-13 01:10:38 +0000 | [diff] [blame] | 3245 | |
Duncan P. N. Exon Smith | 8775476 | 2015-02-13 01:14:11 +0000 | [diff] [blame] | 3246 | /// ParseMDEnumerator: |
| 3247 | /// ::= !MDEnumerator(value: 30, name: "SomeKind") |
Duncan P. N. Exon Smith | ed458fa | 2015-02-10 01:08:16 +0000 | [diff] [blame] | 3248 | bool LLParser::ParseMDEnumerator(MDNode *&Result, bool IsDistinct) { |
Duncan P. N. Exon Smith | 8775476 | 2015-02-13 01:14:11 +0000 | [diff] [blame] | 3249 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 3250 | REQUIRED(value, MDSignedField, ); \ |
| 3251 | REQUIRED(name, MDStringField, ); |
| 3252 | PARSE_MD_FIELDS(); |
| 3253 | #undef VISIT_MD_FIELDS |
| 3254 | |
| 3255 | Result = GET_OR_DISTINCT(MDEnumerator, (Context, value.Val, name.Val)); |
| 3256 | return false; |
Duncan P. N. Exon Smith | ed458fa | 2015-02-10 01:08:16 +0000 | [diff] [blame] | 3257 | } |
Duncan P. N. Exon Smith | 8775476 | 2015-02-13 01:14:11 +0000 | [diff] [blame] | 3258 | |
Duncan P. N. Exon Smith | 09e03f3 | 2015-02-13 01:14:58 +0000 | [diff] [blame] | 3259 | /// ParseMDBasicType: |
| 3260 | /// ::= !MDBasicType(tag: DW_TAG_base_type, name: "int", size: 32, align: 32) |
Duncan P. N. Exon Smith | ed458fa | 2015-02-10 01:08:16 +0000 | [diff] [blame] | 3261 | bool LLParser::ParseMDBasicType(MDNode *&Result, bool IsDistinct) { |
Duncan P. N. Exon Smith | 09e03f3 | 2015-02-13 01:14:58 +0000 | [diff] [blame] | 3262 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 3263 | REQUIRED(tag, DwarfTagField, ); \ |
| 3264 | OPTIONAL(name, MDStringField, ); \ |
| 3265 | OPTIONAL(size, MDUnsignedField, (0, UINT32_MAX)); \ |
| 3266 | OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \ |
Duncan P. N. Exon Smith | cd6636c | 2015-02-13 01:17:35 +0000 | [diff] [blame] | 3267 | OPTIONAL(encoding, DwarfAttEncodingField, ); |
Duncan P. N. Exon Smith | 09e03f3 | 2015-02-13 01:14:58 +0000 | [diff] [blame] | 3268 | PARSE_MD_FIELDS(); |
| 3269 | #undef VISIT_MD_FIELDS |
| 3270 | |
| 3271 | Result = GET_OR_DISTINCT(MDBasicType, (Context, tag.Val, name.Val, size.Val, |
| 3272 | align.Val, encoding.Val)); |
| 3273 | return false; |
Duncan P. N. Exon Smith | ed458fa | 2015-02-10 01:08:16 +0000 | [diff] [blame] | 3274 | } |
Duncan P. N. Exon Smith | 09e03f3 | 2015-02-13 01:14:58 +0000 | [diff] [blame] | 3275 | |
Duncan P. N. Exon Smith | 171d077 | 2015-02-13 01:20:38 +0000 | [diff] [blame] | 3276 | /// ParseMDDerivedType: |
| 3277 | /// ::= !MDDerivedType(tag: DW_TAG_pointer_type, name: "int", file: !0, |
| 3278 | /// line: 7, scope: !1, baseType: !2, size: 32, |
| 3279 | /// align: 32, offset: 0, flags: 0, extraData: !3) |
Duncan P. N. Exon Smith | ed458fa | 2015-02-10 01:08:16 +0000 | [diff] [blame] | 3280 | bool LLParser::ParseMDDerivedType(MDNode *&Result, bool IsDistinct) { |
Duncan P. N. Exon Smith | 171d077 | 2015-02-13 01:20:38 +0000 | [diff] [blame] | 3281 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 3282 | REQUIRED(tag, DwarfTagField, ); \ |
| 3283 | OPTIONAL(name, MDStringField, ); \ |
| 3284 | OPTIONAL(file, MDField, ); \ |
| 3285 | OPTIONAL(line, LineField, ); \ |
| 3286 | OPTIONAL(scope, MDField, ); \ |
| 3287 | REQUIRED(baseType, MDField, ); \ |
| 3288 | OPTIONAL(size, MDUnsignedField, (0, UINT32_MAX)); \ |
| 3289 | OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \ |
| 3290 | OPTIONAL(offset, MDUnsignedField, (0, UINT32_MAX)); \ |
| 3291 | OPTIONAL(flags, MDUnsignedField, (0, UINT32_MAX)); \ |
| 3292 | OPTIONAL(extraData, MDField, ); |
| 3293 | PARSE_MD_FIELDS(); |
| 3294 | #undef VISIT_MD_FIELDS |
| 3295 | |
| 3296 | Result = GET_OR_DISTINCT(MDDerivedType, |
| 3297 | (Context, tag.Val, name.Val, file.Val, line.Val, |
| 3298 | scope.Val, baseType.Val, size.Val, align.Val, |
| 3299 | offset.Val, flags.Val, extraData.Val)); |
| 3300 | return false; |
Duncan P. N. Exon Smith | ed458fa | 2015-02-10 01:08:16 +0000 | [diff] [blame] | 3301 | } |
Duncan P. N. Exon Smith | 171d077 | 2015-02-13 01:20:38 +0000 | [diff] [blame] | 3302 | |
Duncan P. N. Exon Smith | ed458fa | 2015-02-10 01:08:16 +0000 | [diff] [blame] | 3303 | bool LLParser::ParseMDCompositeType(MDNode *&Result, bool IsDistinct) { |
Duncan P. N. Exon Smith | 171d077 | 2015-02-13 01:20:38 +0000 | [diff] [blame] | 3304 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 3305 | REQUIRED(tag, DwarfTagField, ); \ |
| 3306 | OPTIONAL(name, MDStringField, ); \ |
| 3307 | OPTIONAL(file, MDField, ); \ |
| 3308 | OPTIONAL(line, LineField, ); \ |
| 3309 | OPTIONAL(scope, MDField, ); \ |
| 3310 | OPTIONAL(baseType, MDField, ); \ |
| 3311 | OPTIONAL(size, MDUnsignedField, (0, UINT32_MAX)); \ |
| 3312 | OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \ |
| 3313 | OPTIONAL(offset, MDUnsignedField, (0, UINT32_MAX)); \ |
| 3314 | OPTIONAL(flags, MDUnsignedField, (0, UINT32_MAX)); \ |
| 3315 | OPTIONAL(elements, MDField, ); \ |
Duncan P. N. Exon Smith | aece2dc | 2015-02-13 01:21:25 +0000 | [diff] [blame] | 3316 | OPTIONAL(runtimeLang, DwarfLangField, ); \ |
Duncan P. N. Exon Smith | 171d077 | 2015-02-13 01:20:38 +0000 | [diff] [blame] | 3317 | OPTIONAL(vtableHolder, MDField, ); \ |
| 3318 | OPTIONAL(templateParams, MDField, ); \ |
| 3319 | OPTIONAL(identifier, MDStringField, ); |
| 3320 | PARSE_MD_FIELDS(); |
| 3321 | #undef VISIT_MD_FIELDS |
| 3322 | |
| 3323 | Result = GET_OR_DISTINCT( |
| 3324 | MDCompositeType, |
| 3325 | (Context, tag.Val, name.Val, file.Val, line.Val, scope.Val, baseType.Val, |
| 3326 | size.Val, align.Val, offset.Val, flags.Val, elements.Val, |
| 3327 | runtimeLang.Val, vtableHolder.Val, templateParams.Val, identifier.Val)); |
| 3328 | return false; |
Duncan P. N. Exon Smith | ed458fa | 2015-02-10 01:08:16 +0000 | [diff] [blame] | 3329 | } |
Duncan P. N. Exon Smith | 171d077 | 2015-02-13 01:20:38 +0000 | [diff] [blame] | 3330 | |
Duncan P. N. Exon Smith | ed458fa | 2015-02-10 01:08:16 +0000 | [diff] [blame] | 3331 | bool LLParser::ParseMDSubroutineType(MDNode *&Result, bool IsDistinct) { |
Duncan P. N. Exon Smith | 54e2bc6 | 2015-02-13 01:22:59 +0000 | [diff] [blame^] | 3332 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 3333 | OPTIONAL(flags, MDUnsignedField, (0, UINT32_MAX)); \ |
| 3334 | REQUIRED(types, MDField, ); |
| 3335 | PARSE_MD_FIELDS(); |
| 3336 | #undef VISIT_MD_FIELDS |
| 3337 | |
| 3338 | Result = GET_OR_DISTINCT(MDSubroutineType, (Context, flags.Val, types.Val)); |
| 3339 | return false; |
Duncan P. N. Exon Smith | ed458fa | 2015-02-10 01:08:16 +0000 | [diff] [blame] | 3340 | } |
Duncan P. N. Exon Smith | f14b9c7 | 2015-02-13 01:19:14 +0000 | [diff] [blame] | 3341 | |
| 3342 | /// ParseMDFileType: |
| 3343 | /// ::= !MDFileType(filename: "path/to/file", directory: "/path/to/dir") |
Duncan P. N. Exon Smith | ed458fa | 2015-02-10 01:08:16 +0000 | [diff] [blame] | 3344 | bool LLParser::ParseMDFile(MDNode *&Result, bool IsDistinct) { |
Duncan P. N. Exon Smith | f14b9c7 | 2015-02-13 01:19:14 +0000 | [diff] [blame] | 3345 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 3346 | REQUIRED(filename, MDStringField, ); \ |
| 3347 | REQUIRED(directory, MDStringField, ); |
| 3348 | PARSE_MD_FIELDS(); |
| 3349 | #undef VISIT_MD_FIELDS |
| 3350 | |
| 3351 | Result = GET_OR_DISTINCT(MDFile, (Context, filename.Val, directory.Val)); |
| 3352 | return false; |
Duncan P. N. Exon Smith | ed458fa | 2015-02-10 01:08:16 +0000 | [diff] [blame] | 3353 | } |
Duncan P. N. Exon Smith | f14b9c7 | 2015-02-13 01:19:14 +0000 | [diff] [blame] | 3354 | |
Duncan P. N. Exon Smith | ed458fa | 2015-02-10 01:08:16 +0000 | [diff] [blame] | 3355 | bool LLParser::ParseMDCompileUnit(MDNode *&Result, bool IsDistinct) { |
| 3356 | return TokError("unimplemented parser"); |
| 3357 | } |
| 3358 | bool LLParser::ParseMDSubprogram(MDNode *&Result, bool IsDistinct) { |
| 3359 | return TokError("unimplemented parser"); |
| 3360 | } |
| 3361 | bool LLParser::ParseMDLexicalBlock(MDNode *&Result, bool IsDistinct) { |
| 3362 | return TokError("unimplemented parser"); |
| 3363 | } |
| 3364 | bool LLParser::ParseMDLexicalBlockFile(MDNode *&Result, bool IsDistinct) { |
| 3365 | return TokError("unimplemented parser"); |
| 3366 | } |
| 3367 | bool LLParser::ParseMDNamespace(MDNode *&Result, bool IsDistinct) { |
| 3368 | return TokError("unimplemented parser"); |
| 3369 | } |
| 3370 | bool LLParser::ParseMDTemplateTypeParameter(MDNode *&Result, bool IsDistinct) { |
| 3371 | return TokError("unimplemented parser"); |
| 3372 | } |
| 3373 | bool LLParser::ParseMDTemplateValueParameter(MDNode *&Result, bool IsDistinct) { |
| 3374 | return TokError("unimplemented parser"); |
| 3375 | } |
| 3376 | bool LLParser::ParseMDGlobalVariable(MDNode *&Result, bool IsDistinct) { |
| 3377 | return TokError("unimplemented parser"); |
| 3378 | } |
| 3379 | bool LLParser::ParseMDLocalVariable(MDNode *&Result, bool IsDistinct) { |
| 3380 | return TokError("unimplemented parser"); |
| 3381 | } |
| 3382 | bool LLParser::ParseMDExpression(MDNode *&Result, bool IsDistinct) { |
| 3383 | return TokError("unimplemented parser"); |
| 3384 | } |
| 3385 | bool LLParser::ParseMDObjCProperty(MDNode *&Result, bool IsDistinct) { |
| 3386 | return TokError("unimplemented parser"); |
| 3387 | } |
| 3388 | bool LLParser::ParseMDImportedEntity(MDNode *&Result, bool IsDistinct) { |
| 3389 | return TokError("unimplemented parser"); |
| 3390 | } |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 3391 | #undef PARSE_MD_FIELD |
Duncan P. N. Exon Smith | 2a6b5fc | 2015-01-19 23:44:41 +0000 | [diff] [blame] | 3392 | #undef NOP_FIELD |
| 3393 | #undef REQUIRE_FIELD |
| 3394 | #undef DECLARE_FIELD |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 3395 | |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 3396 | /// ParseMetadataAsValue |
| 3397 | /// ::= metadata i32 %local |
| 3398 | /// ::= metadata i32 @global |
| 3399 | /// ::= metadata i32 7 |
| 3400 | /// ::= metadata !0 |
| 3401 | /// ::= metadata !{...} |
| 3402 | /// ::= metadata !"string" |
| 3403 | bool LLParser::ParseMetadataAsValue(Value *&V, PerFunctionState &PFS) { |
| 3404 | // Note: the type 'metadata' has already been parsed. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 3405 | Metadata *MD; |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 3406 | if (ParseMetadata(MD, &PFS)) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 3407 | return true; |
| 3408 | |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 3409 | V = MetadataAsValue::get(Context, MD); |
| 3410 | return false; |
| 3411 | } |
| 3412 | |
| 3413 | /// ParseValueAsMetadata |
| 3414 | /// ::= i32 %local |
| 3415 | /// ::= i32 @global |
| 3416 | /// ::= i32 7 |
| 3417 | bool LLParser::ParseValueAsMetadata(Metadata *&MD, PerFunctionState *PFS) { |
| 3418 | Type *Ty; |
| 3419 | LocTy Loc; |
| 3420 | if (ParseType(Ty, "expected metadata operand", Loc)) |
| 3421 | return true; |
| 3422 | if (Ty->isMetadataTy()) |
| 3423 | return Error(Loc, "invalid metadata-value-metadata roundtrip"); |
| 3424 | |
| 3425 | Value *V; |
| 3426 | if (ParseValue(Ty, V, PFS)) |
| 3427 | return true; |
| 3428 | |
| 3429 | MD = ValueAsMetadata::get(V); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 3430 | return false; |
| 3431 | } |
| 3432 | |
| 3433 | /// ParseMetadata |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 3434 | /// ::= i32 %local |
| 3435 | /// ::= i32 @global |
| 3436 | /// ::= i32 7 |
Dan Gohman | 8939ba33 | 2010-07-14 18:26:50 +0000 | [diff] [blame] | 3437 | /// ::= !42 |
| 3438 | /// ::= !{...} |
| 3439 | /// ::= !"string" |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 3440 | /// ::= !MDLocation(...) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 3441 | bool LLParser::ParseMetadata(Metadata *&MD, PerFunctionState *PFS) { |
Duncan P. N. Exon Smith | 6a48483 | 2015-01-13 21:10:44 +0000 | [diff] [blame] | 3442 | if (Lex.getKind() == lltok::MetadataVar) { |
| 3443 | MDNode *N; |
| 3444 | if (ParseSpecializedMDNode(N)) |
| 3445 | return true; |
| 3446 | MD = N; |
| 3447 | return false; |
| 3448 | } |
| 3449 | |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 3450 | // ValueAsMetadata: |
| 3451 | // <type> <value> |
| 3452 | if (Lex.getKind() != lltok::exclaim) |
| 3453 | return ParseValueAsMetadata(MD, PFS); |
| 3454 | |
| 3455 | // '!'. |
| 3456 | assert(Lex.getKind() == lltok::exclaim && "Expected '!' here"); |
| 3457 | Lex.Lex(); |
Dan Gohman | 8939ba33 | 2010-07-14 18:26:50 +0000 | [diff] [blame] | 3458 | |
Duncan P. N. Exon Smith | 62a7919 | 2015-01-12 22:24:50 +0000 | [diff] [blame] | 3459 | // MDString: |
| 3460 | // ::= '!' STRINGCONSTANT |
| 3461 | if (Lex.getKind() == lltok::StringConstant) { |
| 3462 | MDString *S; |
| 3463 | if (ParseMDString(S)) |
| 3464 | return true; |
| 3465 | MD = S; |
| 3466 | return false; |
| 3467 | } |
| 3468 | |
Dan Gohman | 8939ba33 | 2010-07-14 18:26:50 +0000 | [diff] [blame] | 3469 | // MDNode: |
| 3470 | // !{ ... } |
Duncan P. N. Exon Smith | f825dae | 2015-01-12 22:26:48 +0000 | [diff] [blame] | 3471 | // !7 |
Duncan P. N. Exon Smith | 62a7919 | 2015-01-12 22:24:50 +0000 | [diff] [blame] | 3472 | MDNode *N; |
Duncan P. N. Exon Smith | f825dae | 2015-01-12 22:26:48 +0000 | [diff] [blame] | 3473 | if (ParseMDNodeTail(N)) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 3474 | return true; |
Duncan P. N. Exon Smith | 62a7919 | 2015-01-12 22:24:50 +0000 | [diff] [blame] | 3475 | MD = N; |
Dan Gohman | 8939ba33 | 2010-07-14 18:26:50 +0000 | [diff] [blame] | 3476 | return false; |
| 3477 | } |
| 3478 | |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 3479 | |
| 3480 | //===----------------------------------------------------------------------===// |
| 3481 | // Function Parsing. |
| 3482 | //===----------------------------------------------------------------------===// |
| 3483 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3484 | bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V, |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 3485 | PerFunctionState *PFS) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3486 | if (Ty->isFunctionTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3487 | return Error(ID.Loc, "functions are not values, refer to them as pointers"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3488 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3489 | switch (ID.Kind) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3490 | case ValID::t_LocalID: |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 3491 | if (!PFS) return Error(ID.Loc, "invalid use of function-local name"); |
| 3492 | V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3493 | return V == nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3494 | case ValID::t_LocalName: |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 3495 | if (!PFS) return Error(ID.Loc, "invalid use of function-local name"); |
| 3496 | V = PFS->GetVal(ID.StrVal, Ty, ID.Loc); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3497 | return V == nullptr; |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 3498 | case ValID::t_InlineAsm: { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3499 | PointerType *PTy = dyn_cast<PointerType>(Ty); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3500 | FunctionType *FTy = |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3501 | PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : nullptr; |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 3502 | if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2)) |
| 3503 | return Error(ID.Loc, "invalid type for inline asm constraint string"); |
Chad Rosier | f42fad6 | 2012-09-05 00:08:17 +0000 | [diff] [blame] | 3504 | V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1, |
Chad Rosier | d8c7610 | 2012-09-05 19:00:49 +0000 | [diff] [blame] | 3505 | (ID.UIntVal>>1)&1, (InlineAsm::AsmDialect(ID.UIntVal>>2))); |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 3506 | return false; |
| 3507 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3508 | case ValID::t_GlobalName: |
| 3509 | V = GetGlobalVal(ID.StrVal, Ty, ID.Loc); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3510 | return V == nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3511 | case ValID::t_GlobalID: |
| 3512 | V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3513 | return V == nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3514 | case ValID::t_APSInt: |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3515 | if (!Ty->isIntegerTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3516 | return Error(ID.Loc, "integer constant must have integer type"); |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 3517 | ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits()); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 3518 | V = ConstantInt::get(Context, ID.APSIntVal); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3519 | return false; |
| 3520 | case ValID::t_APFloat: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3521 | if (!Ty->isFloatingPointTy() || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3522 | !ConstantFP::isValueValidForType(Ty, ID.APFloatVal)) |
| 3523 | return Error(ID.Loc, "floating point constant invalid for type"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3524 | |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 3525 | // The lexer has no type info, so builds all half, float, and double FP |
| 3526 | // constants as double. Fix this here. Long double does not need this. |
| 3527 | if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3528 | bool Ignored; |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 3529 | if (Ty->isHalfTy()) |
| 3530 | ID.APFloatVal.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven, |
| 3531 | &Ignored); |
| 3532 | else if (Ty->isFloatTy()) |
| 3533 | ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, |
| 3534 | &Ignored); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3535 | } |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 3536 | V = ConstantFP::get(Context, ID.APFloatVal); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3537 | |
Chris Lattner | 8f57d29e | 2009-01-05 18:24:23 +0000 | [diff] [blame] | 3538 | if (V->getType() != Ty) |
| 3539 | return Error(ID.Loc, "floating point constant does not have type '" + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 3540 | getTypeString(Ty) + "'"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3541 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3542 | return false; |
| 3543 | case ValID::t_Null: |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3544 | if (!Ty->isPointerTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3545 | return Error(ID.Loc, "null must be a pointer type"); |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 3546 | V = ConstantPointerNull::get(cast<PointerType>(Ty)); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3547 | return false; |
| 3548 | case ValID::t_Undef: |
Chris Lattner | ffa0778 | 2009-01-05 08:13:38 +0000 | [diff] [blame] | 3549 | // FIXME: LabelTy should not be a first-class type. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3550 | if (!Ty->isFirstClassType() || Ty->isLabelTy()) |
Chris Lattner | ffa0778 | 2009-01-05 08:13:38 +0000 | [diff] [blame] | 3551 | return Error(ID.Loc, "invalid type for undef constant"); |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 3552 | V = UndefValue::get(Ty); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3553 | return false; |
Chris Lattner | 998fa0a | 2009-01-05 07:52:51 +0000 | [diff] [blame] | 3554 | case ValID::t_EmptyArray: |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3555 | if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0) |
Chris Lattner | 998fa0a | 2009-01-05 07:52:51 +0000 | [diff] [blame] | 3556 | return Error(ID.Loc, "invalid empty array initializer"); |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 3557 | V = UndefValue::get(Ty); |
Chris Lattner | 998fa0a | 2009-01-05 07:52:51 +0000 | [diff] [blame] | 3558 | return false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3559 | case ValID::t_Zero: |
Chris Lattner | ffa0778 | 2009-01-05 08:13:38 +0000 | [diff] [blame] | 3560 | // FIXME: LabelTy should not be a first-class type. |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 3561 | if (!Ty->isFirstClassType() || Ty->isLabelTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3562 | return Error(ID.Loc, "invalid type for null constant"); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 3563 | V = Constant::getNullValue(Ty); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3564 | return false; |
| 3565 | case ValID::t_Constant: |
Chris Lattner | 13ee795 | 2010-08-28 04:09:24 +0000 | [diff] [blame] | 3566 | if (ID.ConstantVal->getType() != Ty) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3567 | return Error(ID.Loc, "constant expression type mismatch"); |
Chris Lattner | 392be58 | 2010-02-12 20:49:41 +0000 | [diff] [blame] | 3568 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3569 | V = ID.ConstantVal; |
| 3570 | return false; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3571 | case ValID::t_ConstantStruct: |
| 3572 | case ValID::t_PackedConstantStruct: |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3573 | if (StructType *ST = dyn_cast<StructType>(Ty)) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3574 | if (ST->getNumElements() != ID.UIntVal) |
| 3575 | return Error(ID.Loc, |
| 3576 | "initializer with struct type has wrong # elements"); |
| 3577 | if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct)) |
| 3578 | return Error(ID.Loc, "packed'ness of initializer and type don't match"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3579 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3580 | // Verify that the elements are compatible with the structtype. |
| 3581 | for (unsigned i = 0, e = ID.UIntVal; i != e; ++i) |
| 3582 | if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i)) |
| 3583 | return Error(ID.Loc, "element " + Twine(i) + |
| 3584 | " of struct initializer doesn't match struct element type"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3585 | |
Frits van Bommel | 717d7ed | 2011-07-18 12:00:32 +0000 | [diff] [blame] | 3586 | V = ConstantStruct::get(ST, makeArrayRef(ID.ConstantStructElts, |
| 3587 | ID.UIntVal)); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3588 | } else |
| 3589 | return Error(ID.Loc, "constant expression type mismatch"); |
| 3590 | return false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3591 | } |
Chandler Carruth | f3e8502 | 2012-01-10 18:08:01 +0000 | [diff] [blame] | 3592 | llvm_unreachable("Invalid ValID"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3593 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3594 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3595 | bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3596 | V = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3597 | ValID ID; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3598 | return ParseValID(ID, PFS) || |
| 3599 | ConvertValIDToValue(Ty, ID, V, PFS); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3600 | } |
| 3601 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3602 | bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3603 | Type *Ty = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3604 | return ParseType(Ty) || |
| 3605 | ParseValue(Ty, V, PFS); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3606 | } |
| 3607 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3608 | bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc, |
| 3609 | PerFunctionState &PFS) { |
| 3610 | Value *V; |
| 3611 | Loc = Lex.getLoc(); |
| 3612 | if (ParseTypeAndValue(V, PFS)) return true; |
| 3613 | if (!isa<BasicBlock>(V)) |
| 3614 | return Error(Loc, "expected a basic block"); |
| 3615 | BB = cast<BasicBlock>(V); |
| 3616 | return false; |
| 3617 | } |
| 3618 | |
| 3619 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3620 | /// FunctionHeader |
| 3621 | /// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs |
Rafael Espindola | 45e6c19 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 3622 | /// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 3623 | /// OptionalAlign OptGC OptionalPrefix OptionalPrologue |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3624 | bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) { |
| 3625 | // Parse the linkage. |
| 3626 | LocTy LinkageLoc = Lex.getLoc(); |
| 3627 | unsigned Linkage; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3628 | |
Kostya Serebryany | a5054ad | 2012-01-20 17:56:17 +0000 | [diff] [blame] | 3629 | unsigned Visibility; |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 3630 | unsigned DLLStorageClass; |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 3631 | AttrBuilder RetAttrs; |
Alexey Samsonov | 17a9cff | 2014-09-10 18:00:17 +0000 | [diff] [blame] | 3632 | unsigned CC; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3633 | Type *RetType = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3634 | LocTy RetTypeLoc = Lex.getLoc(); |
| 3635 | if (ParseOptionalLinkage(Linkage) || |
| 3636 | ParseOptionalVisibility(Visibility) || |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 3637 | ParseOptionalDLLStorageClass(DLLStorageClass) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3638 | ParseOptionalCallingConv(CC) || |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 3639 | ParseOptionalReturnAttrs(RetAttrs) || |
Chris Lattner | f880ca2 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 3640 | ParseType(RetType, RetTypeLoc, true /*void allowed*/)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3641 | return true; |
| 3642 | |
| 3643 | // Verify that the linkage is ok. |
| 3644 | switch ((GlobalValue::LinkageTypes)Linkage) { |
| 3645 | case GlobalValue::ExternalLinkage: |
| 3646 | break; // always ok. |
Duncan Sands | e288105 | 2009-03-11 08:08:06 +0000 | [diff] [blame] | 3647 | case GlobalValue::ExternalWeakLinkage: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3648 | if (isDefine) |
| 3649 | return Error(LinkageLoc, "invalid linkage for function definition"); |
| 3650 | break; |
Rafael Espindola | 6de96a1 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 3651 | case GlobalValue::PrivateLinkage: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3652 | case GlobalValue::InternalLinkage: |
Nick Lewycky | 8019af6 | 2009-04-13 07:02:02 +0000 | [diff] [blame] | 3653 | case GlobalValue::AvailableExternallyLinkage: |
Duncan Sands | 12da8ce | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 3654 | case GlobalValue::LinkOnceAnyLinkage: |
| 3655 | case GlobalValue::LinkOnceODRLinkage: |
| 3656 | case GlobalValue::WeakAnyLinkage: |
| 3657 | case GlobalValue::WeakODRLinkage: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3658 | if (!isDefine) |
| 3659 | return Error(LinkageLoc, "invalid linkage for function declaration"); |
| 3660 | break; |
| 3661 | case GlobalValue::AppendingLinkage: |
Duncan Sands | 4581beb | 2009-03-11 20:14:15 +0000 | [diff] [blame] | 3662 | case GlobalValue::CommonLinkage: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3663 | return Error(LinkageLoc, "invalid function linkage type"); |
| 3664 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3665 | |
Duncan P. N. Exon Smith | b80de10 | 2014-05-07 22:57:20 +0000 | [diff] [blame] | 3666 | if (!isValidVisibilityForLinkage(Visibility, Linkage)) |
| 3667 | return Error(LinkageLoc, |
| 3668 | "symbol with local linkage must have default visibility"); |
| 3669 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3670 | if (!FunctionType::isValidReturnType(RetType)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3671 | return Error(RetTypeLoc, "invalid function return type"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3672 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3673 | LocTy NameLoc = Lex.getLoc(); |
Chris Lattner | 778c62c | 2009-02-18 21:48:13 +0000 | [diff] [blame] | 3674 | |
| 3675 | std::string FunctionName; |
| 3676 | if (Lex.getKind() == lltok::GlobalVar) { |
| 3677 | FunctionName = Lex.getStrVal(); |
| 3678 | } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok. |
| 3679 | unsigned NameID = Lex.getUIntVal(); |
| 3680 | |
| 3681 | if (NameID != NumberedVals.size()) |
| 3682 | return TokError("function expected to be numbered '%" + |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 3683 | Twine(NumberedVals.size()) + "'"); |
Chris Lattner | 778c62c | 2009-02-18 21:48:13 +0000 | [diff] [blame] | 3684 | } else { |
| 3685 | return TokError("expected function name"); |
| 3686 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3687 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 3688 | Lex.Lex(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3689 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 3690 | if (Lex.getKind() != lltok::lparen) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3691 | return TokError("expected '(' in function argument list"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3692 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3693 | SmallVector<ArgInfo, 8> ArgList; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3694 | bool isVarArg; |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 3695 | AttrBuilder FuncAttrs; |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 3696 | std::vector<unsigned> FwdRefAttrGrps; |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 3697 | LocTy BuiltinLoc; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3698 | std::string Section; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3699 | unsigned Alignment; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 3700 | std::string GC; |
Rafael Espindola | 563eb4b | 2011-01-25 19:09:56 +0000 | [diff] [blame] | 3701 | bool UnnamedAddr; |
| 3702 | LocTy UnnamedAddrLoc; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3703 | Constant *Prefix = nullptr; |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 3704 | Constant *Prologue = nullptr; |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 3705 | Comdat *C; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 3706 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3707 | if (ParseArgumentList(ArgList, isVarArg) || |
Rafael Espindola | 563eb4b | 2011-01-25 19:09:56 +0000 | [diff] [blame] | 3708 | ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr, |
| 3709 | &UnnamedAddrLoc) || |
Bill Wendling | 09bd1f7 | 2013-02-22 00:12:35 +0000 | [diff] [blame] | 3710 | ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false, |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 3711 | BuiltinLoc) || |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 3712 | (EatIfPresent(lltok::kw_section) && |
| 3713 | ParseStringConstant(Section)) || |
Rafael Espindola | 83a362c | 2015-01-06 22:55:16 +0000 | [diff] [blame] | 3714 | parseOptionalComdat(FunctionName, C) || |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 3715 | ParseOptionalAlignment(Alignment) || |
| 3716 | (EatIfPresent(lltok::kw_gc) && |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 3717 | ParseStringConstant(GC)) || |
| 3718 | (EatIfPresent(lltok::kw_prefix) && |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 3719 | ParseGlobalTypeAndValue(Prefix)) || |
| 3720 | (EatIfPresent(lltok::kw_prologue) && |
| 3721 | ParseGlobalTypeAndValue(Prologue))) |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 3722 | return true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3723 | |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 3724 | if (FuncAttrs.contains(Attribute::Builtin)) |
| 3725 | return Error(BuiltinLoc, "'builtin' attribute not valid on function"); |
Bill Wendling | 09bd1f7 | 2013-02-22 00:12:35 +0000 | [diff] [blame] | 3726 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3727 | // If the alignment was parsed as an attribute, move to the alignment field. |
Bill Wendling | c6daefa | 2012-10-08 23:27:46 +0000 | [diff] [blame] | 3728 | if (FuncAttrs.hasAlignmentAttr()) { |
Bill Wendling | 9be7759 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 3729 | Alignment = FuncAttrs.getAlignment(); |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 3730 | FuncAttrs.removeAttribute(Attribute::Alignment); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3731 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3732 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3733 | // Okay, if we got here, the function is syntactically valid. Convert types |
| 3734 | // and do semantic checks. |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 3735 | std::vector<Type*> ParamTypeList; |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 3736 | SmallVector<AttributeSet, 8> Attrs; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3737 | |
Bill Wendling | 3bef2dd | 2012-09-19 23:54:18 +0000 | [diff] [blame] | 3738 | if (RetAttrs.hasAttributes()) |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 3739 | Attrs.push_back(AttributeSet::get(RetType->getContext(), |
| 3740 | AttributeSet::ReturnIndex, |
| 3741 | RetAttrs)); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3742 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3743 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3744 | ParamTypeList.push_back(ArgList[i].Ty); |
Bill Wendling | fe0021a | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 3745 | if (ArgList[i].Attrs.hasAttributes(i + 1)) { |
| 3746 | AttrBuilder B(ArgList[i].Attrs, i + 1); |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 3747 | Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B)); |
| 3748 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3749 | } |
| 3750 | |
Bill Wendling | 3bef2dd | 2012-09-19 23:54:18 +0000 | [diff] [blame] | 3751 | if (FuncAttrs.hasAttributes()) |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 3752 | Attrs.push_back(AttributeSet::get(RetType->getContext(), |
| 3753 | AttributeSet::FunctionIndex, |
| 3754 | FuncAttrs)); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3755 | |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 3756 | AttributeSet PAL = AttributeSet::get(Context, Attrs); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3757 | |
Bill Wendling | 749a43d | 2012-12-30 13:50:49 +0000 | [diff] [blame] | 3758 | if (PAL.hasAttribute(1, Attribute::StructRet) && !RetType->isVoidTy()) |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3759 | return Error(RetTypeLoc, "functions with 'sret' argument must return void"); |
| 3760 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3761 | FunctionType *FT = |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 3762 | FunctionType::get(RetType, ParamTypeList, isVarArg); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3763 | PointerType *PFT = PointerType::getUnqual(FT); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3764 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3765 | Fn = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3766 | if (!FunctionName.empty()) { |
| 3767 | // If this was a definition of a forward reference, remove the definition |
| 3768 | // from the forward reference table and fill in the forward ref. |
| 3769 | std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI = |
| 3770 | ForwardRefVals.find(FunctionName); |
| 3771 | if (FRVI != ForwardRefVals.end()) { |
| 3772 | Fn = M->getFunction(FunctionName); |
Nick Lewycky | 686d7cb | 2012-10-11 00:38:25 +0000 | [diff] [blame] | 3773 | if (!Fn) |
| 3774 | return Error(FRVI->second.second, "invalid forward reference to " |
| 3775 | "function as global value!"); |
Chris Lattner | c239eb7 | 2010-04-20 04:49:11 +0000 | [diff] [blame] | 3776 | if (Fn->getType() != PFT) |
| 3777 | return Error(FRVI->second.second, "invalid forward reference to " |
| 3778 | "function '" + FunctionName + "' with wrong type!"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3779 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3780 | ForwardRefVals.erase(FRVI); |
| 3781 | } else if ((Fn = M->getFunction(FunctionName))) { |
Chris Lattner | 5756c16 | 2011-06-17 07:06:44 +0000 | [diff] [blame] | 3782 | // Reject redefinitions. |
| 3783 | return Error(NameLoc, "invalid redefinition of function '" + |
| 3784 | FunctionName + "'"); |
Chris Lattner | e38317f | 2009-10-25 23:22:50 +0000 | [diff] [blame] | 3785 | } else if (M->getNamedValue(FunctionName)) { |
| 3786 | return Error(NameLoc, "redefinition of function '@" + FunctionName + "'"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3787 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3788 | |
Dan Gohman | 399d6ae | 2009-08-29 23:37:49 +0000 | [diff] [blame] | 3789 | } else { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3790 | // If this is a definition of a forward referenced function, make sure the |
| 3791 | // types agree. |
| 3792 | std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I |
| 3793 | = ForwardRefValIDs.find(NumberedVals.size()); |
| 3794 | if (I != ForwardRefValIDs.end()) { |
| 3795 | Fn = cast<Function>(I->second.first); |
| 3796 | if (Fn->getType() != PFT) |
| 3797 | return Error(NameLoc, "type of definition and forward reference of '@" + |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 3798 | Twine(NumberedVals.size()) + "' disagree"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3799 | ForwardRefValIDs.erase(I); |
| 3800 | } |
| 3801 | } |
| 3802 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3803 | if (!Fn) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3804 | Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M); |
| 3805 | else // Move the forward-reference to the correct spot in the module. |
| 3806 | M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn); |
| 3807 | |
| 3808 | if (FunctionName.empty()) |
| 3809 | NumberedVals.push_back(Fn); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3810 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3811 | Fn->setLinkage((GlobalValue::LinkageTypes)Linkage); |
| 3812 | Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility); |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 3813 | Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3814 | Fn->setCallingConv(CC); |
| 3815 | Fn->setAttributes(PAL); |
Rafael Espindola | 45e6c19 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 3816 | Fn->setUnnamedAddr(UnnamedAddr); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3817 | Fn->setAlignment(Alignment); |
| 3818 | Fn->setSection(Section); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 3819 | Fn->setComdat(C); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3820 | if (!GC.empty()) Fn->setGC(GC.c_str()); |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 3821 | Fn->setPrefixData(Prefix); |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 3822 | Fn->setPrologueData(Prologue); |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 3823 | ForwardRefAttrGroups[Fn] = FwdRefAttrGrps; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3824 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3825 | // Add all of the arguments we parsed to the function. |
| 3826 | Function::arg_iterator ArgIt = Fn->arg_begin(); |
| 3827 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) { |
| 3828 | // If the argument has a name, insert it into the argument symbol table. |
| 3829 | if (ArgList[i].Name.empty()) continue; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3830 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3831 | // Set the name, if it conflicted, it will be auto-renamed. |
| 3832 | ArgIt->setName(ArgList[i].Name); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3833 | |
Benjamin Kramer | 1dc34b4 | 2010-10-16 11:28:23 +0000 | [diff] [blame] | 3834 | if (ArgIt->getName() != ArgList[i].Name) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3835 | return Error(ArgList[i].Loc, "redefinition of argument '%" + |
| 3836 | ArgList[i].Name + "'"); |
| 3837 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3838 | |
Duncan P. N. Exon Smith | 1716990 | 2014-08-19 00:13:19 +0000 | [diff] [blame] | 3839 | if (isDefine) |
| 3840 | return false; |
| 3841 | |
Robin Morisset | 039781e | 2014-08-29 21:53:01 +0000 | [diff] [blame] | 3842 | // Check the declaration has no block address forward references. |
Duncan P. N. Exon Smith | 1716990 | 2014-08-19 00:13:19 +0000 | [diff] [blame] | 3843 | ValID ID; |
| 3844 | if (FunctionName.empty()) { |
| 3845 | ID.Kind = ValID::t_GlobalID; |
| 3846 | ID.UIntVal = NumberedVals.size() - 1; |
| 3847 | } else { |
| 3848 | ID.Kind = ValID::t_GlobalName; |
| 3849 | ID.StrVal = FunctionName; |
| 3850 | } |
| 3851 | auto Blocks = ForwardRefBlockAddresses.find(ID); |
| 3852 | if (Blocks != ForwardRefBlockAddresses.end()) |
| 3853 | return Error(Blocks->first.Loc, |
| 3854 | "cannot take blockaddress inside a declaration"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3855 | return false; |
| 3856 | } |
| 3857 | |
Duncan P. N. Exon Smith | 1716990 | 2014-08-19 00:13:19 +0000 | [diff] [blame] | 3858 | bool LLParser::PerFunctionState::resolveForwardRefBlockAddresses() { |
| 3859 | ValID ID; |
| 3860 | if (FunctionNumber == -1) { |
| 3861 | ID.Kind = ValID::t_GlobalName; |
| 3862 | ID.StrVal = F.getName(); |
| 3863 | } else { |
| 3864 | ID.Kind = ValID::t_GlobalID; |
| 3865 | ID.UIntVal = FunctionNumber; |
| 3866 | } |
| 3867 | |
| 3868 | auto Blocks = P.ForwardRefBlockAddresses.find(ID); |
| 3869 | if (Blocks == P.ForwardRefBlockAddresses.end()) |
| 3870 | return false; |
| 3871 | |
| 3872 | for (const auto &I : Blocks->second) { |
| 3873 | const ValID &BBID = I.first; |
| 3874 | GlobalValue *GV = I.second; |
| 3875 | |
| 3876 | assert((BBID.Kind == ValID::t_LocalID || BBID.Kind == ValID::t_LocalName) && |
| 3877 | "Expected local id or name"); |
| 3878 | BasicBlock *BB; |
| 3879 | if (BBID.Kind == ValID::t_LocalName) |
| 3880 | BB = GetBB(BBID.StrVal, BBID.Loc); |
| 3881 | else |
| 3882 | BB = GetBB(BBID.UIntVal, BBID.Loc); |
| 3883 | if (!BB) |
| 3884 | return P.Error(BBID.Loc, "referenced value is not a basic block"); |
| 3885 | |
| 3886 | GV->replaceAllUsesWith(BlockAddress::get(&F, BB)); |
| 3887 | GV->eraseFromParent(); |
| 3888 | } |
| 3889 | |
| 3890 | P.ForwardRefBlockAddresses.erase(Blocks); |
| 3891 | return false; |
| 3892 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3893 | |
| 3894 | /// ParseFunctionBody |
Duncan P. N. Exon Smith | 0a448fb | 2014-08-19 21:30:15 +0000 | [diff] [blame] | 3895 | /// ::= '{' BasicBlock+ UseListOrderDirective* '}' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3896 | bool LLParser::ParseFunctionBody(Function &Fn) { |
Chris Lattner | 4649a73 | 2011-06-17 06:42:57 +0000 | [diff] [blame] | 3897 | if (Lex.getKind() != lltok::lbrace) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3898 | return TokError("expected '{' in function body"); |
| 3899 | Lex.Lex(); // eat the {. |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3900 | |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 3901 | int FunctionNumber = -1; |
| 3902 | if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3903 | |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 3904 | PerFunctionState PFS(*this, Fn, FunctionNumber); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3905 | |
Duncan P. N. Exon Smith | 1716990 | 2014-08-19 00:13:19 +0000 | [diff] [blame] | 3906 | // Resolve block addresses and allow basic blocks to be forward-declared |
| 3907 | // within this function. |
| 3908 | if (PFS.resolveForwardRefBlockAddresses()) |
| 3909 | return true; |
| 3910 | SaveAndRestore<PerFunctionState *> ScopeExit(BlockAddressPFS, &PFS); |
| 3911 | |
Chris Lattner | bbddd96 | 2010-01-09 19:20:07 +0000 | [diff] [blame] | 3912 | // We need at least one basic block. |
Duncan P. N. Exon Smith | 0a448fb | 2014-08-19 21:30:15 +0000 | [diff] [blame] | 3913 | if (Lex.getKind() == lltok::rbrace || Lex.getKind() == lltok::kw_uselistorder) |
Chris Lattner | bbddd96 | 2010-01-09 19:20:07 +0000 | [diff] [blame] | 3914 | return TokError("function body requires at least one basic block"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3915 | |
Duncan P. N. Exon Smith | 0a448fb | 2014-08-19 21:30:15 +0000 | [diff] [blame] | 3916 | while (Lex.getKind() != lltok::rbrace && |
| 3917 | Lex.getKind() != lltok::kw_uselistorder) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3918 | if (ParseBasicBlock(PFS)) return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3919 | |
Duncan P. N. Exon Smith | 0a448fb | 2014-08-19 21:30:15 +0000 | [diff] [blame] | 3920 | while (Lex.getKind() != lltok::rbrace) |
| 3921 | if (ParseUseListOrder(&PFS)) |
| 3922 | return true; |
| 3923 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3924 | // Eat the }. |
| 3925 | Lex.Lex(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3926 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3927 | // Verify function is ok. |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 3928 | return PFS.FinishFunction(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3929 | } |
| 3930 | |
| 3931 | /// ParseBasicBlock |
| 3932 | /// ::= LabelStr? Instruction* |
| 3933 | bool LLParser::ParseBasicBlock(PerFunctionState &PFS) { |
| 3934 | // If this basic block starts out with a name, remember it. |
| 3935 | std::string Name; |
| 3936 | LocTy NameLoc = Lex.getLoc(); |
| 3937 | if (Lex.getKind() == lltok::LabelStr) { |
| 3938 | Name = Lex.getStrVal(); |
| 3939 | Lex.Lex(); |
| 3940 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3941 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3942 | BasicBlock *BB = PFS.DefineBB(Name, NameLoc); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3943 | if (!BB) return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3944 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3945 | std::string NameStr; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3946 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3947 | // Parse the instructions in this block until we get a terminator. |
| 3948 | Instruction *Inst; |
| 3949 | do { |
| 3950 | // This instruction may have three possibilities for a name: a) none |
| 3951 | // specified, b) name specified "%foo =", c) number specified: "%4 =". |
| 3952 | LocTy NameLoc = Lex.getLoc(); |
| 3953 | int NameID = -1; |
| 3954 | NameStr = ""; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3955 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3956 | if (Lex.getKind() == lltok::LocalVarID) { |
| 3957 | NameID = Lex.getUIntVal(); |
| 3958 | Lex.Lex(); |
| 3959 | if (ParseToken(lltok::equal, "expected '=' after instruction id")) |
| 3960 | return true; |
Chris Lattner | def1949 | 2011-06-17 06:36:20 +0000 | [diff] [blame] | 3961 | } else if (Lex.getKind() == lltok::LocalVar) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3962 | NameStr = Lex.getStrVal(); |
| 3963 | Lex.Lex(); |
| 3964 | if (ParseToken(lltok::equal, "expected '=' after instruction name")) |
| 3965 | return true; |
| 3966 | } |
Devang Patel | ea8a4b9 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 3967 | |
Chris Lattner | 77b89dc | 2009-12-30 05:23:43 +0000 | [diff] [blame] | 3968 | switch (ParseInstruction(Inst, BB, PFS)) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 3969 | default: llvm_unreachable("Unknown ParseInstruction result!"); |
Chris Lattner | 77b89dc | 2009-12-30 05:23:43 +0000 | [diff] [blame] | 3970 | case InstError: return true; |
| 3971 | case InstNormal: |
Chris Lattner | 2e664bd | 2010-04-07 04:08:57 +0000 | [diff] [blame] | 3972 | BB->getInstList().push_back(Inst); |
| 3973 | |
Chris Lattner | 77b89dc | 2009-12-30 05:23:43 +0000 | [diff] [blame] | 3974 | // With a normal result, we check to see if the instruction is followed by |
| 3975 | // a comma and metadata. |
| 3976 | if (EatIfPresent(lltok::comma)) |
Dan Gohman | 338d9a4 | 2010-08-24 02:05:17 +0000 | [diff] [blame] | 3977 | if (ParseInstructionMetadata(Inst, &PFS)) |
Chris Lattner | 77b89dc | 2009-12-30 05:23:43 +0000 | [diff] [blame] | 3978 | return true; |
| 3979 | break; |
| 3980 | case InstExtraComma: |
Chris Lattner | 2e664bd | 2010-04-07 04:08:57 +0000 | [diff] [blame] | 3981 | BB->getInstList().push_back(Inst); |
| 3982 | |
Chris Lattner | 77b89dc | 2009-12-30 05:23:43 +0000 | [diff] [blame] | 3983 | // If the instruction parser ate an extra comma at the end of it, it |
| 3984 | // *must* be followed by metadata. |
Dan Gohman | 338d9a4 | 2010-08-24 02:05:17 +0000 | [diff] [blame] | 3985 | if (ParseInstructionMetadata(Inst, &PFS)) |
Chris Lattner | 77b89dc | 2009-12-30 05:23:43 +0000 | [diff] [blame] | 3986 | return true; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3987 | break; |
Chris Lattner | 77b89dc | 2009-12-30 05:23:43 +0000 | [diff] [blame] | 3988 | } |
Devang Patel | ea8a4b9 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 3989 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3990 | // Set the name on the instruction. |
| 3991 | if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true; |
| 3992 | } while (!isa<TerminatorInst>(Inst)); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3993 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3994 | return false; |
| 3995 | } |
| 3996 | |
| 3997 | //===----------------------------------------------------------------------===// |
| 3998 | // Instruction Parsing. |
| 3999 | //===----------------------------------------------------------------------===// |
| 4000 | |
| 4001 | /// ParseInstruction - Parse one of the many different instructions. |
| 4002 | /// |
Chris Lattner | 77b89dc | 2009-12-30 05:23:43 +0000 | [diff] [blame] | 4003 | int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB, |
| 4004 | PerFunctionState &PFS) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4005 | lltok::Kind Token = Lex.getKind(); |
| 4006 | if (Token == lltok::Eof) |
| 4007 | return TokError("found end of file when expecting more instructions"); |
| 4008 | LocTy Loc = Lex.getLoc(); |
Chris Lattner | 89d856e | 2009-03-01 00:53:13 +0000 | [diff] [blame] | 4009 | unsigned KeywordVal = Lex.getUIntVal(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4010 | Lex.Lex(); // Eat the keyword. |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4011 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4012 | switch (Token) { |
| 4013 | default: return Error(Loc, "expected instruction opcode"); |
| 4014 | // Terminator Instructions. |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4015 | case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4016 | case lltok::kw_ret: return ParseRet(Inst, BB, PFS); |
| 4017 | case lltok::kw_br: return ParseBr(Inst, PFS); |
| 4018 | case lltok::kw_switch: return ParseSwitch(Inst, PFS); |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 4019 | case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4020 | case lltok::kw_invoke: return ParseInvoke(Inst, PFS); |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 4021 | case lltok::kw_resume: return ParseResume(Inst, PFS); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4022 | // Binary Operators. |
| 4023 | case lltok::kw_add: |
| 4024 | case lltok::kw_sub: |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 4025 | case lltok::kw_mul: |
| 4026 | case lltok::kw_shl: { |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 4027 | bool NUW = EatIfPresent(lltok::kw_nuw); |
| 4028 | bool NSW = EatIfPresent(lltok::kw_nsw); |
| 4029 | if (!NUW) NUW = EatIfPresent(lltok::kw_nuw); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4030 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 4031 | if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4032 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 4033 | if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true); |
| 4034 | if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true); |
| 4035 | return false; |
Dan Gohman | 9c7f808 | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 4036 | } |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 4037 | case lltok::kw_fadd: |
| 4038 | case lltok::kw_fsub: |
Michael Ilseman | 9205317 | 2012-11-27 00:42:44 +0000 | [diff] [blame] | 4039 | case lltok::kw_fmul: |
| 4040 | case lltok::kw_fdiv: |
| 4041 | case lltok::kw_frem: { |
| 4042 | FastMathFlags FMF = EatFastMathFlagsIfPresent(); |
| 4043 | int Res = ParseArithmetic(Inst, PFS, KeywordVal, 2); |
| 4044 | if (Res != 0) |
| 4045 | return Res; |
| 4046 | if (FMF.any()) |
| 4047 | Inst->setFastMathFlags(FMF); |
| 4048 | return 0; |
| 4049 | } |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 4050 | |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 4051 | case lltok::kw_sdiv: |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 4052 | case lltok::kw_udiv: |
| 4053 | case lltok::kw_lshr: |
| 4054 | case lltok::kw_ashr: { |
| 4055 | bool Exact = EatIfPresent(lltok::kw_exact); |
| 4056 | |
| 4057 | if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true; |
| 4058 | if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true); |
| 4059 | return false; |
Dan Gohman | 9c7f808 | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 4060 | } |
| 4061 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4062 | case lltok::kw_urem: |
Chris Lattner | 89d856e | 2009-03-01 00:53:13 +0000 | [diff] [blame] | 4063 | case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4064 | case lltok::kw_and: |
| 4065 | case lltok::kw_or: |
Chris Lattner | 89d856e | 2009-03-01 00:53:13 +0000 | [diff] [blame] | 4066 | case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4067 | case lltok::kw_icmp: |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 4068 | case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4069 | // Casts. |
| 4070 | case lltok::kw_trunc: |
| 4071 | case lltok::kw_zext: |
| 4072 | case lltok::kw_sext: |
| 4073 | case lltok::kw_fptrunc: |
| 4074 | case lltok::kw_fpext: |
| 4075 | case lltok::kw_bitcast: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 4076 | case lltok::kw_addrspacecast: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4077 | case lltok::kw_uitofp: |
| 4078 | case lltok::kw_sitofp: |
| 4079 | case lltok::kw_fptoui: |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4080 | case lltok::kw_fptosi: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4081 | case lltok::kw_inttoptr: |
Chris Lattner | 89d856e | 2009-03-01 00:53:13 +0000 | [diff] [blame] | 4082 | case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4083 | // Other. |
| 4084 | case lltok::kw_select: return ParseSelect(Inst, PFS); |
Chris Lattner | b55ab54 | 2009-01-05 08:18:44 +0000 | [diff] [blame] | 4085 | case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4086 | case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS); |
| 4087 | case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS); |
| 4088 | case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS); |
| 4089 | case lltok::kw_phi: return ParsePHI(Inst, PFS); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4090 | case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS); |
Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 4091 | // Call. |
| 4092 | case lltok::kw_call: return ParseCall(Inst, PFS, CallInst::TCK_None); |
| 4093 | case lltok::kw_tail: return ParseCall(Inst, PFS, CallInst::TCK_Tail); |
| 4094 | case lltok::kw_musttail: return ParseCall(Inst, PFS, CallInst::TCK_MustTail); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4095 | // Memory. |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 4096 | case lltok::kw_alloca: return ParseAlloc(Inst, PFS); |
Chris Lattner | bc63929 | 2011-11-27 06:56:53 +0000 | [diff] [blame] | 4097 | case lltok::kw_load: return ParseLoad(Inst, PFS); |
| 4098 | case lltok::kw_store: return ParseStore(Inst, PFS); |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4099 | case lltok::kw_cmpxchg: return ParseCmpXchg(Inst, PFS); |
| 4100 | case lltok::kw_atomicrmw: return ParseAtomicRMW(Inst, PFS); |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 4101 | case lltok::kw_fence: return ParseFence(Inst, PFS); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4102 | case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS); |
| 4103 | case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS); |
| 4104 | case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS); |
| 4105 | } |
| 4106 | } |
| 4107 | |
| 4108 | /// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind. |
| 4109 | bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) { |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 4110 | if (Opc == Instruction::FCmp) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4111 | switch (Lex.getKind()) { |
David Tweed | a11edf0 | 2013-01-07 13:32:38 +0000 | [diff] [blame] | 4112 | default: return TokError("expected fcmp predicate (e.g. 'oeq')"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4113 | case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break; |
| 4114 | case lltok::kw_one: P = CmpInst::FCMP_ONE; break; |
| 4115 | case lltok::kw_olt: P = CmpInst::FCMP_OLT; break; |
| 4116 | case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break; |
| 4117 | case lltok::kw_ole: P = CmpInst::FCMP_OLE; break; |
| 4118 | case lltok::kw_oge: P = CmpInst::FCMP_OGE; break; |
| 4119 | case lltok::kw_ord: P = CmpInst::FCMP_ORD; break; |
| 4120 | case lltok::kw_uno: P = CmpInst::FCMP_UNO; break; |
| 4121 | case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break; |
| 4122 | case lltok::kw_une: P = CmpInst::FCMP_UNE; break; |
| 4123 | case lltok::kw_ult: P = CmpInst::FCMP_ULT; break; |
| 4124 | case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break; |
| 4125 | case lltok::kw_ule: P = CmpInst::FCMP_ULE; break; |
| 4126 | case lltok::kw_uge: P = CmpInst::FCMP_UGE; break; |
| 4127 | case lltok::kw_true: P = CmpInst::FCMP_TRUE; break; |
| 4128 | case lltok::kw_false: P = CmpInst::FCMP_FALSE; break; |
| 4129 | } |
| 4130 | } else { |
| 4131 | switch (Lex.getKind()) { |
David Tweed | a11edf0 | 2013-01-07 13:32:38 +0000 | [diff] [blame] | 4132 | default: return TokError("expected icmp predicate (e.g. 'eq')"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4133 | case lltok::kw_eq: P = CmpInst::ICMP_EQ; break; |
| 4134 | case lltok::kw_ne: P = CmpInst::ICMP_NE; break; |
| 4135 | case lltok::kw_slt: P = CmpInst::ICMP_SLT; break; |
| 4136 | case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break; |
| 4137 | case lltok::kw_sle: P = CmpInst::ICMP_SLE; break; |
| 4138 | case lltok::kw_sge: P = CmpInst::ICMP_SGE; break; |
| 4139 | case lltok::kw_ult: P = CmpInst::ICMP_ULT; break; |
| 4140 | case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break; |
| 4141 | case lltok::kw_ule: P = CmpInst::ICMP_ULE; break; |
| 4142 | case lltok::kw_uge: P = CmpInst::ICMP_UGE; break; |
| 4143 | } |
| 4144 | } |
| 4145 | Lex.Lex(); |
| 4146 | return false; |
| 4147 | } |
| 4148 | |
| 4149 | //===----------------------------------------------------------------------===// |
| 4150 | // Terminator Instructions. |
| 4151 | //===----------------------------------------------------------------------===// |
| 4152 | |
| 4153 | /// ParseRet - Parse a return instruction. |
Chris Lattner | 596760d | 2009-12-29 21:25:40 +0000 | [diff] [blame] | 4154 | /// ::= 'ret' void (',' !dbg, !1)* |
| 4155 | /// ::= 'ret' TypeAndValue (',' !dbg, !1)* |
Chris Lattner | 33de427 | 2011-06-17 06:49:41 +0000 | [diff] [blame] | 4156 | bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB, |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 4157 | PerFunctionState &PFS) { |
| 4158 | SMLoc TypeLoc = Lex.getLoc(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4159 | Type *Ty = nullptr; |
Chris Lattner | f880ca2 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 4160 | if (ParseType(Ty, true /*void allowed*/)) return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4161 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 4162 | Type *ResType = PFS.getFunction().getReturnType(); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4163 | |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 4164 | if (Ty->isVoidTy()) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 4165 | if (!ResType->isVoidTy()) |
| 4166 | return Error(TypeLoc, "value doesn't match function result type '" + |
| 4167 | getTypeString(ResType) + "'"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4168 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4169 | Inst = ReturnInst::Create(Context); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4170 | return false; |
| 4171 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4172 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4173 | Value *RV; |
| 4174 | if (ParseValue(Ty, RV, PFS)) return true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4175 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 4176 | if (ResType != RV->getType()) |
| 4177 | return Error(TypeLoc, "value doesn't match function result type '" + |
| 4178 | getTypeString(ResType) + "'"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4179 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4180 | Inst = ReturnInst::Create(Context, RV); |
Chris Lattner | 33de427 | 2011-06-17 06:49:41 +0000 | [diff] [blame] | 4181 | return false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4182 | } |
| 4183 | |
| 4184 | |
| 4185 | /// ParseBr |
| 4186 | /// ::= 'br' TypeAndValue |
| 4187 | /// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue |
| 4188 | bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) { |
| 4189 | LocTy Loc, Loc2; |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4190 | Value *Op0; |
| 4191 | BasicBlock *Op1, *Op2; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4192 | if (ParseTypeAndValue(Op0, Loc, PFS)) return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4193 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4194 | if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) { |
| 4195 | Inst = BranchInst::Create(BB); |
| 4196 | return false; |
| 4197 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4198 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4199 | if (Op0->getType() != Type::getInt1Ty(Context)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4200 | return Error(Loc, "branch condition must have 'i1' type"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4201 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4202 | if (ParseToken(lltok::comma, "expected ',' after branch condition") || |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4203 | ParseTypeAndBasicBlock(Op1, Loc, PFS) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4204 | ParseToken(lltok::comma, "expected ',' after true destination") || |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4205 | ParseTypeAndBasicBlock(Op2, Loc2, PFS)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4206 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4207 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4208 | Inst = BranchInst::Create(Op1, Op2, Op0); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4209 | return false; |
| 4210 | } |
| 4211 | |
| 4212 | /// ParseSwitch |
| 4213 | /// Instruction |
| 4214 | /// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']' |
| 4215 | /// JumpTable |
| 4216 | /// ::= (TypeAndValue ',' TypeAndValue)* |
| 4217 | bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) { |
| 4218 | LocTy CondLoc, BBLoc; |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4219 | Value *Cond; |
| 4220 | BasicBlock *DefaultBB; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4221 | if (ParseTypeAndValue(Cond, CondLoc, PFS) || |
| 4222 | ParseToken(lltok::comma, "expected ',' after switch condition") || |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4223 | ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4224 | ParseToken(lltok::lsquare, "expected '[' with switch table")) |
| 4225 | return true; |
| 4226 | |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 4227 | if (!Cond->getType()->isIntegerTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4228 | return Error(CondLoc, "switch condition must have integer type"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4229 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4230 | // Parse the jump table pairs. |
| 4231 | SmallPtrSet<Value*, 32> SeenCases; |
| 4232 | SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table; |
| 4233 | while (Lex.getKind() != lltok::rsquare) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4234 | Value *Constant; |
| 4235 | BasicBlock *DestBB; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4236 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4237 | if (ParseTypeAndValue(Constant, CondLoc, PFS) || |
| 4238 | ParseToken(lltok::comma, "expected ',' after case value") || |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4239 | ParseTypeAndBasicBlock(DestBB, PFS)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4240 | return true; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4241 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 4242 | if (!SeenCases.insert(Constant).second) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4243 | return Error(CondLoc, "duplicate case value in switch"); |
| 4244 | if (!isa<ConstantInt>(Constant)) |
| 4245 | return Error(CondLoc, "case value is not a constant integer"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4246 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4247 | Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB)); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4248 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4249 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4250 | Lex.Lex(); // Eat the ']'. |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4251 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4252 | SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size()); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4253 | for (unsigned i = 0, e = Table.size(); i != e; ++i) |
| 4254 | SI->addCase(Table[i].first, Table[i].second); |
| 4255 | Inst = SI; |
| 4256 | return false; |
| 4257 | } |
| 4258 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 4259 | /// ParseIndirectBr |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4260 | /// Instruction |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 4261 | /// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']' |
| 4262 | bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4263 | LocTy AddrLoc; |
| 4264 | Value *Address; |
| 4265 | if (ParseTypeAndValue(Address, AddrLoc, PFS) || |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 4266 | ParseToken(lltok::comma, "expected ',' after indirectbr address") || |
| 4267 | ParseToken(lltok::lsquare, "expected '[' with indirectbr")) |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4268 | return true; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4269 | |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 4270 | if (!Address->getType()->isPointerTy()) |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 4271 | return Error(AddrLoc, "indirectbr address must have pointer type"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4272 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4273 | // Parse the destination list. |
| 4274 | SmallVector<BasicBlock*, 16> DestList; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4275 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4276 | if (Lex.getKind() != lltok::rsquare) { |
| 4277 | BasicBlock *DestBB; |
| 4278 | if (ParseTypeAndBasicBlock(DestBB, PFS)) |
| 4279 | return true; |
| 4280 | DestList.push_back(DestBB); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4281 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4282 | while (EatIfPresent(lltok::comma)) { |
| 4283 | if (ParseTypeAndBasicBlock(DestBB, PFS)) |
| 4284 | return true; |
| 4285 | DestList.push_back(DestBB); |
| 4286 | } |
| 4287 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4288 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4289 | if (ParseToken(lltok::rsquare, "expected ']' at end of block list")) |
| 4290 | return true; |
| 4291 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 4292 | IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size()); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4293 | for (unsigned i = 0, e = DestList.size(); i != e; ++i) |
| 4294 | IBI->addDestination(DestList[i]); |
| 4295 | Inst = IBI; |
| 4296 | return false; |
| 4297 | } |
| 4298 | |
| 4299 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4300 | /// ParseInvoke |
| 4301 | /// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList |
| 4302 | /// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue |
| 4303 | bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) { |
| 4304 | LocTy CallLoc = Lex.getLoc(); |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 4305 | AttrBuilder RetAttrs, FnAttrs; |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 4306 | std::vector<unsigned> FwdRefAttrGrps; |
Bill Wendling | 09bd1f7 | 2013-02-22 00:12:35 +0000 | [diff] [blame] | 4307 | LocTy NoBuiltinLoc; |
Alexey Samsonov | 17a9cff | 2014-09-10 18:00:17 +0000 | [diff] [blame] | 4308 | unsigned CC; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4309 | Type *RetType = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4310 | LocTy RetTypeLoc; |
| 4311 | ValID CalleeID; |
| 4312 | SmallVector<ParamInfo, 16> ArgList; |
| 4313 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4314 | BasicBlock *NormalBB, *UnwindBB; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4315 | if (ParseOptionalCallingConv(CC) || |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 4316 | ParseOptionalReturnAttrs(RetAttrs) || |
Chris Lattner | f880ca2 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 4317 | ParseType(RetType, RetTypeLoc, true /*void allowed*/) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4318 | ParseValID(CalleeID) || |
| 4319 | ParseParameterList(ArgList, PFS) || |
Bill Wendling | 09bd1f7 | 2013-02-22 00:12:35 +0000 | [diff] [blame] | 4320 | ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false, |
| 4321 | NoBuiltinLoc) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4322 | ParseToken(lltok::kw_to, "expected 'to' in invoke") || |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4323 | ParseTypeAndBasicBlock(NormalBB, PFS) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4324 | ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") || |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 4325 | ParseTypeAndBasicBlock(UnwindBB, PFS)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4326 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4327 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4328 | // If RetType is a non-function pointer type, then this is the short syntax |
| 4329 | // for the call, which means that RetType is just the return type. Infer the |
| 4330 | // rest of the function argument types from the arguments that are present. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4331 | PointerType *PFTy = nullptr; |
| 4332 | FunctionType *Ty = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4333 | if (!(PFTy = dyn_cast<PointerType>(RetType)) || |
| 4334 | !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) { |
| 4335 | // Pull out the types of all of the arguments... |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 4336 | std::vector<Type*> ParamTypes; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4337 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) |
| 4338 | ParamTypes.push_back(ArgList[i].V->getType()); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4339 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4340 | if (!FunctionType::isValidReturnType(RetType)) |
| 4341 | return Error(RetTypeLoc, "Invalid result type for LLVM function"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4342 | |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 4343 | Ty = FunctionType::get(RetType, ParamTypes, false); |
| 4344 | PFTy = PointerType::getUnqual(Ty); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4345 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4346 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4347 | // Look up the callee. |
| 4348 | Value *Callee; |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 4349 | if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4350 | |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 4351 | // Set up the Attribute for the function. |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 4352 | SmallVector<AttributeSet, 8> Attrs; |
Bill Wendling | 3bef2dd | 2012-09-19 23:54:18 +0000 | [diff] [blame] | 4353 | if (RetAttrs.hasAttributes()) |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 4354 | Attrs.push_back(AttributeSet::get(RetType->getContext(), |
| 4355 | AttributeSet::ReturnIndex, |
| 4356 | RetAttrs)); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4357 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4358 | SmallVector<Value*, 8> Args; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4359 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4360 | // Loop through FunctionType's arguments and ensure they are specified |
| 4361 | // correctly. Also, gather any parameter attributes. |
| 4362 | FunctionType::param_iterator I = Ty->param_begin(); |
| 4363 | FunctionType::param_iterator E = Ty->param_end(); |
| 4364 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4365 | Type *ExpectedTy = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4366 | if (I != E) { |
| 4367 | ExpectedTy = *I++; |
| 4368 | } else if (!Ty->isVarArg()) { |
| 4369 | return Error(ArgList[i].Loc, "too many arguments specified"); |
| 4370 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4371 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4372 | if (ExpectedTy && ExpectedTy != ArgList[i].V->getType()) |
| 4373 | return Error(ArgList[i].Loc, "argument is not of expected type '" + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 4374 | getTypeString(ExpectedTy) + "'"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4375 | Args.push_back(ArgList[i].V); |
Bill Wendling | fe0021a | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 4376 | if (ArgList[i].Attrs.hasAttributes(i + 1)) { |
| 4377 | AttrBuilder B(ArgList[i].Attrs, i + 1); |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 4378 | Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B)); |
| 4379 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4380 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4381 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4382 | if (I != E) |
| 4383 | return Error(CallLoc, "not enough parameters specified for call"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4384 | |
Bill Wendling | 3bef2dd | 2012-09-19 23:54:18 +0000 | [diff] [blame] | 4385 | if (FnAttrs.hasAttributes()) |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 4386 | Attrs.push_back(AttributeSet::get(RetType->getContext(), |
| 4387 | AttributeSet::FunctionIndex, |
| 4388 | FnAttrs)); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4389 | |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 4390 | // Finish off the Attribute and check them |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 4391 | AttributeSet PAL = AttributeSet::get(Context, Attrs); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4392 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 4393 | InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB, Args); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4394 | II->setCallingConv(CC); |
| 4395 | II->setAttributes(PAL); |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 4396 | ForwardRefAttrGroups[II] = FwdRefAttrGrps; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4397 | Inst = II; |
| 4398 | return false; |
| 4399 | } |
| 4400 | |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 4401 | /// ParseResume |
| 4402 | /// ::= 'resume' TypeAndValue |
| 4403 | bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) { |
| 4404 | Value *Exn; LocTy ExnLoc; |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 4405 | if (ParseTypeAndValue(Exn, ExnLoc, PFS)) |
| 4406 | return true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4407 | |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 4408 | ResumeInst *RI = ResumeInst::Create(Exn); |
| 4409 | Inst = RI; |
| 4410 | return false; |
| 4411 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4412 | |
| 4413 | //===----------------------------------------------------------------------===// |
| 4414 | // Binary Operators. |
| 4415 | //===----------------------------------------------------------------------===// |
| 4416 | |
| 4417 | /// ParseArithmetic |
Chris Lattner | eeefa9a | 2009-01-05 08:24:46 +0000 | [diff] [blame] | 4418 | /// ::= ArithmeticOps TypeAndValue ',' Value |
| 4419 | /// |
| 4420 | /// If OperandType is 0, then any FP or integer operand is allowed. If it is 1, |
| 4421 | /// then any integer operand is allowed, if it is 2, any fp operand is allowed. |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4422 | bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS, |
Chris Lattner | eeefa9a | 2009-01-05 08:24:46 +0000 | [diff] [blame] | 4423 | unsigned Opc, unsigned OperandType) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4424 | LocTy Loc; Value *LHS, *RHS; |
| 4425 | if (ParseTypeAndValue(LHS, Loc, PFS) || |
| 4426 | ParseToken(lltok::comma, "expected ',' in arithmetic operation") || |
| 4427 | ParseValue(LHS->getType(), RHS, PFS)) |
| 4428 | return true; |
| 4429 | |
Chris Lattner | eeefa9a | 2009-01-05 08:24:46 +0000 | [diff] [blame] | 4430 | bool Valid; |
| 4431 | switch (OperandType) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4432 | default: llvm_unreachable("Unknown operand type!"); |
Chris Lattner | eeefa9a | 2009-01-05 08:24:46 +0000 | [diff] [blame] | 4433 | case 0: // int or FP. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 4434 | Valid = LHS->getType()->isIntOrIntVectorTy() || |
| 4435 | LHS->getType()->isFPOrFPVectorTy(); |
Chris Lattner | eeefa9a | 2009-01-05 08:24:46 +0000 | [diff] [blame] | 4436 | break; |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 4437 | case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break; |
| 4438 | case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break; |
Chris Lattner | eeefa9a | 2009-01-05 08:24:46 +0000 | [diff] [blame] | 4439 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4440 | |
Chris Lattner | eeefa9a | 2009-01-05 08:24:46 +0000 | [diff] [blame] | 4441 | if (!Valid) |
| 4442 | return Error(Loc, "invalid operand type for instruction"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4443 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4444 | Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); |
| 4445 | return false; |
| 4446 | } |
| 4447 | |
| 4448 | /// ParseLogical |
| 4449 | /// ::= ArithmeticOps TypeAndValue ',' Value { |
| 4450 | bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS, |
| 4451 | unsigned Opc) { |
| 4452 | LocTy Loc; Value *LHS, *RHS; |
| 4453 | if (ParseTypeAndValue(LHS, Loc, PFS) || |
| 4454 | ParseToken(lltok::comma, "expected ',' in logical operation") || |
| 4455 | ParseValue(LHS->getType(), RHS, PFS)) |
| 4456 | return true; |
| 4457 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 4458 | if (!LHS->getType()->isIntOrIntVectorTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4459 | return Error(Loc,"instruction requires integer or integer vector operands"); |
| 4460 | |
| 4461 | Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); |
| 4462 | return false; |
| 4463 | } |
| 4464 | |
| 4465 | |
| 4466 | /// ParseCompare |
| 4467 | /// ::= 'icmp' IPredicates TypeAndValue ',' Value |
| 4468 | /// ::= 'fcmp' FPredicates TypeAndValue ',' Value |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4469 | bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS, |
| 4470 | unsigned Opc) { |
| 4471 | // Parse the integer/fp comparison predicate. |
| 4472 | LocTy Loc; |
| 4473 | unsigned Pred; |
| 4474 | Value *LHS, *RHS; |
| 4475 | if (ParseCmpPredicate(Pred, Opc) || |
| 4476 | ParseTypeAndValue(LHS, Loc, PFS) || |
| 4477 | ParseToken(lltok::comma, "expected ',' after compare value") || |
| 4478 | ParseValue(LHS->getType(), RHS, PFS)) |
| 4479 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4480 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4481 | if (Opc == Instruction::FCmp) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 4482 | if (!LHS->getType()->isFPOrFPVectorTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4483 | return Error(Loc, "fcmp requires floating point operands"); |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 4484 | Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS); |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 4485 | } else { |
| 4486 | assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 4487 | if (!LHS->getType()->isIntOrIntVectorTy() && |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 4488 | !LHS->getType()->getScalarType()->isPointerTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4489 | return Error(Loc, "icmp requires integer operands"); |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 4490 | Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4491 | } |
| 4492 | return false; |
| 4493 | } |
| 4494 | |
| 4495 | //===----------------------------------------------------------------------===// |
| 4496 | // Other Instructions. |
| 4497 | //===----------------------------------------------------------------------===// |
| 4498 | |
| 4499 | |
| 4500 | /// ParseCast |
| 4501 | /// ::= CastOpc TypeAndValue 'to' Type |
| 4502 | bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS, |
| 4503 | unsigned Opc) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 4504 | LocTy Loc; |
| 4505 | Value *Op; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4506 | Type *DestTy = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4507 | if (ParseTypeAndValue(Op, Loc, PFS) || |
| 4508 | ParseToken(lltok::kw_to, "expected 'to' after cast value") || |
| 4509 | ParseType(DestTy)) |
| 4510 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4511 | |
Chris Lattner | 89d856e | 2009-03-01 00:53:13 +0000 | [diff] [blame] | 4512 | if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) { |
| 4513 | CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4514 | return Error(Loc, "invalid cast opcode for cast from '" + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 4515 | getTypeString(Op->getType()) + "' to '" + |
| 4516 | getTypeString(DestTy) + "'"); |
Chris Lattner | 89d856e | 2009-03-01 00:53:13 +0000 | [diff] [blame] | 4517 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4518 | Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy); |
| 4519 | return false; |
| 4520 | } |
| 4521 | |
| 4522 | /// ParseSelect |
| 4523 | /// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue |
| 4524 | bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) { |
| 4525 | LocTy Loc; |
| 4526 | Value *Op0, *Op1, *Op2; |
| 4527 | if (ParseTypeAndValue(Op0, Loc, PFS) || |
| 4528 | ParseToken(lltok::comma, "expected ',' after select condition") || |
| 4529 | ParseTypeAndValue(Op1, PFS) || |
| 4530 | ParseToken(lltok::comma, "expected ',' after select value") || |
| 4531 | ParseTypeAndValue(Op2, PFS)) |
| 4532 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4533 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4534 | if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2)) |
| 4535 | return Error(Loc, Reason); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4536 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4537 | Inst = SelectInst::Create(Op0, Op1, Op2); |
| 4538 | return false; |
| 4539 | } |
| 4540 | |
Chris Lattner | b55ab54 | 2009-01-05 08:18:44 +0000 | [diff] [blame] | 4541 | /// ParseVA_Arg |
| 4542 | /// ::= 'va_arg' TypeAndValue ',' Type |
| 4543 | bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4544 | Value *Op; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4545 | Type *EltTy = nullptr; |
Chris Lattner | b55ab54 | 2009-01-05 08:18:44 +0000 | [diff] [blame] | 4546 | LocTy TypeLoc; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4547 | if (ParseTypeAndValue(Op, PFS) || |
| 4548 | ParseToken(lltok::comma, "expected ',' after vaarg operand") || |
Chris Lattner | b55ab54 | 2009-01-05 08:18:44 +0000 | [diff] [blame] | 4549 | ParseType(EltTy, TypeLoc)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4550 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4551 | |
Chris Lattner | b55ab54 | 2009-01-05 08:18:44 +0000 | [diff] [blame] | 4552 | if (!EltTy->isFirstClassType()) |
| 4553 | return Error(TypeLoc, "va_arg requires operand with first class type"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4554 | |
| 4555 | Inst = new VAArgInst(Op, EltTy); |
| 4556 | return false; |
| 4557 | } |
| 4558 | |
| 4559 | /// ParseExtractElement |
| 4560 | /// ::= 'extractelement' TypeAndValue ',' TypeAndValue |
| 4561 | bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) { |
| 4562 | LocTy Loc; |
| 4563 | Value *Op0, *Op1; |
| 4564 | if (ParseTypeAndValue(Op0, Loc, PFS) || |
| 4565 | ParseToken(lltok::comma, "expected ',' after extract value") || |
| 4566 | ParseTypeAndValue(Op1, PFS)) |
| 4567 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4568 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4569 | if (!ExtractElementInst::isValidOperands(Op0, Op1)) |
| 4570 | return Error(Loc, "invalid extractelement operands"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4571 | |
Eric Christopher | c974225 | 2009-07-25 02:28:41 +0000 | [diff] [blame] | 4572 | Inst = ExtractElementInst::Create(Op0, Op1); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4573 | return false; |
| 4574 | } |
| 4575 | |
| 4576 | /// ParseInsertElement |
| 4577 | /// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue |
| 4578 | bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) { |
| 4579 | LocTy Loc; |
| 4580 | Value *Op0, *Op1, *Op2; |
| 4581 | if (ParseTypeAndValue(Op0, Loc, PFS) || |
| 4582 | ParseToken(lltok::comma, "expected ',' after insertelement value") || |
| 4583 | ParseTypeAndValue(Op1, PFS) || |
| 4584 | ParseToken(lltok::comma, "expected ',' after insertelement value") || |
| 4585 | ParseTypeAndValue(Op2, PFS)) |
| 4586 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4587 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4588 | if (!InsertElementInst::isValidOperands(Op0, Op1, Op2)) |
Eric Christopher | fef8db6 | 2009-07-23 01:01:32 +0000 | [diff] [blame] | 4589 | return Error(Loc, "invalid insertelement operands"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4590 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4591 | Inst = InsertElementInst::Create(Op0, Op1, Op2); |
| 4592 | return false; |
| 4593 | } |
| 4594 | |
| 4595 | /// ParseShuffleVector |
| 4596 | /// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue |
| 4597 | bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) { |
| 4598 | LocTy Loc; |
| 4599 | Value *Op0, *Op1, *Op2; |
| 4600 | if (ParseTypeAndValue(Op0, Loc, PFS) || |
| 4601 | ParseToken(lltok::comma, "expected ',' after shuffle mask") || |
| 4602 | ParseTypeAndValue(Op1, PFS) || |
| 4603 | ParseToken(lltok::comma, "expected ',' after shuffle value") || |
| 4604 | ParseTypeAndValue(Op2, PFS)) |
| 4605 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4606 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4607 | if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2)) |
Pete Cooper | c1a6f98 | 2012-02-01 23:43:12 +0000 | [diff] [blame] | 4608 | return Error(Loc, "invalid shufflevector operands"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4609 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4610 | Inst = new ShuffleVectorInst(Op0, Op1, Op2); |
| 4611 | return false; |
| 4612 | } |
| 4613 | |
| 4614 | /// ParsePHI |
Chris Lattner | c05471e | 2009-10-18 05:27:44 +0000 | [diff] [blame] | 4615 | /// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')* |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4616 | int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4617 | Type *Ty = nullptr; LocTy TypeLoc; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4618 | Value *Op0, *Op1; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4619 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 4620 | if (ParseType(Ty, TypeLoc) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4621 | ParseToken(lltok::lsquare, "expected '[' in phi value list") || |
| 4622 | ParseValue(Ty, Op0, PFS) || |
| 4623 | ParseToken(lltok::comma, "expected ',' after insertelement value") || |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4624 | ParseValue(Type::getLabelTy(Context), Op1, PFS) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4625 | ParseToken(lltok::rsquare, "expected ']' in phi value list")) |
| 4626 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4627 | |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4628 | bool AteExtraComma = false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4629 | SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals; |
| 4630 | while (1) { |
| 4631 | PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1))); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4632 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 4633 | if (!EatIfPresent(lltok::comma)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4634 | break; |
| 4635 | |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4636 | if (Lex.getKind() == lltok::MetadataVar) { |
| 4637 | AteExtraComma = true; |
Devang Patel | 8f842d3 | 2009-10-16 18:45:49 +0000 | [diff] [blame] | 4638 | break; |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4639 | } |
Devang Patel | 8f842d3 | 2009-10-16 18:45:49 +0000 | [diff] [blame] | 4640 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 4641 | if (ParseToken(lltok::lsquare, "expected '[' in phi value list") || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4642 | ParseValue(Ty, Op0, PFS) || |
| 4643 | ParseToken(lltok::comma, "expected ',' after insertelement value") || |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4644 | ParseValue(Type::getLabelTy(Context), Op1, PFS) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4645 | ParseToken(lltok::rsquare, "expected ']' in phi value list")) |
| 4646 | return true; |
| 4647 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4648 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4649 | if (!Ty->isFirstClassType()) |
| 4650 | return Error(TypeLoc, "phi node must have first class type"); |
| 4651 | |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 4652 | PHINode *PN = PHINode::Create(Ty, PHIVals.size()); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4653 | for (unsigned i = 0, e = PHIVals.size(); i != e; ++i) |
| 4654 | PN->addIncoming(PHIVals[i].first, PHIVals[i].second); |
| 4655 | Inst = PN; |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4656 | return AteExtraComma ? InstExtraComma : InstNormal; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4657 | } |
| 4658 | |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4659 | /// ParseLandingPad |
| 4660 | /// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+ |
| 4661 | /// Clause |
| 4662 | /// ::= 'catch' TypeAndValue |
| 4663 | /// ::= 'filter' |
| 4664 | /// ::= 'filter' TypeAndValue ( ',' TypeAndValue )* |
| 4665 | bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4666 | Type *Ty = nullptr; LocTy TyLoc; |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4667 | Value *PersFn; LocTy PersFnLoc; |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4668 | |
| 4669 | if (ParseType(Ty, TyLoc) || |
| 4670 | ParseToken(lltok::kw_personality, "expected 'personality'") || |
| 4671 | ParseTypeAndValue(PersFn, PersFnLoc, PFS)) |
| 4672 | return true; |
| 4673 | |
| 4674 | LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, 0); |
| 4675 | LP->setCleanup(EatIfPresent(lltok::kw_cleanup)); |
| 4676 | |
| 4677 | while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){ |
| 4678 | LandingPadInst::ClauseType CT; |
| 4679 | if (EatIfPresent(lltok::kw_catch)) |
| 4680 | CT = LandingPadInst::Catch; |
| 4681 | else if (EatIfPresent(lltok::kw_filter)) |
| 4682 | CT = LandingPadInst::Filter; |
| 4683 | else |
| 4684 | return TokError("expected 'catch' or 'filter' clause type"); |
| 4685 | |
Rafael Espindola | 4dc5dfc | 2014-06-04 18:51:31 +0000 | [diff] [blame] | 4686 | Value *V; |
| 4687 | LocTy VLoc; |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4688 | if (ParseTypeAndValue(V, VLoc, PFS)) { |
| 4689 | delete LP; |
| 4690 | return true; |
| 4691 | } |
| 4692 | |
Bill Wendling | a52aa3c | 2011-08-12 20:52:25 +0000 | [diff] [blame] | 4693 | // A 'catch' type expects a non-array constant. A filter clause expects an |
| 4694 | // array constant. |
| 4695 | if (CT == LandingPadInst::Catch) { |
| 4696 | if (isa<ArrayType>(V->getType())) |
| 4697 | Error(VLoc, "'catch' clause has an invalid type"); |
| 4698 | } else { |
| 4699 | if (!isa<ArrayType>(V->getType())) |
| 4700 | Error(VLoc, "'filter' clause has an invalid type"); |
| 4701 | } |
| 4702 | |
Rafael Espindola | 4dc5dfc | 2014-06-04 18:51:31 +0000 | [diff] [blame] | 4703 | LP->addClause(cast<Constant>(V)); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4704 | } |
| 4705 | |
| 4706 | Inst = LP; |
| 4707 | return false; |
| 4708 | } |
| 4709 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4710 | /// ParseCall |
Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 4711 | /// ::= 'call' OptionalCallingConv OptionalAttrs Type Value |
| 4712 | /// ParameterList OptionalAttrs |
| 4713 | /// ::= 'tail' 'call' OptionalCallingConv OptionalAttrs Type Value |
| 4714 | /// ParameterList OptionalAttrs |
| 4715 | /// ::= 'musttail' 'call' OptionalCallingConv OptionalAttrs Type Value |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4716 | /// ParameterList OptionalAttrs |
| 4717 | bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS, |
Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 4718 | CallInst::TailCallKind TCK) { |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 4719 | AttrBuilder RetAttrs, FnAttrs; |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 4720 | std::vector<unsigned> FwdRefAttrGrps; |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 4721 | LocTy BuiltinLoc; |
Alexey Samsonov | 17a9cff | 2014-09-10 18:00:17 +0000 | [diff] [blame] | 4722 | unsigned CC; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4723 | Type *RetType = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4724 | LocTy RetTypeLoc; |
| 4725 | ValID CalleeID; |
| 4726 | SmallVector<ParamInfo, 16> ArgList; |
| 4727 | LocTy CallLoc = Lex.getLoc(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4728 | |
Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 4729 | if ((TCK != CallInst::TCK_None && |
| 4730 | ParseToken(lltok::kw_call, "expected 'tail call'")) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4731 | ParseOptionalCallingConv(CC) || |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 4732 | ParseOptionalReturnAttrs(RetAttrs) || |
Chris Lattner | f880ca2 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 4733 | ParseType(RetType, RetTypeLoc, true /*void allowed*/) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4734 | ParseValID(CalleeID) || |
Reid Kleckner | 8349864 | 2014-08-26 00:33:28 +0000 | [diff] [blame] | 4735 | ParseParameterList(ArgList, PFS, TCK == CallInst::TCK_MustTail, |
| 4736 | PFS.getFunction().isVarArg()) || |
Bill Wendling | 09bd1f7 | 2013-02-22 00:12:35 +0000 | [diff] [blame] | 4737 | ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false, |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 4738 | BuiltinLoc)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4739 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4740 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4741 | // If RetType is a non-function pointer type, then this is the short syntax |
| 4742 | // for the call, which means that RetType is just the return type. Infer the |
| 4743 | // rest of the function argument types from the arguments that are present. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4744 | PointerType *PFTy = nullptr; |
| 4745 | FunctionType *Ty = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4746 | if (!(PFTy = dyn_cast<PointerType>(RetType)) || |
| 4747 | !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) { |
| 4748 | // Pull out the types of all of the arguments... |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 4749 | std::vector<Type*> ParamTypes; |
Eli Friedman | 6cf5141 | 2010-07-24 23:06:59 +0000 | [diff] [blame] | 4750 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) |
| 4751 | ParamTypes.push_back(ArgList[i].V->getType()); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4752 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4753 | if (!FunctionType::isValidReturnType(RetType)) |
| 4754 | return Error(RetTypeLoc, "Invalid result type for LLVM function"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4755 | |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 4756 | Ty = FunctionType::get(RetType, ParamTypes, false); |
| 4757 | PFTy = PointerType::getUnqual(Ty); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4758 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4759 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4760 | // Look up the callee. |
| 4761 | Value *Callee; |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 4762 | if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4763 | |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 4764 | // Set up the Attribute for the function. |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 4765 | SmallVector<AttributeSet, 8> Attrs; |
Bill Wendling | 3bef2dd | 2012-09-19 23:54:18 +0000 | [diff] [blame] | 4766 | if (RetAttrs.hasAttributes()) |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 4767 | Attrs.push_back(AttributeSet::get(RetType->getContext(), |
| 4768 | AttributeSet::ReturnIndex, |
| 4769 | RetAttrs)); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4770 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4771 | SmallVector<Value*, 8> Args; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4772 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4773 | // Loop through FunctionType's arguments and ensure they are specified |
| 4774 | // correctly. Also, gather any parameter attributes. |
| 4775 | FunctionType::param_iterator I = Ty->param_begin(); |
| 4776 | FunctionType::param_iterator E = Ty->param_end(); |
| 4777 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4778 | Type *ExpectedTy = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4779 | if (I != E) { |
| 4780 | ExpectedTy = *I++; |
| 4781 | } else if (!Ty->isVarArg()) { |
| 4782 | return Error(ArgList[i].Loc, "too many arguments specified"); |
| 4783 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4784 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4785 | if (ExpectedTy && ExpectedTy != ArgList[i].V->getType()) |
| 4786 | return Error(ArgList[i].Loc, "argument is not of expected type '" + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 4787 | getTypeString(ExpectedTy) + "'"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4788 | Args.push_back(ArgList[i].V); |
Bill Wendling | fe0021a | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 4789 | if (ArgList[i].Attrs.hasAttributes(i + 1)) { |
| 4790 | AttrBuilder B(ArgList[i].Attrs, i + 1); |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 4791 | Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B)); |
| 4792 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4793 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4794 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4795 | if (I != E) |
| 4796 | return Error(CallLoc, "not enough parameters specified for call"); |
| 4797 | |
Bill Wendling | 3bef2dd | 2012-09-19 23:54:18 +0000 | [diff] [blame] | 4798 | if (FnAttrs.hasAttributes()) |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 4799 | Attrs.push_back(AttributeSet::get(RetType->getContext(), |
| 4800 | AttributeSet::FunctionIndex, |
| 4801 | FnAttrs)); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4802 | |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 4803 | // Finish off the Attribute and check them |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 4804 | AttributeSet PAL = AttributeSet::get(Context, Attrs); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4805 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 4806 | CallInst *CI = CallInst::Create(Callee, Args); |
Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 4807 | CI->setTailCallKind(TCK); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4808 | CI->setCallingConv(CC); |
| 4809 | CI->setAttributes(PAL); |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 4810 | ForwardRefAttrGroups[CI] = FwdRefAttrGrps; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4811 | Inst = CI; |
| 4812 | return false; |
| 4813 | } |
| 4814 | |
| 4815 | //===----------------------------------------------------------------------===// |
| 4816 | // Memory Instructions. |
| 4817 | //===----------------------------------------------------------------------===// |
| 4818 | |
| 4819 | /// ParseAlloc |
David Majnemer | c4ab61c | 2014-03-09 06:41:58 +0000 | [diff] [blame] | 4820 | /// ::= 'alloca' 'inalloca'? Type (',' TypeAndValue)? (',' 'align' i32)? |
Chris Lattner | 7810372 | 2011-06-17 03:16:47 +0000 | [diff] [blame] | 4821 | int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4822 | Value *Size = nullptr; |
Chris Lattner | 200e075 | 2009-07-02 23:08:13 +0000 | [diff] [blame] | 4823 | LocTy SizeLoc; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4824 | unsigned Alignment = 0; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4825 | Type *Ty = nullptr; |
David Majnemer | c4ab61c | 2014-03-09 06:41:58 +0000 | [diff] [blame] | 4826 | |
| 4827 | bool IsInAlloca = EatIfPresent(lltok::kw_inalloca); |
| 4828 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 4829 | if (ParseType(Ty)) return true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4830 | |
David Majnemer | fad5a31 | 2015-02-11 09:13:11 +0000 | [diff] [blame] | 4831 | if (!PointerType::isValidElementType(Ty)) |
| 4832 | return TokError("pointer to this type is invalid"); |
| 4833 | |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 4834 | bool AteExtraComma = false; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 4835 | if (EatIfPresent(lltok::comma)) { |
David Majnemer | c4ab61c | 2014-03-09 06:41:58 +0000 | [diff] [blame] | 4836 | if (Lex.getKind() == lltok::kw_align) { |
| 4837 | if (ParseOptionalAlignment(Alignment)) return true; |
| 4838 | } else if (Lex.getKind() == lltok::MetadataVar) { |
| 4839 | AteExtraComma = true; |
| 4840 | } else { |
| 4841 | if (ParseTypeAndValue(Size, SizeLoc, PFS) || |
| 4842 | ParseOptionalCommaAlign(Alignment, AteExtraComma)) |
| 4843 | return true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4844 | } |
| 4845 | } |
| 4846 | |
Dan Gohman | 2140a74 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 4847 | if (Size && !Size->getType()->isIntegerTy()) |
| 4848 | return Error(SizeLoc, "element count must have integer type"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4849 | |
Reid Kleckner | 436c42e | 2014-01-17 23:58:17 +0000 | [diff] [blame] | 4850 | AllocaInst *AI = new AllocaInst(Ty, Size, Alignment); |
| 4851 | AI->setUsedWithInAlloca(IsInAlloca); |
| 4852 | Inst = AI; |
Chris Lattner | 7810372 | 2011-06-17 03:16:47 +0000 | [diff] [blame] | 4853 | return AteExtraComma ? InstExtraComma : InstNormal; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4854 | } |
| 4855 | |
| 4856 | /// ParseLoad |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4857 | /// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)? |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4858 | /// ::= 'load' 'atomic' 'volatile'? TypeAndValue |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4859 | /// 'singlethread'? AtomicOrdering (',' 'align' i32)? |
Chris Lattner | bc63929 | 2011-11-27 06:56:53 +0000 | [diff] [blame] | 4860 | int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4861 | Value *Val; LocTy Loc; |
Devang Patel | ea8a4b9 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 4862 | unsigned Alignment = 0; |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 4863 | bool AteExtraComma = false; |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4864 | bool isAtomic = false; |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4865 | AtomicOrdering Ordering = NotAtomic; |
| 4866 | SynchronizationScope Scope = CrossThread; |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4867 | |
| 4868 | if (Lex.getKind() == lltok::kw_atomic) { |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4869 | isAtomic = true; |
| 4870 | Lex.Lex(); |
| 4871 | } |
| 4872 | |
Chris Lattner | bc63929 | 2011-11-27 06:56:53 +0000 | [diff] [blame] | 4873 | bool isVolatile = false; |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4874 | if (Lex.getKind() == lltok::kw_volatile) { |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4875 | isVolatile = true; |
| 4876 | Lex.Lex(); |
| 4877 | } |
| 4878 | |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 4879 | if (ParseTypeAndValue(Val, Loc, PFS) || |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4880 | ParseScopeAndOrdering(isAtomic, Scope, Ordering) || |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 4881 | ParseOptionalCommaAlign(Alignment, AteExtraComma)) |
| 4882 | return true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4883 | |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 4884 | if (!Val->getType()->isPointerTy() || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4885 | !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType()) |
| 4886 | return Error(Loc, "load operand must be a pointer to a first class type"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4887 | if (isAtomic && !Alignment) |
| 4888 | return Error(Loc, "atomic load must have explicit non-zero alignment"); |
| 4889 | if (Ordering == Release || Ordering == AcquireRelease) |
| 4890 | return Error(Loc, "atomic load cannot use Release ordering"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4891 | |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4892 | Inst = new LoadInst(Val, "", isVolatile, Alignment, Ordering, Scope); |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 4893 | return AteExtraComma ? InstExtraComma : InstNormal; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4894 | } |
| 4895 | |
| 4896 | /// ParseStore |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4897 | |
| 4898 | /// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)? |
| 4899 | /// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4900 | /// 'singlethread'? AtomicOrdering (',' 'align' i32)? |
Chris Lattner | bc63929 | 2011-11-27 06:56:53 +0000 | [diff] [blame] | 4901 | int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4902 | Value *Val, *Ptr; LocTy Loc, PtrLoc; |
Devang Patel | ea8a4b9 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 4903 | unsigned Alignment = 0; |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 4904 | bool AteExtraComma = false; |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4905 | bool isAtomic = false; |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4906 | AtomicOrdering Ordering = NotAtomic; |
| 4907 | SynchronizationScope Scope = CrossThread; |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4908 | |
| 4909 | if (Lex.getKind() == lltok::kw_atomic) { |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4910 | isAtomic = true; |
| 4911 | Lex.Lex(); |
| 4912 | } |
| 4913 | |
Chris Lattner | bc63929 | 2011-11-27 06:56:53 +0000 | [diff] [blame] | 4914 | bool isVolatile = false; |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4915 | if (Lex.getKind() == lltok::kw_volatile) { |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4916 | isVolatile = true; |
| 4917 | Lex.Lex(); |
| 4918 | } |
| 4919 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4920 | if (ParseTypeAndValue(Val, Loc, PFS) || |
| 4921 | ParseToken(lltok::comma, "expected ',' after store operand") || |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 4922 | ParseTypeAndValue(Ptr, PtrLoc, PFS) || |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4923 | ParseScopeAndOrdering(isAtomic, Scope, Ordering) || |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 4924 | ParseOptionalCommaAlign(Alignment, AteExtraComma)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4925 | return true; |
Devang Patel | ea8a4b9 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 4926 | |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 4927 | if (!Ptr->getType()->isPointerTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4928 | return Error(PtrLoc, "store operand must be a pointer"); |
| 4929 | if (!Val->getType()->isFirstClassType()) |
| 4930 | return Error(Loc, "store operand must be a first class value"); |
| 4931 | if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType()) |
| 4932 | return Error(Loc, "stored value and pointer type do not match"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4933 | if (isAtomic && !Alignment) |
| 4934 | return Error(Loc, "atomic store must have explicit non-zero alignment"); |
| 4935 | if (Ordering == Acquire || Ordering == AcquireRelease) |
| 4936 | return Error(Loc, "atomic store cannot use Acquire ordering"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4937 | |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4938 | Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, Scope); |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 4939 | return AteExtraComma ? InstExtraComma : InstNormal; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4940 | } |
| 4941 | |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4942 | /// ParseCmpXchg |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 4943 | /// ::= 'cmpxchg' 'weak'? 'volatile'? TypeAndValue ',' TypeAndValue ',' |
| 4944 | /// TypeAndValue 'singlethread'? AtomicOrdering AtomicOrdering |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4945 | int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) { |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4946 | Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc; |
| 4947 | bool AteExtraComma = false; |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 4948 | AtomicOrdering SuccessOrdering = NotAtomic; |
| 4949 | AtomicOrdering FailureOrdering = NotAtomic; |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4950 | SynchronizationScope Scope = CrossThread; |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4951 | bool isVolatile = false; |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 4952 | bool isWeak = false; |
| 4953 | |
| 4954 | if (EatIfPresent(lltok::kw_weak)) |
| 4955 | isWeak = true; |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4956 | |
| 4957 | if (EatIfPresent(lltok::kw_volatile)) |
| 4958 | isVolatile = true; |
| 4959 | |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4960 | if (ParseTypeAndValue(Ptr, PtrLoc, PFS) || |
| 4961 | ParseToken(lltok::comma, "expected ',' after cmpxchg address") || |
| 4962 | ParseTypeAndValue(Cmp, CmpLoc, PFS) || |
| 4963 | ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") || |
| 4964 | ParseTypeAndValue(New, NewLoc, PFS) || |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 4965 | ParseScopeAndOrdering(true /*Always atomic*/, Scope, SuccessOrdering) || |
| 4966 | ParseOrdering(FailureOrdering)) |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4967 | return true; |
| 4968 | |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 4969 | if (SuccessOrdering == Unordered || FailureOrdering == Unordered) |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4970 | return TokError("cmpxchg cannot be unordered"); |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 4971 | if (SuccessOrdering < FailureOrdering) |
| 4972 | return TokError("cmpxchg must be at least as ordered on success as failure"); |
| 4973 | if (FailureOrdering == Release || FailureOrdering == AcquireRelease) |
| 4974 | return TokError("cmpxchg failure ordering cannot include release semantics"); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4975 | if (!Ptr->getType()->isPointerTy()) |
| 4976 | return Error(PtrLoc, "cmpxchg operand must be a pointer"); |
| 4977 | if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType()) |
| 4978 | return Error(CmpLoc, "compare value and pointer type do not match"); |
| 4979 | if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType()) |
| 4980 | return Error(NewLoc, "new value and pointer type do not match"); |
| 4981 | if (!New->getType()->isIntegerTy()) |
| 4982 | return Error(NewLoc, "cmpxchg operand must be an integer"); |
| 4983 | unsigned Size = New->getType()->getPrimitiveSizeInBits(); |
| 4984 | if (Size < 8 || (Size & (Size - 1))) |
| 4985 | return Error(NewLoc, "cmpxchg operand must be power-of-two byte-sized" |
| 4986 | " integer"); |
| 4987 | |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 4988 | AtomicCmpXchgInst *CXI = new AtomicCmpXchgInst( |
| 4989 | Ptr, Cmp, New, SuccessOrdering, FailureOrdering, Scope); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4990 | CXI->setVolatile(isVolatile); |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 4991 | CXI->setWeak(isWeak); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4992 | Inst = CXI; |
| 4993 | return AteExtraComma ? InstExtraComma : InstNormal; |
| 4994 | } |
| 4995 | |
| 4996 | /// ParseAtomicRMW |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4997 | /// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue |
| 4998 | /// 'singlethread'? AtomicOrdering |
| 4999 | int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) { |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 5000 | Value *Ptr, *Val; LocTy PtrLoc, ValLoc; |
| 5001 | bool AteExtraComma = false; |
| 5002 | AtomicOrdering Ordering = NotAtomic; |
| 5003 | SynchronizationScope Scope = CrossThread; |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 5004 | bool isVolatile = false; |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 5005 | AtomicRMWInst::BinOp Operation; |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 5006 | |
| 5007 | if (EatIfPresent(lltok::kw_volatile)) |
| 5008 | isVolatile = true; |
| 5009 | |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 5010 | switch (Lex.getKind()) { |
| 5011 | default: return TokError("expected binary operation in atomicrmw"); |
| 5012 | case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break; |
| 5013 | case lltok::kw_add: Operation = AtomicRMWInst::Add; break; |
| 5014 | case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break; |
| 5015 | case lltok::kw_and: Operation = AtomicRMWInst::And; break; |
| 5016 | case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break; |
| 5017 | case lltok::kw_or: Operation = AtomicRMWInst::Or; break; |
| 5018 | case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break; |
| 5019 | case lltok::kw_max: Operation = AtomicRMWInst::Max; break; |
| 5020 | case lltok::kw_min: Operation = AtomicRMWInst::Min; break; |
| 5021 | case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break; |
| 5022 | case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break; |
| 5023 | } |
| 5024 | Lex.Lex(); // Eat the operation. |
| 5025 | |
| 5026 | if (ParseTypeAndValue(Ptr, PtrLoc, PFS) || |
| 5027 | ParseToken(lltok::comma, "expected ',' after atomicrmw address") || |
| 5028 | ParseTypeAndValue(Val, ValLoc, PFS) || |
| 5029 | ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering)) |
| 5030 | return true; |
| 5031 | |
| 5032 | if (Ordering == Unordered) |
| 5033 | return TokError("atomicrmw cannot be unordered"); |
| 5034 | if (!Ptr->getType()->isPointerTy()) |
| 5035 | return Error(PtrLoc, "atomicrmw operand must be a pointer"); |
| 5036 | if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType()) |
| 5037 | return Error(ValLoc, "atomicrmw value and pointer type do not match"); |
| 5038 | if (!Val->getType()->isIntegerTy()) |
| 5039 | return Error(ValLoc, "atomicrmw operand must be an integer"); |
| 5040 | unsigned Size = Val->getType()->getPrimitiveSizeInBits(); |
| 5041 | if (Size < 8 || (Size & (Size - 1))) |
| 5042 | return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized" |
| 5043 | " integer"); |
| 5044 | |
| 5045 | AtomicRMWInst *RMWI = |
| 5046 | new AtomicRMWInst(Operation, Ptr, Val, Ordering, Scope); |
| 5047 | RMWI->setVolatile(isVolatile); |
| 5048 | Inst = RMWI; |
| 5049 | return AteExtraComma ? InstExtraComma : InstNormal; |
| 5050 | } |
| 5051 | |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 5052 | /// ParseFence |
| 5053 | /// ::= 'fence' 'singlethread'? AtomicOrdering |
| 5054 | int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) { |
| 5055 | AtomicOrdering Ordering = NotAtomic; |
| 5056 | SynchronizationScope Scope = CrossThread; |
| 5057 | if (ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering)) |
| 5058 | return true; |
| 5059 | |
| 5060 | if (Ordering == Unordered) |
| 5061 | return TokError("fence cannot be unordered"); |
| 5062 | if (Ordering == Monotonic) |
| 5063 | return TokError("fence cannot be monotonic"); |
| 5064 | |
| 5065 | Inst = new FenceInst(Context, Ordering, Scope); |
| 5066 | return InstNormal; |
| 5067 | } |
| 5068 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 5069 | /// ParseGetElementPtr |
Dan Gohman | 1639c39 | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 5070 | /// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)* |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 5071 | int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 5072 | Value *Ptr = nullptr; |
| 5073 | Value *Val = nullptr; |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 5074 | LocTy Loc, EltLoc; |
Dan Gohman | 1639c39 | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 5075 | |
Dan Gohman | 16cbbe4 | 2009-07-29 15:58:36 +0000 | [diff] [blame] | 5076 | bool InBounds = EatIfPresent(lltok::kw_inbounds); |
Dan Gohman | 1639c39 | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 5077 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 5078 | if (ParseTypeAndValue(Ptr, Loc, PFS)) return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 5079 | |
Eli Bendersky | d980668 | 2013-04-22 17:03:42 +0000 | [diff] [blame] | 5080 | Type *BaseType = Ptr->getType(); |
| 5081 | PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType()); |
| 5082 | if (!BasePointerType) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 5083 | return Error(Loc, "base of getelementptr must be a pointer"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 5084 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 5085 | SmallVector<Value*, 16> Indices; |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 5086 | bool AteExtraComma = false; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 5087 | while (EatIfPresent(lltok::comma)) { |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 5088 | if (Lex.getKind() == lltok::MetadataVar) { |
| 5089 | AteExtraComma = true; |
Devang Patel | 52b1745 | 2009-10-13 18:49:55 +0000 | [diff] [blame] | 5090 | break; |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 5091 | } |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 5092 | if (ParseTypeAndValue(Val, EltLoc, PFS)) return true; |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 5093 | if (!Val->getType()->getScalarType()->isIntegerTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 5094 | return Error(EltLoc, "getelementptr index must be an integer"); |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 5095 | if (Val->getType()->isVectorTy() != Ptr->getType()->isVectorTy()) |
| 5096 | return Error(EltLoc, "getelementptr index type missmatch"); |
| 5097 | if (Val->getType()->isVectorTy()) { |
| 5098 | unsigned ValNumEl = cast<VectorType>(Val->getType())->getNumElements(); |
| 5099 | unsigned PtrNumEl = cast<VectorType>(Ptr->getType())->getNumElements(); |
| 5100 | if (ValNumEl != PtrNumEl) |
| 5101 | return Error(EltLoc, |
| 5102 | "getelementptr vector index has a wrong number of elements"); |
| 5103 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 5104 | Indices.push_back(Val); |
| 5105 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 5106 | |
Eli Bendersky | d980668 | 2013-04-22 17:03:42 +0000 | [diff] [blame] | 5107 | if (!Indices.empty() && !BasePointerType->getElementType()->isSized()) |
| 5108 | return Error(Loc, "base element of getelementptr must be sized"); |
| 5109 | |
| 5110 | if (!GetElementPtrInst::getIndexedType(BaseType, Indices)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 5111 | return Error(Loc, "invalid getelementptr indices"); |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 5112 | Inst = GetElementPtrInst::Create(Ptr, Indices); |
Dan Gohman | 1639c39 | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 5113 | if (InBounds) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 5114 | cast<GetElementPtrInst>(Inst)->setIsInBounds(true); |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 5115 | return AteExtraComma ? InstExtraComma : InstNormal; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 5116 | } |
| 5117 | |
| 5118 | /// ParseExtractValue |
| 5119 | /// ::= 'extractvalue' TypeAndValue (',' uint32)+ |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 5120 | int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 5121 | Value *Val; LocTy Loc; |
| 5122 | SmallVector<unsigned, 4> Indices; |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 5123 | bool AteExtraComma; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 5124 | if (ParseTypeAndValue(Val, Loc, PFS) || |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 5125 | ParseIndexList(Indices, AteExtraComma)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 5126 | return true; |
| 5127 | |
Chris Lattner | 392be58 | 2010-02-12 20:49:41 +0000 | [diff] [blame] | 5128 | if (!Val->getType()->isAggregateType()) |
| 5129 | return Error(Loc, "extractvalue operand must be aggregate type"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 5130 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 5131 | if (!ExtractValueInst::getIndexedType(Val->getType(), Indices)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 5132 | return Error(Loc, "invalid indices for extractvalue"); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 5133 | Inst = ExtractValueInst::Create(Val, Indices); |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 5134 | return AteExtraComma ? InstExtraComma : InstNormal; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 5135 | } |
| 5136 | |
| 5137 | /// ParseInsertValue |
| 5138 | /// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+ |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 5139 | int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 5140 | Value *Val0, *Val1; LocTy Loc0, Loc1; |
| 5141 | SmallVector<unsigned, 4> Indices; |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 5142 | bool AteExtraComma; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 5143 | if (ParseTypeAndValue(Val0, Loc0, PFS) || |
| 5144 | ParseToken(lltok::comma, "expected comma after insertvalue operand") || |
| 5145 | ParseTypeAndValue(Val1, Loc1, PFS) || |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 5146 | ParseIndexList(Indices, AteExtraComma)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 5147 | return true; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 5148 | |
Chris Lattner | 392be58 | 2010-02-12 20:49:41 +0000 | [diff] [blame] | 5149 | if (!Val0->getType()->isAggregateType()) |
| 5150 | return Error(Loc0, "insertvalue operand must be aggregate type"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 5151 | |
David Majnemer | 3007453 | 2015-02-11 07:43:58 +0000 | [diff] [blame] | 5152 | Type *IndexedType = ExtractValueInst::getIndexedType(Val0->getType(), Indices); |
| 5153 | if (!IndexedType) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 5154 | return Error(Loc0, "invalid indices for insertvalue"); |
David Majnemer | 3007453 | 2015-02-11 07:43:58 +0000 | [diff] [blame] | 5155 | if (IndexedType != Val1->getType()) |
| 5156 | return Error(Loc1, "insertvalue operand and field disagree in type: '" + |
| 5157 | getTypeString(Val1->getType()) + "' instead of '" + |
| 5158 | getTypeString(IndexedType) + "'"); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 5159 | Inst = InsertValueInst::Create(Val0, Val1, Indices); |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 5160 | return AteExtraComma ? InstExtraComma : InstNormal; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 5161 | } |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 5162 | |
| 5163 | //===----------------------------------------------------------------------===// |
| 5164 | // Embedded metadata. |
| 5165 | //===----------------------------------------------------------------------===// |
| 5166 | |
| 5167 | /// ParseMDNodeVector |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 5168 | /// ::= { Element (',' Element)* } |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 5169 | /// Element |
| 5170 | /// ::= 'null' | TypeAndValue |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 5171 | bool LLParser::ParseMDNodeVector(SmallVectorImpl<Metadata *> &Elts) { |
David Majnemer | 06f960d | 2014-12-11 20:44:09 +0000 | [diff] [blame] | 5172 | if (ParseToken(lltok::lbrace, "expected '{' here")) |
| 5173 | return true; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 5174 | |
Dan Gohman | 1e0213a | 2010-07-13 19:33:27 +0000 | [diff] [blame] | 5175 | // Check for an empty list. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 5176 | if (EatIfPresent(lltok::rbrace)) |
Dan Gohman | 1e0213a | 2010-07-13 19:33:27 +0000 | [diff] [blame] | 5177 | return false; |
| 5178 | |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 5179 | do { |
Chris Lattner | a01ddfc | 2009-12-30 04:42:57 +0000 | [diff] [blame] | 5180 | // Null is a special case since it is typeless. |
| 5181 | if (EatIfPresent(lltok::kw_null)) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 5182 | Elts.push_back(nullptr); |
Chris Lattner | a01ddfc | 2009-12-30 04:42:57 +0000 | [diff] [blame] | 5183 | continue; |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 5184 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 5185 | |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 5186 | Metadata *MD; |
| 5187 | if (ParseMetadata(MD, nullptr)) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 5188 | return true; |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 5189 | Elts.push_back(MD); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 5190 | } while (EatIfPresent(lltok::comma)); |
| 5191 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 5192 | return ParseToken(lltok::rbrace, "expected end of metadata node"); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 5193 | } |
Duncan P. N. Exon Smith | 0a448fb | 2014-08-19 21:30:15 +0000 | [diff] [blame] | 5194 | |
| 5195 | //===----------------------------------------------------------------------===// |
| 5196 | // Use-list order directives. |
| 5197 | //===----------------------------------------------------------------------===// |
| 5198 | bool LLParser::sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes, |
| 5199 | SMLoc Loc) { |
| 5200 | if (V->use_empty()) |
| 5201 | return Error(Loc, "value has no uses"); |
| 5202 | |
| 5203 | unsigned NumUses = 0; |
| 5204 | SmallDenseMap<const Use *, unsigned, 16> Order; |
| 5205 | for (const Use &U : V->uses()) { |
| 5206 | if (++NumUses > Indexes.size()) |
| 5207 | break; |
| 5208 | Order[&U] = Indexes[NumUses - 1]; |
| 5209 | } |
| 5210 | if (NumUses < 2) |
| 5211 | return Error(Loc, "value only has one use"); |
| 5212 | if (Order.size() != Indexes.size() || NumUses > Indexes.size()) |
| 5213 | return Error(Loc, "wrong number of indexes, expected " + |
| 5214 | Twine(std::distance(V->use_begin(), V->use_end()))); |
| 5215 | |
| 5216 | V->sortUseList([&](const Use &L, const Use &R) { |
| 5217 | return Order.lookup(&L) < Order.lookup(&R); |
| 5218 | }); |
| 5219 | return false; |
| 5220 | } |
| 5221 | |
| 5222 | /// ParseUseListOrderIndexes |
| 5223 | /// ::= '{' uint32 (',' uint32)+ '}' |
| 5224 | bool LLParser::ParseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes) { |
| 5225 | SMLoc Loc = Lex.getLoc(); |
| 5226 | if (ParseToken(lltok::lbrace, "expected '{' here")) |
| 5227 | return true; |
| 5228 | if (Lex.getKind() == lltok::rbrace) |
| 5229 | return Lex.Error("expected non-empty list of uselistorder indexes"); |
| 5230 | |
| 5231 | // Use Offset, Max, and IsOrdered to check consistency of indexes. The |
| 5232 | // indexes should be distinct numbers in the range [0, size-1], and should |
| 5233 | // not be in order. |
| 5234 | unsigned Offset = 0; |
| 5235 | unsigned Max = 0; |
| 5236 | bool IsOrdered = true; |
| 5237 | assert(Indexes.empty() && "Expected empty order vector"); |
| 5238 | do { |
| 5239 | unsigned Index; |
| 5240 | if (ParseUInt32(Index)) |
| 5241 | return true; |
| 5242 | |
| 5243 | // Update consistency checks. |
| 5244 | Offset += Index - Indexes.size(); |
| 5245 | Max = std::max(Max, Index); |
| 5246 | IsOrdered &= Index == Indexes.size(); |
| 5247 | |
| 5248 | Indexes.push_back(Index); |
| 5249 | } while (EatIfPresent(lltok::comma)); |
| 5250 | |
| 5251 | if (ParseToken(lltok::rbrace, "expected '}' here")) |
| 5252 | return true; |
| 5253 | |
| 5254 | if (Indexes.size() < 2) |
| 5255 | return Error(Loc, "expected >= 2 uselistorder indexes"); |
| 5256 | if (Offset != 0 || Max >= Indexes.size()) |
| 5257 | return Error(Loc, "expected distinct uselistorder indexes in range [0, size)"); |
| 5258 | if (IsOrdered) |
| 5259 | return Error(Loc, "expected uselistorder indexes to change the order"); |
| 5260 | |
| 5261 | return false; |
| 5262 | } |
| 5263 | |
| 5264 | /// ParseUseListOrder |
| 5265 | /// ::= 'uselistorder' Type Value ',' UseListOrderIndexes |
| 5266 | bool LLParser::ParseUseListOrder(PerFunctionState *PFS) { |
| 5267 | SMLoc Loc = Lex.getLoc(); |
| 5268 | if (ParseToken(lltok::kw_uselistorder, "expected uselistorder directive")) |
| 5269 | return true; |
| 5270 | |
| 5271 | Value *V; |
| 5272 | SmallVector<unsigned, 16> Indexes; |
| 5273 | if (ParseTypeAndValue(V, PFS) || |
| 5274 | ParseToken(lltok::comma, "expected comma in uselistorder directive") || |
| 5275 | ParseUseListOrderIndexes(Indexes)) |
| 5276 | return true; |
| 5277 | |
| 5278 | return sortUseListOrder(V, Indexes, Loc); |
| 5279 | } |
| 5280 | |
| 5281 | /// ParseUseListOrderBB |
| 5282 | /// ::= 'uselistorder_bb' @foo ',' %bar ',' UseListOrderIndexes |
| 5283 | bool LLParser::ParseUseListOrderBB() { |
| 5284 | assert(Lex.getKind() == lltok::kw_uselistorder_bb); |
| 5285 | SMLoc Loc = Lex.getLoc(); |
| 5286 | Lex.Lex(); |
| 5287 | |
| 5288 | ValID Fn, Label; |
| 5289 | SmallVector<unsigned, 16> Indexes; |
| 5290 | if (ParseValID(Fn) || |
| 5291 | ParseToken(lltok::comma, "expected comma in uselistorder_bb directive") || |
| 5292 | ParseValID(Label) || |
| 5293 | ParseToken(lltok::comma, "expected comma in uselistorder_bb directive") || |
| 5294 | ParseUseListOrderIndexes(Indexes)) |
| 5295 | return true; |
| 5296 | |
| 5297 | // Check the function. |
| 5298 | GlobalValue *GV; |
| 5299 | if (Fn.Kind == ValID::t_GlobalName) |
| 5300 | GV = M->getNamedValue(Fn.StrVal); |
| 5301 | else if (Fn.Kind == ValID::t_GlobalID) |
| 5302 | GV = Fn.UIntVal < NumberedVals.size() ? NumberedVals[Fn.UIntVal] : nullptr; |
| 5303 | else |
| 5304 | return Error(Fn.Loc, "expected function name in uselistorder_bb"); |
| 5305 | if (!GV) |
| 5306 | return Error(Fn.Loc, "invalid function forward reference in uselistorder_bb"); |
| 5307 | auto *F = dyn_cast<Function>(GV); |
| 5308 | if (!F) |
| 5309 | return Error(Fn.Loc, "expected function name in uselistorder_bb"); |
| 5310 | if (F->isDeclaration()) |
| 5311 | return Error(Fn.Loc, "invalid declaration in uselistorder_bb"); |
| 5312 | |
| 5313 | // Check the basic block. |
| 5314 | if (Label.Kind == ValID::t_LocalID) |
| 5315 | return Error(Label.Loc, "invalid numeric label in uselistorder_bb"); |
| 5316 | if (Label.Kind != ValID::t_LocalName) |
| 5317 | return Error(Label.Loc, "expected basic block name in uselistorder_bb"); |
| 5318 | Value *V = F->getValueSymbolTable().lookup(Label.StrVal); |
| 5319 | if (!V) |
| 5320 | return Error(Label.Loc, "invalid basic block in uselistorder_bb"); |
| 5321 | if (!isa<BasicBlock>(V)) |
| 5322 | return Error(Label.Loc, "expected basic block in uselistorder_bb"); |
| 5323 | |
| 5324 | return sortUseListOrder(V, Indexes, Loc); |
| 5325 | } |