Error in the code generator for conditional allocation

Started by AlexN, November 06, 2008, 09:33:47 PM

Previous topic - Next topic

AlexN

Hi Pelle

I think, I have found an error in the code generator for condition allocation. I tried to port WinVI (http://www.winvi.de/en/) to Pelles C. In the file "Paint.c" in the function "TextLine" in the source line 1187 there is a conditional allocation. In the false path of this allocation the compiler forgets to calculate the address where to write the data.

I have tried to reduce to code which produces this fault, but unfortunately I was not successful. So I try to add the complete project. So that you can search for the misstake.

best regards
Alex  ;)
best regards
Alex ;)

AlexN

best regards
Alex ;)

severach

Pelles has a project manager bug where it puts the real name of the include folder in WinVI.ppj for the .rc file. It should be using $(PellesCDir)\Include\Win. I worked around this by removing and adding the .rc file.

Quote
PORC: ...\WINVI\Winvi.rc(16): fatal error: Can't find include file WinVi32.rc.

Code (paint.c) Select
p = Line ? LineInfo[Line].Pos : ScreenStart;
Line 1187 is not an allocation but a structure copy. Because POSITION is 3 elements Pelles chooses to do it by copying each of the 3 elements rather than a generic memcpy(). I can't get the compile to complete so I made a conditional structure copy with the POSITION structure and another larger structure with enough elements that Pelles chooses a memcpy() variant. Both assembly listings look like they should work. I didn't test because I do not allow implicit structure copies anywhere in my code.

Avoid this project as a code learning exercise.

How To Write Unmaintainable Code

AlexN

Quote from: severach on November 09, 2008, 07:45:00 AM

Quote
PORC: ...\WINVI\Winvi.rc(16): fatal error: Can't find include file WinVi32.rc.

You can find the complete (I hope so) project at http://members.vienna.at/neumeister/software/WinVI.zip or http://members.aon.at/aneumeister/Programme/WinVI.zip.

Quote
Code (paint.c) Select
p = Line ? LineInfo[Line].Pos : ScreenStart;
Line 1187 is not an allocation but a structure copy. Because POSITION is 3 elements Pelles chooses to do it by copying each of the 3 elements rather than a generic memcpy(). I can't get the compile to complete so I made a conditional structure copy with the POSITION structure and another larger structure with enough elements that Pelles chooses a memcpy() variant. Both assembly listings look like they should work. I didn't test because I do not allow implicit structure copies anywhere in my code.

Avoid this project as a code learning exercise.

How To Write Unmaintainable Code


The main probleme is (if you use any optimizer) that in the "false" path the destination address is never read, so it is impossible to write to the variable (structure) p.

To avoid the probleme, there is the easiest way to deactivate optmization for this code-line, or you can eliminate the static from the declaration of p ...

I have tried to reproduce this fault in a short program and there I was not sucessful. To find a workaround, was not very hard after knew what the problem is.

I hope Pelle can find what the problem is and can fix it (when not I have also no problem ;) )
best regards
Alex ;)

AlexN

This is not a good program for Pelles C, version 5 crashes - version 6 don't crash but calculates the window size wrong. If I have enough time, I will look why.
Are the variables are initialized in an other way now?
best regards
Alex ;)

Pelle

The SSA form/optimizations can change things, so maybe. Initializing with a constant (integer, null pointer, ...) can move the initialization closer to the point where it's first needed.

Does it work without optimizations turned on?

I will look at it, but right now it's not obvious to me where the problem is (and where I should start looking)...
/Pelle

Pelle

/Pelle

JohnF

In the second message Alex posted the rc file.

John

Pelle

I can only find winvi.rc there - WinVi32.rc is included from winvi.rc...

Anyway, unless I have the exact same source code, and a pointer to where the compiler is assumed to do something wrong, I can't do much. Guessing which package to download from where isn't going to help me. Is the OP still using the code posted here, or some other modified version...?
/Pelle

AlexN

Quote from: Pelle on May 12, 2009, 05:37:33 PM
I can only find winvi.rc there - WinVi32.rc is included from winvi.rc...

Sorry!
I hope, I have added now all you need to build it.

best regards
Alex ;)

JohnF

I eventually got it to compile - if anyone wants to have it, it's on my web site as it's too large to post here complete.

http://www.johnfindlay.plus.com/pellesc/WinVI.zip

John

Pelle

OK, I can finally build and run the program. I can't see any problem in v6.0 for the originally reported problem. I can see some painting problems, but are we sure this code actually works?! Until I see some proof there is a compiler bug, I will assume the compiler is working correctly.
/Pelle

AlexN

Quote from: Pelle on May 13, 2009, 01:53:19 PM
OK, I can finally build and run the program. I can't see any problem in v6.0 for the originally reported problem. I can see some painting problems, but are we sure this code actually works?! Until I see some proof there is a compiler bug, I will assume the compiler is working correctly.

OK, I think that I found the problem.  :)

In the file WinVI.c in line 1149 there is a local variable SaveClientRect (RECT SaveClientRect) defined which gets the same adress like the local variable Rect (RECT Rect) defined in line 1143. Two variables at the same memory is not so good.  :(

If I force to get another adress for SaveClientRect with the instruction static, all works fine.  :)

PS: For the compiler v6.0 the topic is not correct. I would say, there is a problem with local variables.
best regards
Alex ;)

AlexN

Quote from: JohnF on May 13, 2009, 10:52:54 AM
I eventually got it to compile - if anyone wants to have it, it's on my web site as it's too large to post here complete.

http://www.johnfindlay.plus.com/pellesc/WinVI.zip

John


The original website for this program (without adaptation for Pelles C) you can find at:

http://www.winvi.de/
best regards
Alex ;)

JohnF

Quote from: Pelle on May 13, 2009, 01:53:19 PM
OK, I can finally build and run the program. I can't see any problem in v6.0 for the originally reported problem. I can see some painting problems, but are we sure this code actually works?! Until I see some proof there is a compiler bug, I will assume the compiler is working correctly.

I can confirm the code works if one makes one of the RECT structs static - here

case WM_ERASEBKGND:
    if (hwndMain)
    {
        static RECT Rect;

John