Commit Graph

5 Commits

Author SHA1 Message Date
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
Zoltan Somogyi
d4861d739d Allow formatting of sized integers.
library/string.m:
    Add {i,u}{8.16,32,64} as function symbols in the poly_type type,
    each with a single argument containing an integer with the named
    signedness and size.

    The idea is that each of these poly_type values works exactly
    the same way as the i(_) poly_type (if signed) or the u(_) poly_type
    (if unsigned), with the exception that the value specified by the call
    is cast to int or uint before being processed.

library/string.parse_runtime.m:
    Parse the new kinds of poly_types. Change the representation of the result
    of the parsing to allow recording of the sizes of ints and uints.

    Put the code that does the parsing into a predicate of its own.

library/string.format.m:
    Do a cast to int or uint if the size information recorded in the
    specification of a signed or unsigned integer value calls for it.

    Provide functions to do the casting that do not require the import
    of {int,uint}{8,16,32,64}.m. This is to allow the compiler to generate
    calls to do such casts without having to implicitly import those modules.

    Abort if a 64 bit number is being cast to a 32 bit word.

compiler/parse_string_format.m:
    Make the same changes as in string.parse_runtime.m, mutatis mutandis.

compiler/format_call.m:
    Handle the new kinds of poly_types by adding a cast to int or uint
    if necessary, using the predicates added to library/string.format.m.

    Use a convenience function to make code creating instmap deltas
    more readable.

library/io.m:
library/pprint.m:
library/string.parse_util.m:
tests/invalid/string_format_bad.m:
tests/invalid/string_format_unknown.m:
    Conform to the changes above.

tests/string_format/string_format_d.m:
tests/string_format/string_format_u.m:
    Test the printing of some of the new poly_types.

tests/string_format/string_format_d.exp2:
tests/string_format/string_format_u.exp2:
    Update the expected output of these tests on 64-bit platforms.

tests/string_format/string_format_lib.m:
    Update programming style.
2020-11-10 11:00:47 +11:00
Julien Fischer
b09f407a1f Update programming style in string format tests.
Document reasons for multiple expected outputs.

tests/string_format/*.m:
     As above.
2020-05-23 15:03:42 +10:00
Zoltan Somogyi
fdd141bf77 Clean up the tests in the other test directories.
tests/invalid/*.{m,err_exp}:
tests/misc_tests/*.m:
tests/mmc_make/*.m:
tests/par_conj/*.m:
tests/purity/*.m:
tests/stm/*.m:
tests/string_format/*.m:
tests/structure_reuse/*.m:
tests/submodules/*.m:
tests/tabling/*.m:
tests/term/*.m:
tests/trailing/*.m:
tests/typeclasses/*.m:
tests/valid/*.m:
tests/warnings/*.{m,exp}:
    Make these tests use four-space indentation, and ensure that
    each module is imported on its own line. (I intend to use the latter
    to figure out which subdirectories' tests can be executed in parallel.)

    These changes usually move code to different lines. For the tests
    that check compiler error messages, expect the new line numbers.

browser/cterm.m:
browser/tree234_cc.m:
    Import only one module per line.

tests/hard_coded/boyer.m:
    Fix something I missed.
2015-02-16 12:32:18 +11:00
Zoltan Somogyi
d9a1050af6 Flatten the test directories, step 1.
Specifically, rename the following subdirectories of the test directory:

    old dir name                new dir name

    analysis/ext                analysis_external
    analysis/ctgc               analysis_ctgc
    analysis/excp               analysis_excp
    analysis/table              analysis_table
    analysis/trail              analysis_trail
    invalid/purity              invalid_purity
    analysis/sharing            analysis_sharing
    hard_coded/purity           purity
    general/accumulator         accumulator
    analysis/unused_args        analysis_unused_args
    debugger/declarative        declarative_debugger
    hard_coded/exceptions       exceptions
    general/string_format       string_format
    hard_coded/sub-modules      submodules
    hard_coded/typeclasses      typeclasses
    general/structure_reuse     structure_reuse

Some subdirectories are still there, to wit, the subdirs of the inactive
test directory stm, each of which (if I remember correctly) holds only one
test case.

The general/structure_reuse directory previously wasn't enabled; I think
this was a bug.

tests/Mmakefile:
tools/bootcheck:
    List the new test directories.

tests/Mmake.common:
    The analysis_* directories each need to know whether the workspace
    uses subdirs. This used to be controlled from one place,
    analysis/Mmakefile, but since analysis_* are not subdirs of analysis,
    we need a new central place to find this out.

tests/analysis/common.sh:
    Update documentation for the move.

tests/OLDDIRS/Mmakefile:
    Set the subdir list to empty.

tests/NEWDIRS/Mmakefile:
    Update the "this" directory's name, as well as TESTS_DIR.

    In analysis_*/Mmakefile, use the new mechanism for detecting the presence
    of subdirs.
2015-02-10 00:43:02 +11:00