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