* 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 ttypeconvnode(left).convtype:=tc_bool_2_bool;
the typeconvnode after insertion, firstpass(left);
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;
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 ttypeconvnode(right).convtype:=tc_bool_2_bool;
the typeconvnode after insertion, firstpass(right);
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;
if not assigned(resultdef) then if not assigned(resultdef) then
resultdef:=rd; resultdef:=rd;
end; end;
@ -2049,43 +2036,35 @@ implementation
unequaln, unequaln,
equaln: equaln:
begin begin
if not(cs_full_boolean_eval in current_settings.localswitches) or { Remove any compares with constants }
(nf_short_bool in flags) then if (left.nodetype=ordconstn) then
begin begin
{ Remove any compares with constants } hp:=right;
if (left.nodetype=ordconstn) then b:=(tordconstnode(left).value<>0);
begin ot:=nodetype;
hp:=right; right:=nil;
b:=(tordconstnode(left).value<>0); if (not(b) and (ot=equaln)) or
ot:=nodetype; (b and (ot=unequaln)) then
left.free; begin
left:=nil; hp:=cnotnode.create(hp);
right:=nil; end;
if (not(b) and (ot=equaln)) or result:=hp;
(b and (ot=unequaln)) then exit;
begin end;
hp:=cnotnode.create(hp); if (right.nodetype=ordconstn) then
end; begin
result:=hp; hp:=left;
exit; b:=(tordconstnode(right).value<>0);
end; ot:=nodetype;
if (right.nodetype=ordconstn) then left:=nil;
begin if (not(b) and (ot=equaln)) or
hp:=left; (b and (ot=unequaln)) then
b:=(tordconstnode(right).value<>0); begin
ot:=nodetype; hp:=cnotnode.create(hp);
right.free; end;
right:=nil; result:=hp;
left:=nil; exit;
if (not(b) and (ot=equaln)) or end;
(b and (ot=unequaln)) then
begin
hp:=cnotnode.create(hp);
end;
result:=hp;
exit;
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