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