if a or (b and c) is true but if a or (c and b) is false??

General FreeBASIC programming questions.
EinOtten
Posts: 4
Joined: Nov 05, 2011 23:33

if a or (b and c) is true but if a or (c and b) is false??

Postby EinOtten » Nov 05, 2011 23:47

Hi guys,

I've looked through the known bugs but it could be that this kind of behaviour is intended for some reason or there is something else i am missing.

These lines give different behaviour in the executable:

Code: Select all

' version 1
if MultiKey(FB.SC_S) or ( gStats.turns=1 and DEBUG_EVO_SIM=1) then...
' version 2
if MultiKey(FB.SC_S) or ( DEBUG_EVO_SIM=1 and gStats.turns=1) then...


There are thousands lines of code, but i can assure you that everything else is working fine.
DEBUG_EVO_SIM is a const, gStats.turns is an integer. Version 2 works as intended but version 1 seems to be true every turn ( not only the first one) and even when DEBUG_EVO_SIM was set to '0'.

Compiled with newest and an older version of fbc.

[/code]
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Postby agamemnus » Nov 05, 2011 23:58

Perhaps you have memory corruption issues in other parts of your code that is actually affecting the IF statement logic.. Do you use pointers?
EinOtten
Posts: 4
Joined: Nov 05, 2011 23:33

Postby EinOtten » Nov 06, 2011 0:11

Thanks for the quick reply.

Yes, i'm using lots of pointers, but it sounds very improbable that a memory corruption would only affect this particular if statement (as said, everything else is working fine) - and only version 1. Though i must admit i've no experience with memory failures that cause that kind of behaviour and dont crash.

For now i'll just use the version that works, my main concern was that this was a mistake in my understanding of how freeBasic works and other projects could be affected.
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Postby Gonzo » Nov 06, 2011 0:14

to be brutally honest, its very likely..
theres an easy test to see if this is true though:
if you reorder your code, other things will break instead :)

try that
it could also be a fbc problem, but that would be rare.. especially for simple boolean tests so its highly improbable
Merick
Posts: 1038
Joined: May 28, 2007 1:52

Postby Merick » Nov 06, 2011 0:53

Might not be the cause, but AND is a bitwise operator, to get a logical instead of bitwise result try using ANDALSO instead.
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Postby Gonzo » Nov 06, 2011 1:06

Merick wrote:Might not be the cause, but AND is a bitwise operator, to get a logical instead of bitwise result try using ANDALSO instead.


that shouldnt be the case... and has highest priority
both expressions use the same syntax, and should "and well together" :P
EinOtten
Posts: 4
Joined: Nov 05, 2011 23:33

Postby EinOtten » Nov 06, 2011 2:31

I reordered the code to try and see if something else went wrong but that didnt happen. I switched off all features i could think off that could corrupt the memory - although there are modules that can't be switched off.
Still version 2 works, version 1 does not.

I have to go to bed now. This still is a mystery to me, since i haven't seen such strange behaviour ever. I can't keep investigating however, as long as the simulation runs stable i have to keep expanding it.

If i find out that there is a international conspiracy behind all this i'll let you know ;)
Sisophon2001
Posts: 1704
Joined: May 27, 2005 6:34
Location: Cambodia, Thailand, Lao, Ireland etc.
Contact:

Postby Sisophon2001 » Nov 06, 2011 8:29

I quickly tested your code by putting it in a small code block, and it gave buggy results, just like you explained. I need to look at this again to see if I am missing something stupid.

Garvan

test code:

Code: Select all

dim as integer gStats_turns, SC_S
const DEBUG_EVO_SIM=0
gStats_turns = 1
SC_S = 0


' version 1
if SC_S or ( gStats_turns=1 and DEBUG_EVO_SIM=1) then
   print "v1 true"
else
   print "v1 false"
end if
' version 2
if SC_S or ( DEBUG_EVO_SIM=1 and gStats_turns=1) then
   print "v2 true"
else
   print "v2 false"
end if
TJF
Posts: 3503
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Postby TJF » Nov 06, 2011 8:51

When I replace
    const DEBUG_EVO_SIM=0
by
    VAR DEBUG_EVO_SIM = 0
it works. It seems to be a bug.
fxm
Posts: 9529
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Postby fxm » Nov 06, 2011 9:18

- If we evaluate the 2 expressions (before each IF), we find the right results, but the first IF failed!

Code: Select all

dim as integer gStats_turns, SC_S
const DEBUG_EVO_SIM=0
gStats_turns = 1
SC_S = 0


' version 1
print SC_S or ( gStats_turns=1 and DEBUG_EVO_SIM=1)
if SC_S or ( gStats_turns=1 and DEBUG_EVO_SIM=1) then
        print "v1 true"
else
        print "v1 false"
end if
' version 2
print SC_S or ( DEBUG_EVO_SIM=1 and gStats_turns=1)
if SC_S or ( DEBUG_EVO_SIM=1 and gStats_turns=1) then
        print "v2 true"
else
        print "v2 false"
end if


- If we compile with option "-e" or "-ex" or "-exx", it works!
fxm
Posts: 9529
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Postby fxm » Nov 06, 2011 9:31

The ASM file is very surprising!

Code: Select all

   .file "d:\\Documents and Settings\\t0003830\\Mes documents\\FBIde0.4.6r3+\\FBIDETEMP.bas"
   .stabs "d:\\Documents and Settings\\t0003830\\Mes documents\\FBIde0.4.6r3+\\",100,0,0,.Lt_0002
   .stabs "d:\\Documents and Settings\\t0003830\\Mes documents\\FBIde0.4.6r3+\\FBIDETEMP.bas",100,0,0,.Lt_0002

.section .text
.Lt_0002:
   .stabs "integer:t1=-1",128,0,0,0
   .stabs "void:t7=-11",128,0,0,0
   .stabs "byte:t2=-6",128,0,0,0
   .stabs "ubyte:t3=-5",128,0,0,0
   .stabs "char:t4=-2",128,0,0,0
   .stabs "short:t5=-3",128,0,0,0
   .stabs "ushort:t6=-7",128,0,0,0
   .stabs "uinteger:t8=-8",128,0,0,0
   .stabs "longint:t9=-31",128,0,0,0
   .stabs "ulongint:t10=-32",128,0,0,0
   .stabs "single:t11=-12",128,0,0,0
   .stabs "double:t12=-13",128,0,0,0
   .stabs "string:t13=s12data:15,0,32;len:1,32,32;size:1,64,32;;",128,0,0,0
   .stabs "fixstr:t14=-2",128,0,0,0
   .stabs "pchar:t15=*4;",128,0,0,0

   .stabs "d:\\Documents and Settings\\t0003830\\Mes documents\\FBIde0.4.6r3+\\FBIDETEMP.bas",130,0,0,0
   .intel_syntax noprefix

   #d:\Documents and Settings\t0003830\Mes documents\FBIde0.4.6r3+\FBIDETEMP.bas' compilation started at 10:19:37 (FreeBASIC 0.23.0)
   .stabs "{MODLEVEL}:f7",36,0,1,_fb_ctor__FBIDETEMP
.balign 16
_fb_ctor__FBIDETEMP:
   push ebp
   mov ebp, esp
   sub esp, 8
   .stabn 68,0,1,_fb_ctor__FBIDETEMP-_fb_ctor__FBIDETEMP
.Lt_0003:
.Lt_0015:
##
##dim as integer gStats_turns, SC_S
   mov dword ptr [ebp-4], 0
   mov dword ptr [ebp-8], 0
.stabn 68,0,2,.Lt_0015-_fb_ctor__FBIDETEMP
.Lt_0016:
##const DEBUG_EVO_SIM=0
##gStats_turns = 1
   mov dword ptr [ebp-4], 1
.stabn 68,0,4,.Lt_0016-_fb_ctor__FBIDETEMP
.Lt_0017:
##SC_S = 0
   mov dword ptr [ebp-8], 0
.stabn 68,0,5,.Lt_0017-_fb_ctor__FBIDETEMP
.Lt_0018:
##
##
##' version 1
##if SC_S or ( gStats_turns=1 and DEBUG_EVO_SIM=1) then
   je .Lt_0006
.Lt_0007:
.stabn 68,0,9,.Lt_0018-_fb_ctor__FBIDETEMP
.Lt_0019:
##        print "v1 true"
   push 1
   push 7
   push offset _Lt_0008
   call _fb_StrAllocTempDescZEx@8
   push eax
   push 0
   call _fb_PrintString@12
.stabn 68,0,10,.Lt_0019-_fb_ctor__FBIDETEMP
.Lt_001A:
##else
.Lt_0009:
   jmp .Lt_0005
.Lt_0006:
.Lt_000A:
.stabn 68,0,11,.Lt_001A-_fb_ctor__FBIDETEMP
.Lt_001B:
##        print "v1 false"
   push 1
   push 8
   push offset _Lt_000B
   call _fb_StrAllocTempDescZEx@8
   push eax
   push 0
   call _fb_PrintString@12
.stabn 68,0,12,.Lt_001B-_fb_ctor__FBIDETEMP
.Lt_001C:
##end if
.Lt_000C:
.Lt_0005:
##' version 2
##if SC_S or ( DEBUG_EVO_SIM=1 and gStats_turns=1) then
   mov eax, dword ptr [ebp-4]
   cmp eax, 1
   sete al
   shr eax, 1
   sbb eax, eax
   and eax, 0
   or eax, dword ptr [ebp-8]
   je .Lt_000E
.Lt_000F:
.stabn 68,0,15,.Lt_001C-_fb_ctor__FBIDETEMP
.Lt_001E:
##        print "v2 true"
   push 1
   push 7
   push offset _Lt_0010
   call _fb_StrAllocTempDescZEx@8
   push eax
   push 0
   call _fb_PrintString@12
.stabn 68,0,16,.Lt_001E-_fb_ctor__FBIDETEMP
.Lt_001F:
##else
.Lt_0011:
   jmp .Lt_000D
.Lt_000E:
.Lt_0012:
.stabn 68,0,17,.Lt_001F-_fb_ctor__FBIDETEMP
.Lt_0020:
##        print "v2 false"
   push 1
   push 8
   push offset _Lt_0013
   call _fb_StrAllocTempDescZEx@8
   push eax
   push 0
   call _fb_PrintString@12
.stabn 68,0,18,.Lt_0020-_fb_ctor__FBIDETEMP
.Lt_0021:
##end if
.Lt_0014:
.Lt_000D:
##
.Lt_0004:
   mov esp, ebp
   pop ebp
   ret
   .stabn 68,0,20,.Lt_0004-_fb_ctor__FBIDETEMP
   .stabs "GSTATS_TURNS:1",128,0,0,-4
   .stabs "SC_S:1",128,0,0,-8
   .stabn 192,0,0,.Lt_0003-_fb_ctor__FBIDETEMP
   .stabn 192,0,0,.Lt_0007-_fb_ctor__FBIDETEMP
   .stabn 224,0,0,.Lt_0009-_fb_ctor__FBIDETEMP
   .stabn 192,0,0,.Lt_000A-_fb_ctor__FBIDETEMP
   .stabn 224,0,0,.Lt_000C-_fb_ctor__FBIDETEMP
   .stabn 192,0,0,.Lt_000F-_fb_ctor__FBIDETEMP
   .stabn 224,0,0,.Lt_0011-_fb_ctor__FBIDETEMP
   .stabn 192,0,0,.Lt_0012-_fb_ctor__FBIDETEMP
   .stabn 224,0,0,.Lt_0014-_fb_ctor__FBIDETEMP
   .stabn 224,0,0,.Lt_0004-_fb_ctor__FBIDETEMP
.Lt_0022:
   .stabs "",36,0,0,.Lt_0022-_fb_ctor__FBIDETEMP
   #d:\Documents and Settings\t0003830\Mes documents\FBIde0.4.6r3+\FBIDETEMP.bas' compilation took 0.0002450032053298301 secs

.section .data
.balign 4
_Lt_0008:   .ascii   "v1 true\0"
.balign 4
_Lt_000B:   .ascii   "v1 false\0"
.balign 4
_Lt_0010:   .ascii   "v2 true\0"
.balign 4
_Lt_0013:   .ascii   "v2 false\0"

.section .ctors
.int _fb_ctor__FBIDETEMP

.section .text
   .stabs "",100,0,0,.Lt_0023
.Lt_0023:
It seems that code is missing for the first IF (at least we must find the test on SC_S value in case of optimum compiler optimization):
##' version 1
##if SC_S or ( gStats_turns=1 and DEBUG_EVO_SIM=1) then
je .Lt_0006

With compile option "-e" or "-ex" or "-exx", the code is always missing but it works, by chance?
Last edited by fxm on Nov 06, 2011 14:20, edited 3 times in total.
fxm
Posts: 9529
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Postby fxm » Nov 06, 2011 10:07

TJF wrote:When I replace
    const DEBUG_EVO_SIM=0
by
    VAR DEBUG_EVO_SIM = 0
it works. It seems to be a bug.

Yes, and the ASM file is better!

Code: Select all

   .file "d:\\Documents and Settings\\t0003830\\Mes documents\\FBIde0.4.6r3+\\FBIDETEMP.bas"
   .stabs "d:\\Documents and Settings\\t0003830\\Mes documents\\FBIde0.4.6r3+\",100,0,0,.Lt_0002
   .stabs "d:\\Documents and Settings\\t0003830\\Mes documents\\FBIde0.4.6r3+\\FBIDETEMP.bas",100,0,0,.Lt_0002

.section .text
.Lt_0002:
   .stabs "integer:t1=-1",128,0,0,0
   .stabs "void:t7=-11",128,0,0,0
   .stabs "byte:t2=-6",128,0,0,0
   .stabs "ubyte:t3=-5",128,0,0,0
   .stabs "char:t4=-2",128,0,0,0
   .stabs "short:t5=-3",128,0,0,0
   .stabs "ushort:t6=-7",128,0,0,0
   .stabs "uinteger:t8=-8",128,0,0,0
   .stabs "longint:t9=-31",128,0,0,0
   .stabs "ulongint:t10=-32",128,0,0,0
   .stabs "single:t11=-12",128,0,0,0
   .stabs "double:t12=-13",128,0,0,0
   .stabs "string:t13=s12data:15,0,32;len:1,32,32;size:1,64,32;;",128,0,0,0
   .stabs "fixstr:t14=-2",128,0,0,0
   .stabs "pchar:t15=*4;",128,0,0,0

   .stabs "d:\\Documents and Settings\\t0003830\\Mes documents\\FBIde0.4.6r3+\\FBIDETEMP.bas",130,0,0,0
   .intel_syntax noprefix

   #d:\Documents and Settings\t0003830\Mes documents\FBIde0.4.6r3+\FBIDETEMP.bas' compilation started at 11:03:57 (FreeBASIC 0.23.0)
   .stabs "{MODLEVEL}:f7",36,0,1,_fb_ctor__FBIDETEMP
.balign 16
_fb_ctor__FBIDETEMP:
   push ebp
   mov ebp, esp
   sub esp, 12
   push ebx
   .stabn 68,0,1,_fb_ctor__FBIDETEMP-_fb_ctor__FBIDETEMP
.Lt_0003:
.Lt_0015:
##
##dim as integer gStats_turns, SC_S
   mov dword ptr [ebp-4], 0
   mov dword ptr [ebp-8], 0
.stabn 68,0,2,.Lt_0015-_fb_ctor__FBIDETEMP
.Lt_0016:
##VAR DEBUG_EVO_SIM = 0
   mov dword ptr [ebp-12], 0
.stabn 68,0,3,.Lt_0016-_fb_ctor__FBIDETEMP
.Lt_0017:
##gStats_turns = 1
   mov dword ptr [ebp-4], 1
.stabn 68,0,4,.Lt_0017-_fb_ctor__FBIDETEMP
.Lt_0018:
##SC_S = 0
   mov dword ptr [ebp-8], 0
.stabn 68,0,5,.Lt_0018-_fb_ctor__FBIDETEMP
.Lt_0019:
##
##
##' version 1
##if SC_S or ( gStats_turns=1 and DEBUG_EVO_SIM=1) then
   mov eax, dword ptr [ebp-4]
   cmp eax, 1
   sete al
   shr eax, 1
   sbb eax, eax
   mov ebx, dword ptr [ebp-12]
   cmp ebx, 1
   sete bl
   shr ebx, 1
   sbb ebx, ebx
   and eax, ebx
   or eax, dword ptr [ebp-8]
   je .Lt_0006
.Lt_0007:
.stabn 68,0,9,.Lt_0019-_fb_ctor__FBIDETEMP
.Lt_001C:
##        print "v1 true"
   push 1
   push 7
   push offset _Lt_0008
   call _fb_StrAllocTempDescZEx@8
   push eax
   push 0
   call _fb_PrintString@12
.stabn 68,0,10,.Lt_001C-_fb_ctor__FBIDETEMP
.Lt_001D:
##else
.Lt_0009:
   jmp .Lt_0005
.Lt_0006:
.Lt_000A:
.stabn 68,0,11,.Lt_001D-_fb_ctor__FBIDETEMP
.Lt_001E:
##        print "v1 false"
   push 1
   push 8
   push offset _Lt_000B
   call _fb_StrAllocTempDescZEx@8
   push eax
   push 0
   call _fb_PrintString@12
.stabn 68,0,12,.Lt_001E-_fb_ctor__FBIDETEMP
.Lt_001F:
##end if
.Lt_000C:
.Lt_0005:
##' version 2
##if SC_S or ( DEBUG_EVO_SIM=1 and gStats_turns=1) then
   mov eax, dword ptr [ebp-12]
   cmp eax, 1
   sete al
   shr eax, 1
   sbb eax, eax
   mov ebx, dword ptr [ebp-4]
   cmp ebx, 1
   sete bl
   shr ebx, 1
   sbb ebx, ebx
   and eax, ebx
   or eax, dword ptr [ebp-8]
   je .Lt_000E
.Lt_000F:
.stabn 68,0,15,.Lt_001F-_fb_ctor__FBIDETEMP
.Lt_0022:
##        print "v2 true"
   push 1
   push 7
   push offset _Lt_0010
   call _fb_StrAllocTempDescZEx@8
   push eax
   push 0
   call _fb_PrintString@12
.stabn 68,0,16,.Lt_0022-_fb_ctor__FBIDETEMP
.Lt_0023:
##else
.Lt_0011:
   jmp .Lt_000D
.Lt_000E:
.Lt_0012:
.stabn 68,0,17,.Lt_0023-_fb_ctor__FBIDETEMP
.Lt_0024:
##        print "v2 false"
   push 1
   push 8
   push offset _Lt_0013
   call _fb_StrAllocTempDescZEx@8
   push eax
   push 0
   call _fb_PrintString@12
.stabn 68,0,18,.Lt_0024-_fb_ctor__FBIDETEMP
.Lt_0025:
##end if
.Lt_0014:
.Lt_000D:
##
.Lt_0004:
   pop ebx
   mov esp, ebp
   pop ebp
   ret
   .stabn 68,0,20,.Lt_0004-_fb_ctor__FBIDETEMP
   .stabs "GSTATS_TURNS:1",128,0,0,-4
   .stabs "SC_S:1",128,0,0,-8
   .stabs "DEBUG_EVO_SIM:1",128,0,0,-12
   .stabn 192,0,0,.Lt_0003-_fb_ctor__FBIDETEMP
   .stabn 192,0,0,.Lt_0007-_fb_ctor__FBIDETEMP
   .stabn 224,0,0,.Lt_0009-_fb_ctor__FBIDETEMP
   .stabn 192,0,0,.Lt_000A-_fb_ctor__FBIDETEMP
   .stabn 224,0,0,.Lt_000C-_fb_ctor__FBIDETEMP
   .stabn 192,0,0,.Lt_000F-_fb_ctor__FBIDETEMP
   .stabn 224,0,0,.Lt_0011-_fb_ctor__FBIDETEMP
   .stabn 192,0,0,.Lt_0012-_fb_ctor__FBIDETEMP
   .stabn 224,0,0,.Lt_0014-_fb_ctor__FBIDETEMP
   .stabn 224,0,0,.Lt_0004-_fb_ctor__FBIDETEMP
.Lt_0026:
   .stabs "",36,0,0,.Lt_0026-_fb_ctor__FBIDETEMP
   #d:\Documents and Settings\t0003830\Mes documents\FBIde0.4.6r3+\FBIDETEMP.bas' compilation took 0.0002503111433886929 secs

.section .data
.balign 4
_Lt_0008:   .ascii   "v1 true\0"
.balign 4
_Lt_000B:   .ascii   "v1 false\0"
.balign 4
_Lt_0010:   .ascii   "v2 true\0"
.balign 4
_Lt_0013:   .ascii   "v2 false\0"

.section .ctors
.int _fb_ctor__FBIDETEMP

.section .text
   .stabs "",100,0,0,.Lt_0027
.Lt_0027:
##' version 1
##if SC_S or ( gStats_turns=1 and DEBUG_EVO_SIM=1) then
mov eax, dword ptr [ebp-4]
cmp eax, 1
sete al
shr eax, 1
sbb eax, eax
mov ebx, dword ptr [ebp-12]
cmp ebx, 1
sete bl
shr ebx, 1
sbb ebx, ebx
and eax, ebx
or eax, dword ptr [ebp-8]
je .Lt_0006
EinOtten
Posts: 4
Joined: Nov 05, 2011 23:33

Postby EinOtten » Nov 06, 2011 13:38

Do you guys post/report this problem? You lost me when you started to look at it in assembler ;)
Sisophon2001
Posts: 1704
Joined: May 27, 2005 6:34
Location: Cambodia, Thailand, Lao, Ireland etc.
Contact:

Postby Sisophon2001 » Nov 06, 2011 13:49

EinOtten wrote:Do you guys post/report this problem? You lost me when you started to look at it in assembler ;)


I would wait 24 hours or so to see if anybody figures out what is going wrong. Then there might be a better bug report.

Garvan
fxm
Posts: 9529
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Postby fxm » Nov 06, 2011 14:18

Sisophon2001 wrote:
EinOtten wrote:Do you guys post/report this problem? You lost me when you started to look at it in assembler ;)


I would wait 24 hours or so to see if anybody figures out what is going wrong. Then there might be a better bug report.

Garvan

Yes, I would think more of a compiler optimization bug in presence of constant, and in that precise case of order of expression to be tested, but it is better to wait a little the opinion of enlightened people as Site Admin (dkl for example) before reporting an official compiler bug to sourceforge.

Return to “General”

Who is online

Users browsing this forum: No registered users and 2 guests