SemaStmtAsm.cpp revision 180d9d98fb51c7f6719d56a73bfd09400502986c
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"
43using namespace clang;
44using 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.
53static 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.
81static 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
95StmtResult 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.
325static 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// Check to see if the expression is a substring of the asm operand.
335static StringRef getMSInlineAsmExprName(StringRef Name) {
336  // Strip off the size directives.
337  // E.g., DWORD PTR [V] -> V
338  if (Name.startswith("BYTE") || Name.startswith("byte") ||
339      Name.startswith("WORD") || Name.startswith("word") ||
340      Name.startswith("DWORD") || Name.startswith("dword") ||
341      Name.startswith("QWORD") || Name.startswith("qword") ||
342      Name.startswith("XWORD") || Name.startswith("xword") ||
343      Name.startswith("XMMWORD") || Name.startswith("xmmword") ||
344      Name.startswith("YMMWORD") || Name.startswith("ymmword")) {
345    std::pair< StringRef, StringRef > SplitName = Name.split(' ');
346    assert((SplitName.second.startswith("PTR") ||
347            SplitName.second.startswith("ptr")) &&
348           "Expected PTR/ptr!");
349    SplitName = SplitName.second.split('[');
350    SplitName = SplitName.second.split(']');
351    return SplitName.first;
352  }
353  return Name;
354}
355
356// getIdentifierInfo - Given a Name and a range of tokens, find the associated
357// IdentifierInfo*.
358static IdentifierInfo *getIdentifierInfo(StringRef Name,
359                                         ArrayRef<Token> AsmToks,
360                                         unsigned Begin, unsigned End) {
361  for (unsigned i = Begin; i <= End; ++i) {
362    IdentifierInfo *II = AsmToks[i].getIdentifierInfo();
363    if (II && II->getName() == Name)
364      return II;
365  }
366  return 0;
367}
368
369// getSpelling - Get the spelling of the AsmTok token.
370static StringRef getSpelling(Sema &SemaRef, Token AsmTok) {
371  StringRef Asm;
372  SmallString<512> TokenBuf;
373  TokenBuf.resize(512);
374  bool StringInvalid = false;
375  Asm = SemaRef.PP.getSpelling(AsmTok, TokenBuf, &StringInvalid);
376  assert (!StringInvalid && "Expected valid string!");
377  return Asm;
378}
379
380// Determine if we should bail on this MSAsm instruction.
381static bool bailOnMSAsm(std::vector<StringRef> Piece) {
382  for (unsigned i = 0, e = Piece.size(); i != e; ++i)
383    if (isMSAsmKeyword(Piece[i]))
384      return true;
385  return false;
386}
387
388// Determine if we should bail on this MSAsm block.
389static bool bailOnMSAsm(std::vector<std::vector<StringRef> > Pieces) {
390  for (unsigned i = 0, e = Pieces.size(); i != e; ++i)
391    if (bailOnMSAsm(Pieces[i]))
392      return true;
393  return false;
394}
395
396// Determine if this is a simple MSAsm instruction.
397static bool isSimpleMSAsm(std::vector<StringRef> &Pieces,
398                          const TargetInfo &TI) {
399  if (isMSAsmKeyword(Pieces[0]))
400      return false;
401
402  for (unsigned i = 1, e = Pieces.size(); i != e; ++i) {
403    StringRef Op = getMSInlineAsmExprName(Pieces[i]);
404    if (!TI.isValidGCCRegisterName(Op))
405      return false;
406  }
407  return true;
408}
409
410// Determine if this is a simple MSAsm block.
411static bool isSimpleMSAsm(std::vector<std::vector<StringRef> > Pieces,
412                          const TargetInfo &TI) {
413  for (unsigned i = 0, e = Pieces.size(); i != e; ++i)
414    if (!isSimpleMSAsm(Pieces[i], TI))
415      return false;
416  return true;
417}
418
419// Break the AsmString into pieces (i.e., mnemonic and operands).
420static void buildMSAsmPieces(StringRef Asm, std::vector<StringRef> &Pieces) {
421  std::pair<StringRef,StringRef> Split = Asm.split(' ');
422
423  // Mnemonic
424  Pieces.push_back(Split.first);
425  Asm = Split.second;
426
427  // Operands
428  while (!Asm.empty()) {
429    Split = Asm.split(", ");
430    Pieces.push_back(Split.first);
431    Asm = Split.second;
432  }
433}
434
435static void buildMSAsmPieces(std::vector<std::string> &AsmStrings,
436                             std::vector<std::vector<StringRef> > &Pieces) {
437  for (unsigned i = 0, e = AsmStrings.size(); i != e; ++i)
438    buildMSAsmPieces(AsmStrings[i], Pieces[i]);
439}
440
441// Build the individual assembly instruction(s) and place them in the AsmStrings
442// vector.  These strings are fed to the AsmParser.  Returns true on error.
443static bool buildMSAsmStrings(Sema &SemaRef,
444                              SourceLocation AsmLoc,
445                              ArrayRef<Token> AsmToks,
446                              std::vector<std::string> &AsmStrings,
447                     std::vector<std::pair<unsigned,unsigned> > &AsmTokRanges) {
448  assert (!AsmToks.empty() && "Didn't expect an empty AsmToks!");
449
450  SmallString<512> Asm;
451  unsigned startTok = 0;
452  for (unsigned i = 0, e = AsmToks.size(); i < e; ++i) {
453    bool isNewAsm = ((i == 0) ||
454                     AsmToks[i].isAtStartOfLine() ||
455                     AsmToks[i].is(tok::kw_asm));
456
457    if (isNewAsm) {
458      if (i) {
459        AsmStrings.push_back(Asm.str());
460        AsmTokRanges.push_back(std::make_pair(startTok, i-1));
461        startTok = i;
462        Asm.clear();
463      }
464      if (AsmToks[i].is(tok::kw_asm)) {
465        i++; // Skip __asm
466        if (i == e) {
467          SemaRef.Diag(AsmLoc, diag::err_asm_empty);
468          return true;
469        }
470      }
471    }
472
473    if (i && AsmToks[i].hasLeadingSpace() && !isNewAsm)
474      Asm += ' ';
475
476    StringRef Spelling = getSpelling(SemaRef, AsmToks[i]);
477    Asm += Spelling;
478  }
479  AsmStrings.push_back(Asm.str());
480  AsmTokRanges.push_back(std::make_pair(startTok, AsmToks.size()-1));
481
482  return false;
483}
484
485#define DEF_SIMPLE_MSASM(STR)                                                \
486  MSAsmStmt *NS =                                                            \
487    new (Context) MSAsmStmt(Context, AsmLoc, LBraceLoc, /*IsSimple*/ true,   \
488                            /*IsVolatile*/ true, AsmToks, Inputs, Outputs,   \
489                            InputExprs, OutputExprs, STR, Constraints,       \
490                            Clobbers, EndLoc);
491
492StmtResult Sema::ActOnMSAsmStmt(SourceLocation AsmLoc, SourceLocation LBraceLoc,
493                                ArrayRef<Token> AsmToks,SourceLocation EndLoc) {
494  SmallVector<StringRef, 4> Constraints;
495  std::vector<std::string> InputConstraints;
496  std::vector<std::string> OutputConstraints;
497  SmallVector<StringRef, 4> Clobbers;
498  std::set<std::string> ClobberRegs;
499
500  // FIXME: Use a struct to hold the various expression information.
501  SmallVector<IdentifierInfo*, 4> Inputs;
502  SmallVector<IdentifierInfo*, 4> Outputs;
503  SmallVector<Expr*, 4> InputExprs;
504  SmallVector<Expr*, 4> OutputExprs;
505  SmallVector<std::string, 4> InputExprNames;
506  SmallVector<std::string, 4> OutputExprNames;
507  SmallVector<unsigned, 4> InputExprStrIdx;
508  SmallVector<unsigned, 4> OutputExprStrIdx;
509
510  // Empty asm statements don't need to instantiate the AsmParser, etc.
511  StringRef EmptyAsmStr;
512  if (AsmToks.empty()) { DEF_SIMPLE_MSASM(EmptyAsmStr); return Owned(NS); }
513
514  std::vector<std::string> AsmStrings;
515  std::vector<std::pair<unsigned,unsigned> > AsmTokRanges;
516  if (buildMSAsmStrings(*this, AsmLoc, AsmToks, AsmStrings, AsmTokRanges))
517    return StmtError();
518
519  std::vector<std::vector<StringRef> > Pieces(AsmStrings.size());
520  buildMSAsmPieces(AsmStrings, Pieces);
521
522  bool IsSimple = isSimpleMSAsm(Pieces, Context.getTargetInfo());
523
524  // AsmParser doesn't fully support these asm statements.
525  if (bailOnMSAsm(Pieces)) { DEF_SIMPLE_MSASM(EmptyAsmStr); return Owned(NS); }
526
527  // Get the target specific parser.
528  std::string Error;
529  const std::string &TT = Context.getTargetInfo().getTriple().getTriple();
530  const llvm::Target *TheTarget(llvm::TargetRegistry::lookupTarget(TT, Error));
531
532  OwningPtr<llvm::MCAsmInfo> MAI(TheTarget->createMCAsmInfo(TT));
533  OwningPtr<llvm::MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TT));
534  OwningPtr<llvm::MCObjectFileInfo> MOFI(new llvm::MCObjectFileInfo());
535  OwningPtr<llvm::MCSubtargetInfo>
536    STI(TheTarget->createMCSubtargetInfo(TT, "", ""));
537
538  for (unsigned StrIdx = 0, e = AsmStrings.size(); StrIdx != e; ++StrIdx) {
539    llvm::SourceMgr SrcMgr;
540    llvm::MCContext Ctx(*MAI, *MRI, MOFI.get(), &SrcMgr);
541    llvm::MemoryBuffer *Buffer =
542      llvm::MemoryBuffer::getMemBuffer(AsmStrings[StrIdx], "<inline asm>");
543
544    // Tell SrcMgr about this buffer, which is what the parser will pick up.
545    SrcMgr.AddNewSourceBuffer(Buffer, llvm::SMLoc());
546
547    OwningPtr<llvm::MCStreamer> Str(createNullStreamer(Ctx));
548    OwningPtr<llvm::MCAsmParser>
549      Parser(createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI));
550    OwningPtr<llvm::MCTargetAsmParser>
551      TargetParser(TheTarget->createMCAsmParser(*STI, *Parser));
552    // Change to the Intel dialect.
553    Parser->setAssemblerDialect(1);
554    Parser->setTargetParser(*TargetParser.get());
555
556    // Prime the lexer.
557    Parser->Lex();
558
559    // Parse the opcode.
560    StringRef IDVal;
561    Parser->ParseIdentifier(IDVal);
562
563    // Canonicalize the opcode to lower case.
564    SmallString<128> OpcodeStr;
565    for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
566      OpcodeStr.push_back(tolower(IDVal[i]));
567    // FIXME: Convert to a StmtError.
568    assert(TargetParser->mnemonicIsValid(OpcodeStr) && "Invalid mnemonic!");
569
570    // Parse the operands.
571    llvm::SMLoc IDLoc;
572    SmallVector<llvm::MCParsedAsmOperand*, 8> Operands;
573    bool HadError = TargetParser->ParseInstruction(OpcodeStr.str(), IDLoc,
574                                                   Operands);
575    // If we had an error parsing the operands, fail gracefully.
576    if (HadError) { DEF_SIMPLE_MSASM(EmptyAsmStr); return Owned(NS); }
577
578    // Match the MCInstr.
579    unsigned Kind;
580    unsigned Opcode;
581    unsigned ErrorInfo;
582    SmallVector<std::pair< unsigned, std::string >, 4> MapAndConstraints;
583    HadError = TargetParser->MatchInstruction(IDLoc, Operands, *Str.get(), Kind,
584                                              Opcode, MapAndConstraints,
585                                              ErrorInfo,
586                                              /*matchingInlineAsm*/ true);
587    // If we had an error parsing the operands, fail gracefully.
588    if (HadError) { DEF_SIMPLE_MSASM(EmptyAsmStr); return Owned(NS); }
589
590    // Get the instruction descriptor.
591    const llvm::MCInstrInfo *MII = TheTarget->createMCInstrInfo();
592    const llvm::MCInstrDesc &Desc = MII->get(Opcode);
593    llvm::MCInstPrinter *IP =
594      TheTarget->createMCInstPrinter(1, *MAI, *MII, *MRI, *STI);
595
596    // Build the list of clobbers, outputs and inputs.
597    unsigned NumDefs = Desc.getNumDefs();
598    for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
599      // Skip immediates.
600      if (Operands[i]->isImm())
601        continue;
602
603      // Register.
604      if (Operands[i]->isReg()) {
605        // Clobber.
606        if (NumDefs && (MapAndConstraints[i-1].first < NumDefs)) {
607          std::string Reg;
608          llvm::raw_string_ostream OS(Reg);
609          IP->printRegName(OS, Operands[i]->getReg());
610          StringRef Clobber(OS.str());
611          if (!Context.getTargetInfo().isValidClobber(Clobber))
612            return StmtError(
613              Diag(AsmLoc, diag::err_asm_unknown_register_name) << Clobber);
614          ClobberRegs.insert(Reg);
615        }
616        continue;
617      }
618
619      // Expr/Input or Output.
620      StringRef Name = getMSInlineAsmExprName(Pieces[StrIdx][i]);
621
622      // The expr may be a register.
623      // E.g., DWORD PTR [eax]
624      if (Context.getTargetInfo().isValidGCCRegisterName(Name))
625        continue;
626
627      IdentifierInfo *II = getIdentifierInfo(Name, AsmToks,
628                                             AsmTokRanges[StrIdx].first,
629                                             AsmTokRanges[StrIdx].second);
630      if (II) {
631        CXXScopeSpec SS;
632        UnqualifiedId Id;
633        SourceLocation Loc;
634        Id.setIdentifier(II, AsmLoc);
635        ExprResult Result = ActOnIdExpression(getCurScope(), SS, Loc, Id,
636                                              false, false);
637        if (!Result.isInvalid()) {
638          // FIXME: Determine the proper constraints.
639          bool isMemDef = (i == 1) && Desc.mayStore();
640          if (isMemDef) {
641            Outputs.push_back(II);
642            OutputExprs.push_back(Result.take());
643            OutputExprNames.push_back(Name.str());
644            OutputExprStrIdx.push_back(StrIdx);
645
646            std::string Constraint = "=" + MapAndConstraints[i-1].second;
647            OutputConstraints.push_back(Constraint);
648          } else {
649            Inputs.push_back(II);
650            InputExprs.push_back(Result.take());
651            InputExprNames.push_back(Name.str());
652            InputExprStrIdx.push_back(StrIdx);
653            InputConstraints.push_back(MapAndConstraints[i-1].second);
654          }
655        }
656      }
657    }
658  }
659  for (std::set<std::string>::iterator I = ClobberRegs.begin(),
660         E = ClobberRegs.end(); I != E; ++I)
661    Clobbers.push_back(*I);
662
663  // Merge the output and input constraints.  Output constraints are expected
664  // first.
665  for (std::vector<std::string>::iterator I = OutputConstraints.begin(),
666         E = OutputConstraints.end(); I != E; ++I)
667    Constraints.push_back(*I);
668
669  for (std::vector<std::string>::iterator I = InputConstraints.begin(),
670         E = InputConstraints.end(); I != E; ++I)
671    Constraints.push_back(*I);
672
673  // Enumerate the AsmString expressions.
674  unsigned OpNum = 0;
675  for (unsigned i = 0, e = OutputExprNames.size(); i != e; ++i, ++OpNum) {
676    unsigned StrIdx = OutputExprStrIdx[i];
677    // Iterate over the assembly instruction pieces, skipping the mnemonic.
678    for (unsigned j = 1, f = Pieces[StrIdx].size(); j != f; ++j) {
679      // If the operand and the expression name match, then rewrite the operand.
680      if (OutputExprNames[i] == Pieces[StrIdx][j]) {
681        SmallString<32> Res;
682        llvm::raw_svector_ostream OS(Res);
683        OS << '$' << OpNum;
684        OutputExprNames[i] = OS.str();
685        Pieces[StrIdx][j] = OutputExprNames[i];
686        break;
687      }
688      // Check to see if the expression is a substring of the asm piece.
689      std::pair< StringRef, StringRef > Split =	Pieces[StrIdx][j].split(' ');
690      bool isKeyword = llvm::StringSwitch<bool>(Split.first)
691        .Cases("BYTE", "byte", "WORD", "word", "DWORD", true)
692        .Cases("dword", "QWORD", "qword", "XWORD", "xword", true)
693        .Cases("XMMWORD", "xmmword", "YMMWORD", "ymmword", true)
694        .Default(false);
695      if (isKeyword &&
696          Split.second.find_first_of(OutputExprNames[i]) != StringRef::npos) {
697        // Is is a substring, do the replacement.
698        SmallString<32> Res;
699        llvm::raw_svector_ostream OS(Res);
700        OS << '$' << OpNum;
701        std::string piece = Pieces[StrIdx][j].str();
702        size_t found = piece.find(InputExprNames[i]);
703        piece.replace(found, InputExprNames[i].size(), OS.str());
704        OutputExprNames[i] = piece;
705        Pieces[StrIdx][j] = OutputExprNames[i];
706        break;
707      }
708    }
709  }
710  for (unsigned i = 0, e = InputExprNames.size(); i != e; ++i, ++OpNum) {
711    unsigned StrIdx = InputExprStrIdx[i];
712    // Iterate over the assembly instruction pieces, skipping the mnemonic.
713    for (unsigned j = 1, f = Pieces[StrIdx].size(); j != f; ++j) {
714      // If the operand and the expression name match, then rewrite the operand.
715      if (InputExprNames[i] == Pieces[StrIdx][j]) {
716        SmallString<32> Res;
717        llvm::raw_svector_ostream OS(Res);
718        OS << '$' << OpNum;
719        InputExprNames[i] = OS.str();
720        Pieces[StrIdx][j] = InputExprNames[i];
721        break;
722      }
723      // Check to see if the expression is a substring of the asm piece.
724      std::pair< StringRef, StringRef > Split =	Pieces[StrIdx][j].split(' ');
725      bool isKeyword = llvm::StringSwitch<bool>(Split.first)
726        .Cases("BYTE", "byte", "WORD", "word", "DWORD", true)
727        .Cases("dword", "QWORD", "qword", "XWORD", "xword", true)
728        .Cases("XMMWORD", "xmmword", "YMMWORD", "ymmword", true)
729        .Default(false);
730      if (isKeyword &&
731          Split.second.find_first_of(InputExprNames[i]) != StringRef::npos) {
732        // It is a substring, do the replacement.
733        SmallString<32> Res;
734        llvm::raw_svector_ostream OS(Res);
735        OS << '$' << OpNum;
736        std::string piece = Pieces[StrIdx][j].str();
737        size_t found = piece.find(InputExprNames[i]);
738        piece.replace(found, InputExprNames[i].size(), OS.str());
739        InputExprNames[i] = piece;
740        Pieces[StrIdx][j] = InputExprNames[i];
741        break;
742      }
743    }
744  }
745
746  // Emit the IR assembly string.
747  std::string AsmString;
748  for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
749    // Skip empty asm stmts.
750    if (Pieces[i].empty()) continue;
751
752    if (i > 0)
753      AsmString += "\n\t";
754
755    // Emit the mnemonic.
756    AsmString += Pieces[i][0];
757    if (Pieces[i].size() > 1)
758      AsmString += ' ';
759
760    // Emit the operands adding $$ to constants.
761    for (unsigned j = 1, f = Pieces[i].size(); j != f; ++j) {
762      if (j > 1) AsmString += ", ";
763      unsigned Val;
764      if (!Pieces[i][j].getAsInteger(0, Val))
765        AsmString += "$$";
766
767      AsmString += Pieces[i][j];
768    }
769  }
770
771  MSAsmStmt *NS =
772    new (Context) MSAsmStmt(Context, AsmLoc, LBraceLoc, IsSimple,
773                            /*IsVolatile*/ true, AsmToks, Inputs, Outputs,
774                            InputExprs, OutputExprs, AsmString, Constraints,
775                            Clobbers, EndLoc);
776  return Owned(NS);
777}
778