[ms-inline asm] MSVC parses multiple __asm statements on a single line as one

statement.  For example,

  if (x)
    __asm out dx, ax  __asm out dx, ax

results in a single inline asm statement (i.e., both "out dx, ax" statements are
predicated on if(x)).

llvm-svn: 161986
This commit is contained in:
Chad Rosier 2012-08-15 21:03:27 +00:00
parent d63f1f5a9c
commit aa7c1cb5f8
4 changed files with 33 additions and 20 deletions

View File

@ -1686,11 +1686,6 @@ StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
if (Tok.is(tok::eof))
break;
// The asm keyword is a statement separator, so multiple asm statements
// are allowed on a single line.
if (!InAsmComment && Tok.is(tok::kw_asm))
break;
if (!InAsmComment && Tok.is(tok::semi)) {
// A semicolon in an asm is the start of a comment.
InAsmComment = true;

View File

@ -2777,12 +2777,21 @@ static void patchMSAsmStrings(Sema &SemaRef, bool &IsSimple,
unsigned NumAsmStrings = 0;
for (unsigned i = 0, e = AsmToks.size(); i != e; ++i) {
// Determine if this should be considered a new asm.
bool isNewAsm = i == 0 || AsmToks[i].isAtStartOfLine() ||
AsmToks[i].is(tok::kw_asm);
// Emit the previous asm string.
if (i && AsmToks[i].isAtStartOfLine())
if (i && isNewAsm) {
AsmStrings[NumAsmStrings++] = Asm.c_str();
if (AsmToks[i].is(tok::kw_asm)) {
++i; // Skip __asm
assert (i != e && "Expected another token.");
}
}
// Start a new asm string with the opcode.
if (i == 0 || AsmToks[i].isAtStartOfLine()) {
if (isNewAsm) {
Asm = AsmToks[i].getIdentifierInfo()->getName().str();
continue;
}
@ -2836,12 +2845,24 @@ static std::string buildMSAsmString(Sema &SemaRef,
SmallString<512> Asm;
SmallString<512> TokenBuf;
TokenBuf.resize(512);
for (unsigned i = 0, e = AsmToks.size(); i < e; ++i) {
bool StringInvalid = false;
if (i && AsmToks[i].isAtStartOfLine())
Asm += '\n';
else if (i && AsmToks[i].hasLeadingSpace())
bool isNewAsm = i == 0 || AsmToks[i].isAtStartOfLine() ||
AsmToks[i].is(tok::kw_asm);
if (isNewAsm) {
if (i)
Asm += '\n';
if (AsmToks[i].is(tok::kw_asm)) {
i++; // Skip __asm
assert (i != e && "Expected another token");
}
}
if (i && AsmToks[i].hasLeadingSpace() && !isNewAsm)
Asm += ' ';
bool StringInvalid = false;
Asm += SemaRef.PP.getSpelling(AsmToks[i], TokenBuf, &StringInvalid);
assert (!StringInvalid && "Expected valid string!");
}
@ -2876,7 +2897,7 @@ StmtResult Sema::ActOnMSAsmStmt(SourceLocation AsmLoc,
// FIXME: Count this while parsing.
unsigned NumAsmStrings = 0;
for (unsigned i = 0, e = AsmToks.size(); i != e; ++i)
if (AsmToks[i].isAtStartOfLine())
if (i == 0 || AsmToks[i].isAtStartOfLine() || AsmToks[i].is(tok::kw_asm))
++NumAsmStrings;
PatchedAsmStrings.resize(NumAsmStrings ? NumAsmStrings : 1);

View File

@ -20,9 +20,7 @@ void t2() {
void t3() {
// CHECK: @t3
// CHECK: call void asm sideeffect "nop", "~{dirflag},~{fpsr},~{flags}"() nounwind ia_nsdialect
// CHECK: call void asm sideeffect "nop", "~{dirflag},~{fpsr},~{flags}"() nounwind ia_nsdialect
// CHECK: call void asm sideeffect "nop", "~{dirflag},~{fpsr},~{flags}"() nounwind ia_nsdialect
// CHECK: call void asm sideeffect "nop\0Anop\0Anop", "~{dirflag},~{fpsr},~{flags}"() nounwind ia_nsdialect
// CHECK: ret void
__asm nop __asm nop __asm nop
}
@ -38,8 +36,7 @@ void t4(void) {
void t5(void) {
// CHECK: @t5
// CHECK: call void asm sideeffect "mov ebx, eax", "~{ebx},~{dirflag},~{fpsr},~{flags}"() nounwind ia_nsdialect
// CHECK: call void asm sideeffect "mov ecx, ebx", "~{ecx},~{dirflag},~{fpsr},~{flags}"() nounwind ia_nsdialect
// CHECK: call void asm sideeffect "mov ebx, eax\0Amov ecx, ebx", "~{ebx},~{ecx},~{dirflag},~{fpsr},~{flags}"() nounwind ia_nsdialect
// CHECK: ret void
__asm mov ebx, eax __asm mov ecx, ebx
}

View File

@ -28,10 +28,10 @@ int t7() {
}
}
void t8() {
__asm nop __asm nop __asm nop // expected-warning {{MS-style inline assembly is not supported}} expected-warning {{MS-style inline assembly is not supported}} expected-warning {{MS-style inline assembly is not supported}}
__asm nop __asm nop __asm nop // expected-warning {{MS-style inline assembly is not supported}}
}
void t9() {
__asm nop __asm nop ; __asm nop // expected-warning {{MS-style inline assembly is not supported}} expected-warning {{MS-style inline assembly is not supported}}
__asm nop __asm nop ; __asm nop // expected-warning {{MS-style inline assembly is not supported}}
}
int t_fail() { // expected-note {{to match this}}
__asm