struct layout not the same as dmd

Iain Buclaw avatarIain Buclaw created an issue

Thought I might cherry pick 3 more issues myself, so here goes.

Originally raised http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=521622

gdc on i386 inserts four bytes of padding in this struct (so that it's total size is 16 bytes), while gcc and dmd doesn't:

import std.stdio;

extern(C)
struct test {
  int mode;
  double mu;
};

void main()
{
  writefln("test.sizeof: ", test.sizeof);
  writefln("test.mode.sizeof: ", test.mode.sizeof);
  writefln("test.mu.sizeof: ", test.mu.sizeof);
}

According to http://www.digitalmars.com/d/1.0/abi.html, structs should conform "to the target's C ABI struct layout", so it looks like this is a bug.

Comments (3)

  1. Iain Buclaw

    Turns out that the alignment sizes of 64bit types are target system dependant, Darwin, Linux and BSD systems are rounded to 4 bytes, whilst Cygwin, Mingw and Windows targets are rounded to 8 bytes. The frontend distinguishes between the two via an "#if TARGET_LINUX || ..." macro in mtype.c at about line 1075.

    Wanting to keep an independence between compiler host and target, to maintain cross compiling, what we could do is define the TARGET_* identifiers in d-confdefs.h (via the target-ver-syms.sh script) and include the header in the dmd/* sources as and when needed.

    Michael, do you think the attached patch is reasonable?

  2. Iain Buclaw

    Pushed into 6f03952ff48f .

    Following unit test compiles on D1.

    struct st {
      double d;
      int i;
    }
    void main() {
      static assert (st.sizeof == (st.d.sizeof + st.i.sizeof));
    }
    

    It still fails on D2, but no worries, as will be fixed in a later DMD frontend update.

    Marking as resolved.

  3. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.