Files
mercury/tests/hard_coded/opt_format_sign_extend.exp
Julien Fischer bbff6f8609 Fix a bug in string.format.
The current implementation of the unsigned conversion specifiers (i.e. %x, %X,
%o, %u) for fixed-size 8-, 16- and 32-bit signed integers works by casting
theses values into (word-sized) ints and then using the existing code for
formatting ints as unsigned values. This does not work for negative values as
they are being sign extended when converted to ints. For example,

      io.format("%x", [i8(min_int8)], !IO)

prints:

      ffffffffffffff80 (on a 64-bit machine)

rather than just:

      80

Fix the above problem by casting ints, int8s, int16s etc. that are being
formatted as unsigned values to uints and using the uint formatting code.

NOTE: the formatting code for 64-bit integers follows a different code path
and is not affected by any of this.

library/string.format.m:
    Implement unsigned conversion specifiers for non-64-bit signed
    integers by casting to uint and using the uint formatting code.

    Add predicates for converting signed integers into uints.

    The format_unsigned_int_* predicates can be deleted after this change
    is installed.

compiler/format_call.m:
    Implement unsigned conversion specifiers for non-64-bit signed
    integers by casting to uint and using the uint formatting code.

compiler/introduced_call_table.m:
    Update the table of introduced predicates.

compiler/options.m:
    Add an option that can be used to test whether this fix is
    installed.

tests/hard_coded/Mmakefile:
tests/hard_coded/Mercury.options:
tests/hard_coded/opt_format_sign_extend.{m,exp}:
    Test that formatting code introduced by the compiler does not
    accidentally sign-extend negative values.

tests/string_format/string_format_{o,x,u}.{m,exp,exp2}:
    Make these tests much more comprehensive then they previously
    were.
2023-01-27 17:16:54 +11:00

28 lines
159 B
Plaintext

80
8000
80000000
ff
ffff
ffffffff
80
8000
80000000
FF
FFFF
FFFFFFFF
200
100000
20000000000
377
177777
37777777777
128
32768
2147483648
255
65535
4294967295