1 //===--- SemaStmtAsm.cpp - Semantic Analysis for Asm 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/MCExpr.h" 28 #include "llvm/MC/MCInst.h" 29 #include "llvm/MC/MCInstPrinter.h" 30 #include "llvm/MC/MCInstrInfo.h" 31 #include "llvm/MC/MCObjectFileInfo.h" 32 #include "llvm/MC/MCRegisterInfo.h" 33 #include "llvm/MC/MCStreamer.h" 34 #include "llvm/MC/MCSubtargetInfo.h" 35 #include "llvm/MC/MCSymbol.h" 36 #include "llvm/MC/MCTargetAsmParser.h" 37 #include "llvm/MC/MCParser/MCAsmLexer.h" 38 #include "llvm/MC/MCParser/MCAsmParser.h" 39 #include "llvm/MC/MCParser/MCParsedAsmOperand.h" 40 #include "llvm/Support/SourceMgr.h" 41 #include "llvm/Support/TargetRegistry.h" 42 #include "llvm/Support/TargetSelect.h" 43 using namespace clang; 44 using namespace sema; 45 46 /// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently 47 /// ignore "noop" casts in places where an lvalue is required by an inline asm. 48 /// We emulate this behavior when -fheinous-gnu-extensions is specified, but 49 /// provide a strong guidance to not use it. 50 /// 51 /// This method checks to see if the argument is an acceptable l-value and 52 /// returns false if it is a case we can handle. 53 static bool CheckAsmLValue(const Expr *E, Sema &S) { 54 // Type dependent expressions will be checked during instantiation. 55 if (E->isTypeDependent()) 56 return false; 57 58 if (E->isLValue()) 59 return false; // Cool, this is an lvalue. 60 61 // Okay, this is not an lvalue, but perhaps it is the result of a cast that we 62 // are supposed to allow. 63 const Expr *E2 = E->IgnoreParenNoopCasts(S.Context); 64 if (E != E2 && E2->isLValue()) { 65 if (!S.getLangOpts().HeinousExtensions) 66 S.Diag(E2->getLocStart(), diag::err_invalid_asm_cast_lvalue) 67 << E->getSourceRange(); 68 else 69 S.Diag(E2->getLocStart(), diag::warn_invalid_asm_cast_lvalue) 70 << E->getSourceRange(); 71 // Accept, even if we emitted an error diagnostic. 72 return false; 73 } 74 75 // None of the above, just randomly invalid non-lvalue. 76 return true; 77 } 78 79 /// isOperandMentioned - Return true if the specified operand # is mentioned 80 /// anywhere in the decomposed asm string. 81 static bool isOperandMentioned(unsigned OpNo, 82 ArrayRef<GCCAsmStmt::AsmStringPiece> AsmStrPieces) { 83 for (unsigned p = 0, e = AsmStrPieces.size(); p != e; ++p) { 84 const GCCAsmStmt::AsmStringPiece &Piece = AsmStrPieces[p]; 85 if (!Piece.isOperand()) continue; 86 87 // If this is a reference to the input and if the input was the smaller 88 // one, then we have to reject this asm. 89 if (Piece.getOperandNo() == OpNo) 90 return true; 91 } 92 return false; 93 } 94 95 StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple, 96 bool IsVolatile, unsigned NumOutputs, 97 unsigned NumInputs, IdentifierInfo **Names, 98 MultiExprArg constraints, MultiExprArg exprs, 99 Expr *asmString, MultiExprArg clobbers, 100 SourceLocation RParenLoc) { 101 unsigned NumClobbers = clobbers.size(); 102 StringLiteral **Constraints = 103 reinterpret_cast<StringLiteral**>(constraints.data()); 104 Expr **Exprs = exprs.data(); 105 StringLiteral *AsmString = cast<StringLiteral>(asmString); 106 StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.data()); 107 108 SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos; 109 110 // The parser verifies that there is a string literal here. 111 if (!AsmString->isAscii()) 112 return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character) 113 << AsmString->getSourceRange()); 114 115 for (unsigned i = 0; i != NumOutputs; i++) { 116 StringLiteral *Literal = Constraints[i]; 117 if (!Literal->isAscii()) 118 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) 119 << Literal->getSourceRange()); 120 121 StringRef OutputName; 122 if (Names[i]) 123 OutputName = Names[i]->getName(); 124 125 TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName); 126 if (!Context.getTargetInfo().validateOutputConstraint(Info)) 127 return StmtError(Diag(Literal->getLocStart(), 128 diag::err_asm_invalid_output_constraint) 129 << Info.getConstraintStr()); 130 131 // Check that the output exprs are valid lvalues. 132 Expr *OutputExpr = Exprs[i]; 133 if (CheckAsmLValue(OutputExpr, *this)) { 134 return StmtError(Diag(OutputExpr->getLocStart(), 135 diag::err_asm_invalid_lvalue_in_output) 136 << OutputExpr->getSourceRange()); 137 } 138 139 OutputConstraintInfos.push_back(Info); 140 } 141 142 SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos; 143 144 for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) { 145 StringLiteral *Literal = Constraints[i]; 146 if (!Literal->isAscii()) 147 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) 148 << Literal->getSourceRange()); 149 150 StringRef InputName; 151 if (Names[i]) 152 InputName = Names[i]->getName(); 153 154 TargetInfo::ConstraintInfo Info(Literal->getString(), InputName); 155 if (!Context.getTargetInfo().validateInputConstraint(OutputConstraintInfos.data(), 156 NumOutputs, Info)) { 157 return StmtError(Diag(Literal->getLocStart(), 158 diag::err_asm_invalid_input_constraint) 159 << Info.getConstraintStr()); 160 } 161 162 Expr *InputExpr = Exprs[i]; 163 164 // Only allow void types for memory constraints. 165 if (Info.allowsMemory() && !Info.allowsRegister()) { 166 if (CheckAsmLValue(InputExpr, *this)) 167 return StmtError(Diag(InputExpr->getLocStart(), 168 diag::err_asm_invalid_lvalue_in_input) 169 << Info.getConstraintStr() 170 << InputExpr->getSourceRange()); 171 } 172 173 if (Info.allowsRegister()) { 174 if (InputExpr->getType()->isVoidType()) { 175 return StmtError(Diag(InputExpr->getLocStart(), 176 diag::err_asm_invalid_type_in_input) 177 << InputExpr->getType() << Info.getConstraintStr() 178 << InputExpr->getSourceRange()); 179 } 180 } 181 182 ExprResult Result = DefaultFunctionArrayLvalueConversion(Exprs[i]); 183 if (Result.isInvalid()) 184 return StmtError(); 185 186 Exprs[i] = Result.take(); 187 InputConstraintInfos.push_back(Info); 188 } 189 190 // Check that the clobbers are valid. 191 for (unsigned i = 0; i != NumClobbers; i++) { 192 StringLiteral *Literal = Clobbers[i]; 193 if (!Literal->isAscii()) 194 return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) 195 << Literal->getSourceRange()); 196 197 StringRef Clobber = Literal->getString(); 198 199 if (!Context.getTargetInfo().isValidClobber(Clobber)) 200 return StmtError(Diag(Literal->getLocStart(), 201 diag::err_asm_unknown_register_name) << Clobber); 202 } 203 204 GCCAsmStmt *NS = 205 new (Context) GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs, 206 NumInputs, Names, Constraints, Exprs, AsmString, 207 NumClobbers, Clobbers, RParenLoc); 208 // Validate the asm string, ensuring it makes sense given the operands we 209 // have. 210 SmallVector<GCCAsmStmt::AsmStringPiece, 8> Pieces; 211 unsigned DiagOffs; 212 if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) { 213 Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID) 214 << AsmString->getSourceRange(); 215 return StmtError(); 216 } 217 218 // Validate tied input operands for type mismatches. 219 for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) { 220 TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i]; 221 222 // If this is a tied constraint, verify that the output and input have 223 // either exactly the same type, or that they are int/ptr operands with the 224 // same size (int/long, int*/long, are ok etc). 225 if (!Info.hasTiedOperand()) continue; 226 227 unsigned TiedTo = Info.getTiedOperand(); 228 unsigned InputOpNo = i+NumOutputs; 229 Expr *OutputExpr = Exprs[TiedTo]; 230 Expr *InputExpr = Exprs[InputOpNo]; 231 232 if (OutputExpr->isTypeDependent() || InputExpr->isTypeDependent()) 233 continue; 234 235 QualType InTy = InputExpr->getType(); 236 QualType OutTy = OutputExpr->getType(); 237 if (Context.hasSameType(InTy, OutTy)) 238 continue; // All types can be tied to themselves. 239 240 // Decide if the input and output are in the same domain (integer/ptr or 241 // floating point. 242 enum AsmDomain { 243 AD_Int, AD_FP, AD_Other 244 } InputDomain, OutputDomain; 245 246 if (InTy->isIntegerType() || InTy->isPointerType()) 247 InputDomain = AD_Int; 248 else if (InTy->isRealFloatingType()) 249 InputDomain = AD_FP; 250 else 251 InputDomain = AD_Other; 252 253 if (OutTy->isIntegerType() || OutTy->isPointerType()) 254 OutputDomain = AD_Int; 255 else if (OutTy->isRealFloatingType()) 256 OutputDomain = AD_FP; 257 else 258 OutputDomain = AD_Other; 259 260 // They are ok if they are the same size and in the same domain. This 261 // allows tying things like: 262 // void* to int* 263 // void* to int if they are the same size. 264 // double to long double if they are the same size. 265 // 266 uint64_t OutSize = Context.getTypeSize(OutTy); 267 uint64_t InSize = Context.getTypeSize(InTy); 268 if (OutSize == InSize && InputDomain == OutputDomain && 269 InputDomain != AD_Other) 270 continue; 271 272 // If the smaller input/output operand is not mentioned in the asm string, 273 // then we can promote the smaller one to a larger input and the asm string 274 // won't notice. 275 bool SmallerValueMentioned = false; 276 277 // If this is a reference to the input and if the input was the smaller 278 // one, then we have to reject this asm. 279 if (isOperandMentioned(InputOpNo, Pieces)) { 280 // This is a use in the asm string of the smaller operand. Since we 281 // codegen this by promoting to a wider value, the asm will get printed 282 // "wrong". 283 SmallerValueMentioned |= InSize < OutSize; 284 } 285 if (isOperandMentioned(TiedTo, Pieces)) { 286 // If this is a reference to the output, and if the output is the larger 287 // value, then it's ok because we'll promote the input to the larger type. 288 SmallerValueMentioned |= OutSize < InSize; 289 } 290 291 // If the smaller value wasn't mentioned in the asm string, and if the 292 // output was a register, just extend the shorter one to the size of the 293 // larger one. 294 if (!SmallerValueMentioned && InputDomain != AD_Other && 295 OutputConstraintInfos[TiedTo].allowsRegister()) 296 continue; 297 298 // Either both of the operands were mentioned or the smaller one was 299 // mentioned. One more special case that we'll allow: if the tied input is 300 // integer, unmentioned, and is a constant, then we'll allow truncating it 301 // down to the size of the destination. 302 if (InputDomain == AD_Int && OutputDomain == AD_Int && 303 !isOperandMentioned(InputOpNo, Pieces) && 304 InputExpr->isEvaluatable(Context)) { 305 CastKind castKind = 306 (OutTy->isBooleanType() ? CK_IntegralToBoolean : CK_IntegralCast); 307 InputExpr = ImpCastExprToType(InputExpr, OutTy, castKind).take(); 308 Exprs[InputOpNo] = InputExpr; 309 NS->setInputExpr(i, InputExpr); 310 continue; 311 } 312 313 Diag(InputExpr->getLocStart(), 314 diag::err_asm_tying_incompatible_types) 315 << InTy << OutTy << OutputExpr->getSourceRange() 316 << InputExpr->getSourceRange(); 317 return StmtError(); 318 } 319 320 return Owned(NS); 321 } 322 323 // isMSAsmKeyword - Return true if this is an MS-style inline asm keyword. These 324 // require special handling. 325 static bool isMSAsmKeyword(StringRef Name) { 326 bool Ret = llvm::StringSwitch<bool>(Name) 327 .Cases("EVEN", "ALIGN", true) // Alignment directives. 328 .Cases("LENGTH", "SIZE", "TYPE", true) // Type and variable sizes. 329 .Case("_emit", true) // _emit Pseudoinstruction. 330 .Default(false); 331 return Ret; 332 } 333 334 // getIdentifierInfo - Given a Name and a range of tokens, find the associated 335 // IdentifierInfo*. 336 static IdentifierInfo *getIdentifierInfo(StringRef Name, 337 ArrayRef<Token> AsmToks, 338 unsigned Begin, unsigned End) { 339 for (unsigned i = Begin; i <= End; ++i) { 340 IdentifierInfo *II = AsmToks[i].getIdentifierInfo(); 341 if (II && II->getName() == Name) 342 return II; 343 } 344 return 0; 345 } 346 347 // getSpelling - Get the spelling of the AsmTok token. 348 static StringRef getSpelling(Sema &SemaRef, Token AsmTok) { 349 StringRef Asm; 350 SmallString<512> TokenBuf; 351 TokenBuf.resize(512); 352 bool StringInvalid = false; 353 Asm = SemaRef.PP.getSpelling(AsmTok, TokenBuf, &StringInvalid); 354 assert (!StringInvalid && "Expected valid string!"); 355 return Asm; 356 } 357 358 // Determine if we should bail on this MSAsm instruction. 359 static bool bailOnMSAsm(std::vector<StringRef> Piece) { 360 for (unsigned i = 0, e = Piece.size(); i != e; ++i) 361 if (isMSAsmKeyword(Piece[i])) 362 return true; 363 return false; 364 } 365 366 // Determine if we should bail on this MSAsm block. 367 static bool bailOnMSAsm(std::vector<std::vector<StringRef> > Pieces) { 368 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) 369 if (bailOnMSAsm(Pieces[i])) 370 return true; 371 return false; 372 } 373 374 // Determine if this is a simple MSAsm instruction. 375 static bool isSimpleMSAsm(std::vector<StringRef> &Pieces, 376 const TargetInfo &TI) { 377 if (isMSAsmKeyword(Pieces[0])) 378 return false; 379 380 for (unsigned i = 1, e = Pieces.size(); i != e; ++i) 381 if (!TI.isValidGCCRegisterName(Pieces[i])) 382 return false; 383 return true; 384 } 385 386 // Determine if this is a simple MSAsm block. 387 static bool isSimpleMSAsm(std::vector<std::vector<StringRef> > Pieces, 388 const TargetInfo &TI) { 389 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) 390 if (!isSimpleMSAsm(Pieces[i], TI)) 391 return false; 392 return true; 393 } 394 395 // Break the AsmSting into pieces (i.e., mnemonic and operands). 396 static void buildMSAsmPieces(StringRef Asm, std::vector<StringRef> &Pieces) { 397 std::pair<StringRef,StringRef> Split = Asm.split(' '); 398 399 // Mnemonic 400 Pieces.push_back(Split.first); 401 Asm = Split.second; 402 403 // Operands 404 while (!Asm.empty()) { 405 Split = Asm.split(", "); 406 Pieces.push_back(Split.first); 407 Asm = Split.second; 408 } 409 } 410 411 static void buildMSAsmPieces(std::vector<std::string> &AsmStrings, 412 std::vector<std::vector<StringRef> > &Pieces) { 413 for (unsigned i = 0, e = AsmStrings.size(); i != e; ++i) 414 buildMSAsmPieces(AsmStrings[i], Pieces[i]); 415 } 416 417 // Build the individual assembly instruction(s) and place them in the AsmStrings 418 // vector. These strings are fed to the AsmParser. 419 static void buildMSAsmStrings(Sema &SemaRef, ArrayRef<Token> AsmToks, 420 std::vector<std::string> &AsmStrings, 421 std::vector<std::pair<unsigned,unsigned> > &AsmTokRanges) { 422 assert (!AsmToks.empty() && "Didn't expect an empty AsmToks!"); 423 424 SmallString<512> Asm; 425 unsigned startTok = 0; 426 for (unsigned i = 0, e = AsmToks.size(); i < e; ++i) { 427 bool isNewAsm = i == 0 || AsmToks[i].isAtStartOfLine() || 428 AsmToks[i].is(tok::kw_asm); 429 430 if (isNewAsm) { 431 if (i) { 432 AsmStrings.push_back(Asm.str()); 433 AsmTokRanges.push_back(std::make_pair(startTok, i-1)); 434 startTok = i; 435 Asm.clear(); 436 } 437 if (AsmToks[i].is(tok::kw_asm)) { 438 i++; // Skip __asm 439 assert (i != e && "Expected another token"); 440 } 441 } 442 443 if (i && AsmToks[i].hasLeadingSpace() && !isNewAsm) 444 Asm += ' '; 445 446 StringRef Spelling = getSpelling(SemaRef, AsmToks[i]); 447 Asm += Spelling; 448 } 449 AsmStrings.push_back(Asm.str()); 450 AsmTokRanges.push_back(std::make_pair(startTok, AsmToks.size()-1)); 451 } 452 453 #define DEF_SIMPLE_MSASM(STR) \ 454 MSAsmStmt *NS = \ 455 new (Context) MSAsmStmt(Context, AsmLoc, LBraceLoc, /*IsSimple*/ true, \ 456 /*IsVolatile*/ true, AsmToks, Inputs, Outputs, \ 457 InputExprs, OutputExprs, STR, Constraints, \ 458 Clobbers, EndLoc); 459 460 StmtResult Sema::ActOnMSAsmStmt(SourceLocation AsmLoc, SourceLocation LBraceLoc, 461 ArrayRef<Token> AsmToks,SourceLocation EndLoc) { 462 SmallVector<StringRef, 4> Constraints; 463 std::vector<std::string> InputConstraints; 464 std::vector<std::string> OutputConstraints; 465 SmallVector<StringRef, 4> Clobbers; 466 std::set<std::string> ClobberRegs; 467 468 // FIXME: Use a struct to hold the various expression information. 469 SmallVector<IdentifierInfo*, 4> Inputs; 470 SmallVector<IdentifierInfo*, 4> Outputs; 471 SmallVector<Expr*, 4> InputExprs; 472 SmallVector<Expr*, 4> OutputExprs; 473 SmallVector<std::string, 4> InputExprNames; 474 SmallVector<std::string, 4> OutputExprNames; 475 SmallVector<unsigned, 4> InputExprStrIdx; 476 SmallVector<unsigned, 4> OutputExprStrIdx; 477 478 // Empty asm statements don't need to instantiate the AsmParser, etc. 479 StringRef EmptyAsmStr; 480 if (AsmToks.empty()) { DEF_SIMPLE_MSASM(EmptyAsmStr); return Owned(NS); } 481 482 std::vector<std::string> AsmStrings; 483 std::vector<std::pair<unsigned,unsigned> > AsmTokRanges; 484 buildMSAsmStrings(*this, AsmToks, AsmStrings, AsmTokRanges); 485 486 std::vector<std::vector<StringRef> > Pieces(AsmStrings.size()); 487 buildMSAsmPieces(AsmStrings, Pieces); 488 489 bool IsSimple = isSimpleMSAsm(Pieces, Context.getTargetInfo()); 490 491 // AsmParser doesn't fully support these asm statements. 492 if (bailOnMSAsm(Pieces)) { DEF_SIMPLE_MSASM(EmptyAsmStr); return Owned(NS); } 493 494 // Initialize targets and assembly printers/parsers. 495 llvm::InitializeAllTargetInfos(); 496 llvm::InitializeAllTargetMCs(); 497 llvm::InitializeAllAsmParsers(); 498 499 // Get the target specific parser. 500 std::string Error; 501 const std::string &TT = Context.getTargetInfo().getTriple().getTriple(); 502 const llvm::Target *TheTarget(llvm::TargetRegistry::lookupTarget(TT, Error)); 503 504 OwningPtr<llvm::MCAsmInfo> MAI(TheTarget->createMCAsmInfo(TT)); 505 OwningPtr<llvm::MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TT)); 506 OwningPtr<llvm::MCObjectFileInfo> MOFI(new llvm::MCObjectFileInfo()); 507 OwningPtr<llvm::MCSubtargetInfo> 508 STI(TheTarget->createMCSubtargetInfo(TT, "", "")); 509 510 for (unsigned StrIdx = 0, e = AsmStrings.size(); StrIdx != e; ++StrIdx) { 511 llvm::SourceMgr SrcMgr; 512 llvm::MCContext Ctx(*MAI, *MRI, MOFI.get(), &SrcMgr); 513 llvm::MemoryBuffer *Buffer = 514 llvm::MemoryBuffer::getMemBuffer(AsmStrings[StrIdx], "<inline asm>"); 515 516 // Tell SrcMgr about this buffer, which is what the parser will pick up. 517 SrcMgr.AddNewSourceBuffer(Buffer, llvm::SMLoc()); 518 519 OwningPtr<llvm::MCStreamer> Str(createNullStreamer(Ctx)); 520 OwningPtr<llvm::MCAsmParser> 521 Parser(createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI)); 522 OwningPtr<llvm::MCTargetAsmParser> 523 TargetParser(TheTarget->createMCAsmParser(*STI, *Parser)); 524 // Change to the Intel dialect. 525 Parser->setAssemblerDialect(1); 526 Parser->setTargetParser(*TargetParser.get()); 527 528 // Prime the lexer. 529 Parser->Lex(); 530 531 // Parse the opcode. 532 StringRef IDVal; 533 Parser->ParseIdentifier(IDVal); 534 535 // Canonicalize the opcode to lower case. 536 SmallString<128> OpcodeStr; 537 for (unsigned i = 0, e = IDVal.size(); i != e; ++i) 538 OpcodeStr.push_back(tolower(IDVal[i])); 539 540 // Parse the operands. 541 llvm::SMLoc IDLoc; 542 SmallVector<llvm::MCParsedAsmOperand*, 8> Operands; 543 bool HadError = TargetParser->ParseInstruction(OpcodeStr.str(), IDLoc, 544 Operands); 545 // If we had an error parsing the operands, fail gracefully. 546 if (HadError) { DEF_SIMPLE_MSASM(EmptyAsmStr); return Owned(NS); } 547 548 // Match the MCInstr. 549 unsigned Kind; 550 unsigned ErrorInfo; 551 SmallVector<llvm::MCInst, 2> Instrs; 552 HadError = TargetParser->MatchInstruction(IDLoc, Kind, Operands, Instrs, 553 ErrorInfo, 554 /*matchingInlineAsm*/ true); 555 // If we had an error parsing the operands, fail gracefully. 556 if (HadError) { DEF_SIMPLE_MSASM(EmptyAsmStr); return Owned(NS); } 557 558 // Get the instruction descriptor. 559 llvm::MCInst Inst = Instrs[0]; 560 const llvm::MCInstrInfo *MII = TheTarget->createMCInstrInfo(); 561 const llvm::MCInstrDesc &Desc = MII->get(Inst.getOpcode()); 562 llvm::MCInstPrinter *IP = 563 TheTarget->createMCInstPrinter(1, *MAI, *MII, *MRI, *STI); 564 565 // Build the list of clobbers, outputs and inputs. 566 unsigned NumDefs = Desc.getNumDefs(); 567 for (unsigned i = 1, e = Operands.size(); i != e; ++i) { 568 if (Operands[i]->isToken() || Operands[i]->isImm()) 569 continue; 570 571 // FIXME: The getMCInstOperandNum() function does not work with tied 572 // operands or custom converters. 573 unsigned NumMCOperands; 574 unsigned MCIdx = TargetParser->getMCInstOperandNum(Kind, Inst, Operands, 575 i, NumMCOperands); 576 assert (NumMCOperands && "Expected at least 1 MCOperand!"); 577 578 for (unsigned j = MCIdx, e = MCIdx + NumMCOperands; j != e; ++j) { 579 const llvm::MCOperand &Op = Inst.getOperand(j); 580 581 // Skip immediates. 582 if (Op.isImm() || Op.isFPImm()) 583 continue; 584 585 // Skip invalid register operands. 586 if (Op.isReg() && Op.getReg() == 0) 587 continue; 588 589 // Register/Clobber. 590 if (Op.isReg() && NumDefs && (j < NumDefs)) { 591 std::string Reg; 592 llvm::raw_string_ostream OS(Reg); 593 IP->printRegName(OS, Op.getReg()); 594 595 StringRef Clobber(OS.str()); 596 if (!Context.getTargetInfo().isValidClobber(Clobber)) 597 return StmtError( 598 Diag(AsmLoc, diag::err_asm_unknown_register_name) << Clobber); 599 ClobberRegs.insert(Reg); 600 continue; 601 } 602 // Expr/Input or Output. 603 if (Op.isExpr()) { 604 const llvm::MCExpr *Expr = Op.getExpr(); 605 const llvm::MCSymbolRefExpr *SymRef; 606 if ((SymRef = dyn_cast<llvm::MCSymbolRefExpr>(Expr))) { 607 StringRef Name = SymRef->getSymbol().getName(); 608 IdentifierInfo *II = getIdentifierInfo(Name, AsmToks, 609 AsmTokRanges[StrIdx].first, 610 AsmTokRanges[StrIdx].second); 611 if (II) { 612 CXXScopeSpec SS; 613 UnqualifiedId Id; 614 SourceLocation Loc; 615 Id.setIdentifier(II, AsmLoc); 616 ExprResult Result = ActOnIdExpression(getCurScope(), SS, Loc, Id, 617 false, false); 618 if (!Result.isInvalid()) { 619 // FIXME: Determine the proper constraints. 620 bool isMemDef = (i == 1) && Desc.mayStore(); 621 if (isMemDef) { 622 Outputs.push_back(II); 623 OutputExprs.push_back(Result.take()); 624 OutputExprNames.push_back(Name.str()); 625 OutputExprStrIdx.push_back(StrIdx); 626 OutputConstraints.push_back("=r"); 627 } else { 628 Inputs.push_back(II); 629 InputExprs.push_back(Result.take()); 630 InputExprNames.push_back(Name.str()); 631 InputExprStrIdx.push_back(StrIdx); 632 InputConstraints.push_back("r"); 633 } 634 } 635 } 636 } 637 } 638 } 639 } 640 } 641 for (std::set<std::string>::iterator I = ClobberRegs.begin(), 642 E = ClobberRegs.end(); I != E; ++I) 643 Clobbers.push_back(*I); 644 645 // Merge the output and input constraints. Output constraints are expected 646 // first. 647 for (std::vector<std::string>::iterator I = OutputConstraints.begin(), 648 E = OutputConstraints.end(); I != E; ++I) 649 Constraints.push_back(*I); 650 651 for (std::vector<std::string>::iterator I = InputConstraints.begin(), 652 E = InputConstraints.end(); I != E; ++I) 653 Constraints.push_back(*I); 654 655 // Enumerate the AsmString expressions. 656 unsigned OpNum = 0; 657 for (unsigned i = 0, e = OutputExprNames.size(); i != e; ++i, ++OpNum) { 658 unsigned StrIdx = OutputExprStrIdx[i]; 659 // Iterate over the assembly instruction pieces, skipping the mnemonic. 660 for (unsigned j = 1, f = Pieces[StrIdx].size(); j != f; ++j) { 661 // If the operand and the expression name match, then rewrite the operand. 662 if (OutputExprNames[i] == Pieces[StrIdx][j]) { 663 SmallString<32> Res; 664 llvm::raw_svector_ostream OS(Res); 665 OS << '$' << OpNum; 666 OutputExprNames[i] = OS.str(); 667 Pieces[StrIdx][j] = OutputExprNames[i]; 668 break; 669 } 670 // Check to see if the expression is a substring of the asm piece. 671 std::pair< StringRef, StringRef > Split = Pieces[StrIdx][j].split(' '); 672 bool isKeyword = llvm::StringSwitch<bool>(Split.first) 673 .Cases("BYTE", "byte", "WORD", "word", "DWORD", true) 674 .Cases("dword", "QWORD", "qword", "XWORD", "xword", true) 675 .Cases("XMMWORD", "xmmword", "YMMWORD", "ymmword", true) 676 .Default(false); 677 if (isKeyword && 678 Split.second.find_first_of(OutputExprNames[i]) != StringRef::npos) { 679 // Is is a substring, do the replacement. 680 SmallString<32> Res; 681 llvm::raw_svector_ostream OS(Res); 682 OS << '$' << OpNum; 683 std::string piece = Pieces[StrIdx][j].str(); 684 size_t found = piece.find(InputExprNames[i]); 685 piece.replace(found, InputExprNames[i].size(), OS.str()); 686 OutputExprNames[i] = piece; 687 Pieces[StrIdx][j] = OutputExprNames[i]; 688 break; 689 } 690 } 691 } 692 for (unsigned i = 0, e = InputExprNames.size(); i != e; ++i, ++OpNum) { 693 unsigned StrIdx = InputExprStrIdx[i]; 694 // Iterate over the assembly instruction pieces, skipping the mnemonic. 695 for (unsigned j = 1, f = Pieces[StrIdx].size(); j != f; ++j) { 696 // If the operand and the expression name match, then rewrite the operand. 697 if (InputExprNames[i] == Pieces[StrIdx][j]) { 698 SmallString<32> Res; 699 llvm::raw_svector_ostream OS(Res); 700 OS << '$' << OpNum; 701 InputExprNames[i] = OS.str(); 702 Pieces[StrIdx][j] = InputExprNames[i]; 703 break; 704 } 705 // Check to see if the expression is a substring of the asm piece. 706 std::pair< StringRef, StringRef > Split = Pieces[StrIdx][j].split(' '); 707 bool isKeyword = llvm::StringSwitch<bool>(Split.first) 708 .Cases("BYTE", "byte", "WORD", "word", "DWORD", true) 709 .Cases("dword", "QWORD", "qword", "XWORD", "xword", true) 710 .Cases("XMMWORD", "xmmword", "YMMWORD", "ymmword", true) 711 .Default(false); 712 if (isKeyword && 713 Split.second.find_first_of(InputExprNames[i]) != StringRef::npos) { 714 // It is a substring, do the replacement. 715 SmallString<32> Res; 716 llvm::raw_svector_ostream OS(Res); 717 OS << '$' << OpNum; 718 std::string piece = Pieces[StrIdx][j].str(); 719 size_t found = piece.find(InputExprNames[i]); 720 piece.replace(found, InputExprNames[i].size(), OS.str()); 721 InputExprNames[i] = piece; 722 Pieces[StrIdx][j] = InputExprNames[i]; 723 break; 724 } 725 } 726 } 727 728 // Emit the IR assembly string. 729 std::string AsmString; 730 for (unsigned i = 0, e = Pieces.size(); i != e; ++i) { 731 // Skip empty asm stmts. 732 if (Pieces[i].empty()) continue; 733 734 if (i > 0) 735 AsmString += "\n\t"; 736 737 // Emit the mnemonic. 738 AsmString += Pieces[i][0]; 739 if (Pieces[i].size() > 1) 740 AsmString += ' '; 741 742 // Emit the operands adding $$ to constants. 743 for (unsigned j = 1, f = Pieces[i].size(); j != f; ++j) { 744 if (j > 1) AsmString += ", "; 745 unsigned Val; 746 if (!Pieces[i][j].getAsInteger(0, Val)) 747 AsmString += "$$"; 748 749 AsmString += Pieces[i][j]; 750 } 751 } 752 753 MSAsmStmt *NS = 754 new (Context) MSAsmStmt(Context, AsmLoc, LBraceLoc, IsSimple, 755 /*IsVolatile*/ true, AsmToks, Inputs, Outputs, 756 InputExprs, OutputExprs, AsmString, Constraints, 757 Clobbers, EndLoc); 758 return Owned(NS); 759 } 760