Fix signed integer overflow issues

It seemed that the consensus in our discussions of signed integer
overflow, which invokes undefined behavior in C, was that integer
arithmetic should be two's complement and there should be no warning for
overflows. I have implemented that by converting values to unsigned types
when appropriate. These changes will mostly preserve existing behavior,
except for a few cases that were being handled incorrectly before.

The case of dividing INT_MIN by -1 previously resulted in a CPU
exception and program termination. Now, that case is detected and results
in a warning and a value of INT_MIN.

Similarly, INT_MIN % -1 would have resulted in a CPU exception. Since this
is a mathematically valid operation with a result of 0, it now simply
gives that result without a warning.

I noticed that in rpn.c, there were attempts in certain operation handlers
to validate the nVal members of the source expressions even when the
expressions may have been relocatable expressions with meaningless numbers
for the nVal member. This could have caused spurious errors/warnings, so I
made those handlers confirm that isReloc is false before validating nVal.

Also, integer constants that are too large now result in a warning. The
post-conversion values have not been changed, in order to preserve
backward compatibility.
This commit is contained in:
dbrotz
2019-06-28 16:36:23 -07:00
parent 54e5bf0f0c
commit ca6149abcf
6 changed files with 170 additions and 33 deletions

42
test/asm/overflow.asm Normal file
View File

@@ -0,0 +1,42 @@
SECTION "sec", ROM0
print_x: MACRO
printv x
printt "\n"
ENDM
x = 2147483647
x = x + 1
dl 2147483647+1
print_x
x = -2147483648
x = x - 1
dl -2147483648-1
print_x
x = -2147483648
x = x * -1
dl -2147483648 * -1
print_x
x = -2147483648
x = x / -1
dl -2147483648 / -1
print_x
x = -2147483648
x = x % -1
dl -2147483648 % -1
print_x
x = -1
x = x << 1
dl -1 << 1
print_x
x = 4294967295
x = 4294967296
x = `33333333
x = `333333333

18
test/asm/overflow.out Normal file
View File

@@ -0,0 +1,18 @@
warning: overflow.asm(24):
Division of min value by -1
warning: overflow.asm(25):
Division of min value by -1
warning: overflow.asm(34):
Left shift of negative value: -1
warning: overflow.asm(35):
Left shift of negative value: -1
warning: overflow.asm(39):
Integer constant '4294967296' is too large
warning: overflow.asm(42):
Graphics constant '`333333333' is too long
$80000000
$7FFFFFFF
$80000000
$80000000
$0
$FFFFFFFE

View File

@@ -0,0 +1,18 @@
warning: -(24):
Division of min value by -1
warning: -(25):
Division of min value by -1
warning: -(34):
Left shift of negative value: -1
warning: -(35):
Left shift of negative value: -1
warning: -(39):
Integer constant '4294967296' is too large
warning: -(42):
Graphics constant '`333333333' is too long
$80000000
$7FFFFFFF
$80000000
$80000000
$0
$FFFFFFFE