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" |
| 19 | #include "llvm/IR/DerivedTypes.h" |
| 20 | #include "llvm/IR/InlineAsm.h" |
| 21 | #include "llvm/IR/Instructions.h" |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 22 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Module.h" |
| 24 | #include "llvm/IR/Operator.h" |
| 25 | #include "llvm/IR/ValueSymbolTable.h" |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Duncan P. N. Exon Smith | 1716990 | 2014-08-19 00:13:19 +0000 | [diff] [blame] | 27 | #include "llvm/Support/SaveAndRestore.h" |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
| 29 | using namespace llvm; |
| 30 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 31 | static std::string getTypeString(Type *T) { |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 32 | std::string Result; |
| 33 | raw_string_ostream Tmp(Result); |
| 34 | Tmp << *T; |
| 35 | return Tmp.str(); |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 38 | /// Run: module ::= toplevelentity* |
Chris Lattner | ad6f335 | 2009-01-04 20:44:11 +0000 | [diff] [blame] | 39 | bool LLParser::Run() { |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 40 | // Prime the lexer. |
| 41 | Lex.Lex(); |
| 42 | |
Chris Lattner | ad6f335 | 2009-01-04 20:44:11 +0000 | [diff] [blame] | 43 | return ParseTopLevelEntities() || |
| 44 | ValidateEndOfModule(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /// ValidateEndOfModule - Do final validity and sanity checks at the end of the |
| 48 | /// module. |
| 49 | bool LLParser::ValidateEndOfModule() { |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 50 | for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++) |
| 51 | UpgradeInstWithTBAATag(InstsWithTBAATag[I]); |
| 52 | |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 53 | // Handle any function attribute group forward references. |
| 54 | for (std::map<Value*, std::vector<unsigned> >::iterator |
| 55 | I = ForwardRefAttrGroups.begin(), E = ForwardRefAttrGroups.end(); |
| 56 | I != E; ++I) { |
| 57 | Value *V = I->first; |
| 58 | std::vector<unsigned> &Vec = I->second; |
| 59 | AttrBuilder B; |
| 60 | |
| 61 | for (std::vector<unsigned>::iterator VI = Vec.begin(), VE = Vec.end(); |
| 62 | VI != VE; ++VI) |
| 63 | B.merge(NumberedAttrBuilders[*VI]); |
| 64 | |
| 65 | if (Function *Fn = dyn_cast<Function>(V)) { |
| 66 | AttributeSet AS = Fn->getAttributes(); |
| 67 | AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex); |
| 68 | AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex, |
| 69 | AS.getFnAttributes()); |
| 70 | |
| 71 | FnAttrs.merge(B); |
| 72 | |
| 73 | // If the alignment was parsed as an attribute, move to the alignment |
| 74 | // field. |
| 75 | if (FnAttrs.hasAlignmentAttr()) { |
| 76 | Fn->setAlignment(FnAttrs.getAlignment()); |
| 77 | FnAttrs.removeAttribute(Attribute::Alignment); |
| 78 | } |
| 79 | |
| 80 | AS = AS.addAttributes(Context, AttributeSet::FunctionIndex, |
| 81 | AttributeSet::get(Context, |
| 82 | AttributeSet::FunctionIndex, |
| 83 | FnAttrs)); |
| 84 | Fn->setAttributes(AS); |
| 85 | } else if (CallInst *CI = dyn_cast<CallInst>(V)) { |
| 86 | AttributeSet AS = CI->getAttributes(); |
| 87 | AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex); |
| 88 | AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex, |
| 89 | AS.getFnAttributes()); |
Bill Wendling | 59dce37 | 2013-02-12 10:13:06 +0000 | [diff] [blame] | 90 | FnAttrs.merge(B); |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 91 | AS = AS.addAttributes(Context, AttributeSet::FunctionIndex, |
| 92 | AttributeSet::get(Context, |
| 93 | AttributeSet::FunctionIndex, |
| 94 | FnAttrs)); |
| 95 | CI->setAttributes(AS); |
| 96 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) { |
| 97 | AttributeSet AS = II->getAttributes(); |
| 98 | AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex); |
| 99 | AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex, |
| 100 | AS.getFnAttributes()); |
Bill Wendling | 59dce37 | 2013-02-12 10:13:06 +0000 | [diff] [blame] | 101 | FnAttrs.merge(B); |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 102 | AS = AS.addAttributes(Context, AttributeSet::FunctionIndex, |
| 103 | AttributeSet::get(Context, |
| 104 | AttributeSet::FunctionIndex, |
| 105 | FnAttrs)); |
| 106 | II->setAttributes(AS); |
| 107 | } else { |
| 108 | llvm_unreachable("invalid object with forward attribute group reference"); |
| 109 | } |
| 110 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 111 | |
Duncan P. N. Exon Smith | 1716990 | 2014-08-19 00:13:19 +0000 | [diff] [blame] | 112 | // If there are entries in ForwardRefBlockAddresses at this point, the |
| 113 | // function was never defined. |
| 114 | if (!ForwardRefBlockAddresses.empty()) |
| 115 | return Error(ForwardRefBlockAddresses.begin()->first.Loc, |
| 116 | "expected function name in blockaddress"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 117 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 118 | for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) |
| 119 | if (NumberedTypes[i].second.isValid()) |
| 120 | return Error(NumberedTypes[i].second, |
| 121 | "use of undefined type '%" + Twine(i) + "'"); |
| 122 | |
| 123 | for (StringMap<std::pair<Type*, LocTy> >::iterator I = |
| 124 | NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) |
| 125 | if (I->second.second.isValid()) |
| 126 | return Error(I->second.second, |
| 127 | "use of undefined type named '" + I->getKey() + "'"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 128 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 129 | if (!ForwardRefComdats.empty()) |
| 130 | return Error(ForwardRefComdats.begin()->second, |
| 131 | "use of undefined comdat '$" + |
| 132 | ForwardRefComdats.begin()->first + "'"); |
| 133 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 134 | if (!ForwardRefVals.empty()) |
| 135 | return Error(ForwardRefVals.begin()->second.second, |
| 136 | "use of undefined value '@" + ForwardRefVals.begin()->first + |
| 137 | "'"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 138 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 139 | if (!ForwardRefValIDs.empty()) |
| 140 | return Error(ForwardRefValIDs.begin()->second.second, |
| 141 | "use of undefined value '@" + |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 142 | Twine(ForwardRefValIDs.begin()->first) + "'"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 143 | |
Devang Patel | d254115 | 2009-07-08 19:23:54 +0000 | [diff] [blame] | 144 | if (!ForwardRefMDNodes.empty()) |
| 145 | return Error(ForwardRefMDNodes.begin()->second.second, |
| 146 | "use of undefined metadata '!" + |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 147 | Twine(ForwardRefMDNodes.begin()->first) + "'"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 148 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 149 | // Resolve metadata cycles. |
| 150 | for (auto &N : NumberedMetadata) |
Duncan P. N. Exon Smith | 118632d | 2015-01-12 20:09:34 +0000 | [diff] [blame] | 151 | if (auto *U = cast_or_null<UniquableMDNode>(N)) |
| 152 | U->resolveCycles(); |
Devang Patel | d254115 | 2009-07-08 19:23:54 +0000 | [diff] [blame] | 153 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 154 | // Look for intrinsic functions and CallInst that need to be upgraded |
| 155 | for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ) |
| 156 | UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 157 | |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 158 | UpgradeDebugInfo(*M); |
| 159 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 160 | return false; |
| 161 | } |
| 162 | |
| 163 | //===----------------------------------------------------------------------===// |
| 164 | // Top-Level Entities |
| 165 | //===----------------------------------------------------------------------===// |
| 166 | |
| 167 | bool LLParser::ParseTopLevelEntities() { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 168 | while (1) { |
| 169 | switch (Lex.getKind()) { |
| 170 | default: return TokError("expected top-level entity"); |
| 171 | case lltok::Eof: return false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 172 | case lltok::kw_declare: if (ParseDeclare()) return true; break; |
| 173 | case lltok::kw_define: if (ParseDefine()) return true; break; |
| 174 | case lltok::kw_module: if (ParseModuleAsm()) return true; break; |
| 175 | case lltok::kw_target: if (ParseTargetDefinition()) return true; break; |
Bill Wendling | 706d3d6 | 2012-11-28 08:41:48 +0000 | [diff] [blame] | 176 | case lltok::kw_deplibs: if (ParseDepLibs()) return true; break; |
Dan Gohman | 466876b | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 177 | case lltok::LocalVarID: if (ParseUnnamedType()) return true; break; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 178 | case lltok::LocalVar: if (ParseNamedType()) return true; break; |
Dan Gohman | 466876b | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 179 | case lltok::GlobalID: if (ParseUnnamedGlobal()) return true; break; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 180 | case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break; |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 181 | case lltok::ComdatVar: if (parseComdat()) return true; break; |
Chris Lattner | 1eed2d6 | 2009-12-30 04:56:59 +0000 | [diff] [blame] | 182 | case lltok::exclaim: if (ParseStandaloneMetadata()) return true; break; |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 183 | case lltok::MetadataVar:if (ParseNamedMetadata()) return true; break; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 184 | |
| 185 | // The Global variable production with no name can have many different |
| 186 | // optional leading prefixes, the production is: |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 187 | // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass |
| 188 | // OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr |
Rafael Espindola | 45e6c19 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 189 | // ('constant'|'global') ... |
Bill Wendling | 03bcd6e | 2010-07-01 21:55:59 +0000 | [diff] [blame] | 190 | case lltok::kw_private: // OptionalLinkage |
Bill Wendling | 03bcd6e | 2010-07-01 21:55:59 +0000 | [diff] [blame] | 191 | case lltok::kw_internal: // OptionalLinkage |
| 192 | case lltok::kw_weak: // OptionalLinkage |
| 193 | case lltok::kw_weak_odr: // OptionalLinkage |
| 194 | case lltok::kw_linkonce: // OptionalLinkage |
| 195 | case lltok::kw_linkonce_odr: // OptionalLinkage |
| 196 | case lltok::kw_appending: // OptionalLinkage |
Bill Wendling | 03bcd6e | 2010-07-01 21:55:59 +0000 | [diff] [blame] | 197 | case lltok::kw_common: // OptionalLinkage |
Bill Wendling | 03bcd6e | 2010-07-01 21:55:59 +0000 | [diff] [blame] | 198 | case lltok::kw_extern_weak: // OptionalLinkage |
Rafael Espindola | 52b7442 | 2014-06-03 20:00:20 +0000 | [diff] [blame] | 199 | case lltok::kw_external: // OptionalLinkage |
| 200 | case lltok::kw_default: // OptionalVisibility |
| 201 | case lltok::kw_hidden: // OptionalVisibility |
| 202 | case lltok::kw_protected: // OptionalVisibility |
Rafael Espindola | 63e92fb | 2014-06-03 20:07:32 +0000 | [diff] [blame] | 203 | case lltok::kw_dllimport: // OptionalDLLStorageClass |
| 204 | case lltok::kw_dllexport: // OptionalDLLStorageClass |
Rafael Espindola | 52b7442 | 2014-06-03 20:00:20 +0000 | [diff] [blame] | 205 | case lltok::kw_thread_local: // OptionalThreadLocal |
| 206 | case lltok::kw_addrspace: // OptionalAddrSpace |
| 207 | case lltok::kw_constant: // GlobalType |
| 208 | case lltok::kw_global: { // GlobalType |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 209 | unsigned Linkage, Visibility, DLLStorageClass; |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 210 | bool UnnamedAddr; |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 211 | GlobalVariable::ThreadLocalMode TLM; |
Rafael Espindola | 52b7442 | 2014-06-03 20:00:20 +0000 | [diff] [blame] | 212 | bool HasLinkage; |
| 213 | if (ParseOptionalLinkage(Linkage, HasLinkage) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 214 | ParseOptionalVisibility(Visibility) || |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 215 | ParseOptionalDLLStorageClass(DLLStorageClass) || |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 216 | ParseOptionalThreadLocal(TLM) || |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 217 | parseOptionalUnnamedAddr(UnnamedAddr) || |
Rafael Espindola | 52b7442 | 2014-06-03 20:00:20 +0000 | [diff] [blame] | 218 | ParseGlobal("", SMLoc(), Linkage, HasLinkage, Visibility, |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 219 | DLLStorageClass, TLM, UnnamedAddr)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 220 | return true; |
| 221 | break; |
| 222 | } |
Bill Wendling | a7c3877 | 2013-02-09 15:48:49 +0000 | [diff] [blame] | 223 | |
| 224 | case lltok::kw_attributes: if (ParseUnnamedAttrGrp()) return true; break; |
Duncan P. N. Exon Smith | 0a448fb | 2014-08-19 21:30:15 +0000 | [diff] [blame] | 225 | case lltok::kw_uselistorder: if (ParseUseListOrder()) return true; break; |
| 226 | case lltok::kw_uselistorder_bb: |
| 227 | if (ParseUseListOrderBB()) return true; break; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | |
| 233 | /// toplevelentity |
| 234 | /// ::= 'module' 'asm' STRINGCONSTANT |
| 235 | bool LLParser::ParseModuleAsm() { |
| 236 | assert(Lex.getKind() == lltok::kw_module); |
| 237 | Lex.Lex(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 238 | |
| 239 | std::string AsmStr; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 240 | if (ParseToken(lltok::kw_asm, "expected 'module asm'") || |
| 241 | ParseStringConstant(AsmStr)) return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 242 | |
Rafael Espindola | 1e49a6d | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 243 | M->appendModuleInlineAsm(AsmStr); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 244 | return false; |
| 245 | } |
| 246 | |
| 247 | /// toplevelentity |
| 248 | /// ::= 'target' 'triple' '=' STRINGCONSTANT |
| 249 | /// ::= 'target' 'datalayout' '=' STRINGCONSTANT |
| 250 | bool LLParser::ParseTargetDefinition() { |
| 251 | assert(Lex.getKind() == lltok::kw_target); |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 252 | std::string Str; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 253 | switch (Lex.Lex()) { |
| 254 | default: return TokError("unknown target property"); |
| 255 | case lltok::kw_triple: |
| 256 | Lex.Lex(); |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 257 | if (ParseToken(lltok::equal, "expected '=' after target triple") || |
| 258 | ParseStringConstant(Str)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 259 | return true; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 260 | M->setTargetTriple(Str); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 261 | return false; |
| 262 | case lltok::kw_datalayout: |
| 263 | Lex.Lex(); |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 264 | if (ParseToken(lltok::equal, "expected '=' after target datalayout") || |
| 265 | ParseStringConstant(Str)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 266 | return true; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 267 | M->setDataLayout(Str); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 268 | return false; |
| 269 | } |
| 270 | } |
| 271 | |
Bill Wendling | 706d3d6 | 2012-11-28 08:41:48 +0000 | [diff] [blame] | 272 | /// toplevelentity |
| 273 | /// ::= 'deplibs' '=' '[' ']' |
| 274 | /// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']' |
| 275 | /// FIXME: Remove in 4.0. Currently parse, but ignore. |
| 276 | bool LLParser::ParseDepLibs() { |
| 277 | assert(Lex.getKind() == lltok::kw_deplibs); |
| 278 | Lex.Lex(); |
| 279 | if (ParseToken(lltok::equal, "expected '=' after deplibs") || |
| 280 | ParseToken(lltok::lsquare, "expected '=' after deplibs")) |
| 281 | return true; |
| 282 | |
| 283 | if (EatIfPresent(lltok::rsquare)) |
| 284 | return false; |
| 285 | |
| 286 | do { |
| 287 | std::string Str; |
| 288 | if (ParseStringConstant(Str)) return true; |
| 289 | } while (EatIfPresent(lltok::comma)); |
| 290 | |
| 291 | return ParseToken(lltok::rsquare, "expected ']' at end of list"); |
| 292 | } |
| 293 | |
Dan Gohman | 466876b | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 294 | /// ParseUnnamedType: |
Dan Gohman | 466876b | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 295 | /// ::= LocalVarID '=' 'type' type |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 296 | bool LLParser::ParseUnnamedType() { |
Chris Lattner | 0703736 | 2011-06-18 23:51:31 +0000 | [diff] [blame] | 297 | LocTy TypeLoc = Lex.getLoc(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 298 | unsigned TypeID = Lex.getUIntVal(); |
Chris Lattner | 8936d2b | 2011-06-19 00:03:46 +0000 | [diff] [blame] | 299 | Lex.Lex(); // eat LocalVarID; |
| 300 | |
| 301 | if (ParseToken(lltok::equal, "expected '=' after name") || |
| 302 | ParseToken(lltok::kw_type, "expected 'type' after '='")) |
| 303 | return true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 304 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 305 | if (TypeID >= NumberedTypes.size()) |
| 306 | NumberedTypes.resize(TypeID+1); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +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. |
Duncan P. N. Exon Smith | a8d9a02 | 2015-01-12 21:14:38 +0000 | [diff] [blame] | 528 | if (MID < NumberedMetadata.size() && NumberedMetadata[MID] != nullptr) { |
| 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 | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 534 | MDNodeFwdDecl *FwdNode = MDNode::getTemporary(Context, None); |
Devang Patel | 8ff0f8d | 2009-07-20 19:00:08 +0000 | [diff] [blame] | 535 | ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc()); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 536 | |
Chris Lattner | fc58af2 | 2009-12-30 04:51:58 +0000 | [diff] [blame] | 537 | if (NumberedMetadata.size() <= MID) |
| 538 | NumberedMetadata.resize(MID+1); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 539 | NumberedMetadata[MID].reset(FwdNode); |
Chris Lattner | 1797fc7 | 2009-12-29 21:53:55 +0000 | [diff] [blame] | 540 | Result = FwdNode; |
Devang Patel | 8ff0f8d | 2009-07-20 19:00:08 +0000 | [diff] [blame] | 541 | return false; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 542 | } |
Devang Patel | 8ff0f8d | 2009-07-20 19:00:08 +0000 | [diff] [blame] | 543 | |
Chris Lattner | f2fe7ff | 2009-12-29 22:35:39 +0000 | [diff] [blame] | 544 | /// ParseNamedMetadata: |
Devang Patel | be62697 | 2009-07-29 00:34:02 +0000 | [diff] [blame] | 545 | /// !foo = !{ !1, !2 } |
| 546 | bool LLParser::ParseNamedMetadata() { |
Chris Lattner | eafe4de | 2009-12-30 05:02:06 +0000 | [diff] [blame] | 547 | assert(Lex.getKind() == lltok::MetadataVar); |
Devang Patel | be62697 | 2009-07-29 00:34:02 +0000 | [diff] [blame] | 548 | std::string Name = Lex.getStrVal(); |
Chris Lattner | eafe4de | 2009-12-30 05:02:06 +0000 | [diff] [blame] | 549 | Lex.Lex(); |
Devang Patel | be62697 | 2009-07-29 00:34:02 +0000 | [diff] [blame] | 550 | |
Chris Lattner | f2fe7ff | 2009-12-29 22:35:39 +0000 | [diff] [blame] | 551 | if (ParseToken(lltok::equal, "expected '=' here") || |
Chris Lattner | 1eed2d6 | 2009-12-30 04:56:59 +0000 | [diff] [blame] | 552 | ParseToken(lltok::exclaim, "Expected '!' here") || |
Chris Lattner | f2fe7ff | 2009-12-29 22:35:39 +0000 | [diff] [blame] | 553 | ParseToken(lltok::lbrace, "Expected '{' here")) |
Devang Patel | be62697 | 2009-07-29 00:34:02 +0000 | [diff] [blame] | 554 | return true; |
| 555 | |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 556 | NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name); |
Dan Gohman | afd69cf | 2010-07-13 19:42:44 +0000 | [diff] [blame] | 557 | if (Lex.getKind() != lltok::rbrace) |
| 558 | do { |
Dan Gohman | afd69cf | 2010-07-13 19:42:44 +0000 | [diff] [blame] | 559 | if (ParseToken(lltok::exclaim, "Expected '!' here")) |
| 560 | return true; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 561 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 562 | MDNode *N = nullptr; |
Dan Gohman | afd69cf | 2010-07-13 19:42:44 +0000 | [diff] [blame] | 563 | if (ParseMDNodeID(N)) return true; |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 564 | NMD->addOperand(N); |
Dan Gohman | afd69cf | 2010-07-13 19:42:44 +0000 | [diff] [blame] | 565 | } while (EatIfPresent(lltok::comma)); |
Devang Patel | be62697 | 2009-07-29 00:34:02 +0000 | [diff] [blame] | 566 | |
| 567 | if (ParseToken(lltok::rbrace, "expected end of metadata node")) |
| 568 | return true; |
| 569 | |
Devang Patel | be62697 | 2009-07-29 00:34:02 +0000 | [diff] [blame] | 570 | return false; |
| 571 | } |
| 572 | |
Devang Patel | 39e64d4 | 2009-07-01 19:21:12 +0000 | [diff] [blame] | 573 | /// ParseStandaloneMetadata: |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 574 | /// !42 = !{...} |
Devang Patel | 39e64d4 | 2009-07-01 19:21:12 +0000 | [diff] [blame] | 575 | bool LLParser::ParseStandaloneMetadata() { |
Chris Lattner | 1eed2d6 | 2009-12-30 04:56:59 +0000 | [diff] [blame] | 576 | assert(Lex.getKind() == lltok::exclaim); |
Devang Patel | 39e64d4 | 2009-07-01 19:21:12 +0000 | [diff] [blame] | 577 | Lex.Lex(); |
| 578 | unsigned MetadataID = 0; |
Devang Patel | 39e64d4 | 2009-07-01 19:21:12 +0000 | [diff] [blame] | 579 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 580 | MDNode *Init; |
Chris Lattner | 278bc95 | 2009-12-29 22:40:21 +0000 | [diff] [blame] | 581 | if (ParseUInt32(MetadataID) || |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 582 | ParseToken(lltok::equal, "expected '=' here")) |
| 583 | return true; |
| 584 | |
| 585 | // Detect common error, from old metadata syntax. |
| 586 | if (Lex.getKind() == lltok::Type) |
| 587 | return TokError("unexpected type in metadata definition"); |
| 588 | |
Duncan P. N. Exon Smith | 090a19b | 2015-01-08 22:38:29 +0000 | [diff] [blame] | 589 | bool IsDistinct = EatIfPresent(lltok::kw_distinct); |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 590 | if (ParseToken(lltok::exclaim, "Expected '!' here") || |
Duncan P. N. Exon Smith | 58ef9d1 | 2015-01-12 21:23:11 +0000 | [diff] [blame] | 591 | ParseMDTuple(Init, IsDistinct)) |
Devang Patel | e059ba6e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 592 | return true; |
| 593 | |
Chris Lattner | fc58af2 | 2009-12-30 04:51:58 +0000 | [diff] [blame] | 594 | // 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] | 595 | auto FI = ForwardRefMDNodes.find(MetadataID); |
Devang Patel | d254115 | 2009-07-08 19:23:54 +0000 | [diff] [blame] | 596 | if (FI != ForwardRefMDNodes.end()) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 597 | auto *Temp = FI->second.first; |
Dan Gohman | 16a5d98 | 2010-08-20 22:02:26 +0000 | [diff] [blame] | 598 | Temp->replaceAllUsesWith(Init); |
| 599 | MDNode::deleteTemporary(Temp); |
Devang Patel | d254115 | 2009-07-08 19:23:54 +0000 | [diff] [blame] | 600 | ForwardRefMDNodes.erase(FI); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 601 | |
Chris Lattner | fc58af2 | 2009-12-30 04:51:58 +0000 | [diff] [blame] | 602 | assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work"); |
| 603 | } else { |
| 604 | if (MetadataID >= NumberedMetadata.size()) |
| 605 | NumberedMetadata.resize(MetadataID+1); |
| 606 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 607 | if (NumberedMetadata[MetadataID] != nullptr) |
Chris Lattner | fc58af2 | 2009-12-30 04:51:58 +0000 | [diff] [blame] | 608 | return TokError("Metadata id is already used"); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 609 | NumberedMetadata[MetadataID].reset(Init); |
Devang Patel | d254115 | 2009-07-08 19:23:54 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Devang Patel | 39e64d4 | 2009-07-01 19:21:12 +0000 | [diff] [blame] | 612 | return false; |
| 613 | } |
| 614 | |
Duncan P. N. Exon Smith | b80de10 | 2014-05-07 22:57:20 +0000 | [diff] [blame] | 615 | static bool isValidVisibilityForLinkage(unsigned V, unsigned L) { |
| 616 | return !GlobalValue::isLocalLinkage((GlobalValue::LinkageTypes)L) || |
| 617 | (GlobalValue::VisibilityTypes)V == GlobalValue::DefaultVisibility; |
| 618 | } |
| 619 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 620 | /// ParseAlias: |
Rafael Espindola | 464fe02 | 2014-07-30 22:51:54 +0000 | [diff] [blame] | 621 | /// ::= GlobalVar '=' OptionalLinkage OptionalVisibility |
| 622 | /// OptionalDLLStorageClass OptionalThreadLocal |
| 623 | /// OptionalUnNammedAddr 'alias' Aliasee |
Rafael Espindola | 6b23863 | 2014-05-16 19:35:39 +0000 | [diff] [blame] | 624 | /// |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 625 | /// Aliasee |
Chris Lattner | 4c73d7a | 2009-04-25 21:26:00 +0000 | [diff] [blame] | 626 | /// ::= TypeAndValue |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 627 | /// |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 628 | /// Everything through OptionalUnNammedAddr has already been parsed. |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 629 | /// |
Rafael Espindola | 464fe02 | 2014-07-30 22:51:54 +0000 | [diff] [blame] | 630 | bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc, unsigned L, |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 631 | unsigned Visibility, unsigned DLLStorageClass, |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 632 | GlobalVariable::ThreadLocalMode TLM, |
| 633 | bool UnnamedAddr) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 634 | assert(Lex.getKind() == lltok::kw_alias); |
| 635 | Lex.Lex(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 636 | |
Rafael Espindola | 7852705 | 2013-10-06 15:10:43 +0000 | [diff] [blame] | 637 | GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L; |
| 638 | |
Rafael Espindola | caa4356 | 2013-10-09 16:07:32 +0000 | [diff] [blame] | 639 | if(!GlobalAlias::isValidLinkage(Linkage)) |
Rafael Espindola | 464fe02 | 2014-07-30 22:51:54 +0000 | [diff] [blame] | 640 | return Error(NameLoc, "invalid linkage type for alias"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 641 | |
Duncan P. N. Exon Smith | b80de10 | 2014-05-07 22:57:20 +0000 | [diff] [blame] | 642 | if (!isValidVisibilityForLinkage(Visibility, L)) |
Rafael Espindola | 464fe02 | 2014-07-30 22:51:54 +0000 | [diff] [blame] | 643 | return Error(NameLoc, |
Duncan P. N. Exon Smith | b80de10 | 2014-05-07 22:57:20 +0000 | [diff] [blame] | 644 | "symbol with local linkage must have default visibility"); |
| 645 | |
Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 646 | Constant *Aliasee; |
| 647 | LocTy AliaseeLoc = Lex.getLoc(); |
| 648 | if (Lex.getKind() != lltok::kw_bitcast && |
| 649 | Lex.getKind() != lltok::kw_getelementptr && |
| 650 | Lex.getKind() != lltok::kw_addrspacecast && |
| 651 | Lex.getKind() != lltok::kw_inttoptr) { |
| 652 | if (ParseGlobalTypeAndValue(Aliasee)) |
Rafael Espindola | 6b23863 | 2014-05-16 19:35:39 +0000 | [diff] [blame] | 653 | return true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 654 | } else { |
Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 655 | // The bitcast dest type is not present, it is implied by the dest type. |
| 656 | ValID ID; |
| 657 | if (ParseValID(ID)) |
| 658 | return true; |
| 659 | if (ID.Kind != ValID::t_Constant) |
| 660 | return Error(AliaseeLoc, "invalid aliasee"); |
| 661 | Aliasee = ID.ConstantVal; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 662 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 663 | |
Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 664 | Type *AliaseeType = Aliasee->getType(); |
| 665 | auto *PTy = dyn_cast<PointerType>(AliaseeType); |
| 666 | if (!PTy) |
| 667 | return Error(AliaseeLoc, "An alias must have pointer type"); |
| 668 | Type *Ty = PTy->getElementType(); |
| 669 | unsigned AddrSpace = PTy->getAddressSpace(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 670 | |
| 671 | // 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] | 672 | std::unique_ptr<GlobalAlias> GA( |
Rafael Espindola | f1bedd374 | 2014-05-17 21:29:57 +0000 | [diff] [blame] | 673 | GlobalAlias::create(Ty, AddrSpace, (GlobalValue::LinkageTypes)Linkage, |
| 674 | Name, Aliasee, /*Parent*/ nullptr)); |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 675 | GA->setThreadLocalMode(TLM); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 676 | GA->setVisibility((GlobalValue::VisibilityTypes)Visibility); |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 677 | GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass); |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 678 | GA->setUnnamedAddr(UnnamedAddr); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 679 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 680 | // See if this value already exists in the symbol table. If so, it is either |
| 681 | // a redefinition or a definition of a forward reference. |
Chris Lattner | e38317f | 2009-10-25 23:22:50 +0000 | [diff] [blame] | 682 | if (GlobalValue *Val = M->getNamedValue(Name)) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 683 | // See if this was a redefinition. If so, there is no entry in |
| 684 | // ForwardRefVals. |
| 685 | std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator |
| 686 | I = ForwardRefVals.find(Name); |
| 687 | if (I == ForwardRefVals.end()) |
| 688 | return Error(NameLoc, "redefinition of global named '@" + Name + "'"); |
| 689 | |
| 690 | // Otherwise, this was a definition of forward ref. Verify that types |
| 691 | // agree. |
| 692 | if (Val->getType() != GA->getType()) |
| 693 | return Error(NameLoc, |
| 694 | "forward reference and definition of alias have different types"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 695 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 696 | // If they agree, just RAUW the old value with the alias and remove the |
| 697 | // forward ref info. |
Rafael Espindola | aa27382 | 2014-05-09 21:49:17 +0000 | [diff] [blame] | 698 | Val->replaceAllUsesWith(GA.get()); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 699 | Val->eraseFromParent(); |
| 700 | ForwardRefVals.erase(I); |
| 701 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 702 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 703 | // Insert into the module, we know its name won't collide now. |
Rafael Espindola | aa27382 | 2014-05-09 21:49:17 +0000 | [diff] [blame] | 704 | M->getAliasList().push_back(GA.get()); |
Benjamin Kramer | 1dc34b4 | 2010-10-16 11:28:23 +0000 | [diff] [blame] | 705 | assert(GA->getName() == Name && "Should not be a name conflict!"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 706 | |
Rafael Espindola | aa27382 | 2014-05-09 21:49:17 +0000 | [diff] [blame] | 707 | // The module owns this now |
| 708 | GA.release(); |
| 709 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 710 | return false; |
| 711 | } |
| 712 | |
| 713 | /// ParseGlobal |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 714 | /// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 715 | /// OptionalThreadLocal OptionalUnNammedAddr OptionalAddrSpace |
Michael Gottesman | 27e7ef3 | 2013-02-05 05:57:38 +0000 | [diff] [blame] | 716 | /// OptionalExternallyInitialized GlobalType Type Const |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 717 | /// ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 718 | /// OptionalThreadLocal OptionalUnNammedAddr OptionalAddrSpace |
Michael Gottesman | 27e7ef3 | 2013-02-05 05:57:38 +0000 | [diff] [blame] | 719 | /// OptionalExternallyInitialized GlobalType Type Const |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 720 | /// |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 721 | /// Everything up to and including OptionalUnNammedAddr has been parsed |
David Majnemer | c4ab61c | 2014-03-09 06:41:58 +0000 | [diff] [blame] | 722 | /// already. |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 723 | /// |
| 724 | bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc, |
| 725 | unsigned Linkage, bool HasLinkage, |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 726 | unsigned Visibility, unsigned DLLStorageClass, |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 727 | GlobalVariable::ThreadLocalMode TLM, |
| 728 | bool UnnamedAddr) { |
Duncan P. N. Exon Smith | b80de10 | 2014-05-07 22:57:20 +0000 | [diff] [blame] | 729 | if (!isValidVisibilityForLinkage(Visibility, Linkage)) |
| 730 | return Error(NameLoc, |
| 731 | "symbol with local linkage must have default visibility"); |
| 732 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 733 | unsigned AddrSpace; |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 734 | bool IsConstant, IsExternallyInitialized; |
Michael Gottesman | 27e7ef3 | 2013-02-05 05:57:38 +0000 | [diff] [blame] | 735 | LocTy IsExternallyInitializedLoc; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 736 | LocTy TyLoc; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 737 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 738 | Type *Ty = nullptr; |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 739 | if (ParseOptionalAddrSpace(AddrSpace) || |
Michael Gottesman | 27e7ef3 | 2013-02-05 05:57:38 +0000 | [diff] [blame] | 740 | ParseOptionalToken(lltok::kw_externally_initialized, |
| 741 | IsExternallyInitialized, |
| 742 | &IsExternallyInitializedLoc) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 743 | ParseGlobalType(IsConstant) || |
| 744 | ParseType(Ty, TyLoc)) |
| 745 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 746 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 747 | // If the linkage is specified and is external, then no initializer is |
| 748 | // present. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 749 | Constant *Init = nullptr; |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 750 | if (!HasLinkage || (Linkage != GlobalValue::ExternalWeakLinkage && |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 751 | Linkage != GlobalValue::ExternalLinkage)) { |
| 752 | if (ParseGlobalValue(Ty, Init)) |
| 753 | return true; |
| 754 | } |
| 755 | |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 756 | if (Ty->isFunctionTy() || Ty->isLabelTy()) |
Chris Lattner | c9e1b48 | 2009-02-08 20:00:15 +0000 | [diff] [blame] | 757 | return Error(TyLoc, "invalid type for global variable"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 758 | |
David Majnemer | 598bd05 | 2014-12-09 05:56:09 +0000 | [diff] [blame] | 759 | GlobalValue *GVal = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 760 | |
| 761 | // See if the global was forward referenced, if so, use the global. |
Chris Lattner | 1f386b8 | 2009-02-02 07:24:28 +0000 | [diff] [blame] | 762 | if (!Name.empty()) { |
David Majnemer | 598bd05 | 2014-12-09 05:56:09 +0000 | [diff] [blame] | 763 | GVal = M->getNamedValue(Name); |
| 764 | if (GVal) { |
Chris Lattner | e38317f | 2009-10-25 23:22:50 +0000 | [diff] [blame] | 765 | if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal)) |
| 766 | return Error(NameLoc, "redefinition of global '@" + Name + "'"); |
Chris Lattner | e38317f | 2009-10-25 23:22:50 +0000 | [diff] [blame] | 767 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 768 | } else { |
| 769 | std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator |
| 770 | I = ForwardRefValIDs.find(NumberedVals.size()); |
| 771 | if (I != ForwardRefValIDs.end()) { |
David Majnemer | 598bd05 | 2014-12-09 05:56:09 +0000 | [diff] [blame] | 772 | GVal = I->second.first; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 773 | ForwardRefValIDs.erase(I); |
| 774 | } |
| 775 | } |
| 776 | |
David Majnemer | 598bd05 | 2014-12-09 05:56:09 +0000 | [diff] [blame] | 777 | GlobalVariable *GV; |
| 778 | if (!GVal) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 779 | GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, nullptr, |
| 780 | Name, nullptr, GlobalVariable::NotThreadLocal, |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 781 | AddrSpace); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 782 | } else { |
David Majnemer | 598bd05 | 2014-12-09 05:56:09 +0000 | [diff] [blame] | 783 | if (GVal->getType()->getElementType() != Ty) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 784 | return Error(TyLoc, |
| 785 | "forward reference and definition of global have different types"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 786 | |
David Majnemer | 598bd05 | 2014-12-09 05:56:09 +0000 | [diff] [blame] | 787 | GV = cast<GlobalVariable>(GVal); |
| 788 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 789 | // Move the forward-reference to the correct spot in the module. |
| 790 | M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV); |
| 791 | } |
| 792 | |
| 793 | if (Name.empty()) |
| 794 | NumberedVals.push_back(GV); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 795 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 796 | // Set the parsed properties on the global. |
| 797 | if (Init) |
| 798 | GV->setInitializer(Init); |
| 799 | GV->setConstant(IsConstant); |
| 800 | GV->setLinkage((GlobalValue::LinkageTypes)Linkage); |
| 801 | GV->setVisibility((GlobalValue::VisibilityTypes)Visibility); |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 802 | GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass); |
Michael Gottesman | 27e7ef3 | 2013-02-05 05:57:38 +0000 | [diff] [blame] | 803 | GV->setExternallyInitialized(IsExternallyInitialized); |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 804 | GV->setThreadLocalMode(TLM); |
Rafael Espindola | 45e6c19 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 805 | GV->setUnnamedAddr(UnnamedAddr); |
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 | // Parse attributes on the global. |
| 808 | while (Lex.getKind() == lltok::comma) { |
| 809 | Lex.Lex(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 810 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 811 | if (Lex.getKind() == lltok::kw_section) { |
| 812 | Lex.Lex(); |
| 813 | GV->setSection(Lex.getStrVal()); |
| 814 | if (ParseToken(lltok::StringConstant, "expected global section string")) |
| 815 | return true; |
| 816 | } else if (Lex.getKind() == lltok::kw_align) { |
| 817 | unsigned Alignment; |
| 818 | if (ParseOptionalAlignment(Alignment)) return true; |
| 819 | GV->setAlignment(Alignment); |
| 820 | } else { |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 821 | Comdat *C; |
Rafael Espindola | 83a362c | 2015-01-06 22:55:16 +0000 | [diff] [blame] | 822 | if (parseOptionalComdat(Name, C)) |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 823 | return true; |
| 824 | if (C) |
| 825 | GV->setComdat(C); |
| 826 | else |
| 827 | return TokError("unknown global variable property!"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 828 | } |
| 829 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 830 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 831 | return false; |
| 832 | } |
| 833 | |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 834 | /// ParseUnnamedAttrGrp |
Bill Wendling | a7c3877 | 2013-02-09 15:48:49 +0000 | [diff] [blame] | 835 | /// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}' |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 836 | bool LLParser::ParseUnnamedAttrGrp() { |
Bill Wendling | a7c3877 | 2013-02-09 15:48:49 +0000 | [diff] [blame] | 837 | assert(Lex.getKind() == lltok::kw_attributes); |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 838 | LocTy AttrGrpLoc = Lex.getLoc(); |
Bill Wendling | a7c3877 | 2013-02-09 15:48:49 +0000 | [diff] [blame] | 839 | Lex.Lex(); |
| 840 | |
David Majnemer | b39e22b | 2014-12-09 18:33:57 +0000 | [diff] [blame] | 841 | if (Lex.getKind() != lltok::AttrGrpID) |
| 842 | return TokError("expected attribute group id"); |
| 843 | |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 844 | unsigned VarID = Lex.getUIntVal(); |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 845 | std::vector<unsigned> unused; |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 846 | LocTy BuiltinLoc; |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 847 | Lex.Lex(); |
| 848 | |
| 849 | if (ParseToken(lltok::equal, "expected '=' here") || |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 850 | ParseToken(lltok::lbrace, "expected '{' here") || |
Bill Wendling | 09bd1f7 | 2013-02-22 00:12:35 +0000 | [diff] [blame] | 851 | ParseFnAttributeValuePairs(NumberedAttrBuilders[VarID], unused, true, |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 852 | BuiltinLoc) || |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 853 | ParseToken(lltok::rbrace, "expected end of attribute group")) |
| 854 | return true; |
| 855 | |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 856 | if (!NumberedAttrBuilders[VarID].hasAttributes()) |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 857 | return Error(AttrGrpLoc, "attribute group has no attributes"); |
| 858 | |
| 859 | return false; |
| 860 | } |
| 861 | |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 862 | /// ParseFnAttributeValuePairs |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 863 | /// ::= <attr> | <attr> '=' <value> |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 864 | bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B, |
| 865 | std::vector<unsigned> &FwdRefAttrGrps, |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 866 | bool inAttrGrp, LocTy &BuiltinLoc) { |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 867 | bool HaveError = false; |
| 868 | |
| 869 | B.clear(); |
| 870 | |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 871 | while (true) { |
| 872 | lltok::Kind Token = Lex.getKind(); |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 873 | if (Token == lltok::kw_builtin) |
| 874 | BuiltinLoc = Lex.getLoc(); |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 875 | switch (Token) { |
| 876 | default: |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 877 | if (!inAttrGrp) return HaveError; |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 878 | return Error(Lex.getLoc(), "unterminated attribute group"); |
| 879 | case lltok::rbrace: |
| 880 | // Finished. |
| 881 | return false; |
| 882 | |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 883 | case lltok::AttrGrpID: { |
| 884 | // Allow a function to reference an attribute group: |
| 885 | // |
| 886 | // define void @foo() #1 { ... } |
| 887 | if (inAttrGrp) |
| 888 | HaveError |= |
| 889 | Error(Lex.getLoc(), |
| 890 | "cannot have an attribute group reference in an attribute group"); |
| 891 | |
| 892 | unsigned AttrGrpNum = Lex.getUIntVal(); |
| 893 | if (inAttrGrp) break; |
| 894 | |
| 895 | // Save the reference to the attribute group. We'll fill it in later. |
| 896 | FwdRefAttrGrps.push_back(AttrGrpNum); |
| 897 | break; |
| 898 | } |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 899 | // Target-dependent attributes: |
| 900 | case lltok::StringConstant: { |
| 901 | std::string Attr = Lex.getStrVal(); |
| 902 | Lex.Lex(); |
| 903 | std::string Val; |
| 904 | if (EatIfPresent(lltok::equal) && |
| 905 | ParseStringConstant(Val)) |
| 906 | return true; |
| 907 | |
| 908 | B.addAttribute(Attr, Val); |
Bill Wendling | b1ea980 | 2013-02-10 10:12:50 +0000 | [diff] [blame] | 909 | continue; |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | // Target-independent attributes: |
| 913 | case lltok::kw_align: { |
Bill Wendling | c62789f | 2013-04-18 18:30:16 +0000 | [diff] [blame] | 914 | // As a hack, we allow function alignment to be initially parsed as an |
| 915 | // attribute on a function declaration/definition or added to an attribute |
| 916 | // group and later moved to the alignment field. |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 917 | unsigned Alignment; |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 918 | if (inAttrGrp) { |
Bill Wendling | 44b08bf | 2013-02-10 23:15:51 +0000 | [diff] [blame] | 919 | Lex.Lex(); |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 920 | if (ParseToken(lltok::equal, "expected '=' here") || |
| 921 | ParseUInt32(Alignment)) |
| 922 | return true; |
| 923 | } else { |
| 924 | if (ParseOptionalAlignment(Alignment)) |
| 925 | return true; |
| 926 | } |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 927 | B.addAlignmentAttr(Alignment); |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 928 | continue; |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 929 | } |
| 930 | case lltok::kw_alignstack: { |
| 931 | unsigned Alignment; |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 932 | if (inAttrGrp) { |
Bill Wendling | 44b08bf | 2013-02-10 23:15:51 +0000 | [diff] [blame] | 933 | Lex.Lex(); |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 934 | if (ParseToken(lltok::equal, "expected '=' here") || |
| 935 | ParseUInt32(Alignment)) |
| 936 | return true; |
| 937 | } else { |
| 938 | if (ParseOptionalStackAlignment(Alignment)) |
| 939 | return true; |
| 940 | } |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 941 | B.addStackAlignmentAttr(Alignment); |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 942 | continue; |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 943 | } |
Kostya Serebryany | cf880b9 | 2013-02-26 06:58:09 +0000 | [diff] [blame] | 944 | case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break; |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 945 | case lltok::kw_builtin: B.addAttribute(Attribute::Builtin); break; |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 946 | case lltok::kw_cold: B.addAttribute(Attribute::Cold); break; |
Kostya Serebryany | cf880b9 | 2013-02-26 06:58:09 +0000 | [diff] [blame] | 947 | case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break; |
Tom Roeder | 44cb65f | 2014-06-05 19:29:43 +0000 | [diff] [blame] | 948 | case lltok::kw_jumptable: B.addAttribute(Attribute::JumpTable); break; |
Kostya Serebryany | cf880b9 | 2013-02-26 06:58:09 +0000 | [diff] [blame] | 949 | case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break; |
| 950 | case lltok::kw_naked: B.addAttribute(Attribute::Naked); break; |
| 951 | case lltok::kw_nobuiltin: B.addAttribute(Attribute::NoBuiltin); break; |
| 952 | case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break; |
| 953 | case lltok::kw_noimplicitfloat: B.addAttribute(Attribute::NoImplicitFloat); break; |
| 954 | case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break; |
| 955 | case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break; |
| 956 | case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break; |
| 957 | case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break; |
| 958 | case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break; |
Andrea Di Biagio | 377496b | 2013-08-23 11:53:55 +0000 | [diff] [blame] | 959 | case lltok::kw_optnone: B.addAttribute(Attribute::OptimizeNone); break; |
Kostya Serebryany | cf880b9 | 2013-02-26 06:58:09 +0000 | [diff] [blame] | 960 | case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break; |
| 961 | case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break; |
| 962 | case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break; |
| 963 | case lltok::kw_returns_twice: B.addAttribute(Attribute::ReturnsTwice); break; |
| 964 | case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break; |
| 965 | case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break; |
| 966 | case lltok::kw_sspstrong: B.addAttribute(Attribute::StackProtectStrong); break; |
| 967 | case lltok::kw_sanitize_address: B.addAttribute(Attribute::SanitizeAddress); break; |
| 968 | case lltok::kw_sanitize_thread: B.addAttribute(Attribute::SanitizeThread); break; |
| 969 | case lltok::kw_sanitize_memory: B.addAttribute(Attribute::SanitizeMemory); break; |
| 970 | case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break; |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 971 | |
| 972 | // Error handling. |
| 973 | case lltok::kw_inreg: |
| 974 | case lltok::kw_signext: |
| 975 | case lltok::kw_zeroext: |
| 976 | HaveError |= |
| 977 | Error(Lex.getLoc(), |
| 978 | "invalid use of attribute on a function"); |
| 979 | break; |
| 980 | case lltok::kw_byval: |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 981 | case lltok::kw_dereferenceable: |
Reid Kleckner | a534a38 | 2013-12-19 02:14:12 +0000 | [diff] [blame] | 982 | case lltok::kw_inalloca: |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 983 | case lltok::kw_nest: |
| 984 | case lltok::kw_noalias: |
| 985 | case lltok::kw_nocapture: |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 986 | case lltok::kw_nonnull: |
Stephen Lin | b8bd232 | 2013-04-20 05:14:40 +0000 | [diff] [blame] | 987 | case lltok::kw_returned: |
Bill Wendling | 8b0321d | 2013-02-08 00:52:31 +0000 | [diff] [blame] | 988 | case lltok::kw_sret: |
| 989 | HaveError |= |
| 990 | Error(Lex.getLoc(), |
| 991 | "invalid use of parameter-only attribute on a function"); |
| 992 | break; |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | Lex.Lex(); |
| 996 | } |
| 997 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 998 | |
| 999 | //===----------------------------------------------------------------------===// |
| 1000 | // GlobalValue Reference/Resolution Routines. |
| 1001 | //===----------------------------------------------------------------------===// |
| 1002 | |
| 1003 | /// GetGlobalVal - Get a value with the specified name or ID, creating a |
| 1004 | /// forward reference record if needed. This can return null if the value |
| 1005 | /// exists but does not have the right type. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1006 | GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty, |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1007 | LocTy Loc) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1008 | PointerType *PTy = dyn_cast<PointerType>(Ty); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1009 | if (!PTy) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1010 | Error(Loc, "global variable reference must have pointer type"); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1011 | return nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1012 | } |
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 | // Look this name up in the normal function symbol table. |
| 1015 | GlobalValue *Val = |
| 1016 | cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name)); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1017 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1018 | // If this is a forward reference for the value, see if we already created a |
| 1019 | // forward ref record. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1020 | if (!Val) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1021 | std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator |
| 1022 | I = ForwardRefVals.find(Name); |
| 1023 | if (I != ForwardRefVals.end()) |
| 1024 | Val = I->second.first; |
| 1025 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1026 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1027 | // If we have the value in the symbol table or fwd-ref table, return it. |
| 1028 | if (Val) { |
| 1029 | if (Val->getType() == Ty) return Val; |
| 1030 | Error(Loc, "'@" + Name + "' defined with type '" + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 1031 | getTypeString(Val->getType()) + "'"); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1032 | return nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1033 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1034 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1035 | // Otherwise, create a new forward reference for this value and remember it. |
| 1036 | GlobalValue *FwdVal; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1037 | if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType())) |
Duncan Sands | e288105 | 2009-03-11 08:08:06 +0000 | [diff] [blame] | 1038 | FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1039 | else |
Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 1040 | FwdVal = new GlobalVariable(*M, PTy->getElementType(), false, |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1041 | GlobalValue::ExternalWeakLinkage, nullptr, Name, |
| 1042 | nullptr, GlobalVariable::NotThreadLocal, |
Justin Holewinski | 898a0a0 | 2012-11-16 21:03:47 +0000 | [diff] [blame] | 1043 | PTy->getAddressSpace()); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1044 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1045 | ForwardRefVals[Name] = std::make_pair(FwdVal, Loc); |
| 1046 | return FwdVal; |
| 1047 | } |
| 1048 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1049 | GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) { |
| 1050 | PointerType *PTy = dyn_cast<PointerType>(Ty); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1051 | if (!PTy) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1052 | Error(Loc, "global variable reference must have pointer type"); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1053 | return nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1054 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1055 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1056 | GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1057 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1058 | // If this is a forward reference for the value, see if we already created a |
| 1059 | // forward ref record. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1060 | if (!Val) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1061 | std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator |
| 1062 | I = ForwardRefValIDs.find(ID); |
| 1063 | if (I != ForwardRefValIDs.end()) |
| 1064 | Val = I->second.first; |
| 1065 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1066 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1067 | // If we have the value in the symbol table or fwd-ref table, return it. |
| 1068 | if (Val) { |
| 1069 | if (Val->getType() == Ty) return Val; |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 1070 | Error(Loc, "'@" + Twine(ID) + "' defined with type '" + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 1071 | getTypeString(Val->getType()) + "'"); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1072 | return nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1073 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1074 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1075 | // Otherwise, create a new forward reference for this value and remember it. |
| 1076 | GlobalValue *FwdVal; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1077 | if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType())) |
Duncan Sands | e288105 | 2009-03-11 08:08:06 +0000 | [diff] [blame] | 1078 | FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1079 | else |
Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 1080 | FwdVal = new GlobalVariable(*M, PTy->getElementType(), false, |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1081 | GlobalValue::ExternalWeakLinkage, nullptr, ""); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1082 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1083 | ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc); |
| 1084 | return FwdVal; |
| 1085 | } |
| 1086 | |
| 1087 | |
| 1088 | //===----------------------------------------------------------------------===// |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1089 | // Comdat Reference/Resolution Routines. |
| 1090 | //===----------------------------------------------------------------------===// |
| 1091 | |
| 1092 | Comdat *LLParser::getComdat(const std::string &Name, LocTy Loc) { |
| 1093 | // Look this name up in the comdat symbol table. |
| 1094 | Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable(); |
| 1095 | Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name); |
| 1096 | if (I != ComdatSymTab.end()) |
| 1097 | return &I->second; |
| 1098 | |
| 1099 | // Otherwise, create a new forward reference for this value and remember it. |
| 1100 | Comdat *C = M->getOrInsertComdat(Name); |
| 1101 | ForwardRefComdats[Name] = Loc; |
| 1102 | return C; |
| 1103 | } |
| 1104 | |
| 1105 | |
| 1106 | //===----------------------------------------------------------------------===// |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1107 | // Helper Routines. |
| 1108 | //===----------------------------------------------------------------------===// |
| 1109 | |
| 1110 | /// ParseToken - If the current token has the specified kind, eat it and return |
| 1111 | /// success. Otherwise, emit the specified error and return failure. |
| 1112 | bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) { |
| 1113 | if (Lex.getKind() != T) |
| 1114 | return TokError(ErrMsg); |
| 1115 | Lex.Lex(); |
| 1116 | return false; |
| 1117 | } |
| 1118 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1119 | /// ParseStringConstant |
| 1120 | /// ::= StringConstant |
| 1121 | bool LLParser::ParseStringConstant(std::string &Result) { |
| 1122 | if (Lex.getKind() != lltok::StringConstant) |
| 1123 | return TokError("expected string constant"); |
| 1124 | Result = Lex.getStrVal(); |
| 1125 | Lex.Lex(); |
| 1126 | return false; |
| 1127 | } |
| 1128 | |
| 1129 | /// ParseUInt32 |
| 1130 | /// ::= uint32 |
| 1131 | bool LLParser::ParseUInt32(unsigned &Val) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1132 | if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned()) |
| 1133 | return TokError("expected integer"); |
| 1134 | uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1); |
| 1135 | if (Val64 != unsigned(Val64)) |
| 1136 | return TokError("expected 32-bit integer (too large)"); |
| 1137 | Val = Val64; |
| 1138 | Lex.Lex(); |
| 1139 | return false; |
| 1140 | } |
| 1141 | |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1142 | /// ParseUInt64 |
| 1143 | /// ::= uint64 |
| 1144 | bool LLParser::ParseUInt64(uint64_t &Val) { |
| 1145 | if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned()) |
| 1146 | return TokError("expected integer"); |
| 1147 | Val = Lex.getAPSIntVal().getLimitedValue(); |
| 1148 | Lex.Lex(); |
| 1149 | return false; |
| 1150 | } |
| 1151 | |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 1152 | /// ParseTLSModel |
| 1153 | /// := 'localdynamic' |
| 1154 | /// := 'initialexec' |
| 1155 | /// := 'localexec' |
| 1156 | bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) { |
| 1157 | switch (Lex.getKind()) { |
| 1158 | default: |
| 1159 | return TokError("expected localdynamic, initialexec or localexec"); |
| 1160 | case lltok::kw_localdynamic: |
| 1161 | TLM = GlobalVariable::LocalDynamicTLSModel; |
| 1162 | break; |
| 1163 | case lltok::kw_initialexec: |
| 1164 | TLM = GlobalVariable::InitialExecTLSModel; |
| 1165 | break; |
| 1166 | case lltok::kw_localexec: |
| 1167 | TLM = GlobalVariable::LocalExecTLSModel; |
| 1168 | break; |
| 1169 | } |
| 1170 | |
| 1171 | Lex.Lex(); |
| 1172 | return false; |
| 1173 | } |
| 1174 | |
| 1175 | /// ParseOptionalThreadLocal |
| 1176 | /// := /*empty*/ |
| 1177 | /// := 'thread_local' |
| 1178 | /// := 'thread_local' '(' tlsmodel ')' |
| 1179 | bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) { |
| 1180 | TLM = GlobalVariable::NotThreadLocal; |
| 1181 | if (!EatIfPresent(lltok::kw_thread_local)) |
| 1182 | return false; |
| 1183 | |
| 1184 | TLM = GlobalVariable::GeneralDynamicTLSModel; |
| 1185 | if (Lex.getKind() == lltok::lparen) { |
| 1186 | Lex.Lex(); |
| 1187 | return ParseTLSModel(TLM) || |
| 1188 | ParseToken(lltok::rparen, "expected ')' after thread local model"); |
| 1189 | } |
| 1190 | return false; |
| 1191 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1192 | |
| 1193 | /// ParseOptionalAddrSpace |
| 1194 | /// := /*empty*/ |
| 1195 | /// := 'addrspace' '(' uint32 ')' |
| 1196 | bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) { |
| 1197 | AddrSpace = 0; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1198 | if (!EatIfPresent(lltok::kw_addrspace)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1199 | return false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1200 | return ParseToken(lltok::lparen, "expected '(' in address space") || |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1201 | ParseUInt32(AddrSpace) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1202 | ParseToken(lltok::rparen, "expected ')' in address space"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1203 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1204 | |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 1205 | /// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes. |
| 1206 | bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) { |
| 1207 | bool HaveError = false; |
| 1208 | |
| 1209 | B.clear(); |
| 1210 | |
| 1211 | while (1) { |
| 1212 | lltok::Kind Token = Lex.getKind(); |
| 1213 | switch (Token) { |
| 1214 | default: // End of attributes. |
| 1215 | return HaveError; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1216 | case lltok::kw_align: { |
| 1217 | unsigned Alignment; |
| 1218 | if (ParseOptionalAlignment(Alignment)) |
| 1219 | return true; |
Bill Wendling | 68d2401 | 2012-10-08 22:20:14 +0000 | [diff] [blame] | 1220 | B.addAlignmentAttr(Alignment); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1221 | continue; |
| 1222 | } |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1223 | case lltok::kw_byval: B.addAttribute(Attribute::ByVal); break; |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1224 | case lltok::kw_dereferenceable: { |
| 1225 | uint64_t Bytes; |
| 1226 | if (ParseOptionalDereferenceableBytes(Bytes)) |
| 1227 | return true; |
| 1228 | B.addDereferenceableAttr(Bytes); |
| 1229 | continue; |
| 1230 | } |
Reid Kleckner | a534a38 | 2013-12-19 02:14:12 +0000 | [diff] [blame] | 1231 | case lltok::kw_inalloca: B.addAttribute(Attribute::InAlloca); break; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1232 | case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break; |
| 1233 | case lltok::kw_nest: B.addAttribute(Attribute::Nest); break; |
| 1234 | case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break; |
| 1235 | case lltok::kw_nocapture: B.addAttribute(Attribute::NoCapture); break; |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 1236 | case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break; |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1237 | case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break; |
| 1238 | case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break; |
Stephen Lin | b8bd232 | 2013-04-20 05:14:40 +0000 | [diff] [blame] | 1239 | case lltok::kw_returned: B.addAttribute(Attribute::Returned); break; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1240 | case lltok::kw_signext: B.addAttribute(Attribute::SExt); break; |
| 1241 | case lltok::kw_sret: B.addAttribute(Attribute::StructRet); break; |
| 1242 | case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break; |
Charles Davis | be5557e | 2010-02-12 00:31:15 +0000 | [diff] [blame] | 1243 | |
Stephen Lin | 7577ed5 | 2013-04-20 13:16:13 +0000 | [diff] [blame] | 1244 | case lltok::kw_alignstack: |
| 1245 | case lltok::kw_alwaysinline: |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 1246 | case lltok::kw_builtin: |
Stephen Lin | 7577ed5 | 2013-04-20 13:16:13 +0000 | [diff] [blame] | 1247 | case lltok::kw_inlinehint: |
Tom Roeder | 44cb65f | 2014-06-05 19:29:43 +0000 | [diff] [blame] | 1248 | case lltok::kw_jumptable: |
Stephen Lin | 7577ed5 | 2013-04-20 13:16:13 +0000 | [diff] [blame] | 1249 | case lltok::kw_minsize: |
| 1250 | case lltok::kw_naked: |
| 1251 | case lltok::kw_nobuiltin: |
| 1252 | case lltok::kw_noduplicate: |
| 1253 | case lltok::kw_noimplicitfloat: |
| 1254 | case lltok::kw_noinline: |
| 1255 | case lltok::kw_nonlazybind: |
| 1256 | case lltok::kw_noredzone: |
| 1257 | case lltok::kw_noreturn: |
| 1258 | case lltok::kw_nounwind: |
Andrea Di Biagio | 377496b | 2013-08-23 11:53:55 +0000 | [diff] [blame] | 1259 | case lltok::kw_optnone: |
Stephen Lin | 7577ed5 | 2013-04-20 13:16:13 +0000 | [diff] [blame] | 1260 | case lltok::kw_optsize: |
Stephen Lin | 7577ed5 | 2013-04-20 13:16:13 +0000 | [diff] [blame] | 1261 | case lltok::kw_returns_twice: |
| 1262 | case lltok::kw_sanitize_address: |
| 1263 | case lltok::kw_sanitize_memory: |
| 1264 | case lltok::kw_sanitize_thread: |
| 1265 | case lltok::kw_ssp: |
| 1266 | case lltok::kw_sspreq: |
| 1267 | case lltok::kw_sspstrong: |
| 1268 | case lltok::kw_uwtable: |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 1269 | HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute"); |
| 1270 | break; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1271 | } |
Bill Wendling | 0be9c40 | 2012-09-28 22:30:18 +0000 | [diff] [blame] | 1272 | |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 1273 | Lex.Lex(); |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | /// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes. |
| 1278 | bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) { |
| 1279 | bool HaveError = false; |
| 1280 | |
| 1281 | B.clear(); |
| 1282 | |
| 1283 | while (1) { |
| 1284 | lltok::Kind Token = Lex.getKind(); |
Bill Wendling | 0be9c40 | 2012-09-28 22:30:18 +0000 | [diff] [blame] | 1285 | switch (Token) { |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 1286 | default: // End of attributes. |
| 1287 | return HaveError; |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1288 | case lltok::kw_dereferenceable: { |
| 1289 | uint64_t Bytes; |
| 1290 | if (ParseOptionalDereferenceableBytes(Bytes)) |
| 1291 | return true; |
| 1292 | B.addDereferenceableAttr(Bytes); |
| 1293 | continue; |
| 1294 | } |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1295 | case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break; |
| 1296 | case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break; |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 1297 | case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1298 | case lltok::kw_signext: B.addAttribute(Attribute::SExt); break; |
| 1299 | case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break; |
Bill Wendling | 0be9c40 | 2012-09-28 22:30:18 +0000 | [diff] [blame] | 1300 | |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 1301 | // Error handling. |
Stephen Lin | 7577ed5 | 2013-04-20 13:16:13 +0000 | [diff] [blame] | 1302 | case lltok::kw_align: |
Chandler Carruth | 9f6b59a | 2013-04-09 19:46:46 +0000 | [diff] [blame] | 1303 | case lltok::kw_byval: |
Reid Kleckner | a534a38 | 2013-12-19 02:14:12 +0000 | [diff] [blame] | 1304 | case lltok::kw_inalloca: |
Chandler Carruth | 9f6b59a | 2013-04-09 19:46:46 +0000 | [diff] [blame] | 1305 | case lltok::kw_nest: |
| 1306 | case lltok::kw_nocapture: |
Stephen Lin | b8bd232 | 2013-04-20 05:14:40 +0000 | [diff] [blame] | 1307 | case lltok::kw_returned: |
Chandler Carruth | 9f6b59a | 2013-04-09 19:46:46 +0000 | [diff] [blame] | 1308 | case lltok::kw_sret: |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 1309 | HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute"); |
Bill Wendling | 0be9c40 | 2012-09-28 22:30:18 +0000 | [diff] [blame] | 1310 | break; |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 1311 | |
Chandler Carruth | 9f6b59a | 2013-04-09 19:46:46 +0000 | [diff] [blame] | 1312 | case lltok::kw_alignstack: |
| 1313 | case lltok::kw_alwaysinline: |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 1314 | case lltok::kw_builtin: |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 1315 | case lltok::kw_cold: |
Chandler Carruth | 9f6b59a | 2013-04-09 19:46:46 +0000 | [diff] [blame] | 1316 | case lltok::kw_inlinehint: |
Tom Roeder | 44cb65f | 2014-06-05 19:29:43 +0000 | [diff] [blame] | 1317 | case lltok::kw_jumptable: |
Chandler Carruth | 9f6b59a | 2013-04-09 19:46:46 +0000 | [diff] [blame] | 1318 | case lltok::kw_minsize: |
| 1319 | case lltok::kw_naked: |
| 1320 | case lltok::kw_nobuiltin: |
| 1321 | case lltok::kw_noduplicate: |
| 1322 | case lltok::kw_noimplicitfloat: |
| 1323 | case lltok::kw_noinline: |
| 1324 | case lltok::kw_nonlazybind: |
| 1325 | case lltok::kw_noredzone: |
| 1326 | case lltok::kw_noreturn: |
| 1327 | case lltok::kw_nounwind: |
Andrea Di Biagio | 377496b | 2013-08-23 11:53:55 +0000 | [diff] [blame] | 1328 | case lltok::kw_optnone: |
Chandler Carruth | 9f6b59a | 2013-04-09 19:46:46 +0000 | [diff] [blame] | 1329 | case lltok::kw_optsize: |
Chandler Carruth | 9f6b59a | 2013-04-09 19:46:46 +0000 | [diff] [blame] | 1330 | case lltok::kw_returns_twice: |
| 1331 | case lltok::kw_sanitize_address: |
| 1332 | case lltok::kw_sanitize_memory: |
| 1333 | case lltok::kw_sanitize_thread: |
| 1334 | case lltok::kw_ssp: |
| 1335 | case lltok::kw_sspreq: |
| 1336 | case lltok::kw_sspstrong: |
| 1337 | case lltok::kw_uwtable: |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 1338 | HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute"); |
Bill Wendling | 0be9c40 | 2012-09-28 22:30:18 +0000 | [diff] [blame] | 1339 | break; |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1340 | |
| 1341 | case lltok::kw_readnone: |
| 1342 | case lltok::kw_readonly: |
| 1343 | HaveError |= Error(Lex.getLoc(), "invalid use of attribute on return type"); |
Bill Wendling | 0be9c40 | 2012-09-28 22:30:18 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1346 | Lex.Lex(); |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | /// ParseOptionalLinkage |
| 1351 | /// ::= /*empty*/ |
Rafael Espindola | 6de96a1 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 1352 | /// ::= 'private' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1353 | /// ::= 'internal' |
| 1354 | /// ::= 'weak' |
Duncan Sands | 12da8ce | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 1355 | /// ::= 'weak_odr' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1356 | /// ::= 'linkonce' |
Duncan Sands | 12da8ce | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 1357 | /// ::= 'linkonce_odr' |
Bill Wendling | 03bcd6e | 2010-07-01 21:55:59 +0000 | [diff] [blame] | 1358 | /// ::= 'available_externally' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1359 | /// ::= 'appending' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1360 | /// ::= 'common' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1361 | /// ::= 'extern_weak' |
| 1362 | /// ::= 'external' |
| 1363 | bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) { |
| 1364 | HasLinkage = false; |
| 1365 | switch (Lex.getKind()) { |
Bill Wendling | a3c6f6b | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 1366 | default: Res=GlobalValue::ExternalLinkage; return false; |
| 1367 | case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break; |
Bill Wendling | a3c6f6b | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 1368 | case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break; |
| 1369 | case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break; |
| 1370 | case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break; |
| 1371 | case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break; |
| 1372 | case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break; |
Chris Lattner | 184f1be | 2009-04-13 05:44:34 +0000 | [diff] [blame] | 1373 | case lltok::kw_available_externally: |
| 1374 | Res = GlobalValue::AvailableExternallyLinkage; |
| 1375 | break; |
Bill Wendling | a3c6f6b | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 1376 | case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break; |
Bill Wendling | a3c6f6b | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 1377 | case lltok::kw_common: Res = GlobalValue::CommonLinkage; break; |
Bill Wendling | a3c6f6b | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 1378 | case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break; |
| 1379 | case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1380 | } |
| 1381 | Lex.Lex(); |
| 1382 | HasLinkage = true; |
| 1383 | return false; |
| 1384 | } |
| 1385 | |
| 1386 | /// ParseOptionalVisibility |
| 1387 | /// ::= /*empty*/ |
| 1388 | /// ::= 'default' |
| 1389 | /// ::= 'hidden' |
| 1390 | /// ::= 'protected' |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1391 | /// |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1392 | bool LLParser::ParseOptionalVisibility(unsigned &Res) { |
| 1393 | switch (Lex.getKind()) { |
| 1394 | default: Res = GlobalValue::DefaultVisibility; return false; |
| 1395 | case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break; |
| 1396 | case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break; |
| 1397 | case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break; |
| 1398 | } |
| 1399 | Lex.Lex(); |
| 1400 | return false; |
| 1401 | } |
| 1402 | |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 1403 | /// ParseOptionalDLLStorageClass |
| 1404 | /// ::= /*empty*/ |
| 1405 | /// ::= 'dllimport' |
| 1406 | /// ::= 'dllexport' |
| 1407 | /// |
| 1408 | bool LLParser::ParseOptionalDLLStorageClass(unsigned &Res) { |
| 1409 | switch (Lex.getKind()) { |
| 1410 | default: Res = GlobalValue::DefaultStorageClass; return false; |
| 1411 | case lltok::kw_dllimport: Res = GlobalValue::DLLImportStorageClass; break; |
| 1412 | case lltok::kw_dllexport: Res = GlobalValue::DLLExportStorageClass; break; |
| 1413 | } |
| 1414 | Lex.Lex(); |
| 1415 | return false; |
| 1416 | } |
| 1417 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1418 | /// ParseOptionalCallingConv |
| 1419 | /// ::= /*empty*/ |
| 1420 | /// ::= 'ccc' |
| 1421 | /// ::= 'fastcc' |
Reid Kleckner | 35fc363 | 2014-12-01 21:04:44 +0000 | [diff] [blame] | 1422 | /// ::= 'intel_ocl_bicc' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1423 | /// ::= 'coldcc' |
| 1424 | /// ::= 'x86_stdcallcc' |
| 1425 | /// ::= 'x86_fastcallcc' |
Anton Korobeynikov | 8f35fab | 2010-05-16 09:08:45 +0000 | [diff] [blame] | 1426 | /// ::= 'x86_thiscallcc' |
Reid Kleckner | 9ccce99 | 2014-10-28 01:29:26 +0000 | [diff] [blame] | 1427 | /// ::= 'x86_vectorcallcc' |
Anton Korobeynikov | a8fd40b | 2009-06-16 18:50:49 +0000 | [diff] [blame] | 1428 | /// ::= 'arm_apcscc' |
| 1429 | /// ::= 'arm_aapcscc' |
| 1430 | /// ::= 'arm_aapcs_vfpcc' |
Anton Korobeynikov | 27a0ecf | 2009-12-07 02:27:35 +0000 | [diff] [blame] | 1431 | /// ::= 'msp430_intrcc' |
Che-Liang Chiou | 2994790 | 2010-09-25 07:46:17 +0000 | [diff] [blame] | 1432 | /// ::= 'ptx_kernel' |
| 1433 | /// ::= 'ptx_device' |
Micah Villmow | 48c8ddc | 2012-10-01 17:01:31 +0000 | [diff] [blame] | 1434 | /// ::= 'spir_func' |
| 1435 | /// ::= 'spir_kernel' |
Charles Davis | e8f297c | 2013-07-12 06:02:35 +0000 | [diff] [blame] | 1436 | /// ::= 'x86_64_sysvcc' |
| 1437 | /// ::= 'x86_64_win64cc' |
Andrew Trick | a3a11de | 2013-10-31 22:12:01 +0000 | [diff] [blame] | 1438 | /// ::= 'webkit_jscc' |
Juergen Ributzka | 9969d3e | 2013-11-08 23:28:16 +0000 | [diff] [blame] | 1439 | /// ::= 'anyregcc' |
Juergen Ributzka | e625013 | 2014-01-17 19:47:03 +0000 | [diff] [blame] | 1440 | /// ::= 'preserve_mostcc' |
| 1441 | /// ::= 'preserve_allcc' |
Reid Kleckner | 35fc363 | 2014-12-01 21:04:44 +0000 | [diff] [blame] | 1442 | /// ::= 'ghccc' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1443 | /// ::= 'cc' UINT |
Anton Korobeynikov | a8fd40b | 2009-06-16 18:50:49 +0000 | [diff] [blame] | 1444 | /// |
Alexey Samsonov | 17a9cff | 2014-09-10 18:00:17 +0000 | [diff] [blame] | 1445 | bool LLParser::ParseOptionalCallingConv(unsigned &CC) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1446 | switch (Lex.getKind()) { |
| 1447 | default: CC = CallingConv::C; return false; |
| 1448 | case lltok::kw_ccc: CC = CallingConv::C; break; |
| 1449 | case lltok::kw_fastcc: CC = CallingConv::Fast; break; |
| 1450 | case lltok::kw_coldcc: CC = CallingConv::Cold; break; |
| 1451 | case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break; |
| 1452 | case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break; |
Anton Korobeynikov | 8f35fab | 2010-05-16 09:08:45 +0000 | [diff] [blame] | 1453 | case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break; |
Reid Kleckner | 9ccce99 | 2014-10-28 01:29:26 +0000 | [diff] [blame] | 1454 | case lltok::kw_x86_vectorcallcc:CC = CallingConv::X86_VectorCall; break; |
Anton Korobeynikov | a8fd40b | 2009-06-16 18:50:49 +0000 | [diff] [blame] | 1455 | case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break; |
| 1456 | case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break; |
| 1457 | case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break; |
Anton Korobeynikov | 27a0ecf | 2009-12-07 02:27:35 +0000 | [diff] [blame] | 1458 | case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break; |
Che-Liang Chiou | 2994790 | 2010-09-25 07:46:17 +0000 | [diff] [blame] | 1459 | case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break; |
| 1460 | case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break; |
Micah Villmow | 48c8ddc | 2012-10-01 17:01:31 +0000 | [diff] [blame] | 1461 | case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break; |
| 1462 | case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break; |
Elena Demikhovsky | d6afb03 | 2012-10-24 14:46:16 +0000 | [diff] [blame] | 1463 | case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break; |
Charles Davis | e8f297c | 2013-07-12 06:02:35 +0000 | [diff] [blame] | 1464 | case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break; |
| 1465 | case lltok::kw_x86_64_win64cc: CC = CallingConv::X86_64_Win64; break; |
Andrew Trick | a3a11de | 2013-10-31 22:12:01 +0000 | [diff] [blame] | 1466 | case lltok::kw_webkit_jscc: CC = CallingConv::WebKit_JS; break; |
Juergen Ributzka | 9969d3e | 2013-11-08 23:28:16 +0000 | [diff] [blame] | 1467 | case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break; |
Juergen Ributzka | e625013 | 2014-01-17 19:47:03 +0000 | [diff] [blame] | 1468 | case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break; |
| 1469 | case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break; |
Reid Kleckner | 35fc363 | 2014-12-01 21:04:44 +0000 | [diff] [blame] | 1470 | case lltok::kw_ghccc: CC = CallingConv::GHC; break; |
Sandeep Patel | 68c5f47 | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 1471 | case lltok::kw_cc: { |
Sandeep Patel | 68c5f47 | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 1472 | Lex.Lex(); |
Alexey Samsonov | 17a9cff | 2014-09-10 18:00:17 +0000 | [diff] [blame] | 1473 | return ParseUInt32(CC); |
Sandeep Patel | 68c5f47 | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 1474 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1475 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1476 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1477 | Lex.Lex(); |
| 1478 | return false; |
| 1479 | } |
| 1480 | |
Chris Lattner | 5c42763 | 2009-12-30 05:31:19 +0000 | [diff] [blame] | 1481 | /// ParseInstructionMetadata |
Chris Lattner | 596760d | 2009-12-29 21:25:40 +0000 | [diff] [blame] | 1482 | /// ::= !dbg !42 (',' !dbg !57)* |
Dan Gohman | 338d9a4 | 2010-08-24 02:05:17 +0000 | [diff] [blame] | 1483 | bool LLParser::ParseInstructionMetadata(Instruction *Inst, |
| 1484 | PerFunctionState *PFS) { |
Chris Lattner | 5c42763 | 2009-12-30 05:31:19 +0000 | [diff] [blame] | 1485 | do { |
| 1486 | if (Lex.getKind() != lltok::MetadataVar) |
| 1487 | return TokError("expected metadata after comma"); |
Devang Patel | ba4a6fd | 2009-09-29 00:01:14 +0000 | [diff] [blame] | 1488 | |
Chris Lattner | 596760d | 2009-12-29 21:25:40 +0000 | [diff] [blame] | 1489 | std::string Name = Lex.getStrVal(); |
Benjamin Kramer | b3bd019 | 2011-12-06 11:50:26 +0000 | [diff] [blame] | 1490 | unsigned MDK = M->getMDKindID(Name); |
Chris Lattner | 596760d | 2009-12-29 21:25:40 +0000 | [diff] [blame] | 1491 | Lex.Lex(); |
Chris Lattner | 8d58f2f | 2009-10-19 05:31:10 +0000 | [diff] [blame] | 1492 | |
Duncan P. N. Exon Smith | f825dae | 2015-01-12 22:26:48 +0000 | [diff] [blame^] | 1493 | MDNode *N; |
| 1494 | if (ParseMDNode(N)) |
Chris Lattner | 1eed2d6 | 2009-12-30 04:56:59 +0000 | [diff] [blame] | 1495 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1496 | |
Duncan P. N. Exon Smith | f825dae | 2015-01-12 22:26:48 +0000 | [diff] [blame^] | 1497 | Inst->setMetadata(MDK, N); |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 1498 | if (MDK == LLVMContext::MD_tbaa) |
| 1499 | InstsWithTBAATag.push_back(Inst); |
| 1500 | |
Chris Lattner | 596760d | 2009-12-29 21:25:40 +0000 | [diff] [blame] | 1501 | // If this is the end of the list, we're done. |
Chris Lattner | 5c42763 | 2009-12-30 05:31:19 +0000 | [diff] [blame] | 1502 | } while (EatIfPresent(lltok::comma)); |
| 1503 | return false; |
Devang Patel | ea8a4b9 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1506 | /// ParseOptionalAlignment |
| 1507 | /// ::= /* empty */ |
| 1508 | /// ::= 'align' 4 |
| 1509 | bool LLParser::ParseOptionalAlignment(unsigned &Alignment) { |
| 1510 | Alignment = 0; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1511 | if (!EatIfPresent(lltok::kw_align)) |
| 1512 | return false; |
Chris Lattner | d11f514 | 2009-01-05 07:46:05 +0000 | [diff] [blame] | 1513 | LocTy AlignLoc = Lex.getLoc(); |
| 1514 | if (ParseUInt32(Alignment)) return true; |
| 1515 | if (!isPowerOf2_32(Alignment)) |
| 1516 | return Error(AlignLoc, "alignment is not a power of two"); |
Dan Gohman | d566d2c | 2010-07-30 21:07:05 +0000 | [diff] [blame] | 1517 | if (Alignment > Value::MaximumAlignment) |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1518 | return Error(AlignLoc, "huge alignments are not supported yet"); |
Chris Lattner | d11f514 | 2009-01-05 07:46:05 +0000 | [diff] [blame] | 1519 | return false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1522 | /// ParseOptionalDereferenceableBytes |
| 1523 | /// ::= /* empty */ |
| 1524 | /// ::= 'dereferenceable' '(' 4 ')' |
| 1525 | bool LLParser::ParseOptionalDereferenceableBytes(uint64_t &Bytes) { |
| 1526 | Bytes = 0; |
| 1527 | if (!EatIfPresent(lltok::kw_dereferenceable)) |
| 1528 | return false; |
| 1529 | LocTy ParenLoc = Lex.getLoc(); |
| 1530 | if (!EatIfPresent(lltok::lparen)) |
| 1531 | return Error(ParenLoc, "expected '('"); |
| 1532 | LocTy DerefLoc = Lex.getLoc(); |
| 1533 | if (ParseUInt64(Bytes)) return true; |
| 1534 | ParenLoc = Lex.getLoc(); |
| 1535 | if (!EatIfPresent(lltok::rparen)) |
| 1536 | return Error(ParenLoc, "expected ')'"); |
| 1537 | if (!Bytes) |
| 1538 | return Error(DerefLoc, "dereferenceable bytes must be non-zero"); |
| 1539 | return false; |
| 1540 | } |
| 1541 | |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 1542 | /// ParseOptionalCommaAlign |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1543 | /// ::= |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 1544 | /// ::= ',' align 4 |
| 1545 | /// |
| 1546 | /// This returns with AteExtraComma set to true if it ate an excess comma at the |
| 1547 | /// end. |
| 1548 | bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment, |
| 1549 | bool &AteExtraComma) { |
| 1550 | AteExtraComma = false; |
| 1551 | while (EatIfPresent(lltok::comma)) { |
| 1552 | // Metadata at the end is an early exit. |
Chris Lattner | eafe4de | 2009-12-30 05:02:06 +0000 | [diff] [blame] | 1553 | if (Lex.getKind() == lltok::MetadataVar) { |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 1554 | AteExtraComma = true; |
| 1555 | return false; |
| 1556 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1557 | |
Chris Lattner | 95b0ff4 | 2010-04-23 00:50:50 +0000 | [diff] [blame] | 1558 | if (Lex.getKind() != lltok::kw_align) |
| 1559 | return Error(Lex.getLoc(), "expected metadata or 'align'"); |
Duncan Sands | 4c5c858 | 2010-10-21 16:07:10 +0000 | [diff] [blame] | 1560 | |
Chris Lattner | 95b0ff4 | 2010-04-23 00:50:50 +0000 | [diff] [blame] | 1561 | if (ParseOptionalAlignment(Alignment)) return true; |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 1562 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1563 | |
Devang Patel | ea8a4b9 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 1564 | return false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1567 | /// ParseScopeAndOrdering |
| 1568 | /// if isAtomic: ::= 'singlethread'? AtomicOrdering |
| 1569 | /// else: ::= |
| 1570 | /// |
| 1571 | /// This sets Scope and Ordering to the parsed values. |
| 1572 | bool LLParser::ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope, |
| 1573 | AtomicOrdering &Ordering) { |
| 1574 | if (!isAtomic) |
| 1575 | return false; |
| 1576 | |
| 1577 | Scope = CrossThread; |
| 1578 | if (EatIfPresent(lltok::kw_singlethread)) |
| 1579 | Scope = SingleThread; |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1580 | |
| 1581 | return ParseOrdering(Ordering); |
| 1582 | } |
| 1583 | |
| 1584 | /// ParseOrdering |
| 1585 | /// ::= AtomicOrdering |
| 1586 | /// |
| 1587 | /// This sets Ordering to the parsed value. |
| 1588 | bool LLParser::ParseOrdering(AtomicOrdering &Ordering) { |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1589 | switch (Lex.getKind()) { |
| 1590 | default: return TokError("Expected ordering on atomic instruction"); |
| 1591 | case lltok::kw_unordered: Ordering = Unordered; break; |
| 1592 | case lltok::kw_monotonic: Ordering = Monotonic; break; |
| 1593 | case lltok::kw_acquire: Ordering = Acquire; break; |
| 1594 | case lltok::kw_release: Ordering = Release; break; |
| 1595 | case lltok::kw_acq_rel: Ordering = AcquireRelease; break; |
| 1596 | case lltok::kw_seq_cst: Ordering = SequentiallyConsistent; break; |
| 1597 | } |
| 1598 | Lex.Lex(); |
| 1599 | return false; |
| 1600 | } |
| 1601 | |
Charles Davis | be5557e | 2010-02-12 00:31:15 +0000 | [diff] [blame] | 1602 | /// ParseOptionalStackAlignment |
| 1603 | /// ::= /* empty */ |
| 1604 | /// ::= 'alignstack' '(' 4 ')' |
| 1605 | bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) { |
| 1606 | Alignment = 0; |
| 1607 | if (!EatIfPresent(lltok::kw_alignstack)) |
| 1608 | return false; |
| 1609 | LocTy ParenLoc = Lex.getLoc(); |
| 1610 | if (!EatIfPresent(lltok::lparen)) |
| 1611 | return Error(ParenLoc, "expected '('"); |
| 1612 | LocTy AlignLoc = Lex.getLoc(); |
| 1613 | if (ParseUInt32(Alignment)) return true; |
| 1614 | ParenLoc = Lex.getLoc(); |
| 1615 | if (!EatIfPresent(lltok::rparen)) |
| 1616 | return Error(ParenLoc, "expected ')'"); |
| 1617 | if (!isPowerOf2_32(Alignment)) |
| 1618 | return Error(AlignLoc, "stack alignment is not a power of two"); |
| 1619 | return false; |
| 1620 | } |
Devang Patel | ea8a4b9 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 1621 | |
Chris Lattner | 28f1eeb | 2009-12-30 05:14:00 +0000 | [diff] [blame] | 1622 | /// ParseIndexList - This parses the index list for an insert/extractvalue |
| 1623 | /// instruction. This sets AteExtraComma in the case where we eat an extra |
| 1624 | /// comma at the end of the line and find that it is followed by metadata. |
| 1625 | /// Clients that don't allow metadata can call the version of this function that |
| 1626 | /// only takes one argument. |
| 1627 | /// |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1628 | /// ParseIndexList |
| 1629 | /// ::= (',' uint32)+ |
Chris Lattner | 28f1eeb | 2009-12-30 05:14:00 +0000 | [diff] [blame] | 1630 | /// |
| 1631 | bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices, |
| 1632 | bool &AteExtraComma) { |
| 1633 | AteExtraComma = false; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1634 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1635 | if (Lex.getKind() != lltok::comma) |
| 1636 | return TokError("expected ',' as start of index list"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1637 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1638 | while (EatIfPresent(lltok::comma)) { |
Chris Lattner | 28f1eeb | 2009-12-30 05:14:00 +0000 | [diff] [blame] | 1639 | if (Lex.getKind() == lltok::MetadataVar) { |
| 1640 | AteExtraComma = true; |
| 1641 | return false; |
| 1642 | } |
Nick Lewycky | 83e4711 | 2010-09-29 23:32:20 +0000 | [diff] [blame] | 1643 | unsigned Idx = 0; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1644 | if (ParseUInt32(Idx)) return true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1645 | Indices.push_back(Idx); |
| 1646 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1647 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1648 | return false; |
| 1649 | } |
| 1650 | |
| 1651 | //===----------------------------------------------------------------------===// |
| 1652 | // Type Parsing. |
| 1653 | //===----------------------------------------------------------------------===// |
| 1654 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1655 | /// ParseType - Parse a type. |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 1656 | bool LLParser::ParseType(Type *&Result, const Twine &Msg, bool AllowVoid) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1657 | SMLoc TypeLoc = Lex.getLoc(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1658 | switch (Lex.getKind()) { |
| 1659 | default: |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 1660 | return TokError(Msg); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1661 | case lltok::Type: |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1662 | // Type ::= 'float' | 'void' (etc) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1663 | Result = Lex.getTyVal(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1664 | Lex.Lex(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1665 | break; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1666 | case lltok::lbrace: |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1667 | // Type ::= StructType |
| 1668 | if (ParseAnonStructType(Result, false)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1669 | return true; |
| 1670 | break; |
| 1671 | case lltok::lsquare: |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1672 | // Type ::= '[' ... ']' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1673 | Lex.Lex(); // eat the lsquare. |
| 1674 | if (ParseArrayVectorType(Result, false)) |
| 1675 | return true; |
| 1676 | break; |
| 1677 | case lltok::less: // Either vector or packed struct. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1678 | // Type ::= '<' ... '>' |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1679 | Lex.Lex(); |
| 1680 | if (Lex.getKind() == lltok::lbrace) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1681 | if (ParseAnonStructType(Result, true) || |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1682 | ParseToken(lltok::greater, "expected '>' at end of packed struct")) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1683 | return true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1684 | } else if (ParseArrayVectorType(Result, true)) |
| 1685 | return true; |
| 1686 | break; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1687 | case lltok::LocalVar: { |
| 1688 | // Type ::= %foo |
| 1689 | std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()]; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1690 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1691 | // If the type hasn't been defined yet, create a forward definition and |
| 1692 | // 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] | 1693 | if (!Entry.first) { |
Chris Lattner | 335d399 | 2011-08-12 18:06:37 +0000 | [diff] [blame] | 1694 | Entry.first = StructType::create(Context, Lex.getStrVal()); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1695 | Entry.second = Lex.getLoc(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1696 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1697 | Result = Entry.first; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1698 | Lex.Lex(); |
| 1699 | break; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1700 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1701 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1702 | case lltok::LocalVarID: { |
| 1703 | // Type ::= %4 |
| 1704 | if (Lex.getUIntVal() >= NumberedTypes.size()) |
| 1705 | NumberedTypes.resize(Lex.getUIntVal()+1); |
| 1706 | std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()]; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1707 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1708 | // If the type hasn't been defined yet, create a forward definition and |
| 1709 | // 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] | 1710 | if (!Entry.first) { |
Chris Lattner | 335d399 | 2011-08-12 18:06:37 +0000 | [diff] [blame] | 1711 | Entry.first = StructType::create(Context); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1712 | Entry.second = Lex.getLoc(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1713 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1714 | Result = Entry.first; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1715 | Lex.Lex(); |
| 1716 | break; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1717 | } |
| 1718 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1719 | |
| 1720 | // Parse the type suffixes. |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1721 | while (1) { |
| 1722 | switch (Lex.getKind()) { |
| 1723 | // End of type. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1724 | default: |
| 1725 | if (!AllowVoid && Result->isVoidTy()) |
| 1726 | return Error(TypeLoc, "void type only allowed for function results"); |
| 1727 | return false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1728 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1729 | // Type ::= Type '*' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1730 | case lltok::star: |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1731 | if (Result->isLabelTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1732 | return TokError("basic block pointers are invalid"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1733 | if (Result->isVoidTy()) |
| 1734 | return TokError("pointers to void are invalid - use i8* instead"); |
| 1735 | if (!PointerType::isValidElementType(Result)) |
Nick Lewycky | 0aa6a74 | 2009-06-07 07:26:46 +0000 | [diff] [blame] | 1736 | return TokError("pointer to this type is invalid"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1737 | Result = PointerType::getUnqual(Result); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1738 | Lex.Lex(); |
| 1739 | break; |
| 1740 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1741 | // Type ::= Type 'addrspace' '(' uint32 ')' '*' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1742 | case lltok::kw_addrspace: { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1743 | if (Result->isLabelTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1744 | return TokError("basic block pointers are invalid"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1745 | if (Result->isVoidTy()) |
Dan Gohman | 9280a68 | 2009-02-09 17:41:21 +0000 | [diff] [blame] | 1746 | return TokError("pointers to void are invalid; use i8* instead"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1747 | if (!PointerType::isValidElementType(Result)) |
Nick Lewycky | 0aa6a74 | 2009-06-07 07:26:46 +0000 | [diff] [blame] | 1748 | return TokError("pointer to this type is invalid"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1749 | unsigned AddrSpace; |
| 1750 | if (ParseOptionalAddrSpace(AddrSpace) || |
| 1751 | ParseToken(lltok::star, "expected '*' in address space")) |
| 1752 | return true; |
| 1753 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1754 | Result = PointerType::get(Result, AddrSpace); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1755 | break; |
| 1756 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1757 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1758 | /// Types '(' ArgTypeListI ')' OptFuncAttrs |
| 1759 | case lltok::lparen: |
| 1760 | if (ParseFunctionType(Result)) |
| 1761 | return true; |
| 1762 | break; |
| 1763 | } |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | /// ParseParameterList |
| 1768 | /// ::= '(' ')' |
| 1769 | /// ::= '(' Arg (',' Arg)* ')' |
| 1770 | /// Arg |
| 1771 | /// ::= Type OptionalAttributes Value OptionalAttributes |
| 1772 | bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList, |
Reid Kleckner | 8349864 | 2014-08-26 00:33:28 +0000 | [diff] [blame] | 1773 | PerFunctionState &PFS, bool IsMustTailCall, |
| 1774 | bool InVarArgsFunc) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1775 | if (ParseToken(lltok::lparen, "expected '(' in call")) |
| 1776 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1777 | |
Bill Wendling | fe0021a | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 1778 | unsigned AttrIndex = 1; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1779 | while (Lex.getKind() != lltok::rparen) { |
| 1780 | // If this isn't the first argument, we need a comma. |
| 1781 | if (!ArgList.empty() && |
| 1782 | ParseToken(lltok::comma, "expected ',' in argument list")) |
| 1783 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1784 | |
Reid Kleckner | 8349864 | 2014-08-26 00:33:28 +0000 | [diff] [blame] | 1785 | // Parse an ellipsis if this is a musttail call in a variadic function. |
| 1786 | if (Lex.getKind() == lltok::dotdotdot) { |
| 1787 | const char *Msg = "unexpected ellipsis in argument list for "; |
| 1788 | if (!IsMustTailCall) |
| 1789 | return TokError(Twine(Msg) + "non-musttail call"); |
| 1790 | if (!InVarArgsFunc) |
| 1791 | return TokError(Twine(Msg) + "musttail call in non-varargs function"); |
| 1792 | Lex.Lex(); // Lex the '...', it is purely for readability. |
| 1793 | return ParseToken(lltok::rparen, "expected ')' at end of argument list"); |
| 1794 | } |
| 1795 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1796 | // Parse the argument. |
| 1797 | LocTy ArgLoc; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1798 | Type *ArgTy = nullptr; |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 1799 | AttrBuilder ArgAttrs; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1800 | Value *V; |
Victor Hernandez | fa23223 | 2009-12-03 23:40:58 +0000 | [diff] [blame] | 1801 | if (ParseType(ArgTy, ArgLoc)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1802 | return true; |
Victor Hernandez | fa23223 | 2009-12-03 23:40:58 +0000 | [diff] [blame] | 1803 | |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 1804 | if (ArgTy->isMetadataTy()) { |
| 1805 | if (ParseMetadataAsValue(V, PFS)) |
| 1806 | return true; |
| 1807 | } else { |
| 1808 | // Otherwise, handle normal operands. |
| 1809 | if (ParseOptionalParamAttrs(ArgAttrs) || ParseValue(ArgTy, V, PFS)) |
| 1810 | return true; |
| 1811 | } |
Bill Wendling | fe0021a | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 1812 | ArgList.push_back(ParamInfo(ArgLoc, V, AttributeSet::get(V->getContext(), |
| 1813 | AttrIndex++, |
| 1814 | ArgAttrs))); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
Reid Kleckner | 8349864 | 2014-08-26 00:33:28 +0000 | [diff] [blame] | 1817 | if (IsMustTailCall && InVarArgsFunc) |
| 1818 | return TokError("expected '...' at end of argument list for musttail call " |
| 1819 | "in varargs function"); |
| 1820 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1821 | Lex.Lex(); // Lex the ')'. |
| 1822 | return false; |
| 1823 | } |
| 1824 | |
| 1825 | |
| 1826 | |
Chris Lattner | 2ed06b4 | 2009-01-05 18:34:07 +0000 | [diff] [blame] | 1827 | /// ParseArgumentList - Parse the argument list for a function type or function |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1828 | /// prototype. |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1829 | /// ::= '(' ArgTypeListI ')' |
| 1830 | /// ArgTypeListI |
| 1831 | /// ::= /*empty*/ |
| 1832 | /// ::= '...' |
| 1833 | /// ::= ArgTypeList ',' '...' |
| 1834 | /// ::= ArgType (',' ArgType)* |
Chris Lattner | 2ed06b4 | 2009-01-05 18:34:07 +0000 | [diff] [blame] | 1835 | /// |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1836 | bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, |
| 1837 | bool &isVarArg){ |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1838 | isVarArg = false; |
| 1839 | assert(Lex.getKind() == lltok::lparen); |
| 1840 | Lex.Lex(); // eat the (. |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1841 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1842 | if (Lex.getKind() == lltok::rparen) { |
| 1843 | // empty |
| 1844 | } else if (Lex.getKind() == lltok::dotdotdot) { |
| 1845 | isVarArg = true; |
| 1846 | Lex.Lex(); |
| 1847 | } else { |
| 1848 | LocTy TypeLoc = Lex.getLoc(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1849 | Type *ArgTy = nullptr; |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 1850 | AttrBuilder Attrs; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1851 | std::string Name; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1852 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1853 | if (ParseType(ArgTy) || |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 1854 | ParseOptionalParamAttrs(Attrs)) return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1855 | |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1856 | if (ArgTy->isVoidTy()) |
Chris Lattner | f880ca2 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 1857 | return Error(TypeLoc, "argument can not have void type"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1858 | |
Chris Lattner | def1949 | 2011-06-17 06:36:20 +0000 | [diff] [blame] | 1859 | if (Lex.getKind() == lltok::LocalVar) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1860 | Name = Lex.getStrVal(); |
| 1861 | Lex.Lex(); |
| 1862 | } |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1863 | |
Nick Lewycky | 0aa6a74 | 2009-06-07 07:26:46 +0000 | [diff] [blame] | 1864 | if (!FunctionType::isValidArgumentType(ArgTy)) |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1865 | return Error(TypeLoc, "invalid type for function argument"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1866 | |
Bill Wendling | fe0021a | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 1867 | unsigned AttrIndex = 1; |
Bill Wendling | d079a44 | 2012-10-15 04:46:55 +0000 | [diff] [blame] | 1868 | ArgList.push_back(ArgInfo(TypeLoc, ArgTy, |
Bill Wendling | fe0021a | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 1869 | AttributeSet::get(ArgTy->getContext(), |
| 1870 | AttrIndex++, Attrs), Name)); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1871 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1872 | while (EatIfPresent(lltok::comma)) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1873 | // Handle ... at end of arg list. |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1874 | if (EatIfPresent(lltok::dotdotdot)) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1875 | isVarArg = true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1876 | break; |
| 1877 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1878 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1879 | // Otherwise must be an argument type. |
| 1880 | TypeLoc = Lex.getLoc(); |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 1881 | if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) return true; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1882 | |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1883 | if (ArgTy->isVoidTy()) |
Chris Lattner | f880ca2 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 1884 | return Error(TypeLoc, "argument can not have void type"); |
| 1885 | |
Chris Lattner | def1949 | 2011-06-17 06:36:20 +0000 | [diff] [blame] | 1886 | if (Lex.getKind() == lltok::LocalVar) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1887 | Name = Lex.getStrVal(); |
| 1888 | Lex.Lex(); |
| 1889 | } else { |
| 1890 | Name = ""; |
| 1891 | } |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1892 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1893 | if (!ArgTy->isFirstClassType()) |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1894 | return Error(TypeLoc, "invalid type for function argument"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1895 | |
Bill Wendling | d079a44 | 2012-10-15 04:46:55 +0000 | [diff] [blame] | 1896 | ArgList.push_back(ArgInfo(TypeLoc, ArgTy, |
Bill Wendling | fe0021a | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 1897 | AttributeSet::get(ArgTy->getContext(), |
| 1898 | AttrIndex++, Attrs), |
Bill Wendling | d079a44 | 2012-10-15 04:46:55 +0000 | [diff] [blame] | 1899 | Name)); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1900 | } |
| 1901 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1902 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 1903 | return ParseToken(lltok::rparen, "expected ')' at end of argument list"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1904 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1905 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1906 | /// ParseFunctionType |
| 1907 | /// ::= Type ArgumentList OptionalAttrs |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1908 | bool LLParser::ParseFunctionType(Type *&Result) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1909 | assert(Lex.getKind() == lltok::lparen); |
| 1910 | |
Chris Lattner | ce473c7 | 2009-01-05 08:04:33 +0000 | [diff] [blame] | 1911 | if (!FunctionType::isValidReturnType(Result)) |
| 1912 | return TokError("invalid function return type"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1913 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1914 | SmallVector<ArgInfo, 8> ArgList; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1915 | bool isVarArg; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1916 | if (ParseArgumentList(ArgList, isVarArg)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1917 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1918 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1919 | // Reject names on the arguments lists. |
| 1920 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { |
| 1921 | if (!ArgList[i].Name.empty()) |
| 1922 | return Error(ArgList[i].Loc, "argument name invalid in function type"); |
Bill Wendling | fe0021a | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 1923 | if (ArgList[i].Attrs.hasAttributes(i + 1)) |
Chris Lattner | 6bc5c89 | 2011-06-17 17:37:13 +0000 | [diff] [blame] | 1924 | return Error(ArgList[i].Loc, |
| 1925 | "argument attributes invalid in function type"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1926 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1927 | |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 1928 | SmallVector<Type*, 16> ArgListTy; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1929 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1930 | ArgListTy.push_back(ArgList[i].Ty); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1931 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1932 | Result = FunctionType::get(Result, ArgListTy, isVarArg); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1933 | return false; |
| 1934 | } |
| 1935 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1936 | /// ParseAnonStructType - Parse an anonymous struct type, which is inlined into |
| 1937 | /// other structs. |
| 1938 | bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) { |
| 1939 | SmallVector<Type*, 8> Elts; |
| 1940 | if (ParseStructBody(Elts)) return true; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1941 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1942 | Result = StructType::get(Context, Elts, Packed); |
| 1943 | return false; |
| 1944 | } |
| 1945 | |
| 1946 | /// ParseStructDefinition - Parse a struct in a 'type' definition. |
| 1947 | bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name, |
| 1948 | std::pair<Type*, LocTy> &Entry, |
| 1949 | Type *&ResultTy) { |
| 1950 | // If the type was already defined, diagnose the redefinition. |
| 1951 | if (Entry.first && !Entry.second.isValid()) |
| 1952 | return Error(TypeLoc, "redefinition of type"); |
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 we have opaque, just return without filling in the definition for the |
| 1955 | // struct. This counts as a definition as far as the .ll file goes. |
| 1956 | if (EatIfPresent(lltok::kw_opaque)) { |
| 1957 | // This type is being defined, so clear the location to indicate this. |
| 1958 | Entry.second = SMLoc(); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1959 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1960 | // If this type number has never been uttered, create it. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1961 | if (!Entry.first) |
Chris Lattner | 335d399 | 2011-08-12 18:06:37 +0000 | [diff] [blame] | 1962 | Entry.first = StructType::create(Context, Name); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1963 | ResultTy = Entry.first; |
| 1964 | return false; |
| 1965 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1966 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1967 | // If the type starts with '<', then it is either a packed struct or a vector. |
| 1968 | bool isPacked = EatIfPresent(lltok::less); |
| 1969 | |
| 1970 | // If we don't have a struct, then we have a random type alias, which we |
| 1971 | // accept for compatibility with old files. These types are not allowed to be |
| 1972 | // forward referenced and not allowed to be recursive. |
| 1973 | if (Lex.getKind() != lltok::lbrace) { |
| 1974 | if (Entry.first) |
| 1975 | return Error(TypeLoc, "forward references to non-struct type"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1976 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1977 | ResultTy = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1978 | if (isPacked) |
| 1979 | return ParseArrayVectorType(ResultTy, true); |
| 1980 | return ParseType(ResultTy); |
| 1981 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1982 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1983 | // This type is being defined, so clear the location to indicate this. |
| 1984 | Entry.second = SMLoc(); |
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 | // If this type number has never been uttered, create it. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1987 | if (!Entry.first) |
Chris Lattner | 335d399 | 2011-08-12 18:06:37 +0000 | [diff] [blame] | 1988 | Entry.first = StructType::create(Context, Name); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1989 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1990 | StructType *STy = cast<StructType>(Entry.first); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1991 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1992 | SmallVector<Type*, 8> Body; |
| 1993 | if (ParseStructBody(Body) || |
| 1994 | (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct"))) |
| 1995 | return true; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1996 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1997 | STy->setBody(Body, isPacked); |
| 1998 | ResultTy = STy; |
| 1999 | return false; |
| 2000 | } |
| 2001 | |
| 2002 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2003 | /// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2004 | /// StructType |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2005 | /// ::= '{' '}' |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2006 | /// ::= '{' Type (',' Type)* '}' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2007 | /// ::= '<' '{' '}' '>' |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2008 | /// ::= '<' '{' Type (',' Type)* '}' '>' |
| 2009 | bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2010 | assert(Lex.getKind() == lltok::lbrace); |
| 2011 | Lex.Lex(); // Consume the '{' |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2012 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2013 | // Handle the empty struct. |
| 2014 | if (EatIfPresent(lltok::rbrace)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2015 | return false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2016 | |
Chris Lattner | f880ca2 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 2017 | LocTy EltTyLoc = Lex.getLoc(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2018 | Type *Ty = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2019 | if (ParseType(Ty)) return true; |
| 2020 | Body.push_back(Ty); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2021 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2022 | if (!StructType::isValidElementType(Ty)) |
Nick Lewycky | 0aa6a74 | 2009-06-07 07:26:46 +0000 | [diff] [blame] | 2023 | return Error(EltTyLoc, "invalid element type for struct"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2024 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2025 | while (EatIfPresent(lltok::comma)) { |
Chris Lattner | f880ca2 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 2026 | EltTyLoc = Lex.getLoc(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2027 | if (ParseType(Ty)) return true; |
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 | if (!StructType::isValidElementType(Ty)) |
Nick Lewycky | 0aa6a74 | 2009-06-07 07:26:46 +0000 | [diff] [blame] | 2030 | return Error(EltTyLoc, "invalid element type for struct"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2031 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2032 | Body.push_back(Ty); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2033 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2034 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2035 | return ParseToken(lltok::rbrace, "expected '}' at end of struct"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | /// ParseArrayVectorType - Parse an array or vector type, assuming the first |
| 2039 | /// token has already been consumed. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2040 | /// Type |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2041 | /// ::= '[' APSINTVAL 'x' Types ']' |
| 2042 | /// ::= '<' APSINTVAL 'x' Types '>' |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2043 | bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2044 | if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() || |
| 2045 | Lex.getAPSIntVal().getBitWidth() > 64) |
| 2046 | return TokError("expected number in address space"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2047 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2048 | LocTy SizeLoc = Lex.getLoc(); |
| 2049 | uint64_t Size = Lex.getAPSIntVal().getZExtValue(); |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2050 | Lex.Lex(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2051 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2052 | if (ParseToken(lltok::kw_x, "expected 'x' after element count")) |
| 2053 | return true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2054 | |
| 2055 | LocTy TypeLoc = Lex.getLoc(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2056 | Type *EltTy = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2057 | if (ParseType(EltTy)) return true; |
Chris Lattner | f880ca2 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 2058 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2059 | if (ParseToken(isVector ? lltok::greater : lltok::rsquare, |
| 2060 | "expected end of sequential type")) |
| 2061 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2062 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2063 | if (isVector) { |
Chris Lattner | bb1fe8a | 2009-02-28 18:12:41 +0000 | [diff] [blame] | 2064 | if (Size == 0) |
| 2065 | return Error(SizeLoc, "zero element vector is illegal"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2066 | if ((unsigned)Size != Size) |
| 2067 | return Error(SizeLoc, "size too large for vector"); |
Nick Lewycky | 0aa6a74 | 2009-06-07 07:26:46 +0000 | [diff] [blame] | 2068 | if (!VectorType::isValidElementType(EltTy)) |
Duncan Sands | e6beec6 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 2069 | return Error(TypeLoc, "invalid vector element type"); |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 2070 | Result = VectorType::get(EltTy, unsigned(Size)); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2071 | } else { |
Nick Lewycky | 0aa6a74 | 2009-06-07 07:26:46 +0000 | [diff] [blame] | 2072 | if (!ArrayType::isValidElementType(EltTy)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2073 | return Error(TypeLoc, "invalid array element type"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2074 | Result = ArrayType::get(EltTy, Size); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2075 | } |
| 2076 | return false; |
| 2077 | } |
| 2078 | |
| 2079 | //===----------------------------------------------------------------------===// |
| 2080 | // Function Semantic Analysis. |
| 2081 | //===----------------------------------------------------------------------===// |
| 2082 | |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 2083 | LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f, |
| 2084 | int functionNumber) |
| 2085 | : P(p), F(f), FunctionNumber(functionNumber) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2086 | |
| 2087 | // Insert unnamed arguments into the NumberedVals list. |
| 2088 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); |
| 2089 | AI != E; ++AI) |
| 2090 | if (!AI->hasName()) |
| 2091 | NumberedVals.push_back(AI); |
| 2092 | } |
| 2093 | |
| 2094 | LLParser::PerFunctionState::~PerFunctionState() { |
| 2095 | // If there were any forward referenced non-basicblock values, delete them. |
| 2096 | for (std::map<std::string, std::pair<Value*, LocTy> >::iterator |
| 2097 | I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I) |
| 2098 | if (!isa<BasicBlock>(I->second.first)) { |
Owen Anderson | 09063ce | 2009-07-02 17:04:01 +0000 | [diff] [blame] | 2099 | I->second.first->replaceAllUsesWith( |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2100 | UndefValue::get(I->second.first->getType())); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2101 | delete I->second.first; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2102 | I->second.first = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2103 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2104 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2105 | for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator |
| 2106 | I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I) |
| 2107 | if (!isa<BasicBlock>(I->second.first)) { |
Owen Anderson | 09063ce | 2009-07-02 17:04:01 +0000 | [diff] [blame] | 2108 | I->second.first->replaceAllUsesWith( |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2109 | UndefValue::get(I->second.first->getType())); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2110 | delete I->second.first; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2111 | I->second.first = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2112 | } |
| 2113 | } |
| 2114 | |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 2115 | bool LLParser::PerFunctionState::FinishFunction() { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2116 | if (!ForwardRefVals.empty()) |
| 2117 | return P.Error(ForwardRefVals.begin()->second.second, |
| 2118 | "use of undefined value '%" + ForwardRefVals.begin()->first + |
| 2119 | "'"); |
| 2120 | if (!ForwardRefValIDs.empty()) |
| 2121 | return P.Error(ForwardRefValIDs.begin()->second.second, |
| 2122 | "use of undefined value '%" + |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 2123 | Twine(ForwardRefValIDs.begin()->first) + "'"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2124 | return false; |
| 2125 | } |
| 2126 | |
| 2127 | |
| 2128 | /// GetVal - Get a value with the specified name or ID, creating a |
| 2129 | /// forward reference record if needed. This can return null if the value |
| 2130 | /// exists but does not have the right type. |
| 2131 | Value *LLParser::PerFunctionState::GetVal(const std::string &Name, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2132 | Type *Ty, LocTy Loc) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2133 | // Look this name up in the normal function symbol table. |
| 2134 | Value *Val = F.getValueSymbolTable().lookup(Name); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2135 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2136 | // If this is a forward reference for the value, see if we already created a |
| 2137 | // forward ref record. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2138 | if (!Val) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2139 | std::map<std::string, std::pair<Value*, LocTy> >::iterator |
| 2140 | I = ForwardRefVals.find(Name); |
| 2141 | if (I != ForwardRefVals.end()) |
| 2142 | Val = I->second.first; |
| 2143 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2144 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2145 | // If we have the value in the symbol table or fwd-ref table, return it. |
| 2146 | if (Val) { |
| 2147 | if (Val->getType() == Ty) return Val; |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 2148 | if (Ty->isLabelTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2149 | P.Error(Loc, "'%" + Name + "' is not a basic block"); |
| 2150 | else |
| 2151 | P.Error(Loc, "'%" + Name + "' defined with type '" + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 2152 | getTypeString(Val->getType()) + "'"); |
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 | // Don't make placeholders with invalid type. |
Duncan P. N. Exon Smith | 6a6e9cb | 2014-08-05 18:22:58 +0000 | [diff] [blame] | 2157 | if (!Ty->isFirstClassType()) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2158 | P.Error(Loc, "invalid use of a non-first-class type"); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2159 | return nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2160 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2161 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2162 | // Otherwise, create a new forward reference for this value and remember it. |
| 2163 | Value *FwdVal; |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 2164 | if (Ty->isLabelTy()) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2165 | FwdVal = BasicBlock::Create(F.getContext(), Name, &F); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2166 | else |
| 2167 | FwdVal = new Argument(Ty, Name); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2168 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2169 | ForwardRefVals[Name] = std::make_pair(FwdVal, Loc); |
| 2170 | return FwdVal; |
| 2171 | } |
| 2172 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2173 | Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty, |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2174 | LocTy Loc) { |
| 2175 | // Look this name up in the normal function symbol table. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2176 | Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2177 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2178 | // If this is a forward reference for the value, see if we already created a |
| 2179 | // forward ref record. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2180 | if (!Val) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2181 | std::map<unsigned, std::pair<Value*, LocTy> >::iterator |
| 2182 | I = ForwardRefValIDs.find(ID); |
| 2183 | if (I != ForwardRefValIDs.end()) |
| 2184 | Val = I->second.first; |
| 2185 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2186 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2187 | // If we have the value in the symbol table or fwd-ref table, return it. |
| 2188 | if (Val) { |
| 2189 | if (Val->getType() == Ty) return Val; |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 2190 | if (Ty->isLabelTy()) |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 2191 | P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2192 | else |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 2193 | P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 2194 | getTypeString(Val->getType()) + "'"); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2195 | return nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2196 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2197 | |
Duncan P. N. Exon Smith | 6a6e9cb | 2014-08-05 18:22:58 +0000 | [diff] [blame] | 2198 | if (!Ty->isFirstClassType()) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2199 | P.Error(Loc, "invalid use of a non-first-class type"); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2200 | return nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2201 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2202 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2203 | // Otherwise, create a new forward reference for this value and remember it. |
| 2204 | Value *FwdVal; |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 2205 | if (Ty->isLabelTy()) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2206 | FwdVal = BasicBlock::Create(F.getContext(), "", &F); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2207 | else |
| 2208 | FwdVal = new Argument(Ty); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2209 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2210 | ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc); |
| 2211 | return FwdVal; |
| 2212 | } |
| 2213 | |
| 2214 | /// SetInstName - After an instruction is parsed and inserted into its |
| 2215 | /// basic block, this installs its name. |
| 2216 | bool LLParser::PerFunctionState::SetInstName(int NameID, |
| 2217 | const std::string &NameStr, |
| 2218 | LocTy NameLoc, Instruction *Inst) { |
| 2219 | // 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] | 2220 | if (Inst->getType()->isVoidTy()) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2221 | if (NameID != -1 || !NameStr.empty()) |
| 2222 | return P.Error(NameLoc, "instructions returning void cannot have a name"); |
| 2223 | return false; |
| 2224 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2225 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2226 | // If this was a numbered instruction, verify that the instruction is the |
| 2227 | // expected value and resolve any forward references. |
| 2228 | if (NameStr.empty()) { |
| 2229 | // If neither a name nor an ID was specified, just use the next ID. |
| 2230 | if (NameID == -1) |
| 2231 | NameID = NumberedVals.size(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2232 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2233 | if (unsigned(NameID) != NumberedVals.size()) |
| 2234 | return P.Error(NameLoc, "instruction expected to be numbered '%" + |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 2235 | Twine(NumberedVals.size()) + "'"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2236 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2237 | std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI = |
| 2238 | ForwardRefValIDs.find(NameID); |
| 2239 | if (FI != ForwardRefValIDs.end()) { |
| 2240 | if (FI->second.first->getType() != Inst->getType()) |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2241 | return P.Error(NameLoc, "instruction forward referenced with type '" + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 2242 | getTypeString(FI->second.first->getType()) + "'"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2243 | FI->second.first->replaceAllUsesWith(Inst); |
Nuno Lopes | 2baa6a3 | 2009-09-02 15:02:57 +0000 | [diff] [blame] | 2244 | delete FI->second.first; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2245 | ForwardRefValIDs.erase(FI); |
| 2246 | } |
| 2247 | |
| 2248 | NumberedVals.push_back(Inst); |
| 2249 | return false; |
| 2250 | } |
| 2251 | |
| 2252 | // Otherwise, the instruction had a name. Resolve forward refs and set it. |
| 2253 | std::map<std::string, std::pair<Value*, LocTy> >::iterator |
| 2254 | FI = ForwardRefVals.find(NameStr); |
| 2255 | if (FI != ForwardRefVals.end()) { |
| 2256 | if (FI->second.first->getType() != Inst->getType()) |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2257 | return P.Error(NameLoc, "instruction forward referenced with type '" + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 2258 | getTypeString(FI->second.first->getType()) + "'"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2259 | FI->second.first->replaceAllUsesWith(Inst); |
Nuno Lopes | 2fcee70 | 2009-09-02 14:22:03 +0000 | [diff] [blame] | 2260 | delete FI->second.first; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2261 | ForwardRefVals.erase(FI); |
| 2262 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2263 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2264 | // Set the name on the instruction. |
| 2265 | Inst->setName(NameStr); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2266 | |
Benjamin Kramer | 1dc34b4 | 2010-10-16 11:28:23 +0000 | [diff] [blame] | 2267 | if (Inst->getName() != NameStr) |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2268 | return P.Error(NameLoc, "multiple definition of local value named '" + |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2269 | NameStr + "'"); |
| 2270 | return false; |
| 2271 | } |
| 2272 | |
| 2273 | /// GetBB - Get a basic block with the specified name or ID, creating a |
| 2274 | /// forward reference record if needed. |
| 2275 | BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name, |
| 2276 | LocTy Loc) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2277 | return cast_or_null<BasicBlock>(GetVal(Name, |
| 2278 | Type::getLabelTy(F.getContext()), Loc)); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
| 2281 | BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2282 | return cast_or_null<BasicBlock>(GetVal(ID, |
| 2283 | Type::getLabelTy(F.getContext()), Loc)); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
| 2286 | /// DefineBB - Define the specified basic block, which is either named or |
| 2287 | /// unnamed. If there is an error, this returns null otherwise it returns |
| 2288 | /// the block being defined. |
| 2289 | BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name, |
| 2290 | LocTy Loc) { |
| 2291 | BasicBlock *BB; |
| 2292 | if (Name.empty()) |
| 2293 | BB = GetBB(NumberedVals.size(), Loc); |
| 2294 | else |
| 2295 | BB = GetBB(Name, Loc); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2296 | if (!BB) return nullptr; // Already diagnosed error. |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2297 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2298 | // Move the block to the end of the function. Forward ref'd blocks are |
| 2299 | // inserted wherever they happen to be referenced. |
| 2300 | F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2301 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2302 | // Remove the block from forward ref sets. |
| 2303 | if (Name.empty()) { |
| 2304 | ForwardRefValIDs.erase(NumberedVals.size()); |
| 2305 | NumberedVals.push_back(BB); |
| 2306 | } else { |
| 2307 | // BB forward references are already in the function symbol table. |
| 2308 | ForwardRefVals.erase(Name); |
| 2309 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2310 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2311 | return BB; |
| 2312 | } |
| 2313 | |
| 2314 | //===----------------------------------------------------------------------===// |
| 2315 | // Constants. |
| 2316 | //===----------------------------------------------------------------------===// |
| 2317 | |
| 2318 | /// ParseValID - Parse an abstract value that doesn't necessarily have a |
| 2319 | /// type implied. For example, if we parse "4" we don't know what integer type |
| 2320 | /// 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] | 2321 | /// sanity. PFS is used to convert function-local operands of metadata (since |
| 2322 | /// metadata operands are not just parsed here but also converted to values). |
| 2323 | /// 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] | 2324 | bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2325 | ID.Loc = Lex.getLoc(); |
| 2326 | switch (Lex.getKind()) { |
| 2327 | default: return TokError("expected value token"); |
| 2328 | case lltok::GlobalID: // @42 |
| 2329 | ID.UIntVal = Lex.getUIntVal(); |
| 2330 | ID.Kind = ValID::t_GlobalID; |
| 2331 | break; |
| 2332 | case lltok::GlobalVar: // @foo |
| 2333 | ID.StrVal = Lex.getStrVal(); |
| 2334 | ID.Kind = ValID::t_GlobalName; |
| 2335 | break; |
| 2336 | case lltok::LocalVarID: // %42 |
| 2337 | ID.UIntVal = Lex.getUIntVal(); |
| 2338 | ID.Kind = ValID::t_LocalID; |
| 2339 | break; |
| 2340 | case lltok::LocalVar: // %foo |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2341 | ID.StrVal = Lex.getStrVal(); |
| 2342 | ID.Kind = ValID::t_LocalName; |
| 2343 | break; |
| 2344 | case lltok::APSInt: |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2345 | ID.APSIntVal = Lex.getAPSIntVal(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2346 | ID.Kind = ValID::t_APSInt; |
| 2347 | break; |
| 2348 | case lltok::APFloat: |
| 2349 | ID.APFloatVal = Lex.getAPFloatVal(); |
| 2350 | ID.Kind = ValID::t_APFloat; |
| 2351 | break; |
| 2352 | case lltok::kw_true: |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 2353 | ID.ConstantVal = ConstantInt::getTrue(Context); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2354 | ID.Kind = ValID::t_Constant; |
| 2355 | break; |
| 2356 | case lltok::kw_false: |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 2357 | ID.ConstantVal = ConstantInt::getFalse(Context); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2358 | ID.Kind = ValID::t_Constant; |
| 2359 | break; |
| 2360 | case lltok::kw_null: ID.Kind = ValID::t_Null; break; |
| 2361 | case lltok::kw_undef: ID.Kind = ValID::t_Undef; break; |
| 2362 | case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2363 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2364 | case lltok::lbrace: { |
| 2365 | // ValID ::= '{' ConstVector '}' |
| 2366 | Lex.Lex(); |
| 2367 | SmallVector<Constant*, 16> Elts; |
| 2368 | if (ParseGlobalValueVector(Elts) || |
| 2369 | ParseToken(lltok::rbrace, "expected end of struct constant")) |
| 2370 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2371 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2372 | ID.ConstantStructElts = new Constant*[Elts.size()]; |
| 2373 | ID.UIntVal = Elts.size(); |
| 2374 | memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0])); |
| 2375 | ID.Kind = ValID::t_ConstantStruct; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2376 | return false; |
| 2377 | } |
| 2378 | case lltok::less: { |
| 2379 | // ValID ::= '<' ConstVector '>' --> Vector. |
| 2380 | // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct. |
| 2381 | Lex.Lex(); |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2382 | bool isPackedStruct = EatIfPresent(lltok::lbrace); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2383 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2384 | SmallVector<Constant*, 16> Elts; |
| 2385 | LocTy FirstEltLoc = Lex.getLoc(); |
| 2386 | if (ParseGlobalValueVector(Elts) || |
| 2387 | (isPackedStruct && |
| 2388 | ParseToken(lltok::rbrace, "expected end of packed struct")) || |
| 2389 | ParseToken(lltok::greater, "expected end of constant")) |
| 2390 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2391 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2392 | if (isPackedStruct) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2393 | ID.ConstantStructElts = new Constant*[Elts.size()]; |
| 2394 | memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0])); |
| 2395 | ID.UIntVal = Elts.size(); |
| 2396 | ID.Kind = ValID::t_PackedConstantStruct; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2397 | return false; |
| 2398 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2399 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2400 | if (Elts.empty()) |
| 2401 | return Error(ID.Loc, "constant vector must not be empty"); |
| 2402 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2403 | if (!Elts[0]->getType()->isIntegerTy() && |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 2404 | !Elts[0]->getType()->isFloatingPointTy() && |
| 2405 | !Elts[0]->getType()->isPointerTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2406 | return Error(FirstEltLoc, |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 2407 | "vector elements must have integer, pointer or floating point type"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2408 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2409 | // Verify that all the vector elements have the same type. |
| 2410 | for (unsigned i = 1, e = Elts.size(); i != e; ++i) |
| 2411 | if (Elts[i]->getType() != Elts[0]->getType()) |
| 2412 | return Error(FirstEltLoc, |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 2413 | "vector element #" + Twine(i) + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 2414 | " is not of type '" + getTypeString(Elts[0]->getType())); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2415 | |
Chris Lattner | 6922931 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 2416 | ID.ConstantVal = ConstantVector::get(Elts); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2417 | ID.Kind = ValID::t_Constant; |
| 2418 | return false; |
| 2419 | } |
| 2420 | case lltok::lsquare: { // Array Constant |
| 2421 | Lex.Lex(); |
| 2422 | SmallVector<Constant*, 16> Elts; |
| 2423 | LocTy FirstEltLoc = Lex.getLoc(); |
| 2424 | if (ParseGlobalValueVector(Elts) || |
| 2425 | ParseToken(lltok::rsquare, "expected end of array constant")) |
| 2426 | return true; |
| 2427 | |
| 2428 | // Handle empty element. |
| 2429 | if (Elts.empty()) { |
| 2430 | // Use undef instead of an array because it's inconvenient to determine |
| 2431 | // the element type at this point, there being no elements to examine. |
Chris Lattner | 998fa0a | 2009-01-05 07:52:51 +0000 | [diff] [blame] | 2432 | ID.Kind = ValID::t_EmptyArray; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2433 | return false; |
| 2434 | } |
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 | if (!Elts[0]->getType()->isFirstClassType()) |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2437 | return Error(FirstEltLoc, "invalid array element type: " + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 2438 | getTypeString(Elts[0]->getType())); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2439 | |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 2440 | ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size()); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2441 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2442 | // Verify all elements are correct type! |
Chris Lattner | 59d0e3b | 2009-01-02 08:49:06 +0000 | [diff] [blame] | 2443 | for (unsigned i = 0, e = Elts.size(); i != e; ++i) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2444 | if (Elts[i]->getType() != Elts[0]->getType()) |
| 2445 | return Error(FirstEltLoc, |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 2446 | "array element #" + Twine(i) + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 2447 | " is not of type '" + getTypeString(Elts[0]->getType())); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2448 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2449 | |
Jay Foad | 83be361 | 2011-06-22 09:24:39 +0000 | [diff] [blame] | 2450 | ID.ConstantVal = ConstantArray::get(ATy, Elts); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2451 | ID.Kind = ValID::t_Constant; |
| 2452 | return false; |
| 2453 | } |
| 2454 | case lltok::kw_c: // c "foo" |
| 2455 | Lex.Lex(); |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 2456 | ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(), |
| 2457 | false); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2458 | if (ParseToken(lltok::StringConstant, "expected string")) return true; |
| 2459 | ID.Kind = ValID::t_Constant; |
| 2460 | return false; |
| 2461 | |
| 2462 | case lltok::kw_asm: { |
Chad Rosier | 6f9c385 | 2013-02-14 20:44:07 +0000 | [diff] [blame] | 2463 | // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ',' |
| 2464 | // STRINGCONSTANT |
Chad Rosier | d8c7610 | 2012-09-05 19:00:49 +0000 | [diff] [blame] | 2465 | bool HasSideEffect, AlignStack, AsmDialect; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2466 | Lex.Lex(); |
| 2467 | if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) || |
Dale Johannesen | 1cfb958 | 2009-10-21 23:28:00 +0000 | [diff] [blame] | 2468 | ParseOptionalToken(lltok::kw_alignstack, AlignStack) || |
Chad Rosier | d8c7610 | 2012-09-05 19:00:49 +0000 | [diff] [blame] | 2469 | ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) || |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 2470 | ParseStringConstant(ID.StrVal) || |
| 2471 | ParseToken(lltok::comma, "expected comma in inline asm expression") || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2472 | ParseToken(lltok::StringConstant, "expected constraint string")) |
| 2473 | return true; |
| 2474 | ID.StrVal2 = Lex.getStrVal(); |
Chad Rosier | f42fad6 | 2012-09-05 00:08:17 +0000 | [diff] [blame] | 2475 | ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) | |
Chad Rosier | d8c7610 | 2012-09-05 19:00:49 +0000 | [diff] [blame] | 2476 | (unsigned(AsmDialect)<<2); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2477 | ID.Kind = ValID::t_InlineAsm; |
| 2478 | return false; |
| 2479 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2480 | |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 2481 | case lltok::kw_blockaddress: { |
| 2482 | // ValID ::= 'blockaddress' '(' @foo ',' %bar ')' |
| 2483 | Lex.Lex(); |
| 2484 | |
| 2485 | ValID Fn, Label; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2486 | |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 2487 | if (ParseToken(lltok::lparen, "expected '(' in block address expression") || |
| 2488 | ParseValID(Fn) || |
| 2489 | ParseToken(lltok::comma, "expected comma in block address expression")|| |
| 2490 | ParseValID(Label) || |
| 2491 | ParseToken(lltok::rparen, "expected ')' in block address expression")) |
| 2492 | return true; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2493 | |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 2494 | if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName) |
| 2495 | return Error(Fn.Loc, "expected function name in blockaddress"); |
Chris Lattner | aa99c94 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 2496 | if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName) |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 2497 | return Error(Label.Loc, "expected basic block name in blockaddress"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2498 | |
Duncan P. N. Exon Smith | 1716990 | 2014-08-19 00:13:19 +0000 | [diff] [blame] | 2499 | // Try to find the function (but skip it if it's forward-referenced). |
| 2500 | GlobalValue *GV = nullptr; |
| 2501 | if (Fn.Kind == ValID::t_GlobalID) { |
| 2502 | if (Fn.UIntVal < NumberedVals.size()) |
| 2503 | GV = NumberedVals[Fn.UIntVal]; |
| 2504 | } else if (!ForwardRefVals.count(Fn.StrVal)) { |
| 2505 | GV = M->getNamedValue(Fn.StrVal); |
| 2506 | } |
| 2507 | Function *F = nullptr; |
| 2508 | if (GV) { |
| 2509 | // Confirm that it's actually a function with a definition. |
| 2510 | if (!isa<Function>(GV)) |
| 2511 | return Error(Fn.Loc, "expected function name in blockaddress"); |
| 2512 | F = cast<Function>(GV); |
| 2513 | if (F->isDeclaration()) |
| 2514 | return Error(Fn.Loc, "cannot take blockaddress inside a declaration"); |
| 2515 | } |
| 2516 | |
| 2517 | if (!F) { |
| 2518 | // Make a global variable as a placeholder for this reference. |
| 2519 | GlobalValue *&FwdRef = ForwardRefBlockAddresses[Fn][Label]; |
| 2520 | if (!FwdRef) |
| 2521 | FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context), false, |
| 2522 | GlobalValue::InternalLinkage, nullptr, ""); |
| 2523 | ID.ConstantVal = FwdRef; |
| 2524 | ID.Kind = ValID::t_Constant; |
| 2525 | return false; |
| 2526 | } |
| 2527 | |
| 2528 | // We found the function; now find the basic block. Don't use PFS, since we |
| 2529 | // might be inside a constant expression. |
| 2530 | BasicBlock *BB; |
| 2531 | if (BlockAddressPFS && F == &BlockAddressPFS->getFunction()) { |
| 2532 | if (Label.Kind == ValID::t_LocalID) |
| 2533 | BB = BlockAddressPFS->GetBB(Label.UIntVal, Label.Loc); |
| 2534 | else |
| 2535 | BB = BlockAddressPFS->GetBB(Label.StrVal, Label.Loc); |
| 2536 | if (!BB) |
| 2537 | return Error(Label.Loc, "referenced value is not a basic block"); |
| 2538 | } else { |
| 2539 | if (Label.Kind == ValID::t_LocalID) |
| 2540 | return Error(Label.Loc, "cannot take address of numeric label after " |
| 2541 | "the function is defined"); |
| 2542 | BB = dyn_cast_or_null<BasicBlock>( |
| 2543 | F->getValueSymbolTable().lookup(Label.StrVal)); |
| 2544 | if (!BB) |
| 2545 | return Error(Label.Loc, "referenced value is not a basic block"); |
| 2546 | } |
| 2547 | |
| 2548 | ID.ConstantVal = BlockAddress::get(F, BB); |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 2549 | ID.Kind = ValID::t_Constant; |
| 2550 | return false; |
| 2551 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2552 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2553 | case lltok::kw_trunc: |
| 2554 | case lltok::kw_zext: |
| 2555 | case lltok::kw_sext: |
| 2556 | case lltok::kw_fptrunc: |
| 2557 | case lltok::kw_fpext: |
| 2558 | case lltok::kw_bitcast: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2559 | case lltok::kw_addrspacecast: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2560 | case lltok::kw_uitofp: |
| 2561 | case lltok::kw_sitofp: |
| 2562 | case lltok::kw_fptoui: |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2563 | case lltok::kw_fptosi: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2564 | case lltok::kw_inttoptr: |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2565 | case lltok::kw_ptrtoint: { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2566 | unsigned Opc = Lex.getUIntVal(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2567 | Type *DestTy = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2568 | Constant *SrcVal; |
| 2569 | Lex.Lex(); |
| 2570 | if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") || |
| 2571 | ParseGlobalTypeAndValue(SrcVal) || |
Dan Gohman | 0b5d042 | 2009-06-15 21:52:11 +0000 | [diff] [blame] | 2572 | ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2573 | ParseType(DestTy) || |
| 2574 | ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast")) |
| 2575 | return true; |
| 2576 | if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy)) |
| 2577 | return Error(ID.Loc, "invalid cast opcode for cast from '" + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 2578 | getTypeString(SrcVal->getType()) + "' to '" + |
| 2579 | getTypeString(DestTy) + "'"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2580 | ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc, |
Owen Anderson | 02a9da3 | 2009-07-01 23:57:11 +0000 | [diff] [blame] | 2581 | SrcVal, DestTy); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2582 | ID.Kind = ValID::t_Constant; |
| 2583 | return false; |
| 2584 | } |
| 2585 | case lltok::kw_extractvalue: { |
| 2586 | Lex.Lex(); |
| 2587 | Constant *Val; |
| 2588 | SmallVector<unsigned, 4> Indices; |
| 2589 | if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")|| |
| 2590 | ParseGlobalTypeAndValue(Val) || |
| 2591 | ParseIndexList(Indices) || |
| 2592 | ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr")) |
| 2593 | return true; |
Devang Patel | 1cb5116 | 2009-11-03 19:06:07 +0000 | [diff] [blame] | 2594 | |
Chris Lattner | 392be58 | 2010-02-12 20:49:41 +0000 | [diff] [blame] | 2595 | if (!Val->getType()->isAggregateType()) |
| 2596 | return Error(ID.Loc, "extractvalue operand must be aggregate type"); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2597 | if (!ExtractValueInst::getIndexedType(Val->getType(), Indices)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2598 | return Error(ID.Loc, "invalid indices for extractvalue"); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2599 | ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2600 | ID.Kind = ValID::t_Constant; |
| 2601 | return false; |
| 2602 | } |
| 2603 | case lltok::kw_insertvalue: { |
| 2604 | Lex.Lex(); |
| 2605 | Constant *Val0, *Val1; |
| 2606 | SmallVector<unsigned, 4> Indices; |
| 2607 | if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")|| |
| 2608 | ParseGlobalTypeAndValue(Val0) || |
| 2609 | ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")|| |
| 2610 | ParseGlobalTypeAndValue(Val1) || |
| 2611 | ParseIndexList(Indices) || |
| 2612 | ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr")) |
| 2613 | return true; |
Chris Lattner | 392be58 | 2010-02-12 20:49:41 +0000 | [diff] [blame] | 2614 | if (!Val0->getType()->isAggregateType()) |
| 2615 | return Error(ID.Loc, "insertvalue operand must be aggregate type"); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2616 | if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2617 | return Error(ID.Loc, "invalid indices for insertvalue"); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2618 | ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2619 | ID.Kind = ValID::t_Constant; |
| 2620 | return false; |
| 2621 | } |
| 2622 | case lltok::kw_icmp: |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 2623 | case lltok::kw_fcmp: { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2624 | unsigned PredVal, Opc = Lex.getUIntVal(); |
| 2625 | Constant *Val0, *Val1; |
| 2626 | Lex.Lex(); |
| 2627 | if (ParseCmpPredicate(PredVal, Opc) || |
| 2628 | ParseToken(lltok::lparen, "expected '(' in compare constantexpr") || |
| 2629 | ParseGlobalTypeAndValue(Val0) || |
| 2630 | ParseToken(lltok::comma, "expected comma in compare constantexpr") || |
| 2631 | ParseGlobalTypeAndValue(Val1) || |
| 2632 | ParseToken(lltok::rparen, "expected ')' in compare constantexpr")) |
| 2633 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2634 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2635 | if (Val0->getType() != Val1->getType()) |
| 2636 | return Error(ID.Loc, "compare operands must have the same type"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2637 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2638 | CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2639 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2640 | if (Opc == Instruction::FCmp) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2641 | if (!Val0->getType()->isFPOrFPVectorTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2642 | return Error(ID.Loc, "fcmp requires floating point operands"); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2643 | ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1); |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 2644 | } else { |
| 2645 | assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2646 | if (!Val0->getType()->isIntOrIntVectorTy() && |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 2647 | !Val0->getType()->getScalarType()->isPointerTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2648 | return Error(ID.Loc, "icmp requires pointer or integer operands"); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2649 | ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2650 | } |
| 2651 | ID.Kind = ValID::t_Constant; |
| 2652 | return false; |
| 2653 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2654 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2655 | // Binary Operators. |
| 2656 | case lltok::kw_add: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2657 | case lltok::kw_fadd: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2658 | case lltok::kw_sub: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2659 | case lltok::kw_fsub: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2660 | case lltok::kw_mul: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2661 | case lltok::kw_fmul: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2662 | case lltok::kw_udiv: |
| 2663 | case lltok::kw_sdiv: |
| 2664 | case lltok::kw_fdiv: |
| 2665 | case lltok::kw_urem: |
| 2666 | case lltok::kw_srem: |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2667 | case lltok::kw_frem: |
| 2668 | case lltok::kw_shl: |
| 2669 | case lltok::kw_lshr: |
| 2670 | case lltok::kw_ashr: { |
Dan Gohman | 9c7f808 | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 2671 | bool NUW = false; |
| 2672 | bool NSW = false; |
| 2673 | bool Exact = false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2674 | unsigned Opc = Lex.getUIntVal(); |
| 2675 | Constant *Val0, *Val1; |
| 2676 | Lex.Lex(); |
Dan Gohman | 9c7f808 | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 2677 | LocTy ModifierLoc = Lex.getLoc(); |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2678 | if (Opc == Instruction::Add || Opc == Instruction::Sub || |
| 2679 | Opc == Instruction::Mul || Opc == Instruction::Shl) { |
Dan Gohman | 9c7f808 | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 2680 | if (EatIfPresent(lltok::kw_nuw)) |
| 2681 | NUW = true; |
| 2682 | if (EatIfPresent(lltok::kw_nsw)) { |
| 2683 | NSW = true; |
| 2684 | if (EatIfPresent(lltok::kw_nuw)) |
| 2685 | NUW = true; |
| 2686 | } |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2687 | } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv || |
| 2688 | Opc == Instruction::LShr || Opc == Instruction::AShr) { |
Dan Gohman | 9c7f808 | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 2689 | if (EatIfPresent(lltok::kw_exact)) |
| 2690 | Exact = true; |
| 2691 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2692 | if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") || |
| 2693 | ParseGlobalTypeAndValue(Val0) || |
| 2694 | ParseToken(lltok::comma, "expected comma in binary constantexpr") || |
| 2695 | ParseGlobalTypeAndValue(Val1) || |
| 2696 | ParseToken(lltok::rparen, "expected ')' in binary constantexpr")) |
| 2697 | return true; |
| 2698 | if (Val0->getType() != Val1->getType()) |
| 2699 | return Error(ID.Loc, "operands of constexpr must have same type"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2700 | if (!Val0->getType()->isIntOrIntVectorTy()) { |
Dan Gohman | 9c7f808 | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 2701 | if (NUW) |
| 2702 | return Error(ModifierLoc, "nuw only applies to integer operations"); |
| 2703 | if (NSW) |
| 2704 | return Error(ModifierLoc, "nsw only applies to integer operations"); |
| 2705 | } |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 2706 | // Check that the type is valid for the operator. |
| 2707 | switch (Opc) { |
| 2708 | case Instruction::Add: |
| 2709 | case Instruction::Sub: |
| 2710 | case Instruction::Mul: |
| 2711 | case Instruction::UDiv: |
| 2712 | case Instruction::SDiv: |
| 2713 | case Instruction::URem: |
| 2714 | case Instruction::SRem: |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2715 | case Instruction::Shl: |
| 2716 | case Instruction::AShr: |
| 2717 | case Instruction::LShr: |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 2718 | if (!Val0->getType()->isIntOrIntVectorTy()) |
| 2719 | return Error(ID.Loc, "constexpr requires integer operands"); |
| 2720 | break; |
| 2721 | case Instruction::FAdd: |
| 2722 | case Instruction::FSub: |
| 2723 | case Instruction::FMul: |
| 2724 | case Instruction::FDiv: |
| 2725 | case Instruction::FRem: |
| 2726 | if (!Val0->getType()->isFPOrFPVectorTy()) |
| 2727 | return Error(ID.Loc, "constexpr requires fp operands"); |
| 2728 | break; |
| 2729 | default: llvm_unreachable("Unknown binary operator!"); |
| 2730 | } |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2731 | unsigned Flags = 0; |
| 2732 | if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap; |
| 2733 | if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap; |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 2734 | if (Exact) Flags |= PossiblyExactOperator::IsExact; |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2735 | Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags); |
Dan Gohman | 9c7f808 | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 2736 | ID.ConstantVal = C; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2737 | ID.Kind = ValID::t_Constant; |
| 2738 | return false; |
| 2739 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2740 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2741 | // Logical Operations |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2742 | case lltok::kw_and: |
| 2743 | case lltok::kw_or: |
| 2744 | case lltok::kw_xor: { |
| 2745 | unsigned Opc = Lex.getUIntVal(); |
| 2746 | Constant *Val0, *Val1; |
| 2747 | Lex.Lex(); |
| 2748 | if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") || |
| 2749 | ParseGlobalTypeAndValue(Val0) || |
| 2750 | ParseToken(lltok::comma, "expected comma in logical constantexpr") || |
| 2751 | ParseGlobalTypeAndValue(Val1) || |
| 2752 | ParseToken(lltok::rparen, "expected ')' in logical constantexpr")) |
| 2753 | return true; |
| 2754 | if (Val0->getType() != Val1->getType()) |
| 2755 | return Error(ID.Loc, "operands of constexpr must have same type"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2756 | if (!Val0->getType()->isIntOrIntVectorTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2757 | return Error(ID.Loc, |
| 2758 | "constexpr requires integer or integer vector operands"); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2759 | ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2760 | ID.Kind = ValID::t_Constant; |
| 2761 | return false; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2764 | case lltok::kw_getelementptr: |
| 2765 | case lltok::kw_shufflevector: |
| 2766 | case lltok::kw_insertelement: |
| 2767 | case lltok::kw_extractelement: |
| 2768 | case lltok::kw_select: { |
| 2769 | unsigned Opc = Lex.getUIntVal(); |
| 2770 | SmallVector<Constant*, 16> Elts; |
Dan Gohman | 1639c39 | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 2771 | bool InBounds = false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2772 | Lex.Lex(); |
Dan Gohman | 1639c39 | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 2773 | if (Opc == Instruction::GetElementPtr) |
Dan Gohman | 16cbbe4 | 2009-07-29 15:58:36 +0000 | [diff] [blame] | 2774 | InBounds = EatIfPresent(lltok::kw_inbounds); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2775 | if (ParseToken(lltok::lparen, "expected '(' in constantexpr") || |
| 2776 | ParseGlobalValueVector(Elts) || |
| 2777 | ParseToken(lltok::rparen, "expected ')' in constantexpr")) |
| 2778 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2779 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2780 | if (Opc == Instruction::GetElementPtr) { |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 2781 | if (Elts.size() == 0 || |
| 2782 | !Elts[0]->getType()->getScalarType()->isPointerTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2783 | return Error(ID.Loc, "getelementptr requires pointer operand"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2784 | |
Jay Foad | ed8db7d | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 2785 | ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end()); |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 2786 | if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), Indices)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2787 | return Error(ID.Loc, "invalid indices for getelementptr"); |
Jay Foad | 2f5fc8c | 2011-07-21 15:15:37 +0000 | [diff] [blame] | 2788 | ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], Indices, |
| 2789 | InBounds); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2790 | } else if (Opc == Instruction::Select) { |
| 2791 | if (Elts.size() != 3) |
| 2792 | return Error(ID.Loc, "expected three operands to select"); |
| 2793 | if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1], |
| 2794 | Elts[2])) |
| 2795 | return Error(ID.Loc, Reason); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2796 | ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2797 | } else if (Opc == Instruction::ShuffleVector) { |
| 2798 | if (Elts.size() != 3) |
| 2799 | return Error(ID.Loc, "expected three operands to shufflevector"); |
| 2800 | if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2])) |
| 2801 | return Error(ID.Loc, "invalid operands to shufflevector"); |
Owen Anderson | 02a9da3 | 2009-07-01 23:57:11 +0000 | [diff] [blame] | 2802 | ID.ConstantVal = |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2803 | ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2804 | } else if (Opc == Instruction::ExtractElement) { |
| 2805 | if (Elts.size() != 2) |
| 2806 | return Error(ID.Loc, "expected two operands to extractelement"); |
| 2807 | if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1])) |
| 2808 | return Error(ID.Loc, "invalid extractelement operands"); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2809 | ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2810 | } else { |
| 2811 | assert(Opc == Instruction::InsertElement && "Unknown opcode"); |
| 2812 | if (Elts.size() != 3) |
| 2813 | return Error(ID.Loc, "expected three operands to insertelement"); |
| 2814 | if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2])) |
| 2815 | return Error(ID.Loc, "invalid insertelement operands"); |
Owen Anderson | 02a9da3 | 2009-07-01 23:57:11 +0000 | [diff] [blame] | 2816 | ID.ConstantVal = |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2817 | ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2818 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2819 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2820 | ID.Kind = ValID::t_Constant; |
| 2821 | return false; |
| 2822 | } |
| 2823 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2824 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2825 | Lex.Lex(); |
| 2826 | return false; |
| 2827 | } |
| 2828 | |
| 2829 | /// ParseGlobalValue - Parse a global value with the specified type. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2830 | bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2831 | C = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2832 | ValID ID; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2833 | Value *V = nullptr; |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 2834 | bool Parsed = ParseValID(ID) || |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2835 | ConvertValIDToValue(Ty, ID, V, nullptr); |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 2836 | if (V && !(C = dyn_cast<Constant>(V))) |
| 2837 | return Error(ID.Loc, "global values must be constants"); |
| 2838 | return Parsed; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 2841 | bool LLParser::ParseGlobalTypeAndValue(Constant *&V) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2842 | Type *Ty = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2843 | return ParseType(Ty) || |
| 2844 | ParseGlobalValue(Ty, V); |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 2845 | } |
| 2846 | |
Rafael Espindola | 83a362c | 2015-01-06 22:55:16 +0000 | [diff] [blame] | 2847 | bool LLParser::parseOptionalComdat(StringRef GlobalName, Comdat *&C) { |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 2848 | C = nullptr; |
Rafael Espindola | 83a362c | 2015-01-06 22:55:16 +0000 | [diff] [blame] | 2849 | |
| 2850 | LocTy KwLoc = Lex.getLoc(); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 2851 | if (!EatIfPresent(lltok::kw_comdat)) |
| 2852 | return false; |
Rafael Espindola | 83a362c | 2015-01-06 22:55:16 +0000 | [diff] [blame] | 2853 | |
| 2854 | if (EatIfPresent(lltok::lparen)) { |
| 2855 | if (Lex.getKind() != lltok::ComdatVar) |
| 2856 | return TokError("expected comdat variable"); |
| 2857 | C = getComdat(Lex.getStrVal(), Lex.getLoc()); |
| 2858 | Lex.Lex(); |
| 2859 | if (ParseToken(lltok::rparen, "expected ')' after comdat var")) |
| 2860 | return true; |
| 2861 | } else { |
| 2862 | if (GlobalName.empty()) |
| 2863 | return TokError("comdat cannot be unnamed"); |
| 2864 | C = getComdat(GlobalName, KwLoc); |
| 2865 | } |
| 2866 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 2867 | return false; |
| 2868 | } |
| 2869 | |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 2870 | /// ParseGlobalValueVector |
| 2871 | /// ::= /*empty*/ |
| 2872 | /// ::= TypeAndValue (',' TypeAndValue)* |
Duncan P. N. Exon Smith | 1716990 | 2014-08-19 00:13:19 +0000 | [diff] [blame] | 2873 | bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts) { |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 2874 | // Empty list. |
| 2875 | if (Lex.getKind() == lltok::rbrace || |
| 2876 | Lex.getKind() == lltok::rsquare || |
| 2877 | Lex.getKind() == lltok::greater || |
| 2878 | Lex.getKind() == lltok::rparen) |
| 2879 | return false; |
| 2880 | |
| 2881 | Constant *C; |
| 2882 | if (ParseGlobalTypeAndValue(C)) return true; |
| 2883 | Elts.push_back(C); |
| 2884 | |
| 2885 | while (EatIfPresent(lltok::comma)) { |
| 2886 | if (ParseGlobalTypeAndValue(C)) return true; |
| 2887 | Elts.push_back(C); |
| 2888 | } |
| 2889 | |
| 2890 | return false; |
| 2891 | } |
| 2892 | |
Duncan P. N. Exon Smith | 58ef9d1 | 2015-01-12 21:23:11 +0000 | [diff] [blame] | 2893 | bool LLParser::ParseMDTuple(MDNode *&MD, bool IsDistinct) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 2894 | SmallVector<Metadata *, 16> Elts; |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 2895 | if (ParseMDNodeVector(Elts)) |
Dan Gohman | c828c54 | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 2896 | return true; |
| 2897 | |
Duncan P. N. Exon Smith | dbcff30 | 2015-01-12 22:23:04 +0000 | [diff] [blame] | 2898 | MD = (IsDistinct ? MDNode::getDistinct : MDNode::get)(Context, Elts); |
Dan Gohman | c828c54 | 2010-08-24 02:24:03 +0000 | [diff] [blame] | 2899 | return false; |
| 2900 | } |
| 2901 | |
Duncan P. N. Exon Smith | f825dae | 2015-01-12 22:26:48 +0000 | [diff] [blame^] | 2902 | /// MDNode: |
| 2903 | /// ::= !{ ... } |
| 2904 | /// ::= !7 |
| 2905 | bool LLParser::ParseMDNode(MDNode *&N) { |
| 2906 | return ParseToken(lltok::exclaim, "expected '!' here") || |
| 2907 | ParseMDNodeTail(N); |
| 2908 | } |
| 2909 | |
| 2910 | bool LLParser::ParseMDNodeTail(MDNode *&N) { |
| 2911 | // !{ ... } |
| 2912 | if (Lex.getKind() == lltok::lbrace) |
| 2913 | return ParseMDTuple(N); |
| 2914 | |
| 2915 | // !42 |
| 2916 | return ParseMDNodeID(N); |
| 2917 | } |
| 2918 | |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 2919 | /// ParseMetadataAsValue |
| 2920 | /// ::= metadata i32 %local |
| 2921 | /// ::= metadata i32 @global |
| 2922 | /// ::= metadata i32 7 |
| 2923 | /// ::= metadata !0 |
| 2924 | /// ::= metadata !{...} |
| 2925 | /// ::= metadata !"string" |
| 2926 | bool LLParser::ParseMetadataAsValue(Value *&V, PerFunctionState &PFS) { |
| 2927 | // Note: the type 'metadata' has already been parsed. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 2928 | Metadata *MD; |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 2929 | if (ParseMetadata(MD, &PFS)) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 2930 | return true; |
| 2931 | |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 2932 | V = MetadataAsValue::get(Context, MD); |
| 2933 | return false; |
| 2934 | } |
| 2935 | |
| 2936 | /// ParseValueAsMetadata |
| 2937 | /// ::= i32 %local |
| 2938 | /// ::= i32 @global |
| 2939 | /// ::= i32 7 |
| 2940 | bool LLParser::ParseValueAsMetadata(Metadata *&MD, PerFunctionState *PFS) { |
| 2941 | Type *Ty; |
| 2942 | LocTy Loc; |
| 2943 | if (ParseType(Ty, "expected metadata operand", Loc)) |
| 2944 | return true; |
| 2945 | if (Ty->isMetadataTy()) |
| 2946 | return Error(Loc, "invalid metadata-value-metadata roundtrip"); |
| 2947 | |
| 2948 | Value *V; |
| 2949 | if (ParseValue(Ty, V, PFS)) |
| 2950 | return true; |
| 2951 | |
| 2952 | MD = ValueAsMetadata::get(V); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 2953 | return false; |
| 2954 | } |
| 2955 | |
| 2956 | /// ParseMetadata |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 2957 | /// ::= i32 %local |
| 2958 | /// ::= i32 @global |
| 2959 | /// ::= i32 7 |
Dan Gohman | 8939ba33 | 2010-07-14 18:26:50 +0000 | [diff] [blame] | 2960 | /// ::= !42 |
| 2961 | /// ::= !{...} |
| 2962 | /// ::= !"string" |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 2963 | bool LLParser::ParseMetadata(Metadata *&MD, PerFunctionState *PFS) { |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 2964 | // ValueAsMetadata: |
| 2965 | // <type> <value> |
| 2966 | if (Lex.getKind() != lltok::exclaim) |
| 2967 | return ParseValueAsMetadata(MD, PFS); |
| 2968 | |
| 2969 | // '!'. |
| 2970 | assert(Lex.getKind() == lltok::exclaim && "Expected '!' here"); |
| 2971 | Lex.Lex(); |
Dan Gohman | 8939ba33 | 2010-07-14 18:26:50 +0000 | [diff] [blame] | 2972 | |
Duncan P. N. Exon Smith | 62a7919 | 2015-01-12 22:24:50 +0000 | [diff] [blame] | 2973 | // MDString: |
| 2974 | // ::= '!' STRINGCONSTANT |
| 2975 | if (Lex.getKind() == lltok::StringConstant) { |
| 2976 | MDString *S; |
| 2977 | if (ParseMDString(S)) |
| 2978 | return true; |
| 2979 | MD = S; |
| 2980 | return false; |
| 2981 | } |
| 2982 | |
Dan Gohman | 8939ba33 | 2010-07-14 18:26:50 +0000 | [diff] [blame] | 2983 | // MDNode: |
| 2984 | // !{ ... } |
Duncan P. N. Exon Smith | f825dae | 2015-01-12 22:26:48 +0000 | [diff] [blame^] | 2985 | // !7 |
Duncan P. N. Exon Smith | 62a7919 | 2015-01-12 22:24:50 +0000 | [diff] [blame] | 2986 | MDNode *N; |
Duncan P. N. Exon Smith | f825dae | 2015-01-12 22:26:48 +0000 | [diff] [blame^] | 2987 | if (ParseMDNodeTail(N)) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 2988 | return true; |
Duncan P. N. Exon Smith | 62a7919 | 2015-01-12 22:24:50 +0000 | [diff] [blame] | 2989 | MD = N; |
Dan Gohman | 8939ba33 | 2010-07-14 18:26:50 +0000 | [diff] [blame] | 2990 | return false; |
| 2991 | } |
| 2992 | |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 2993 | |
| 2994 | //===----------------------------------------------------------------------===// |
| 2995 | // Function Parsing. |
| 2996 | //===----------------------------------------------------------------------===// |
| 2997 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2998 | bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V, |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 2999 | PerFunctionState *PFS) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3000 | if (Ty->isFunctionTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3001 | 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] | 3002 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3003 | switch (ID.Kind) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3004 | case ValID::t_LocalID: |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 3005 | if (!PFS) return Error(ID.Loc, "invalid use of function-local name"); |
| 3006 | V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3007 | return V == nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3008 | case ValID::t_LocalName: |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 3009 | if (!PFS) return Error(ID.Loc, "invalid use of function-local name"); |
| 3010 | V = PFS->GetVal(ID.StrVal, Ty, ID.Loc); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3011 | return V == nullptr; |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 3012 | case ValID::t_InlineAsm: { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3013 | PointerType *PTy = dyn_cast<PointerType>(Ty); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3014 | FunctionType *FTy = |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3015 | PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : nullptr; |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 3016 | if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2)) |
| 3017 | return Error(ID.Loc, "invalid type for inline asm constraint string"); |
Chad Rosier | f42fad6 | 2012-09-05 00:08:17 +0000 | [diff] [blame] | 3018 | V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1, |
Chad Rosier | d8c7610 | 2012-09-05 19:00:49 +0000 | [diff] [blame] | 3019 | (ID.UIntVal>>1)&1, (InlineAsm::AsmDialect(ID.UIntVal>>2))); |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 3020 | return false; |
| 3021 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3022 | case ValID::t_GlobalName: |
| 3023 | V = GetGlobalVal(ID.StrVal, Ty, ID.Loc); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3024 | return V == nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3025 | case ValID::t_GlobalID: |
| 3026 | V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3027 | return V == nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3028 | case ValID::t_APSInt: |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3029 | if (!Ty->isIntegerTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3030 | return Error(ID.Loc, "integer constant must have integer type"); |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 3031 | ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits()); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 3032 | V = ConstantInt::get(Context, ID.APSIntVal); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3033 | return false; |
| 3034 | case ValID::t_APFloat: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3035 | if (!Ty->isFloatingPointTy() || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3036 | !ConstantFP::isValueValidForType(Ty, ID.APFloatVal)) |
| 3037 | return Error(ID.Loc, "floating point constant invalid for type"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3038 | |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 3039 | // The lexer has no type info, so builds all half, float, and double FP |
| 3040 | // constants as double. Fix this here. Long double does not need this. |
| 3041 | if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3042 | bool Ignored; |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 3043 | if (Ty->isHalfTy()) |
| 3044 | ID.APFloatVal.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven, |
| 3045 | &Ignored); |
| 3046 | else if (Ty->isFloatTy()) |
| 3047 | ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, |
| 3048 | &Ignored); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3049 | } |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 3050 | V = ConstantFP::get(Context, ID.APFloatVal); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3051 | |
Chris Lattner | 8f57d29e | 2009-01-05 18:24:23 +0000 | [diff] [blame] | 3052 | if (V->getType() != Ty) |
| 3053 | return Error(ID.Loc, "floating point constant does not have type '" + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 3054 | getTypeString(Ty) + "'"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3055 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3056 | return false; |
| 3057 | case ValID::t_Null: |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3058 | if (!Ty->isPointerTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3059 | return Error(ID.Loc, "null must be a pointer type"); |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 3060 | V = ConstantPointerNull::get(cast<PointerType>(Ty)); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3061 | return false; |
| 3062 | case ValID::t_Undef: |
Chris Lattner | ffa0778 | 2009-01-05 08:13:38 +0000 | [diff] [blame] | 3063 | // FIXME: LabelTy should not be a first-class type. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3064 | if (!Ty->isFirstClassType() || Ty->isLabelTy()) |
Chris Lattner | ffa0778 | 2009-01-05 08:13:38 +0000 | [diff] [blame] | 3065 | return Error(ID.Loc, "invalid type for undef constant"); |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 3066 | V = UndefValue::get(Ty); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3067 | return false; |
Chris Lattner | 998fa0a | 2009-01-05 07:52:51 +0000 | [diff] [blame] | 3068 | case ValID::t_EmptyArray: |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3069 | if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0) |
Chris Lattner | 998fa0a | 2009-01-05 07:52:51 +0000 | [diff] [blame] | 3070 | return Error(ID.Loc, "invalid empty array initializer"); |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 3071 | V = UndefValue::get(Ty); |
Chris Lattner | 998fa0a | 2009-01-05 07:52:51 +0000 | [diff] [blame] | 3072 | return false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3073 | case ValID::t_Zero: |
Chris Lattner | ffa0778 | 2009-01-05 08:13:38 +0000 | [diff] [blame] | 3074 | // FIXME: LabelTy should not be a first-class type. |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 3075 | if (!Ty->isFirstClassType() || Ty->isLabelTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3076 | return Error(ID.Loc, "invalid type for null constant"); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 3077 | V = Constant::getNullValue(Ty); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3078 | return false; |
| 3079 | case ValID::t_Constant: |
Chris Lattner | 13ee795 | 2010-08-28 04:09:24 +0000 | [diff] [blame] | 3080 | if (ID.ConstantVal->getType() != Ty) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3081 | return Error(ID.Loc, "constant expression type mismatch"); |
Chris Lattner | 392be58 | 2010-02-12 20:49:41 +0000 | [diff] [blame] | 3082 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3083 | V = ID.ConstantVal; |
| 3084 | return false; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3085 | case ValID::t_ConstantStruct: |
| 3086 | case ValID::t_PackedConstantStruct: |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3087 | if (StructType *ST = dyn_cast<StructType>(Ty)) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3088 | if (ST->getNumElements() != ID.UIntVal) |
| 3089 | return Error(ID.Loc, |
| 3090 | "initializer with struct type has wrong # elements"); |
| 3091 | if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct)) |
| 3092 | 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] | 3093 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3094 | // Verify that the elements are compatible with the structtype. |
| 3095 | for (unsigned i = 0, e = ID.UIntVal; i != e; ++i) |
| 3096 | if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i)) |
| 3097 | return Error(ID.Loc, "element " + Twine(i) + |
| 3098 | " of struct initializer doesn't match struct element type"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3099 | |
Frits van Bommel | 717d7ed | 2011-07-18 12:00:32 +0000 | [diff] [blame] | 3100 | V = ConstantStruct::get(ST, makeArrayRef(ID.ConstantStructElts, |
| 3101 | ID.UIntVal)); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3102 | } else |
| 3103 | return Error(ID.Loc, "constant expression type mismatch"); |
| 3104 | return false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3105 | } |
Chandler Carruth | f3e8502 | 2012-01-10 18:08:01 +0000 | [diff] [blame] | 3106 | llvm_unreachable("Invalid ValID"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3107 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3108 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3109 | bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3110 | V = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3111 | ValID ID; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3112 | return ParseValID(ID, PFS) || |
| 3113 | ConvertValIDToValue(Ty, ID, V, PFS); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3116 | bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3117 | Type *Ty = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3118 | return ParseType(Ty) || |
| 3119 | ParseValue(Ty, V, PFS); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3120 | } |
| 3121 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3122 | bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc, |
| 3123 | PerFunctionState &PFS) { |
| 3124 | Value *V; |
| 3125 | Loc = Lex.getLoc(); |
| 3126 | if (ParseTypeAndValue(V, PFS)) return true; |
| 3127 | if (!isa<BasicBlock>(V)) |
| 3128 | return Error(Loc, "expected a basic block"); |
| 3129 | BB = cast<BasicBlock>(V); |
| 3130 | return false; |
| 3131 | } |
| 3132 | |
| 3133 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3134 | /// FunctionHeader |
| 3135 | /// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs |
Rafael Espindola | 45e6c19 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 3136 | /// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 3137 | /// OptionalAlign OptGC OptionalPrefix OptionalPrologue |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3138 | bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) { |
| 3139 | // Parse the linkage. |
| 3140 | LocTy LinkageLoc = Lex.getLoc(); |
| 3141 | unsigned Linkage; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3142 | |
Kostya Serebryany | a5054ad | 2012-01-20 17:56:17 +0000 | [diff] [blame] | 3143 | unsigned Visibility; |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 3144 | unsigned DLLStorageClass; |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 3145 | AttrBuilder RetAttrs; |
Alexey Samsonov | 17a9cff | 2014-09-10 18:00:17 +0000 | [diff] [blame] | 3146 | unsigned CC; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3147 | Type *RetType = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3148 | LocTy RetTypeLoc = Lex.getLoc(); |
| 3149 | if (ParseOptionalLinkage(Linkage) || |
| 3150 | ParseOptionalVisibility(Visibility) || |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 3151 | ParseOptionalDLLStorageClass(DLLStorageClass) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3152 | ParseOptionalCallingConv(CC) || |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 3153 | ParseOptionalReturnAttrs(RetAttrs) || |
Chris Lattner | f880ca2 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 3154 | ParseType(RetType, RetTypeLoc, true /*void allowed*/)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3155 | return true; |
| 3156 | |
| 3157 | // Verify that the linkage is ok. |
| 3158 | switch ((GlobalValue::LinkageTypes)Linkage) { |
| 3159 | case GlobalValue::ExternalLinkage: |
| 3160 | break; // always ok. |
Duncan Sands | e288105 | 2009-03-11 08:08:06 +0000 | [diff] [blame] | 3161 | case GlobalValue::ExternalWeakLinkage: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3162 | if (isDefine) |
| 3163 | return Error(LinkageLoc, "invalid linkage for function definition"); |
| 3164 | break; |
Rafael Espindola | 6de96a1 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 3165 | case GlobalValue::PrivateLinkage: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3166 | case GlobalValue::InternalLinkage: |
Nick Lewycky | 8019af6 | 2009-04-13 07:02:02 +0000 | [diff] [blame] | 3167 | case GlobalValue::AvailableExternallyLinkage: |
Duncan Sands | 12da8ce | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 3168 | case GlobalValue::LinkOnceAnyLinkage: |
| 3169 | case GlobalValue::LinkOnceODRLinkage: |
| 3170 | case GlobalValue::WeakAnyLinkage: |
| 3171 | case GlobalValue::WeakODRLinkage: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3172 | if (!isDefine) |
| 3173 | return Error(LinkageLoc, "invalid linkage for function declaration"); |
| 3174 | break; |
| 3175 | case GlobalValue::AppendingLinkage: |
Duncan Sands | 4581beb | 2009-03-11 20:14:15 +0000 | [diff] [blame] | 3176 | case GlobalValue::CommonLinkage: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3177 | return Error(LinkageLoc, "invalid function linkage type"); |
| 3178 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3179 | |
Duncan P. N. Exon Smith | b80de10 | 2014-05-07 22:57:20 +0000 | [diff] [blame] | 3180 | if (!isValidVisibilityForLinkage(Visibility, Linkage)) |
| 3181 | return Error(LinkageLoc, |
| 3182 | "symbol with local linkage must have default visibility"); |
| 3183 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3184 | if (!FunctionType::isValidReturnType(RetType)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3185 | return Error(RetTypeLoc, "invalid function return type"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3186 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3187 | LocTy NameLoc = Lex.getLoc(); |
Chris Lattner | 778c62c | 2009-02-18 21:48:13 +0000 | [diff] [blame] | 3188 | |
| 3189 | std::string FunctionName; |
| 3190 | if (Lex.getKind() == lltok::GlobalVar) { |
| 3191 | FunctionName = Lex.getStrVal(); |
| 3192 | } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok. |
| 3193 | unsigned NameID = Lex.getUIntVal(); |
| 3194 | |
| 3195 | if (NameID != NumberedVals.size()) |
| 3196 | return TokError("function expected to be numbered '%" + |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 3197 | Twine(NumberedVals.size()) + "'"); |
Chris Lattner | 778c62c | 2009-02-18 21:48:13 +0000 | [diff] [blame] | 3198 | } else { |
| 3199 | return TokError("expected function name"); |
| 3200 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3201 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 3202 | Lex.Lex(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3203 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 3204 | if (Lex.getKind() != lltok::lparen) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3205 | return TokError("expected '(' in function argument list"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3206 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3207 | SmallVector<ArgInfo, 8> ArgList; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3208 | bool isVarArg; |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 3209 | AttrBuilder FuncAttrs; |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 3210 | std::vector<unsigned> FwdRefAttrGrps; |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 3211 | LocTy BuiltinLoc; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3212 | std::string Section; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3213 | unsigned Alignment; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 3214 | std::string GC; |
Rafael Espindola | 563eb4b | 2011-01-25 19:09:56 +0000 | [diff] [blame] | 3215 | bool UnnamedAddr; |
| 3216 | LocTy UnnamedAddrLoc; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3217 | Constant *Prefix = nullptr; |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 3218 | Constant *Prologue = nullptr; |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 3219 | Comdat *C; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 3220 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3221 | if (ParseArgumentList(ArgList, isVarArg) || |
Rafael Espindola | 563eb4b | 2011-01-25 19:09:56 +0000 | [diff] [blame] | 3222 | ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr, |
| 3223 | &UnnamedAddrLoc) || |
Bill Wendling | 09bd1f7 | 2013-02-22 00:12:35 +0000 | [diff] [blame] | 3224 | ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false, |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 3225 | BuiltinLoc) || |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 3226 | (EatIfPresent(lltok::kw_section) && |
| 3227 | ParseStringConstant(Section)) || |
Rafael Espindola | 83a362c | 2015-01-06 22:55:16 +0000 | [diff] [blame] | 3228 | parseOptionalComdat(FunctionName, C) || |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 3229 | ParseOptionalAlignment(Alignment) || |
| 3230 | (EatIfPresent(lltok::kw_gc) && |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 3231 | ParseStringConstant(GC)) || |
| 3232 | (EatIfPresent(lltok::kw_prefix) && |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 3233 | ParseGlobalTypeAndValue(Prefix)) || |
| 3234 | (EatIfPresent(lltok::kw_prologue) && |
| 3235 | ParseGlobalTypeAndValue(Prologue))) |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 3236 | return true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3237 | |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 3238 | if (FuncAttrs.contains(Attribute::Builtin)) |
| 3239 | return Error(BuiltinLoc, "'builtin' attribute not valid on function"); |
Bill Wendling | 09bd1f7 | 2013-02-22 00:12:35 +0000 | [diff] [blame] | 3240 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3241 | // 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] | 3242 | if (FuncAttrs.hasAlignmentAttr()) { |
Bill Wendling | 9be7759 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 3243 | Alignment = FuncAttrs.getAlignment(); |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 3244 | FuncAttrs.removeAttribute(Attribute::Alignment); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3245 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3246 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3247 | // Okay, if we got here, the function is syntactically valid. Convert types |
| 3248 | // and do semantic checks. |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 3249 | std::vector<Type*> ParamTypeList; |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 3250 | SmallVector<AttributeSet, 8> Attrs; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3251 | |
Bill Wendling | 3bef2dd | 2012-09-19 23:54:18 +0000 | [diff] [blame] | 3252 | if (RetAttrs.hasAttributes()) |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 3253 | Attrs.push_back(AttributeSet::get(RetType->getContext(), |
| 3254 | AttributeSet::ReturnIndex, |
| 3255 | RetAttrs)); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3256 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3257 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3258 | ParamTypeList.push_back(ArgList[i].Ty); |
Bill Wendling | fe0021a | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 3259 | if (ArgList[i].Attrs.hasAttributes(i + 1)) { |
| 3260 | AttrBuilder B(ArgList[i].Attrs, i + 1); |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 3261 | Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B)); |
| 3262 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3263 | } |
| 3264 | |
Bill Wendling | 3bef2dd | 2012-09-19 23:54:18 +0000 | [diff] [blame] | 3265 | if (FuncAttrs.hasAttributes()) |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 3266 | Attrs.push_back(AttributeSet::get(RetType->getContext(), |
| 3267 | AttributeSet::FunctionIndex, |
| 3268 | FuncAttrs)); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3269 | |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 3270 | AttributeSet PAL = AttributeSet::get(Context, Attrs); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3271 | |
Bill Wendling | 749a43d | 2012-12-30 13:50:49 +0000 | [diff] [blame] | 3272 | if (PAL.hasAttribute(1, Attribute::StructRet) && !RetType->isVoidTy()) |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3273 | return Error(RetTypeLoc, "functions with 'sret' argument must return void"); |
| 3274 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3275 | FunctionType *FT = |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 3276 | FunctionType::get(RetType, ParamTypeList, isVarArg); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3277 | PointerType *PFT = PointerType::getUnqual(FT); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3278 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3279 | Fn = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3280 | if (!FunctionName.empty()) { |
| 3281 | // If this was a definition of a forward reference, remove the definition |
| 3282 | // from the forward reference table and fill in the forward ref. |
| 3283 | std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI = |
| 3284 | ForwardRefVals.find(FunctionName); |
| 3285 | if (FRVI != ForwardRefVals.end()) { |
| 3286 | Fn = M->getFunction(FunctionName); |
Nick Lewycky | 686d7cb | 2012-10-11 00:38:25 +0000 | [diff] [blame] | 3287 | if (!Fn) |
| 3288 | return Error(FRVI->second.second, "invalid forward reference to " |
| 3289 | "function as global value!"); |
Chris Lattner | c239eb7 | 2010-04-20 04:49:11 +0000 | [diff] [blame] | 3290 | if (Fn->getType() != PFT) |
| 3291 | return Error(FRVI->second.second, "invalid forward reference to " |
| 3292 | "function '" + FunctionName + "' with wrong type!"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3293 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3294 | ForwardRefVals.erase(FRVI); |
| 3295 | } else if ((Fn = M->getFunction(FunctionName))) { |
Chris Lattner | 5756c16 | 2011-06-17 07:06:44 +0000 | [diff] [blame] | 3296 | // Reject redefinitions. |
| 3297 | return Error(NameLoc, "invalid redefinition of function '" + |
| 3298 | FunctionName + "'"); |
Chris Lattner | e38317f | 2009-10-25 23:22:50 +0000 | [diff] [blame] | 3299 | } else if (M->getNamedValue(FunctionName)) { |
| 3300 | return Error(NameLoc, "redefinition of function '@" + FunctionName + "'"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3301 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3302 | |
Dan Gohman | 399d6ae | 2009-08-29 23:37:49 +0000 | [diff] [blame] | 3303 | } else { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3304 | // If this is a definition of a forward referenced function, make sure the |
| 3305 | // types agree. |
| 3306 | std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I |
| 3307 | = ForwardRefValIDs.find(NumberedVals.size()); |
| 3308 | if (I != ForwardRefValIDs.end()) { |
| 3309 | Fn = cast<Function>(I->second.first); |
| 3310 | if (Fn->getType() != PFT) |
| 3311 | return Error(NameLoc, "type of definition and forward reference of '@" + |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 3312 | Twine(NumberedVals.size()) + "' disagree"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3313 | ForwardRefValIDs.erase(I); |
| 3314 | } |
| 3315 | } |
| 3316 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3317 | if (!Fn) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3318 | Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M); |
| 3319 | else // Move the forward-reference to the correct spot in the module. |
| 3320 | M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn); |
| 3321 | |
| 3322 | if (FunctionName.empty()) |
| 3323 | NumberedVals.push_back(Fn); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3324 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3325 | Fn->setLinkage((GlobalValue::LinkageTypes)Linkage); |
| 3326 | Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility); |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 3327 | Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3328 | Fn->setCallingConv(CC); |
| 3329 | Fn->setAttributes(PAL); |
Rafael Espindola | 45e6c19 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 3330 | Fn->setUnnamedAddr(UnnamedAddr); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3331 | Fn->setAlignment(Alignment); |
| 3332 | Fn->setSection(Section); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 3333 | Fn->setComdat(C); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3334 | if (!GC.empty()) Fn->setGC(GC.c_str()); |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 3335 | Fn->setPrefixData(Prefix); |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 3336 | Fn->setPrologueData(Prologue); |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 3337 | ForwardRefAttrGroups[Fn] = FwdRefAttrGrps; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3338 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3339 | // Add all of the arguments we parsed to the function. |
| 3340 | Function::arg_iterator ArgIt = Fn->arg_begin(); |
| 3341 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) { |
| 3342 | // If the argument has a name, insert it into the argument symbol table. |
| 3343 | if (ArgList[i].Name.empty()) continue; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3344 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3345 | // Set the name, if it conflicted, it will be auto-renamed. |
| 3346 | ArgIt->setName(ArgList[i].Name); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3347 | |
Benjamin Kramer | 1dc34b4 | 2010-10-16 11:28:23 +0000 | [diff] [blame] | 3348 | if (ArgIt->getName() != ArgList[i].Name) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3349 | return Error(ArgList[i].Loc, "redefinition of argument '%" + |
| 3350 | ArgList[i].Name + "'"); |
| 3351 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3352 | |
Duncan P. N. Exon Smith | 1716990 | 2014-08-19 00:13:19 +0000 | [diff] [blame] | 3353 | if (isDefine) |
| 3354 | return false; |
| 3355 | |
Robin Morisset | 039781e | 2014-08-29 21:53:01 +0000 | [diff] [blame] | 3356 | // Check the declaration has no block address forward references. |
Duncan P. N. Exon Smith | 1716990 | 2014-08-19 00:13:19 +0000 | [diff] [blame] | 3357 | ValID ID; |
| 3358 | if (FunctionName.empty()) { |
| 3359 | ID.Kind = ValID::t_GlobalID; |
| 3360 | ID.UIntVal = NumberedVals.size() - 1; |
| 3361 | } else { |
| 3362 | ID.Kind = ValID::t_GlobalName; |
| 3363 | ID.StrVal = FunctionName; |
| 3364 | } |
| 3365 | auto Blocks = ForwardRefBlockAddresses.find(ID); |
| 3366 | if (Blocks != ForwardRefBlockAddresses.end()) |
| 3367 | return Error(Blocks->first.Loc, |
| 3368 | "cannot take blockaddress inside a declaration"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3369 | return false; |
| 3370 | } |
| 3371 | |
Duncan P. N. Exon Smith | 1716990 | 2014-08-19 00:13:19 +0000 | [diff] [blame] | 3372 | bool LLParser::PerFunctionState::resolveForwardRefBlockAddresses() { |
| 3373 | ValID ID; |
| 3374 | if (FunctionNumber == -1) { |
| 3375 | ID.Kind = ValID::t_GlobalName; |
| 3376 | ID.StrVal = F.getName(); |
| 3377 | } else { |
| 3378 | ID.Kind = ValID::t_GlobalID; |
| 3379 | ID.UIntVal = FunctionNumber; |
| 3380 | } |
| 3381 | |
| 3382 | auto Blocks = P.ForwardRefBlockAddresses.find(ID); |
| 3383 | if (Blocks == P.ForwardRefBlockAddresses.end()) |
| 3384 | return false; |
| 3385 | |
| 3386 | for (const auto &I : Blocks->second) { |
| 3387 | const ValID &BBID = I.first; |
| 3388 | GlobalValue *GV = I.second; |
| 3389 | |
| 3390 | assert((BBID.Kind == ValID::t_LocalID || BBID.Kind == ValID::t_LocalName) && |
| 3391 | "Expected local id or name"); |
| 3392 | BasicBlock *BB; |
| 3393 | if (BBID.Kind == ValID::t_LocalName) |
| 3394 | BB = GetBB(BBID.StrVal, BBID.Loc); |
| 3395 | else |
| 3396 | BB = GetBB(BBID.UIntVal, BBID.Loc); |
| 3397 | if (!BB) |
| 3398 | return P.Error(BBID.Loc, "referenced value is not a basic block"); |
| 3399 | |
| 3400 | GV->replaceAllUsesWith(BlockAddress::get(&F, BB)); |
| 3401 | GV->eraseFromParent(); |
| 3402 | } |
| 3403 | |
| 3404 | P.ForwardRefBlockAddresses.erase(Blocks); |
| 3405 | return false; |
| 3406 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3407 | |
| 3408 | /// ParseFunctionBody |
Duncan P. N. Exon Smith | 0a448fb | 2014-08-19 21:30:15 +0000 | [diff] [blame] | 3409 | /// ::= '{' BasicBlock+ UseListOrderDirective* '}' |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3410 | bool LLParser::ParseFunctionBody(Function &Fn) { |
Chris Lattner | 4649a73 | 2011-06-17 06:42:57 +0000 | [diff] [blame] | 3411 | if (Lex.getKind() != lltok::lbrace) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3412 | return TokError("expected '{' in function body"); |
| 3413 | Lex.Lex(); // eat the {. |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3414 | |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 3415 | int FunctionNumber = -1; |
| 3416 | if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3417 | |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 3418 | PerFunctionState PFS(*this, Fn, FunctionNumber); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3419 | |
Duncan P. N. Exon Smith | 1716990 | 2014-08-19 00:13:19 +0000 | [diff] [blame] | 3420 | // Resolve block addresses and allow basic blocks to be forward-declared |
| 3421 | // within this function. |
| 3422 | if (PFS.resolveForwardRefBlockAddresses()) |
| 3423 | return true; |
| 3424 | SaveAndRestore<PerFunctionState *> ScopeExit(BlockAddressPFS, &PFS); |
| 3425 | |
Chris Lattner | bbddd96 | 2010-01-09 19:20:07 +0000 | [diff] [blame] | 3426 | // We need at least one basic block. |
Duncan P. N. Exon Smith | 0a448fb | 2014-08-19 21:30:15 +0000 | [diff] [blame] | 3427 | if (Lex.getKind() == lltok::rbrace || Lex.getKind() == lltok::kw_uselistorder) |
Chris Lattner | bbddd96 | 2010-01-09 19:20:07 +0000 | [diff] [blame] | 3428 | return TokError("function body requires at least one basic block"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3429 | |
Duncan P. N. Exon Smith | 0a448fb | 2014-08-19 21:30:15 +0000 | [diff] [blame] | 3430 | while (Lex.getKind() != lltok::rbrace && |
| 3431 | Lex.getKind() != lltok::kw_uselistorder) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3432 | if (ParseBasicBlock(PFS)) return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3433 | |
Duncan P. N. Exon Smith | 0a448fb | 2014-08-19 21:30:15 +0000 | [diff] [blame] | 3434 | while (Lex.getKind() != lltok::rbrace) |
| 3435 | if (ParseUseListOrder(&PFS)) |
| 3436 | return true; |
| 3437 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3438 | // Eat the }. |
| 3439 | Lex.Lex(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3440 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3441 | // Verify function is ok. |
Chris Lattner | 3432c62 | 2009-10-28 03:39:23 +0000 | [diff] [blame] | 3442 | return PFS.FinishFunction(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3443 | } |
| 3444 | |
| 3445 | /// ParseBasicBlock |
| 3446 | /// ::= LabelStr? Instruction* |
| 3447 | bool LLParser::ParseBasicBlock(PerFunctionState &PFS) { |
| 3448 | // If this basic block starts out with a name, remember it. |
| 3449 | std::string Name; |
| 3450 | LocTy NameLoc = Lex.getLoc(); |
| 3451 | if (Lex.getKind() == lltok::LabelStr) { |
| 3452 | Name = Lex.getStrVal(); |
| 3453 | Lex.Lex(); |
| 3454 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3455 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3456 | BasicBlock *BB = PFS.DefineBB(Name, NameLoc); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3457 | if (!BB) return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3458 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3459 | std::string NameStr; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3460 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3461 | // Parse the instructions in this block until we get a terminator. |
| 3462 | Instruction *Inst; |
| 3463 | do { |
| 3464 | // This instruction may have three possibilities for a name: a) none |
| 3465 | // specified, b) name specified "%foo =", c) number specified: "%4 =". |
| 3466 | LocTy NameLoc = Lex.getLoc(); |
| 3467 | int NameID = -1; |
| 3468 | NameStr = ""; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3469 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3470 | if (Lex.getKind() == lltok::LocalVarID) { |
| 3471 | NameID = Lex.getUIntVal(); |
| 3472 | Lex.Lex(); |
| 3473 | if (ParseToken(lltok::equal, "expected '=' after instruction id")) |
| 3474 | return true; |
Chris Lattner | def1949 | 2011-06-17 06:36:20 +0000 | [diff] [blame] | 3475 | } else if (Lex.getKind() == lltok::LocalVar) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3476 | NameStr = Lex.getStrVal(); |
| 3477 | Lex.Lex(); |
| 3478 | if (ParseToken(lltok::equal, "expected '=' after instruction name")) |
| 3479 | return true; |
| 3480 | } |
Devang Patel | ea8a4b9 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 3481 | |
Chris Lattner | 77b89dc | 2009-12-30 05:23:43 +0000 | [diff] [blame] | 3482 | switch (ParseInstruction(Inst, BB, PFS)) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 3483 | default: llvm_unreachable("Unknown ParseInstruction result!"); |
Chris Lattner | 77b89dc | 2009-12-30 05:23:43 +0000 | [diff] [blame] | 3484 | case InstError: return true; |
| 3485 | case InstNormal: |
Chris Lattner | 2e664bd | 2010-04-07 04:08:57 +0000 | [diff] [blame] | 3486 | BB->getInstList().push_back(Inst); |
| 3487 | |
Chris Lattner | 77b89dc | 2009-12-30 05:23:43 +0000 | [diff] [blame] | 3488 | // With a normal result, we check to see if the instruction is followed by |
| 3489 | // a comma and metadata. |
| 3490 | if (EatIfPresent(lltok::comma)) |
Dan Gohman | 338d9a4 | 2010-08-24 02:05:17 +0000 | [diff] [blame] | 3491 | if (ParseInstructionMetadata(Inst, &PFS)) |
Chris Lattner | 77b89dc | 2009-12-30 05:23:43 +0000 | [diff] [blame] | 3492 | return true; |
| 3493 | break; |
| 3494 | case InstExtraComma: |
Chris Lattner | 2e664bd | 2010-04-07 04:08:57 +0000 | [diff] [blame] | 3495 | BB->getInstList().push_back(Inst); |
| 3496 | |
Chris Lattner | 77b89dc | 2009-12-30 05:23:43 +0000 | [diff] [blame] | 3497 | // If the instruction parser ate an extra comma at the end of it, it |
| 3498 | // *must* be followed by metadata. |
Dan Gohman | 338d9a4 | 2010-08-24 02:05:17 +0000 | [diff] [blame] | 3499 | if (ParseInstructionMetadata(Inst, &PFS)) |
Chris Lattner | 77b89dc | 2009-12-30 05:23:43 +0000 | [diff] [blame] | 3500 | return true; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3501 | break; |
Chris Lattner | 77b89dc | 2009-12-30 05:23:43 +0000 | [diff] [blame] | 3502 | } |
Devang Patel | ea8a4b9 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 3503 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3504 | // Set the name on the instruction. |
| 3505 | if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true; |
| 3506 | } while (!isa<TerminatorInst>(Inst)); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3507 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3508 | return false; |
| 3509 | } |
| 3510 | |
| 3511 | //===----------------------------------------------------------------------===// |
| 3512 | // Instruction Parsing. |
| 3513 | //===----------------------------------------------------------------------===// |
| 3514 | |
| 3515 | /// ParseInstruction - Parse one of the many different instructions. |
| 3516 | /// |
Chris Lattner | 77b89dc | 2009-12-30 05:23:43 +0000 | [diff] [blame] | 3517 | int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB, |
| 3518 | PerFunctionState &PFS) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3519 | lltok::Kind Token = Lex.getKind(); |
| 3520 | if (Token == lltok::Eof) |
| 3521 | return TokError("found end of file when expecting more instructions"); |
| 3522 | LocTy Loc = Lex.getLoc(); |
Chris Lattner | 89d856e | 2009-03-01 00:53:13 +0000 | [diff] [blame] | 3523 | unsigned KeywordVal = Lex.getUIntVal(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3524 | Lex.Lex(); // Eat the keyword. |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3525 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3526 | switch (Token) { |
| 3527 | default: return Error(Loc, "expected instruction opcode"); |
| 3528 | // Terminator Instructions. |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3529 | case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3530 | case lltok::kw_ret: return ParseRet(Inst, BB, PFS); |
| 3531 | case lltok::kw_br: return ParseBr(Inst, PFS); |
| 3532 | case lltok::kw_switch: return ParseSwitch(Inst, PFS); |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3533 | case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3534 | case lltok::kw_invoke: return ParseInvoke(Inst, PFS); |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 3535 | case lltok::kw_resume: return ParseResume(Inst, PFS); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3536 | // Binary Operators. |
| 3537 | case lltok::kw_add: |
| 3538 | case lltok::kw_sub: |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 3539 | case lltok::kw_mul: |
| 3540 | case lltok::kw_shl: { |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 3541 | bool NUW = EatIfPresent(lltok::kw_nuw); |
| 3542 | bool NSW = EatIfPresent(lltok::kw_nsw); |
| 3543 | if (!NUW) NUW = EatIfPresent(lltok::kw_nuw); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3544 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 3545 | if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3546 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 3547 | if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true); |
| 3548 | if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true); |
| 3549 | return false; |
Dan Gohman | 9c7f808 | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 3550 | } |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 3551 | case lltok::kw_fadd: |
| 3552 | case lltok::kw_fsub: |
Michael Ilseman | 9205317 | 2012-11-27 00:42:44 +0000 | [diff] [blame] | 3553 | case lltok::kw_fmul: |
| 3554 | case lltok::kw_fdiv: |
| 3555 | case lltok::kw_frem: { |
| 3556 | FastMathFlags FMF = EatFastMathFlagsIfPresent(); |
| 3557 | int Res = ParseArithmetic(Inst, PFS, KeywordVal, 2); |
| 3558 | if (Res != 0) |
| 3559 | return Res; |
| 3560 | if (FMF.any()) |
| 3561 | Inst->setFastMathFlags(FMF); |
| 3562 | return 0; |
| 3563 | } |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 3564 | |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 3565 | case lltok::kw_sdiv: |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 3566 | case lltok::kw_udiv: |
| 3567 | case lltok::kw_lshr: |
| 3568 | case lltok::kw_ashr: { |
| 3569 | bool Exact = EatIfPresent(lltok::kw_exact); |
| 3570 | |
| 3571 | if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true; |
| 3572 | if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true); |
| 3573 | return false; |
Dan Gohman | 9c7f808 | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3576 | case lltok::kw_urem: |
Chris Lattner | 89d856e | 2009-03-01 00:53:13 +0000 | [diff] [blame] | 3577 | case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3578 | case lltok::kw_and: |
| 3579 | case lltok::kw_or: |
Chris Lattner | 89d856e | 2009-03-01 00:53:13 +0000 | [diff] [blame] | 3580 | case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3581 | case lltok::kw_icmp: |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 3582 | case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3583 | // Casts. |
| 3584 | case lltok::kw_trunc: |
| 3585 | case lltok::kw_zext: |
| 3586 | case lltok::kw_sext: |
| 3587 | case lltok::kw_fptrunc: |
| 3588 | case lltok::kw_fpext: |
| 3589 | case lltok::kw_bitcast: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3590 | case lltok::kw_addrspacecast: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3591 | case lltok::kw_uitofp: |
| 3592 | case lltok::kw_sitofp: |
| 3593 | case lltok::kw_fptoui: |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3594 | case lltok::kw_fptosi: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3595 | case lltok::kw_inttoptr: |
Chris Lattner | 89d856e | 2009-03-01 00:53:13 +0000 | [diff] [blame] | 3596 | case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3597 | // Other. |
| 3598 | case lltok::kw_select: return ParseSelect(Inst, PFS); |
Chris Lattner | b55ab54 | 2009-01-05 08:18:44 +0000 | [diff] [blame] | 3599 | case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3600 | case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS); |
| 3601 | case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS); |
| 3602 | case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS); |
| 3603 | case lltok::kw_phi: return ParsePHI(Inst, PFS); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 3604 | case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS); |
Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 3605 | // Call. |
| 3606 | case lltok::kw_call: return ParseCall(Inst, PFS, CallInst::TCK_None); |
| 3607 | case lltok::kw_tail: return ParseCall(Inst, PFS, CallInst::TCK_Tail); |
| 3608 | case lltok::kw_musttail: return ParseCall(Inst, PFS, CallInst::TCK_MustTail); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3609 | // Memory. |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 3610 | case lltok::kw_alloca: return ParseAlloc(Inst, PFS); |
Chris Lattner | bc63929 | 2011-11-27 06:56:53 +0000 | [diff] [blame] | 3611 | case lltok::kw_load: return ParseLoad(Inst, PFS); |
| 3612 | case lltok::kw_store: return ParseStore(Inst, PFS); |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 3613 | case lltok::kw_cmpxchg: return ParseCmpXchg(Inst, PFS); |
| 3614 | case lltok::kw_atomicrmw: return ParseAtomicRMW(Inst, PFS); |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 3615 | case lltok::kw_fence: return ParseFence(Inst, PFS); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3616 | case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS); |
| 3617 | case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS); |
| 3618 | case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS); |
| 3619 | } |
| 3620 | } |
| 3621 | |
| 3622 | /// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind. |
| 3623 | bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) { |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 3624 | if (Opc == Instruction::FCmp) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3625 | switch (Lex.getKind()) { |
David Tweed | a11edf0 | 2013-01-07 13:32:38 +0000 | [diff] [blame] | 3626 | default: return TokError("expected fcmp predicate (e.g. 'oeq')"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3627 | case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break; |
| 3628 | case lltok::kw_one: P = CmpInst::FCMP_ONE; break; |
| 3629 | case lltok::kw_olt: P = CmpInst::FCMP_OLT; break; |
| 3630 | case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break; |
| 3631 | case lltok::kw_ole: P = CmpInst::FCMP_OLE; break; |
| 3632 | case lltok::kw_oge: P = CmpInst::FCMP_OGE; break; |
| 3633 | case lltok::kw_ord: P = CmpInst::FCMP_ORD; break; |
| 3634 | case lltok::kw_uno: P = CmpInst::FCMP_UNO; break; |
| 3635 | case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break; |
| 3636 | case lltok::kw_une: P = CmpInst::FCMP_UNE; break; |
| 3637 | case lltok::kw_ult: P = CmpInst::FCMP_ULT; break; |
| 3638 | case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break; |
| 3639 | case lltok::kw_ule: P = CmpInst::FCMP_ULE; break; |
| 3640 | case lltok::kw_uge: P = CmpInst::FCMP_UGE; break; |
| 3641 | case lltok::kw_true: P = CmpInst::FCMP_TRUE; break; |
| 3642 | case lltok::kw_false: P = CmpInst::FCMP_FALSE; break; |
| 3643 | } |
| 3644 | } else { |
| 3645 | switch (Lex.getKind()) { |
David Tweed | a11edf0 | 2013-01-07 13:32:38 +0000 | [diff] [blame] | 3646 | default: return TokError("expected icmp predicate (e.g. 'eq')"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3647 | case lltok::kw_eq: P = CmpInst::ICMP_EQ; break; |
| 3648 | case lltok::kw_ne: P = CmpInst::ICMP_NE; break; |
| 3649 | case lltok::kw_slt: P = CmpInst::ICMP_SLT; break; |
| 3650 | case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break; |
| 3651 | case lltok::kw_sle: P = CmpInst::ICMP_SLE; break; |
| 3652 | case lltok::kw_sge: P = CmpInst::ICMP_SGE; break; |
| 3653 | case lltok::kw_ult: P = CmpInst::ICMP_ULT; break; |
| 3654 | case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break; |
| 3655 | case lltok::kw_ule: P = CmpInst::ICMP_ULE; break; |
| 3656 | case lltok::kw_uge: P = CmpInst::ICMP_UGE; break; |
| 3657 | } |
| 3658 | } |
| 3659 | Lex.Lex(); |
| 3660 | return false; |
| 3661 | } |
| 3662 | |
| 3663 | //===----------------------------------------------------------------------===// |
| 3664 | // Terminator Instructions. |
| 3665 | //===----------------------------------------------------------------------===// |
| 3666 | |
| 3667 | /// ParseRet - Parse a return instruction. |
Chris Lattner | 596760d | 2009-12-29 21:25:40 +0000 | [diff] [blame] | 3668 | /// ::= 'ret' void (',' !dbg, !1)* |
| 3669 | /// ::= 'ret' TypeAndValue (',' !dbg, !1)* |
Chris Lattner | 33de427 | 2011-06-17 06:49:41 +0000 | [diff] [blame] | 3670 | bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB, |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3671 | PerFunctionState &PFS) { |
| 3672 | SMLoc TypeLoc = Lex.getLoc(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3673 | Type *Ty = nullptr; |
Chris Lattner | f880ca2 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 3674 | if (ParseType(Ty, true /*void allowed*/)) return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3675 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3676 | Type *ResType = PFS.getFunction().getReturnType(); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3677 | |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 3678 | if (Ty->isVoidTy()) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3679 | if (!ResType->isVoidTy()) |
| 3680 | return Error(TypeLoc, "value doesn't match function result type '" + |
| 3681 | getTypeString(ResType) + "'"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3682 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3683 | Inst = ReturnInst::Create(Context); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3684 | return false; |
| 3685 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3686 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3687 | Value *RV; |
| 3688 | if (ParseValue(Ty, RV, PFS)) return true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3689 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3690 | if (ResType != RV->getType()) |
| 3691 | return Error(TypeLoc, "value doesn't match function result type '" + |
| 3692 | getTypeString(ResType) + "'"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3693 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3694 | Inst = ReturnInst::Create(Context, RV); |
Chris Lattner | 33de427 | 2011-06-17 06:49:41 +0000 | [diff] [blame] | 3695 | return false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3696 | } |
| 3697 | |
| 3698 | |
| 3699 | /// ParseBr |
| 3700 | /// ::= 'br' TypeAndValue |
| 3701 | /// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue |
| 3702 | bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) { |
| 3703 | LocTy Loc, Loc2; |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3704 | Value *Op0; |
| 3705 | BasicBlock *Op1, *Op2; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3706 | if (ParseTypeAndValue(Op0, Loc, PFS)) return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3707 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3708 | if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) { |
| 3709 | Inst = BranchInst::Create(BB); |
| 3710 | return false; |
| 3711 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3712 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3713 | if (Op0->getType() != Type::getInt1Ty(Context)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3714 | return Error(Loc, "branch condition must have 'i1' type"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3715 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3716 | if (ParseToken(lltok::comma, "expected ',' after branch condition") || |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3717 | ParseTypeAndBasicBlock(Op1, Loc, PFS) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3718 | ParseToken(lltok::comma, "expected ',' after true destination") || |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3719 | ParseTypeAndBasicBlock(Op2, Loc2, PFS)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3720 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3721 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3722 | Inst = BranchInst::Create(Op1, Op2, Op0); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3723 | return false; |
| 3724 | } |
| 3725 | |
| 3726 | /// ParseSwitch |
| 3727 | /// Instruction |
| 3728 | /// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']' |
| 3729 | /// JumpTable |
| 3730 | /// ::= (TypeAndValue ',' TypeAndValue)* |
| 3731 | bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) { |
| 3732 | LocTy CondLoc, BBLoc; |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3733 | Value *Cond; |
| 3734 | BasicBlock *DefaultBB; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3735 | if (ParseTypeAndValue(Cond, CondLoc, PFS) || |
| 3736 | ParseToken(lltok::comma, "expected ',' after switch condition") || |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3737 | ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3738 | ParseToken(lltok::lsquare, "expected '[' with switch table")) |
| 3739 | return true; |
| 3740 | |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3741 | if (!Cond->getType()->isIntegerTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3742 | return Error(CondLoc, "switch condition must have integer type"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3743 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3744 | // Parse the jump table pairs. |
| 3745 | SmallPtrSet<Value*, 32> SeenCases; |
| 3746 | SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table; |
| 3747 | while (Lex.getKind() != lltok::rsquare) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3748 | Value *Constant; |
| 3749 | BasicBlock *DestBB; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3750 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3751 | if (ParseTypeAndValue(Constant, CondLoc, PFS) || |
| 3752 | ParseToken(lltok::comma, "expected ',' after case value") || |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3753 | ParseTypeAndBasicBlock(DestBB, PFS)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3754 | return true; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3755 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 3756 | if (!SeenCases.insert(Constant).second) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3757 | return Error(CondLoc, "duplicate case value in switch"); |
| 3758 | if (!isa<ConstantInt>(Constant)) |
| 3759 | return Error(CondLoc, "case value is not a constant integer"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3760 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3761 | Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB)); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3762 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3763 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3764 | Lex.Lex(); // Eat the ']'. |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3765 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3766 | SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size()); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3767 | for (unsigned i = 0, e = Table.size(); i != e; ++i) |
| 3768 | SI->addCase(Table[i].first, Table[i].second); |
| 3769 | Inst = SI; |
| 3770 | return false; |
| 3771 | } |
| 3772 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3773 | /// ParseIndirectBr |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3774 | /// Instruction |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3775 | /// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']' |
| 3776 | bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3777 | LocTy AddrLoc; |
| 3778 | Value *Address; |
| 3779 | if (ParseTypeAndValue(Address, AddrLoc, PFS) || |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3780 | ParseToken(lltok::comma, "expected ',' after indirectbr address") || |
| 3781 | ParseToken(lltok::lsquare, "expected '[' with indirectbr")) |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3782 | return true; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3783 | |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3784 | if (!Address->getType()->isPointerTy()) |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3785 | return Error(AddrLoc, "indirectbr address must have pointer type"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3786 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3787 | // Parse the destination list. |
| 3788 | SmallVector<BasicBlock*, 16> DestList; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3789 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3790 | if (Lex.getKind() != lltok::rsquare) { |
| 3791 | BasicBlock *DestBB; |
| 3792 | if (ParseTypeAndBasicBlock(DestBB, PFS)) |
| 3793 | return true; |
| 3794 | DestList.push_back(DestBB); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3795 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3796 | while (EatIfPresent(lltok::comma)) { |
| 3797 | if (ParseTypeAndBasicBlock(DestBB, PFS)) |
| 3798 | return true; |
| 3799 | DestList.push_back(DestBB); |
| 3800 | } |
| 3801 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3802 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3803 | if (ParseToken(lltok::rsquare, "expected ']' at end of block list")) |
| 3804 | return true; |
| 3805 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3806 | IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size()); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3807 | for (unsigned i = 0, e = DestList.size(); i != e; ++i) |
| 3808 | IBI->addDestination(DestList[i]); |
| 3809 | Inst = IBI; |
| 3810 | return false; |
| 3811 | } |
| 3812 | |
| 3813 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3814 | /// ParseInvoke |
| 3815 | /// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList |
| 3816 | /// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue |
| 3817 | bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) { |
| 3818 | LocTy CallLoc = Lex.getLoc(); |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 3819 | AttrBuilder RetAttrs, FnAttrs; |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 3820 | std::vector<unsigned> FwdRefAttrGrps; |
Bill Wendling | 09bd1f7 | 2013-02-22 00:12:35 +0000 | [diff] [blame] | 3821 | LocTy NoBuiltinLoc; |
Alexey Samsonov | 17a9cff | 2014-09-10 18:00:17 +0000 | [diff] [blame] | 3822 | unsigned CC; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3823 | Type *RetType = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3824 | LocTy RetTypeLoc; |
| 3825 | ValID CalleeID; |
| 3826 | SmallVector<ParamInfo, 16> ArgList; |
| 3827 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3828 | BasicBlock *NormalBB, *UnwindBB; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3829 | if (ParseOptionalCallingConv(CC) || |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 3830 | ParseOptionalReturnAttrs(RetAttrs) || |
Chris Lattner | f880ca2 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 3831 | ParseType(RetType, RetTypeLoc, true /*void allowed*/) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3832 | ParseValID(CalleeID) || |
| 3833 | ParseParameterList(ArgList, PFS) || |
Bill Wendling | 09bd1f7 | 2013-02-22 00:12:35 +0000 | [diff] [blame] | 3834 | ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false, |
| 3835 | NoBuiltinLoc) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3836 | ParseToken(lltok::kw_to, "expected 'to' in invoke") || |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3837 | ParseTypeAndBasicBlock(NormalBB, PFS) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3838 | ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") || |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3839 | ParseTypeAndBasicBlock(UnwindBB, PFS)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3840 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3841 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3842 | // If RetType is a non-function pointer type, then this is the short syntax |
| 3843 | // for the call, which means that RetType is just the return type. Infer the |
| 3844 | // rest of the function argument types from the arguments that are present. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3845 | PointerType *PFTy = nullptr; |
| 3846 | FunctionType *Ty = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3847 | if (!(PFTy = dyn_cast<PointerType>(RetType)) || |
| 3848 | !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) { |
| 3849 | // Pull out the types of all of the arguments... |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 3850 | std::vector<Type*> ParamTypes; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3851 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) |
| 3852 | ParamTypes.push_back(ArgList[i].V->getType()); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3853 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3854 | if (!FunctionType::isValidReturnType(RetType)) |
| 3855 | return Error(RetTypeLoc, "Invalid result type for LLVM function"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3856 | |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 3857 | Ty = FunctionType::get(RetType, ParamTypes, false); |
| 3858 | PFTy = PointerType::getUnqual(Ty); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3859 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3860 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3861 | // Look up the callee. |
| 3862 | Value *Callee; |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 3863 | if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3864 | |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 3865 | // Set up the Attribute for the function. |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 3866 | SmallVector<AttributeSet, 8> Attrs; |
Bill Wendling | 3bef2dd | 2012-09-19 23:54:18 +0000 | [diff] [blame] | 3867 | if (RetAttrs.hasAttributes()) |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 3868 | Attrs.push_back(AttributeSet::get(RetType->getContext(), |
| 3869 | AttributeSet::ReturnIndex, |
| 3870 | RetAttrs)); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3871 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3872 | SmallVector<Value*, 8> Args; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3873 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3874 | // Loop through FunctionType's arguments and ensure they are specified |
| 3875 | // correctly. Also, gather any parameter attributes. |
| 3876 | FunctionType::param_iterator I = Ty->param_begin(); |
| 3877 | FunctionType::param_iterator E = Ty->param_end(); |
| 3878 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3879 | Type *ExpectedTy = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3880 | if (I != E) { |
| 3881 | ExpectedTy = *I++; |
| 3882 | } else if (!Ty->isVarArg()) { |
| 3883 | return Error(ArgList[i].Loc, "too many arguments specified"); |
| 3884 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3885 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3886 | if (ExpectedTy && ExpectedTy != ArgList[i].V->getType()) |
| 3887 | return Error(ArgList[i].Loc, "argument is not of expected type '" + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 3888 | getTypeString(ExpectedTy) + "'"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3889 | Args.push_back(ArgList[i].V); |
Bill Wendling | fe0021a | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 3890 | if (ArgList[i].Attrs.hasAttributes(i + 1)) { |
| 3891 | AttrBuilder B(ArgList[i].Attrs, i + 1); |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 3892 | Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B)); |
| 3893 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3894 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3895 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3896 | if (I != E) |
| 3897 | return Error(CallLoc, "not enough parameters specified for call"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3898 | |
Bill Wendling | 3bef2dd | 2012-09-19 23:54:18 +0000 | [diff] [blame] | 3899 | if (FnAttrs.hasAttributes()) |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 3900 | Attrs.push_back(AttributeSet::get(RetType->getContext(), |
| 3901 | AttributeSet::FunctionIndex, |
| 3902 | FnAttrs)); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3903 | |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 3904 | // Finish off the Attribute and check them |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 3905 | AttributeSet PAL = AttributeSet::get(Context, Attrs); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3906 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 3907 | InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB, Args); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3908 | II->setCallingConv(CC); |
| 3909 | II->setAttributes(PAL); |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 3910 | ForwardRefAttrGroups[II] = FwdRefAttrGrps; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3911 | Inst = II; |
| 3912 | return false; |
| 3913 | } |
| 3914 | |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 3915 | /// ParseResume |
| 3916 | /// ::= 'resume' TypeAndValue |
| 3917 | bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) { |
| 3918 | Value *Exn; LocTy ExnLoc; |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 3919 | if (ParseTypeAndValue(Exn, ExnLoc, PFS)) |
| 3920 | return true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3921 | |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 3922 | ResumeInst *RI = ResumeInst::Create(Exn); |
| 3923 | Inst = RI; |
| 3924 | return false; |
| 3925 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3926 | |
| 3927 | //===----------------------------------------------------------------------===// |
| 3928 | // Binary Operators. |
| 3929 | //===----------------------------------------------------------------------===// |
| 3930 | |
| 3931 | /// ParseArithmetic |
Chris Lattner | eeefa9a | 2009-01-05 08:24:46 +0000 | [diff] [blame] | 3932 | /// ::= ArithmeticOps TypeAndValue ',' Value |
| 3933 | /// |
| 3934 | /// If OperandType is 0, then any FP or integer operand is allowed. If it is 1, |
| 3935 | /// 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] | 3936 | bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS, |
Chris Lattner | eeefa9a | 2009-01-05 08:24:46 +0000 | [diff] [blame] | 3937 | unsigned Opc, unsigned OperandType) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3938 | LocTy Loc; Value *LHS, *RHS; |
| 3939 | if (ParseTypeAndValue(LHS, Loc, PFS) || |
| 3940 | ParseToken(lltok::comma, "expected ',' in arithmetic operation") || |
| 3941 | ParseValue(LHS->getType(), RHS, PFS)) |
| 3942 | return true; |
| 3943 | |
Chris Lattner | eeefa9a | 2009-01-05 08:24:46 +0000 | [diff] [blame] | 3944 | bool Valid; |
| 3945 | switch (OperandType) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3946 | default: llvm_unreachable("Unknown operand type!"); |
Chris Lattner | eeefa9a | 2009-01-05 08:24:46 +0000 | [diff] [blame] | 3947 | case 0: // int or FP. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3948 | Valid = LHS->getType()->isIntOrIntVectorTy() || |
| 3949 | LHS->getType()->isFPOrFPVectorTy(); |
Chris Lattner | eeefa9a | 2009-01-05 08:24:46 +0000 | [diff] [blame] | 3950 | break; |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3951 | case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break; |
| 3952 | case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break; |
Chris Lattner | eeefa9a | 2009-01-05 08:24:46 +0000 | [diff] [blame] | 3953 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3954 | |
Chris Lattner | eeefa9a | 2009-01-05 08:24:46 +0000 | [diff] [blame] | 3955 | if (!Valid) |
| 3956 | return Error(Loc, "invalid operand type for instruction"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3957 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3958 | Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); |
| 3959 | return false; |
| 3960 | } |
| 3961 | |
| 3962 | /// ParseLogical |
| 3963 | /// ::= ArithmeticOps TypeAndValue ',' Value { |
| 3964 | bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS, |
| 3965 | unsigned Opc) { |
| 3966 | LocTy Loc; Value *LHS, *RHS; |
| 3967 | if (ParseTypeAndValue(LHS, Loc, PFS) || |
| 3968 | ParseToken(lltok::comma, "expected ',' in logical operation") || |
| 3969 | ParseValue(LHS->getType(), RHS, PFS)) |
| 3970 | return true; |
| 3971 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3972 | if (!LHS->getType()->isIntOrIntVectorTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3973 | return Error(Loc,"instruction requires integer or integer vector operands"); |
| 3974 | |
| 3975 | Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); |
| 3976 | return false; |
| 3977 | } |
| 3978 | |
| 3979 | |
| 3980 | /// ParseCompare |
| 3981 | /// ::= 'icmp' IPredicates TypeAndValue ',' Value |
| 3982 | /// ::= 'fcmp' FPredicates TypeAndValue ',' Value |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3983 | bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS, |
| 3984 | unsigned Opc) { |
| 3985 | // Parse the integer/fp comparison predicate. |
| 3986 | LocTy Loc; |
| 3987 | unsigned Pred; |
| 3988 | Value *LHS, *RHS; |
| 3989 | if (ParseCmpPredicate(Pred, Opc) || |
| 3990 | ParseTypeAndValue(LHS, Loc, PFS) || |
| 3991 | ParseToken(lltok::comma, "expected ',' after compare value") || |
| 3992 | ParseValue(LHS->getType(), RHS, PFS)) |
| 3993 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3994 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3995 | if (Opc == Instruction::FCmp) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3996 | if (!LHS->getType()->isFPOrFPVectorTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 3997 | return Error(Loc, "fcmp requires floating point operands"); |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3998 | Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS); |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 3999 | } else { |
| 4000 | assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 4001 | if (!LHS->getType()->isIntOrIntVectorTy() && |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 4002 | !LHS->getType()->getScalarType()->isPointerTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4003 | return Error(Loc, "icmp requires integer operands"); |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 4004 | Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4005 | } |
| 4006 | return false; |
| 4007 | } |
| 4008 | |
| 4009 | //===----------------------------------------------------------------------===// |
| 4010 | // Other Instructions. |
| 4011 | //===----------------------------------------------------------------------===// |
| 4012 | |
| 4013 | |
| 4014 | /// ParseCast |
| 4015 | /// ::= CastOpc TypeAndValue 'to' Type |
| 4016 | bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS, |
| 4017 | unsigned Opc) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 4018 | LocTy Loc; |
| 4019 | Value *Op; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4020 | Type *DestTy = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4021 | if (ParseTypeAndValue(Op, Loc, PFS) || |
| 4022 | ParseToken(lltok::kw_to, "expected 'to' after cast value") || |
| 4023 | ParseType(DestTy)) |
| 4024 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4025 | |
Chris Lattner | 89d856e | 2009-03-01 00:53:13 +0000 | [diff] [blame] | 4026 | if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) { |
| 4027 | CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4028 | return Error(Loc, "invalid cast opcode for cast from '" + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 4029 | getTypeString(Op->getType()) + "' to '" + |
| 4030 | getTypeString(DestTy) + "'"); |
Chris Lattner | 89d856e | 2009-03-01 00:53:13 +0000 | [diff] [blame] | 4031 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4032 | Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy); |
| 4033 | return false; |
| 4034 | } |
| 4035 | |
| 4036 | /// ParseSelect |
| 4037 | /// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue |
| 4038 | bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) { |
| 4039 | LocTy Loc; |
| 4040 | Value *Op0, *Op1, *Op2; |
| 4041 | if (ParseTypeAndValue(Op0, Loc, PFS) || |
| 4042 | ParseToken(lltok::comma, "expected ',' after select condition") || |
| 4043 | ParseTypeAndValue(Op1, PFS) || |
| 4044 | ParseToken(lltok::comma, "expected ',' after select value") || |
| 4045 | ParseTypeAndValue(Op2, PFS)) |
| 4046 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4047 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4048 | if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2)) |
| 4049 | return Error(Loc, Reason); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4050 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4051 | Inst = SelectInst::Create(Op0, Op1, Op2); |
| 4052 | return false; |
| 4053 | } |
| 4054 | |
Chris Lattner | b55ab54 | 2009-01-05 08:18:44 +0000 | [diff] [blame] | 4055 | /// ParseVA_Arg |
| 4056 | /// ::= 'va_arg' TypeAndValue ',' Type |
| 4057 | bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4058 | Value *Op; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4059 | Type *EltTy = nullptr; |
Chris Lattner | b55ab54 | 2009-01-05 08:18:44 +0000 | [diff] [blame] | 4060 | LocTy TypeLoc; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4061 | if (ParseTypeAndValue(Op, PFS) || |
| 4062 | ParseToken(lltok::comma, "expected ',' after vaarg operand") || |
Chris Lattner | b55ab54 | 2009-01-05 08:18:44 +0000 | [diff] [blame] | 4063 | ParseType(EltTy, TypeLoc)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4064 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4065 | |
Chris Lattner | b55ab54 | 2009-01-05 08:18:44 +0000 | [diff] [blame] | 4066 | if (!EltTy->isFirstClassType()) |
| 4067 | return Error(TypeLoc, "va_arg requires operand with first class type"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4068 | |
| 4069 | Inst = new VAArgInst(Op, EltTy); |
| 4070 | return false; |
| 4071 | } |
| 4072 | |
| 4073 | /// ParseExtractElement |
| 4074 | /// ::= 'extractelement' TypeAndValue ',' TypeAndValue |
| 4075 | bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) { |
| 4076 | LocTy Loc; |
| 4077 | Value *Op0, *Op1; |
| 4078 | if (ParseTypeAndValue(Op0, Loc, PFS) || |
| 4079 | ParseToken(lltok::comma, "expected ',' after extract value") || |
| 4080 | ParseTypeAndValue(Op1, PFS)) |
| 4081 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4082 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4083 | if (!ExtractElementInst::isValidOperands(Op0, Op1)) |
| 4084 | return Error(Loc, "invalid extractelement operands"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4085 | |
Eric Christopher | c974225 | 2009-07-25 02:28:41 +0000 | [diff] [blame] | 4086 | Inst = ExtractElementInst::Create(Op0, Op1); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4087 | return false; |
| 4088 | } |
| 4089 | |
| 4090 | /// ParseInsertElement |
| 4091 | /// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue |
| 4092 | bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) { |
| 4093 | LocTy Loc; |
| 4094 | Value *Op0, *Op1, *Op2; |
| 4095 | if (ParseTypeAndValue(Op0, Loc, PFS) || |
| 4096 | ParseToken(lltok::comma, "expected ',' after insertelement value") || |
| 4097 | ParseTypeAndValue(Op1, PFS) || |
| 4098 | ParseToken(lltok::comma, "expected ',' after insertelement value") || |
| 4099 | ParseTypeAndValue(Op2, PFS)) |
| 4100 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4101 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4102 | if (!InsertElementInst::isValidOperands(Op0, Op1, Op2)) |
Eric Christopher | fef8db6 | 2009-07-23 01:01:32 +0000 | [diff] [blame] | 4103 | return Error(Loc, "invalid insertelement operands"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4104 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4105 | Inst = InsertElementInst::Create(Op0, Op1, Op2); |
| 4106 | return false; |
| 4107 | } |
| 4108 | |
| 4109 | /// ParseShuffleVector |
| 4110 | /// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue |
| 4111 | bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) { |
| 4112 | LocTy Loc; |
| 4113 | Value *Op0, *Op1, *Op2; |
| 4114 | if (ParseTypeAndValue(Op0, Loc, PFS) || |
| 4115 | ParseToken(lltok::comma, "expected ',' after shuffle mask") || |
| 4116 | ParseTypeAndValue(Op1, PFS) || |
| 4117 | ParseToken(lltok::comma, "expected ',' after shuffle value") || |
| 4118 | ParseTypeAndValue(Op2, PFS)) |
| 4119 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4120 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4121 | if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2)) |
Pete Cooper | c1a6f98 | 2012-02-01 23:43:12 +0000 | [diff] [blame] | 4122 | return Error(Loc, "invalid shufflevector operands"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4123 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4124 | Inst = new ShuffleVectorInst(Op0, Op1, Op2); |
| 4125 | return false; |
| 4126 | } |
| 4127 | |
| 4128 | /// ParsePHI |
Chris Lattner | c05471e | 2009-10-18 05:27:44 +0000 | [diff] [blame] | 4129 | /// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')* |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4130 | int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4131 | Type *Ty = nullptr; LocTy TypeLoc; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4132 | Value *Op0, *Op1; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4133 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 4134 | if (ParseType(Ty, TypeLoc) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4135 | ParseToken(lltok::lsquare, "expected '[' in phi value list") || |
| 4136 | ParseValue(Ty, Op0, PFS) || |
| 4137 | ParseToken(lltok::comma, "expected ',' after insertelement value") || |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4138 | ParseValue(Type::getLabelTy(Context), Op1, PFS) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4139 | ParseToken(lltok::rsquare, "expected ']' in phi value list")) |
| 4140 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4141 | |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4142 | bool AteExtraComma = false; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4143 | SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals; |
| 4144 | while (1) { |
| 4145 | PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1))); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4146 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 4147 | if (!EatIfPresent(lltok::comma)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4148 | break; |
| 4149 | |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4150 | if (Lex.getKind() == lltok::MetadataVar) { |
| 4151 | AteExtraComma = true; |
Devang Patel | 8f842d3 | 2009-10-16 18:45:49 +0000 | [diff] [blame] | 4152 | break; |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4153 | } |
Devang Patel | 8f842d3 | 2009-10-16 18:45:49 +0000 | [diff] [blame] | 4154 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 4155 | if (ParseToken(lltok::lsquare, "expected '[' in phi value list") || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4156 | ParseValue(Ty, Op0, PFS) || |
| 4157 | ParseToken(lltok::comma, "expected ',' after insertelement value") || |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4158 | ParseValue(Type::getLabelTy(Context), Op1, PFS) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4159 | ParseToken(lltok::rsquare, "expected ']' in phi value list")) |
| 4160 | return true; |
| 4161 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4162 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4163 | if (!Ty->isFirstClassType()) |
| 4164 | return Error(TypeLoc, "phi node must have first class type"); |
| 4165 | |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 4166 | PHINode *PN = PHINode::Create(Ty, PHIVals.size()); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4167 | for (unsigned i = 0, e = PHIVals.size(); i != e; ++i) |
| 4168 | PN->addIncoming(PHIVals[i].first, PHIVals[i].second); |
| 4169 | Inst = PN; |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4170 | return AteExtraComma ? InstExtraComma : InstNormal; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4171 | } |
| 4172 | |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4173 | /// ParseLandingPad |
| 4174 | /// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+ |
| 4175 | /// Clause |
| 4176 | /// ::= 'catch' TypeAndValue |
| 4177 | /// ::= 'filter' |
| 4178 | /// ::= 'filter' TypeAndValue ( ',' TypeAndValue )* |
| 4179 | bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4180 | Type *Ty = nullptr; LocTy TyLoc; |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4181 | Value *PersFn; LocTy PersFnLoc; |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4182 | |
| 4183 | if (ParseType(Ty, TyLoc) || |
| 4184 | ParseToken(lltok::kw_personality, "expected 'personality'") || |
| 4185 | ParseTypeAndValue(PersFn, PersFnLoc, PFS)) |
| 4186 | return true; |
| 4187 | |
| 4188 | LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, 0); |
| 4189 | LP->setCleanup(EatIfPresent(lltok::kw_cleanup)); |
| 4190 | |
| 4191 | while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){ |
| 4192 | LandingPadInst::ClauseType CT; |
| 4193 | if (EatIfPresent(lltok::kw_catch)) |
| 4194 | CT = LandingPadInst::Catch; |
| 4195 | else if (EatIfPresent(lltok::kw_filter)) |
| 4196 | CT = LandingPadInst::Filter; |
| 4197 | else |
| 4198 | return TokError("expected 'catch' or 'filter' clause type"); |
| 4199 | |
Rafael Espindola | 4dc5dfc | 2014-06-04 18:51:31 +0000 | [diff] [blame] | 4200 | Value *V; |
| 4201 | LocTy VLoc; |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4202 | if (ParseTypeAndValue(V, VLoc, PFS)) { |
| 4203 | delete LP; |
| 4204 | return true; |
| 4205 | } |
| 4206 | |
Bill Wendling | a52aa3c | 2011-08-12 20:52:25 +0000 | [diff] [blame] | 4207 | // A 'catch' type expects a non-array constant. A filter clause expects an |
| 4208 | // array constant. |
| 4209 | if (CT == LandingPadInst::Catch) { |
| 4210 | if (isa<ArrayType>(V->getType())) |
| 4211 | Error(VLoc, "'catch' clause has an invalid type"); |
| 4212 | } else { |
| 4213 | if (!isa<ArrayType>(V->getType())) |
| 4214 | Error(VLoc, "'filter' clause has an invalid type"); |
| 4215 | } |
| 4216 | |
Rafael Espindola | 4dc5dfc | 2014-06-04 18:51:31 +0000 | [diff] [blame] | 4217 | LP->addClause(cast<Constant>(V)); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4218 | } |
| 4219 | |
| 4220 | Inst = LP; |
| 4221 | return false; |
| 4222 | } |
| 4223 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4224 | /// ParseCall |
Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 4225 | /// ::= 'call' OptionalCallingConv OptionalAttrs Type Value |
| 4226 | /// ParameterList OptionalAttrs |
| 4227 | /// ::= 'tail' 'call' OptionalCallingConv OptionalAttrs Type Value |
| 4228 | /// ParameterList OptionalAttrs |
| 4229 | /// ::= 'musttail' 'call' OptionalCallingConv OptionalAttrs Type Value |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4230 | /// ParameterList OptionalAttrs |
| 4231 | bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS, |
Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 4232 | CallInst::TailCallKind TCK) { |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 4233 | AttrBuilder RetAttrs, FnAttrs; |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 4234 | std::vector<unsigned> FwdRefAttrGrps; |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 4235 | LocTy BuiltinLoc; |
Alexey Samsonov | 17a9cff | 2014-09-10 18:00:17 +0000 | [diff] [blame] | 4236 | unsigned CC; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4237 | Type *RetType = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4238 | LocTy RetTypeLoc; |
| 4239 | ValID CalleeID; |
| 4240 | SmallVector<ParamInfo, 16> ArgList; |
| 4241 | LocTy CallLoc = Lex.getLoc(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4242 | |
Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 4243 | if ((TCK != CallInst::TCK_None && |
| 4244 | ParseToken(lltok::kw_call, "expected 'tail call'")) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4245 | ParseOptionalCallingConv(CC) || |
Bill Wendling | 34c2eb2 | 2012-12-04 23:40:58 +0000 | [diff] [blame] | 4246 | ParseOptionalReturnAttrs(RetAttrs) || |
Chris Lattner | f880ca2 | 2009-03-09 04:49:14 +0000 | [diff] [blame] | 4247 | ParseType(RetType, RetTypeLoc, true /*void allowed*/) || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4248 | ParseValID(CalleeID) || |
Reid Kleckner | 8349864 | 2014-08-26 00:33:28 +0000 | [diff] [blame] | 4249 | ParseParameterList(ArgList, PFS, TCK == CallInst::TCK_MustTail, |
| 4250 | PFS.getFunction().isVarArg()) || |
Bill Wendling | 09bd1f7 | 2013-02-22 00:12:35 +0000 | [diff] [blame] | 4251 | ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false, |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 4252 | BuiltinLoc)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4253 | return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4254 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4255 | // If RetType is a non-function pointer type, then this is the short syntax |
| 4256 | // for the call, which means that RetType is just the return type. Infer the |
| 4257 | // rest of the function argument types from the arguments that are present. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4258 | PointerType *PFTy = nullptr; |
| 4259 | FunctionType *Ty = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4260 | if (!(PFTy = dyn_cast<PointerType>(RetType)) || |
| 4261 | !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) { |
| 4262 | // Pull out the types of all of the arguments... |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 4263 | std::vector<Type*> ParamTypes; |
Eli Friedman | 6cf5141 | 2010-07-24 23:06:59 +0000 | [diff] [blame] | 4264 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) |
| 4265 | ParamTypes.push_back(ArgList[i].V->getType()); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4266 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4267 | if (!FunctionType::isValidReturnType(RetType)) |
| 4268 | return Error(RetTypeLoc, "Invalid result type for LLVM function"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4269 | |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 4270 | Ty = FunctionType::get(RetType, ParamTypes, false); |
| 4271 | PFTy = PointerType::getUnqual(Ty); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4272 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4273 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4274 | // Look up the callee. |
| 4275 | Value *Callee; |
Victor Hernandez | 9d75c96 | 2010-01-11 22:31:58 +0000 | [diff] [blame] | 4276 | if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4277 | |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 4278 | // Set up the Attribute for the function. |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 4279 | SmallVector<AttributeSet, 8> Attrs; |
Bill Wendling | 3bef2dd | 2012-09-19 23:54:18 +0000 | [diff] [blame] | 4280 | if (RetAttrs.hasAttributes()) |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 4281 | Attrs.push_back(AttributeSet::get(RetType->getContext(), |
| 4282 | AttributeSet::ReturnIndex, |
| 4283 | RetAttrs)); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4284 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4285 | SmallVector<Value*, 8> Args; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4286 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4287 | // Loop through FunctionType's arguments and ensure they are specified |
| 4288 | // correctly. Also, gather any parameter attributes. |
| 4289 | FunctionType::param_iterator I = Ty->param_begin(); |
| 4290 | FunctionType::param_iterator E = Ty->param_end(); |
| 4291 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4292 | Type *ExpectedTy = nullptr; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4293 | if (I != E) { |
| 4294 | ExpectedTy = *I++; |
| 4295 | } else if (!Ty->isVarArg()) { |
| 4296 | return Error(ArgList[i].Loc, "too many arguments specified"); |
| 4297 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4298 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4299 | if (ExpectedTy && ExpectedTy != ArgList[i].V->getType()) |
| 4300 | return Error(ArgList[i].Loc, "argument is not of expected type '" + |
Chris Lattner | 0f214eb | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 4301 | getTypeString(ExpectedTy) + "'"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4302 | Args.push_back(ArgList[i].V); |
Bill Wendling | fe0021a | 2013-01-31 00:29:54 +0000 | [diff] [blame] | 4303 | if (ArgList[i].Attrs.hasAttributes(i + 1)) { |
| 4304 | AttrBuilder B(ArgList[i].Attrs, i + 1); |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 4305 | Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B)); |
| 4306 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4307 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4308 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4309 | if (I != E) |
| 4310 | return Error(CallLoc, "not enough parameters specified for call"); |
| 4311 | |
Bill Wendling | 3bef2dd | 2012-09-19 23:54:18 +0000 | [diff] [blame] | 4312 | if (FnAttrs.hasAttributes()) |
Bill Wendling | f5075a4 | 2013-01-27 02:24:02 +0000 | [diff] [blame] | 4313 | Attrs.push_back(AttributeSet::get(RetType->getContext(), |
| 4314 | AttributeSet::FunctionIndex, |
| 4315 | FnAttrs)); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4316 | |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 4317 | // Finish off the Attribute and check them |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 4318 | AttributeSet PAL = AttributeSet::get(Context, Attrs); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4319 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 4320 | CallInst *CI = CallInst::Create(Callee, Args); |
Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 4321 | CI->setTailCallKind(TCK); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4322 | CI->setCallingConv(CC); |
| 4323 | CI->setAttributes(PAL); |
Bill Wendling | b32b041 | 2013-02-08 06:32:06 +0000 | [diff] [blame] | 4324 | ForwardRefAttrGroups[CI] = FwdRefAttrGrps; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4325 | Inst = CI; |
| 4326 | return false; |
| 4327 | } |
| 4328 | |
| 4329 | //===----------------------------------------------------------------------===// |
| 4330 | // Memory Instructions. |
| 4331 | //===----------------------------------------------------------------------===// |
| 4332 | |
| 4333 | /// ParseAlloc |
David Majnemer | c4ab61c | 2014-03-09 06:41:58 +0000 | [diff] [blame] | 4334 | /// ::= 'alloca' 'inalloca'? Type (',' TypeAndValue)? (',' 'align' i32)? |
Chris Lattner | 7810372 | 2011-06-17 03:16:47 +0000 | [diff] [blame] | 4335 | int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4336 | Value *Size = nullptr; |
Chris Lattner | 200e075 | 2009-07-02 23:08:13 +0000 | [diff] [blame] | 4337 | LocTy SizeLoc; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4338 | unsigned Alignment = 0; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4339 | Type *Ty = nullptr; |
David Majnemer | c4ab61c | 2014-03-09 06:41:58 +0000 | [diff] [blame] | 4340 | |
| 4341 | bool IsInAlloca = EatIfPresent(lltok::kw_inalloca); |
| 4342 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 4343 | if (ParseType(Ty)) return true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4344 | |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 4345 | bool AteExtraComma = false; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 4346 | if (EatIfPresent(lltok::comma)) { |
David Majnemer | c4ab61c | 2014-03-09 06:41:58 +0000 | [diff] [blame] | 4347 | if (Lex.getKind() == lltok::kw_align) { |
| 4348 | if (ParseOptionalAlignment(Alignment)) return true; |
| 4349 | } else if (Lex.getKind() == lltok::MetadataVar) { |
| 4350 | AteExtraComma = true; |
| 4351 | } else { |
| 4352 | if (ParseTypeAndValue(Size, SizeLoc, PFS) || |
| 4353 | ParseOptionalCommaAlign(Alignment, AteExtraComma)) |
| 4354 | return true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4355 | } |
| 4356 | } |
| 4357 | |
Dan Gohman | 2140a74 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 4358 | if (Size && !Size->getType()->isIntegerTy()) |
| 4359 | return Error(SizeLoc, "element count must have integer type"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4360 | |
Reid Kleckner | 436c42e | 2014-01-17 23:58:17 +0000 | [diff] [blame] | 4361 | AllocaInst *AI = new AllocaInst(Ty, Size, Alignment); |
| 4362 | AI->setUsedWithInAlloca(IsInAlloca); |
| 4363 | Inst = AI; |
Chris Lattner | 7810372 | 2011-06-17 03:16:47 +0000 | [diff] [blame] | 4364 | return AteExtraComma ? InstExtraComma : InstNormal; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4365 | } |
| 4366 | |
| 4367 | /// ParseLoad |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4368 | /// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)? |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4369 | /// ::= 'load' 'atomic' 'volatile'? TypeAndValue |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4370 | /// 'singlethread'? AtomicOrdering (',' 'align' i32)? |
Chris Lattner | bc63929 | 2011-11-27 06:56:53 +0000 | [diff] [blame] | 4371 | int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4372 | Value *Val; LocTy Loc; |
Devang Patel | ea8a4b9 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 4373 | unsigned Alignment = 0; |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 4374 | bool AteExtraComma = false; |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4375 | bool isAtomic = false; |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4376 | AtomicOrdering Ordering = NotAtomic; |
| 4377 | SynchronizationScope Scope = CrossThread; |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4378 | |
| 4379 | if (Lex.getKind() == lltok::kw_atomic) { |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4380 | isAtomic = true; |
| 4381 | Lex.Lex(); |
| 4382 | } |
| 4383 | |
Chris Lattner | bc63929 | 2011-11-27 06:56:53 +0000 | [diff] [blame] | 4384 | bool isVolatile = false; |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4385 | if (Lex.getKind() == lltok::kw_volatile) { |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4386 | isVolatile = true; |
| 4387 | Lex.Lex(); |
| 4388 | } |
| 4389 | |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 4390 | if (ParseTypeAndValue(Val, Loc, PFS) || |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4391 | ParseScopeAndOrdering(isAtomic, Scope, Ordering) || |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 4392 | ParseOptionalCommaAlign(Alignment, AteExtraComma)) |
| 4393 | return true; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4394 | |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 4395 | if (!Val->getType()->isPointerTy() || |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4396 | !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType()) |
| 4397 | 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] | 4398 | if (isAtomic && !Alignment) |
| 4399 | return Error(Loc, "atomic load must have explicit non-zero alignment"); |
| 4400 | if (Ordering == Release || Ordering == AcquireRelease) |
| 4401 | return Error(Loc, "atomic load cannot use Release ordering"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4402 | |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4403 | Inst = new LoadInst(Val, "", isVolatile, Alignment, Ordering, Scope); |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 4404 | return AteExtraComma ? InstExtraComma : InstNormal; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4405 | } |
| 4406 | |
| 4407 | /// ParseStore |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4408 | |
| 4409 | /// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)? |
| 4410 | /// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4411 | /// 'singlethread'? AtomicOrdering (',' 'align' i32)? |
Chris Lattner | bc63929 | 2011-11-27 06:56:53 +0000 | [diff] [blame] | 4412 | int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4413 | Value *Val, *Ptr; LocTy Loc, PtrLoc; |
Devang Patel | ea8a4b9 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 4414 | unsigned Alignment = 0; |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 4415 | bool AteExtraComma = false; |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4416 | bool isAtomic = false; |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4417 | AtomicOrdering Ordering = NotAtomic; |
| 4418 | SynchronizationScope Scope = CrossThread; |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4419 | |
| 4420 | if (Lex.getKind() == lltok::kw_atomic) { |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4421 | isAtomic = true; |
| 4422 | Lex.Lex(); |
| 4423 | } |
| 4424 | |
Chris Lattner | bc63929 | 2011-11-27 06:56:53 +0000 | [diff] [blame] | 4425 | bool isVolatile = false; |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4426 | if (Lex.getKind() == lltok::kw_volatile) { |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4427 | isVolatile = true; |
| 4428 | Lex.Lex(); |
| 4429 | } |
| 4430 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4431 | if (ParseTypeAndValue(Val, Loc, PFS) || |
| 4432 | ParseToken(lltok::comma, "expected ',' after store operand") || |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 4433 | ParseTypeAndValue(Ptr, PtrLoc, PFS) || |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4434 | ParseScopeAndOrdering(isAtomic, Scope, Ordering) || |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 4435 | ParseOptionalCommaAlign(Alignment, AteExtraComma)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4436 | return true; |
Devang Patel | ea8a4b9 | 2009-09-17 23:04:48 +0000 | [diff] [blame] | 4437 | |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 4438 | if (!Ptr->getType()->isPointerTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4439 | return Error(PtrLoc, "store operand must be a pointer"); |
| 4440 | if (!Val->getType()->isFirstClassType()) |
| 4441 | return Error(Loc, "store operand must be a first class value"); |
| 4442 | if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType()) |
| 4443 | return Error(Loc, "stored value and pointer type do not match"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4444 | if (isAtomic && !Alignment) |
| 4445 | return Error(Loc, "atomic store must have explicit non-zero alignment"); |
| 4446 | if (Ordering == Acquire || Ordering == AcquireRelease) |
| 4447 | return Error(Loc, "atomic store cannot use Acquire ordering"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4448 | |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4449 | Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, Scope); |
Chris Lattner | b2f3950 | 2009-12-30 05:44:30 +0000 | [diff] [blame] | 4450 | return AteExtraComma ? InstExtraComma : InstNormal; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4451 | } |
| 4452 | |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4453 | /// ParseCmpXchg |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 4454 | /// ::= 'cmpxchg' 'weak'? 'volatile'? TypeAndValue ',' TypeAndValue ',' |
| 4455 | /// TypeAndValue 'singlethread'? AtomicOrdering AtomicOrdering |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4456 | int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) { |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4457 | Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc; |
| 4458 | bool AteExtraComma = false; |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 4459 | AtomicOrdering SuccessOrdering = NotAtomic; |
| 4460 | AtomicOrdering FailureOrdering = NotAtomic; |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4461 | SynchronizationScope Scope = CrossThread; |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4462 | bool isVolatile = false; |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 4463 | bool isWeak = false; |
| 4464 | |
| 4465 | if (EatIfPresent(lltok::kw_weak)) |
| 4466 | isWeak = true; |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4467 | |
| 4468 | if (EatIfPresent(lltok::kw_volatile)) |
| 4469 | isVolatile = true; |
| 4470 | |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4471 | if (ParseTypeAndValue(Ptr, PtrLoc, PFS) || |
| 4472 | ParseToken(lltok::comma, "expected ',' after cmpxchg address") || |
| 4473 | ParseTypeAndValue(Cmp, CmpLoc, PFS) || |
| 4474 | ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") || |
| 4475 | ParseTypeAndValue(New, NewLoc, PFS) || |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 4476 | ParseScopeAndOrdering(true /*Always atomic*/, Scope, SuccessOrdering) || |
| 4477 | ParseOrdering(FailureOrdering)) |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4478 | return true; |
| 4479 | |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 4480 | if (SuccessOrdering == Unordered || FailureOrdering == Unordered) |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4481 | return TokError("cmpxchg cannot be unordered"); |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 4482 | if (SuccessOrdering < FailureOrdering) |
| 4483 | return TokError("cmpxchg must be at least as ordered on success as failure"); |
| 4484 | if (FailureOrdering == Release || FailureOrdering == AcquireRelease) |
| 4485 | return TokError("cmpxchg failure ordering cannot include release semantics"); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4486 | if (!Ptr->getType()->isPointerTy()) |
| 4487 | return Error(PtrLoc, "cmpxchg operand must be a pointer"); |
| 4488 | if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType()) |
| 4489 | return Error(CmpLoc, "compare value and pointer type do not match"); |
| 4490 | if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType()) |
| 4491 | return Error(NewLoc, "new value and pointer type do not match"); |
| 4492 | if (!New->getType()->isIntegerTy()) |
| 4493 | return Error(NewLoc, "cmpxchg operand must be an integer"); |
| 4494 | unsigned Size = New->getType()->getPrimitiveSizeInBits(); |
| 4495 | if (Size < 8 || (Size & (Size - 1))) |
| 4496 | return Error(NewLoc, "cmpxchg operand must be power-of-two byte-sized" |
| 4497 | " integer"); |
| 4498 | |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 4499 | AtomicCmpXchgInst *CXI = new AtomicCmpXchgInst( |
| 4500 | Ptr, Cmp, New, SuccessOrdering, FailureOrdering, Scope); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4501 | CXI->setVolatile(isVolatile); |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 4502 | CXI->setWeak(isWeak); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4503 | Inst = CXI; |
| 4504 | return AteExtraComma ? InstExtraComma : InstNormal; |
| 4505 | } |
| 4506 | |
| 4507 | /// ParseAtomicRMW |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4508 | /// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue |
| 4509 | /// 'singlethread'? AtomicOrdering |
| 4510 | int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) { |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4511 | Value *Ptr, *Val; LocTy PtrLoc, ValLoc; |
| 4512 | bool AteExtraComma = false; |
| 4513 | AtomicOrdering Ordering = NotAtomic; |
| 4514 | SynchronizationScope Scope = CrossThread; |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4515 | bool isVolatile = false; |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4516 | AtomicRMWInst::BinOp Operation; |
Eli Friedman | 02e737b | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 4517 | |
| 4518 | if (EatIfPresent(lltok::kw_volatile)) |
| 4519 | isVolatile = true; |
| 4520 | |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4521 | switch (Lex.getKind()) { |
| 4522 | default: return TokError("expected binary operation in atomicrmw"); |
| 4523 | case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break; |
| 4524 | case lltok::kw_add: Operation = AtomicRMWInst::Add; break; |
| 4525 | case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break; |
| 4526 | case lltok::kw_and: Operation = AtomicRMWInst::And; break; |
| 4527 | case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break; |
| 4528 | case lltok::kw_or: Operation = AtomicRMWInst::Or; break; |
| 4529 | case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break; |
| 4530 | case lltok::kw_max: Operation = AtomicRMWInst::Max; break; |
| 4531 | case lltok::kw_min: Operation = AtomicRMWInst::Min; break; |
| 4532 | case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break; |
| 4533 | case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break; |
| 4534 | } |
| 4535 | Lex.Lex(); // Eat the operation. |
| 4536 | |
| 4537 | if (ParseTypeAndValue(Ptr, PtrLoc, PFS) || |
| 4538 | ParseToken(lltok::comma, "expected ',' after atomicrmw address") || |
| 4539 | ParseTypeAndValue(Val, ValLoc, PFS) || |
| 4540 | ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering)) |
| 4541 | return true; |
| 4542 | |
| 4543 | if (Ordering == Unordered) |
| 4544 | return TokError("atomicrmw cannot be unordered"); |
| 4545 | if (!Ptr->getType()->isPointerTy()) |
| 4546 | return Error(PtrLoc, "atomicrmw operand must be a pointer"); |
| 4547 | if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType()) |
| 4548 | return Error(ValLoc, "atomicrmw value and pointer type do not match"); |
| 4549 | if (!Val->getType()->isIntegerTy()) |
| 4550 | return Error(ValLoc, "atomicrmw operand must be an integer"); |
| 4551 | unsigned Size = Val->getType()->getPrimitiveSizeInBits(); |
| 4552 | if (Size < 8 || (Size & (Size - 1))) |
| 4553 | return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized" |
| 4554 | " integer"); |
| 4555 | |
| 4556 | AtomicRMWInst *RMWI = |
| 4557 | new AtomicRMWInst(Operation, Ptr, Val, Ordering, Scope); |
| 4558 | RMWI->setVolatile(isVolatile); |
| 4559 | Inst = RMWI; |
| 4560 | return AteExtraComma ? InstExtraComma : InstNormal; |
| 4561 | } |
| 4562 | |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 4563 | /// ParseFence |
| 4564 | /// ::= 'fence' 'singlethread'? AtomicOrdering |
| 4565 | int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) { |
| 4566 | AtomicOrdering Ordering = NotAtomic; |
| 4567 | SynchronizationScope Scope = CrossThread; |
| 4568 | if (ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering)) |
| 4569 | return true; |
| 4570 | |
| 4571 | if (Ordering == Unordered) |
| 4572 | return TokError("fence cannot be unordered"); |
| 4573 | if (Ordering == Monotonic) |
| 4574 | return TokError("fence cannot be monotonic"); |
| 4575 | |
| 4576 | Inst = new FenceInst(Context, Ordering, Scope); |
| 4577 | return InstNormal; |
| 4578 | } |
| 4579 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4580 | /// ParseGetElementPtr |
Dan Gohman | 1639c39 | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 4581 | /// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)* |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4582 | int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4583 | Value *Ptr = nullptr; |
| 4584 | Value *Val = nullptr; |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 4585 | LocTy Loc, EltLoc; |
Dan Gohman | 1639c39 | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 4586 | |
Dan Gohman | 16cbbe4 | 2009-07-29 15:58:36 +0000 | [diff] [blame] | 4587 | bool InBounds = EatIfPresent(lltok::kw_inbounds); |
Dan Gohman | 1639c39 | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 4588 | |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 4589 | if (ParseTypeAndValue(Ptr, Loc, PFS)) return true; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4590 | |
Eli Bendersky | d980668 | 2013-04-22 17:03:42 +0000 | [diff] [blame] | 4591 | Type *BaseType = Ptr->getType(); |
| 4592 | PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType()); |
| 4593 | if (!BasePointerType) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4594 | return Error(Loc, "base of getelementptr must be a pointer"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4595 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4596 | SmallVector<Value*, 16> Indices; |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4597 | bool AteExtraComma = false; |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 4598 | while (EatIfPresent(lltok::comma)) { |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4599 | if (Lex.getKind() == lltok::MetadataVar) { |
| 4600 | AteExtraComma = true; |
Devang Patel | 52b1745 | 2009-10-13 18:49:55 +0000 | [diff] [blame] | 4601 | break; |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4602 | } |
Chris Lattner | 3822f63 | 2009-01-02 08:05:26 +0000 | [diff] [blame] | 4603 | if (ParseTypeAndValue(Val, EltLoc, PFS)) return true; |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 4604 | if (!Val->getType()->getScalarType()->isIntegerTy()) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4605 | return Error(EltLoc, "getelementptr index must be an integer"); |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 4606 | if (Val->getType()->isVectorTy() != Ptr->getType()->isVectorTy()) |
| 4607 | return Error(EltLoc, "getelementptr index type missmatch"); |
| 4608 | if (Val->getType()->isVectorTy()) { |
| 4609 | unsigned ValNumEl = cast<VectorType>(Val->getType())->getNumElements(); |
| 4610 | unsigned PtrNumEl = cast<VectorType>(Ptr->getType())->getNumElements(); |
| 4611 | if (ValNumEl != PtrNumEl) |
| 4612 | return Error(EltLoc, |
| 4613 | "getelementptr vector index has a wrong number of elements"); |
| 4614 | } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4615 | Indices.push_back(Val); |
| 4616 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4617 | |
Eli Bendersky | d980668 | 2013-04-22 17:03:42 +0000 | [diff] [blame] | 4618 | if (!Indices.empty() && !BasePointerType->getElementType()->isSized()) |
| 4619 | return Error(Loc, "base element of getelementptr must be sized"); |
| 4620 | |
| 4621 | if (!GetElementPtrInst::getIndexedType(BaseType, Indices)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4622 | return Error(Loc, "invalid getelementptr indices"); |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 4623 | Inst = GetElementPtrInst::Create(Ptr, Indices); |
Dan Gohman | 1639c39 | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 4624 | if (InBounds) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 4625 | cast<GetElementPtrInst>(Inst)->setIsInBounds(true); |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4626 | return AteExtraComma ? InstExtraComma : InstNormal; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4627 | } |
| 4628 | |
| 4629 | /// ParseExtractValue |
| 4630 | /// ::= 'extractvalue' TypeAndValue (',' uint32)+ |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4631 | int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4632 | Value *Val; LocTy Loc; |
| 4633 | SmallVector<unsigned, 4> Indices; |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4634 | bool AteExtraComma; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4635 | if (ParseTypeAndValue(Val, Loc, PFS) || |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4636 | ParseIndexList(Indices, AteExtraComma)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4637 | return true; |
| 4638 | |
Chris Lattner | 392be58 | 2010-02-12 20:49:41 +0000 | [diff] [blame] | 4639 | if (!Val->getType()->isAggregateType()) |
| 4640 | return Error(Loc, "extractvalue operand must be aggregate type"); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4641 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 4642 | if (!ExtractValueInst::getIndexedType(Val->getType(), Indices)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4643 | return Error(Loc, "invalid indices for extractvalue"); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 4644 | Inst = ExtractValueInst::Create(Val, Indices); |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4645 | return AteExtraComma ? InstExtraComma : InstNormal; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4646 | } |
| 4647 | |
| 4648 | /// ParseInsertValue |
| 4649 | /// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+ |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4650 | int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) { |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4651 | Value *Val0, *Val1; LocTy Loc0, Loc1; |
| 4652 | SmallVector<unsigned, 4> Indices; |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4653 | bool AteExtraComma; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4654 | if (ParseTypeAndValue(Val0, Loc0, PFS) || |
| 4655 | ParseToken(lltok::comma, "expected comma after insertvalue operand") || |
| 4656 | ParseTypeAndValue(Val1, Loc1, PFS) || |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4657 | ParseIndexList(Indices, AteExtraComma)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4658 | return true; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4659 | |
Chris Lattner | 392be58 | 2010-02-12 20:49:41 +0000 | [diff] [blame] | 4660 | if (!Val0->getType()->isAggregateType()) |
| 4661 | return Error(Loc0, "insertvalue operand must be aggregate type"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4662 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 4663 | if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices)) |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4664 | return Error(Loc0, "invalid indices for insertvalue"); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 4665 | Inst = InsertValueInst::Create(Val0, Val1, Indices); |
Chris Lattner | f4f0342 | 2009-12-30 05:27:33 +0000 | [diff] [blame] | 4666 | return AteExtraComma ? InstExtraComma : InstNormal; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 4667 | } |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 4668 | |
| 4669 | //===----------------------------------------------------------------------===// |
| 4670 | // Embedded metadata. |
| 4671 | //===----------------------------------------------------------------------===// |
| 4672 | |
| 4673 | /// ParseMDNodeVector |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 4674 | /// ::= { Element (',' Element)* } |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 4675 | /// Element |
| 4676 | /// ::= 'null' | TypeAndValue |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 4677 | bool LLParser::ParseMDNodeVector(SmallVectorImpl<Metadata *> &Elts) { |
David Majnemer | 06f960d | 2014-12-11 20:44:09 +0000 | [diff] [blame] | 4678 | if (ParseToken(lltok::lbrace, "expected '{' here")) |
| 4679 | return true; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 4680 | |
Dan Gohman | 1e0213a | 2010-07-13 19:33:27 +0000 | [diff] [blame] | 4681 | // Check for an empty list. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 4682 | if (EatIfPresent(lltok::rbrace)) |
Dan Gohman | 1e0213a | 2010-07-13 19:33:27 +0000 | [diff] [blame] | 4683 | return false; |
| 4684 | |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 4685 | do { |
Chris Lattner | a01ddfc | 2009-12-30 04:42:57 +0000 | [diff] [blame] | 4686 | // Null is a special case since it is typeless. |
| 4687 | if (EatIfPresent(lltok::kw_null)) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4688 | Elts.push_back(nullptr); |
Chris Lattner | a01ddfc | 2009-12-30 04:42:57 +0000 | [diff] [blame] | 4689 | continue; |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 4690 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 4691 | |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 4692 | Metadata *MD; |
| 4693 | if (ParseMetadata(MD, nullptr)) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 4694 | return true; |
Duncan P. N. Exon Smith | be7ea19 | 2014-12-15 19:07:53 +0000 | [diff] [blame] | 4695 | Elts.push_back(MD); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 4696 | } while (EatIfPresent(lltok::comma)); |
| 4697 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 4698 | return ParseToken(lltok::rbrace, "expected end of metadata node"); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 4699 | } |
Duncan P. N. Exon Smith | 0a448fb | 2014-08-19 21:30:15 +0000 | [diff] [blame] | 4700 | |
| 4701 | //===----------------------------------------------------------------------===// |
| 4702 | // Use-list order directives. |
| 4703 | //===----------------------------------------------------------------------===// |
| 4704 | bool LLParser::sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes, |
| 4705 | SMLoc Loc) { |
| 4706 | if (V->use_empty()) |
| 4707 | return Error(Loc, "value has no uses"); |
| 4708 | |
| 4709 | unsigned NumUses = 0; |
| 4710 | SmallDenseMap<const Use *, unsigned, 16> Order; |
| 4711 | for (const Use &U : V->uses()) { |
| 4712 | if (++NumUses > Indexes.size()) |
| 4713 | break; |
| 4714 | Order[&U] = Indexes[NumUses - 1]; |
| 4715 | } |
| 4716 | if (NumUses < 2) |
| 4717 | return Error(Loc, "value only has one use"); |
| 4718 | if (Order.size() != Indexes.size() || NumUses > Indexes.size()) |
| 4719 | return Error(Loc, "wrong number of indexes, expected " + |
| 4720 | Twine(std::distance(V->use_begin(), V->use_end()))); |
| 4721 | |
| 4722 | V->sortUseList([&](const Use &L, const Use &R) { |
| 4723 | return Order.lookup(&L) < Order.lookup(&R); |
| 4724 | }); |
| 4725 | return false; |
| 4726 | } |
| 4727 | |
| 4728 | /// ParseUseListOrderIndexes |
| 4729 | /// ::= '{' uint32 (',' uint32)+ '}' |
| 4730 | bool LLParser::ParseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes) { |
| 4731 | SMLoc Loc = Lex.getLoc(); |
| 4732 | if (ParseToken(lltok::lbrace, "expected '{' here")) |
| 4733 | return true; |
| 4734 | if (Lex.getKind() == lltok::rbrace) |
| 4735 | return Lex.Error("expected non-empty list of uselistorder indexes"); |
| 4736 | |
| 4737 | // Use Offset, Max, and IsOrdered to check consistency of indexes. The |
| 4738 | // indexes should be distinct numbers in the range [0, size-1], and should |
| 4739 | // not be in order. |
| 4740 | unsigned Offset = 0; |
| 4741 | unsigned Max = 0; |
| 4742 | bool IsOrdered = true; |
| 4743 | assert(Indexes.empty() && "Expected empty order vector"); |
| 4744 | do { |
| 4745 | unsigned Index; |
| 4746 | if (ParseUInt32(Index)) |
| 4747 | return true; |
| 4748 | |
| 4749 | // Update consistency checks. |
| 4750 | Offset += Index - Indexes.size(); |
| 4751 | Max = std::max(Max, Index); |
| 4752 | IsOrdered &= Index == Indexes.size(); |
| 4753 | |
| 4754 | Indexes.push_back(Index); |
| 4755 | } while (EatIfPresent(lltok::comma)); |
| 4756 | |
| 4757 | if (ParseToken(lltok::rbrace, "expected '}' here")) |
| 4758 | return true; |
| 4759 | |
| 4760 | if (Indexes.size() < 2) |
| 4761 | return Error(Loc, "expected >= 2 uselistorder indexes"); |
| 4762 | if (Offset != 0 || Max >= Indexes.size()) |
| 4763 | return Error(Loc, "expected distinct uselistorder indexes in range [0, size)"); |
| 4764 | if (IsOrdered) |
| 4765 | return Error(Loc, "expected uselistorder indexes to change the order"); |
| 4766 | |
| 4767 | return false; |
| 4768 | } |
| 4769 | |
| 4770 | /// ParseUseListOrder |
| 4771 | /// ::= 'uselistorder' Type Value ',' UseListOrderIndexes |
| 4772 | bool LLParser::ParseUseListOrder(PerFunctionState *PFS) { |
| 4773 | SMLoc Loc = Lex.getLoc(); |
| 4774 | if (ParseToken(lltok::kw_uselistorder, "expected uselistorder directive")) |
| 4775 | return true; |
| 4776 | |
| 4777 | Value *V; |
| 4778 | SmallVector<unsigned, 16> Indexes; |
| 4779 | if (ParseTypeAndValue(V, PFS) || |
| 4780 | ParseToken(lltok::comma, "expected comma in uselistorder directive") || |
| 4781 | ParseUseListOrderIndexes(Indexes)) |
| 4782 | return true; |
| 4783 | |
| 4784 | return sortUseListOrder(V, Indexes, Loc); |
| 4785 | } |
| 4786 | |
| 4787 | /// ParseUseListOrderBB |
| 4788 | /// ::= 'uselistorder_bb' @foo ',' %bar ',' UseListOrderIndexes |
| 4789 | bool LLParser::ParseUseListOrderBB() { |
| 4790 | assert(Lex.getKind() == lltok::kw_uselistorder_bb); |
| 4791 | SMLoc Loc = Lex.getLoc(); |
| 4792 | Lex.Lex(); |
| 4793 | |
| 4794 | ValID Fn, Label; |
| 4795 | SmallVector<unsigned, 16> Indexes; |
| 4796 | if (ParseValID(Fn) || |
| 4797 | ParseToken(lltok::comma, "expected comma in uselistorder_bb directive") || |
| 4798 | ParseValID(Label) || |
| 4799 | ParseToken(lltok::comma, "expected comma in uselistorder_bb directive") || |
| 4800 | ParseUseListOrderIndexes(Indexes)) |
| 4801 | return true; |
| 4802 | |
| 4803 | // Check the function. |
| 4804 | GlobalValue *GV; |
| 4805 | if (Fn.Kind == ValID::t_GlobalName) |
| 4806 | GV = M->getNamedValue(Fn.StrVal); |
| 4807 | else if (Fn.Kind == ValID::t_GlobalID) |
| 4808 | GV = Fn.UIntVal < NumberedVals.size() ? NumberedVals[Fn.UIntVal] : nullptr; |
| 4809 | else |
| 4810 | return Error(Fn.Loc, "expected function name in uselistorder_bb"); |
| 4811 | if (!GV) |
| 4812 | return Error(Fn.Loc, "invalid function forward reference in uselistorder_bb"); |
| 4813 | auto *F = dyn_cast<Function>(GV); |
| 4814 | if (!F) |
| 4815 | return Error(Fn.Loc, "expected function name in uselistorder_bb"); |
| 4816 | if (F->isDeclaration()) |
| 4817 | return Error(Fn.Loc, "invalid declaration in uselistorder_bb"); |
| 4818 | |
| 4819 | // Check the basic block. |
| 4820 | if (Label.Kind == ValID::t_LocalID) |
| 4821 | return Error(Label.Loc, "invalid numeric label in uselistorder_bb"); |
| 4822 | if (Label.Kind != ValID::t_LocalName) |
| 4823 | return Error(Label.Loc, "expected basic block name in uselistorder_bb"); |
| 4824 | Value *V = F->getValueSymbolTable().lookup(Label.StrVal); |
| 4825 | if (!V) |
| 4826 | return Error(Label.Loc, "invalid basic block in uselistorder_bb"); |
| 4827 | if (!isa<BasicBlock>(V)) |
| 4828 | return Error(Label.Loc, "expected basic block in uselistorder_bb"); |
| 4829 | |
| 4830 | return sortUseListOrder(V, Indexes, Loc); |
| 4831 | } |