Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 1 | //===--- SemaStmtAsm.cpp - Semantic Analysis for Statements ---------------===// |
| 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" |
| 15 | #include "clang/Sema/Scope.h" |
| 16 | #include "clang/Sema/ScopeInfo.h" |
| 17 | #include "clang/Sema/Initialization.h" |
| 18 | #include "clang/Sema/Lookup.h" |
| 19 | #include "clang/AST/TypeLoc.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | #include "clang/Basic/TargetInfo.h" |
| 22 | #include "llvm/ADT/ArrayRef.h" |
| 23 | #include "llvm/ADT/BitVector.h" |
| 24 | #include "llvm/ADT/SmallString.h" |
| 25 | #include "llvm/MC/MCAsmInfo.h" |
| 26 | #include "llvm/MC/MCContext.h" |
| 27 | #include "llvm/MC/MCInst.h" |
| 28 | #include "llvm/MC/MCInstPrinter.h" |
| 29 | #include "llvm/MC/MCInstrInfo.h" |
| 30 | #include "llvm/MC/MCObjectFileInfo.h" |
| 31 | #include "llvm/MC/MCRegisterInfo.h" |
| 32 | #include "llvm/MC/MCStreamer.h" |
| 33 | #include "llvm/MC/MCSubtargetInfo.h" |
| 34 | #include "llvm/MC/MCTargetAsmParser.h" |
| 35 | #include "llvm/MC/MCParser/MCAsmLexer.h" |
| 36 | #include "llvm/MC/MCParser/MCAsmParser.h" |
| 37 | #include "llvm/Support/SourceMgr.h" |
| 38 | #include "llvm/Support/TargetRegistry.h" |
| 39 | #include "llvm/Support/TargetSelect.h" |
| 40 | using namespace clang; |
| 41 | using namespace sema; |
| 42 | |
| 43 | /// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently |
| 44 | /// ignore "noop" casts in places where an lvalue is required by an inline asm. |
| 45 | /// We emulate this behavior when -fheinous-gnu-extensions is specified, but |
| 46 | /// provide a strong guidance to not use it. |
| 47 | /// |
| 48 | /// This method checks to see if the argument is an acceptable l-value and |
| 49 | /// returns false if it is a case we can handle. |
| 50 | static bool CheckAsmLValue(const Expr *E, Sema &S) { |
| 51 | // Type dependent expressions will be checked during instantiation. |
| 52 | if (E->isTypeDependent()) |
| 53 | return false; |
| 54 | |
| 55 | if (E->isLValue()) |
| 56 | return false; // Cool, this is an lvalue. |
| 57 | |
| 58 | // Okay, this is not an lvalue, but perhaps it is the result of a cast that we |
| 59 | // are supposed to allow. |
| 60 | const Expr *E2 = E->IgnoreParenNoopCasts(S.Context); |
| 61 | if (E != E2 && E2->isLValue()) { |
| 62 | if (!S.getLangOpts().HeinousExtensions) |
| 63 | S.Diag(E2->getLocStart(), diag::err_invalid_asm_cast_lvalue) |
| 64 | << E->getSourceRange(); |
| 65 | else |
| 66 | S.Diag(E2->getLocStart(), diag::warn_invalid_asm_cast_lvalue) |
| 67 | << E->getSourceRange(); |
| 68 | // Accept, even if we emitted an error diagnostic. |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | // None of the above, just randomly invalid non-lvalue. |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | /// isOperandMentioned - Return true if the specified operand # is mentioned |
| 77 | /// anywhere in the decomposed asm string. |
| 78 | static bool isOperandMentioned(unsigned OpNo, |
| 79 | ArrayRef<AsmStmt::AsmStringPiece> AsmStrPieces) { |
| 80 | for (unsigned p = 0, e = AsmStrPieces.size(); p != e; ++p) { |
| 81 | const AsmStmt::AsmStringPiece &Piece = AsmStrPieces[p]; |
| 82 | if (!Piece.isOperand()) continue; |
| 83 | |
| 84 | // If this is a reference to the input and if the input was the smaller |
| 85 | // one, then we have to reject this asm. |
| 86 | if (Piece.getOperandNo() == OpNo) |
| 87 | return true; |
| 88 | } |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | StmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc, bool IsSimple, |
| 93 | bool IsVolatile, unsigned NumOutputs, |
| 94 | unsigned NumInputs, IdentifierInfo **Names, |
| 95 | MultiExprArg constraints, MultiExprArg exprs, |
| 96 | Expr *asmString, MultiExprArg clobbers, |
| 97 | SourceLocation RParenLoc, bool MSAsm) { |
| 98 | unsigned NumClobbers = clobbers.size(); |
| 99 | StringLiteral **Constraints = |
| 100 | reinterpret_cast<StringLiteral**>(constraints.get()); |
| 101 | Expr **Exprs = exprs.get(); |
| 102 | StringLiteral *AsmString = cast<StringLiteral>(asmString); |
| 103 | StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.get()); |
| 104 | |
| 105 | SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos; |
| 106 | |
| 107 | // The parser verifies that there is a string literal here. |
| 108 | if (!AsmString->isAscii()) |
| 109 | return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character) |
| 110 | << AsmString->getSourceRange()); |
| 111 | |
| 112 | for (unsigned i = 0; i != NumOutputs; i++) { |
| 113 | StringLiteral *Literal = Constraints[i]; |
| 114 | if (!Literal->isAscii()) |
| 115 | return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) |
| 116 | << Literal->getSourceRange()); |
| 117 | |
| 118 | StringRef OutputName; |
| 119 | if (Names[i]) |
| 120 | OutputName = Names[i]->getName(); |
| 121 | |
| 122 | TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName); |
| 123 | if (!Context.getTargetInfo().validateOutputConstraint(Info)) |
| 124 | return StmtError(Diag(Literal->getLocStart(), |
| 125 | diag::err_asm_invalid_output_constraint) |
| 126 | << Info.getConstraintStr()); |
| 127 | |
| 128 | // Check that the output exprs are valid lvalues. |
| 129 | Expr *OutputExpr = Exprs[i]; |
| 130 | if (CheckAsmLValue(OutputExpr, *this)) { |
| 131 | return StmtError(Diag(OutputExpr->getLocStart(), |
| 132 | diag::err_asm_invalid_lvalue_in_output) |
| 133 | << OutputExpr->getSourceRange()); |
| 134 | } |
| 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); |
| 185 | } |
| 186 | |
| 187 | // Check that the clobbers are valid. |
| 188 | for (unsigned i = 0; i != NumClobbers; i++) { |
| 189 | StringLiteral *Literal = Clobbers[i]; |
| 190 | if (!Literal->isAscii()) |
| 191 | return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) |
| 192 | << Literal->getSourceRange()); |
| 193 | |
| 194 | StringRef Clobber = Literal->getString(); |
| 195 | |
| 196 | if (!Context.getTargetInfo().isValidClobber(Clobber)) |
| 197 | return StmtError(Diag(Literal->getLocStart(), |
| 198 | diag::err_asm_unknown_register_name) << Clobber); |
| 199 | } |
| 200 | |
| 201 | AsmStmt *NS = |
| 202 | new (Context) AsmStmt(Context, AsmLoc, IsSimple, IsVolatile, MSAsm, |
| 203 | NumOutputs, NumInputs, Names, Constraints, Exprs, |
| 204 | AsmString, NumClobbers, Clobbers, RParenLoc); |
| 205 | // Validate the asm string, ensuring it makes sense given the operands we |
| 206 | // have. |
| 207 | SmallVector<AsmStmt::AsmStringPiece, 8> Pieces; |
| 208 | unsigned DiagOffs; |
| 209 | if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) { |
| 210 | Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID) |
| 211 | << AsmString->getSourceRange(); |
| 212 | return StmtError(); |
| 213 | } |
| 214 | |
| 215 | // Validate tied input operands for type mismatches. |
| 216 | for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) { |
| 217 | TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i]; |
| 218 | |
| 219 | // If this is a tied constraint, verify that the output and input have |
| 220 | // either exactly the same type, or that they are int/ptr operands with the |
| 221 | // same size (int/long, int*/long, are ok etc). |
| 222 | if (!Info.hasTiedOperand()) continue; |
| 223 | |
| 224 | unsigned TiedTo = Info.getTiedOperand(); |
| 225 | unsigned InputOpNo = i+NumOutputs; |
| 226 | Expr *OutputExpr = Exprs[TiedTo]; |
| 227 | Expr *InputExpr = Exprs[InputOpNo]; |
| 228 | |
| 229 | if (OutputExpr->isTypeDependent() || InputExpr->isTypeDependent()) |
| 230 | continue; |
| 231 | |
| 232 | QualType InTy = InputExpr->getType(); |
| 233 | QualType OutTy = OutputExpr->getType(); |
| 234 | if (Context.hasSameType(InTy, OutTy)) |
| 235 | continue; // All types can be tied to themselves. |
| 236 | |
| 237 | // Decide if the input and output are in the same domain (integer/ptr or |
| 238 | // floating point. |
| 239 | enum AsmDomain { |
| 240 | AD_Int, AD_FP, AD_Other |
| 241 | } InputDomain, OutputDomain; |
| 242 | |
| 243 | if (InTy->isIntegerType() || InTy->isPointerType()) |
| 244 | InputDomain = AD_Int; |
| 245 | else if (InTy->isRealFloatingType()) |
| 246 | InputDomain = AD_FP; |
| 247 | else |
| 248 | InputDomain = AD_Other; |
| 249 | |
| 250 | if (OutTy->isIntegerType() || OutTy->isPointerType()) |
| 251 | OutputDomain = AD_Int; |
| 252 | else if (OutTy->isRealFloatingType()) |
| 253 | OutputDomain = AD_FP; |
| 254 | else |
| 255 | OutputDomain = AD_Other; |
| 256 | |
| 257 | // They are ok if they are the same size and in the same domain. This |
| 258 | // allows tying things like: |
| 259 | // void* to int* |
| 260 | // void* to int if they are the same size. |
| 261 | // double to long double if they are the same size. |
| 262 | // |
| 263 | uint64_t OutSize = Context.getTypeSize(OutTy); |
| 264 | uint64_t InSize = Context.getTypeSize(InTy); |
| 265 | if (OutSize == InSize && InputDomain == OutputDomain && |
| 266 | InputDomain != AD_Other) |
| 267 | continue; |
| 268 | |
| 269 | // If the smaller input/output operand is not mentioned in the asm string, |
| 270 | // then we can promote the smaller one to a larger input and the asm string |
| 271 | // won't notice. |
| 272 | bool SmallerValueMentioned = false; |
| 273 | |
| 274 | // If this is a reference to the input and if the input was the smaller |
| 275 | // one, then we have to reject this asm. |
| 276 | if (isOperandMentioned(InputOpNo, Pieces)) { |
| 277 | // This is a use in the asm string of the smaller operand. Since we |
| 278 | // codegen this by promoting to a wider value, the asm will get printed |
| 279 | // "wrong". |
| 280 | SmallerValueMentioned |= InSize < OutSize; |
| 281 | } |
| 282 | if (isOperandMentioned(TiedTo, Pieces)) { |
| 283 | // If this is a reference to the output, and if the output is the larger |
| 284 | // value, then it's ok because we'll promote the input to the larger type. |
| 285 | SmallerValueMentioned |= OutSize < InSize; |
| 286 | } |
| 287 | |
| 288 | // If the smaller value wasn't mentioned in the asm string, and if the |
| 289 | // output was a register, just extend the shorter one to the size of the |
| 290 | // larger one. |
| 291 | if (!SmallerValueMentioned && InputDomain != AD_Other && |
| 292 | OutputConstraintInfos[TiedTo].allowsRegister()) |
| 293 | continue; |
| 294 | |
| 295 | // Either both of the operands were mentioned or the smaller one was |
| 296 | // mentioned. One more special case that we'll allow: if the tied input is |
| 297 | // integer, unmentioned, and is a constant, then we'll allow truncating it |
| 298 | // down to the size of the destination. |
| 299 | if (InputDomain == AD_Int && OutputDomain == AD_Int && |
| 300 | !isOperandMentioned(InputOpNo, Pieces) && |
| 301 | InputExpr->isEvaluatable(Context)) { |
| 302 | CastKind castKind = |
| 303 | (OutTy->isBooleanType() ? CK_IntegralToBoolean : CK_IntegralCast); |
| 304 | InputExpr = ImpCastExprToType(InputExpr, OutTy, castKind).take(); |
| 305 | Exprs[InputOpNo] = InputExpr; |
| 306 | NS->setInputExpr(i, InputExpr); |
| 307 | continue; |
| 308 | } |
| 309 | |
| 310 | Diag(InputExpr->getLocStart(), |
| 311 | diag::err_asm_tying_incompatible_types) |
| 312 | << InTy << OutTy << OutputExpr->getSourceRange() |
| 313 | << InputExpr->getSourceRange(); |
| 314 | return StmtError(); |
| 315 | } |
| 316 | |
| 317 | return Owned(NS); |
| 318 | } |
| 319 | |
| 320 | // isMSAsmKeyword - Return true if this is an MS-style inline asm keyword. These |
| 321 | // require special handling. |
| 322 | static bool isMSAsmKeyword(StringRef Name) { |
| 323 | bool Ret = llvm::StringSwitch<bool>(Name) |
| 324 | .Cases("EVEN", "ALIGN", true) // Alignment directives. |
| 325 | .Cases("LENGTH", "SIZE", "TYPE", true) // Type and variable sizes. |
| 326 | .Case("_emit", true) // _emit Pseudoinstruction. |
| 327 | .Default(false); |
| 328 | return Ret; |
| 329 | } |
| 330 | |
| 331 | static StringRef getSpelling(Sema &SemaRef, Token AsmTok) { |
| 332 | StringRef Asm; |
| 333 | SmallString<512> TokenBuf; |
| 334 | TokenBuf.resize(512); |
| 335 | bool StringInvalid = false; |
| 336 | Asm = SemaRef.PP.getSpelling(AsmTok, TokenBuf, &StringInvalid); |
| 337 | assert (!StringInvalid && "Expected valid string!"); |
| 338 | return Asm; |
| 339 | } |
| 340 | |
| 341 | static void patchMSAsmStrings(Sema &SemaRef, bool &IsSimple, |
| 342 | SourceLocation AsmLoc, |
| 343 | ArrayRef<Token> AsmToks, |
| 344 | const TargetInfo &TI, |
| 345 | std::vector<llvm::BitVector> &AsmRegs, |
| 346 | std::vector<llvm::BitVector> &AsmNames, |
| 347 | std::vector<std::string> &AsmStrings) { |
| 348 | assert (!AsmToks.empty() && "Didn't expect an empty AsmToks!"); |
| 349 | |
| 350 | // Assume simple asm stmt until we parse a non-register identifer (or we just |
| 351 | // need to bail gracefully). |
| 352 | IsSimple = true; |
| 353 | |
| 354 | SmallString<512> Asm; |
| 355 | unsigned NumAsmStrings = 0; |
| 356 | for (unsigned i = 0, e = AsmToks.size(); i != e; ++i) { |
| 357 | |
| 358 | // Determine if this should be considered a new asm. |
| 359 | bool isNewAsm = i == 0 || AsmToks[i].isAtStartOfLine() || |
| 360 | AsmToks[i].is(tok::kw_asm); |
| 361 | |
| 362 | // Emit the previous asm string. |
| 363 | if (i && isNewAsm) { |
| 364 | AsmStrings[NumAsmStrings++] = Asm.c_str(); |
| 365 | if (AsmToks[i].is(tok::kw_asm)) { |
| 366 | ++i; // Skip __asm |
| 367 | assert (i != e && "Expected another token."); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | // Start a new asm string with the opcode. |
| 372 | if (isNewAsm) { |
| 373 | AsmRegs[NumAsmStrings].resize(AsmToks.size()); |
| 374 | AsmNames[NumAsmStrings].resize(AsmToks.size()); |
| 375 | |
| 376 | StringRef Piece = AsmToks[i].getIdentifierInfo()->getName(); |
| 377 | // MS-style inline asm keywords require special handling. |
| 378 | if (isMSAsmKeyword(Piece)) |
| 379 | IsSimple = false; |
| 380 | |
| 381 | // TODO: Verify this is a valid opcode. |
| 382 | Asm = Piece; |
| 383 | continue; |
| 384 | } |
| 385 | |
| 386 | if (i && AsmToks[i].hasLeadingSpace()) |
| 387 | Asm += ' '; |
| 388 | |
| 389 | // Check the operand(s). |
| 390 | switch (AsmToks[i].getKind()) { |
| 391 | default: |
| 392 | IsSimple = false; |
| 393 | Asm += getSpelling(SemaRef, AsmToks[i]); |
| 394 | break; |
| 395 | case tok::comma: Asm += ","; break; |
| 396 | case tok::colon: Asm += ":"; break; |
| 397 | case tok::l_square: Asm += "["; break; |
| 398 | case tok::r_square: Asm += "]"; break; |
| 399 | case tok::l_brace: Asm += "{"; break; |
| 400 | case tok::r_brace: Asm += "}"; break; |
| 401 | case tok::numeric_constant: |
| 402 | Asm += getSpelling(SemaRef, AsmToks[i]); |
| 403 | break; |
| 404 | case tok::identifier: { |
| 405 | IdentifierInfo *II = AsmToks[i].getIdentifierInfo(); |
| 406 | StringRef Name = II->getName(); |
| 407 | |
| 408 | // Valid register? |
| 409 | if (TI.isValidGCCRegisterName(Name)) { |
| 410 | AsmRegs[NumAsmStrings].set(i); |
| 411 | Asm += Name; |
| 412 | break; |
| 413 | } |
| 414 | |
| 415 | IsSimple = false; |
| 416 | |
| 417 | // MS-style inline asm keywords require special handling. |
| 418 | if (isMSAsmKeyword(Name)) { |
| 419 | IsSimple = false; |
| 420 | Asm += Name; |
| 421 | break; |
| 422 | } |
| 423 | |
| 424 | // FIXME: Why are we missing this segment register? |
| 425 | if (Name == "fs") { |
| 426 | Asm += Name; |
| 427 | break; |
| 428 | } |
| 429 | |
| 430 | // Lookup the identifier. |
| 431 | // TODO: Someone with more experience with clang should verify this the |
| 432 | // proper way of doing a symbol lookup. |
| 433 | DeclarationName DeclName(II); |
| 434 | Scope *CurScope = SemaRef.getCurScope(); |
| 435 | LookupResult R(SemaRef, DeclName, AsmLoc, Sema::LookupOrdinaryName); |
| 436 | if (!SemaRef.LookupName(R, CurScope, false/*AllowBuiltinCreation*/)) |
| 437 | break; |
| 438 | |
| 439 | assert (R.isSingleResult() && "Expected a single result?!"); |
| 440 | NamedDecl *Decl = R.getFoundDecl(); |
| 441 | switch (Decl->getKind()) { |
| 442 | default: |
| 443 | assert(0 && "Unknown decl kind."); |
| 444 | break; |
| 445 | case Decl::Var: { |
| 446 | case Decl::ParmVar: |
| 447 | AsmNames[NumAsmStrings].set(i); |
| 448 | |
| 449 | VarDecl *Var = cast<VarDecl>(Decl); |
| 450 | QualType Ty = Var->getType(); |
| 451 | (void)Ty; // Avoid warning. |
| 452 | // TODO: Patch identifier with valid operand. One potential idea is to |
| 453 | // probe the backend with type information to guess the possible |
| 454 | // operand. |
| 455 | break; |
| 456 | } |
| 457 | } |
| 458 | break; |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | // Emit the final (and possibly only) asm string. |
| 464 | AsmStrings[NumAsmStrings] = Asm.c_str(); |
| 465 | } |
| 466 | |
| 467 | // Build the unmodified MSAsmString. |
| 468 | static std::string buildMSAsmString(Sema &SemaRef, |
| 469 | ArrayRef<Token> AsmToks, |
| 470 | unsigned &NumAsmStrings) { |
| 471 | assert (!AsmToks.empty() && "Didn't expect an empty AsmToks!"); |
| 472 | NumAsmStrings = 0; |
| 473 | |
| 474 | SmallString<512> Asm; |
| 475 | for (unsigned i = 0, e = AsmToks.size(); i < e; ++i) { |
| 476 | bool isNewAsm = i == 0 || AsmToks[i].isAtStartOfLine() || |
| 477 | AsmToks[i].is(tok::kw_asm); |
| 478 | |
| 479 | if (isNewAsm) { |
| 480 | ++NumAsmStrings; |
| 481 | if (i) |
| 482 | Asm += '\n'; |
| 483 | if (AsmToks[i].is(tok::kw_asm)) { |
| 484 | i++; // Skip __asm |
| 485 | assert (i != e && "Expected another token"); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | if (i && AsmToks[i].hasLeadingSpace() && !isNewAsm) |
| 490 | Asm += ' '; |
| 491 | |
| 492 | Asm += getSpelling(SemaRef, AsmToks[i]); |
| 493 | } |
| 494 | return Asm.c_str(); |
| 495 | } |
| 496 | |
| 497 | StmtResult Sema::ActOnMSAsmStmt(SourceLocation AsmLoc, |
| 498 | SourceLocation LBraceLoc, |
| 499 | ArrayRef<Token> AsmToks, |
| 500 | SourceLocation EndLoc) { |
| 501 | // MS-style inline assembly is not fully supported, so emit a warning. |
| 502 | Diag(AsmLoc, diag::warn_unsupported_msasm); |
| 503 | SmallVector<StringRef,4> Clobbers; |
| 504 | std::set<std::string> ClobberRegs; |
| 505 | SmallVector<IdentifierInfo*, 4> Inputs; |
| 506 | SmallVector<IdentifierInfo*, 4> Outputs; |
| 507 | |
| 508 | // Empty asm statements don't need to instantiate the AsmParser, etc. |
| 509 | if (AsmToks.empty()) { |
| 510 | StringRef AsmString; |
| 511 | MSAsmStmt *NS = |
| 512 | new (Context) MSAsmStmt(Context, AsmLoc, LBraceLoc, /*IsSimple*/ true, |
| 513 | /*IsVolatile*/ true, AsmToks, Inputs, Outputs, |
| 514 | AsmString, Clobbers, EndLoc); |
| 515 | return Owned(NS); |
| 516 | } |
| 517 | |
| 518 | unsigned NumAsmStrings; |
| 519 | std::string AsmString = buildMSAsmString(*this, AsmToks, NumAsmStrings); |
| 520 | |
| 521 | bool IsSimple; |
| 522 | std::vector<llvm::BitVector> Regs; |
| 523 | std::vector<llvm::BitVector> Names; |
| 524 | std::vector<std::string> PatchedAsmStrings; |
| 525 | |
| 526 | Regs.resize(NumAsmStrings); |
| 527 | Names.resize(NumAsmStrings); |
| 528 | PatchedAsmStrings.resize(NumAsmStrings); |
| 529 | |
| 530 | // Rewrite operands to appease the AsmParser. |
| 531 | patchMSAsmStrings(*this, IsSimple, AsmLoc, AsmToks, |
| 532 | Context.getTargetInfo(), Regs, Names, PatchedAsmStrings); |
| 533 | |
| 534 | // patchMSAsmStrings doesn't correctly patch non-simple asm statements. |
| 535 | if (!IsSimple) { |
| 536 | MSAsmStmt *NS = |
| 537 | new (Context) MSAsmStmt(Context, AsmLoc, LBraceLoc, /*IsSimple*/ true, |
| 538 | /*IsVolatile*/ true, AsmToks, Inputs, Outputs, |
| 539 | AsmString, Clobbers, EndLoc); |
| 540 | return Owned(NS); |
| 541 | } |
| 542 | |
| 543 | // Initialize targets and assembly printers/parsers. |
| 544 | llvm::InitializeAllTargetInfos(); |
| 545 | llvm::InitializeAllTargetMCs(); |
| 546 | llvm::InitializeAllAsmParsers(); |
| 547 | |
| 548 | // Get the target specific parser. |
| 549 | std::string Error; |
| 550 | const std::string &TT = Context.getTargetInfo().getTriple().getTriple(); |
| 551 | const llvm::Target *TheTarget(llvm::TargetRegistry::lookupTarget(TT, Error)); |
| 552 | |
| 553 | OwningPtr<llvm::MCAsmInfo> MAI(TheTarget->createMCAsmInfo(TT)); |
| 554 | OwningPtr<llvm::MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TT)); |
| 555 | OwningPtr<llvm::MCObjectFileInfo> MOFI(new llvm::MCObjectFileInfo()); |
| 556 | OwningPtr<llvm::MCSubtargetInfo> |
| 557 | STI(TheTarget->createMCSubtargetInfo(TT, "", "")); |
| 558 | |
| 559 | for (unsigned i = 0, e = PatchedAsmStrings.size(); i != e; ++i) { |
| 560 | llvm::SourceMgr SrcMgr; |
| 561 | llvm::MCContext Ctx(*MAI, *MRI, MOFI.get(), &SrcMgr); |
| 562 | llvm::MemoryBuffer *Buffer = |
| 563 | llvm::MemoryBuffer::getMemBuffer(PatchedAsmStrings[i], "<inline asm>"); |
| 564 | |
| 565 | // Tell SrcMgr about this buffer, which is what the parser will pick up. |
| 566 | SrcMgr.AddNewSourceBuffer(Buffer, llvm::SMLoc()); |
| 567 | |
| 568 | OwningPtr<llvm::MCStreamer> Str(createNullStreamer(Ctx)); |
| 569 | OwningPtr<llvm::MCAsmParser> |
| 570 | Parser(createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI)); |
| 571 | OwningPtr<llvm::MCTargetAsmParser> |
| 572 | TargetParser(TheTarget->createMCAsmParser(*STI, *Parser)); |
| 573 | // Change to the Intel dialect. |
| 574 | Parser->setAssemblerDialect(1); |
| 575 | Parser->setTargetParser(*TargetParser.get()); |
| 576 | |
| 577 | // Prime the lexer. |
| 578 | Parser->Lex(); |
| 579 | |
| 580 | // Parse the opcode. |
| 581 | StringRef IDVal; |
| 582 | Parser->ParseIdentifier(IDVal); |
| 583 | |
| 584 | // Canonicalize the opcode to lower case. |
| 585 | SmallString<128> Opcode; |
| 586 | for (unsigned i = 0, e = IDVal.size(); i != e; ++i) |
| 587 | Opcode.push_back(tolower(IDVal[i])); |
| 588 | |
| 589 | // Parse the operands. |
| 590 | llvm::SMLoc IDLoc; |
| 591 | SmallVector<llvm::MCParsedAsmOperand*, 8> Operands; |
| 592 | bool HadError = TargetParser->ParseInstruction(Opcode.str(), IDLoc, |
| 593 | Operands); |
| 594 | assert (!HadError && "Unexpected error parsing instruction"); |
| 595 | |
| 596 | // Match the MCInstr. |
| 597 | SmallVector<llvm::MCInst, 2> Instrs; |
| 598 | HadError = TargetParser->MatchInstruction(IDLoc, Operands, Instrs); |
| 599 | assert (!HadError && "Unexpected error matching instruction"); |
| 600 | assert ((Instrs.size() == 1) && "Expected only a single instruction."); |
| 601 | |
| 602 | // Get the instruction descriptor. |
| 603 | llvm::MCInst Inst = Instrs[0]; |
| 604 | const llvm::MCInstrInfo *MII = TheTarget->createMCInstrInfo(); |
| 605 | const llvm::MCInstrDesc &Desc = MII->get(Inst.getOpcode()); |
| 606 | llvm::MCInstPrinter *IP = |
| 607 | TheTarget->createMCInstPrinter(1, *MAI, *MII, *MRI, *STI); |
| 608 | |
| 609 | // Build the list of clobbers. |
| 610 | for (unsigned i = 0, e = Desc.getNumDefs(); i != e; ++i) { |
| 611 | const llvm::MCOperand &Op = Inst.getOperand(i); |
| 612 | if (!Op.isReg()) |
| 613 | continue; |
| 614 | |
| 615 | std::string Reg; |
| 616 | llvm::raw_string_ostream OS(Reg); |
| 617 | IP->printRegName(OS, Op.getReg()); |
| 618 | |
| 619 | StringRef Clobber(OS.str()); |
| 620 | if (!Context.getTargetInfo().isValidClobber(Clobber)) |
| 621 | return StmtError(Diag(AsmLoc, diag::err_asm_unknown_register_name) << |
| 622 | Clobber); |
| 623 | ClobberRegs.insert(Reg); |
| 624 | } |
| 625 | } |
| 626 | for (std::set<std::string>::iterator I = ClobberRegs.begin(), |
| 627 | E = ClobberRegs.end(); I != E; ++I) |
| 628 | Clobbers.push_back(*I); |
| 629 | |
| 630 | MSAsmStmt *NS = |
| 631 | new (Context) MSAsmStmt(Context, AsmLoc, LBraceLoc, IsSimple, |
| 632 | /*IsVolatile*/ true, AsmToks, Inputs, Outputs, |
| 633 | AsmString, Clobbers, EndLoc); |
| 634 | return Owned(NS); |
| 635 | } |