Chad Rosier | 3d45a77 | 2012-08-17 21:27:25 +0000 | [diff] [blame] | 1 | //===--- SemaStmtAsm.cpp - Semantic Analysis for Asm Statements -----------===// |
Chad Rosier | 4b5e48d | 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 | 802f937 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 15 | #include "clang/AST/RecordLayout.h" |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 16 | #include "clang/AST/TypeLoc.h" |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" |
| 19 | #include "clang/Sema/Initialization.h" |
| 20 | #include "clang/Sema/Lookup.h" |
| 21 | #include "clang/Sema/Scope.h" |
| 22 | #include "clang/Sema/ScopeInfo.h" |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/ArrayRef.h" |
| 24 | #include "llvm/ADT/BitVector.h" |
| 25 | #include "llvm/ADT/SmallString.h" |
| 26 | #include "llvm/MC/MCAsmInfo.h" |
| 27 | #include "llvm/MC/MCContext.h" |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCObjectFileInfo.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCParser/MCAsmParser.h" |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCRegisterInfo.h" |
| 31 | #include "llvm/MC/MCStreamer.h" |
| 32 | #include "llvm/MC/MCSubtargetInfo.h" |
| 33 | #include "llvm/MC/MCTargetAsmParser.h" |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 34 | #include "llvm/Support/SourceMgr.h" |
| 35 | #include "llvm/Support/TargetRegistry.h" |
| 36 | #include "llvm/Support/TargetSelect.h" |
| 37 | using namespace clang; |
| 38 | using namespace sema; |
| 39 | |
| 40 | /// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently |
| 41 | /// ignore "noop" casts in places where an lvalue is required by an inline asm. |
| 42 | /// We emulate this behavior when -fheinous-gnu-extensions is specified, but |
| 43 | /// provide a strong guidance to not use it. |
| 44 | /// |
| 45 | /// This method checks to see if the argument is an acceptable l-value and |
| 46 | /// returns false if it is a case we can handle. |
| 47 | static bool CheckAsmLValue(const Expr *E, Sema &S) { |
| 48 | // Type dependent expressions will be checked during instantiation. |
| 49 | if (E->isTypeDependent()) |
| 50 | return false; |
| 51 | |
| 52 | if (E->isLValue()) |
| 53 | return false; // Cool, this is an lvalue. |
| 54 | |
| 55 | // Okay, this is not an lvalue, but perhaps it is the result of a cast that we |
| 56 | // are supposed to allow. |
| 57 | const Expr *E2 = E->IgnoreParenNoopCasts(S.Context); |
| 58 | if (E != E2 && E2->isLValue()) { |
| 59 | if (!S.getLangOpts().HeinousExtensions) |
| 60 | S.Diag(E2->getLocStart(), diag::err_invalid_asm_cast_lvalue) |
| 61 | << E->getSourceRange(); |
| 62 | else |
| 63 | S.Diag(E2->getLocStart(), diag::warn_invalid_asm_cast_lvalue) |
| 64 | << E->getSourceRange(); |
| 65 | // Accept, even if we emitted an error diagnostic. |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | // None of the above, just randomly invalid non-lvalue. |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | /// isOperandMentioned - Return true if the specified operand # is mentioned |
| 74 | /// anywhere in the decomposed asm string. |
| 75 | static bool isOperandMentioned(unsigned OpNo, |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 76 | ArrayRef<GCCAsmStmt::AsmStringPiece> AsmStrPieces) { |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 77 | for (unsigned p = 0, e = AsmStrPieces.size(); p != e; ++p) { |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 78 | const GCCAsmStmt::AsmStringPiece &Piece = AsmStrPieces[p]; |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 79 | if (!Piece.isOperand()) continue; |
| 80 | |
| 81 | // If this is a reference to the input and if the input was the smaller |
| 82 | // one, then we have to reject this asm. |
| 83 | if (Piece.getOperandNo() == OpNo) |
| 84 | return true; |
| 85 | } |
| 86 | return false; |
| 87 | } |
| 88 | |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 89 | StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple, |
| 90 | bool IsVolatile, unsigned NumOutputs, |
| 91 | unsigned NumInputs, IdentifierInfo **Names, |
| 92 | MultiExprArg constraints, MultiExprArg exprs, |
| 93 | Expr *asmString, MultiExprArg clobbers, |
| 94 | SourceLocation RParenLoc) { |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 95 | unsigned NumClobbers = clobbers.size(); |
| 96 | StringLiteral **Constraints = |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 97 | reinterpret_cast<StringLiteral**>(constraints.data()); |
| 98 | Expr **Exprs = exprs.data(); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 99 | StringLiteral *AsmString = cast<StringLiteral>(asmString); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 100 | StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.data()); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 101 | |
| 102 | SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos; |
| 103 | |
| 104 | // The parser verifies that there is a string literal here. |
| 105 | if (!AsmString->isAscii()) |
| 106 | return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character) |
| 107 | << AsmString->getSourceRange()); |
| 108 | |
| 109 | for (unsigned i = 0; i != NumOutputs; i++) { |
| 110 | StringLiteral *Literal = Constraints[i]; |
| 111 | if (!Literal->isAscii()) |
| 112 | return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) |
| 113 | << Literal->getSourceRange()); |
| 114 | |
| 115 | StringRef OutputName; |
| 116 | if (Names[i]) |
| 117 | OutputName = Names[i]->getName(); |
| 118 | |
| 119 | TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName); |
| 120 | if (!Context.getTargetInfo().validateOutputConstraint(Info)) |
| 121 | return StmtError(Diag(Literal->getLocStart(), |
| 122 | diag::err_asm_invalid_output_constraint) |
| 123 | << Info.getConstraintStr()); |
| 124 | |
| 125 | // Check that the output exprs are valid lvalues. |
| 126 | Expr *OutputExpr = Exprs[i]; |
Bill Wendling | 14df23b | 2013-03-25 21:09:49 +0000 | [diff] [blame] | 127 | if (CheckAsmLValue(OutputExpr, *this)) |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 128 | return StmtError(Diag(OutputExpr->getLocStart(), |
Bill Wendling | 14df23b | 2013-03-25 21:09:49 +0000 | [diff] [blame] | 129 | diag::err_asm_invalid_lvalue_in_output) |
| 130 | << OutputExpr->getSourceRange()); |
| 131 | |
Bill Wendling | d835d94 | 2013-03-27 06:06:26 +0000 | [diff] [blame] | 132 | if (RequireCompleteType(OutputExpr->getLocStart(), Exprs[i]->getType(), |
| 133 | diag::err_dereference_incomplete_type)) |
| 134 | return StmtError(); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 135 | |
| 136 | OutputConstraintInfos.push_back(Info); |
| 137 | } |
| 138 | |
| 139 | SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos; |
| 140 | |
| 141 | for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) { |
| 142 | StringLiteral *Literal = Constraints[i]; |
| 143 | if (!Literal->isAscii()) |
| 144 | return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) |
| 145 | << Literal->getSourceRange()); |
| 146 | |
| 147 | StringRef InputName; |
| 148 | if (Names[i]) |
| 149 | InputName = Names[i]->getName(); |
| 150 | |
| 151 | TargetInfo::ConstraintInfo Info(Literal->getString(), InputName); |
| 152 | if (!Context.getTargetInfo().validateInputConstraint(OutputConstraintInfos.data(), |
| 153 | NumOutputs, Info)) { |
| 154 | return StmtError(Diag(Literal->getLocStart(), |
| 155 | diag::err_asm_invalid_input_constraint) |
| 156 | << Info.getConstraintStr()); |
| 157 | } |
| 158 | |
| 159 | Expr *InputExpr = Exprs[i]; |
| 160 | |
| 161 | // Only allow void types for memory constraints. |
| 162 | if (Info.allowsMemory() && !Info.allowsRegister()) { |
| 163 | if (CheckAsmLValue(InputExpr, *this)) |
| 164 | return StmtError(Diag(InputExpr->getLocStart(), |
| 165 | diag::err_asm_invalid_lvalue_in_input) |
| 166 | << Info.getConstraintStr() |
| 167 | << InputExpr->getSourceRange()); |
| 168 | } |
| 169 | |
| 170 | if (Info.allowsRegister()) { |
| 171 | if (InputExpr->getType()->isVoidType()) { |
| 172 | return StmtError(Diag(InputExpr->getLocStart(), |
| 173 | diag::err_asm_invalid_type_in_input) |
| 174 | << InputExpr->getType() << Info.getConstraintStr() |
| 175 | << InputExpr->getSourceRange()); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | ExprResult Result = DefaultFunctionArrayLvalueConversion(Exprs[i]); |
| 180 | if (Result.isInvalid()) |
| 181 | return StmtError(); |
| 182 | |
| 183 | Exprs[i] = Result.take(); |
| 184 | InputConstraintInfos.push_back(Info); |
Bill Wendling | 68fd608 | 2012-11-12 06:42:51 +0000 | [diff] [blame] | 185 | |
| 186 | const Type *Ty = Exprs[i]->getType().getTypePtr(); |
Bill Wendling | 14df23b | 2013-03-25 21:09:49 +0000 | [diff] [blame] | 187 | if (Ty->isDependentType()) |
Eric Christopher | 6ceb377 | 2012-11-12 23:13:34 +0000 | [diff] [blame] | 188 | continue; |
| 189 | |
Bill Wendling | 14df23b | 2013-03-25 21:09:49 +0000 | [diff] [blame] | 190 | if (!Ty->isVoidType() || !Info.allowsMemory()) |
Bill Wendling | d835d94 | 2013-03-27 06:06:26 +0000 | [diff] [blame] | 191 | if (RequireCompleteType(InputExpr->getLocStart(), Exprs[i]->getType(), |
| 192 | diag::err_dereference_incomplete_type)) |
| 193 | return StmtError(); |
Bill Wendling | 14df23b | 2013-03-25 21:09:49 +0000 | [diff] [blame] | 194 | |
Bill Wendling | 68fd608 | 2012-11-12 06:42:51 +0000 | [diff] [blame] | 195 | unsigned Size = Context.getTypeSize(Ty); |
| 196 | if (!Context.getTargetInfo().validateInputSize(Literal->getString(), |
| 197 | Size)) |
| 198 | return StmtError(Diag(InputExpr->getLocStart(), |
| 199 | diag::err_asm_invalid_input_size) |
| 200 | << Info.getConstraintStr()); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | // Check that the clobbers are valid. |
| 204 | for (unsigned i = 0; i != NumClobbers; i++) { |
| 205 | StringLiteral *Literal = Clobbers[i]; |
| 206 | if (!Literal->isAscii()) |
| 207 | return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) |
| 208 | << Literal->getSourceRange()); |
| 209 | |
| 210 | StringRef Clobber = Literal->getString(); |
| 211 | |
| 212 | if (!Context.getTargetInfo().isValidClobber(Clobber)) |
| 213 | return StmtError(Diag(Literal->getLocStart(), |
| 214 | diag::err_asm_unknown_register_name) << Clobber); |
| 215 | } |
| 216 | |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 217 | GCCAsmStmt *NS = |
| 218 | new (Context) GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs, |
| 219 | NumInputs, Names, Constraints, Exprs, AsmString, |
| 220 | NumClobbers, Clobbers, RParenLoc); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 221 | // Validate the asm string, ensuring it makes sense given the operands we |
| 222 | // have. |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 223 | SmallVector<GCCAsmStmt::AsmStringPiece, 8> Pieces; |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 224 | unsigned DiagOffs; |
| 225 | if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) { |
| 226 | Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID) |
| 227 | << AsmString->getSourceRange(); |
| 228 | return StmtError(); |
| 229 | } |
| 230 | |
Bill Wendling | 50d46ca | 2012-10-25 23:28:48 +0000 | [diff] [blame] | 231 | // Validate constraints and modifiers. |
| 232 | for (unsigned i = 0, e = Pieces.size(); i != e; ++i) { |
| 233 | GCCAsmStmt::AsmStringPiece &Piece = Pieces[i]; |
| 234 | if (!Piece.isOperand()) continue; |
| 235 | |
| 236 | // Look for the correct constraint index. |
| 237 | unsigned Idx = 0; |
| 238 | unsigned ConstraintIdx = 0; |
| 239 | for (unsigned i = 0, e = NS->getNumOutputs(); i != e; ++i, ++ConstraintIdx) { |
| 240 | TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i]; |
| 241 | if (Idx == Piece.getOperandNo()) |
| 242 | break; |
| 243 | ++Idx; |
| 244 | |
| 245 | if (Info.isReadWrite()) { |
| 246 | if (Idx == Piece.getOperandNo()) |
| 247 | break; |
| 248 | ++Idx; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | for (unsigned i = 0, e = NS->getNumInputs(); i != e; ++i, ++ConstraintIdx) { |
| 253 | TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i]; |
| 254 | if (Idx == Piece.getOperandNo()) |
| 255 | break; |
| 256 | ++Idx; |
| 257 | |
| 258 | if (Info.isReadWrite()) { |
| 259 | if (Idx == Piece.getOperandNo()) |
| 260 | break; |
| 261 | ++Idx; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // Now that we have the right indexes go ahead and check. |
| 266 | StringLiteral *Literal = Constraints[ConstraintIdx]; |
| 267 | const Type *Ty = Exprs[ConstraintIdx]->getType().getTypePtr(); |
| 268 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
| 269 | continue; |
| 270 | |
| 271 | unsigned Size = Context.getTypeSize(Ty); |
| 272 | if (!Context.getTargetInfo() |
| 273 | .validateConstraintModifier(Literal->getString(), Piece.getModifier(), |
| 274 | Size)) |
| 275 | Diag(Exprs[ConstraintIdx]->getLocStart(), |
| 276 | diag::warn_asm_mismatched_size_modifier); |
| 277 | } |
| 278 | |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 279 | // Validate tied input operands for type mismatches. |
| 280 | for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) { |
| 281 | TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i]; |
| 282 | |
| 283 | // If this is a tied constraint, verify that the output and input have |
| 284 | // either exactly the same type, or that they are int/ptr operands with the |
| 285 | // same size (int/long, int*/long, are ok etc). |
| 286 | if (!Info.hasTiedOperand()) continue; |
| 287 | |
| 288 | unsigned TiedTo = Info.getTiedOperand(); |
| 289 | unsigned InputOpNo = i+NumOutputs; |
| 290 | Expr *OutputExpr = Exprs[TiedTo]; |
| 291 | Expr *InputExpr = Exprs[InputOpNo]; |
| 292 | |
| 293 | if (OutputExpr->isTypeDependent() || InputExpr->isTypeDependent()) |
| 294 | continue; |
| 295 | |
| 296 | QualType InTy = InputExpr->getType(); |
| 297 | QualType OutTy = OutputExpr->getType(); |
| 298 | if (Context.hasSameType(InTy, OutTy)) |
| 299 | continue; // All types can be tied to themselves. |
| 300 | |
| 301 | // Decide if the input and output are in the same domain (integer/ptr or |
| 302 | // floating point. |
| 303 | enum AsmDomain { |
| 304 | AD_Int, AD_FP, AD_Other |
| 305 | } InputDomain, OutputDomain; |
| 306 | |
| 307 | if (InTy->isIntegerType() || InTy->isPointerType()) |
| 308 | InputDomain = AD_Int; |
| 309 | else if (InTy->isRealFloatingType()) |
| 310 | InputDomain = AD_FP; |
| 311 | else |
| 312 | InputDomain = AD_Other; |
| 313 | |
| 314 | if (OutTy->isIntegerType() || OutTy->isPointerType()) |
| 315 | OutputDomain = AD_Int; |
| 316 | else if (OutTy->isRealFloatingType()) |
| 317 | OutputDomain = AD_FP; |
| 318 | else |
| 319 | OutputDomain = AD_Other; |
| 320 | |
| 321 | // They are ok if they are the same size and in the same domain. This |
| 322 | // allows tying things like: |
| 323 | // void* to int* |
| 324 | // void* to int if they are the same size. |
| 325 | // double to long double if they are the same size. |
| 326 | // |
| 327 | uint64_t OutSize = Context.getTypeSize(OutTy); |
| 328 | uint64_t InSize = Context.getTypeSize(InTy); |
| 329 | if (OutSize == InSize && InputDomain == OutputDomain && |
| 330 | InputDomain != AD_Other) |
| 331 | continue; |
| 332 | |
| 333 | // If the smaller input/output operand is not mentioned in the asm string, |
| 334 | // then we can promote the smaller one to a larger input and the asm string |
| 335 | // won't notice. |
| 336 | bool SmallerValueMentioned = false; |
| 337 | |
| 338 | // If this is a reference to the input and if the input was the smaller |
| 339 | // one, then we have to reject this asm. |
| 340 | if (isOperandMentioned(InputOpNo, Pieces)) { |
| 341 | // This is a use in the asm string of the smaller operand. Since we |
| 342 | // codegen this by promoting to a wider value, the asm will get printed |
| 343 | // "wrong". |
| 344 | SmallerValueMentioned |= InSize < OutSize; |
| 345 | } |
| 346 | if (isOperandMentioned(TiedTo, Pieces)) { |
| 347 | // If this is a reference to the output, and if the output is the larger |
| 348 | // value, then it's ok because we'll promote the input to the larger type. |
| 349 | SmallerValueMentioned |= OutSize < InSize; |
| 350 | } |
| 351 | |
| 352 | // If the smaller value wasn't mentioned in the asm string, and if the |
| 353 | // output was a register, just extend the shorter one to the size of the |
| 354 | // larger one. |
| 355 | if (!SmallerValueMentioned && InputDomain != AD_Other && |
| 356 | OutputConstraintInfos[TiedTo].allowsRegister()) |
| 357 | continue; |
| 358 | |
| 359 | // Either both of the operands were mentioned or the smaller one was |
| 360 | // mentioned. One more special case that we'll allow: if the tied input is |
| 361 | // integer, unmentioned, and is a constant, then we'll allow truncating it |
| 362 | // down to the size of the destination. |
| 363 | if (InputDomain == AD_Int && OutputDomain == AD_Int && |
| 364 | !isOperandMentioned(InputOpNo, Pieces) && |
| 365 | InputExpr->isEvaluatable(Context)) { |
| 366 | CastKind castKind = |
| 367 | (OutTy->isBooleanType() ? CK_IntegralToBoolean : CK_IntegralCast); |
| 368 | InputExpr = ImpCastExprToType(InputExpr, OutTy, castKind).take(); |
| 369 | Exprs[InputOpNo] = InputExpr; |
| 370 | NS->setInputExpr(i, InputExpr); |
| 371 | continue; |
| 372 | } |
| 373 | |
| 374 | Diag(InputExpr->getLocStart(), |
| 375 | diag::err_asm_tying_incompatible_types) |
| 376 | << InTy << OutTy << OutputExpr->getSourceRange() |
| 377 | << InputExpr->getSourceRange(); |
| 378 | return StmtError(); |
| 379 | } |
| 380 | |
| 381 | return Owned(NS); |
| 382 | } |
| 383 | |
Chad Rosier | 358ab76 | 2012-08-22 21:08:06 +0000 | [diff] [blame] | 384 | // getSpelling - Get the spelling of the AsmTok token. |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 385 | static StringRef getSpelling(Sema &SemaRef, Token AsmTok) { |
| 386 | StringRef Asm; |
| 387 | SmallString<512> TokenBuf; |
| 388 | TokenBuf.resize(512); |
| 389 | bool StringInvalid = false; |
| 390 | Asm = SemaRef.PP.getSpelling(AsmTok, TokenBuf, &StringInvalid); |
| 391 | assert (!StringInvalid && "Expected valid string!"); |
| 392 | return Asm; |
| 393 | } |
| 394 | |
Chad Rosier | d9b56ed | 2012-10-15 19:56:10 +0000 | [diff] [blame] | 395 | // Build the inline assembly string. Returns true on error. |
| 396 | static bool buildMSAsmString(Sema &SemaRef, |
| 397 | SourceLocation AsmLoc, |
| 398 | ArrayRef<Token> AsmToks, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 399 | SmallVectorImpl<unsigned> &TokOffsets, |
Chad Rosier | d9b56ed | 2012-10-15 19:56:10 +0000 | [diff] [blame] | 400 | std::string &AsmString) { |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 401 | assert (!AsmToks.empty() && "Didn't expect an empty AsmToks!"); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 402 | |
| 403 | SmallString<512> Asm; |
| 404 | for (unsigned i = 0, e = AsmToks.size(); i < e; ++i) { |
Bob Wilson | 40d39e3 | 2012-09-24 19:57:55 +0000 | [diff] [blame] | 405 | bool isNewAsm = ((i == 0) || |
| 406 | AsmToks[i].isAtStartOfLine() || |
| 407 | AsmToks[i].is(tok::kw_asm)); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 408 | if (isNewAsm) { |
Chad Rosier | d9b56ed | 2012-10-15 19:56:10 +0000 | [diff] [blame] | 409 | if (i != 0) |
| 410 | Asm += "\n\t"; |
| 411 | |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 412 | if (AsmToks[i].is(tok::kw_asm)) { |
| 413 | i++; // Skip __asm |
Bob Wilson | b0f6b9c | 2012-09-24 19:57:59 +0000 | [diff] [blame] | 414 | if (i == e) { |
| 415 | SemaRef.Diag(AsmLoc, diag::err_asm_empty); |
| 416 | return true; |
| 417 | } |
Chad Rosier | d9b56ed | 2012-10-15 19:56:10 +0000 | [diff] [blame] | 418 | |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
Chad Rosier | b55e602 | 2012-09-13 00:06:55 +0000 | [diff] [blame] | 422 | if (i && AsmToks[i].hasLeadingSpace() && !isNewAsm) |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 423 | Asm += ' '; |
Chad Rosier | 4de9716 | 2012-09-11 00:51:28 +0000 | [diff] [blame] | 424 | |
| 425 | StringRef Spelling = getSpelling(SemaRef, AsmToks[i]); |
| 426 | Asm += Spelling; |
Eli Friedman | 5f1385b | 2012-10-23 02:43:30 +0000 | [diff] [blame] | 427 | TokOffsets.push_back(Asm.size()); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 428 | } |
Chad Rosier | d9b56ed | 2012-10-15 19:56:10 +0000 | [diff] [blame] | 429 | AsmString = Asm.str(); |
Bob Wilson | b0f6b9c | 2012-09-24 19:57:59 +0000 | [diff] [blame] | 430 | return false; |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Benjamin Kramer | c6f84cf | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 433 | namespace { |
| 434 | |
Chad Rosier | 7fd00b1 | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 435 | class MCAsmParserSemaCallbackImpl : public llvm::MCAsmParserSemaCallback { |
Eli Friedman | 5f1385b | 2012-10-23 02:43:30 +0000 | [diff] [blame] | 436 | Sema &SemaRef; |
| 437 | SourceLocation AsmLoc; |
| 438 | ArrayRef<Token> AsmToks; |
| 439 | ArrayRef<unsigned> TokOffsets; |
Chad Rosier | d9b56ed | 2012-10-15 19:56:10 +0000 | [diff] [blame] | 440 | |
| 441 | public: |
Eli Friedman | 5f1385b | 2012-10-23 02:43:30 +0000 | [diff] [blame] | 442 | MCAsmParserSemaCallbackImpl(Sema &Ref, SourceLocation Loc, |
| 443 | ArrayRef<Token> Toks, |
| 444 | ArrayRef<unsigned> Offsets) |
| 445 | : SemaRef(Ref), AsmLoc(Loc), AsmToks(Toks), TokOffsets(Offsets) { } |
Chad Rosier | 7fd00b1 | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 446 | ~MCAsmParserSemaCallbackImpl() {} |
| 447 | |
Chad Rosier | 1e7ca62 | 2013-04-22 17:01:37 +0000 | [diff] [blame] | 448 | void *LookupInlineAsmIdentifier(StringRef &LineBuf, |
| 449 | InlineAsmIdentifierInfo &Info) { |
| 450 | SourceLocation Loc = SourceLocation::getFromPtrEncoding(LineBuf.data()); |
| 451 | NamedDecl *OpDecl = SemaRef.LookupInlineAsmIdentifier(LineBuf, Loc, Info); |
Chad Rosier | d052ebf | 2012-10-18 19:39:37 +0000 | [diff] [blame] | 452 | return static_cast<void *>(OpDecl); |
Chad Rosier | 7fd00b1 | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 453 | } |
Eli Friedman | 5f1385b | 2012-10-23 02:43:30 +0000 | [diff] [blame] | 454 | |
Chad Rosier | 802f937 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 455 | bool LookupInlineAsmField(StringRef Base, StringRef Member, |
| 456 | unsigned &Offset) { |
| 457 | return SemaRef.LookupInlineAsmField(Base, Member, Offset, AsmLoc); |
| 458 | } |
| 459 | |
Eli Friedman | 5f1385b | 2012-10-23 02:43:30 +0000 | [diff] [blame] | 460 | static void MSAsmDiagHandlerCallback(const llvm::SMDiagnostic &D, |
| 461 | void *Context) { |
| 462 | ((MCAsmParserSemaCallbackImpl*)Context)->MSAsmDiagHandler(D); |
| 463 | } |
| 464 | void MSAsmDiagHandler(const llvm::SMDiagnostic &D) { |
| 465 | // Compute an offset into the inline asm buffer. |
| 466 | // FIXME: This isn't right if .macro is involved (but hopefully, no |
| 467 | // real-world code does that). |
| 468 | const llvm::SourceMgr &LSM = *D.getSourceMgr(); |
| 469 | const llvm::MemoryBuffer *LBuf = |
| 470 | LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc())); |
| 471 | unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart(); |
| 472 | |
| 473 | // Figure out which token that offset points into. |
| 474 | const unsigned *OffsetPtr = |
| 475 | std::lower_bound(TokOffsets.begin(), TokOffsets.end(), Offset); |
| 476 | unsigned TokIndex = OffsetPtr - TokOffsets.begin(); |
| 477 | |
| 478 | // If we come up with an answer which seems sane, use it; otherwise, |
| 479 | // just point at the __asm keyword. |
| 480 | // FIXME: Assert the answer is sane once we handle .macro correctly. |
| 481 | SourceLocation Loc = AsmLoc; |
| 482 | if (TokIndex < AsmToks.size()) { |
| 483 | const Token *Tok = &AsmToks[TokIndex]; |
| 484 | Loc = Tok->getLocation(); |
| 485 | Loc = Loc.getLocWithOffset(Offset - (*OffsetPtr - Tok->getLength())); |
| 486 | } |
| 487 | SemaRef.Diag(Loc, diag::err_inline_ms_asm_parsing) << D.getMessage(); |
| 488 | } |
Chad Rosier | d9b56ed | 2012-10-15 19:56:10 +0000 | [diff] [blame] | 489 | }; |
| 490 | |
Benjamin Kramer | c6f84cf | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Chad Rosier | 87a9f2b | 2013-04-19 20:37:49 +0000 | [diff] [blame] | 493 | // FIXME: Temporary hack until the frontend parser is hooked up to parse |
| 494 | // variables. |
| 495 | static bool isIdentifierChar(char c) { |
| 496 | return isalnum(c) || c == '_' || c == '$' || c == '.' || c == '@'; |
| 497 | } |
| 498 | |
| 499 | static void lexIdentifier(const char *&CurPtr) { |
| 500 | while (isIdentifierChar(*CurPtr)) |
| 501 | ++CurPtr; |
| 502 | } |
| 503 | |
| 504 | static StringRef parseIdentifier(StringRef Identifier) { |
| 505 | const char *StartPtr = Identifier.data(), *EndPtr, *CurPtr; |
| 506 | EndPtr = StartPtr + Identifier.size(); |
| 507 | CurPtr = StartPtr; |
| 508 | while(CurPtr <= EndPtr) { |
| 509 | if (isIdentifierChar(*CurPtr)) |
| 510 | lexIdentifier(CurPtr); |
| 511 | else if (CurPtr[0] == ':' && CurPtr[1] == ':') |
| 512 | CurPtr += 2; |
| 513 | else |
| 514 | break; |
| 515 | } |
| 516 | return StringRef(StartPtr, CurPtr - StartPtr); |
| 517 | } |
| 518 | |
Chad Rosier | 1e7ca62 | 2013-04-22 17:01:37 +0000 | [diff] [blame] | 519 | NamedDecl *Sema::LookupInlineAsmIdentifier(StringRef &LineBuf, SourceLocation Loc, |
| 520 | InlineAsmIdentifierInfo &Info) { |
| 521 | Info.clear(); |
Chad Rosier | 87a9f2b | 2013-04-19 20:37:49 +0000 | [diff] [blame] | 522 | // FIXME: Temporary hack until the frontend parser is hooked up to parse |
| 523 | // variables. |
Chad Rosier | 1e7ca62 | 2013-04-22 17:01:37 +0000 | [diff] [blame] | 524 | LineBuf = parseIdentifier(LineBuf); |
| 525 | LookupResult Result(*this, &Context.Idents.get(LineBuf), Loc, |
Chad Rosier | 7fd00b1 | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 526 | Sema::LookupOrdinaryName); |
| 527 | |
| 528 | if (!LookupName(Result, getCurScope())) { |
| 529 | // If we don't find anything, return null; the AsmParser will assume |
| 530 | // it is a label of some sort. |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | if (!Result.isSingleResult()) { |
| 535 | // FIXME: Diagnose result. |
| 536 | return 0; |
| 537 | } |
| 538 | |
Chad Rosier | e3faa6e | 2013-04-01 17:58:03 +0000 | [diff] [blame] | 539 | NamedDecl *FoundDecl = Result.getFoundDecl(); |
| 540 | if (isa<FunctionDecl>(FoundDecl)) |
| 541 | return FoundDecl; |
| 542 | if (VarDecl *Var = dyn_cast<VarDecl>(FoundDecl)) { |
Chad Rosier | e3faa6e | 2013-04-01 17:58:03 +0000 | [diff] [blame] | 543 | QualType Ty = Var->getType(); |
Chad Rosier | 1e7ca62 | 2013-04-22 17:01:37 +0000 | [diff] [blame] | 544 | Info.Type = Info.Size = Context.getTypeSizeInChars(Ty).getQuantity(); |
Chad Rosier | e3faa6e | 2013-04-01 17:58:03 +0000 | [diff] [blame] | 545 | if (Ty->isArrayType()) { |
| 546 | const ArrayType *ATy = Context.getAsArrayType(Ty); |
Chad Rosier | 1e7ca62 | 2013-04-22 17:01:37 +0000 | [diff] [blame] | 547 | Info.Type = Context.getTypeSizeInChars(ATy->getElementType()).getQuantity(); |
| 548 | Info.Length = Info.Size / Info.Type; |
Chad Rosier | 3973f28 | 2013-01-10 22:10:16 +0000 | [diff] [blame] | 549 | } |
Chad Rosier | 1e7ca62 | 2013-04-22 17:01:37 +0000 | [diff] [blame] | 550 | Info.IsVarDecl = true; |
Chad Rosier | e3faa6e | 2013-04-01 17:58:03 +0000 | [diff] [blame] | 551 | return FoundDecl; |
Chad Rosier | 7fd00b1 | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | // FIXME: Handle other kinds of results? (FieldDecl, etc.) |
| 555 | // FIXME: Diagnose if we find something we can't handle, like a typedef. |
| 556 | return 0; |
Chad Rosier | d9b56ed | 2012-10-15 19:56:10 +0000 | [diff] [blame] | 557 | } |
Chad Rosier | 2735df2 | 2012-08-22 19:18:30 +0000 | [diff] [blame] | 558 | |
Chad Rosier | 802f937 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 559 | bool Sema::LookupInlineAsmField(StringRef Base, StringRef Member, |
| 560 | unsigned &Offset, SourceLocation AsmLoc) { |
| 561 | Offset = 0; |
| 562 | LookupResult BaseResult(*this, &Context.Idents.get(Base), SourceLocation(), |
| 563 | LookupOrdinaryName); |
| 564 | |
| 565 | if (!LookupName(BaseResult, getCurScope())) |
| 566 | return true; |
| 567 | |
| 568 | if (!BaseResult.isSingleResult()) |
| 569 | return true; |
| 570 | |
Chad Rosier | 802f937 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 571 | const RecordType *RT = 0; |
Chad Rosier | e3faa6e | 2013-04-01 17:58:03 +0000 | [diff] [blame] | 572 | NamedDecl *FoundDecl = BaseResult.getFoundDecl(); |
| 573 | if (VarDecl *VD = dyn_cast<VarDecl>(FoundDecl)) |
Chad Rosier | 802f937 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 574 | RT = VD->getType()->getAs<RecordType>(); |
Chad Rosier | e3faa6e | 2013-04-01 17:58:03 +0000 | [diff] [blame] | 575 | else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(FoundDecl)) |
Chad Rosier | 802f937 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 576 | RT = TD->getUnderlyingType()->getAs<RecordType>(); |
Chad Rosier | 802f937 | 2012-10-25 21:49:22 +0000 | [diff] [blame] | 577 | if (!RT) |
| 578 | return true; |
| 579 | |
| 580 | if (RequireCompleteType(AsmLoc, QualType(RT, 0), 0)) |
| 581 | return true; |
| 582 | |
| 583 | LookupResult FieldResult(*this, &Context.Idents.get(Member), SourceLocation(), |
| 584 | LookupMemberName); |
| 585 | |
| 586 | if (!LookupQualifiedName(FieldResult, RT->getDecl())) |
| 587 | return true; |
| 588 | |
| 589 | // FIXME: Handle IndirectFieldDecl? |
| 590 | FieldDecl *FD = dyn_cast<FieldDecl>(FieldResult.getFoundDecl()); |
| 591 | if (!FD) |
| 592 | return true; |
| 593 | |
| 594 | const ASTRecordLayout &RL = Context.getASTRecordLayout(RT->getDecl()); |
| 595 | unsigned i = FD->getFieldIndex(); |
| 596 | CharUnits Result = Context.toCharUnitsFromBits(RL.getFieldOffset(i)); |
| 597 | Offset = (unsigned)Result.getQuantity(); |
| 598 | |
| 599 | return false; |
| 600 | } |
| 601 | |
Chad Rosier | b55e602 | 2012-09-13 00:06:55 +0000 | [diff] [blame] | 602 | StmtResult Sema::ActOnMSAsmStmt(SourceLocation AsmLoc, SourceLocation LBraceLoc, |
| 603 | ArrayRef<Token> AsmToks,SourceLocation EndLoc) { |
Chad Rosier | e54cba1 | 2012-10-16 21:55:39 +0000 | [diff] [blame] | 604 | SmallVector<IdentifierInfo*, 4> Names; |
Chad Rosier | 7fd00b1 | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 605 | SmallVector<StringRef, 4> ConstraintRefs; |
Chad Rosier | e54cba1 | 2012-10-16 21:55:39 +0000 | [diff] [blame] | 606 | SmallVector<Expr*, 4> Exprs; |
Chad Rosier | 7fd00b1 | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 607 | SmallVector<StringRef, 4> ClobberRefs; |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 608 | |
Chad Rosier | ae07378 | 2013-01-24 20:24:34 +0000 | [diff] [blame] | 609 | llvm::Triple TheTriple = Context.getTargetInfo().getTriple(); |
| 610 | llvm::Triple::ArchType ArchTy = TheTriple.getArch(); |
| 611 | bool UnsupportedArch = ArchTy != llvm::Triple::x86 && |
| 612 | ArchTy != llvm::Triple::x86_64; |
| 613 | if (UnsupportedArch) |
| 614 | Diag(AsmLoc, diag::err_msasm_unsupported_arch) << TheTriple.getArchName(); |
| 615 | |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 616 | // Empty asm statements don't need to instantiate the AsmParser, etc. |
Chad Rosier | ae07378 | 2013-01-24 20:24:34 +0000 | [diff] [blame] | 617 | if (UnsupportedArch || AsmToks.empty()) { |
Chad Rosier | d9b56ed | 2012-10-15 19:56:10 +0000 | [diff] [blame] | 618 | StringRef EmptyAsmStr; |
| 619 | MSAsmStmt *NS = |
| 620 | new (Context) MSAsmStmt(Context, AsmLoc, LBraceLoc, /*IsSimple*/ true, |
Chad Rosier | e54cba1 | 2012-10-16 21:55:39 +0000 | [diff] [blame] | 621 | /*IsVolatile*/ true, AsmToks, /*NumOutputs*/ 0, |
Chad Rosier | 7fd00b1 | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 622 | /*NumInputs*/ 0, Names, ConstraintRefs, Exprs, |
| 623 | EmptyAsmStr, ClobberRefs, EndLoc); |
Chad Rosier | d9b56ed | 2012-10-15 19:56:10 +0000 | [diff] [blame] | 624 | return Owned(NS); |
| 625 | } |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 626 | |
Chad Rosier | 7fd00b1 | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 627 | std::string AsmString; |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 628 | SmallVector<unsigned, 8> TokOffsets; |
Eli Friedman | 5f1385b | 2012-10-23 02:43:30 +0000 | [diff] [blame] | 629 | if (buildMSAsmString(*this, AsmLoc, AsmToks, TokOffsets, AsmString)) |
Bob Wilson | b0f6b9c | 2012-09-24 19:57:59 +0000 | [diff] [blame] | 630 | return StmtError(); |
Chad Rosier | 38c71d3 | 2012-08-21 21:56:39 +0000 | [diff] [blame] | 631 | |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 632 | // Get the target specific parser. |
| 633 | std::string Error; |
Chad Rosier | ae07378 | 2013-01-24 20:24:34 +0000 | [diff] [blame] | 634 | const std::string &TT = TheTriple.getTriple(); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 635 | const llvm::Target *TheTarget(llvm::TargetRegistry::lookupTarget(TT, Error)); |
| 636 | |
| 637 | OwningPtr<llvm::MCAsmInfo> MAI(TheTarget->createMCAsmInfo(TT)); |
| 638 | OwningPtr<llvm::MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TT)); |
| 639 | OwningPtr<llvm::MCObjectFileInfo> MOFI(new llvm::MCObjectFileInfo()); |
| 640 | OwningPtr<llvm::MCSubtargetInfo> |
| 641 | STI(TheTarget->createMCSubtargetInfo(TT, "", "")); |
| 642 | |
Chad Rosier | d9b56ed | 2012-10-15 19:56:10 +0000 | [diff] [blame] | 643 | llvm::SourceMgr SrcMgr; |
| 644 | llvm::MCContext Ctx(*MAI, *MRI, MOFI.get(), &SrcMgr); |
| 645 | llvm::MemoryBuffer *Buffer = |
Chad Rosier | 324645a | 2013-04-18 15:45:31 +0000 | [diff] [blame] | 646 | llvm::MemoryBuffer::getMemBuffer(AsmString, "<MS inline asm>"); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 647 | |
Chad Rosier | d9b56ed | 2012-10-15 19:56:10 +0000 | [diff] [blame] | 648 | // Tell SrcMgr about this buffer, which is what the parser will pick up. |
| 649 | SrcMgr.AddNewSourceBuffer(Buffer, llvm::SMLoc()); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 650 | |
Chad Rosier | d9b56ed | 2012-10-15 19:56:10 +0000 | [diff] [blame] | 651 | OwningPtr<llvm::MCStreamer> Str(createNullStreamer(Ctx)); |
| 652 | OwningPtr<llvm::MCAsmParser> |
| 653 | Parser(createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI)); |
| 654 | OwningPtr<llvm::MCTargetAsmParser> |
| 655 | TargetParser(TheTarget->createMCAsmParser(*STI, *Parser)); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 656 | |
Chad Rosier | d9b56ed | 2012-10-15 19:56:10 +0000 | [diff] [blame] | 657 | // Get the instruction descriptor. |
| 658 | const llvm::MCInstrInfo *MII = TheTarget->createMCInstrInfo(); |
| 659 | llvm::MCInstPrinter *IP = |
| 660 | TheTarget->createMCInstPrinter(1, *MAI, *MII, *MRI, *STI); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 661 | |
Chad Rosier | d9b56ed | 2012-10-15 19:56:10 +0000 | [diff] [blame] | 662 | // Change to the Intel dialect. |
| 663 | Parser->setAssemblerDialect(1); |
| 664 | Parser->setTargetParser(*TargetParser.get()); |
| 665 | Parser->setParsingInlineAsm(true); |
Chad Rosier | cf81cd2 | 2012-10-19 17:58:45 +0000 | [diff] [blame] | 666 | TargetParser->setParsingInlineAsm(true); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 667 | |
Eli Friedman | 5f1385b | 2012-10-23 02:43:30 +0000 | [diff] [blame] | 668 | MCAsmParserSemaCallbackImpl MCAPSI(*this, AsmLoc, AsmToks, TokOffsets); |
Chad Rosier | 793c405 | 2012-10-19 20:36:37 +0000 | [diff] [blame] | 669 | TargetParser->setSemaCallback(&MCAPSI); |
Eli Friedman | 5f1385b | 2012-10-23 02:43:30 +0000 | [diff] [blame] | 670 | SrcMgr.setDiagHandler(MCAsmParserSemaCallbackImpl::MSAsmDiagHandlerCallback, |
| 671 | &MCAPSI); |
Chad Rosier | 793c405 | 2012-10-19 20:36:37 +0000 | [diff] [blame] | 672 | |
Chad Rosier | 7fd00b1 | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 673 | unsigned NumOutputs; |
| 674 | unsigned NumInputs; |
Chad Rosier | d9b56ed | 2012-10-15 19:56:10 +0000 | [diff] [blame] | 675 | std::string AsmStringIR; |
Chad Rosier | 4d5dd7c | 2012-10-23 17:44:40 +0000 | [diff] [blame] | 676 | SmallVector<std::pair<void *, bool>, 4> OpDecls; |
Chad Rosier | 7fd00b1 | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 677 | SmallVector<std::string, 4> Constraints; |
Chad Rosier | 7fd00b1 | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 678 | SmallVector<std::string, 4> Clobbers; |
Jim Grosbach | d6d864f | 2013-02-20 22:25:15 +0000 | [diff] [blame] | 679 | if (Parser->parseMSInlineAsm(AsmLoc.getPtrEncoding(), AsmStringIR, |
Chad Rosier | d052ebf | 2012-10-18 19:39:37 +0000 | [diff] [blame] | 680 | NumOutputs, NumInputs, OpDecls, Constraints, |
| 681 | Clobbers, MII, IP, MCAPSI)) |
Chad Rosier | 7fd00b1 | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 682 | return StmtError(); |
Chad Rosier | d9b56ed | 2012-10-15 19:56:10 +0000 | [diff] [blame] | 683 | |
Chad Rosier | 7fd00b1 | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 684 | // Build the vector of clobber StringRefs. |
| 685 | unsigned NumClobbers = Clobbers.size(); |
| 686 | ClobberRefs.resize(NumClobbers); |
| 687 | for (unsigned i = 0; i != NumClobbers; ++i) |
| 688 | ClobberRefs[i] = StringRef(Clobbers[i]); |
Chad Rosier | d9b56ed | 2012-10-15 19:56:10 +0000 | [diff] [blame] | 689 | |
Chad Rosier | 7fd00b1 | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 690 | // Recast the void pointers and build the vector of constraint StringRefs. |
| 691 | unsigned NumExprs = NumOutputs + NumInputs; |
Chad Rosier | 7fd00b1 | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 692 | Names.resize(NumExprs); |
| 693 | ConstraintRefs.resize(NumExprs); |
| 694 | Exprs.resize(NumExprs); |
| 695 | for (unsigned i = 0, e = NumExprs; i != e; ++i) { |
Chad Rosier | 4d5dd7c | 2012-10-23 17:44:40 +0000 | [diff] [blame] | 696 | NamedDecl *OpDecl = static_cast<NamedDecl *>(OpDecls[i].first); |
Chad Rosier | d052ebf | 2012-10-18 19:39:37 +0000 | [diff] [blame] | 697 | if (!OpDecl) |
| 698 | return StmtError(); |
| 699 | |
| 700 | DeclarationNameInfo NameInfo(OpDecl->getDeclName(), AsmLoc); |
| 701 | ExprResult OpExpr = BuildDeclarationNameExpr(CXXScopeSpec(), NameInfo, |
| 702 | OpDecl); |
| 703 | if (OpExpr.isInvalid()) |
| 704 | return StmtError(); |
Chad Rosier | 4d5dd7c | 2012-10-23 17:44:40 +0000 | [diff] [blame] | 705 | |
Chad Rosier | 3973f28 | 2013-01-10 22:10:16 +0000 | [diff] [blame] | 706 | // Need address of variable. |
Chad Rosier | 4d5dd7c | 2012-10-23 17:44:40 +0000 | [diff] [blame] | 707 | if (OpDecls[i].second) |
| 708 | OpExpr = BuildUnaryOp(getCurScope(), AsmLoc, clang::UO_AddrOf, |
| 709 | OpExpr.take()); |
| 710 | |
Chad Rosier | d052ebf | 2012-10-18 19:39:37 +0000 | [diff] [blame] | 711 | Names[i] = OpDecl->getIdentifier(); |
Chad Rosier | 7fd00b1 | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 712 | ConstraintRefs[i] = StringRef(Constraints[i]); |
Chad Rosier | d052ebf | 2012-10-18 19:39:37 +0000 | [diff] [blame] | 713 | Exprs[i] = OpExpr.take(); |
Chad Rosier | acc22b6 | 2012-09-06 19:35:00 +0000 | [diff] [blame] | 714 | } |
Chad Rosier | b55e602 | 2012-09-13 00:06:55 +0000 | [diff] [blame] | 715 | |
Chad Rosier | 7fd00b1 | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 716 | bool IsSimple = NumExprs > 0; |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 717 | MSAsmStmt *NS = |
Chad Rosier | 7fd00b1 | 2012-10-18 15:49:40 +0000 | [diff] [blame] | 718 | new (Context) MSAsmStmt(Context, AsmLoc, LBraceLoc, IsSimple, |
| 719 | /*IsVolatile*/ true, AsmToks, NumOutputs, NumInputs, |
| 720 | Names, ConstraintRefs, Exprs, AsmStringIR, |
| 721 | ClobberRefs, EndLoc); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 722 | return Owned(NS); |
| 723 | } |