Make expressionValue a saturating approximation of the true value for long long expressions.

This gives sensible behavior for several things in the parser, e.g. where all negative values or all very large values should be disallowed.
This commit is contained in:
Stephen Heumann 2021-02-04 12:44:44 -06:00
parent 10cf6e446d
commit 2408c9602c
3 changed files with 19 additions and 6 deletions

View File

@ -491,7 +491,7 @@ var
{------------------}
doDispose: boolean; {dispose of the expression tree as we go?}
realExpressionValue: double; {value of the last real constant expression}
longlongExpressionValue: longlong; {value of the last long long constant expression}
llExpressionValue: longlong; {value of the last long long constant expression}
expressionValue: longint; {value of the last constant expression}
expressionType: typePtr; {the type of the expression}
initializerTree: tokenPtr; {for non-constant initializers}

View File

@ -3899,6 +3899,8 @@ if kind = normalExpression then begin {generate code from the expression tree}
else begin {record the expression for an initializer}
initializerTree := tree;
isConstant := false;
llExpressionValue.lo := 0;
llExpressionValue.hi := 0;
if errorFound then begin
DisposeTree(initializerTree);
initializerTree := nil;
@ -3955,12 +3957,23 @@ else begin {record the expression for an initialize
isConstant := true;
end {else if}
else if tree^.token.kind = longlongconst then begin
longlongExpressionValue := tree^.token.qval;
llExpressionValue := tree^.token.qval;
if ((llExpressionValue.hi = 0) and (llExpressionValue.lo >= 0))
or ((llExpressionValue.hi = -1) and (llExpressionValue.lo < 0)) then
expressionValue := llExpressionValue.lo
else if llExpressionValue.hi < 0 then
expressionValue := $80000000
else
expressionValue := $7fffffff;
expressionType := longLongPtr;
isConstant := true;
end {else if}
else if tree^.token.kind = ulonglongconst then begin
longlongExpressionValue := tree^.token.qval;
llExpressionValue := tree^.token.qval;
if llExpressionValue.hi = 0 then
expressionValue := llExpressionValue.lo
else
expressionValue := $FFFFFFFF;
expressionType := ulongLongPtr;
isConstant := true;
end {else if}

View File

@ -1947,7 +1947,7 @@ var
if isConstant and (variable^.storage in [external,global,private]) then begin
if bitsize = 0 then begin
if etype^.baseType in [cgQuad,cgUQuad] then begin
iPtr^.qVal := longlongExpressionValue;
iPtr^.qVal := llExpressionValue;
end {if}
else begin
iPtr^.qval.hi := 0;
@ -2031,8 +2031,8 @@ var
errorFound := true;
end {else}
else if etype^.baseType in [cgQuad,cgUQuad] then
if (longlongExpressionValue.hi = 0) and
(longlongExpressionValue.lo = 0) then
if (llExpressionValue.hi = 0) and
(llExpressionValue.lo = 0) then
iPtr^.iType := cgULong
else begin
Error(47);