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" |
| 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" |
Chad Rosier | 6e97be7 | 2012-08-22 23:42:09 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCExpr.h" |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 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" |
Chad Rosier | 6e97be7 | 2012-08-22 23:42:09 +0000 | [diff] [blame] | 35 | #include "llvm/MC/MCSymbol.h" |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 36 | #include "llvm/MC/MCTargetAsmParser.h" |
| 37 | #include "llvm/MC/MCParser/MCAsmLexer.h" |
| 38 | #include "llvm/MC/MCParser/MCAsmParser.h" |
Chad Rosier | fb70026 | 2012-09-11 23:13:15 +0000 | [diff] [blame] | 39 | #include "llvm/MC/MCParser/MCParsedAsmOperand.h" |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 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, |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 82 | ArrayRef<GCCAsmStmt::AsmStringPiece> AsmStrPieces) { |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 83 | for (unsigned p = 0, e = AsmStrPieces.size(); p != e; ++p) { |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 84 | const GCCAsmStmt::AsmStringPiece &Piece = AsmStrPieces[p]; |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 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 | |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 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) { |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 101 | unsigned NumClobbers = clobbers.size(); |
| 102 | StringLiteral **Constraints = |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 103 | reinterpret_cast<StringLiteral**>(constraints.data()); |
| 104 | Expr **Exprs = exprs.data(); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 105 | StringLiteral *AsmString = cast<StringLiteral>(asmString); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 106 | StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.data()); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 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 | |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 204 | GCCAsmStmt *NS = |
| 205 | new (Context) GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs, |
| 206 | NumInputs, Names, Constraints, Exprs, AsmString, |
| 207 | NumClobbers, Clobbers, RParenLoc); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 208 | // Validate the asm string, ensuring it makes sense given the operands we |
| 209 | // have. |
Chad Rosier | df5faf5 | 2012-08-25 00:11:56 +0000 | [diff] [blame] | 210 | SmallVector<GCCAsmStmt::AsmStringPiece, 8> Pieces; |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 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 | |
Chad Rosier | 6e97be7 | 2012-08-22 23:42:09 +0000 | [diff] [blame] | 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 | |
Chad Rosier | 358ab76 | 2012-08-22 21:08:06 +0000 | [diff] [blame] | 347 | // getSpelling - Get the spelling of the AsmTok token. |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 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 | |
Chad Rosier | 358ab76 | 2012-08-22 21:08:06 +0000 | [diff] [blame] | 358 | // Determine if we should bail on this MSAsm instruction. |
Chad Rosier | 2735df2 | 2012-08-22 19:18:30 +0000 | [diff] [blame] | 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 | |
Chad Rosier | 358ab76 | 2012-08-22 21:08:06 +0000 | [diff] [blame] | 366 | // Determine if we should bail on this MSAsm block. |
Chad Rosier | 2735df2 | 2012-08-22 19:18:30 +0000 | [diff] [blame] | 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 | |
Chad Rosier | 358ab76 | 2012-08-22 21:08:06 +0000 | [diff] [blame] | 374 | // Determine if this is a simple MSAsm instruction. |
Chad Rosier | 98ac608 | 2012-08-21 23:09:21 +0000 | [diff] [blame] | 375 | static bool isSimpleMSAsm(std::vector<StringRef> &Pieces, |
| 376 | const TargetInfo &TI) { |
| 377 | if (isMSAsmKeyword(Pieces[0])) |
| 378 | return false; |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 379 | |
Chad Rosier | 98ac608 | 2012-08-21 23:09:21 +0000 | [diff] [blame] | 380 | for (unsigned i = 1, e = Pieces.size(); i != e; ++i) |
| 381 | if (!TI.isValidGCCRegisterName(Pieces[i])) |
| 382 | return false; |
Chad Rosier | 153f8ec | 2012-08-22 19:50:28 +0000 | [diff] [blame] | 383 | return true; |
| 384 | } |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 385 | |
Chad Rosier | 358ab76 | 2012-08-22 21:08:06 +0000 | [diff] [blame] | 386 | // Determine if this is a simple MSAsm block. |
Chad Rosier | 153f8ec | 2012-08-22 19:50:28 +0000 | [diff] [blame] | 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; |
Chad Rosier | 98ac608 | 2012-08-21 23:09:21 +0000 | [diff] [blame] | 392 | return true; |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Chad Rosier | 358ab76 | 2012-08-22 21:08:06 +0000 | [diff] [blame] | 395 | // Break the AsmSting into pieces (i.e., mnemonic and operands). |
Chad Rosier | 38c71d3 | 2012-08-21 21:56:39 +0000 | [diff] [blame] | 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 | |
Chad Rosier | f0fbd77 | 2012-08-22 21:04:07 +0000 | [diff] [blame] | 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 | |
Chad Rosier | b55e602 | 2012-09-13 00:06:55 +0000 | [diff] [blame] | 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, |
Chad Rosier | 9072a02 | 2012-08-22 20:30:58 +0000 | [diff] [blame] | 421 | std::vector<std::pair<unsigned,unsigned> > &AsmTokRanges) { |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 422 | assert (!AsmToks.empty() && "Didn't expect an empty AsmToks!"); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 423 | |
| 424 | SmallString<512> Asm; |
Chad Rosier | 9072a02 | 2012-08-22 20:30:58 +0000 | [diff] [blame] | 425 | unsigned startTok = 0; |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 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) { |
Chad Rosier | 38c71d3 | 2012-08-21 21:56:39 +0000 | [diff] [blame] | 431 | if (i) { |
Benjamin Kramer | 32f3acc | 2012-08-24 20:43:21 +0000 | [diff] [blame] | 432 | AsmStrings.push_back(Asm.str()); |
Chad Rosier | 9072a02 | 2012-08-22 20:30:58 +0000 | [diff] [blame] | 433 | AsmTokRanges.push_back(std::make_pair(startTok, i-1)); |
| 434 | startTok = i; |
Chad Rosier | 38c71d3 | 2012-08-21 21:56:39 +0000 | [diff] [blame] | 435 | Asm.clear(); |
Chad Rosier | 38c71d3 | 2012-08-21 21:56:39 +0000 | [diff] [blame] | 436 | } |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 437 | if (AsmToks[i].is(tok::kw_asm)) { |
| 438 | i++; // Skip __asm |
| 439 | assert (i != e && "Expected another token"); |
| 440 | } |
| 441 | } |
| 442 | |
Chad Rosier | b55e602 | 2012-09-13 00:06:55 +0000 | [diff] [blame] | 443 | if (i && AsmToks[i].hasLeadingSpace() && !isNewAsm) |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 444 | Asm += ' '; |
Chad Rosier | 4de9716 | 2012-09-11 00:51:28 +0000 | [diff] [blame] | 445 | |
| 446 | StringRef Spelling = getSpelling(SemaRef, AsmToks[i]); |
| 447 | Asm += Spelling; |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 448 | } |
Benjamin Kramer | 32f3acc | 2012-08-24 20:43:21 +0000 | [diff] [blame] | 449 | AsmStrings.push_back(Asm.str()); |
Chad Rosier | 9072a02 | 2012-08-22 20:30:58 +0000 | [diff] [blame] | 450 | AsmTokRanges.push_back(std::make_pair(startTok, AsmToks.size()-1)); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Chad Rosier | c691649 | 2012-09-06 19:56:25 +0000 | [diff] [blame] | 453 | #define DEF_SIMPLE_MSASM(STR) \ |
Chad Rosier | 89fb6d7 | 2012-08-28 20:28:20 +0000 | [diff] [blame] | 454 | MSAsmStmt *NS = \ |
| 455 | new (Context) MSAsmStmt(Context, AsmLoc, LBraceLoc, /*IsSimple*/ true, \ |
| 456 | /*IsVolatile*/ true, AsmToks, Inputs, Outputs, \ |
Chad Rosier | c691649 | 2012-09-06 19:56:25 +0000 | [diff] [blame] | 457 | InputExprs, OutputExprs, STR, Constraints, \ |
Chad Rosier | 89fb6d7 | 2012-08-28 20:28:20 +0000 | [diff] [blame] | 458 | Clobbers, EndLoc); |
Chad Rosier | 2735df2 | 2012-08-22 19:18:30 +0000 | [diff] [blame] | 459 | |
Chad Rosier | b55e602 | 2012-09-13 00:06:55 +0000 | [diff] [blame] | 460 | StmtResult Sema::ActOnMSAsmStmt(SourceLocation AsmLoc, SourceLocation LBraceLoc, |
| 461 | ArrayRef<Token> AsmToks,SourceLocation EndLoc) { |
Chad Rosier | 89fb6d7 | 2012-08-28 20:28:20 +0000 | [diff] [blame] | 462 | SmallVector<StringRef, 4> Constraints; |
| 463 | std::vector<std::string> InputConstraints; |
| 464 | std::vector<std::string> OutputConstraints; |
Chad Rosier | 4112a4c | 2012-08-28 20:35:06 +0000 | [diff] [blame] | 465 | SmallVector<StringRef, 4> Clobbers; |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 466 | std::set<std::string> ClobberRegs; |
Chad Rosier | b55e602 | 2012-09-13 00:06:55 +0000 | [diff] [blame] | 467 | |
| 468 | // FIXME: Use a struct to hold the various expression information. |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 469 | SmallVector<IdentifierInfo*, 4> Inputs; |
| 470 | SmallVector<IdentifierInfo*, 4> Outputs; |
Chad Rosier | 633abb0 | 2012-08-24 00:07:09 +0000 | [diff] [blame] | 471 | SmallVector<Expr*, 4> InputExprs; |
| 472 | SmallVector<Expr*, 4> OutputExprs; |
Chad Rosier | acc22b6 | 2012-09-06 19:35:00 +0000 | [diff] [blame] | 473 | SmallVector<std::string, 4> InputExprNames; |
| 474 | SmallVector<std::string, 4> OutputExprNames; |
Chad Rosier | b55e602 | 2012-09-13 00:06:55 +0000 | [diff] [blame] | 475 | SmallVector<unsigned, 4> InputExprStrIdx; |
| 476 | SmallVector<unsigned, 4> OutputExprStrIdx; |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 477 | |
| 478 | // Empty asm statements don't need to instantiate the AsmParser, etc. |
Chad Rosier | c691649 | 2012-09-06 19:56:25 +0000 | [diff] [blame] | 479 | StringRef EmptyAsmStr; |
| 480 | if (AsmToks.empty()) { DEF_SIMPLE_MSASM(EmptyAsmStr); return Owned(NS); } |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 481 | |
Chad Rosier | 38c71d3 | 2012-08-21 21:56:39 +0000 | [diff] [blame] | 482 | std::vector<std::string> AsmStrings; |
Chad Rosier | 9072a02 | 2012-08-22 20:30:58 +0000 | [diff] [blame] | 483 | std::vector<std::pair<unsigned,unsigned> > AsmTokRanges; |
Chad Rosier | b55e602 | 2012-09-13 00:06:55 +0000 | [diff] [blame] | 484 | buildMSAsmStrings(*this, AsmToks, AsmStrings, AsmTokRanges); |
Chad Rosier | 38c71d3 | 2012-08-21 21:56:39 +0000 | [diff] [blame] | 485 | |
Chad Rosier | e78460f | 2012-08-22 20:57:07 +0000 | [diff] [blame] | 486 | std::vector<std::vector<StringRef> > Pieces(AsmStrings.size()); |
Chad Rosier | f0fbd77 | 2012-08-22 21:04:07 +0000 | [diff] [blame] | 487 | buildMSAsmPieces(AsmStrings, Pieces); |
Chad Rosier | 153f8ec | 2012-08-22 19:50:28 +0000 | [diff] [blame] | 488 | |
| 489 | bool IsSimple = isSimpleMSAsm(Pieces, Context.getTargetInfo()); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 490 | |
Chad Rosier | 2735df2 | 2012-08-22 19:18:30 +0000 | [diff] [blame] | 491 | // AsmParser doesn't fully support these asm statements. |
Chad Rosier | c691649 | 2012-09-06 19:56:25 +0000 | [diff] [blame] | 492 | if (bailOnMSAsm(Pieces)) { DEF_SIMPLE_MSASM(EmptyAsmStr); return Owned(NS); } |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 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 | |
Chad Rosier | 25bd298 | 2012-08-23 15:44:35 +0000 | [diff] [blame] | 510 | for (unsigned StrIdx = 0, e = AsmStrings.size(); StrIdx != e; ++StrIdx) { |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 511 | llvm::SourceMgr SrcMgr; |
| 512 | llvm::MCContext Ctx(*MAI, *MRI, MOFI.get(), &SrcMgr); |
| 513 | llvm::MemoryBuffer *Buffer = |
Chad Rosier | 25bd298 | 2012-08-23 15:44:35 +0000 | [diff] [blame] | 514 | llvm::MemoryBuffer::getMemBuffer(AsmStrings[StrIdx], "<inline asm>"); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 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. |
Chad Rosier | be5c3fb | 2012-09-03 02:30:13 +0000 | [diff] [blame] | 536 | SmallString<128> OpcodeStr; |
Chad Rosier | b706d90 | 2012-08-28 22:08:58 +0000 | [diff] [blame] | 537 | for (unsigned i = 0, e = IDVal.size(); i != e; ++i) |
Chad Rosier | be5c3fb | 2012-09-03 02:30:13 +0000 | [diff] [blame] | 538 | OpcodeStr.push_back(tolower(IDVal[i])); |
Chad Rosier | 3d44219 | 2012-09-21 22:22:39 +0000 | [diff] [blame] | 539 | // FIXME: Convert to a StmtError. |
| 540 | assert(TargetParser->mnemonicIsValid(OpcodeStr) && "Invalid mnemonic!"); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 541 | |
| 542 | // Parse the operands. |
| 543 | llvm::SMLoc IDLoc; |
| 544 | SmallVector<llvm::MCParsedAsmOperand*, 8> Operands; |
Chad Rosier | be5c3fb | 2012-09-03 02:30:13 +0000 | [diff] [blame] | 545 | bool HadError = TargetParser->ParseInstruction(OpcodeStr.str(), IDLoc, |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 546 | Operands); |
Chad Rosier | 2735df2 | 2012-08-22 19:18:30 +0000 | [diff] [blame] | 547 | // If we had an error parsing the operands, fail gracefully. |
Chad Rosier | c691649 | 2012-09-06 19:56:25 +0000 | [diff] [blame] | 548 | if (HadError) { DEF_SIMPLE_MSASM(EmptyAsmStr); return Owned(NS); } |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 549 | |
| 550 | // Match the MCInstr. |
Chad Rosier | be5c3fb | 2012-09-03 02:30:13 +0000 | [diff] [blame] | 551 | unsigned Kind; |
Chad Rosier | 83591b6 | 2012-08-21 18:15:08 +0000 | [diff] [blame] | 552 | unsigned ErrorInfo; |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 553 | SmallVector<llvm::MCInst, 2> Instrs; |
Chad Rosier | 7065c52 | 2012-09-03 03:16:15 +0000 | [diff] [blame] | 554 | HadError = TargetParser->MatchInstruction(IDLoc, Kind, Operands, Instrs, |
| 555 | ErrorInfo, |
Chad Rosier | 51a6b3f | 2012-08-21 19:37:55 +0000 | [diff] [blame] | 556 | /*matchingInlineAsm*/ true); |
Chad Rosier | 2735df2 | 2012-08-22 19:18:30 +0000 | [diff] [blame] | 557 | // If we had an error parsing the operands, fail gracefully. |
Chad Rosier | c691649 | 2012-09-06 19:56:25 +0000 | [diff] [blame] | 558 | if (HadError) { DEF_SIMPLE_MSASM(EmptyAsmStr); return Owned(NS); } |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 559 | |
| 560 | // Get the instruction descriptor. |
| 561 | llvm::MCInst Inst = Instrs[0]; |
| 562 | const llvm::MCInstrInfo *MII = TheTarget->createMCInstrInfo(); |
| 563 | const llvm::MCInstrDesc &Desc = MII->get(Inst.getOpcode()); |
| 564 | llvm::MCInstPrinter *IP = |
| 565 | TheTarget->createMCInstPrinter(1, *MAI, *MII, *MRI, *STI); |
| 566 | |
Chad Rosier | 6e97be7 | 2012-08-22 23:42:09 +0000 | [diff] [blame] | 567 | // Build the list of clobbers, outputs and inputs. |
Chad Rosier | fd5e56e | 2012-08-22 22:10:51 +0000 | [diff] [blame] | 568 | unsigned NumDefs = Desc.getNumDefs(); |
Chad Rosier | 1b497f2 | 2012-09-03 20:40:52 +0000 | [diff] [blame] | 569 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) { |
Chad Rosier | fb70026 | 2012-09-11 23:13:15 +0000 | [diff] [blame] | 570 | if (Operands[i]->isToken() || Operands[i]->isImm()) |
| 571 | continue; |
| 572 | |
Chad Rosier | 3ef95a5 | 2012-09-13 00:20:37 +0000 | [diff] [blame] | 573 | // FIXME: The getMCInstOperandNum() function does not work with tied |
| 574 | // operands or custom converters. |
Chad Rosier | 1b497f2 | 2012-09-03 20:40:52 +0000 | [diff] [blame] | 575 | unsigned NumMCOperands; |
Chad Rosier | 8cdd8a9 | 2012-09-05 01:16:06 +0000 | [diff] [blame] | 576 | unsigned MCIdx = TargetParser->getMCInstOperandNum(Kind, Inst, Operands, |
Chad Rosier | d5eb585 | 2012-09-04 15:58:44 +0000 | [diff] [blame] | 577 | i, NumMCOperands); |
Chad Rosier | 1b497f2 | 2012-09-03 20:40:52 +0000 | [diff] [blame] | 578 | assert (NumMCOperands && "Expected at least 1 MCOperand!"); |
Chad Rosier | 7f9678b | 2012-09-12 18:14:25 +0000 | [diff] [blame] | 579 | |
| 580 | for (unsigned j = MCIdx, e = MCIdx + NumMCOperands; j != e; ++j) { |
| 581 | const llvm::MCOperand &Op = Inst.getOperand(j); |
| 582 | |
| 583 | // Skip immediates. |
| 584 | if (Op.isImm() || Op.isFPImm()) |
| 585 | continue; |
| 586 | |
| 587 | // Skip invalid register operands. |
| 588 | if (Op.isReg() && Op.getReg() == 0) |
| 589 | continue; |
| 590 | |
| 591 | // Register/Clobber. |
| 592 | if (Op.isReg() && NumDefs && (j < NumDefs)) { |
| 593 | std::string Reg; |
| 594 | llvm::raw_string_ostream OS(Reg); |
| 595 | IP->printRegName(OS, Op.getReg()); |
| 596 | |
| 597 | StringRef Clobber(OS.str()); |
| 598 | if (!Context.getTargetInfo().isValidClobber(Clobber)) |
| 599 | return StmtError( |
| 600 | Diag(AsmLoc, diag::err_asm_unknown_register_name) << Clobber); |
| 601 | ClobberRegs.insert(Reg); |
| 602 | continue; |
Chad Rosier | 1b497f2 | 2012-09-03 20:40:52 +0000 | [diff] [blame] | 603 | } |
Chad Rosier | 7f9678b | 2012-09-12 18:14:25 +0000 | [diff] [blame] | 604 | // Expr/Input or Output. |
| 605 | if (Op.isExpr()) { |
| 606 | const llvm::MCExpr *Expr = Op.getExpr(); |
| 607 | const llvm::MCSymbolRefExpr *SymRef; |
| 608 | if ((SymRef = dyn_cast<llvm::MCSymbolRefExpr>(Expr))) { |
| 609 | StringRef Name = SymRef->getSymbol().getName(); |
| 610 | IdentifierInfo *II = getIdentifierInfo(Name, AsmToks, |
| 611 | AsmTokRanges[StrIdx].first, |
| 612 | AsmTokRanges[StrIdx].second); |
| 613 | if (II) { |
| 614 | CXXScopeSpec SS; |
| 615 | UnqualifiedId Id; |
| 616 | SourceLocation Loc; |
| 617 | Id.setIdentifier(II, AsmLoc); |
| 618 | ExprResult Result = ActOnIdExpression(getCurScope(), SS, Loc, Id, |
| 619 | false, false); |
| 620 | if (!Result.isInvalid()) { |
Chad Rosier | 3ef95a5 | 2012-09-13 00:20:37 +0000 | [diff] [blame] | 621 | // FIXME: Determine the proper constraints. |
Chad Rosier | 7f9678b | 2012-09-12 18:14:25 +0000 | [diff] [blame] | 622 | bool isMemDef = (i == 1) && Desc.mayStore(); |
| 623 | if (isMemDef) { |
| 624 | Outputs.push_back(II); |
| 625 | OutputExprs.push_back(Result.take()); |
| 626 | OutputExprNames.push_back(Name.str()); |
Chad Rosier | b55e602 | 2012-09-13 00:06:55 +0000 | [diff] [blame] | 627 | OutputExprStrIdx.push_back(StrIdx); |
Chad Rosier | 7f9678b | 2012-09-12 18:14:25 +0000 | [diff] [blame] | 628 | OutputConstraints.push_back("=r"); |
| 629 | } else { |
| 630 | Inputs.push_back(II); |
| 631 | InputExprs.push_back(Result.take()); |
| 632 | InputExprNames.push_back(Name.str()); |
Chad Rosier | b55e602 | 2012-09-13 00:06:55 +0000 | [diff] [blame] | 633 | InputExprStrIdx.push_back(StrIdx); |
Chad Rosier | 7f9678b | 2012-09-12 18:14:25 +0000 | [diff] [blame] | 634 | InputConstraints.push_back("r"); |
| 635 | } |
Chad Rosier | 1016bdf | 2012-08-24 16:38:58 +0000 | [diff] [blame] | 636 | } |
Chad Rosier | 633abb0 | 2012-08-24 00:07:09 +0000 | [diff] [blame] | 637 | } |
| 638 | } |
Chad Rosier | 6e97be7 | 2012-08-22 23:42:09 +0000 | [diff] [blame] | 639 | } |
Chad Rosier | fd5e56e | 2012-08-22 22:10:51 +0000 | [diff] [blame] | 640 | } |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | for (std::set<std::string>::iterator I = ClobberRegs.begin(), |
| 644 | E = ClobberRegs.end(); I != E; ++I) |
| 645 | Clobbers.push_back(*I); |
| 646 | |
Chad Rosier | 89fb6d7 | 2012-08-28 20:28:20 +0000 | [diff] [blame] | 647 | // Merge the output and input constraints. Output constraints are expected |
| 648 | // first. |
| 649 | for (std::vector<std::string>::iterator I = OutputConstraints.begin(), |
| 650 | E = OutputConstraints.end(); I != E; ++I) |
| 651 | Constraints.push_back(*I); |
| 652 | |
| 653 | for (std::vector<std::string>::iterator I = InputConstraints.begin(), |
| 654 | E = InputConstraints.end(); I != E; ++I) |
| 655 | Constraints.push_back(*I); |
| 656 | |
Chad Rosier | acc22b6 | 2012-09-06 19:35:00 +0000 | [diff] [blame] | 657 | // Enumerate the AsmString expressions. |
Chad Rosier | acc22b6 | 2012-09-06 19:35:00 +0000 | [diff] [blame] | 658 | unsigned OpNum = 0; |
| 659 | for (unsigned i = 0, e = OutputExprNames.size(); i != e; ++i, ++OpNum) { |
Chad Rosier | b55e602 | 2012-09-13 00:06:55 +0000 | [diff] [blame] | 660 | unsigned StrIdx = OutputExprStrIdx[i]; |
| 661 | // Iterate over the assembly instruction pieces, skipping the mnemonic. |
| 662 | for (unsigned j = 1, f = Pieces[StrIdx].size(); j != f; ++j) { |
| 663 | // If the operand and the expression name match, then rewrite the operand. |
| 664 | if (OutputExprNames[i] == Pieces[StrIdx][j]) { |
| 665 | SmallString<32> Res; |
| 666 | llvm::raw_svector_ostream OS(Res); |
| 667 | OS << '$' << OpNum; |
| 668 | OutputExprNames[i] = OS.str(); |
| 669 | Pieces[StrIdx][j] = OutputExprNames[i]; |
| 670 | break; |
| 671 | } |
| 672 | // Check to see if the expression is a substring of the asm piece. |
| 673 | std::pair< StringRef, StringRef > Split = Pieces[StrIdx][j].split(' '); |
| 674 | bool isKeyword = llvm::StringSwitch<bool>(Split.first) |
| 675 | .Cases("BYTE", "byte", "WORD", "word", "DWORD", true) |
| 676 | .Cases("dword", "QWORD", "qword", "XWORD", "xword", true) |
| 677 | .Cases("XMMWORD", "xmmword", "YMMWORD", "ymmword", true) |
| 678 | .Default(false); |
| 679 | if (isKeyword && |
| 680 | Split.second.find_first_of(OutputExprNames[i]) != StringRef::npos) { |
| 681 | // Is is a substring, do the replacement. |
| 682 | SmallString<32> Res; |
| 683 | llvm::raw_svector_ostream OS(Res); |
| 684 | OS << '$' << OpNum; |
| 685 | std::string piece = Pieces[StrIdx][j].str(); |
| 686 | size_t found = piece.find(InputExprNames[i]); |
| 687 | piece.replace(found, InputExprNames[i].size(), OS.str()); |
| 688 | OutputExprNames[i] = piece; |
| 689 | Pieces[StrIdx][j] = OutputExprNames[i]; |
| 690 | break; |
| 691 | } |
| 692 | } |
Chad Rosier | acc22b6 | 2012-09-06 19:35:00 +0000 | [diff] [blame] | 693 | } |
| 694 | for (unsigned i = 0, e = InputExprNames.size(); i != e; ++i, ++OpNum) { |
Chad Rosier | b55e602 | 2012-09-13 00:06:55 +0000 | [diff] [blame] | 695 | unsigned StrIdx = InputExprStrIdx[i]; |
| 696 | // Iterate over the assembly instruction pieces, skipping the mnemonic. |
| 697 | for (unsigned j = 1, f = Pieces[StrIdx].size(); j != f; ++j) { |
| 698 | // If the operand and the expression name match, then rewrite the operand. |
| 699 | if (InputExprNames[i] == Pieces[StrIdx][j]) { |
| 700 | SmallString<32> Res; |
| 701 | llvm::raw_svector_ostream OS(Res); |
| 702 | OS << '$' << OpNum; |
| 703 | InputExprNames[i] = OS.str(); |
| 704 | Pieces[StrIdx][j] = InputExprNames[i]; |
| 705 | break; |
| 706 | } |
| 707 | // Check to see if the expression is a substring of the asm piece. |
| 708 | std::pair< StringRef, StringRef > Split = Pieces[StrIdx][j].split(' '); |
| 709 | bool isKeyword = llvm::StringSwitch<bool>(Split.first) |
| 710 | .Cases("BYTE", "byte", "WORD", "word", "DWORD", true) |
| 711 | .Cases("dword", "QWORD", "qword", "XWORD", "xword", true) |
| 712 | .Cases("XMMWORD", "xmmword", "YMMWORD", "ymmword", true) |
| 713 | .Default(false); |
| 714 | if (isKeyword && |
| 715 | Split.second.find_first_of(InputExprNames[i]) != StringRef::npos) { |
| 716 | // It is a substring, do the replacement. |
| 717 | SmallString<32> Res; |
| 718 | llvm::raw_svector_ostream OS(Res); |
| 719 | OS << '$' << OpNum; |
| 720 | std::string piece = Pieces[StrIdx][j].str(); |
| 721 | size_t found = piece.find(InputExprNames[i]); |
| 722 | piece.replace(found, InputExprNames[i].size(), OS.str()); |
| 723 | InputExprNames[i] = piece; |
| 724 | Pieces[StrIdx][j] = InputExprNames[i]; |
| 725 | break; |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | // Emit the IR assembly string. |
| 731 | std::string AsmString; |
| 732 | for (unsigned i = 0, e = Pieces.size(); i != e; ++i) { |
| 733 | // Skip empty asm stmts. |
| 734 | if (Pieces[i].empty()) continue; |
| 735 | |
| 736 | if (i > 0) |
| 737 | AsmString += "\n\t"; |
| 738 | |
| 739 | // Emit the mnemonic. |
| 740 | AsmString += Pieces[i][0]; |
| 741 | if (Pieces[i].size() > 1) |
| 742 | AsmString += ' '; |
| 743 | |
| 744 | // Emit the operands adding $$ to constants. |
| 745 | for (unsigned j = 1, f = Pieces[i].size(); j != f; ++j) { |
| 746 | if (j > 1) AsmString += ", "; |
| 747 | unsigned Val; |
| 748 | if (!Pieces[i][j].getAsInteger(0, Val)) |
| 749 | AsmString += "$$"; |
| 750 | |
| 751 | AsmString += Pieces[i][j]; |
| 752 | } |
Chad Rosier | acc22b6 | 2012-09-06 19:35:00 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 755 | MSAsmStmt *NS = |
| 756 | new (Context) MSAsmStmt(Context, AsmLoc, LBraceLoc, IsSimple, |
| 757 | /*IsVolatile*/ true, AsmToks, Inputs, Outputs, |
Chad Rosier | 89fb6d7 | 2012-08-28 20:28:20 +0000 | [diff] [blame] | 758 | InputExprs, OutputExprs, AsmString, Constraints, |
| 759 | Clobbers, EndLoc); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 760 | return Owned(NS); |
| 761 | } |