D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 14872 - [2.068.2] Label address in asm [x86-64]
Summary: [2.068.2] Label address in asm [x86-64]
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 All
: P1 major
Assignee: No Owner
URL:
Keywords: iasm, pull, wrong-code
Depends on:
Blocks:
 
Reported: 2015-08-05 15:49 UTC by Daniel
Modified: 2020-08-27 08:44 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Daniel 2015-08-05 15:49:03 UTC
32bit x86 asm works fine, but I can't get 64bit working.

import std.stdio;

void main()
{
  size_t addr1=123;
  size_t addr2=456;

  version(D_InlineAsm_X86_64)
    asm
    {
      lea RAX,  lbl1;
      mov addr1, RAX;
      lea RAX,  lbl2;
      mov addr2, RAX;
    }
  else version(D_InlineAsm_X86)
    asm
    {
      lea EAX,  lbl1;
      mov addr1, EAX;
      lea EAX,  lbl2;
      mov addr2, EAX;
    }

lbl1: 
  writeln(addr1); // equals 1 for X86-64
lbl2: 
  writeln(addr2); // equals 1 for X86-64
}
Comment 1 Daniel 2015-09-29 19:54:44 UTC
I tested that it works with ldc2 and saw some interesting things when disassembling at asm.dlang.org

dmd -m32
lea    %cs:0x28,%eax
mov    %eax,-0x8(%ebp)
lea    %cs:0x30,%eax
mov    %eax,-0x4(%ebp)

dmd -m64
cs
mov    %rax,%rax
mov    %rax,-0x10(%rbp)
cs
mov    %rax,%rax
mov    %rax,-0x8(%rbp)

ldc2 -m64
leaq	.L_Dmain_lbl1, %rax
movq	%rax, -72(%rbp)
leaq	.L_Dmain_lbl2, %rax
movq	%rax, -80(%rbp)
Comment 2 Vladimir Panteleev 2017-07-21 07:16:14 UTC
(In reply to Daniel from comment #0)
> 32bit x86 asm works fine, but I can't get 64bit working.

I think that has something to do with Intel x64 being position-independent, meaning that "lea RAX, lbl1" is not going to do what you want. It looks like in case of DMD, it's generating nonsense code (mov eax, eax), but it could still use the offset relative to some base. In any case, it certainly won't be the actual in-address memory of the code at that label (so that you can "jmp" to it).

So, your example is a bit nonsensical, but DMD should still not be emitting garbage code, and either generate an error or use some other kind of offset (so that it's at least possible to use the offset in calculations to produce something useful).
Comment 3 Dlang Bot 2020-08-27 05:43:44 UTC
@WalterBright created dlang/dmd pull request #11631 "fix Issue 14872 - [2.068.2] Label address in asm [x86-64]" fixing this issue:

- fix Issue 14872 - [2.068.2] Label address in asm [x86-64]

https://github.com/dlang/dmd/pull/11631
Comment 4 Dlang Bot 2020-08-27 08:44:27 UTC
dlang/dmd pull request #11631 "fix Issue 14872 - [2.068.2] Label address in asm [x86-64]" was merged into master:

- b8a8d6f183342bb5bd9944595218661efcb22b08 by Walter Bright:
  fix Issue 14872 - [2.068.2] Label address in asm [x86-64]

https://github.com/dlang/dmd/pull/11631