Mercury Bugs - mercury
View Issue Details
0000240mercuryBugpublic2011-12-02 20:082011-12-09 10:26
Reportermaclarty 
Assigned Towangp 
PrioritynormalSeveritymajorReproducibilityalways
StatusresolvedResolutionfixed 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version 
Summary0000240: float unboxing issue on MinGW
DescriptionThe attached program produces incorrect output when compiled on MinGW with rotd-2011-11-29 in grade hlc.gc at -O1 or lower:

$ mmc bug.m -O1 --grade hlc.gc
$ ./bug
0.0
123.0
$ mmc bug.m -O2 --grade hlc.gc
$ ./bug
123.0
123.0

$ uname -a
MINGW32_NT-6.1 TTT 1.0.17(0.48/3/2) 2011-04-24 23:39 i686 Msys

$ gcc --version
gcc.exe (GCC) 4.6.1

11.07-beta-2011-11-30 works.
TagsNo tags attached.
Attached Files? bug.m (474) 2011-12-02 20:08
https://bugs.mercurylang.org/file_download.php?file_id=144&type=bug

Notes
(0000422)
juliensf   
2011-12-02 20:53   
Hi,

Does the same also occur with the asm_fast.gc grade on MinGW? Does the same occur on other
non-Windows 32-bit machines.
(If the problem is not restricted to MinGW then it's probably an issue to related to Peter's changes
for unboxed doubles, since that didn't go onto the 11.07 branch.)
(0000423)
maclarty   
2011-12-02 21:40   
The problem does not occur with asm_fast.gc or 32bit Linux. I also compared the c file generated on Linux and Windows and there are no differences. Note that I used a different version of gcc on Linux (4.4.3).
(0000424)
juliensf   
2011-12-02 21:49   
I have MinGW gcc 4.5.0 on my laptop so I will give that a try. We should try 4.6.1 on Linux or Mac OS X.
(0000425)
juliensf   
2011-12-03 04:58   
It also occurs with gcc 4.5.0 on MinGW.
(0000427)
juliensf   
2011-12-03 05:03   
-O[01] --common-struct works

-O1 --inline-simple causes both values to be printed incorrectly.
(0000428)
maclarty   
2011-12-03 11:59   
I believe the cause of the problem is some incorrect assumptions about how struct fields are aligned. Consider the following C program:

#include <stdio.h>
struct t {
    void *f1;
    double f2;
};
int main() {
    printf("%p\n", &((struct t*)0)->f2);
}

On MinGW this produces 8, but on 32 bit Linux it produces 4.

In the C code generated for bug.m there is the following struct:

struct bug_scalar_cell_group_1 {
  MR_Box f1;
  MR_Float f2;
};

Later on we use MR_hl_field to extract the float field. However MR_hl_field casts the struct to an array of pointers and then looks up element 1. That would look at offset 4, but on MinGW, the float field is at offset 8.
(0000429)
wangp   
2011-12-03 12:36   
That'd be it, thanks.

See also bug 0000211
(0000435)
wangp   
2011-12-09 10:25   
Fix committed.

Issue History
2011-12-02 20:08maclartyNew Issue
2011-12-02 20:08maclartyFile Added: bug.m
2011-12-02 20:53juliensfNote Added: 0000422
2011-12-02 20:53juliensfStatusnew => feedback
2011-12-02 21:40maclartyNote Added: 0000423
2011-12-02 21:49juliensfNote Added: 0000424
2011-12-03 04:58juliensfNote Added: 0000425
2011-12-03 05:03juliensfNote Added: 0000427
2011-12-03 11:59maclartyNote Added: 0000428
2011-12-03 12:36wangpNote Added: 0000429
2011-12-05 11:00wangpStatusfeedback => assigned
2011-12-05 11:00wangpAssigned To => wangp
2011-12-09 10:25wangpNote Added: 0000435
2011-12-09 10:26wangpStatusassigned => resolved
2011-12-09 10:26wangpResolutionopen => fixed