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" |
| 39 | #include "llvm/Support/SourceMgr.h" |
| 40 | #include "llvm/Support/TargetRegistry.h" |
| 41 | #include "llvm/Support/TargetSelect.h" |
| 42 | using namespace clang; |
| 43 | using namespace sema; |
| 44 | |
| 45 | /// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently |
| 46 | /// ignore "noop" casts in places where an lvalue is required by an inline asm. |
| 47 | /// We emulate this behavior when -fheinous-gnu-extensions is specified, but |
| 48 | /// provide a strong guidance to not use it. |
| 49 | /// |
| 50 | /// This method checks to see if the argument is an acceptable l-value and |
| 51 | /// returns false if it is a case we can handle. |
| 52 | static bool CheckAsmLValue(const Expr *E, Sema &S) { |
| 53 | // Type dependent expressions will be checked during instantiation. |
| 54 | if (E->isTypeDependent()) |
| 55 | return false; |
| 56 | |
| 57 | if (E->isLValue()) |
| 58 | return false; // Cool, this is an lvalue. |
| 59 | |
| 60 | // Okay, this is not an lvalue, but perhaps it is the result of a cast that we |
| 61 | // are supposed to allow. |
| 62 | const Expr *E2 = E->IgnoreParenNoopCasts(S.Context); |
| 63 | if (E != E2 && E2->isLValue()) { |
| 64 | if (!S.getLangOpts().HeinousExtensions) |
| 65 | S.Diag(E2->getLocStart(), diag::err_invalid_asm_cast_lvalue) |
| 66 | << E->getSourceRange(); |
| 67 | else |
| 68 | S.Diag(E2->getLocStart(), diag::warn_invalid_asm_cast_lvalue) |
| 69 | << E->getSourceRange(); |
| 70 | // Accept, even if we emitted an error diagnostic. |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | // None of the above, just randomly invalid non-lvalue. |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | /// isOperandMentioned - Return true if the specified operand # is mentioned |
| 79 | /// anywhere in the decomposed asm string. |
| 80 | static bool isOperandMentioned(unsigned OpNo, |
| 81 | ArrayRef<AsmStmt::AsmStringPiece> AsmStrPieces) { |
| 82 | for (unsigned p = 0, e = AsmStrPieces.size(); p != e; ++p) { |
| 83 | const AsmStmt::AsmStringPiece &Piece = AsmStrPieces[p]; |
| 84 | if (!Piece.isOperand()) continue; |
| 85 | |
| 86 | // If this is a reference to the input and if the input was the smaller |
| 87 | // one, then we have to reject this asm. |
| 88 | if (Piece.getOperandNo() == OpNo) |
| 89 | return true; |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | StmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc, bool IsSimple, |
| 95 | bool IsVolatile, unsigned NumOutputs, |
| 96 | unsigned NumInputs, IdentifierInfo **Names, |
| 97 | MultiExprArg constraints, MultiExprArg exprs, |
| 98 | Expr *asmString, MultiExprArg clobbers, |
Chad Rosier | df4ee10 | 2012-08-20 17:11:53 +0000 | [diff] [blame] | 99 | SourceLocation RParenLoc) { |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 100 | unsigned NumClobbers = clobbers.size(); |
| 101 | StringLiteral **Constraints = |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame^] | 102 | reinterpret_cast<StringLiteral**>(constraints.data()); |
| 103 | Expr **Exprs = exprs.data(); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 104 | StringLiteral *AsmString = cast<StringLiteral>(asmString); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame^] | 105 | StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.data()); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 106 | |
| 107 | SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos; |
| 108 | |
| 109 | // The parser verifies that there is a string literal here. |
| 110 | if (!AsmString->isAscii()) |
| 111 | return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character) |
| 112 | << AsmString->getSourceRange()); |
| 113 | |
| 114 | for (unsigned i = 0; i != NumOutputs; i++) { |
| 115 | StringLiteral *Literal = Constraints[i]; |
| 116 | if (!Literal->isAscii()) |
| 117 | return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) |
| 118 | << Literal->getSourceRange()); |
| 119 | |
| 120 | StringRef OutputName; |
| 121 | if (Names[i]) |
| 122 | OutputName = Names[i]->getName(); |
| 123 | |
| 124 | TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName); |
| 125 | if (!Context.getTargetInfo().validateOutputConstraint(Info)) |
| 126 | return StmtError(Diag(Literal->getLocStart(), |
| 127 | diag::err_asm_invalid_output_constraint) |
| 128 | << Info.getConstraintStr()); |
| 129 | |
| 130 | // Check that the output exprs are valid lvalues. |
| 131 | Expr *OutputExpr = Exprs[i]; |
| 132 | if (CheckAsmLValue(OutputExpr, *this)) { |
| 133 | return StmtError(Diag(OutputExpr->getLocStart(), |
| 134 | diag::err_asm_invalid_lvalue_in_output) |
| 135 | << OutputExpr->getSourceRange()); |
| 136 | } |
| 137 | |
| 138 | OutputConstraintInfos.push_back(Info); |
| 139 | } |
| 140 | |
| 141 | SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos; |
| 142 | |
| 143 | for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) { |
| 144 | StringLiteral *Literal = Constraints[i]; |
| 145 | if (!Literal->isAscii()) |
| 146 | return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) |
| 147 | << Literal->getSourceRange()); |
| 148 | |
| 149 | StringRef InputName; |
| 150 | if (Names[i]) |
| 151 | InputName = Names[i]->getName(); |
| 152 | |
| 153 | TargetInfo::ConstraintInfo Info(Literal->getString(), InputName); |
| 154 | if (!Context.getTargetInfo().validateInputConstraint(OutputConstraintInfos.data(), |
| 155 | NumOutputs, Info)) { |
| 156 | return StmtError(Diag(Literal->getLocStart(), |
| 157 | diag::err_asm_invalid_input_constraint) |
| 158 | << Info.getConstraintStr()); |
| 159 | } |
| 160 | |
| 161 | Expr *InputExpr = Exprs[i]; |
| 162 | |
| 163 | // Only allow void types for memory constraints. |
| 164 | if (Info.allowsMemory() && !Info.allowsRegister()) { |
| 165 | if (CheckAsmLValue(InputExpr, *this)) |
| 166 | return StmtError(Diag(InputExpr->getLocStart(), |
| 167 | diag::err_asm_invalid_lvalue_in_input) |
| 168 | << Info.getConstraintStr() |
| 169 | << InputExpr->getSourceRange()); |
| 170 | } |
| 171 | |
| 172 | if (Info.allowsRegister()) { |
| 173 | if (InputExpr->getType()->isVoidType()) { |
| 174 | return StmtError(Diag(InputExpr->getLocStart(), |
| 175 | diag::err_asm_invalid_type_in_input) |
| 176 | << InputExpr->getType() << Info.getConstraintStr() |
| 177 | << InputExpr->getSourceRange()); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | ExprResult Result = DefaultFunctionArrayLvalueConversion(Exprs[i]); |
| 182 | if (Result.isInvalid()) |
| 183 | return StmtError(); |
| 184 | |
| 185 | Exprs[i] = Result.take(); |
| 186 | InputConstraintInfos.push_back(Info); |
| 187 | } |
| 188 | |
| 189 | // Check that the clobbers are valid. |
| 190 | for (unsigned i = 0; i != NumClobbers; i++) { |
| 191 | StringLiteral *Literal = Clobbers[i]; |
| 192 | if (!Literal->isAscii()) |
| 193 | return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character) |
| 194 | << Literal->getSourceRange()); |
| 195 | |
| 196 | StringRef Clobber = Literal->getString(); |
| 197 | |
| 198 | if (!Context.getTargetInfo().isValidClobber(Clobber)) |
| 199 | return StmtError(Diag(Literal->getLocStart(), |
| 200 | diag::err_asm_unknown_register_name) << Clobber); |
| 201 | } |
| 202 | |
| 203 | AsmStmt *NS = |
Chad Rosier | df4ee10 | 2012-08-20 17:11:53 +0000 | [diff] [blame] | 204 | new (Context) AsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs, |
| 205 | NumInputs, Names, Constraints, Exprs, AsmString, |
| 206 | NumClobbers, Clobbers, RParenLoc); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 207 | // Validate the asm string, ensuring it makes sense given the operands we |
| 208 | // have. |
| 209 | SmallVector<AsmStmt::AsmStringPiece, 8> Pieces; |
| 210 | unsigned DiagOffs; |
| 211 | if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) { |
| 212 | Diag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID) |
| 213 | << AsmString->getSourceRange(); |
| 214 | return StmtError(); |
| 215 | } |
| 216 | |
| 217 | // Validate tied input operands for type mismatches. |
| 218 | for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) { |
| 219 | TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i]; |
| 220 | |
| 221 | // If this is a tied constraint, verify that the output and input have |
| 222 | // either exactly the same type, or that they are int/ptr operands with the |
| 223 | // same size (int/long, int*/long, are ok etc). |
| 224 | if (!Info.hasTiedOperand()) continue; |
| 225 | |
| 226 | unsigned TiedTo = Info.getTiedOperand(); |
| 227 | unsigned InputOpNo = i+NumOutputs; |
| 228 | Expr *OutputExpr = Exprs[TiedTo]; |
| 229 | Expr *InputExpr = Exprs[InputOpNo]; |
| 230 | |
| 231 | if (OutputExpr->isTypeDependent() || InputExpr->isTypeDependent()) |
| 232 | continue; |
| 233 | |
| 234 | QualType InTy = InputExpr->getType(); |
| 235 | QualType OutTy = OutputExpr->getType(); |
| 236 | if (Context.hasSameType(InTy, OutTy)) |
| 237 | continue; // All types can be tied to themselves. |
| 238 | |
| 239 | // Decide if the input and output are in the same domain (integer/ptr or |
| 240 | // floating point. |
| 241 | enum AsmDomain { |
| 242 | AD_Int, AD_FP, AD_Other |
| 243 | } InputDomain, OutputDomain; |
| 244 | |
| 245 | if (InTy->isIntegerType() || InTy->isPointerType()) |
| 246 | InputDomain = AD_Int; |
| 247 | else if (InTy->isRealFloatingType()) |
| 248 | InputDomain = AD_FP; |
| 249 | else |
| 250 | InputDomain = AD_Other; |
| 251 | |
| 252 | if (OutTy->isIntegerType() || OutTy->isPointerType()) |
| 253 | OutputDomain = AD_Int; |
| 254 | else if (OutTy->isRealFloatingType()) |
| 255 | OutputDomain = AD_FP; |
| 256 | else |
| 257 | OutputDomain = AD_Other; |
| 258 | |
| 259 | // They are ok if they are the same size and in the same domain. This |
| 260 | // allows tying things like: |
| 261 | // void* to int* |
| 262 | // void* to int if they are the same size. |
| 263 | // double to long double if they are the same size. |
| 264 | // |
| 265 | uint64_t OutSize = Context.getTypeSize(OutTy); |
| 266 | uint64_t InSize = Context.getTypeSize(InTy); |
| 267 | if (OutSize == InSize && InputDomain == OutputDomain && |
| 268 | InputDomain != AD_Other) |
| 269 | continue; |
| 270 | |
| 271 | // If the smaller input/output operand is not mentioned in the asm string, |
| 272 | // then we can promote the smaller one to a larger input and the asm string |
| 273 | // won't notice. |
| 274 | bool SmallerValueMentioned = false; |
| 275 | |
| 276 | // If this is a reference to the input and if the input was the smaller |
| 277 | // one, then we have to reject this asm. |
| 278 | if (isOperandMentioned(InputOpNo, Pieces)) { |
| 279 | // This is a use in the asm string of the smaller operand. Since we |
| 280 | // codegen this by promoting to a wider value, the asm will get printed |
| 281 | // "wrong". |
| 282 | SmallerValueMentioned |= InSize < OutSize; |
| 283 | } |
| 284 | if (isOperandMentioned(TiedTo, Pieces)) { |
| 285 | // If this is a reference to the output, and if the output is the larger |
| 286 | // value, then it's ok because we'll promote the input to the larger type. |
| 287 | SmallerValueMentioned |= OutSize < InSize; |
| 288 | } |
| 289 | |
| 290 | // If the smaller value wasn't mentioned in the asm string, and if the |
| 291 | // output was a register, just extend the shorter one to the size of the |
| 292 | // larger one. |
| 293 | if (!SmallerValueMentioned && InputDomain != AD_Other && |
| 294 | OutputConstraintInfos[TiedTo].allowsRegister()) |
| 295 | continue; |
| 296 | |
| 297 | // Either both of the operands were mentioned or the smaller one was |
| 298 | // mentioned. One more special case that we'll allow: if the tied input is |
| 299 | // integer, unmentioned, and is a constant, then we'll allow truncating it |
| 300 | // down to the size of the destination. |
| 301 | if (InputDomain == AD_Int && OutputDomain == AD_Int && |
| 302 | !isOperandMentioned(InputOpNo, Pieces) && |
| 303 | InputExpr->isEvaluatable(Context)) { |
| 304 | CastKind castKind = |
| 305 | (OutTy->isBooleanType() ? CK_IntegralToBoolean : CK_IntegralCast); |
| 306 | InputExpr = ImpCastExprToType(InputExpr, OutTy, castKind).take(); |
| 307 | Exprs[InputOpNo] = InputExpr; |
| 308 | NS->setInputExpr(i, InputExpr); |
| 309 | continue; |
| 310 | } |
| 311 | |
| 312 | Diag(InputExpr->getLocStart(), |
| 313 | diag::err_asm_tying_incompatible_types) |
| 314 | << InTy << OutTy << OutputExpr->getSourceRange() |
| 315 | << InputExpr->getSourceRange(); |
| 316 | return StmtError(); |
| 317 | } |
| 318 | |
| 319 | return Owned(NS); |
| 320 | } |
| 321 | |
| 322 | // isMSAsmKeyword - Return true if this is an MS-style inline asm keyword. These |
| 323 | // require special handling. |
| 324 | static bool isMSAsmKeyword(StringRef Name) { |
| 325 | bool Ret = llvm::StringSwitch<bool>(Name) |
| 326 | .Cases("EVEN", "ALIGN", true) // Alignment directives. |
| 327 | .Cases("LENGTH", "SIZE", "TYPE", true) // Type and variable sizes. |
| 328 | .Case("_emit", true) // _emit Pseudoinstruction. |
| 329 | .Default(false); |
| 330 | return Ret; |
| 331 | } |
| 332 | |
Chad Rosier | 6e97be7 | 2012-08-22 23:42:09 +0000 | [diff] [blame] | 333 | // getIdentifierInfo - Given a Name and a range of tokens, find the associated |
| 334 | // IdentifierInfo*. |
| 335 | static IdentifierInfo *getIdentifierInfo(StringRef Name, |
| 336 | ArrayRef<Token> AsmToks, |
| 337 | unsigned Begin, unsigned End) { |
| 338 | for (unsigned i = Begin; i <= End; ++i) { |
| 339 | IdentifierInfo *II = AsmToks[i].getIdentifierInfo(); |
| 340 | if (II && II->getName() == Name) |
| 341 | return II; |
| 342 | } |
| 343 | return 0; |
| 344 | } |
| 345 | |
Chad Rosier | 358ab76 | 2012-08-22 21:08:06 +0000 | [diff] [blame] | 346 | // getSpelling - Get the spelling of the AsmTok token. |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 347 | static StringRef getSpelling(Sema &SemaRef, Token AsmTok) { |
| 348 | StringRef Asm; |
| 349 | SmallString<512> TokenBuf; |
| 350 | TokenBuf.resize(512); |
| 351 | bool StringInvalid = false; |
| 352 | Asm = SemaRef.PP.getSpelling(AsmTok, TokenBuf, &StringInvalid); |
| 353 | assert (!StringInvalid && "Expected valid string!"); |
| 354 | return Asm; |
| 355 | } |
| 356 | |
Chad Rosier | 358ab76 | 2012-08-22 21:08:06 +0000 | [diff] [blame] | 357 | // Determine if we should bail on this MSAsm instruction. |
Chad Rosier | 2735df2 | 2012-08-22 19:18:30 +0000 | [diff] [blame] | 358 | static bool bailOnMSAsm(std::vector<StringRef> Piece) { |
| 359 | for (unsigned i = 0, e = Piece.size(); i != e; ++i) |
| 360 | if (isMSAsmKeyword(Piece[i])) |
| 361 | return true; |
| 362 | return false; |
| 363 | } |
| 364 | |
Chad Rosier | 358ab76 | 2012-08-22 21:08:06 +0000 | [diff] [blame] | 365 | // Determine if we should bail on this MSAsm block. |
Chad Rosier | 2735df2 | 2012-08-22 19:18:30 +0000 | [diff] [blame] | 366 | static bool bailOnMSAsm(std::vector<std::vector<StringRef> > Pieces) { |
| 367 | for (unsigned i = 0, e = Pieces.size(); i != e; ++i) |
| 368 | if (bailOnMSAsm(Pieces[i])) |
| 369 | return true; |
| 370 | return false; |
| 371 | } |
| 372 | |
Chad Rosier | 358ab76 | 2012-08-22 21:08:06 +0000 | [diff] [blame] | 373 | // Determine if this is a simple MSAsm instruction. |
Chad Rosier | 98ac608 | 2012-08-21 23:09:21 +0000 | [diff] [blame] | 374 | static bool isSimpleMSAsm(std::vector<StringRef> &Pieces, |
| 375 | const TargetInfo &TI) { |
| 376 | if (isMSAsmKeyword(Pieces[0])) |
| 377 | return false; |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 378 | |
Chad Rosier | 98ac608 | 2012-08-21 23:09:21 +0000 | [diff] [blame] | 379 | for (unsigned i = 1, e = Pieces.size(); i != e; ++i) |
| 380 | if (!TI.isValidGCCRegisterName(Pieces[i])) |
| 381 | return false; |
Chad Rosier | 153f8ec | 2012-08-22 19:50:28 +0000 | [diff] [blame] | 382 | return true; |
| 383 | } |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 384 | |
Chad Rosier | 358ab76 | 2012-08-22 21:08:06 +0000 | [diff] [blame] | 385 | // Determine if this is a simple MSAsm block. |
Chad Rosier | 153f8ec | 2012-08-22 19:50:28 +0000 | [diff] [blame] | 386 | static bool isSimpleMSAsm(std::vector<std::vector<StringRef> > Pieces, |
| 387 | const TargetInfo &TI) { |
| 388 | for (unsigned i = 0, e = Pieces.size(); i != e; ++i) |
| 389 | if (!isSimpleMSAsm(Pieces[i], TI)) |
| 390 | return false; |
Chad Rosier | 98ac608 | 2012-08-21 23:09:21 +0000 | [diff] [blame] | 391 | return true; |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Chad Rosier | 358ab76 | 2012-08-22 21:08:06 +0000 | [diff] [blame] | 394 | // Break the AsmSting into pieces (i.e., mnemonic and operands). |
Chad Rosier | 38c71d3 | 2012-08-21 21:56:39 +0000 | [diff] [blame] | 395 | static void buildMSAsmPieces(StringRef Asm, std::vector<StringRef> &Pieces) { |
| 396 | std::pair<StringRef,StringRef> Split = Asm.split(' '); |
| 397 | |
| 398 | // Mnemonic |
| 399 | Pieces.push_back(Split.first); |
| 400 | Asm = Split.second; |
| 401 | |
| 402 | // Operands |
| 403 | while (!Asm.empty()) { |
| 404 | Split = Asm.split(", "); |
| 405 | Pieces.push_back(Split.first); |
| 406 | Asm = Split.second; |
| 407 | } |
| 408 | } |
| 409 | |
Chad Rosier | f0fbd77 | 2012-08-22 21:04:07 +0000 | [diff] [blame] | 410 | static void buildMSAsmPieces(std::vector<std::string> &AsmStrings, |
| 411 | std::vector<std::vector<StringRef> > &Pieces) { |
| 412 | for (unsigned i = 0, e = AsmStrings.size(); i != e; ++i) |
| 413 | buildMSAsmPieces(AsmStrings[i], Pieces[i]); |
| 414 | } |
| 415 | |
Chad Rosier | 682ad16 | 2012-08-22 21:12:19 +0000 | [diff] [blame] | 416 | // Build the unmodified AsmString used by the IR. Also build the individual |
| 417 | // asm instruction(s) and place them in the AsmStrings vector; these are fed |
| 418 | // to the AsmParser. |
Chad Rosier | 9072a02 | 2012-08-22 20:30:58 +0000 | [diff] [blame] | 419 | static std::string buildMSAsmString(Sema &SemaRef, ArrayRef<Token> AsmToks, |
| 420 | std::vector<std::string> &AsmStrings, |
| 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 | |
Chad Rosier | 38c71d3 | 2012-08-21 21:56:39 +0000 | [diff] [blame] | 424 | SmallString<512> Res; |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 425 | SmallString<512> Asm; |
Chad Rosier | 9072a02 | 2012-08-22 20:30:58 +0000 | [diff] [blame] | 426 | unsigned startTok = 0; |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 427 | for (unsigned i = 0, e = AsmToks.size(); i < e; ++i) { |
| 428 | bool isNewAsm = i == 0 || AsmToks[i].isAtStartOfLine() || |
| 429 | AsmToks[i].is(tok::kw_asm); |
| 430 | |
| 431 | if (isNewAsm) { |
Chad Rosier | 38c71d3 | 2012-08-21 21:56:39 +0000 | [diff] [blame] | 432 | if (i) { |
| 433 | AsmStrings.push_back(Asm.c_str()); |
Chad Rosier | 9072a02 | 2012-08-22 20:30:58 +0000 | [diff] [blame] | 434 | AsmTokRanges.push_back(std::make_pair(startTok, i-1)); |
| 435 | startTok = i; |
Chad Rosier | 38c71d3 | 2012-08-21 21:56:39 +0000 | [diff] [blame] | 436 | Res += Asm; |
| 437 | Asm.clear(); |
| 438 | Res += '\n'; |
| 439 | } |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 440 | if (AsmToks[i].is(tok::kw_asm)) { |
| 441 | i++; // Skip __asm |
| 442 | assert (i != e && "Expected another token"); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | if (i && AsmToks[i].hasLeadingSpace() && !isNewAsm) |
| 447 | Asm += ' '; |
| 448 | |
| 449 | Asm += getSpelling(SemaRef, AsmToks[i]); |
| 450 | } |
Chad Rosier | 38c71d3 | 2012-08-21 21:56:39 +0000 | [diff] [blame] | 451 | AsmStrings.push_back(Asm.c_str()); |
Chad Rosier | 9072a02 | 2012-08-22 20:30:58 +0000 | [diff] [blame] | 452 | AsmTokRanges.push_back(std::make_pair(startTok, AsmToks.size()-1)); |
Chad Rosier | 38c71d3 | 2012-08-21 21:56:39 +0000 | [diff] [blame] | 453 | Res += Asm; |
| 454 | return Res.c_str(); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Chad Rosier | 2735df2 | 2012-08-22 19:18:30 +0000 | [diff] [blame] | 457 | #define DEF_SIMPLE_MSASM \ |
| 458 | MSAsmStmt *NS = \ |
| 459 | new (Context) MSAsmStmt(Context, AsmLoc, LBraceLoc, /*IsSimple*/ true, \ |
| 460 | /*IsVolatile*/ true, AsmToks, Inputs, Outputs, \ |
| 461 | AsmString, Clobbers, EndLoc); |
| 462 | |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 463 | StmtResult Sema::ActOnMSAsmStmt(SourceLocation AsmLoc, |
| 464 | SourceLocation LBraceLoc, |
| 465 | ArrayRef<Token> AsmToks, |
| 466 | SourceLocation EndLoc) { |
| 467 | // MS-style inline assembly is not fully supported, so emit a warning. |
| 468 | Diag(AsmLoc, diag::warn_unsupported_msasm); |
| 469 | SmallVector<StringRef,4> Clobbers; |
| 470 | std::set<std::string> ClobberRegs; |
| 471 | SmallVector<IdentifierInfo*, 4> Inputs; |
| 472 | SmallVector<IdentifierInfo*, 4> Outputs; |
| 473 | |
| 474 | // Empty asm statements don't need to instantiate the AsmParser, etc. |
| 475 | if (AsmToks.empty()) { |
| 476 | StringRef AsmString; |
Chad Rosier | 2735df2 | 2012-08-22 19:18:30 +0000 | [diff] [blame] | 477 | DEF_SIMPLE_MSASM; |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 478 | return Owned(NS); |
| 479 | } |
| 480 | |
Chad Rosier | 38c71d3 | 2012-08-21 21:56:39 +0000 | [diff] [blame] | 481 | std::vector<std::string> AsmStrings; |
Chad Rosier | 9072a02 | 2012-08-22 20:30:58 +0000 | [diff] [blame] | 482 | std::vector<std::pair<unsigned,unsigned> > AsmTokRanges; |
| 483 | std::string AsmString = buildMSAsmString(*this, AsmToks, AsmStrings, AsmTokRanges); |
Chad Rosier | 38c71d3 | 2012-08-21 21:56:39 +0000 | [diff] [blame] | 484 | |
Chad Rosier | e78460f | 2012-08-22 20:57:07 +0000 | [diff] [blame] | 485 | std::vector<std::vector<StringRef> > Pieces(AsmStrings.size()); |
Chad Rosier | f0fbd77 | 2012-08-22 21:04:07 +0000 | [diff] [blame] | 486 | buildMSAsmPieces(AsmStrings, Pieces); |
Chad Rosier | 153f8ec | 2012-08-22 19:50:28 +0000 | [diff] [blame] | 487 | |
| 488 | bool IsSimple = isSimpleMSAsm(Pieces, Context.getTargetInfo()); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 489 | |
Chad Rosier | 2735df2 | 2012-08-22 19:18:30 +0000 | [diff] [blame] | 490 | // AsmParser doesn't fully support these asm statements. |
| 491 | if (bailOnMSAsm(Pieces)) { DEF_SIMPLE_MSASM; return Owned(NS); } |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 492 | |
| 493 | // Initialize targets and assembly printers/parsers. |
| 494 | llvm::InitializeAllTargetInfos(); |
| 495 | llvm::InitializeAllTargetMCs(); |
| 496 | llvm::InitializeAllAsmParsers(); |
| 497 | |
| 498 | // Get the target specific parser. |
| 499 | std::string Error; |
| 500 | const std::string &TT = Context.getTargetInfo().getTriple().getTriple(); |
| 501 | const llvm::Target *TheTarget(llvm::TargetRegistry::lookupTarget(TT, Error)); |
| 502 | |
| 503 | OwningPtr<llvm::MCAsmInfo> MAI(TheTarget->createMCAsmInfo(TT)); |
| 504 | OwningPtr<llvm::MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TT)); |
| 505 | OwningPtr<llvm::MCObjectFileInfo> MOFI(new llvm::MCObjectFileInfo()); |
| 506 | OwningPtr<llvm::MCSubtargetInfo> |
| 507 | STI(TheTarget->createMCSubtargetInfo(TT, "", "")); |
| 508 | |
Chad Rosier | 25bd298 | 2012-08-23 15:44:35 +0000 | [diff] [blame] | 509 | for (unsigned StrIdx = 0, e = AsmStrings.size(); StrIdx != e; ++StrIdx) { |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 510 | llvm::SourceMgr SrcMgr; |
| 511 | llvm::MCContext Ctx(*MAI, *MRI, MOFI.get(), &SrcMgr); |
| 512 | llvm::MemoryBuffer *Buffer = |
Chad Rosier | 25bd298 | 2012-08-23 15:44:35 +0000 | [diff] [blame] | 513 | llvm::MemoryBuffer::getMemBuffer(AsmStrings[StrIdx], "<inline asm>"); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 514 | |
| 515 | // Tell SrcMgr about this buffer, which is what the parser will pick up. |
| 516 | SrcMgr.AddNewSourceBuffer(Buffer, llvm::SMLoc()); |
| 517 | |
| 518 | OwningPtr<llvm::MCStreamer> Str(createNullStreamer(Ctx)); |
| 519 | OwningPtr<llvm::MCAsmParser> |
| 520 | Parser(createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI)); |
| 521 | OwningPtr<llvm::MCTargetAsmParser> |
| 522 | TargetParser(TheTarget->createMCAsmParser(*STI, *Parser)); |
| 523 | // Change to the Intel dialect. |
| 524 | Parser->setAssemblerDialect(1); |
| 525 | Parser->setTargetParser(*TargetParser.get()); |
| 526 | |
| 527 | // Prime the lexer. |
| 528 | Parser->Lex(); |
| 529 | |
| 530 | // Parse the opcode. |
| 531 | StringRef IDVal; |
| 532 | Parser->ParseIdentifier(IDVal); |
| 533 | |
| 534 | // Canonicalize the opcode to lower case. |
| 535 | SmallString<128> Opcode; |
Matt Beaumont-Gay | 2d5a1de | 2012-08-23 01:51:18 +0000 | [diff] [blame] | 536 | for (unsigned j = 0, e = IDVal.size(); j != e; ++j) |
| 537 | Opcode.push_back(tolower(IDVal[j])); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 538 | |
| 539 | // Parse the operands. |
| 540 | llvm::SMLoc IDLoc; |
| 541 | SmallVector<llvm::MCParsedAsmOperand*, 8> Operands; |
| 542 | bool HadError = TargetParser->ParseInstruction(Opcode.str(), IDLoc, |
| 543 | Operands); |
Chad Rosier | 2735df2 | 2012-08-22 19:18:30 +0000 | [diff] [blame] | 544 | // If we had an error parsing the operands, fail gracefully. |
| 545 | if (HadError) { DEF_SIMPLE_MSASM; return Owned(NS); } |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 546 | |
| 547 | // Match the MCInstr. |
Chad Rosier | 83591b6 | 2012-08-21 18:15:08 +0000 | [diff] [blame] | 548 | unsigned ErrorInfo; |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 549 | SmallVector<llvm::MCInst, 2> Instrs; |
Chad Rosier | 83591b6 | 2012-08-21 18:15:08 +0000 | [diff] [blame] | 550 | HadError = TargetParser->MatchInstruction(IDLoc, Operands, Instrs, |
Chad Rosier | 51a6b3f | 2012-08-21 19:37:55 +0000 | [diff] [blame] | 551 | ErrorInfo, |
| 552 | /*matchingInlineAsm*/ true); |
Chad Rosier | 2735df2 | 2012-08-22 19:18:30 +0000 | [diff] [blame] | 553 | // If we had an error parsing the operands, fail gracefully. |
| 554 | if (HadError) { DEF_SIMPLE_MSASM; return Owned(NS); } |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 555 | |
| 556 | // Get the instruction descriptor. |
| 557 | llvm::MCInst Inst = Instrs[0]; |
| 558 | const llvm::MCInstrInfo *MII = TheTarget->createMCInstrInfo(); |
| 559 | const llvm::MCInstrDesc &Desc = MII->get(Inst.getOpcode()); |
| 560 | llvm::MCInstPrinter *IP = |
| 561 | TheTarget->createMCInstPrinter(1, *MAI, *MII, *MRI, *STI); |
| 562 | |
Chad Rosier | 6e97be7 | 2012-08-22 23:42:09 +0000 | [diff] [blame] | 563 | // Build the list of clobbers, outputs and inputs. |
Chad Rosier | fd5e56e | 2012-08-22 22:10:51 +0000 | [diff] [blame] | 564 | unsigned NumDefs = Desc.getNumDefs(); |
Chad Rosier | 25bd298 | 2012-08-23 15:44:35 +0000 | [diff] [blame] | 565 | for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) { |
| 566 | const llvm::MCOperand &Op = Inst.getOperand(i); |
Chad Rosier | fd5e56e | 2012-08-22 22:10:51 +0000 | [diff] [blame] | 567 | |
| 568 | // Immediate. |
| 569 | if (Op.isImm() || Op.isFPImm()) |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 570 | continue; |
| 571 | |
Chad Rosier | 25bd298 | 2012-08-23 15:44:35 +0000 | [diff] [blame] | 572 | bool isDef = NumDefs && (i < NumDefs); |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 573 | |
Chad Rosier | fd5e56e | 2012-08-22 22:10:51 +0000 | [diff] [blame] | 574 | // Register/Clobber. |
| 575 | if (Op.isReg() && isDef) { |
| 576 | std::string Reg; |
| 577 | llvm::raw_string_ostream OS(Reg); |
| 578 | IP->printRegName(OS, Op.getReg()); |
| 579 | |
| 580 | StringRef Clobber(OS.str()); |
| 581 | if (!Context.getTargetInfo().isValidClobber(Clobber)) |
| 582 | return StmtError(Diag(AsmLoc, diag::err_asm_unknown_register_name) << |
| 583 | Clobber); |
| 584 | ClobberRegs.insert(Reg); |
Chad Rosier | 6e97be7 | 2012-08-22 23:42:09 +0000 | [diff] [blame] | 585 | continue; |
| 586 | } |
| 587 | // Expr/Input or Output. |
| 588 | if (Op.isExpr()) { |
| 589 | const llvm::MCExpr *Expr = Op.getExpr(); |
| 590 | const llvm::MCSymbolRefExpr *SymRef; |
| 591 | if ((SymRef = dyn_cast<llvm::MCSymbolRefExpr>(Expr))) { |
| 592 | StringRef Name = SymRef->getSymbol().getName(); |
Chad Rosier | 60ce584 | 2012-08-23 00:12:51 +0000 | [diff] [blame] | 593 | IdentifierInfo *II = getIdentifierInfo(Name, AsmToks, |
Chad Rosier | 25bd298 | 2012-08-23 15:44:35 +0000 | [diff] [blame] | 594 | AsmTokRanges[StrIdx].first, |
| 595 | AsmTokRanges[StrIdx].second); |
Chad Rosier | 6e97be7 | 2012-08-22 23:42:09 +0000 | [diff] [blame] | 596 | if (II) |
| 597 | isDef ? Outputs.push_back(II) : Inputs.push_back(II); |
| 598 | } |
Chad Rosier | fd5e56e | 2012-08-22 22:10:51 +0000 | [diff] [blame] | 599 | } |
Chad Rosier | 4b5e48d | 2012-08-17 21:19:40 +0000 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | for (std::set<std::string>::iterator I = ClobberRegs.begin(), |
| 603 | E = ClobberRegs.end(); I != E; ++I) |
| 604 | Clobbers.push_back(*I); |
| 605 | |
| 606 | MSAsmStmt *NS = |
| 607 | new (Context) MSAsmStmt(Context, AsmLoc, LBraceLoc, IsSimple, |
| 608 | /*IsVolatile*/ true, AsmToks, Inputs, Outputs, |
| 609 | AsmString, Clobbers, EndLoc); |
| 610 | return Owned(NS); |
| 611 | } |