Optimizer malfunction

Discuss about beta versions of mikroPascal compiler.
Post Reply
Author
Message
janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Optimizer malfunction

#1 Post by janni » 31 Aug 2007 11:15

I've found a dangerous (and, in my case, expensive :cry: ) error. Optimizer sneakily falsifies logical expressions on bit variables (and increases code size).

Let's look at this simple code:

Code: Select all

var Svect1:byte;

begin
 Svect1.6:=1;
 Svect1.7:=1;
 if (Svect1.6=0) or ((Svect1.6=1) and (Svect1.7=1)) then
  begin
   Svect1:=0;   // never executed
  end;

end.
The conditional statement should evaluate true, but instruction clearing Svect1 is never executed because optimizer errs in assuming that one of internal registers (STACK_x) contains copy of Svect1.

Moreover, the problem is not restricted to if statement. This

Code: Select all

var Svect1:byte; Roboo:boolean;

begin
 Svect1.6:=1;
 Svect1.7:=1;
 Roboo:=(Svect1.6=1) and (Svect1.7=1);
 Roboo:=Roboo or (Svect1.6=0);
 if Roboo then
  begin
   Svect1:=0;
  end;
end.
produces the same effect (Roboo=false), only the optimizer errs in different place.

Solution to this problem is stopping the optimizer by introducing asm statement in between logical assignments:

Code: Select all

begin
 Svect1.6:=1;
 Svect1.7:=1;
 Roboo:=(Svect1.6=1) and (Svect1.7=1);
 asm nop end;   // stops optimizer operation
 Roboo:=Roboo or (Svect1.6=0);
 if Roboo then
  begin
   Svect1:=0;
end.
What's intresting - the last code is significantly shorter than the previous one.

jpc
Posts: 1986
Joined: 22 Apr 2005 17:40
Location: France 87

#2 Post by jpc » 31 Aug 2007 12:13

if you declare the variable volatile the same thing happens , it starts to work ok and the code is more compact and is a lot faster , the 'optimizer' might need some optimisations here.

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

#3 Post by janni » 31 Aug 2007 13:08

Yes, declaring a variable volatile certainly blocks the optimizer from assuming that it's stored value may be reused.

The severity of this bug lays in fact that it's hard to take measures against it apart from declaring all pertinent variables volatile (and one can't do it directly with record fields).

jpc
Posts: 1986
Joined: 22 Apr 2005 17:40
Location: France 87

#4 Post by jpc » 31 Aug 2007 13:24

besides the bug i think there is a problem with the optimizer here as non-optimized code is more compact and faster the'optimized' code

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

#5 Post by janni » 31 Aug 2007 15:16

Yeah, I've just made volatile a couple of hundred bit-oriented variables in one of my programs and the code shrinked by over 400 bytes :roll: .

User avatar
zristic
mikroElektronika team
Posts: 6608
Joined: 03 Aug 2004 12:59
Contact:

#6 Post by zristic » 03 Sep 2007 09:05

We will check/fix this.
Thanks for reporting.

Post Reply

Return to “mikroPascal Beta testing”