From 2408c9602c89f974a9b91dbd171b8cc133068c19 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Thu, 4 Feb 2021 12:44:44 -0600 Subject: [PATCH] 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. --- CCommon.pas | 2 +- Expression.pas | 17 +++++++++++++++-- Parser.pas | 6 +++--- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/CCommon.pas b/CCommon.pas index cad3a88..0c060da 100644 --- a/CCommon.pas +++ b/CCommon.pas @@ -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} diff --git a/Expression.pas b/Expression.pas index b914088..9a801d0 100644 --- a/Expression.pas +++ b/Expression.pas @@ -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} diff --git a/Parser.pas b/Parser.pas index 4c2e0c5..1addc76 100644 --- a/Parser.pas +++ b/Parser.pas @@ -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);