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