From 958c61ad2091fc748f9ca089a4a228bc87ded11e Mon Sep 17 00:00:00 2001 From: Coby Tayree Date: Wed, 9 Aug 2017 13:31:41 +0000 Subject: [PATCH] [X86][Ms-InlineAsm] Extend MS Dot operator to accept "this" + struct/class pointers aliases MS InlineAsm Dot operator accepts "Bases" such as "this" (cpp) and class/struct pointer typedef. This patch enhance its implementation with this behavior. Differential Revision: https://reviews.llvm.org/D36450 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@310472 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaStmtAsm.cpp | 27 +++++++++++++++++++-------- test/CodeGen/ms-inline-asm.c | 8 ++++++-- test/CodeGen/ms-inline-asm.cpp | 16 ++++++++++++++++ 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/lib/Sema/SemaStmtAsm.cpp b/lib/Sema/SemaStmtAsm.cpp index c182b35bfa..603f970dbc 100644 --- a/lib/Sema/SemaStmtAsm.cpp +++ b/lib/Sema/SemaStmtAsm.cpp @@ -677,22 +677,33 @@ bool Sema::LookupInlineAsmField(StringRef Base, StringRef Member, SmallVector Members; Member.split(Members, "."); - LookupResult BaseResult(*this, &Context.Idents.get(Base), SourceLocation(), - LookupOrdinaryName); + NamedDecl *FoundDecl = nullptr; - if (!LookupName(BaseResult, getCurScope())) - return true; - - if(!BaseResult.isSingleResult()) + // MS InlineAsm uses 'this' as a base + if (getLangOpts().CPlusPlus && Base.equals("this")) { + if (const Type *PT = getCurrentThisType().getTypePtrOrNull()) + FoundDecl = PT->getPointeeType()->getAsTagDecl(); + } else { + LookupResult BaseResult(*this, &Context.Idents.get(Base), SourceLocation(), + LookupOrdinaryName); + if (LookupName(BaseResult, getCurScope()) && BaseResult.isSingleResult()) + FoundDecl = BaseResult.getFoundDecl(); + } + + if (!FoundDecl) return true; - NamedDecl *FoundDecl = BaseResult.getFoundDecl(); + for (StringRef NextMember : Members) { const RecordType *RT = nullptr; if (VarDecl *VD = dyn_cast(FoundDecl)) RT = VD->getType()->getAs(); else if (TypedefNameDecl *TD = dyn_cast(FoundDecl)) { MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false); - RT = TD->getUnderlyingType()->getAs(); + // MS InlineAsm often uses struct pointer aliases as a base + QualType QT = TD->getUnderlyingType(); + if (const auto *PT = QT->getAs()) + QT = PT->getPointeeType(); + RT = QT->getAs(); } else if (TypeDecl *TD = dyn_cast(FoundDecl)) RT = TD->getTypeForDecl()->getAs(); else if (FieldDecl *TD = dyn_cast(FoundDecl)) diff --git a/test/CodeGen/ms-inline-asm.c b/test/CodeGen/ms-inline-asm.c index 60dffb02b8..9f6852fd47 100644 --- a/test/CodeGen/ms-inline-asm.c +++ b/test/CodeGen/ms-inline-asm.c @@ -527,7 +527,7 @@ void cpuid() { typedef struct { int a; int b; -} A; +} A, *pA; typedef struct { int b1; @@ -539,13 +539,15 @@ typedef struct { A c2; int c3; B c4; -} C; +} C, *pC; void t39() { // CHECK-LABEL: define void @t39 __asm mov eax, [eax].A.b // CHECK: mov eax, [eax].4 __asm mov eax, [eax] A.b +// CHECK: mov eax, [eax] .4 + __asm mov eax, [eax] pA.b // CHECK: mov eax, [eax] .4 __asm mov eax, fs:[0] A.b // CHECK: mov eax, fs:[$$0] .4 @@ -556,6 +558,8 @@ void t39() { __asm mov eax, fs:[0] C.c2.b // CHECK: mov eax, fs:[$$0] .8 __asm mov eax, [eax]C.c4.b2.b +// CHECK: mov eax, [eax].24 + __asm mov eax, [eax]pC.c4.b2.b // CHECK: mov eax, [eax].24 // CHECK: "~{eax},~{dirflag},~{fpsr},~{flags}"() } diff --git a/test/CodeGen/ms-inline-asm.cpp b/test/CodeGen/ms-inline-asm.cpp index a435e4b826..094e34aed2 100644 --- a/test/CodeGen/ms-inline-asm.cpp +++ b/test/CodeGen/ms-inline-asm.cpp @@ -180,3 +180,19 @@ void t8() { A::g(); } +void t9() { + // CHECK-LABEL: define void @_Z2t9v() + struct A { + int a; + int b; + void g() { + __asm mov eax, dword ptr [eax]this.b + // CHECK: call void asm sideeffect inteldialect + // CHECK-SAME: mov eax, dword ptr [eax].4 + // CHECK-SAME: "~{eax},~{dirflag},~{fpsr},~{flags}"() + } + }; + A AA; + AA.g(); +} + -- 2.40.0