Index: clang/test/CodeGen/ms-inline-asm-variables.c =================================================================== --- clang/test/CodeGen/ms-inline-asm-variables.c +++ clang/test/CodeGen/ms-inline-asm-variables.c @@ -11,7 +11,7 @@ __asm add ebx, dword ptr gVar[271 - 82 + 81 + ebx] // CHECK: add dword ptr ${{[0-9]}}[ebx + $$828], ebx __asm add dword ptr [ebx + gVar + 828], ebx - // CHECK: add ecx, dword ptr ${{[0-9]}}[ecx + ecx * $$4 + $$4590] + // CHECK: add ecx, dword ptr ${{{[0-9]}}:P}[ecx + ecx * $$4 + $$4590] __asm add ecx, dword ptr gVar[4590 + ecx + ecx*4] // CHECK: add dword ptr ${{[0-9]}}[ecx + ecx * $$8 + $$73], ecx __asm add dword ptr [gVar + ecx + 45 + 23 - 53 + 60 - 2 + ecx*8], ecx Index: llvm/lib/MC/MCParser/AsmParser.cpp =================================================================== --- llvm/lib/MC/MCParser/AsmParser.cpp +++ llvm/lib/MC/MCParser/AsmParser.cpp @@ -5944,6 +5944,54 @@ llvm_unreachable("Unstable rewrite sort."); } +// IntelExpr with [BaseReg + Index + ...] +static bool isSpecialIntelExprAppend(SmallVector &AsmStrRewrites, + SmallVector::iterator It) { + if (It == AsmStrRewrites.end()) + return false; + + ++It; + if (It == AsmStrRewrites.end()) { + --It; + return false; + } + + const AsmRewrite &AR = *It; + --It; + if (AR.Kind != AOK_IntelExpr) + return false; + + if(!AR.IntelExp.hasBaseReg() || !AR.IntelExp.hasIndexReg()) + return false; + + const AsmRewrite &PreAR = *It; + char *Ptr1 = const_cast(PreAR.Loc.getPointer()); + char *Ptr2 = const_cast(AR.Loc.getPointer()); + + Ptr1 += PreAR.Len; + assert (Ptr1 <= Ptr2 && "Unexpected Asm Sting Pointer!"); + + // In most case, there are ARR[..] or ARR []. + // Ptr1 ^ Ptr1 ^ + // ^ Ptr2 ^ Ptr2 + // So quickly return true. + if (Ptr1 + 1 >= Ptr2) + return true; + + // The IntelExpr not append the Pre-Input if they are breaked by instruction(s). + // For example: + // mov eax,ARR + // call dword ptr[edx+eax*4] + // We can't see them as ARR[edx+eax*4]. + while (++Ptr1 < Ptr2) { + char C = *Ptr1; + if (C != ' ' || C != '\t' || C != '\n') + return false; + } + + return true; +} + bool AsmParser::parseMSInlineAsm( std::string &AsmString, unsigned &NumOutputs, unsigned &NumInputs, SmallVectorImpl> &OpDecls, @@ -6150,7 +6198,12 @@ OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label; break; case AOK_Input: - OS << '$' << InputIdx++; + // The Symbol of IntelExpr input can not transfer to pc related + // mode, when the IntelExpr contrains both base reg and index reg. + if (isSpecialIntelExprAppend(AsmStrRewrites, it)) + OS << "${" << InputIdx++ << ":P}"; + else + OS << '$' << InputIdx++; break; case AOK_CallInput: OS << "${" << InputIdx++ << ":P}";