www.digitalmars.com         C & C++   DMDScript  

D.gnu - Porting GDC to QNX

reply Sheff <sheffmail mail.ru> writes:
Hi!
I'm porting GDC compiler to QNX (my previous posts related to this topic are:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmar
.D&article_id=51281 and
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=51456)
I'm almost done here, everything is working great, except one little thing:
optimization. It seems to me that -O1, -O2 and -O3 (but not -Os) options
generate bad code. The problem is that the following code doesn't work as
expected:

    char[] a = "abcd";
    char[] r;

    r = a.dup.reverse;
    assert(r == "dcba");

    a = "a\u1235\u1234c";
    r = a.dup.reverse;
    assert(r == "c\u1234\u1235a");

When you compile it without optimization everything is ok, but when you use -O
for example, the following happens:

function _adReverseChar (that's the function that is called when you use
.reverse) works for a very very very long time, that happens because the
"char[] a" parameter that is passed to that function is passed wrongly, if you
call a.length then you'll see that it'll return something like 134_XXX_XXX, i.e
a very large number though the real size of a string is 4.

What could be the problem ? I really don't have a clue, because same code works
fine with -O in linux and QNX is a lot like linux. So it's unlikely that gcc
optimizer works different in linux and qnx, that means that I probably forgot
to turn on/off some switches while compiling GDC, does anyone have any ideas
what exactly could it be ?
Apr 22 2007
parent reply Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sheff schrieb am 2007-04-22:
 Hi!
 I'm porting GDC compiler to QNX (my previous posts related to this topic are:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmar
.D&article_id=51281 and
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=51456)
 I'm almost done here, everything is working great, except one little thing:
optimization. It seems to me that -O1, -O2 and -O3 (but not -Os) options
generate bad code. The problem is that the following code doesn't work as
expected:

     char[] a = "abcd";
     char[] r;

     r = a.dup.reverse;
     assert(r == "dcba");

     a = "a\u1235\u1234c";
     r = a.dup.reverse;
     assert(r == "c\u1234\u1235a");

 When you compile it without optimization everything is ok, but when you use -O
for example,
 the following happens:

 function _adReverseChar (that's the function that is called when you use
.reverse)
 works for a very very very long time, that happens because the "char[] a"
parameter
 that is passed to that function is passed wrongly, if you call a.length then
you'll
 see that it'll return something like 134_XXX_XXX, i.e a very large number
though the
 real size of a string is 4.

What is the ouput of the below code if it is run inside _adReverseChar? # printf("LEN %zX\n", a.length); # printf("PTR %zX\n", a.ptr); # size_t* data = &a; # printf("pre %zX\n", *(data-1)); # printf("len %zX\n", *(data)); # printf("ptr %zX\n", *(data+1)); # printf("post %zX\n", *(data+2)); Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFGLJYZLK5blCcjpWoRAmH5AJ9Kr2t386L1mD1pkg1Y2RXuY0Y3iACdGYwt MyznI2I11SSGFAcjRBEic54= =rPGB -----END PGP SIGNATURE-----
Apr 23 2007
parent Sheff <sheffmail mail.ru> writes:
Thomas Kuehne Wrote:

 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1
 
 Sheff schrieb am 2007-04-22:
 Hi!
 I'm porting GDC compiler to QNX (my previous posts related to this topic are:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmar
.D&article_id=51281 and
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=51456)
 I'm almost done here, everything is working great, except one little thing:
optimization. It seems to me that -O1, -O2 and -O3 (but not -Os) options
generate bad code. The problem is that the following code doesn't work as
expected:

     char[] a = "abcd";
     char[] r;

     r = a.dup.reverse;
     assert(r == "dcba");

     a = "a\u1235\u1234c";
     r = a.dup.reverse;
     assert(r == "c\u1234\u1235a");

 When you compile it without optimization everything is ok, but when you use -O
for example,
 the following happens:

 function _adReverseChar (that's the function that is called when you use
.reverse)
 works for a very very very long time, that happens because the "char[] a"
parameter
 that is passed to that function is passed wrongly, if you call a.length then
you'll
 see that it'll return something like 134_XXX_XXX, i.e a very large number
though the
 real size of a string is 4.

What is the ouput of the below code if it is run inside _adReverseChar? # printf("LEN %zX\n", a.length); # printf("PTR %zX\n", a.ptr); # size_t* data = &a; # printf("pre %zX\n", *(data-1)); # printf("len %zX\n", *(data)); # printf("ptr %zX\n", *(data+1)); # printf("post %zX\n", *(data+2)); Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFGLJYZLK5blCcjpWoRAmH5AJ9Kr2t386L1mD1pkg1Y2RXuY0Y3iACdGYwt MyznI2I11SSGFAcjRBEic54= =rPGB -----END PGP SIGNATURE-----

The output is: LEN 4 PTR 8078FE0 pre 80478A8 len 4 ptr 8078FE0 post 805E980 LEN 805E991 PTR 8180000 pre 8047898 len 805E991 ptr 8180000 post 805E991 I've already understood what was the problem, the optimizer generated bad code, which was lefting garbage on the stack which then was passed to functions as arguments (in this example, the size of array "a"), I've manually fixed this in generated asm listing (added few pop instructions) and compiled from that listing, the program worked. So, where should I search for bugs ? In gcc backend or in gdc frontend ? Who's responsible for such "optimization" ?
Apr 25 2007