| Chad Rosier | 571c5e9 | 2012-08-17 21:27:25 +0000 | [diff] [blame] | 1 | //===--- SemaStmtAsm.cpp - Semantic Analysis for Asm Statements -----------===// |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements semantic analysis for inline asm statements. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| Weiming Zhao | 71ac240 | 2015-02-03 22:35:58 +0000 | [diff] [blame] | 13 | #include "clang/AST/ExprCXX.h" |
| Craig Topper | d35bcbb | 2019-12-23 10:38:38 -0800 | [diff] [blame] | 14 | #include "clang/AST/GlobalDecl.h" |
| Chad Rosier | 5c56364 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 15 | #include "clang/AST/RecordLayout.h" |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 16 | #include "clang/AST/TypeLoc.h" |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
| Ehsan Akhgari | 3109758 | 2014-09-22 02:21:54 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/Sema/Initialization.h" |
| 20 | #include "clang/Sema/Lookup.h" |
| 21 | #include "clang/Sema/Scope.h" |
| 22 | #include "clang/Sema/ScopeInfo.h" |
| Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 23 | #include "clang/Sema/SemaInternal.h" |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/ArrayRef.h" |
| Marina Yatsina | c42fd03 | 2016-12-26 12:23:42 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringSet.h" |
| Alp Toker | 1039927 | 2014-06-08 05:11:37 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCParser/MCAsmParser.h" |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | using namespace sema; |
| 29 | |
| Aleksei Sidorin | 55365e4 | 2018-10-20 22:49:23 +0000 | [diff] [blame] | 30 | /// Remove the upper-level LValueToRValue cast from an expression. |
| 31 | static void removeLValueToRValueCast(Expr *E) { |
| 32 | Expr *Parent = E; |
| 33 | Expr *ExprUnderCast = nullptr; |
| 34 | SmallVector<Expr *, 8> ParentsToUpdate; |
| 35 | |
| 36 | while (true) { |
| 37 | ParentsToUpdate.push_back(Parent); |
| 38 | if (auto *ParenE = dyn_cast<ParenExpr>(Parent)) { |
| 39 | Parent = ParenE->getSubExpr(); |
| 40 | continue; |
| 41 | } |
| 42 | |
| 43 | Expr *Child = nullptr; |
| 44 | CastExpr *ParentCast = dyn_cast<CastExpr>(Parent); |
| 45 | if (ParentCast) |
| 46 | Child = ParentCast->getSubExpr(); |
| 47 | else |
| 48 | return; |
| 49 | |
| 50 | if (auto *CastE = dyn_cast<CastExpr>(Child)) |
| 51 | if (CastE->getCastKind() == CK_LValueToRValue) { |
| 52 | ExprUnderCast = CastE->getSubExpr(); |
| 53 | // LValueToRValue cast inside GCCAsmStmt requires an explicit cast. |
| 54 | ParentCast->setSubExpr(ExprUnderCast); |
| 55 | break; |
| 56 | } |
| 57 | Parent = Child; |
| 58 | } |
| 59 | |
| 60 | // Update parent expressions to have same ValueType as the underlying. |
| 61 | assert(ExprUnderCast && |
| 62 | "Should be reachable only if LValueToRValue cast was found!"); |
| 63 | auto ValueKind = ExprUnderCast->getValueKind(); |
| 64 | for (Expr *E : ParentsToUpdate) |
| 65 | E->setValueKind(ValueKind); |
| 66 | } |
| 67 | |
| 68 | /// Emit a warning about usage of "noop"-like casts for lvalues (GNU extension) |
| 69 | /// and fix the argument with removing LValueToRValue cast from the expression. |
| 70 | static void emitAndFixInvalidAsmCastLValue(const Expr *LVal, Expr *BadArgument, |
| 71 | Sema &S) { |
| 72 | if (!S.getLangOpts().HeinousExtensions) { |
| 73 | S.Diag(LVal->getBeginLoc(), diag::err_invalid_asm_cast_lvalue) |
| 74 | << BadArgument->getSourceRange(); |
| 75 | } else { |
| 76 | S.Diag(LVal->getBeginLoc(), diag::warn_invalid_asm_cast_lvalue) |
| 77 | << BadArgument->getSourceRange(); |
| 78 | } |
| 79 | removeLValueToRValueCast(BadArgument); |
| 80 | } |
| 81 | |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 82 | /// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently |
| 83 | /// ignore "noop" casts in places where an lvalue is required by an inline asm. |
| 84 | /// We emulate this behavior when -fheinous-gnu-extensions is specified, but |
| 85 | /// provide a strong guidance to not use it. |
| 86 | /// |
| 87 | /// This method checks to see if the argument is an acceptable l-value and |
| 88 | /// returns false if it is a case we can handle. |
| Aleksei Sidorin | 55365e4 | 2018-10-20 22:49:23 +0000 | [diff] [blame] | 89 | static bool CheckAsmLValue(Expr *E, Sema &S) { |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 90 | // Type dependent expressions will be checked during instantiation. |
| 91 | if (E->isTypeDependent()) |
| 92 | return false; |
| 93 | |
| 94 | if (E->isLValue()) |
| 95 | return false; // Cool, this is an lvalue. |
| 96 | |
| 97 | // Okay, this is not an lvalue, but perhaps it is the result of a cast that we |
| 98 | // are supposed to allow. |
| 99 | const Expr *E2 = E->IgnoreParenNoopCasts(S.Context); |
| 100 | if (E != E2 && E2->isLValue()) { |
| Aleksei Sidorin | 55365e4 | 2018-10-20 22:49:23 +0000 | [diff] [blame] | 101 | emitAndFixInvalidAsmCastLValue(E2, E, S); |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 102 | // Accept, even if we emitted an error diagnostic. |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | // None of the above, just randomly invalid non-lvalue. |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | /// isOperandMentioned - Return true if the specified operand # is mentioned |
| 111 | /// anywhere in the decomposed asm string. |
| Coby Tayree | 18f9218 | 2017-09-10 12:39:21 +0000 | [diff] [blame] | 112 | static bool |
| 113 | isOperandMentioned(unsigned OpNo, |
| 114 | ArrayRef<GCCAsmStmt::AsmStringPiece> AsmStrPieces) { |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 115 | for (unsigned p = 0, e = AsmStrPieces.size(); p != e; ++p) { |
| Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 116 | const GCCAsmStmt::AsmStringPiece &Piece = AsmStrPieces[p]; |
| Coby Tayree | 18f9218 | 2017-09-10 12:39:21 +0000 | [diff] [blame] | 117 | if (!Piece.isOperand()) |
| 118 | continue; |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 119 | |
| 120 | // If this is a reference to the input and if the input was the smaller |
| 121 | // one, then we have to reject this asm. |
| 122 | if (Piece.getOperandNo() == OpNo) |
| 123 | return true; |
| 124 | } |
| 125 | return false; |
| 126 | } |
| 127 | |
| Hans Wennborg | e9d240a | 2014-10-08 01:58:02 +0000 | [diff] [blame] | 128 | static bool CheckNakedParmReference(Expr *E, Sema &S) { |
| 129 | FunctionDecl *Func = dyn_cast<FunctionDecl>(S.CurContext); |
| 130 | if (!Func) |
| 131 | return false; |
| 132 | if (!Func->hasAttr<NakedAttr>()) |
| 133 | return false; |
| 134 | |
| 135 | SmallVector<Expr*, 4> WorkList; |
| 136 | WorkList.push_back(E); |
| 137 | while (WorkList.size()) { |
| 138 | Expr *E = WorkList.pop_back_val(); |
| Weiming Zhao | 71ac240 | 2015-02-03 22:35:58 +0000 | [diff] [blame] | 139 | if (isa<CXXThisExpr>(E)) { |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 140 | S.Diag(E->getBeginLoc(), diag::err_asm_naked_this_ref); |
| Weiming Zhao | 71ac240 | 2015-02-03 22:35:58 +0000 | [diff] [blame] | 141 | S.Diag(Func->getAttr<NakedAttr>()->getLocation(), diag::note_attribute); |
| 142 | return true; |
| 143 | } |
| Hans Wennborg | e9d240a | 2014-10-08 01:58:02 +0000 | [diff] [blame] | 144 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 145 | if (isa<ParmVarDecl>(DRE->getDecl())) { |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 146 | S.Diag(DRE->getBeginLoc(), diag::err_asm_naked_parm_ref); |
| Hans Wennborg | e9d240a | 2014-10-08 01:58:02 +0000 | [diff] [blame] | 147 | S.Diag(Func->getAttr<NakedAttr>()->getLocation(), diag::note_attribute); |
| 148 | return true; |
| 149 | } |
| 150 | } |
| 151 | for (Stmt *Child : E->children()) { |
| 152 | if (Expr *E = dyn_cast_or_null<Expr>(Child)) |
| 153 | WorkList.push_back(E); |
| 154 | } |
| 155 | } |
| 156 | return false; |
| 157 | } |
| 158 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 159 | /// Returns true if given expression is not compatible with inline |
| Andrey Bokhanko | d9eab9c | 2015-08-03 10:38:10 +0000 | [diff] [blame] | 160 | /// assembly's memory constraint; false otherwise. |
| 161 | static bool checkExprMemoryConstraintCompat(Sema &S, Expr *E, |
| 162 | TargetInfo::ConstraintInfo &Info, |
| 163 | bool is_input_expr) { |
| 164 | enum { |
| 165 | ExprBitfield = 0, |
| 166 | ExprVectorElt, |
| 167 | ExprGlobalRegVar, |
| 168 | ExprSafeType |
| 169 | } EType = ExprSafeType; |
| 170 | |
| 171 | // Bitfields, vector elements and global register variables are not |
| 172 | // compatible. |
| 173 | if (E->refersToBitField()) |
| 174 | EType = ExprBitfield; |
| 175 | else if (E->refersToVectorElement()) |
| 176 | EType = ExprVectorElt; |
| 177 | else if (E->refersToGlobalRegisterVar()) |
| 178 | EType = ExprGlobalRegVar; |
| 179 | |
| 180 | if (EType != ExprSafeType) { |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 181 | S.Diag(E->getBeginLoc(), diag::err_asm_non_addr_value_in_memory_constraint) |
| Andrey Bokhanko | d9eab9c | 2015-08-03 10:38:10 +0000 | [diff] [blame] | 182 | << EType << is_input_expr << Info.getConstraintStr() |
| 183 | << E->getSourceRange(); |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | return false; |
| 188 | } |
| 189 | |
| Marina Yatsina | c42fd03 | 2016-12-26 12:23:42 +0000 | [diff] [blame] | 190 | // Extracting the register name from the Expression value, |
| 191 | // if there is no register name to extract, returns "" |
| 192 | static StringRef extractRegisterName(const Expr *Expression, |
| 193 | const TargetInfo &Target) { |
| 194 | Expression = Expression->IgnoreImpCasts(); |
| 195 | if (const DeclRefExpr *AsmDeclRef = dyn_cast<DeclRefExpr>(Expression)) { |
| 196 | // Handle cases where the expression is a variable |
| 197 | const VarDecl *Variable = dyn_cast<VarDecl>(AsmDeclRef->getDecl()); |
| 198 | if (Variable && Variable->getStorageClass() == SC_Register) { |
| 199 | if (AsmLabelAttr *Attr = Variable->getAttr<AsmLabelAttr>()) |
| 200 | if (Target.isValidGCCRegisterName(Attr->getLabel())) |
| 201 | return Target.getNormalizedGCCRegisterName(Attr->getLabel(), true); |
| 202 | } |
| 203 | } |
| 204 | return ""; |
| 205 | } |
| 206 | |
| 207 | // Checks if there is a conflict between the input and output lists with the |
| 208 | // clobbers list. If there's a conflict, returns the location of the |
| 209 | // conflicted clobber, else returns nullptr |
| 210 | static SourceLocation |
| 211 | getClobberConflictLocation(MultiExprArg Exprs, StringLiteral **Constraints, |
| 212 | StringLiteral **Clobbers, int NumClobbers, |
| Jennifer Yu | b8fee67 | 2019-06-03 15:57:25 +0000 | [diff] [blame] | 213 | unsigned NumLabels, |
| Marina Yatsina | c42fd03 | 2016-12-26 12:23:42 +0000 | [diff] [blame] | 214 | const TargetInfo &Target, ASTContext &Cont) { |
| 215 | llvm::StringSet<> InOutVars; |
| 216 | // Collect all the input and output registers from the extended asm |
| Marina Yatsina | c5cf7a8 | 2016-12-26 13:16:40 +0000 | [diff] [blame] | 217 | // statement in order to check for conflicts with the clobber list |
| Jennifer Yu | b8fee67 | 2019-06-03 15:57:25 +0000 | [diff] [blame] | 218 | for (unsigned int i = 0; i < Exprs.size() - NumLabels; ++i) { |
| Marina Yatsina | c42fd03 | 2016-12-26 12:23:42 +0000 | [diff] [blame] | 219 | StringRef Constraint = Constraints[i]->getString(); |
| 220 | StringRef InOutReg = Target.getConstraintRegister( |
| 221 | Constraint, extractRegisterName(Exprs[i], Target)); |
| 222 | if (InOutReg != "") |
| 223 | InOutVars.insert(InOutReg); |
| 224 | } |
| 225 | // Check for each item in the clobber list if it conflicts with the input |
| 226 | // or output |
| 227 | for (int i = 0; i < NumClobbers; ++i) { |
| 228 | StringRef Clobber = Clobbers[i]->getString(); |
| 229 | // We only check registers, therefore we don't check cc and memory |
| 230 | // clobbers |
| 231 | if (Clobber == "cc" || Clobber == "memory") |
| 232 | continue; |
| 233 | Clobber = Target.getNormalizedGCCRegisterName(Clobber, true); |
| 234 | // Go over the output's registers we collected |
| 235 | if (InOutVars.count(Clobber)) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 236 | return Clobbers[i]->getBeginLoc(); |
| Marina Yatsina | c42fd03 | 2016-12-26 12:23:42 +0000 | [diff] [blame] | 237 | } |
| 238 | return SourceLocation(); |
| 239 | } |
| 240 | |
| Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 241 | StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple, |
| 242 | bool IsVolatile, unsigned NumOutputs, |
| 243 | unsigned NumInputs, IdentifierInfo **Names, |
| Dmitri Gribenko | ea2d5f8 | 2013-05-10 01:14:26 +0000 | [diff] [blame] | 244 | MultiExprArg constraints, MultiExprArg Exprs, |
| Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 245 | Expr *asmString, MultiExprArg clobbers, |
| Jennifer Yu | b8fee67 | 2019-06-03 15:57:25 +0000 | [diff] [blame] | 246 | unsigned NumLabels, |
| Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 247 | SourceLocation RParenLoc) { |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 248 | unsigned NumClobbers = clobbers.size(); |
| 249 | StringLiteral **Constraints = |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 250 | reinterpret_cast<StringLiteral**>(constraints.data()); |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 251 | StringLiteral *AsmString = cast<StringLiteral>(asmString); |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 252 | StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.data()); |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 253 | |
| 254 | SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos; |
| 255 | |
| 256 | // The parser verifies that there is a string literal here. |
| David Majnemer | b3e96f7 | 2014-12-11 01:00:48 +0000 | [diff] [blame] | 257 | assert(AsmString->isAscii()); |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 258 | |
| Craig Topper | d35bcbb | 2019-12-23 10:38:38 -0800 | [diff] [blame] | 259 | FunctionDecl *FD = dyn_cast<FunctionDecl>(getCurLexicalContext()); |
| 260 | llvm::StringMap<bool> FeatureMap; |
| 261 | Context.getFunctionFeatureMap(FeatureMap, FD); |
| 262 | |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 263 | for (unsigned i = 0; i != NumOutputs; i++) { |
| 264 | StringLiteral *Literal = Constraints[i]; |
| David Majnemer | b3e96f7 | 2014-12-11 01:00:48 +0000 | [diff] [blame] | 265 | assert(Literal->isAscii()); |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 266 | |
| 267 | StringRef OutputName; |
| 268 | if (Names[i]) |
| 269 | OutputName = Names[i]->getName(); |
| 270 | |
| 271 | TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName); |
| Alexey Bataev | 305b6b9 | 2019-02-26 21:51:16 +0000 | [diff] [blame] | 272 | if (!Context.getTargetInfo().validateOutputConstraint(Info)) { |
| 273 | targetDiag(Literal->getBeginLoc(), |
| 274 | diag::err_asm_invalid_output_constraint) |
| 275 | << Info.getConstraintStr(); |
| 276 | return new (Context) |
| 277 | GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs, |
| 278 | NumInputs, Names, Constraints, Exprs.data(), AsmString, |
| Jennifer Yu | b8fee67 | 2019-06-03 15:57:25 +0000 | [diff] [blame] | 279 | NumClobbers, Clobbers, NumLabels, RParenLoc); |
| Alexey Bataev | 305b6b9 | 2019-02-26 21:51:16 +0000 | [diff] [blame] | 280 | } |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 281 | |
| David Majnemer | 0f4d641 | 2014-12-29 09:30:33 +0000 | [diff] [blame] | 282 | ExprResult ER = CheckPlaceholderExpr(Exprs[i]); |
| 283 | if (ER.isInvalid()) |
| 284 | return StmtError(); |
| 285 | Exprs[i] = ER.get(); |
| 286 | |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 287 | // Check that the output exprs are valid lvalues. |
| 288 | Expr *OutputExpr = Exprs[i]; |
| Bill Wendling | c4fc3a2 | 2013-03-25 21:09:49 +0000 | [diff] [blame] | 289 | |
| Hans Wennborg | e9d240a | 2014-10-08 01:58:02 +0000 | [diff] [blame] | 290 | // Referring to parameters is not allowed in naked functions. |
| 291 | if (CheckNakedParmReference(OutputExpr, *this)) |
| 292 | return StmtError(); |
| 293 | |
| Andrey Bokhanko | d9eab9c | 2015-08-03 10:38:10 +0000 | [diff] [blame] | 294 | // Check that the output expression is compatible with memory constraint. |
| 295 | if (Info.allowsMemory() && |
| 296 | checkExprMemoryConstraintCompat(*this, OutputExpr, Info, false)) |
| 297 | return StmtError(); |
| Alexander Musman | eae29e2 | 2015-06-05 13:40:59 +0000 | [diff] [blame] | 298 | |
| Erich Keane | 5f1f4a5 | 2020-05-13 13:24:03 -0700 | [diff] [blame] | 299 | // Disallow _ExtInt, since the backends tend to have difficulties with |
| 300 | // non-normal sizes. |
| 301 | if (OutputExpr->getType()->isExtIntType()) |
| 302 | return StmtError( |
| 303 | Diag(OutputExpr->getBeginLoc(), diag::err_asm_invalid_type) |
| 304 | << OutputExpr->getType() << 0 /*Input*/ |
| 305 | << OutputExpr->getSourceRange()); |
| 306 | |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 307 | OutputConstraintInfos.push_back(Info); |
| Akira Hatanaka | 974131e | 2014-09-18 18:17:18 +0000 | [diff] [blame] | 308 | |
| David Majnemer | 0f4d641 | 2014-12-29 09:30:33 +0000 | [diff] [blame] | 309 | // If this is dependent, just continue. |
| 310 | if (OutputExpr->isTypeDependent()) |
| Akira Hatanaka | 974131e | 2014-09-18 18:17:18 +0000 | [diff] [blame] | 311 | continue; |
| 312 | |
| David Majnemer | 0f4d641 | 2014-12-29 09:30:33 +0000 | [diff] [blame] | 313 | Expr::isModifiableLvalueResult IsLV = |
| 314 | OutputExpr->isModifiableLvalue(Context, /*Loc=*/nullptr); |
| 315 | switch (IsLV) { |
| 316 | case Expr::MLV_Valid: |
| 317 | // Cool, this is an lvalue. |
| 318 | break; |
| David Majnemer | 04b7841 | 2014-12-29 10:29:53 +0000 | [diff] [blame] | 319 | case Expr::MLV_ArrayType: |
| 320 | // This is OK too. |
| 321 | break; |
| David Majnemer | 0f4d641 | 2014-12-29 09:30:33 +0000 | [diff] [blame] | 322 | case Expr::MLV_LValueCast: { |
| 323 | const Expr *LVal = OutputExpr->IgnoreParenNoopCasts(Context); |
| Aleksei Sidorin | 55365e4 | 2018-10-20 22:49:23 +0000 | [diff] [blame] | 324 | emitAndFixInvalidAsmCastLValue(LVal, OutputExpr, *this); |
| David Majnemer | 0f4d641 | 2014-12-29 09:30:33 +0000 | [diff] [blame] | 325 | // Accept, even if we emitted an error diagnostic. |
| 326 | break; |
| 327 | } |
| 328 | case Expr::MLV_IncompleteType: |
| 329 | case Expr::MLV_IncompleteVoidType: |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 330 | if (RequireCompleteType(OutputExpr->getBeginLoc(), Exprs[i]->getType(), |
| David Majnemer | 0f4d641 | 2014-12-29 09:30:33 +0000 | [diff] [blame] | 331 | diag::err_dereference_incomplete_type)) |
| 332 | return StmtError(); |
| Galina Kistanova | 3339911 | 2017-06-03 06:35:06 +0000 | [diff] [blame] | 333 | LLVM_FALLTHROUGH; |
| David Majnemer | 0f4d641 | 2014-12-29 09:30:33 +0000 | [diff] [blame] | 334 | default: |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 335 | return StmtError(Diag(OutputExpr->getBeginLoc(), |
| David Majnemer | 0f4d641 | 2014-12-29 09:30:33 +0000 | [diff] [blame] | 336 | diag::err_asm_invalid_lvalue_in_output) |
| 337 | << OutputExpr->getSourceRange()); |
| 338 | } |
| 339 | |
| 340 | unsigned Size = Context.getTypeSize(OutputExpr->getType()); |
| Craig Topper | d35bcbb | 2019-12-23 10:38:38 -0800 | [diff] [blame] | 341 | if (!Context.getTargetInfo().validateOutputSize( |
| 342 | FeatureMap, Literal->getString(), Size)) { |
| Alexey Bataev | 305b6b9 | 2019-02-26 21:51:16 +0000 | [diff] [blame] | 343 | targetDiag(OutputExpr->getBeginLoc(), diag::err_asm_invalid_output_size) |
| 344 | << Info.getConstraintStr(); |
| 345 | return new (Context) |
| 346 | GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs, |
| 347 | NumInputs, Names, Constraints, Exprs.data(), AsmString, |
| Jennifer Yu | b8fee67 | 2019-06-03 15:57:25 +0000 | [diff] [blame] | 348 | NumClobbers, Clobbers, NumLabels, RParenLoc); |
| Alexey Bataev | 305b6b9 | 2019-02-26 21:51:16 +0000 | [diff] [blame] | 349 | } |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos; |
| 353 | |
| 354 | for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) { |
| 355 | StringLiteral *Literal = Constraints[i]; |
| David Majnemer | b3e96f7 | 2014-12-11 01:00:48 +0000 | [diff] [blame] | 356 | assert(Literal->isAscii()); |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 357 | |
| 358 | StringRef InputName; |
| 359 | if (Names[i]) |
| 360 | InputName = Names[i]->getName(); |
| 361 | |
| 362 | TargetInfo::ConstraintInfo Info(Literal->getString(), InputName); |
| Craig Topper | 55765ca | 2015-10-21 02:34:10 +0000 | [diff] [blame] | 363 | if (!Context.getTargetInfo().validateInputConstraint(OutputConstraintInfos, |
| 364 | Info)) { |
| Alexey Bataev | 305b6b9 | 2019-02-26 21:51:16 +0000 | [diff] [blame] | 365 | targetDiag(Literal->getBeginLoc(), diag::err_asm_invalid_input_constraint) |
| 366 | << Info.getConstraintStr(); |
| 367 | return new (Context) |
| 368 | GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs, |
| 369 | NumInputs, Names, Constraints, Exprs.data(), AsmString, |
| Jennifer Yu | b8fee67 | 2019-06-03 15:57:25 +0000 | [diff] [blame] | 370 | NumClobbers, Clobbers, NumLabels, RParenLoc); |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| David Majnemer | 0f4d641 | 2014-12-29 09:30:33 +0000 | [diff] [blame] | 373 | ExprResult ER = CheckPlaceholderExpr(Exprs[i]); |
| 374 | if (ER.isInvalid()) |
| 375 | return StmtError(); |
| 376 | Exprs[i] = ER.get(); |
| 377 | |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 378 | Expr *InputExpr = Exprs[i]; |
| 379 | |
| Hans Wennborg | e9d240a | 2014-10-08 01:58:02 +0000 | [diff] [blame] | 380 | // Referring to parameters is not allowed in naked functions. |
| 381 | if (CheckNakedParmReference(InputExpr, *this)) |
| 382 | return StmtError(); |
| 383 | |
| Andrey Bokhanko | d9eab9c | 2015-08-03 10:38:10 +0000 | [diff] [blame] | 384 | // Check that the input expression is compatible with memory constraint. |
| 385 | if (Info.allowsMemory() && |
| 386 | checkExprMemoryConstraintCompat(*this, InputExpr, Info, true)) |
| 387 | return StmtError(); |
| Alexander Musman | eae29e2 | 2015-06-05 13:40:59 +0000 | [diff] [blame] | 388 | |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 389 | // Only allow void types for memory constraints. |
| 390 | if (Info.allowsMemory() && !Info.allowsRegister()) { |
| 391 | if (CheckAsmLValue(InputExpr, *this)) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 392 | return StmtError(Diag(InputExpr->getBeginLoc(), |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 393 | diag::err_asm_invalid_lvalue_in_input) |
| 394 | << Info.getConstraintStr() |
| 395 | << InputExpr->getSourceRange()); |
| Saleem Abdulrasool | a282357 | 2015-01-06 04:26:34 +0000 | [diff] [blame] | 396 | } else if (Info.requiresImmediateConstant() && !Info.allowsRegister()) { |
| Sunil Srivastava | 780e501 | 2015-07-14 18:08:50 +0000 | [diff] [blame] | 397 | if (!InputExpr->isValueDependent()) { |
| Bill Wendling | 13381fb | 2018-12-19 04:36:42 +0000 | [diff] [blame] | 398 | Expr::EvalResult EVResult; |
| Bill Wendling | ce29291 | 2019-08-06 22:41:22 +0000 | [diff] [blame] | 399 | if (InputExpr->EvaluateAsRValue(EVResult, Context, true)) { |
| 400 | // For compatibility with GCC, we also allow pointers that would be |
| 401 | // integral constant expressions if they were cast to int. |
| 402 | llvm::APSInt IntResult; |
| 403 | if (EVResult.Val.toIntegralConstant(IntResult, InputExpr->getType(), |
| 404 | Context)) |
| 405 | if (!Info.isValidAsmImmediate(IntResult)) |
| 406 | return StmtError(Diag(InputExpr->getBeginLoc(), |
| 407 | diag::err_invalid_asm_value_for_constraint) |
| 408 | << IntResult.toString(10) |
| 409 | << Info.getConstraintStr() |
| 410 | << InputExpr->getSourceRange()); |
| 411 | } |
| Sunil Srivastava | 780e501 | 2015-07-14 18:08:50 +0000 | [diff] [blame] | 412 | } |
| Saleem Abdulrasool | a282357 | 2015-01-06 04:26:34 +0000 | [diff] [blame] | 413 | |
| David Majnemer | ade4bee | 2014-07-14 16:27:53 +0000 | [diff] [blame] | 414 | } else { |
| 415 | ExprResult Result = DefaultFunctionArrayLvalueConversion(Exprs[i]); |
| 416 | if (Result.isInvalid()) |
| 417 | return StmtError(); |
| 418 | |
| 419 | Exprs[i] = Result.get(); |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | if (Info.allowsRegister()) { |
| 423 | if (InputExpr->getType()->isVoidType()) { |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 424 | return StmtError( |
| 425 | Diag(InputExpr->getBeginLoc(), diag::err_asm_invalid_type_in_input) |
| 426 | << InputExpr->getType() << Info.getConstraintStr() |
| 427 | << InputExpr->getSourceRange()); |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
| Erich Keane | 5f1f4a5 | 2020-05-13 13:24:03 -0700 | [diff] [blame] | 431 | if (InputExpr->getType()->isExtIntType()) |
| 432 | return StmtError( |
| 433 | Diag(InputExpr->getBeginLoc(), diag::err_asm_invalid_type) |
| 434 | << InputExpr->getType() << 1 /*Output*/ |
| 435 | << InputExpr->getSourceRange()); |
| 436 | |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 437 | InputConstraintInfos.push_back(Info); |
| Bill Wendling | 887b485 | 2012-11-12 06:42:51 +0000 | [diff] [blame] | 438 | |
| 439 | const Type *Ty = Exprs[i]->getType().getTypePtr(); |
| Bill Wendling | c4fc3a2 | 2013-03-25 21:09:49 +0000 | [diff] [blame] | 440 | if (Ty->isDependentType()) |
| Eric Christopher | d41010a | 2012-11-12 23:13:34 +0000 | [diff] [blame] | 441 | continue; |
| 442 | |
| Bill Wendling | c4fc3a2 | 2013-03-25 21:09:49 +0000 | [diff] [blame] | 443 | if (!Ty->isVoidType() || !Info.allowsMemory()) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 444 | if (RequireCompleteType(InputExpr->getBeginLoc(), Exprs[i]->getType(), |
| Bill Wendling | b68b757 | 2013-03-27 06:06:26 +0000 | [diff] [blame] | 445 | diag::err_dereference_incomplete_type)) |
| 446 | return StmtError(); |
| Bill Wendling | c4fc3a2 | 2013-03-25 21:09:49 +0000 | [diff] [blame] | 447 | |
| Bill Wendling | 887b485 | 2012-11-12 06:42:51 +0000 | [diff] [blame] | 448 | unsigned Size = Context.getTypeSize(Ty); |
| Craig Topper | d35bcbb | 2019-12-23 10:38:38 -0800 | [diff] [blame] | 449 | if (!Context.getTargetInfo().validateInputSize(FeatureMap, |
| 450 | Literal->getString(), Size)) |
| Alexey Bataev | 5c96c1c | 2019-02-20 17:42:57 +0000 | [diff] [blame] | 451 | return StmtResult( |
| 452 | targetDiag(InputExpr->getBeginLoc(), diag::err_asm_invalid_input_size) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 453 | << Info.getConstraintStr()); |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | // Check that the clobbers are valid. |
| 457 | for (unsigned i = 0; i != NumClobbers; i++) { |
| 458 | StringLiteral *Literal = Clobbers[i]; |
| David Majnemer | b3e96f7 | 2014-12-11 01:00:48 +0000 | [diff] [blame] | 459 | assert(Literal->isAscii()); |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 460 | |
| 461 | StringRef Clobber = Literal->getString(); |
| 462 | |
| Alexey Bataev | 305b6b9 | 2019-02-26 21:51:16 +0000 | [diff] [blame] | 463 | if (!Context.getTargetInfo().isValidClobber(Clobber)) { |
| 464 | targetDiag(Literal->getBeginLoc(), diag::err_asm_unknown_register_name) |
| 465 | << Clobber; |
| 466 | return new (Context) |
| 467 | GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs, |
| 468 | NumInputs, Names, Constraints, Exprs.data(), AsmString, |
| Jennifer Yu | b8fee67 | 2019-06-03 15:57:25 +0000 | [diff] [blame] | 469 | NumClobbers, Clobbers, NumLabels, RParenLoc); |
| Alexey Bataev | 305b6b9 | 2019-02-26 21:51:16 +0000 | [diff] [blame] | 470 | } |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 473 | GCCAsmStmt *NS = |
| 474 | new (Context) GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs, |
| Dmitri Gribenko | ea2d5f8 | 2013-05-10 01:14:26 +0000 | [diff] [blame] | 475 | NumInputs, Names, Constraints, Exprs.data(), |
| Jennifer Yu | b8fee67 | 2019-06-03 15:57:25 +0000 | [diff] [blame] | 476 | AsmString, NumClobbers, Clobbers, NumLabels, |
| 477 | RParenLoc); |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 478 | // Validate the asm string, ensuring it makes sense given the operands we |
| 479 | // have. |
| Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 480 | SmallVector<GCCAsmStmt::AsmStringPiece, 8> Pieces; |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 481 | unsigned DiagOffs; |
| Alexey Bataev | 305b6b9 | 2019-02-26 21:51:16 +0000 | [diff] [blame] | 482 | if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) { |
| 483 | targetDiag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID) |
| 484 | << AsmString->getSourceRange(); |
| 485 | return NS; |
| 486 | } |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 487 | |
| Bill Wendling | 9d1ee11 | 2012-10-25 23:28:48 +0000 | [diff] [blame] | 488 | // Validate constraints and modifiers. |
| 489 | for (unsigned i = 0, e = Pieces.size(); i != e; ++i) { |
| 490 | GCCAsmStmt::AsmStringPiece &Piece = Pieces[i]; |
| 491 | if (!Piece.isOperand()) continue; |
| 492 | |
| 493 | // Look for the correct constraint index. |
| Akira Hatanaka | 96a3601 | 2015-02-04 00:27:13 +0000 | [diff] [blame] | 494 | unsigned ConstraintIdx = Piece.getOperandNo(); |
| 495 | unsigned NumOperands = NS->getNumOutputs() + NS->getNumInputs(); |
| Bill Wendling | 50cac24 | 2020-02-24 18:32:50 -0800 | [diff] [blame] | 496 | // Labels are the last in the Exprs list. |
| 497 | if (NS->isAsmGoto() && ConstraintIdx >= NumOperands) |
| 498 | continue; |
| Akira Hatanaka | 96a3601 | 2015-02-04 00:27:13 +0000 | [diff] [blame] | 499 | // Look for the (ConstraintIdx - NumOperands + 1)th constraint with |
| 500 | // modifier '+'. |
| 501 | if (ConstraintIdx >= NumOperands) { |
| 502 | unsigned I = 0, E = NS->getNumOutputs(); |
| 503 | |
| 504 | for (unsigned Cnt = ConstraintIdx - NumOperands; I != E; ++I) |
| 505 | if (OutputConstraintInfos[I].isReadWrite() && Cnt-- == 0) { |
| 506 | ConstraintIdx = I; |
| Bill Wendling | 9d1ee11 | 2012-10-25 23:28:48 +0000 | [diff] [blame] | 507 | break; |
| Akira Hatanaka | 96a3601 | 2015-02-04 00:27:13 +0000 | [diff] [blame] | 508 | } |
| Bill Wendling | 9d1ee11 | 2012-10-25 23:28:48 +0000 | [diff] [blame] | 509 | |
| Akira Hatanaka | 96a3601 | 2015-02-04 00:27:13 +0000 | [diff] [blame] | 510 | assert(I != E && "Invalid operand number should have been caught in " |
| 511 | " AnalyzeAsmString"); |
| Bill Wendling | 9d1ee11 | 2012-10-25 23:28:48 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | // Now that we have the right indexes go ahead and check. |
| 515 | StringLiteral *Literal = Constraints[ConstraintIdx]; |
| 516 | const Type *Ty = Exprs[ConstraintIdx]->getType().getTypePtr(); |
| 517 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
| 518 | continue; |
| 519 | |
| 520 | unsigned Size = Context.getTypeSize(Ty); |
| Akira Hatanaka | 987f186 | 2014-08-22 06:05:21 +0000 | [diff] [blame] | 521 | std::string SuggestedModifier; |
| 522 | if (!Context.getTargetInfo().validateConstraintModifier( |
| 523 | Literal->getString(), Piece.getModifier(), Size, |
| 524 | SuggestedModifier)) { |
| Alexey Bataev | 5c96c1c | 2019-02-20 17:42:57 +0000 | [diff] [blame] | 525 | targetDiag(Exprs[ConstraintIdx]->getBeginLoc(), |
| 526 | diag::warn_asm_mismatched_size_modifier); |
| Akira Hatanaka | 987f186 | 2014-08-22 06:05:21 +0000 | [diff] [blame] | 527 | |
| 528 | if (!SuggestedModifier.empty()) { |
| Alexey Bataev | 5c96c1c | 2019-02-20 17:42:57 +0000 | [diff] [blame] | 529 | auto B = targetDiag(Piece.getRange().getBegin(), |
| 530 | diag::note_asm_missing_constraint_modifier) |
| Akira Hatanaka | 987f186 | 2014-08-22 06:05:21 +0000 | [diff] [blame] | 531 | << SuggestedModifier; |
| 532 | SuggestedModifier = "%" + SuggestedModifier + Piece.getString(); |
| Alexey Bataev | 5c96c1c | 2019-02-20 17:42:57 +0000 | [diff] [blame] | 533 | B << FixItHint::CreateReplacement(Piece.getRange(), SuggestedModifier); |
| Akira Hatanaka | 987f186 | 2014-08-22 06:05:21 +0000 | [diff] [blame] | 534 | } |
| 535 | } |
| Bill Wendling | 9d1ee11 | 2012-10-25 23:28:48 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 538 | // Validate tied input operands for type mismatches. |
| David Majnemer | c63fa61 | 2014-12-29 04:09:59 +0000 | [diff] [blame] | 539 | unsigned NumAlternatives = ~0U; |
| 540 | for (unsigned i = 0, e = OutputConstraintInfos.size(); i != e; ++i) { |
| 541 | TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i]; |
| 542 | StringRef ConstraintStr = Info.getConstraintStr(); |
| 543 | unsigned AltCount = ConstraintStr.count(',') + 1; |
| Alexey Bataev | 305b6b9 | 2019-02-26 21:51:16 +0000 | [diff] [blame] | 544 | if (NumAlternatives == ~0U) { |
| David Majnemer | c63fa61 | 2014-12-29 04:09:59 +0000 | [diff] [blame] | 545 | NumAlternatives = AltCount; |
| Alexey Bataev | 305b6b9 | 2019-02-26 21:51:16 +0000 | [diff] [blame] | 546 | } else if (NumAlternatives != AltCount) { |
| 547 | targetDiag(NS->getOutputExpr(i)->getBeginLoc(), |
| 548 | diag::err_asm_unexpected_constraint_alternatives) |
| 549 | << NumAlternatives << AltCount; |
| 550 | return NS; |
| 551 | } |
| David Majnemer | c63fa61 | 2014-12-29 04:09:59 +0000 | [diff] [blame] | 552 | } |
| Alexander Musman | 8e261be | 2015-09-21 14:41:00 +0000 | [diff] [blame] | 553 | SmallVector<size_t, 4> InputMatchedToOutput(OutputConstraintInfos.size(), |
| 554 | ~0U); |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 555 | for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) { |
| 556 | TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i]; |
| David Majnemer | c63fa61 | 2014-12-29 04:09:59 +0000 | [diff] [blame] | 557 | StringRef ConstraintStr = Info.getConstraintStr(); |
| 558 | unsigned AltCount = ConstraintStr.count(',') + 1; |
| Alexey Bataev | 305b6b9 | 2019-02-26 21:51:16 +0000 | [diff] [blame] | 559 | if (NumAlternatives == ~0U) { |
| David Majnemer | c63fa61 | 2014-12-29 04:09:59 +0000 | [diff] [blame] | 560 | NumAlternatives = AltCount; |
| Alexey Bataev | 305b6b9 | 2019-02-26 21:51:16 +0000 | [diff] [blame] | 561 | } else if (NumAlternatives != AltCount) { |
| 562 | targetDiag(NS->getInputExpr(i)->getBeginLoc(), |
| 563 | diag::err_asm_unexpected_constraint_alternatives) |
| 564 | << NumAlternatives << AltCount; |
| 565 | return NS; |
| 566 | } |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 567 | |
| 568 | // If this is a tied constraint, verify that the output and input have |
| 569 | // either exactly the same type, or that they are int/ptr operands with the |
| 570 | // same size (int/long, int*/long, are ok etc). |
| 571 | if (!Info.hasTiedOperand()) continue; |
| 572 | |
| 573 | unsigned TiedTo = Info.getTiedOperand(); |
| 574 | unsigned InputOpNo = i+NumOutputs; |
| 575 | Expr *OutputExpr = Exprs[TiedTo]; |
| 576 | Expr *InputExpr = Exprs[InputOpNo]; |
| 577 | |
| Alexander Musman | 8e261be | 2015-09-21 14:41:00 +0000 | [diff] [blame] | 578 | // Make sure no more than one input constraint matches each output. |
| 579 | assert(TiedTo < InputMatchedToOutput.size() && "TiedTo value out of range"); |
| 580 | if (InputMatchedToOutput[TiedTo] != ~0U) { |
| Alexey Bataev | 5c96c1c | 2019-02-20 17:42:57 +0000 | [diff] [blame] | 581 | targetDiag(NS->getInputExpr(i)->getBeginLoc(), |
| 582 | diag::err_asm_input_duplicate_match) |
| Alexander Musman | 8e261be | 2015-09-21 14:41:00 +0000 | [diff] [blame] | 583 | << TiedTo; |
| Alexey Bataev | 305b6b9 | 2019-02-26 21:51:16 +0000 | [diff] [blame] | 584 | targetDiag(NS->getInputExpr(InputMatchedToOutput[TiedTo])->getBeginLoc(), |
| 585 | diag::note_asm_input_duplicate_first) |
| 586 | << TiedTo; |
| 587 | return NS; |
| Alexander Musman | 8e261be | 2015-09-21 14:41:00 +0000 | [diff] [blame] | 588 | } |
| 589 | InputMatchedToOutput[TiedTo] = i; |
| 590 | |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 591 | if (OutputExpr->isTypeDependent() || InputExpr->isTypeDependent()) |
| 592 | continue; |
| 593 | |
| 594 | QualType InTy = InputExpr->getType(); |
| 595 | QualType OutTy = OutputExpr->getType(); |
| 596 | if (Context.hasSameType(InTy, OutTy)) |
| 597 | continue; // All types can be tied to themselves. |
| 598 | |
| 599 | // Decide if the input and output are in the same domain (integer/ptr or |
| 600 | // floating point. |
| 601 | enum AsmDomain { |
| 602 | AD_Int, AD_FP, AD_Other |
| 603 | } InputDomain, OutputDomain; |
| 604 | |
| 605 | if (InTy->isIntegerType() || InTy->isPointerType()) |
| 606 | InputDomain = AD_Int; |
| 607 | else if (InTy->isRealFloatingType()) |
| 608 | InputDomain = AD_FP; |
| 609 | else |
| 610 | InputDomain = AD_Other; |
| 611 | |
| 612 | if (OutTy->isIntegerType() || OutTy->isPointerType()) |
| 613 | OutputDomain = AD_Int; |
| 614 | else if (OutTy->isRealFloatingType()) |
| 615 | OutputDomain = AD_FP; |
| 616 | else |
| 617 | OutputDomain = AD_Other; |
| 618 | |
| 619 | // They are ok if they are the same size and in the same domain. This |
| 620 | // allows tying things like: |
| 621 | // void* to int* |
| 622 | // void* to int if they are the same size. |
| 623 | // double to long double if they are the same size. |
| 624 | // |
| 625 | uint64_t OutSize = Context.getTypeSize(OutTy); |
| 626 | uint64_t InSize = Context.getTypeSize(InTy); |
| 627 | if (OutSize == InSize && InputDomain == OutputDomain && |
| 628 | InputDomain != AD_Other) |
| 629 | continue; |
| 630 | |
| 631 | // If the smaller input/output operand is not mentioned in the asm string, |
| 632 | // then we can promote the smaller one to a larger input and the asm string |
| 633 | // won't notice. |
| 634 | bool SmallerValueMentioned = false; |
| 635 | |
| 636 | // If this is a reference to the input and if the input was the smaller |
| 637 | // one, then we have to reject this asm. |
| 638 | if (isOperandMentioned(InputOpNo, Pieces)) { |
| 639 | // This is a use in the asm string of the smaller operand. Since we |
| 640 | // codegen this by promoting to a wider value, the asm will get printed |
| 641 | // "wrong". |
| 642 | SmallerValueMentioned |= InSize < OutSize; |
| 643 | } |
| 644 | if (isOperandMentioned(TiedTo, Pieces)) { |
| 645 | // If this is a reference to the output, and if the output is the larger |
| 646 | // value, then it's ok because we'll promote the input to the larger type. |
| 647 | SmallerValueMentioned |= OutSize < InSize; |
| 648 | } |
| 649 | |
| 650 | // If the smaller value wasn't mentioned in the asm string, and if the |
| 651 | // output was a register, just extend the shorter one to the size of the |
| 652 | // larger one. |
| 653 | if (!SmallerValueMentioned && InputDomain != AD_Other && |
| 654 | OutputConstraintInfos[TiedTo].allowsRegister()) |
| 655 | continue; |
| 656 | |
| 657 | // Either both of the operands were mentioned or the smaller one was |
| 658 | // mentioned. One more special case that we'll allow: if the tied input is |
| 659 | // integer, unmentioned, and is a constant, then we'll allow truncating it |
| 660 | // down to the size of the destination. |
| 661 | if (InputDomain == AD_Int && OutputDomain == AD_Int && |
| 662 | !isOperandMentioned(InputOpNo, Pieces) && |
| 663 | InputExpr->isEvaluatable(Context)) { |
| 664 | CastKind castKind = |
| 665 | (OutTy->isBooleanType() ? CK_IntegralToBoolean : CK_IntegralCast); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 666 | InputExpr = ImpCastExprToType(InputExpr, OutTy, castKind).get(); |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 667 | Exprs[InputOpNo] = InputExpr; |
| 668 | NS->setInputExpr(i, InputExpr); |
| 669 | continue; |
| 670 | } |
| 671 | |
| Alexey Bataev | 305b6b9 | 2019-02-26 21:51:16 +0000 | [diff] [blame] | 672 | targetDiag(InputExpr->getBeginLoc(), diag::err_asm_tying_incompatible_types) |
| 673 | << InTy << OutTy << OutputExpr->getSourceRange() |
| 674 | << InputExpr->getSourceRange(); |
| 675 | return NS; |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| Marina Yatsina | c42fd03 | 2016-12-26 12:23:42 +0000 | [diff] [blame] | 678 | // Check for conflicts between clobber list and input or output lists |
| 679 | SourceLocation ConstraintLoc = |
| 680 | getClobberConflictLocation(Exprs, Constraints, Clobbers, NumClobbers, |
| Jennifer Yu | b8fee67 | 2019-06-03 15:57:25 +0000 | [diff] [blame] | 681 | NumLabels, |
| Marina Yatsina | c42fd03 | 2016-12-26 12:23:42 +0000 | [diff] [blame] | 682 | Context.getTargetInfo(), Context); |
| 683 | if (ConstraintLoc.isValid()) |
| Alexey Bataev | 305b6b9 | 2019-02-26 21:51:16 +0000 | [diff] [blame] | 684 | targetDiag(ConstraintLoc, diag::error_inoutput_conflict_with_clobber); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 685 | |
| Jennifer Yu | b8fee67 | 2019-06-03 15:57:25 +0000 | [diff] [blame] | 686 | // Check for duplicate asm operand name between input, output and label lists. |
| 687 | typedef std::pair<StringRef , Expr *> NamedOperand; |
| 688 | SmallVector<NamedOperand, 4> NamedOperandList; |
| 689 | for (unsigned i = 0, e = NumOutputs + NumInputs + NumLabels; i != e; ++i) |
| 690 | if (Names[i]) |
| 691 | NamedOperandList.emplace_back( |
| 692 | std::make_pair(Names[i]->getName(), Exprs[i])); |
| 693 | // Sort NamedOperandList. |
| 694 | std::stable_sort(NamedOperandList.begin(), NamedOperandList.end(), |
| 695 | [](const NamedOperand &LHS, const NamedOperand &RHS) { |
| 696 | return LHS.first < RHS.first; |
| 697 | }); |
| 698 | // Find adjacent duplicate operand. |
| 699 | SmallVector<NamedOperand, 4>::iterator Found = |
| 700 | std::adjacent_find(begin(NamedOperandList), end(NamedOperandList), |
| 701 | [](const NamedOperand &LHS, const NamedOperand &RHS) { |
| 702 | return LHS.first == RHS.first; |
| 703 | }); |
| 704 | if (Found != NamedOperandList.end()) { |
| 705 | Diag((Found + 1)->second->getBeginLoc(), |
| 706 | diag::error_duplicate_asm_operand_name) |
| 707 | << (Found + 1)->first; |
| 708 | Diag(Found->second->getBeginLoc(), diag::note_duplicate_asm_operand_name) |
| 709 | << Found->first; |
| 710 | return StmtError(); |
| 711 | } |
| 712 | if (NS->isAsmGoto()) |
| 713 | setFunctionHasBranchIntoScope(); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 714 | return NS; |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| Coby Tayree | 6150419 | 2017-09-29 07:02:49 +0000 | [diff] [blame] | 717 | void Sema::FillInlineAsmIdentifierInfo(Expr *Res, |
| 718 | llvm::InlineAsmIdentifierInfo &Info) { |
| 719 | QualType T = Res->getType(); |
| 720 | Expr::EvalResult Eval; |
| 721 | if (T->isFunctionType() || T->isDependentType()) |
| 722 | return Info.setLabel(Res); |
| 723 | if (Res->isRValue()) { |
| Eric Astor | 4a7aa25 | 2019-12-30 14:33:56 -0500 | [diff] [blame] | 724 | bool IsEnum = isa<clang::EnumType>(T); |
| 725 | if (DeclRefExpr *DRE = dyn_cast<clang::DeclRefExpr>(Res)) |
| 726 | if (DRE->getDecl()->getKind() == Decl::EnumConstant) |
| 727 | IsEnum = true; |
| 728 | if (IsEnum && Res->EvaluateAsRValue(Eval, Context)) |
| Coby Tayree | 6150419 | 2017-09-29 07:02:49 +0000 | [diff] [blame] | 729 | return Info.setEnum(Eval.Val.getInt().getSExtValue()); |
| Eric Astor | 4a7aa25 | 2019-12-30 14:33:56 -0500 | [diff] [blame] | 730 | |
| Coby Tayree | 6150419 | 2017-09-29 07:02:49 +0000 | [diff] [blame] | 731 | return Info.setLabel(Res); |
| Reid Kleckner | 14e96b4 | 2015-08-26 21:57:20 +0000 | [diff] [blame] | 732 | } |
| Coby Tayree | 6150419 | 2017-09-29 07:02:49 +0000 | [diff] [blame] | 733 | unsigned Size = Context.getTypeSizeInChars(T).getQuantity(); |
| 734 | unsigned Type = Size; |
| 735 | if (const auto *ATy = Context.getAsArrayType(T)) |
| 736 | Type = Context.getTypeSizeInChars(ATy->getElementType()).getQuantity(); |
| 737 | bool IsGlobalLV = false; |
| 738 | if (Res->EvaluateAsLValue(Eval, Context)) |
| 739 | IsGlobalLV = Eval.isGlobalLValue(); |
| 740 | Info.setVar(Res, IsGlobalLV, Size, Type); |
| Reid Kleckner | 14e96b4 | 2015-08-26 21:57:20 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 743 | ExprResult Sema::LookupInlineAsmIdentifier(CXXScopeSpec &SS, |
| 744 | SourceLocation TemplateKWLoc, |
| 745 | UnqualifiedId &Id, |
| John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 746 | bool IsUnevaluatedContext) { |
| Chad Rosier | ce2bcbf | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 747 | |
| John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 748 | if (IsUnevaluatedContext) |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 749 | PushExpressionEvaluationContext( |
| 750 | ExpressionEvaluationContext::UnevaluatedAbstract, |
| 751 | ReuseLambdaContextDecl); |
| John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 752 | |
| 753 | ExprResult Result = ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Id, |
| 754 | /*trailing lparen*/ false, |
| Chad Rosier | b9aff1e | 2013-05-24 18:32:55 +0000 | [diff] [blame] | 755 | /*is & operand*/ false, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 756 | /*CorrectionCandidateCallback=*/nullptr, |
| Chad Rosier | b9aff1e | 2013-05-24 18:32:55 +0000 | [diff] [blame] | 757 | /*IsInlineAsmIdentifier=*/ true); |
| John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 758 | |
| 759 | if (IsUnevaluatedContext) |
| 760 | PopExpressionEvaluationContext(); |
| 761 | |
| 762 | if (!Result.isUsable()) return Result; |
| 763 | |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 764 | Result = CheckPlaceholderExpr(Result.get()); |
| John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 765 | if (!Result.isUsable()) return Result; |
| 766 | |
| Hans Wennborg | 93dbeae | 2014-09-04 22:16:48 +0000 | [diff] [blame] | 767 | // Referring to parameters is not allowed in naked functions. |
| Hans Wennborg | e9d240a | 2014-10-08 01:58:02 +0000 | [diff] [blame] | 768 | if (CheckNakedParmReference(Result.get(), *this)) |
| 769 | return ExprError(); |
| Eric Christopher | cf94152 | 2017-07-25 19:17:32 +0000 | [diff] [blame] | 770 | |
| 771 | QualType T = Result.get()->getType(); |
| John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 772 | |
| John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 773 | if (T->isDependentType()) { |
| David Majnemer | f8b569c | 2016-01-04 23:51:15 +0000 | [diff] [blame] | 774 | return Result; |
| Chad Rosier | ce2bcbf | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 777 | // Any sort of function type is fine. |
| 778 | if (T->isFunctionType()) { |
| 779 | return Result; |
| Chad Rosier | ce2bcbf | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 782 | // Otherwise, it needs to be a complete type. |
| Eric Christopher | cf94152 | 2017-07-25 19:17:32 +0000 | [diff] [blame] | 783 | if (RequireCompleteExprType(Result.get(), diag::err_asm_incomplete_type)) { |
| John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 784 | return ExprError(); |
| 785 | } |
| 786 | |
| John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 787 | return Result; |
| Chad Rosier | 4a0054f | 2012-10-15 19:56:10 +0000 | [diff] [blame] | 788 | } |
| Chad Rosier | d997bd1 | 2012-08-22 19:18:30 +0000 | [diff] [blame] | 789 | |
| Chad Rosier | 5c56364 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 790 | bool Sema::LookupInlineAsmField(StringRef Base, StringRef Member, |
| 791 | unsigned &Offset, SourceLocation AsmLoc) { |
| 792 | Offset = 0; |
| Marina Yatsina | 71ebc69 | 2015-12-17 12:51:51 +0000 | [diff] [blame] | 793 | SmallVector<StringRef, 2> Members; |
| 794 | Member.split(Members, "."); |
| 795 | |
| Coby Tayree | 69eb696 | 2017-08-09 13:31:41 +0000 | [diff] [blame] | 796 | NamedDecl *FoundDecl = nullptr; |
| Chad Rosier | 5c56364 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 797 | |
| Coby Tayree | 69eb696 | 2017-08-09 13:31:41 +0000 | [diff] [blame] | 798 | // MS InlineAsm uses 'this' as a base |
| 799 | if (getLangOpts().CPlusPlus && Base.equals("this")) { |
| 800 | if (const Type *PT = getCurrentThisType().getTypePtrOrNull()) |
| 801 | FoundDecl = PT->getPointeeType()->getAsTagDecl(); |
| 802 | } else { |
| 803 | LookupResult BaseResult(*this, &Context.Idents.get(Base), SourceLocation(), |
| 804 | LookupOrdinaryName); |
| 805 | if (LookupName(BaseResult, getCurScope()) && BaseResult.isSingleResult()) |
| 806 | FoundDecl = BaseResult.getFoundDecl(); |
| 807 | } |
| 808 | |
| 809 | if (!FoundDecl) |
| Chad Rosier | 5c56364 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 810 | return true; |
| Coby Tayree | 69eb696 | 2017-08-09 13:31:41 +0000 | [diff] [blame] | 811 | |
| Marina Yatsina | 71ebc69 | 2015-12-17 12:51:51 +0000 | [diff] [blame] | 812 | for (StringRef NextMember : Members) { |
| Marina Yatsina | 71ebc69 | 2015-12-17 12:51:51 +0000 | [diff] [blame] | 813 | const RecordType *RT = nullptr; |
| Marina Yatsina | 71ebc69 | 2015-12-17 12:51:51 +0000 | [diff] [blame] | 814 | if (VarDecl *VD = dyn_cast<VarDecl>(FoundDecl)) |
| 815 | RT = VD->getType()->getAs<RecordType>(); |
| 816 | else if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(FoundDecl)) { |
| 817 | MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false); |
| Coby Tayree | 69eb696 | 2017-08-09 13:31:41 +0000 | [diff] [blame] | 818 | // MS InlineAsm often uses struct pointer aliases as a base |
| 819 | QualType QT = TD->getUnderlyingType(); |
| 820 | if (const auto *PT = QT->getAs<PointerType>()) |
| 821 | QT = PT->getPointeeType(); |
| 822 | RT = QT->getAs<RecordType>(); |
| Marina Yatsina | 71ebc69 | 2015-12-17 12:51:51 +0000 | [diff] [blame] | 823 | } else if (TypeDecl *TD = dyn_cast<TypeDecl>(FoundDecl)) |
| 824 | RT = TD->getTypeForDecl()->getAs<RecordType>(); |
| 825 | else if (FieldDecl *TD = dyn_cast<FieldDecl>(FoundDecl)) |
| 826 | RT = TD->getType()->getAs<RecordType>(); |
| 827 | if (!RT) |
| 828 | return true; |
| Chad Rosier | 5c56364 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 829 | |
| Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 830 | if (RequireCompleteType(AsmLoc, QualType(RT, 0), |
| 831 | diag::err_asm_incomplete_type)) |
| Marina Yatsina | 71ebc69 | 2015-12-17 12:51:51 +0000 | [diff] [blame] | 832 | return true; |
| Chad Rosier | 5c56364 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 833 | |
| Marina Yatsina | 71ebc69 | 2015-12-17 12:51:51 +0000 | [diff] [blame] | 834 | LookupResult FieldResult(*this, &Context.Idents.get(NextMember), |
| 835 | SourceLocation(), LookupMemberName); |
| Chad Rosier | 5c56364 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 836 | |
| Marina Yatsina | 71ebc69 | 2015-12-17 12:51:51 +0000 | [diff] [blame] | 837 | if (!LookupQualifiedName(FieldResult, RT->getDecl())) |
| 838 | return true; |
| 839 | |
| Marina Yatsina | d6d8b31 | 2016-03-16 09:56:58 +0000 | [diff] [blame] | 840 | if (!FieldResult.isSingleResult()) |
| 841 | return true; |
| 842 | FoundDecl = FieldResult.getFoundDecl(); |
| 843 | |
| Marina Yatsina | 71ebc69 | 2015-12-17 12:51:51 +0000 | [diff] [blame] | 844 | // FIXME: Handle IndirectFieldDecl? |
| Marina Yatsina | d6d8b31 | 2016-03-16 09:56:58 +0000 | [diff] [blame] | 845 | FieldDecl *FD = dyn_cast<FieldDecl>(FoundDecl); |
| Marina Yatsina | 71ebc69 | 2015-12-17 12:51:51 +0000 | [diff] [blame] | 846 | if (!FD) |
| 847 | return true; |
| 848 | |
| Marina Yatsina | 71ebc69 | 2015-12-17 12:51:51 +0000 | [diff] [blame] | 849 | const ASTRecordLayout &RL = Context.getASTRecordLayout(RT->getDecl()); |
| 850 | unsigned i = FD->getFieldIndex(); |
| 851 | CharUnits Result = Context.toCharUnitsFromBits(RL.getFieldOffset(i)); |
| 852 | Offset += (unsigned)Result.getQuantity(); |
| 853 | } |
| Chad Rosier | 5c56364 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 854 | |
| 855 | return false; |
| 856 | } |
| 857 | |
| Reid Kleckner | 14e96b4 | 2015-08-26 21:57:20 +0000 | [diff] [blame] | 858 | ExprResult |
| David Majnemer | 758e798 | 2016-01-05 00:08:41 +0000 | [diff] [blame] | 859 | Sema::LookupInlineAsmVarDeclField(Expr *E, StringRef Member, |
| Reid Kleckner | 14e96b4 | 2015-08-26 21:57:20 +0000 | [diff] [blame] | 860 | SourceLocation AsmLoc) { |
| Reid Kleckner | 14e96b4 | 2015-08-26 21:57:20 +0000 | [diff] [blame] | 861 | |
| David Majnemer | f8b569c | 2016-01-04 23:51:15 +0000 | [diff] [blame] | 862 | QualType T = E->getType(); |
| 863 | if (T->isDependentType()) { |
| 864 | DeclarationNameInfo NameInfo; |
| 865 | NameInfo.setLoc(AsmLoc); |
| 866 | NameInfo.setName(&Context.Idents.get(Member)); |
| 867 | return CXXDependentScopeMemberExpr::Create( |
| 868 | Context, E, T, /*IsArrow=*/false, AsmLoc, NestedNameSpecifierLoc(), |
| 869 | SourceLocation(), |
| Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 870 | /*FirstQualifierFoundInScope=*/nullptr, NameInfo, /*TemplateArgs=*/nullptr); |
| David Majnemer | f8b569c | 2016-01-04 23:51:15 +0000 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | const RecordType *RT = T->getAs<RecordType>(); |
| Reid Kleckner | 14e96b4 | 2015-08-26 21:57:20 +0000 | [diff] [blame] | 874 | // FIXME: Diagnose this as field access into a scalar type. |
| 875 | if (!RT) |
| 876 | return ExprResult(); |
| 877 | |
| 878 | LookupResult FieldResult(*this, &Context.Idents.get(Member), AsmLoc, |
| 879 | LookupMemberName); |
| 880 | |
| 881 | if (!LookupQualifiedName(FieldResult, RT->getDecl())) |
| 882 | return ExprResult(); |
| 883 | |
| 884 | // Only normal and indirect field results will work. |
| 885 | ValueDecl *FD = dyn_cast<FieldDecl>(FieldResult.getFoundDecl()); |
| 886 | if (!FD) |
| 887 | FD = dyn_cast<IndirectFieldDecl>(FieldResult.getFoundDecl()); |
| 888 | if (!FD) |
| 889 | return ExprResult(); |
| 890 | |
| Reid Kleckner | 14e96b4 | 2015-08-26 21:57:20 +0000 | [diff] [blame] | 891 | // Make an Expr to thread through OpDecl. |
| 892 | ExprResult Result = BuildMemberReferenceExpr( |
| 893 | E, E->getType(), AsmLoc, /*IsArrow=*/false, CXXScopeSpec(), |
| Aaron Ballman | 6924dcd | 2015-09-01 14:49:24 +0000 | [diff] [blame] | 894 | SourceLocation(), nullptr, FieldResult, nullptr, nullptr); |
| Reid Kleckner | 14e96b4 | 2015-08-26 21:57:20 +0000 | [diff] [blame] | 895 | |
| 896 | return Result; |
| 897 | } |
| 898 | |
| Chad Rosier | b261a50 | 2012-09-13 00:06:55 +0000 | [diff] [blame] | 899 | StmtResult Sema::ActOnMSAsmStmt(SourceLocation AsmLoc, SourceLocation LBraceLoc, |
| John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 900 | ArrayRef<Token> AsmToks, |
| 901 | StringRef AsmString, |
| 902 | unsigned NumOutputs, unsigned NumInputs, |
| 903 | ArrayRef<StringRef> Constraints, |
| 904 | ArrayRef<StringRef> Clobbers, |
| 905 | ArrayRef<Expr*> Exprs, |
| 906 | SourceLocation EndLoc) { |
| 907 | bool IsSimple = (NumOutputs != 0 || NumInputs != 0); |
| Reid Kleckner | 87a3180 | 2018-03-12 21:43:02 +0000 | [diff] [blame] | 908 | setFunctionHasBranchProtectedScope(); |
| Erich Keane | 5f1f4a5 | 2020-05-13 13:24:03 -0700 | [diff] [blame] | 909 | |
| 910 | for (uint64_t I = 0; I < NumOutputs + NumInputs; ++I) { |
| 911 | if (Exprs[I]->getType()->isExtIntType()) |
| 912 | return StmtError( |
| 913 | Diag(Exprs[I]->getBeginLoc(), diag::err_asm_invalid_type) |
| 914 | << Exprs[I]->getType() << (I < NumOutputs) |
| 915 | << Exprs[I]->getSourceRange()); |
| 916 | } |
| 917 | |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 918 | MSAsmStmt *NS = |
| Chad Rosier | ce2bcbf | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 919 | new (Context) MSAsmStmt(Context, AsmLoc, LBraceLoc, IsSimple, |
| 920 | /*IsVolatile*/ true, AsmToks, NumOutputs, NumInputs, |
| John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 921 | Constraints, Exprs, AsmString, |
| 922 | Clobbers, EndLoc); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 923 | return NS; |
| Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 924 | } |
| Ehsan Akhgari | 3109758 | 2014-09-22 02:21:54 +0000 | [diff] [blame] | 925 | |
| 926 | LabelDecl *Sema::GetOrCreateMSAsmLabel(StringRef ExternalLabelName, |
| 927 | SourceLocation Location, |
| 928 | bool AlwaysCreate) { |
| 929 | LabelDecl* Label = LookupOrCreateLabel(PP.getIdentifierInfo(ExternalLabelName), |
| 930 | Location); |
| 931 | |
| Ehsan Akhgari | 4292443 | 2014-10-08 17:28:34 +0000 | [diff] [blame] | 932 | if (Label->isMSAsmLabel()) { |
| 933 | // If we have previously created this label implicitly, mark it as used. |
| 934 | Label->markUsed(Context); |
| 935 | } else { |
| Ehsan Akhgari | 3109758 | 2014-09-22 02:21:54 +0000 | [diff] [blame] | 936 | // Otherwise, insert it, but only resolve it if we have seen the label itself. |
| 937 | std::string InternalName; |
| 938 | llvm::raw_string_ostream OS(InternalName); |
| Reid Kleckner | 36c201a | 2016-12-07 00:17:18 +0000 | [diff] [blame] | 939 | // Create an internal name for the label. The name should not be a valid |
| 940 | // mangled name, and should be unique. We use a dot to make the name an |
| 941 | // invalid mangled name. We use LLVM's inline asm ${:uid} escape so that a |
| 942 | // unique label is generated each time this blob is emitted, even after |
| 943 | // inlining or LTO. |
| Reid Kleckner | fec0f32 | 2016-11-29 00:39:37 +0000 | [diff] [blame] | 944 | OS << "__MSASMLABEL_.${:uid}__"; |
| Reid Kleckner | 08ebbce | 2016-11-28 20:52:19 +0000 | [diff] [blame] | 945 | for (char C : ExternalLabelName) { |
| 946 | OS << C; |
| 947 | // We escape '$' in asm strings by replacing it with "$$" |
| 948 | if (C == '$') |
| Marina Yatsina | afb72f3 | 2015-12-29 08:49:34 +0000 | [diff] [blame] | 949 | OS << '$'; |
| Marina Yatsina | afb72f3 | 2015-12-29 08:49:34 +0000 | [diff] [blame] | 950 | } |
| Ehsan Akhgari | 3109758 | 2014-09-22 02:21:54 +0000 | [diff] [blame] | 951 | Label->setMSAsmLabel(OS.str()); |
| 952 | } |
| 953 | if (AlwaysCreate) { |
| 954 | // The label might have been created implicitly from a previously encountered |
| 955 | // goto statement. So, for both newly created and looked up labels, we mark |
| 956 | // them as resolved. |
| 957 | Label->setMSAsmLabelResolved(); |
| 958 | } |
| 959 | // Adjust their location for being able to generate accurate diagnostics. |
| 960 | Label->setLocation(Location); |
| 961 | |
| 962 | return Label; |
| 963 | } |