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