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" |
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" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Initialization.h" |
| 19 | #include "clang/Sema/Lookup.h" |
| 20 | #include "clang/Sema/Scope.h" |
| 21 | #include "clang/Sema/ScopeInfo.h" |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/ArrayRef.h" |
| 23 | #include "llvm/ADT/BitVector.h" |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace sema; |
| 26 | |
| 27 | /// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently |
| 28 | /// ignore "noop" casts in places where an lvalue is required by an inline asm. |
| 29 | /// We emulate this behavior when -fheinous-gnu-extensions is specified, but |
| 30 | /// provide a strong guidance to not use it. |
| 31 | /// |
| 32 | /// This method checks to see if the argument is an acceptable l-value and |
| 33 | /// returns false if it is a case we can handle. |
| 34 | static bool CheckAsmLValue(const Expr *E, Sema &S) { |
| 35 | // Type dependent expressions will be checked during instantiation. |
| 36 | if (E->isTypeDependent()) |
| 37 | return false; |
| 38 | |
| 39 | if (E->isLValue()) |
| 40 | return false; // Cool, this is an lvalue. |
| 41 | |
| 42 | // Okay, this is not an lvalue, but perhaps it is the result of a cast that we |
| 43 | // are supposed to allow. |
| 44 | const Expr *E2 = E->IgnoreParenNoopCasts(S.Context); |
| 45 | if (E != E2 && E2->isLValue()) { |
| 46 | if (!S.getLangOpts().HeinousExtensions) |
| 47 | S.Diag(E2->getLocStart(), diag::err_invalid_asm_cast_lvalue) |
| 48 | << E->getSourceRange(); |
| 49 | else |
| 50 | S.Diag(E2->getLocStart(), diag::warn_invalid_asm_cast_lvalue) |
| 51 | << E->getSourceRange(); |
| 52 | // Accept, even if we emitted an error diagnostic. |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | // None of the above, just randomly invalid non-lvalue. |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | /// isOperandMentioned - Return true if the specified operand # is mentioned |
| 61 | /// anywhere in the decomposed asm string. |
| 62 | static bool isOperandMentioned(unsigned OpNo, |
Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 63 | ArrayRef<GCCAsmStmt::AsmStringPiece> AsmStrPieces) { |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 64 | for (unsigned p = 0, e = AsmStrPieces.size(); p != e; ++p) { |
Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 65 | const GCCAsmStmt::AsmStringPiece &Piece = AsmStrPieces[p]; |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 66 | if (!Piece.isOperand()) continue; |
| 67 | |
| 68 | // If this is a reference to the input and if the input was the smaller |
| 69 | // one, then we have to reject this asm. |
| 70 | if (Piece.getOperandNo() == OpNo) |
| 71 | return true; |
| 72 | } |
| 73 | return false; |
| 74 | } |
| 75 | |
Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 76 | StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple, |
| 77 | bool IsVolatile, unsigned NumOutputs, |
| 78 | unsigned NumInputs, IdentifierInfo **Names, |
Dmitri Gribenko | ea2d5f8 | 2013-05-10 01:14:26 +0000 | [diff] [blame] | 79 | MultiExprArg constraints, MultiExprArg Exprs, |
Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 80 | Expr *asmString, MultiExprArg clobbers, |
| 81 | SourceLocation RParenLoc) { |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 82 | unsigned NumClobbers = clobbers.size(); |
| 83 | StringLiteral **Constraints = |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 84 | reinterpret_cast<StringLiteral**>(constraints.data()); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 85 | StringLiteral *AsmString = cast<StringLiteral>(asmString); |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 86 | StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.data()); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 87 | |
| 88 | SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos; |
| 89 | |
| 90 | // The parser verifies that there is a string literal here. |
| 91 | if (!AsmString->isAscii()) |
| 92 | return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character) |
| 93 | << AsmString->getSourceRange()); |
| 94 | |
| 95 | for (unsigned i = 0; i != NumOutputs; i++) { |
| 96 | StringLiteral *Literal = Constraints[i]; |
| 97 | if (!Literal->isAscii()) |
| 98 | return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) |
| 99 | << Literal->getSourceRange()); |
| 100 | |
| 101 | StringRef OutputName; |
| 102 | if (Names[i]) |
| 103 | OutputName = Names[i]->getName(); |
| 104 | |
| 105 | TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName); |
| 106 | if (!Context.getTargetInfo().validateOutputConstraint(Info)) |
| 107 | return StmtError(Diag(Literal->getLocStart(), |
| 108 | diag::err_asm_invalid_output_constraint) |
| 109 | << Info.getConstraintStr()); |
| 110 | |
| 111 | // Check that the output exprs are valid lvalues. |
| 112 | Expr *OutputExpr = Exprs[i]; |
Bill Wendling | c4fc3a2 | 2013-03-25 21:09:49 +0000 | [diff] [blame] | 113 | if (CheckAsmLValue(OutputExpr, *this)) |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 114 | return StmtError(Diag(OutputExpr->getLocStart(), |
Bill Wendling | c4fc3a2 | 2013-03-25 21:09:49 +0000 | [diff] [blame] | 115 | diag::err_asm_invalid_lvalue_in_output) |
| 116 | << OutputExpr->getSourceRange()); |
| 117 | |
Bill Wendling | b68b757 | 2013-03-27 06:06:26 +0000 | [diff] [blame] | 118 | if (RequireCompleteType(OutputExpr->getLocStart(), Exprs[i]->getType(), |
| 119 | diag::err_dereference_incomplete_type)) |
| 120 | return StmtError(); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 121 | |
| 122 | OutputConstraintInfos.push_back(Info); |
| 123 | } |
| 124 | |
| 125 | SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos; |
| 126 | |
| 127 | for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) { |
| 128 | StringLiteral *Literal = Constraints[i]; |
| 129 | if (!Literal->isAscii()) |
| 130 | return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) |
| 131 | << Literal->getSourceRange()); |
| 132 | |
| 133 | StringRef InputName; |
| 134 | if (Names[i]) |
| 135 | InputName = Names[i]->getName(); |
| 136 | |
| 137 | TargetInfo::ConstraintInfo Info(Literal->getString(), InputName); |
| 138 | if (!Context.getTargetInfo().validateInputConstraint(OutputConstraintInfos.data(), |
| 139 | NumOutputs, Info)) { |
| 140 | return StmtError(Diag(Literal->getLocStart(), |
| 141 | diag::err_asm_invalid_input_constraint) |
| 142 | << Info.getConstraintStr()); |
| 143 | } |
| 144 | |
| 145 | Expr *InputExpr = Exprs[i]; |
| 146 | |
| 147 | // Only allow void types for memory constraints. |
| 148 | if (Info.allowsMemory() && !Info.allowsRegister()) { |
| 149 | if (CheckAsmLValue(InputExpr, *this)) |
| 150 | return StmtError(Diag(InputExpr->getLocStart(), |
| 151 | diag::err_asm_invalid_lvalue_in_input) |
| 152 | << Info.getConstraintStr() |
| 153 | << InputExpr->getSourceRange()); |
| 154 | } |
| 155 | |
| 156 | if (Info.allowsRegister()) { |
| 157 | if (InputExpr->getType()->isVoidType()) { |
| 158 | return StmtError(Diag(InputExpr->getLocStart(), |
| 159 | diag::err_asm_invalid_type_in_input) |
| 160 | << InputExpr->getType() << Info.getConstraintStr() |
| 161 | << InputExpr->getSourceRange()); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | ExprResult Result = DefaultFunctionArrayLvalueConversion(Exprs[i]); |
| 166 | if (Result.isInvalid()) |
| 167 | return StmtError(); |
| 168 | |
| 169 | Exprs[i] = Result.take(); |
| 170 | InputConstraintInfos.push_back(Info); |
Bill Wendling | 887b485 | 2012-11-12 06:42:51 +0000 | [diff] [blame] | 171 | |
| 172 | const Type *Ty = Exprs[i]->getType().getTypePtr(); |
Bill Wendling | c4fc3a2 | 2013-03-25 21:09:49 +0000 | [diff] [blame] | 173 | if (Ty->isDependentType()) |
Eric Christopher | d41010a | 2012-11-12 23:13:34 +0000 | [diff] [blame] | 174 | continue; |
| 175 | |
Bill Wendling | c4fc3a2 | 2013-03-25 21:09:49 +0000 | [diff] [blame] | 176 | if (!Ty->isVoidType() || !Info.allowsMemory()) |
Bill Wendling | b68b757 | 2013-03-27 06:06:26 +0000 | [diff] [blame] | 177 | if (RequireCompleteType(InputExpr->getLocStart(), Exprs[i]->getType(), |
| 178 | diag::err_dereference_incomplete_type)) |
| 179 | return StmtError(); |
Bill Wendling | c4fc3a2 | 2013-03-25 21:09:49 +0000 | [diff] [blame] | 180 | |
Bill Wendling | 887b485 | 2012-11-12 06:42:51 +0000 | [diff] [blame] | 181 | unsigned Size = Context.getTypeSize(Ty); |
| 182 | if (!Context.getTargetInfo().validateInputSize(Literal->getString(), |
| 183 | Size)) |
| 184 | return StmtError(Diag(InputExpr->getLocStart(), |
| 185 | diag::err_asm_invalid_input_size) |
| 186 | << Info.getConstraintStr()); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // Check that the clobbers are valid. |
| 190 | for (unsigned i = 0; i != NumClobbers; i++) { |
| 191 | StringLiteral *Literal = Clobbers[i]; |
| 192 | if (!Literal->isAscii()) |
| 193 | return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) |
| 194 | << Literal->getSourceRange()); |
| 195 | |
| 196 | StringRef Clobber = Literal->getString(); |
| 197 | |
| 198 | if (!Context.getTargetInfo().isValidClobber(Clobber)) |
| 199 | return StmtError(Diag(Literal->getLocStart(), |
| 200 | diag::err_asm_unknown_register_name) << Clobber); |
| 201 | } |
| 202 | |
Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 203 | GCCAsmStmt *NS = |
| 204 | new (Context) GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs, |
Dmitri Gribenko | ea2d5f8 | 2013-05-10 01:14:26 +0000 | [diff] [blame] | 205 | NumInputs, Names, Constraints, Exprs.data(), |
| 206 | AsmString, NumClobbers, Clobbers, RParenLoc); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 207 | // Validate the asm string, ensuring it makes sense given the operands we |
| 208 | // have. |
Chad Rosier | de70e0e | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 209 | SmallVector<GCCAsmStmt::AsmStringPiece, 8> Pieces; |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 210 | unsigned DiagOffs; |
| 211 | if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) { |
| 212 | Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID) |
| 213 | << AsmString->getSourceRange(); |
| 214 | return StmtError(); |
| 215 | } |
| 216 | |
Bill Wendling | 9d1ee11 | 2012-10-25 23:28:48 +0000 | [diff] [blame] | 217 | // Validate constraints and modifiers. |
| 218 | for (unsigned i = 0, e = Pieces.size(); i != e; ++i) { |
| 219 | GCCAsmStmt::AsmStringPiece &Piece = Pieces[i]; |
| 220 | if (!Piece.isOperand()) continue; |
| 221 | |
| 222 | // Look for the correct constraint index. |
| 223 | unsigned Idx = 0; |
| 224 | unsigned ConstraintIdx = 0; |
| 225 | for (unsigned i = 0, e = NS->getNumOutputs(); i != e; ++i, ++ConstraintIdx) { |
| 226 | TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i]; |
| 227 | if (Idx == Piece.getOperandNo()) |
| 228 | break; |
| 229 | ++Idx; |
| 230 | |
| 231 | if (Info.isReadWrite()) { |
| 232 | if (Idx == Piece.getOperandNo()) |
| 233 | break; |
| 234 | ++Idx; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | for (unsigned i = 0, e = NS->getNumInputs(); i != e; ++i, ++ConstraintIdx) { |
| 239 | TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i]; |
| 240 | if (Idx == Piece.getOperandNo()) |
| 241 | break; |
| 242 | ++Idx; |
| 243 | |
| 244 | if (Info.isReadWrite()) { |
| 245 | if (Idx == Piece.getOperandNo()) |
| 246 | break; |
| 247 | ++Idx; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | // Now that we have the right indexes go ahead and check. |
| 252 | StringLiteral *Literal = Constraints[ConstraintIdx]; |
| 253 | const Type *Ty = Exprs[ConstraintIdx]->getType().getTypePtr(); |
| 254 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
| 255 | continue; |
| 256 | |
| 257 | unsigned Size = Context.getTypeSize(Ty); |
| 258 | if (!Context.getTargetInfo() |
| 259 | .validateConstraintModifier(Literal->getString(), Piece.getModifier(), |
| 260 | Size)) |
| 261 | Diag(Exprs[ConstraintIdx]->getLocStart(), |
| 262 | diag::warn_asm_mismatched_size_modifier); |
| 263 | } |
| 264 | |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 265 | // Validate tied input operands for type mismatches. |
| 266 | for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) { |
| 267 | TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i]; |
| 268 | |
| 269 | // If this is a tied constraint, verify that the output and input have |
| 270 | // either exactly the same type, or that they are int/ptr operands with the |
| 271 | // same size (int/long, int*/long, are ok etc). |
| 272 | if (!Info.hasTiedOperand()) continue; |
| 273 | |
| 274 | unsigned TiedTo = Info.getTiedOperand(); |
| 275 | unsigned InputOpNo = i+NumOutputs; |
| 276 | Expr *OutputExpr = Exprs[TiedTo]; |
| 277 | Expr *InputExpr = Exprs[InputOpNo]; |
| 278 | |
| 279 | if (OutputExpr->isTypeDependent() || InputExpr->isTypeDependent()) |
| 280 | continue; |
| 281 | |
| 282 | QualType InTy = InputExpr->getType(); |
| 283 | QualType OutTy = OutputExpr->getType(); |
| 284 | if (Context.hasSameType(InTy, OutTy)) |
| 285 | continue; // All types can be tied to themselves. |
| 286 | |
| 287 | // Decide if the input and output are in the same domain (integer/ptr or |
| 288 | // floating point. |
| 289 | enum AsmDomain { |
| 290 | AD_Int, AD_FP, AD_Other |
| 291 | } InputDomain, OutputDomain; |
| 292 | |
| 293 | if (InTy->isIntegerType() || InTy->isPointerType()) |
| 294 | InputDomain = AD_Int; |
| 295 | else if (InTy->isRealFloatingType()) |
| 296 | InputDomain = AD_FP; |
| 297 | else |
| 298 | InputDomain = AD_Other; |
| 299 | |
| 300 | if (OutTy->isIntegerType() || OutTy->isPointerType()) |
| 301 | OutputDomain = AD_Int; |
| 302 | else if (OutTy->isRealFloatingType()) |
| 303 | OutputDomain = AD_FP; |
| 304 | else |
| 305 | OutputDomain = AD_Other; |
| 306 | |
| 307 | // They are ok if they are the same size and in the same domain. This |
| 308 | // allows tying things like: |
| 309 | // void* to int* |
| 310 | // void* to int if they are the same size. |
| 311 | // double to long double if they are the same size. |
| 312 | // |
| 313 | uint64_t OutSize = Context.getTypeSize(OutTy); |
| 314 | uint64_t InSize = Context.getTypeSize(InTy); |
| 315 | if (OutSize == InSize && InputDomain == OutputDomain && |
| 316 | InputDomain != AD_Other) |
| 317 | continue; |
| 318 | |
| 319 | // If the smaller input/output operand is not mentioned in the asm string, |
| 320 | // then we can promote the smaller one to a larger input and the asm string |
| 321 | // won't notice. |
| 322 | bool SmallerValueMentioned = false; |
| 323 | |
| 324 | // If this is a reference to the input and if the input was the smaller |
| 325 | // one, then we have to reject this asm. |
| 326 | if (isOperandMentioned(InputOpNo, Pieces)) { |
| 327 | // This is a use in the asm string of the smaller operand. Since we |
| 328 | // codegen this by promoting to a wider value, the asm will get printed |
| 329 | // "wrong". |
| 330 | SmallerValueMentioned |= InSize < OutSize; |
| 331 | } |
| 332 | if (isOperandMentioned(TiedTo, Pieces)) { |
| 333 | // If this is a reference to the output, and if the output is the larger |
| 334 | // value, then it's ok because we'll promote the input to the larger type. |
| 335 | SmallerValueMentioned |= OutSize < InSize; |
| 336 | } |
| 337 | |
| 338 | // If the smaller value wasn't mentioned in the asm string, and if the |
| 339 | // output was a register, just extend the shorter one to the size of the |
| 340 | // larger one. |
| 341 | if (!SmallerValueMentioned && InputDomain != AD_Other && |
| 342 | OutputConstraintInfos[TiedTo].allowsRegister()) |
| 343 | continue; |
| 344 | |
| 345 | // Either both of the operands were mentioned or the smaller one was |
| 346 | // mentioned. One more special case that we'll allow: if the tied input is |
| 347 | // integer, unmentioned, and is a constant, then we'll allow truncating it |
| 348 | // down to the size of the destination. |
| 349 | if (InputDomain == AD_Int && OutputDomain == AD_Int && |
| 350 | !isOperandMentioned(InputOpNo, Pieces) && |
| 351 | InputExpr->isEvaluatable(Context)) { |
| 352 | CastKind castKind = |
| 353 | (OutTy->isBooleanType() ? CK_IntegralToBoolean : CK_IntegralCast); |
| 354 | InputExpr = ImpCastExprToType(InputExpr, OutTy, castKind).take(); |
| 355 | Exprs[InputOpNo] = InputExpr; |
| 356 | NS->setInputExpr(i, InputExpr); |
| 357 | continue; |
| 358 | } |
| 359 | |
| 360 | Diag(InputExpr->getLocStart(), |
| 361 | diag::err_asm_tying_incompatible_types) |
| 362 | << InTy << OutTy << OutputExpr->getSourceRange() |
| 363 | << InputExpr->getSourceRange(); |
| 364 | return StmtError(); |
| 365 | } |
| 366 | |
| 367 | return Owned(NS); |
| 368 | } |
| 369 | |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 370 | ExprResult Sema::LookupInlineAsmIdentifier(CXXScopeSpec &SS, |
| 371 | SourceLocation TemplateKWLoc, |
| 372 | UnqualifiedId &Id, |
| 373 | InlineAsmIdentifierInfo &Info, |
| 374 | bool IsUnevaluatedContext) { |
Chad Rosier | b18a285 | 2013-04-22 17:01:37 +0000 | [diff] [blame] | 375 | Info.clear(); |
Chad Rosier | ce2bcbf | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 376 | |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 377 | if (IsUnevaluatedContext) |
| 378 | PushExpressionEvaluationContext(UnevaluatedAbstract, |
| 379 | ReuseLambdaContextDecl); |
| 380 | |
| 381 | ExprResult Result = ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Id, |
| 382 | /*trailing lparen*/ false, |
Chad Rosier | b9aff1e | 2013-05-24 18:32:55 +0000 | [diff] [blame] | 383 | /*is & operand*/ false, |
| 384 | /*CorrectionCandidateCallback=*/0, |
| 385 | /*IsInlineAsmIdentifier=*/ true); |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 386 | |
| 387 | if (IsUnevaluatedContext) |
| 388 | PopExpressionEvaluationContext(); |
| 389 | |
| 390 | if (!Result.isUsable()) return Result; |
| 391 | |
| 392 | Result = CheckPlaceholderExpr(Result.take()); |
| 393 | if (!Result.isUsable()) return Result; |
| 394 | |
| 395 | QualType T = Result.get()->getType(); |
| 396 | |
| 397 | // For now, reject dependent types. |
| 398 | if (T->isDependentType()) { |
| 399 | Diag(Id.getLocStart(), diag::err_asm_incomplete_type) << T; |
| 400 | return ExprError(); |
Chad Rosier | ce2bcbf | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 401 | } |
| 402 | |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 403 | // Any sort of function type is fine. |
| 404 | if (T->isFunctionType()) { |
| 405 | return Result; |
Chad Rosier | ce2bcbf | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 406 | } |
| 407 | |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 408 | // Otherwise, it needs to be a complete type. |
| 409 | if (RequireCompleteExprType(Result.get(), diag::err_asm_incomplete_type)) { |
| 410 | return ExprError(); |
| 411 | } |
| 412 | |
| 413 | // Compute the type size (and array length if applicable?). |
| 414 | Info.Type = Info.Size = Context.getTypeSizeInChars(T).getQuantity(); |
| 415 | if (T->isArrayType()) { |
| 416 | const ArrayType *ATy = Context.getAsArrayType(T); |
| 417 | Info.Type = Context.getTypeSizeInChars(ATy->getElementType()).getQuantity(); |
| 418 | Info.Length = Info.Size / Info.Type; |
| 419 | } |
| 420 | |
| 421 | // We can work with the expression as long as it's not an r-value. |
| 422 | if (!Result.get()->isRValue()) |
Chad Rosier | b18a285 | 2013-04-22 17:01:37 +0000 | [diff] [blame] | 423 | Info.IsVarDecl = true; |
Chad Rosier | ce2bcbf | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 424 | |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 425 | return Result; |
Chad Rosier | 4a0054f | 2012-10-15 19:56:10 +0000 | [diff] [blame] | 426 | } |
Chad Rosier | d997bd1 | 2012-08-22 19:18:30 +0000 | [diff] [blame] | 427 | |
Chad Rosier | 5c56364 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 428 | bool Sema::LookupInlineAsmField(StringRef Base, StringRef Member, |
| 429 | unsigned &Offset, SourceLocation AsmLoc) { |
| 430 | Offset = 0; |
| 431 | LookupResult BaseResult(*this, &Context.Idents.get(Base), SourceLocation(), |
| 432 | LookupOrdinaryName); |
| 433 | |
| 434 | if (!LookupName(BaseResult, getCurScope())) |
| 435 | return true; |
| 436 | |
| 437 | if (!BaseResult.isSingleResult()) |
| 438 | return true; |
| 439 | |
Chad Rosier | 5c56364 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 440 | const RecordType *RT = 0; |
Chad Rosier | 10230d4 | 2013-04-01 17:58:03 +0000 | [diff] [blame] | 441 | NamedDecl *FoundDecl = BaseResult.getFoundDecl(); |
| 442 | if (VarDecl *VD = dyn_cast<VarDecl>(FoundDecl)) |
Chad Rosier | 5c56364 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 443 | RT = VD->getType()->getAs<RecordType>(); |
Chad Rosier | 10230d4 | 2013-04-01 17:58:03 +0000 | [diff] [blame] | 444 | else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(FoundDecl)) |
Chad Rosier | 5c56364 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 445 | RT = TD->getUnderlyingType()->getAs<RecordType>(); |
Chad Rosier | 5c56364 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 446 | if (!RT) |
| 447 | return true; |
| 448 | |
| 449 | if (RequireCompleteType(AsmLoc, QualType(RT, 0), 0)) |
| 450 | return true; |
| 451 | |
| 452 | LookupResult FieldResult(*this, &Context.Idents.get(Member), SourceLocation(), |
| 453 | LookupMemberName); |
| 454 | |
| 455 | if (!LookupQualifiedName(FieldResult, RT->getDecl())) |
| 456 | return true; |
| 457 | |
| 458 | // FIXME: Handle IndirectFieldDecl? |
| 459 | FieldDecl *FD = dyn_cast<FieldDecl>(FieldResult.getFoundDecl()); |
| 460 | if (!FD) |
| 461 | return true; |
| 462 | |
| 463 | const ASTRecordLayout &RL = Context.getASTRecordLayout(RT->getDecl()); |
| 464 | unsigned i = FD->getFieldIndex(); |
| 465 | CharUnits Result = Context.toCharUnitsFromBits(RL.getFieldOffset(i)); |
| 466 | Offset = (unsigned)Result.getQuantity(); |
| 467 | |
| 468 | return false; |
| 469 | } |
| 470 | |
Chad Rosier | b261a50 | 2012-09-13 00:06:55 +0000 | [diff] [blame] | 471 | StmtResult Sema::ActOnMSAsmStmt(SourceLocation AsmLoc, SourceLocation LBraceLoc, |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 472 | ArrayRef<Token> AsmToks, |
| 473 | StringRef AsmString, |
| 474 | unsigned NumOutputs, unsigned NumInputs, |
| 475 | ArrayRef<StringRef> Constraints, |
| 476 | ArrayRef<StringRef> Clobbers, |
| 477 | ArrayRef<Expr*> Exprs, |
| 478 | SourceLocation EndLoc) { |
| 479 | bool IsSimple = (NumOutputs != 0 || NumInputs != 0); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 480 | MSAsmStmt *NS = |
Chad Rosier | ce2bcbf | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 481 | new (Context) MSAsmStmt(Context, AsmLoc, LBraceLoc, IsSimple, |
| 482 | /*IsVolatile*/ true, AsmToks, NumOutputs, NumInputs, |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 483 | Constraints, Exprs, AsmString, |
| 484 | Clobbers, EndLoc); |
Chad Rosier | 0731aff | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 485 | return Owned(NS); |
| 486 | } |