* replace explicit (sometimes incomplete) tests with calls to doshortbooleval()

* the c-style boolean evaluation of "and" is independent of short-circuit evaluation
    (you always have to consider the complete values in case the first one is <> 0)

git-svn-id: trunk@45990 -
This commit is contained in:
Jonas Maebe 2020-08-02 14:39:50 +00:00
parent 198efe2075
commit 346adf7f55

View File

@ -1435,7 +1435,7 @@ implementation
end; end;
end end
{ short to full boolean evalution possible and useful? } { short to full boolean evalution possible and useful? }
else if not(might_have_sideeffects(right,[mhs_exceptions])) and not(cs_full_boolean_eval in localswitches) then else if not(might_have_sideeffects(right,[mhs_exceptions])) and doshortbooleval(self) then
begin begin
case nodetype of case nodetype of
andn,orn: andn,orn:
@ -1447,6 +1447,7 @@ implementation
begin begin
{ we need to copy the whole tree to force another pass_1 } { we need to copy the whole tree to force another pass_1 }
include(localswitches,cs_full_boolean_eval); include(localswitches,cs_full_boolean_eval);
exclude(flags,nf_short_bool);
result:=getcopy; result:=getcopy;
exit; exit;
end; end;
@ -1975,41 +1976,27 @@ implementation
andn, andn,
orn: orn:
begin begin
{ in case of xor, or 'and' with full and cbool: convert both to Pascal bool and then { in case of xor or 'and' with cbool: convert both to Pascal bool and then
perform the xor/and to prevent issues with "longbool(1) and/xor perform the xor/and to prevent issues with "longbool(1) and/xor
longbool(2)" } longbool(2)" }
if (is_cbool(ld) or is_cbool(rd)) and if (is_cbool(ld) or is_cbool(rd)) and
((nodetype=xorn) or (nodetype in [xorn,andn]) then
((nodetype=andn) and
((cs_full_boolean_eval in current_settings.localswitches) or
not(nf_short_bool in flags)
)
)
) then
begin begin
resultdef:=nil; resultdef:=nil;
if is_cbool(ld) then if is_cbool(ld) then
begin begin
inserttypeconv(left,pasbool8type); left:=ctypeconvnode.create(left,pasbool8type);
{ inserttypeconv might already simplify
the typeconvnode after insertion,
thus we need to check if it still
really is a typeconv node }
if left is ttypeconvnode then
ttypeconvnode(left).convtype:=tc_bool_2_bool; ttypeconvnode(left).convtype:=tc_bool_2_bool;
firstpass(left);
if not is_cbool(rd) or if not is_cbool(rd) or
(ld.size>=rd.size) then (ld.size>=rd.size) then
resultdef:=ld; resultdef:=ld;
end; end;
if is_cbool(rd) then if is_cbool(rd) then
begin begin
inserttypeconv(right,pasbool8type); right:=ctypeconvnode.Create(right,pasbool8type);
{ inserttypeconv might already simplify
the typeconvnode after insertion,
thus we need to check if it still
really is a typeconv node }
if right is ttypeconvnode then
ttypeconvnode(right).convtype:=tc_bool_2_bool; ttypeconvnode(right).convtype:=tc_bool_2_bool;
firstpass(right);
if not assigned(resultdef) then if not assigned(resultdef) then
resultdef:=rd; resultdef:=rd;
end; end;
@ -2048,9 +2035,6 @@ implementation
end; end;
unequaln, unequaln,
equaln: equaln:
begin
if not(cs_full_boolean_eval in current_settings.localswitches) or
(nf_short_bool in flags) then
begin begin
{ Remove any compares with constants } { Remove any compares with constants }
if (left.nodetype=ordconstn) then if (left.nodetype=ordconstn) then
@ -2058,8 +2042,6 @@ implementation
hp:=right; hp:=right;
b:=(tordconstnode(left).value<>0); b:=(tordconstnode(left).value<>0);
ot:=nodetype; ot:=nodetype;
left.free;
left:=nil;
right:=nil; right:=nil;
if (not(b) and (ot=equaln)) or if (not(b) and (ot=equaln)) or
(b and (ot=unequaln)) then (b and (ot=unequaln)) then
@ -2074,8 +2056,6 @@ implementation
hp:=left; hp:=left;
b:=(tordconstnode(right).value<>0); b:=(tordconstnode(right).value<>0);
ot:=nodetype; ot:=nodetype;
right.free;
right:=nil;
left:=nil; left:=nil;
if (not(b) and (ot=equaln)) or if (not(b) and (ot=equaln)) or
(b and (ot=unequaln)) then (b and (ot=unequaln)) then
@ -2085,7 +2065,6 @@ implementation
result:=hp; result:=hp;
exit; exit;
end; end;
end;
{ Delphi-compatibility: convert both to pasbool to { Delphi-compatibility: convert both to pasbool to
perform the equality comparison } perform the equality comparison }
inserttypeconv(left,pasbool1type); inserttypeconv(left,pasbool1type);
@ -4105,9 +4084,7 @@ implementation
{ 2 booleans ? } { 2 booleans ? }
if is_boolean(ld) and is_boolean(rd) then if is_boolean(ld) and is_boolean(rd) then
begin begin
if (not(cs_full_boolean_eval in current_settings.localswitches) or if doshortbooleval(self) then
(nf_short_bool in flags)) and
(nodetype in [andn,orn]) then
expectloc:=LOC_JUMP expectloc:=LOC_JUMP
else else
begin begin