diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index c2594ba2ac21..b66544c92801 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -3375,6 +3375,10 @@ public: bool IsUnevaluatedContext); bool LookupInlineAsmField(StringRef Base, StringRef Member, unsigned &Offset, SourceLocation AsmLoc); + ExprResult LookupInlineAsmVarDeclField(Expr *RefExpr, StringRef Member, + unsigned &Offset, + llvm::InlineAsmIdentifierInfo &Info, + SourceLocation AsmLoc); StmtResult ActOnMSAsmStmt(SourceLocation AsmLoc, SourceLocation LBraceLoc, ArrayRef AsmToks, StringRef AsmString, diff --git a/clang/lib/Parse/ParseStmtAsm.cpp b/clang/lib/Parse/ParseStmtAsm.cpp index 8cdae6a74b20..d3281393f923 100644 --- a/clang/lib/Parse/ParseStmtAsm.cpp +++ b/clang/lib/Parse/ParseStmtAsm.cpp @@ -222,6 +222,25 @@ ExprResult Parser::ParseMSAsmIdentifier(llvm::SmallVectorImpl &LineToks, /*AllowConstructorName=*/false, /*ObjectType=*/ParsedType(), TemplateKWLoc, Id); + // Perform the lookup. + ExprResult Result = Actions.LookupInlineAsmIdentifier( + SS, TemplateKWLoc, Id, Info, IsUnevaluatedContext); + + // While the next two tokens are 'period' 'identifier', repeatedly parse it as + // a field access. We have to avoid consuming assembler directives that look + // like '.' 'else'. + while (Result.isUsable() && Tok.is(tok::period)) { + Token IdTok = PP.LookAhead(0); + if (IdTok.isNot(tok::identifier)) + break; + ConsumeToken(); // Consume the period. + IdentifierInfo *Id = Tok.getIdentifierInfo(); + ConsumeToken(); // Consume the identifier. + unsigned OffsetUnused; + Result = Actions.LookupInlineAsmVarDeclField( + Result.get(), Id->getName(), OffsetUnused, Info, Tok.getLocation()); + } + // Figure out how many tokens we are into LineToks. unsigned LineIndex = 0; if (Tok.is(EndOfStream)) { @@ -254,9 +273,7 @@ ExprResult Parser::ParseMSAsmIdentifier(llvm::SmallVectorImpl &LineToks, LineToks.pop_back(); LineToks.pop_back(); - // Perform the lookup. - return Actions.LookupInlineAsmIdentifier(SS, TemplateKWLoc, Id, Info, - IsUnevaluatedContext); + return Result; } /// Turn a sequence of our tokens back into a string that we can hand diff --git a/clang/lib/Sema/SemaStmtAsm.cpp b/clang/lib/Sema/SemaStmtAsm.cpp index 799689f19951..52987cc13164 100644 --- a/clang/lib/Sema/SemaStmtAsm.cpp +++ b/clang/lib/Sema/SemaStmtAsm.cpp @@ -528,6 +528,17 @@ StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple, return NS; } +static void fillInlineAsmTypeInfo(const ASTContext &Context, QualType T, + llvm::InlineAsmIdentifierInfo &Info) { + // Compute the type size (and array length if applicable?). + Info.Type = Info.Size = Context.getTypeSizeInChars(T).getQuantity(); + if (T->isArrayType()) { + const ArrayType *ATy = Context.getAsArrayType(T); + Info.Type = Context.getTypeSizeInChars(ATy->getElementType()).getQuantity(); + Info.Length = Info.Size / Info.Type; + } +} + ExprResult Sema::LookupInlineAsmIdentifier(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, UnqualifiedId &Id, @@ -575,13 +586,7 @@ ExprResult Sema::LookupInlineAsmIdentifier(CXXScopeSpec &SS, return ExprError(); } - // Compute the type size (and array length if applicable?). - Info.Type = Info.Size = Context.getTypeSizeInChars(T).getQuantity(); - if (T->isArrayType()) { - const ArrayType *ATy = Context.getAsArrayType(T); - Info.Type = Context.getTypeSizeInChars(ATy->getElementType()).getQuantity(); - Info.Length = Info.Size / Info.Type; - } + fillInlineAsmTypeInfo(Context, T, Info); // We can work with the expression as long as it's not an r-value. if (!Result.get()->isRValue()) @@ -636,6 +641,49 @@ bool Sema::LookupInlineAsmField(StringRef Base, StringRef Member, return false; } +ExprResult +Sema::LookupInlineAsmVarDeclField(Expr *E, StringRef Member, unsigned &Offset, + llvm::InlineAsmIdentifierInfo &Info, + SourceLocation AsmLoc) { + Info.clear(); + + const RecordType *RT = E->getType()->getAs(); + // FIXME: Diagnose this as field access into a scalar type. + if (!RT) + return ExprResult(); + + LookupResult FieldResult(*this, &Context.Idents.get(Member), AsmLoc, + LookupMemberName); + + if (!LookupQualifiedName(FieldResult, RT->getDecl())) + return ExprResult(); + + // Only normal and indirect field results will work. + ValueDecl *FD = dyn_cast(FieldResult.getFoundDecl()); + if (!FD) + FD = dyn_cast(FieldResult.getFoundDecl()); + if (!FD) + return ExprResult(); + + Offset = (unsigned)Context.toCharUnitsFromBits(Context.getFieldOffset(FD)) + .getQuantity(); + + // Make an Expr to thread through OpDecl. + ExprResult Result = BuildMemberReferenceExpr( + E, E->getType(), AsmLoc, /*IsArrow=*/false, CXXScopeSpec(), + SourceLocation(), nullptr, FieldResult, nullptr); + if (Result.isInvalid()) + return Result; + Info.OpDecl = Result.get(); + + fillInlineAsmTypeInfo(Context, Result.get()->getType(), Info); + + // Fields are "variables" as far as inline assembly is concerned. + Info.IsVarDecl = true; + + return Result; +} + StmtResult Sema::ActOnMSAsmStmt(SourceLocation AsmLoc, SourceLocation LBraceLoc, ArrayRef AsmToks, StringRef AsmString, diff --git a/clang/test/CodeGen/ms-inline-asm.c b/clang/test/CodeGen/ms-inline-asm.c index 4f04a8bb502a..cc0fb06dd039 100644 --- a/clang/test/CodeGen/ms-inline-asm.c +++ b/clang/test/CodeGen/ms-inline-asm.c @@ -563,3 +563,21 @@ void label4() { // CHECK-LABEL: define void @label4 // CHECK: call void asm sideeffect inteldialect "{{.*}}__MSASMLABEL_.4__label:\0A\09mov eax, {{.*}}__MSASMLABEL_.4__label", "~{eax},~{dirflag},~{fpsr},~{flags}"() } + +typedef union _LARGE_INTEGER { + struct { + unsigned int LowPart; + unsigned int HighPart; + }; + struct { + unsigned int LowPart; + unsigned int HighPart; + } u; + unsigned long long QuadPart; +} LARGE_INTEGER, *PLARGE_INTEGER; + +int test_indirect_field(LARGE_INTEGER LargeInteger) { + __asm mov eax, LargeInteger.LowPart +} +// CHECK-LABEL: define i32 @test_indirect_field( +// CHECK: call i32 asm sideeffect inteldialect "mov eax, dword ptr $1", diff --git a/clang/test/CodeGenCXX/ms-inline-asm-fields.cpp b/clang/test/CodeGenCXX/ms-inline-asm-fields.cpp new file mode 100644 index 000000000000..a78d511485aa --- /dev/null +++ b/clang/test/CodeGenCXX/ms-inline-asm-fields.cpp @@ -0,0 +1,31 @@ +// REQUIRES: x86-registered-target +// RUN: %clang_cc1 %s -triple i386-apple-darwin10 -fasm-blocks -emit-llvm -o - | FileCheck %s + +struct A { + int a1; + int a2; + struct B { + int b1; + int b2; + } a3; +}; + +namespace asdf { +A a_global; +} + +extern "C" int test_param_field(A p) { +// CHECK: define i32 @test_param_field(%struct.A* byval align 4 %p) +// CHECK: getelementptr inbounds %struct.A, %struct.A* %p, i32 0, i32 0 +// CHECK: call i32 asm sideeffect inteldialect "mov eax, dword ptr $1" +// CHECK: ret i32 + __asm mov eax, p.a1 +} + +extern "C" int test_namespace_global() { +// CHECK: define i32 @test_namespace_global() +// CHECK: call i32 asm sideeffect inteldialect "mov eax, dword ptr $1", "{{.*}}"(i32* getelementptr inbounds (%struct.A, %struct.A* @_ZN4asdf8a_globalE, i32 0, i32 2, i32 1)) +// CHECK: ret i32 + __asm mov eax, asdf::a_global.a3.b2 +} + diff --git a/clang/test/SemaCXX/ms-inline-asm.cpp b/clang/test/SemaCXX/ms-inline-asm.cpp new file mode 100644 index 000000000000..3e445b80f887 --- /dev/null +++ b/clang/test/SemaCXX/ms-inline-asm.cpp @@ -0,0 +1,54 @@ +// REQUIRES: x86-registered-target +// RUN: %clang_cc1 %s -triple i386-apple-darwin10 -fasm-blocks -verify + +struct A { + int a1; + int a2; + struct B { + int b1; + int b2; + enum { kValue = 42 }; + } a3; + struct { + int indirect_field; + }; +}; + +namespace asdf { +A a_global; +} + +// The parser combines adjacent __asm blocks into one. Avoid that by calling +// this. +void split_inline_asm_call(); + +void test_field_lookup() { + __asm mov eax, asdf::a_global.a3.b2 + split_inline_asm_call(); + + // FIXME: These diagnostics are crap. + + // expected-error@+1 {{undeclared label}} + __asm mov eax, asdf::a_global.not_a_field.b2 + split_inline_asm_call(); + + // expected-error@+1 {{undeclared label}} + __asm mov eax, asdf::a_global.a3.not_a_field + split_inline_asm_call(); + + __asm mov eax, A::B::kValue + split_inline_asm_call(); + + // expected-error@+1 {{undeclared label}} + __asm mov eax, asdf::a_global.a3.kValue + split_inline_asm_call(); + + __asm mov eax, asdf :: a_global.a3.b2 + split_inline_asm_call(); + + __asm mov eax, asdf::a_global . a3 . b2 + split_inline_asm_call(); + + __asm mov eax, asdf::a_global.indirect_field + split_inline_asm_call(); +}