From 393fb8d6351805ddc01af259d0fdc929110b1835 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Sun, 31 Jan 2021 12:00:31 -0600 Subject: [PATCH] Make floating point to character type conversions yield values within the type's range. This affects cases where the floating value, truncated to an integer, is outside the range of the destination type. Previously, the result value might appear to be an int value outside the range of the character type. These situations are undefined behavior under the C standards, so this was not technically a bug, but the new behavior is less surprising. (Note that it still may not raise the "invalid" floating-point exception in some cases where Annex F would call for that.) --- Gen.pas | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Gen.pas b/Gen.pas index 2844285..88e1c68 100644 --- a/Gen.pas +++ b/Gen.pas @@ -1317,10 +1317,20 @@ else if op^.q in [longToReal,uLongToReal] then begin else GenCall(13); end {else} -else if op^.q in [realToByte,realToUbyte,realToWord] then begin +else if op^.q =realToWord then + GenCall(14) +else if op^.q = realToUbyte then begin GenCall(14); - if (op^.q & $00FF) in [0,1] then - GenNative(m_and_imm, immediate, $00FF, nil, 0); + GenNative(m_and_imm, immediate, $00FF, nil, 0); + end {else if} +else if op^.q = realToByte then begin + lab1 := GenLabel; + GenCall(14); + GenNative(m_and_imm, immediate, $00FF, nil, 0); + GenNative(m_bit_imm, immediate, $0080, nil, 0); + GenNative(m_beq, relative, lab1, nil, 0); + GenNative(m_ora_imm, immediate, $FF00, nil, 0); + GenLab(lab1); end {else if} else if op^.q = realToUword then GenCall(15)