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 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for inline asm statements. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Weiming Zhao | 71ac240 | 2015-02-03 22:35:58 +0000 | [diff] [blame] | 14 | #include "clang/AST/ExprCXX.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, |
| 213 | const TargetInfo &Target, ASTContext &Cont) { |
| 214 | llvm::StringSet<> InOutVars; |
| 215 | // Collect all the input and output registers from the extended asm |
Marina Yatsina | c5cf7a8 | 2016-12-26 13:16:40 +0000 | [diff] [blame] | 216 | // statement in order to check for conflicts with the clobber list |
| 217 | for (unsigned int i = 0; i < Exprs.size(); ++i) { |
Marina Yatsina | c42fd03 | 2016-12-26 12:23:42 +0000 | [diff] [blame] | 218 | StringRef Constraint = Constraints[i]->getString(); |
| 219 | StringRef InOutReg = Target.getConstraintRegister( |
| 220 | Constraint, extractRegisterName(Exprs[i], Target)); |
| 221 | if (InOutReg != "") |
| 222 | InOutVars.insert(InOutReg); |
| 223 | } |
| 224 | // Check for each item in the clobber list if it conflicts with the input |
| 225 | // or output |
| 226 | for (int i = 0; i < NumClobbers; ++i) { |
| 227 | StringRef Clobber = Clobbers[i]->getString(); |
| 228 | // We only check registers, therefore we don't check cc and memory |
| 229 | // clobbers |
| 230 | if (Clobber == "cc" || Clobber == "memory") |
| 231 | continue; |
| 232 | Clobber = Target.getNormalizedGCCRegisterName(Clobber, true); |
| 233 | // Go over the output's registers we collected |
| 234 | if (InOutVars.count(Clobber)) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 235 | return Clobbers[i]->getBeginLoc(); |
Marina Yatsina | c42fd03 | 2016-12-26 12:23:42 +0000 | [diff] [blame] | 236 | } |
| 237 | return SourceLocation(); |
| 238 | } |
| 239 | |
Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 240 | StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple, |
| 241 | bool IsVolatile, unsigned NumOutputs, |
| 242 | unsigned NumInputs, IdentifierInfo **Names, |
Dmitri Gribenko | ea2d5f8 | 2013-05-10 01:14:26 +0000 | [diff] [blame] | 243 | MultiExprArg constraints, MultiExprArg Exprs, |
Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 244 | Expr *asmString, MultiExprArg clobbers, |
| 245 | SourceLocation RParenLoc) { |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 246 | unsigned NumClobbers = clobbers.size(); |
| 247 | StringLiteral **Constraints = |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 248 | reinterpret_cast<StringLiteral**>(constraints.data()); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 249 | StringLiteral *AsmString = cast<StringLiteral>(asmString); |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 250 | StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.data()); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 251 | |
| 252 | SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos; |
| 253 | |
| 254 | // The parser verifies that there is a string literal here. |
David Majnemer | b3e96f7 | 2014-12-11 01:00:48 +0000 | [diff] [blame] | 255 | assert(AsmString->isAscii()); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 256 | |
Artem Belevich | 5ef02c2 | 2015-08-27 19:54:21 +0000 | [diff] [blame] | 257 | // If we're compiling CUDA file and function attributes indicate that it's not |
| 258 | // for this compilation side, skip all the checks. |
| 259 | if (!DeclAttrsMatchCUDAMode(getLangOpts(), getCurFunctionDecl())) { |
| 260 | GCCAsmStmt *NS = new (Context) GCCAsmStmt( |
| 261 | Context, AsmLoc, IsSimple, IsVolatile, NumOutputs, NumInputs, Names, |
| 262 | Constraints, Exprs.data(), AsmString, NumClobbers, Clobbers, RParenLoc); |
| 263 | return NS; |
| 264 | } |
Artem Belevich | 5196fe7 | 2015-03-19 18:40:25 +0000 | [diff] [blame] | 265 | |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 266 | for (unsigned i = 0; i != NumOutputs; i++) { |
| 267 | StringLiteral *Literal = Constraints[i]; |
David Majnemer | b3e96f7 | 2014-12-11 01:00:48 +0000 | [diff] [blame] | 268 | assert(Literal->isAscii()); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 269 | |
| 270 | StringRef OutputName; |
| 271 | if (Names[i]) |
| 272 | OutputName = Names[i]->getName(); |
| 273 | |
| 274 | TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName); |
Artem Belevich | 5ef02c2 | 2015-08-27 19:54:21 +0000 | [diff] [blame] | 275 | if (!Context.getTargetInfo().validateOutputConstraint(Info)) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 276 | return StmtError( |
| 277 | Diag(Literal->getBeginLoc(), diag::err_asm_invalid_output_constraint) |
| 278 | << Info.getConstraintStr()); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 279 | |
David Majnemer | 0f4d641 | 2014-12-29 09:30:33 +0000 | [diff] [blame] | 280 | ExprResult ER = CheckPlaceholderExpr(Exprs[i]); |
| 281 | if (ER.isInvalid()) |
| 282 | return StmtError(); |
| 283 | Exprs[i] = ER.get(); |
| 284 | |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 285 | // Check that the output exprs are valid lvalues. |
| 286 | Expr *OutputExpr = Exprs[i]; |
Bill Wendling | c4fc3a2 | 2013-03-25 21:09:49 +0000 | [diff] [blame] | 287 | |
Hans Wennborg | e9d240a | 2014-10-08 01:58:02 +0000 | [diff] [blame] | 288 | // Referring to parameters is not allowed in naked functions. |
| 289 | if (CheckNakedParmReference(OutputExpr, *this)) |
| 290 | return StmtError(); |
| 291 | |
Andrey Bokhanko | d9eab9c | 2015-08-03 10:38:10 +0000 | [diff] [blame] | 292 | // Check that the output expression is compatible with memory constraint. |
| 293 | if (Info.allowsMemory() && |
| 294 | checkExprMemoryConstraintCompat(*this, OutputExpr, Info, false)) |
| 295 | return StmtError(); |
Alexander Musman | eae29e2 | 2015-06-05 13:40:59 +0000 | [diff] [blame] | 296 | |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 297 | OutputConstraintInfos.push_back(Info); |
Akira Hatanaka | 974131e | 2014-09-18 18:17:18 +0000 | [diff] [blame] | 298 | |
David Majnemer | 0f4d641 | 2014-12-29 09:30:33 +0000 | [diff] [blame] | 299 | // If this is dependent, just continue. |
| 300 | if (OutputExpr->isTypeDependent()) |
Akira Hatanaka | 974131e | 2014-09-18 18:17:18 +0000 | [diff] [blame] | 301 | continue; |
| 302 | |
David Majnemer | 0f4d641 | 2014-12-29 09:30:33 +0000 | [diff] [blame] | 303 | Expr::isModifiableLvalueResult IsLV = |
| 304 | OutputExpr->isModifiableLvalue(Context, /*Loc=*/nullptr); |
| 305 | switch (IsLV) { |
| 306 | case Expr::MLV_Valid: |
| 307 | // Cool, this is an lvalue. |
| 308 | break; |
David Majnemer | 04b7841 | 2014-12-29 10:29:53 +0000 | [diff] [blame] | 309 | case Expr::MLV_ArrayType: |
| 310 | // This is OK too. |
| 311 | break; |
David Majnemer | 0f4d641 | 2014-12-29 09:30:33 +0000 | [diff] [blame] | 312 | case Expr::MLV_LValueCast: { |
| 313 | const Expr *LVal = OutputExpr->IgnoreParenNoopCasts(Context); |
Aleksei Sidorin | 55365e4 | 2018-10-20 22:49:23 +0000 | [diff] [blame] | 314 | emitAndFixInvalidAsmCastLValue(LVal, OutputExpr, *this); |
David Majnemer | 0f4d641 | 2014-12-29 09:30:33 +0000 | [diff] [blame] | 315 | // Accept, even if we emitted an error diagnostic. |
| 316 | break; |
| 317 | } |
| 318 | case Expr::MLV_IncompleteType: |
| 319 | case Expr::MLV_IncompleteVoidType: |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 320 | if (RequireCompleteType(OutputExpr->getBeginLoc(), Exprs[i]->getType(), |
David Majnemer | 0f4d641 | 2014-12-29 09:30:33 +0000 | [diff] [blame] | 321 | diag::err_dereference_incomplete_type)) |
| 322 | return StmtError(); |
Galina Kistanova | 3339911 | 2017-06-03 06:35:06 +0000 | [diff] [blame] | 323 | LLVM_FALLTHROUGH; |
David Majnemer | 0f4d641 | 2014-12-29 09:30:33 +0000 | [diff] [blame] | 324 | default: |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 325 | return StmtError(Diag(OutputExpr->getBeginLoc(), |
David Majnemer | 0f4d641 | 2014-12-29 09:30:33 +0000 | [diff] [blame] | 326 | diag::err_asm_invalid_lvalue_in_output) |
| 327 | << OutputExpr->getSourceRange()); |
| 328 | } |
| 329 | |
| 330 | unsigned Size = Context.getTypeSize(OutputExpr->getType()); |
Akira Hatanaka | 974131e | 2014-09-18 18:17:18 +0000 | [diff] [blame] | 331 | if (!Context.getTargetInfo().validateOutputSize(Literal->getString(), |
| 332 | Size)) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 333 | return StmtError( |
| 334 | Diag(OutputExpr->getBeginLoc(), diag::err_asm_invalid_output_size) |
| 335 | << Info.getConstraintStr()); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos; |
| 339 | |
| 340 | for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) { |
| 341 | StringLiteral *Literal = Constraints[i]; |
David Majnemer | b3e96f7 | 2014-12-11 01:00:48 +0000 | [diff] [blame] | 342 | assert(Literal->isAscii()); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 343 | |
| 344 | StringRef InputName; |
| 345 | if (Names[i]) |
| 346 | InputName = Names[i]->getName(); |
| 347 | |
| 348 | TargetInfo::ConstraintInfo Info(Literal->getString(), InputName); |
Craig Topper | 55765ca | 2015-10-21 02:34:10 +0000 | [diff] [blame] | 349 | if (!Context.getTargetInfo().validateInputConstraint(OutputConstraintInfos, |
| 350 | Info)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 351 | return StmtError( |
| 352 | Diag(Literal->getBeginLoc(), diag::err_asm_invalid_input_constraint) |
| 353 | << Info.getConstraintStr()); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 354 | } |
| 355 | |
David Majnemer | 0f4d641 | 2014-12-29 09:30:33 +0000 | [diff] [blame] | 356 | ExprResult ER = CheckPlaceholderExpr(Exprs[i]); |
| 357 | if (ER.isInvalid()) |
| 358 | return StmtError(); |
| 359 | Exprs[i] = ER.get(); |
| 360 | |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 361 | Expr *InputExpr = Exprs[i]; |
| 362 | |
Hans Wennborg | e9d240a | 2014-10-08 01:58:02 +0000 | [diff] [blame] | 363 | // Referring to parameters is not allowed in naked functions. |
| 364 | if (CheckNakedParmReference(InputExpr, *this)) |
| 365 | return StmtError(); |
| 366 | |
Andrey Bokhanko | d9eab9c | 2015-08-03 10:38:10 +0000 | [diff] [blame] | 367 | // Check that the input expression is compatible with memory constraint. |
| 368 | if (Info.allowsMemory() && |
| 369 | checkExprMemoryConstraintCompat(*this, InputExpr, Info, true)) |
| 370 | return StmtError(); |
Alexander Musman | eae29e2 | 2015-06-05 13:40:59 +0000 | [diff] [blame] | 371 | |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 372 | // Only allow void types for memory constraints. |
| 373 | if (Info.allowsMemory() && !Info.allowsRegister()) { |
| 374 | if (CheckAsmLValue(InputExpr, *this)) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 375 | return StmtError(Diag(InputExpr->getBeginLoc(), |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 376 | diag::err_asm_invalid_lvalue_in_input) |
| 377 | << Info.getConstraintStr() |
| 378 | << InputExpr->getSourceRange()); |
Saleem Abdulrasool | a282357 | 2015-01-06 04:26:34 +0000 | [diff] [blame] | 379 | } else if (Info.requiresImmediateConstant() && !Info.allowsRegister()) { |
Sunil Srivastava | 780e501 | 2015-07-14 18:08:50 +0000 | [diff] [blame] | 380 | if (!InputExpr->isValueDependent()) { |
Bill Wendling | 91549ed | 2018-11-20 23:24:16 +0000 | [diff] [blame] | 381 | Expr::EvalResult EVResult; |
| 382 | if (!InputExpr->EvaluateAsInt(EVResult, Context)) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 383 | return StmtError( |
| 384 | Diag(InputExpr->getBeginLoc(), diag::err_asm_immediate_expected) |
| 385 | << Info.getConstraintStr() << InputExpr->getSourceRange()); |
Bill Wendling | 91549ed | 2018-11-20 23:24:16 +0000 | [diff] [blame] | 386 | llvm::APSInt Result = EVResult.Val.getInt(); |
Alexey Bataev | 91e5860 | 2015-07-20 12:08:00 +0000 | [diff] [blame] | 387 | if (!Info.isValidAsmImmediate(Result)) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 388 | return StmtError(Diag(InputExpr->getBeginLoc(), |
Sunil Srivastava | 780e501 | 2015-07-14 18:08:50 +0000 | [diff] [blame] | 389 | diag::err_invalid_asm_value_for_constraint) |
| 390 | << Result.toString(10) << Info.getConstraintStr() |
| 391 | << InputExpr->getSourceRange()); |
| 392 | } |
Saleem Abdulrasool | a282357 | 2015-01-06 04:26:34 +0000 | [diff] [blame] | 393 | |
David Majnemer | ade4bee | 2014-07-14 16:27:53 +0000 | [diff] [blame] | 394 | } else { |
| 395 | ExprResult Result = DefaultFunctionArrayLvalueConversion(Exprs[i]); |
| 396 | if (Result.isInvalid()) |
| 397 | return StmtError(); |
| 398 | |
| 399 | Exprs[i] = Result.get(); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | if (Info.allowsRegister()) { |
| 403 | if (InputExpr->getType()->isVoidType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 404 | return StmtError( |
| 405 | Diag(InputExpr->getBeginLoc(), diag::err_asm_invalid_type_in_input) |
| 406 | << InputExpr->getType() << Info.getConstraintStr() |
| 407 | << InputExpr->getSourceRange()); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 411 | InputConstraintInfos.push_back(Info); |
Bill Wendling | 887b485 | 2012-11-12 06:42:51 +0000 | [diff] [blame] | 412 | |
| 413 | const Type *Ty = Exprs[i]->getType().getTypePtr(); |
Bill Wendling | c4fc3a2 | 2013-03-25 21:09:49 +0000 | [diff] [blame] | 414 | if (Ty->isDependentType()) |
Eric Christopher | d41010a | 2012-11-12 23:13:34 +0000 | [diff] [blame] | 415 | continue; |
| 416 | |
Bill Wendling | c4fc3a2 | 2013-03-25 21:09:49 +0000 | [diff] [blame] | 417 | if (!Ty->isVoidType() || !Info.allowsMemory()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 418 | if (RequireCompleteType(InputExpr->getBeginLoc(), Exprs[i]->getType(), |
Bill Wendling | b68b757 | 2013-03-27 06:06:26 +0000 | [diff] [blame] | 419 | diag::err_dereference_incomplete_type)) |
| 420 | return StmtError(); |
Bill Wendling | c4fc3a2 | 2013-03-25 21:09:49 +0000 | [diff] [blame] | 421 | |
Bill Wendling | 887b485 | 2012-11-12 06:42:51 +0000 | [diff] [blame] | 422 | unsigned Size = Context.getTypeSize(Ty); |
| 423 | if (!Context.getTargetInfo().validateInputSize(Literal->getString(), |
| 424 | Size)) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 425 | return StmtError( |
| 426 | Diag(InputExpr->getBeginLoc(), diag::err_asm_invalid_input_size) |
| 427 | << Info.getConstraintStr()); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | // Check that the clobbers are valid. |
| 431 | for (unsigned i = 0; i != NumClobbers; i++) { |
| 432 | StringLiteral *Literal = Clobbers[i]; |
David Majnemer | b3e96f7 | 2014-12-11 01:00:48 +0000 | [diff] [blame] | 433 | assert(Literal->isAscii()); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 434 | |
| 435 | StringRef Clobber = Literal->getString(); |
| 436 | |
| 437 | if (!Context.getTargetInfo().isValidClobber(Clobber)) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 438 | return StmtError( |
| 439 | Diag(Literal->getBeginLoc(), diag::err_asm_unknown_register_name) |
| 440 | << Clobber); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 443 | GCCAsmStmt *NS = |
| 444 | new (Context) GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs, |
Dmitri Gribenko | ea2d5f8 | 2013-05-10 01:14:26 +0000 | [diff] [blame] | 445 | NumInputs, Names, Constraints, Exprs.data(), |
| 446 | AsmString, NumClobbers, Clobbers, RParenLoc); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 447 | // Validate the asm string, ensuring it makes sense given the operands we |
| 448 | // have. |
Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 449 | SmallVector<GCCAsmStmt::AsmStringPiece, 8> Pieces; |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 450 | unsigned DiagOffs; |
| 451 | if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) { |
| 452 | Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID) |
| 453 | << AsmString->getSourceRange(); |
| 454 | return StmtError(); |
| 455 | } |
| 456 | |
Bill Wendling | 9d1ee11 | 2012-10-25 23:28:48 +0000 | [diff] [blame] | 457 | // Validate constraints and modifiers. |
| 458 | for (unsigned i = 0, e = Pieces.size(); i != e; ++i) { |
| 459 | GCCAsmStmt::AsmStringPiece &Piece = Pieces[i]; |
| 460 | if (!Piece.isOperand()) continue; |
| 461 | |
| 462 | // Look for the correct constraint index. |
Akira Hatanaka | 96a3601 | 2015-02-04 00:27:13 +0000 | [diff] [blame] | 463 | unsigned ConstraintIdx = Piece.getOperandNo(); |
| 464 | unsigned NumOperands = NS->getNumOutputs() + NS->getNumInputs(); |
Bill Wendling | 9d1ee11 | 2012-10-25 23:28:48 +0000 | [diff] [blame] | 465 | |
Akira Hatanaka | 96a3601 | 2015-02-04 00:27:13 +0000 | [diff] [blame] | 466 | // Look for the (ConstraintIdx - NumOperands + 1)th constraint with |
| 467 | // modifier '+'. |
| 468 | if (ConstraintIdx >= NumOperands) { |
| 469 | unsigned I = 0, E = NS->getNumOutputs(); |
| 470 | |
| 471 | for (unsigned Cnt = ConstraintIdx - NumOperands; I != E; ++I) |
| 472 | if (OutputConstraintInfos[I].isReadWrite() && Cnt-- == 0) { |
| 473 | ConstraintIdx = I; |
Bill Wendling | 9d1ee11 | 2012-10-25 23:28:48 +0000 | [diff] [blame] | 474 | break; |
Akira Hatanaka | 96a3601 | 2015-02-04 00:27:13 +0000 | [diff] [blame] | 475 | } |
Bill Wendling | 9d1ee11 | 2012-10-25 23:28:48 +0000 | [diff] [blame] | 476 | |
Akira Hatanaka | 96a3601 | 2015-02-04 00:27:13 +0000 | [diff] [blame] | 477 | assert(I != E && "Invalid operand number should have been caught in " |
| 478 | " AnalyzeAsmString"); |
Bill Wendling | 9d1ee11 | 2012-10-25 23:28:48 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | // Now that we have the right indexes go ahead and check. |
| 482 | StringLiteral *Literal = Constraints[ConstraintIdx]; |
| 483 | const Type *Ty = Exprs[ConstraintIdx]->getType().getTypePtr(); |
| 484 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
| 485 | continue; |
| 486 | |
| 487 | unsigned Size = Context.getTypeSize(Ty); |
Akira Hatanaka | 987f186 | 2014-08-22 06:05:21 +0000 | [diff] [blame] | 488 | std::string SuggestedModifier; |
| 489 | if (!Context.getTargetInfo().validateConstraintModifier( |
| 490 | Literal->getString(), Piece.getModifier(), Size, |
| 491 | SuggestedModifier)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 492 | Diag(Exprs[ConstraintIdx]->getBeginLoc(), |
Bill Wendling | 9d1ee11 | 2012-10-25 23:28:48 +0000 | [diff] [blame] | 493 | diag::warn_asm_mismatched_size_modifier); |
Akira Hatanaka | 987f186 | 2014-08-22 06:05:21 +0000 | [diff] [blame] | 494 | |
| 495 | if (!SuggestedModifier.empty()) { |
| 496 | auto B = Diag(Piece.getRange().getBegin(), |
| 497 | diag::note_asm_missing_constraint_modifier) |
| 498 | << SuggestedModifier; |
| 499 | SuggestedModifier = "%" + SuggestedModifier + Piece.getString(); |
| 500 | B.AddFixItHint(FixItHint::CreateReplacement(Piece.getRange(), |
| 501 | SuggestedModifier)); |
| 502 | } |
| 503 | } |
Bill Wendling | 9d1ee11 | 2012-10-25 23:28:48 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 506 | // Validate tied input operands for type mismatches. |
David Majnemer | c63fa61 | 2014-12-29 04:09:59 +0000 | [diff] [blame] | 507 | unsigned NumAlternatives = ~0U; |
| 508 | for (unsigned i = 0, e = OutputConstraintInfos.size(); i != e; ++i) { |
| 509 | TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i]; |
| 510 | StringRef ConstraintStr = Info.getConstraintStr(); |
| 511 | unsigned AltCount = ConstraintStr.count(',') + 1; |
| 512 | if (NumAlternatives == ~0U) |
| 513 | NumAlternatives = AltCount; |
| 514 | else if (NumAlternatives != AltCount) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 515 | return StmtError(Diag(NS->getOutputExpr(i)->getBeginLoc(), |
David Majnemer | c63fa61 | 2014-12-29 04:09:59 +0000 | [diff] [blame] | 516 | diag::err_asm_unexpected_constraint_alternatives) |
| 517 | << NumAlternatives << AltCount); |
| 518 | } |
Alexander Musman | 8e261be | 2015-09-21 14:41:00 +0000 | [diff] [blame] | 519 | SmallVector<size_t, 4> InputMatchedToOutput(OutputConstraintInfos.size(), |
| 520 | ~0U); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 521 | for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) { |
| 522 | TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i]; |
David Majnemer | c63fa61 | 2014-12-29 04:09:59 +0000 | [diff] [blame] | 523 | StringRef ConstraintStr = Info.getConstraintStr(); |
| 524 | unsigned AltCount = ConstraintStr.count(',') + 1; |
| 525 | if (NumAlternatives == ~0U) |
| 526 | NumAlternatives = AltCount; |
| 527 | else if (NumAlternatives != AltCount) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 528 | return StmtError(Diag(NS->getInputExpr(i)->getBeginLoc(), |
David Majnemer | c63fa61 | 2014-12-29 04:09:59 +0000 | [diff] [blame] | 529 | diag::err_asm_unexpected_constraint_alternatives) |
| 530 | << NumAlternatives << AltCount); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 531 | |
| 532 | // If this is a tied constraint, verify that the output and input have |
| 533 | // either exactly the same type, or that they are int/ptr operands with the |
| 534 | // same size (int/long, int*/long, are ok etc). |
| 535 | if (!Info.hasTiedOperand()) continue; |
| 536 | |
| 537 | unsigned TiedTo = Info.getTiedOperand(); |
| 538 | unsigned InputOpNo = i+NumOutputs; |
| 539 | Expr *OutputExpr = Exprs[TiedTo]; |
| 540 | Expr *InputExpr = Exprs[InputOpNo]; |
| 541 | |
Alexander Musman | 8e261be | 2015-09-21 14:41:00 +0000 | [diff] [blame] | 542 | // Make sure no more than one input constraint matches each output. |
| 543 | assert(TiedTo < InputMatchedToOutput.size() && "TiedTo value out of range"); |
| 544 | if (InputMatchedToOutput[TiedTo] != ~0U) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 545 | Diag(NS->getInputExpr(i)->getBeginLoc(), |
Alexander Musman | 8e261be | 2015-09-21 14:41:00 +0000 | [diff] [blame] | 546 | diag::err_asm_input_duplicate_match) |
| 547 | << TiedTo; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 548 | Diag(NS->getInputExpr(InputMatchedToOutput[TiedTo])->getBeginLoc(), |
Alexander Musman | 8e261be | 2015-09-21 14:41:00 +0000 | [diff] [blame] | 549 | diag::note_asm_input_duplicate_first) |
| 550 | << TiedTo; |
| 551 | return StmtError(); |
| 552 | } |
| 553 | InputMatchedToOutput[TiedTo] = i; |
| 554 | |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 555 | if (OutputExpr->isTypeDependent() || InputExpr->isTypeDependent()) |
| 556 | continue; |
| 557 | |
| 558 | QualType InTy = InputExpr->getType(); |
| 559 | QualType OutTy = OutputExpr->getType(); |
| 560 | if (Context.hasSameType(InTy, OutTy)) |
| 561 | continue; // All types can be tied to themselves. |
| 562 | |
| 563 | // Decide if the input and output are in the same domain (integer/ptr or |
| 564 | // floating point. |
| 565 | enum AsmDomain { |
| 566 | AD_Int, AD_FP, AD_Other |
| 567 | } InputDomain, OutputDomain; |
| 568 | |
| 569 | if (InTy->isIntegerType() || InTy->isPointerType()) |
| 570 | InputDomain = AD_Int; |
| 571 | else if (InTy->isRealFloatingType()) |
| 572 | InputDomain = AD_FP; |
| 573 | else |
| 574 | InputDomain = AD_Other; |
| 575 | |
| 576 | if (OutTy->isIntegerType() || OutTy->isPointerType()) |
| 577 | OutputDomain = AD_Int; |
| 578 | else if (OutTy->isRealFloatingType()) |
| 579 | OutputDomain = AD_FP; |
| 580 | else |
| 581 | OutputDomain = AD_Other; |
| 582 | |
| 583 | // They are ok if they are the same size and in the same domain. This |
| 584 | // allows tying things like: |
| 585 | // void* to int* |
| 586 | // void* to int if they are the same size. |
| 587 | // double to long double if they are the same size. |
| 588 | // |
| 589 | uint64_t OutSize = Context.getTypeSize(OutTy); |
| 590 | uint64_t InSize = Context.getTypeSize(InTy); |
| 591 | if (OutSize == InSize && InputDomain == OutputDomain && |
| 592 | InputDomain != AD_Other) |
| 593 | continue; |
| 594 | |
| 595 | // If the smaller input/output operand is not mentioned in the asm string, |
| 596 | // then we can promote the smaller one to a larger input and the asm string |
| 597 | // won't notice. |
| 598 | bool SmallerValueMentioned = false; |
| 599 | |
| 600 | // If this is a reference to the input and if the input was the smaller |
| 601 | // one, then we have to reject this asm. |
| 602 | if (isOperandMentioned(InputOpNo, Pieces)) { |
| 603 | // This is a use in the asm string of the smaller operand. Since we |
| 604 | // codegen this by promoting to a wider value, the asm will get printed |
| 605 | // "wrong". |
| 606 | SmallerValueMentioned |= InSize < OutSize; |
| 607 | } |
| 608 | if (isOperandMentioned(TiedTo, Pieces)) { |
| 609 | // If this is a reference to the output, and if the output is the larger |
| 610 | // value, then it's ok because we'll promote the input to the larger type. |
| 611 | SmallerValueMentioned |= OutSize < InSize; |
| 612 | } |
| 613 | |
| 614 | // If the smaller value wasn't mentioned in the asm string, and if the |
| 615 | // output was a register, just extend the shorter one to the size of the |
| 616 | // larger one. |
| 617 | if (!SmallerValueMentioned && InputDomain != AD_Other && |
| 618 | OutputConstraintInfos[TiedTo].allowsRegister()) |
| 619 | continue; |
| 620 | |
| 621 | // Either both of the operands were mentioned or the smaller one was |
| 622 | // mentioned. One more special case that we'll allow: if the tied input is |
| 623 | // integer, unmentioned, and is a constant, then we'll allow truncating it |
| 624 | // down to the size of the destination. |
| 625 | if (InputDomain == AD_Int && OutputDomain == AD_Int && |
| 626 | !isOperandMentioned(InputOpNo, Pieces) && |
| 627 | InputExpr->isEvaluatable(Context)) { |
| 628 | CastKind castKind = |
| 629 | (OutTy->isBooleanType() ? CK_IntegralToBoolean : CK_IntegralCast); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 630 | InputExpr = ImpCastExprToType(InputExpr, OutTy, castKind).get(); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 631 | Exprs[InputOpNo] = InputExpr; |
| 632 | NS->setInputExpr(i, InputExpr); |
| 633 | continue; |
| 634 | } |
| 635 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 636 | Diag(InputExpr->getBeginLoc(), diag::err_asm_tying_incompatible_types) |
| 637 | << InTy << OutTy << OutputExpr->getSourceRange() |
| 638 | << InputExpr->getSourceRange(); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 639 | return StmtError(); |
| 640 | } |
| 641 | |
Marina Yatsina | c42fd03 | 2016-12-26 12:23:42 +0000 | [diff] [blame] | 642 | // Check for conflicts between clobber list and input or output lists |
| 643 | SourceLocation ConstraintLoc = |
| 644 | getClobberConflictLocation(Exprs, Constraints, Clobbers, NumClobbers, |
| 645 | Context.getTargetInfo(), Context); |
| 646 | if (ConstraintLoc.isValid()) |
| 647 | return Diag(ConstraintLoc, diag::error_inoutput_conflict_with_clobber); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 648 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 649 | return NS; |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Coby Tayree | 6150419 | 2017-09-29 07:02:49 +0000 | [diff] [blame] | 652 | void Sema::FillInlineAsmIdentifierInfo(Expr *Res, |
| 653 | llvm::InlineAsmIdentifierInfo &Info) { |
| 654 | QualType T = Res->getType(); |
| 655 | Expr::EvalResult Eval; |
| 656 | if (T->isFunctionType() || T->isDependentType()) |
| 657 | return Info.setLabel(Res); |
| 658 | if (Res->isRValue()) { |
| 659 | if (isa<clang::EnumType>(T) && Res->EvaluateAsRValue(Eval, Context)) |
| 660 | return Info.setEnum(Eval.Val.getInt().getSExtValue()); |
| 661 | return Info.setLabel(Res); |
Reid Kleckner | 14e96b4 | 2015-08-26 21:57:20 +0000 | [diff] [blame] | 662 | } |
Coby Tayree | 6150419 | 2017-09-29 07:02:49 +0000 | [diff] [blame] | 663 | unsigned Size = Context.getTypeSizeInChars(T).getQuantity(); |
| 664 | unsigned Type = Size; |
| 665 | if (const auto *ATy = Context.getAsArrayType(T)) |
| 666 | Type = Context.getTypeSizeInChars(ATy->getElementType()).getQuantity(); |
| 667 | bool IsGlobalLV = false; |
| 668 | if (Res->EvaluateAsLValue(Eval, Context)) |
| 669 | IsGlobalLV = Eval.isGlobalLValue(); |
| 670 | Info.setVar(Res, IsGlobalLV, Size, Type); |
Reid Kleckner | 14e96b4 | 2015-08-26 21:57:20 +0000 | [diff] [blame] | 671 | } |
| 672 | |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 673 | ExprResult Sema::LookupInlineAsmIdentifier(CXXScopeSpec &SS, |
| 674 | SourceLocation TemplateKWLoc, |
| 675 | UnqualifiedId &Id, |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 676 | bool IsUnevaluatedContext) { |
Chad Rosier | ce2bcbf | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 677 | |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 678 | if (IsUnevaluatedContext) |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 679 | PushExpressionEvaluationContext( |
| 680 | ExpressionEvaluationContext::UnevaluatedAbstract, |
| 681 | ReuseLambdaContextDecl); |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 682 | |
| 683 | ExprResult Result = ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Id, |
| 684 | /*trailing lparen*/ false, |
Chad Rosier | b9aff1e | 2013-05-24 18:32:55 +0000 | [diff] [blame] | 685 | /*is & operand*/ false, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 686 | /*CorrectionCandidateCallback=*/nullptr, |
Chad Rosier | b9aff1e | 2013-05-24 18:32:55 +0000 | [diff] [blame] | 687 | /*IsInlineAsmIdentifier=*/ true); |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 688 | |
| 689 | if (IsUnevaluatedContext) |
| 690 | PopExpressionEvaluationContext(); |
| 691 | |
| 692 | if (!Result.isUsable()) return Result; |
| 693 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 694 | Result = CheckPlaceholderExpr(Result.get()); |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 695 | if (!Result.isUsable()) return Result; |
| 696 | |
Hans Wennborg | 93dbeae | 2014-09-04 22:16:48 +0000 | [diff] [blame] | 697 | // Referring to parameters is not allowed in naked functions. |
Hans Wennborg | e9d240a | 2014-10-08 01:58:02 +0000 | [diff] [blame] | 698 | if (CheckNakedParmReference(Result.get(), *this)) |
| 699 | return ExprError(); |
Eric Christopher | cf94152 | 2017-07-25 19:17:32 +0000 | [diff] [blame] | 700 | |
| 701 | QualType T = Result.get()->getType(); |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 702 | |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 703 | if (T->isDependentType()) { |
David Majnemer | f8b569c | 2016-01-04 23:51:15 +0000 | [diff] [blame] | 704 | return Result; |
Chad Rosier | ce2bcbf | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 705 | } |
| 706 | |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 707 | // Any sort of function type is fine. |
| 708 | if (T->isFunctionType()) { |
| 709 | return Result; |
Chad Rosier | ce2bcbf | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 710 | } |
| 711 | |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 712 | // Otherwise, it needs to be a complete type. |
Eric Christopher | cf94152 | 2017-07-25 19:17:32 +0000 | [diff] [blame] | 713 | if (RequireCompleteExprType(Result.get(), diag::err_asm_incomplete_type)) { |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 714 | return ExprError(); |
| 715 | } |
| 716 | |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 717 | return Result; |
Chad Rosier | 4a0054f | 2012-10-15 19:56:10 +0000 | [diff] [blame] | 718 | } |
Chad Rosier | d997bd1 | 2012-08-22 19:18:30 +0000 | [diff] [blame] | 719 | |
Chad Rosier | 5c56364 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 720 | bool Sema::LookupInlineAsmField(StringRef Base, StringRef Member, |
| 721 | unsigned &Offset, SourceLocation AsmLoc) { |
| 722 | Offset = 0; |
Marina Yatsina | 71ebc69 | 2015-12-17 12:51:51 +0000 | [diff] [blame] | 723 | SmallVector<StringRef, 2> Members; |
| 724 | Member.split(Members, "."); |
| 725 | |
Coby Tayree | 69eb696 | 2017-08-09 13:31:41 +0000 | [diff] [blame] | 726 | NamedDecl *FoundDecl = nullptr; |
Chad Rosier | 5c56364 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 727 | |
Coby Tayree | 69eb696 | 2017-08-09 13:31:41 +0000 | [diff] [blame] | 728 | // MS InlineAsm uses 'this' as a base |
| 729 | if (getLangOpts().CPlusPlus && Base.equals("this")) { |
| 730 | if (const Type *PT = getCurrentThisType().getTypePtrOrNull()) |
| 731 | FoundDecl = PT->getPointeeType()->getAsTagDecl(); |
| 732 | } else { |
| 733 | LookupResult BaseResult(*this, &Context.Idents.get(Base), SourceLocation(), |
| 734 | LookupOrdinaryName); |
| 735 | if (LookupName(BaseResult, getCurScope()) && BaseResult.isSingleResult()) |
| 736 | FoundDecl = BaseResult.getFoundDecl(); |
| 737 | } |
| 738 | |
| 739 | if (!FoundDecl) |
Chad Rosier | 5c56364 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 740 | return true; |
Coby Tayree | 69eb696 | 2017-08-09 13:31:41 +0000 | [diff] [blame] | 741 | |
Marina Yatsina | 71ebc69 | 2015-12-17 12:51:51 +0000 | [diff] [blame] | 742 | for (StringRef NextMember : Members) { |
Marina Yatsina | 71ebc69 | 2015-12-17 12:51:51 +0000 | [diff] [blame] | 743 | const RecordType *RT = nullptr; |
Marina Yatsina | 71ebc69 | 2015-12-17 12:51:51 +0000 | [diff] [blame] | 744 | if (VarDecl *VD = dyn_cast<VarDecl>(FoundDecl)) |
| 745 | RT = VD->getType()->getAs<RecordType>(); |
| 746 | else if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(FoundDecl)) { |
| 747 | MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false); |
Coby Tayree | 69eb696 | 2017-08-09 13:31:41 +0000 | [diff] [blame] | 748 | // MS InlineAsm often uses struct pointer aliases as a base |
| 749 | QualType QT = TD->getUnderlyingType(); |
| 750 | if (const auto *PT = QT->getAs<PointerType>()) |
| 751 | QT = PT->getPointeeType(); |
| 752 | RT = QT->getAs<RecordType>(); |
Marina Yatsina | 71ebc69 | 2015-12-17 12:51:51 +0000 | [diff] [blame] | 753 | } else if (TypeDecl *TD = dyn_cast<TypeDecl>(FoundDecl)) |
| 754 | RT = TD->getTypeForDecl()->getAs<RecordType>(); |
| 755 | else if (FieldDecl *TD = dyn_cast<FieldDecl>(FoundDecl)) |
| 756 | RT = TD->getType()->getAs<RecordType>(); |
| 757 | if (!RT) |
| 758 | return true; |
Chad Rosier | 5c56364 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 759 | |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 760 | if (RequireCompleteType(AsmLoc, QualType(RT, 0), |
| 761 | diag::err_asm_incomplete_type)) |
Marina Yatsina | 71ebc69 | 2015-12-17 12:51:51 +0000 | [diff] [blame] | 762 | return true; |
Chad Rosier | 5c56364 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 763 | |
Marina Yatsina | 71ebc69 | 2015-12-17 12:51:51 +0000 | [diff] [blame] | 764 | LookupResult FieldResult(*this, &Context.Idents.get(NextMember), |
| 765 | SourceLocation(), LookupMemberName); |
Chad Rosier | 5c56364 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 766 | |
Marina Yatsina | 71ebc69 | 2015-12-17 12:51:51 +0000 | [diff] [blame] | 767 | if (!LookupQualifiedName(FieldResult, RT->getDecl())) |
| 768 | return true; |
| 769 | |
Marina Yatsina | d6d8b31 | 2016-03-16 09:56:58 +0000 | [diff] [blame] | 770 | if (!FieldResult.isSingleResult()) |
| 771 | return true; |
| 772 | FoundDecl = FieldResult.getFoundDecl(); |
| 773 | |
Marina Yatsina | 71ebc69 | 2015-12-17 12:51:51 +0000 | [diff] [blame] | 774 | // FIXME: Handle IndirectFieldDecl? |
Marina Yatsina | d6d8b31 | 2016-03-16 09:56:58 +0000 | [diff] [blame] | 775 | FieldDecl *FD = dyn_cast<FieldDecl>(FoundDecl); |
Marina Yatsina | 71ebc69 | 2015-12-17 12:51:51 +0000 | [diff] [blame] | 776 | if (!FD) |
| 777 | return true; |
| 778 | |
Marina Yatsina | 71ebc69 | 2015-12-17 12:51:51 +0000 | [diff] [blame] | 779 | const ASTRecordLayout &RL = Context.getASTRecordLayout(RT->getDecl()); |
| 780 | unsigned i = FD->getFieldIndex(); |
| 781 | CharUnits Result = Context.toCharUnitsFromBits(RL.getFieldOffset(i)); |
| 782 | Offset += (unsigned)Result.getQuantity(); |
| 783 | } |
Chad Rosier | 5c56364 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 784 | |
| 785 | return false; |
| 786 | } |
| 787 | |
Reid Kleckner | 14e96b4 | 2015-08-26 21:57:20 +0000 | [diff] [blame] | 788 | ExprResult |
David Majnemer | 758e798 | 2016-01-05 00:08:41 +0000 | [diff] [blame] | 789 | Sema::LookupInlineAsmVarDeclField(Expr *E, StringRef Member, |
Reid Kleckner | 14e96b4 | 2015-08-26 21:57:20 +0000 | [diff] [blame] | 790 | SourceLocation AsmLoc) { |
Reid Kleckner | 14e96b4 | 2015-08-26 21:57:20 +0000 | [diff] [blame] | 791 | |
David Majnemer | f8b569c | 2016-01-04 23:51:15 +0000 | [diff] [blame] | 792 | QualType T = E->getType(); |
| 793 | if (T->isDependentType()) { |
| 794 | DeclarationNameInfo NameInfo; |
| 795 | NameInfo.setLoc(AsmLoc); |
| 796 | NameInfo.setName(&Context.Idents.get(Member)); |
| 797 | return CXXDependentScopeMemberExpr::Create( |
| 798 | Context, E, T, /*IsArrow=*/false, AsmLoc, NestedNameSpecifierLoc(), |
| 799 | SourceLocation(), |
| 800 | /*FirstQualifierInScope=*/nullptr, NameInfo, /*TemplateArgs=*/nullptr); |
| 801 | } |
| 802 | |
| 803 | const RecordType *RT = T->getAs<RecordType>(); |
Reid Kleckner | 14e96b4 | 2015-08-26 21:57:20 +0000 | [diff] [blame] | 804 | // FIXME: Diagnose this as field access into a scalar type. |
| 805 | if (!RT) |
| 806 | return ExprResult(); |
| 807 | |
| 808 | LookupResult FieldResult(*this, &Context.Idents.get(Member), AsmLoc, |
| 809 | LookupMemberName); |
| 810 | |
| 811 | if (!LookupQualifiedName(FieldResult, RT->getDecl())) |
| 812 | return ExprResult(); |
| 813 | |
| 814 | // Only normal and indirect field results will work. |
| 815 | ValueDecl *FD = dyn_cast<FieldDecl>(FieldResult.getFoundDecl()); |
| 816 | if (!FD) |
| 817 | FD = dyn_cast<IndirectFieldDecl>(FieldResult.getFoundDecl()); |
| 818 | if (!FD) |
| 819 | return ExprResult(); |
| 820 | |
Reid Kleckner | 14e96b4 | 2015-08-26 21:57:20 +0000 | [diff] [blame] | 821 | // Make an Expr to thread through OpDecl. |
| 822 | ExprResult Result = BuildMemberReferenceExpr( |
| 823 | E, E->getType(), AsmLoc, /*IsArrow=*/false, CXXScopeSpec(), |
Aaron Ballman | 6924dcd | 2015-09-01 14:49:24 +0000 | [diff] [blame] | 824 | SourceLocation(), nullptr, FieldResult, nullptr, nullptr); |
Reid Kleckner | 14e96b4 | 2015-08-26 21:57:20 +0000 | [diff] [blame] | 825 | |
| 826 | return Result; |
| 827 | } |
| 828 | |
Chad Rosier | b261a50 | 2012-09-13 00:06:55 +0000 | [diff] [blame] | 829 | StmtResult Sema::ActOnMSAsmStmt(SourceLocation AsmLoc, SourceLocation LBraceLoc, |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 830 | ArrayRef<Token> AsmToks, |
| 831 | StringRef AsmString, |
| 832 | unsigned NumOutputs, unsigned NumInputs, |
| 833 | ArrayRef<StringRef> Constraints, |
| 834 | ArrayRef<StringRef> Clobbers, |
| 835 | ArrayRef<Expr*> Exprs, |
| 836 | SourceLocation EndLoc) { |
| 837 | bool IsSimple = (NumOutputs != 0 || NumInputs != 0); |
Reid Kleckner | 87a3180 | 2018-03-12 21:43:02 +0000 | [diff] [blame] | 838 | setFunctionHasBranchProtectedScope(); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 839 | MSAsmStmt *NS = |
Chad Rosier | ce2bcbf | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 840 | new (Context) MSAsmStmt(Context, AsmLoc, LBraceLoc, IsSimple, |
| 841 | /*IsVolatile*/ true, AsmToks, NumOutputs, NumInputs, |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 842 | Constraints, Exprs, AsmString, |
| 843 | Clobbers, EndLoc); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 844 | return NS; |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 845 | } |
Ehsan Akhgari | 3109758 | 2014-09-22 02:21:54 +0000 | [diff] [blame] | 846 | |
| 847 | LabelDecl *Sema::GetOrCreateMSAsmLabel(StringRef ExternalLabelName, |
| 848 | SourceLocation Location, |
| 849 | bool AlwaysCreate) { |
| 850 | LabelDecl* Label = LookupOrCreateLabel(PP.getIdentifierInfo(ExternalLabelName), |
| 851 | Location); |
| 852 | |
Ehsan Akhgari | 4292443 | 2014-10-08 17:28:34 +0000 | [diff] [blame] | 853 | if (Label->isMSAsmLabel()) { |
| 854 | // If we have previously created this label implicitly, mark it as used. |
| 855 | Label->markUsed(Context); |
| 856 | } else { |
Ehsan Akhgari | 3109758 | 2014-09-22 02:21:54 +0000 | [diff] [blame] | 857 | // Otherwise, insert it, but only resolve it if we have seen the label itself. |
| 858 | std::string InternalName; |
| 859 | llvm::raw_string_ostream OS(InternalName); |
Reid Kleckner | 36c201a | 2016-12-07 00:17:18 +0000 | [diff] [blame] | 860 | // Create an internal name for the label. The name should not be a valid |
| 861 | // mangled name, and should be unique. We use a dot to make the name an |
| 862 | // invalid mangled name. We use LLVM's inline asm ${:uid} escape so that a |
| 863 | // unique label is generated each time this blob is emitted, even after |
| 864 | // inlining or LTO. |
Reid Kleckner | fec0f32 | 2016-11-29 00:39:37 +0000 | [diff] [blame] | 865 | OS << "__MSASMLABEL_.${:uid}__"; |
Reid Kleckner | 08ebbce | 2016-11-28 20:52:19 +0000 | [diff] [blame] | 866 | for (char C : ExternalLabelName) { |
| 867 | OS << C; |
| 868 | // We escape '$' in asm strings by replacing it with "$$" |
| 869 | if (C == '$') |
Marina Yatsina | afb72f3 | 2015-12-29 08:49:34 +0000 | [diff] [blame] | 870 | OS << '$'; |
Marina Yatsina | afb72f3 | 2015-12-29 08:49:34 +0000 | [diff] [blame] | 871 | } |
Ehsan Akhgari | 3109758 | 2014-09-22 02:21:54 +0000 | [diff] [blame] | 872 | Label->setMSAsmLabel(OS.str()); |
| 873 | } |
| 874 | if (AlwaysCreate) { |
| 875 | // The label might have been created implicitly from a previously encountered |
| 876 | // goto statement. So, for both newly created and looked up labels, we mark |
| 877 | // them as resolved. |
| 878 | Label->setMSAsmLabelResolved(); |
| 879 | } |
| 880 | // Adjust their location for being able to generate accurate diagnostics. |
| 881 | Label->setLocation(Location); |
| 882 | |
| 883 | return Label; |
| 884 | } |