<div dir="ltr"><div><div><div><div><div><div><div><div>Hi,<br><br></div>I'm currently working on a project in which I start from a semantic description of an assembly basic block, and I want to output a semantically similar basic block through LLVM.<br><br></div>For instance:<br><br></div> - Semantic:<br>EAX = EBX + 2<br>ECX = EBX<br><br></div> - Expected output:<br></div>lea eax, [ebx + 2]<br></div>mov ebx, ecx<br><br></div>For now, I have a working PoC using inline asm (for loading a register and saving to it). I also load and save registers to avoid having them erased with temporary values.<br><br></div><div>But I get an unexpected behavior using the ESP register. It seems that the backend "ignores" the relation between ESP value and the stack.<br><br></div><div>For instance:<br><br></div><div> - Semantic:<br><br>@32[ESP - 4] = EAX (that is to say, write at (ESP - 4) a 32 bits value, EAX)<br><br></div><div>- Output:<br><br>   0:    55                       push   %ebp<br>   1:    53                       push   %ebx<br>   2:    57                       push   %edi<br>   3:    56                       push   %esi<br>   4:    89 44 24 fc              mov    %eax,-0x4(%esp)<br>   8:    5e                       pop    %esi<br>   9:    5f                       pop    %edi<br>   a:    5b                       pop    %ebx<br>   b:    5d                       pop    %ebp<br><br></div><div>Which is obviously wrong.<br></div><div>The corresponding IR LLVM:<br><br>define void @"fc"() <br>{<br>entry:<br>  %".2" = call i32 asm  "", "={eax}"<br>()<br>  %".3" = call i32 asm  "", "={ecx}"<br>()<br>...<br>  %".6" = call i32 asm  "", "={esp}"<br>()<br>...<br>  %".18" = add i32 %".6", 4294967292<br>  %".19" = inttoptr i32 %".18" to i32*<br>  store i32 %".2", i32* %".19"<br>  call void asm sideeffect "", "{eax}"<br>(i32 %".2")<br>...<br>  call void asm sideeffect "", "{esp}"<br>(i32 %".6")<br>...<br>  ret void<br>}<br><br></div><div>I understand that LLVM is not intended to work with this kind of ASM mix, but maybe I'm missing a feature or something.<br></div><div><br></div><div>A hack would be to use an `alloca` at the beginning of the first basic block, assuming that it will be mapped relatively to ESP, and then re-arranging my memory accesses based on it. But it seems to be a very hacky way to do it...<br><br></div><div>If you have any idea, I will be happy to test it and provide you feedback.<br></div><div>Thanks in advance,<br><br></div><div>-- commial<br></div></div>