[Bug inline-asm/40621] New: GCC doesn't take into account ECX being modified inside inline assembler

2009-07-02 Thread hunterggl at gmail dot com
GCC doesn't produce the correct assembler code when I use optimizations.

[code]
#include 

#define mulscale(a,d,c) \
({ int __a=(a), __d=(d), __c=(c); \
   __asm__ __volatile__ ("imull %%edx; shrdl %%cl, %%edx, %%eax" \
: "=a" (__a), "=d" (__d) \
: "a" (__a), "d" (__d), "c" (__c) : "cc"); \
 __a; })

#define divscale(a,b,c) \
({ int __a=(a), __b=(b), __c=(c); \
   __asm__ __volatile__ ("movl %%eax, %%edx; shll %%cl, %%eax; negb %%cl;
sarl %%cl, %%edx; idivl %%ebx" \
: "=a" (__a) : "a" (__a), "c" (__c), "b" (__b) : "edx", "cc"); \
 __a; })

void badcode(int scale,int i,int vx,int vy,int vz)
{
int x1,y1;
i = divscale(i,vz,scale);
printf(" ");
x1 = mulscale(vx,i,scale);
y1 = mulscale(vy,i,scale);
printf("%d %d\n",x1,y1);
}

int main()
{
badcode(29,1856,-2048,-59,768);
return 0;
}
[/code]

Scenario 1:
Compile options "-O0"
Program output "-4950 -143"
CORRECT

Scenario 2:
Compile options "-O1"
Uncomment the printf(" ")
Program output "-4950 -143"
CORRECT but it's an ugly workaround

Scenario 3:
Compile options "-O1"
Comment out the printf(" ")
Program output "-1431655680 -978670931"
INCORRECT

The
screenshot(http://www.mediafire.com/?sharekey=bf284efa985bb0620dec85adfe0a530ae04e75f6e8ebb871)
shows comparison between Scenario 2 and Scenario 3.
As you can see, GCC stores the "scale" variable in ECX which is modified later
and GCC fails to restore it.
In the meantime I fixed my code by saving ECX in the stack.
I understand it might be difficult to detect all the cases when registers are
modified(especially when it happens implicitly) but
if GCC actually tries to do so and fails, this ticket can be considered as a
bugreport rather a feature request.

By the way, I'm quite impressed that GCC is smart enough to replace printf(" ")
with putch(' ').


-- 
   Summary: GCC doesn't take into account ECX being modified inside
            inline assembler
   Product: gcc
   Version: 4.4.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: inline-asm
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: hunterggl at gmail dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40621



[Bug inline-asm/40621] GCC doesn't take into account ECX being modified inside inline assembler

2009-07-02 Thread hunterggl at gmail dot com


--- Comment #1 from hunterggl at gmail dot com  2009-07-02 16:38 ---
Created an attachment (id=18122)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=18122&action=view)
Test file

This a small fragment of code that causes problems


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40621



[Bug inline-asm/40621] GCC doesn't take into account ECX being modified inside inline assembler

2009-07-02 Thread hunterggl at gmail dot com


--- Comment #2 from hunterggl at gmail dot com  2009-07-02 16:39 ---
Created an attachment (id=18123)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=18123&action=view)
Comparison of asm


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40621



[Bug inline-asm/40621] GCC doesn't take into account ECX being modified inside inline assembler

2009-07-02 Thread hunterggl at gmail dot com


--- Comment #4 from hunterggl at gmail dot com  2009-07-02 17:46 ---
(In reply to comment #3)
> For both mulscale and divscale you have ecx as an input register but nothing
> marks it as being modified so GCC does not know that.  It does not read the
> template string to figure out what you had meant to say.
> 

I see.

Anyway, how do I let GCC know about it?
What's the best solution?
Saving ECX isn't a very good solution because it isn't always necessary. And
what about other registers(EAX,EDX)? Do I have to save them too?

There are many such assembler definitions so I'd like to make code behavior
predictable.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40621