Files
mercury/trace/mercury_trace_util.c
Peter Ross 2e66995c01 Fix the I/O routines for floats so that can roundtrip.
Estimated hours taken: 8
Branches: main

Fix the I/O routines for floats so that can roundtrip.

library/string.m:
    Ensure that string__float_to_string returns a float that is
    round-trippable.  On the C backend we do that by starting at the min
    precision required and increasing it until the float roundtrips.
    This functionality is provided by ML_sprintf_float, so that it can
    be reused in io.m. On the IL backend the R flag guarantees that a
    double will be round-trippable, so we just use that.
    Change string__to_float so that it uses the format string for the
    precision float that we are using, and export this predicate for use
    by the trace system.
    Delete the unused string__float_to_f_string.

library/io.m:
    Print round-tripable floats in io__write_float.
    For the C backend use ML_sprintf_float to avoid unnecessary string
    allocation.  For other backends use string__float_to_string to
    generate a valid float.

trace/mercury_trace_util.c:
    Rather than duplicating the code from the library, call the code in
    the library.

tests/general/Mmakefile:
tests/general/float_roundtrip.exp:
tests/general/float_roundtrip.m:
    Test that floats roundtrip for different required miniumum
    precisions.
2002-11-21 08:00:58 +00:00

152 lines
3.2 KiB
C

/*
** Copyright (C) 2000-2002 The University of Melbourne.
** This file may only be copied under the terms of the GNU Library General
** Public License - see the file COPYING.LIB in the Mercury distribution.
*/
/*
** This file contains utility functions that can be used by any or all
** of the various kinds of Mercury debuggers.
**
** Author: zs.
*/
#include "mercury_imp.h"
#include "mercury_trace_util.h"
#include "mercury_file.h"
#include "string.mh"
#include <ctype.h>
void
MR_c_file_to_mercury_file(FILE *c_file, MercuryFile *mercury_file)
{
MR_mercuryfile_init(c_file, 1, mercury_file);
}
MR_bool
MR_trace_is_natural_number(const char *word, int *value)
{
if (MR_isdigit(*word)) {
*value = *word - '0';
word++;
while (MR_isdigit(*word)) {
*value = (*value * 10) + *word - '0';
word++;
}
if (*word == '\0') {
return MR_TRUE;
}
}
return MR_FALSE;
}
MR_bool
MR_trace_is_integer(const char *word, MR_Integer *value)
{
int sign;
if (*word == '-') {
sign = -1;
word++;
} else {
sign = 1;
}
if (MR_isdigit(*word)) {
*value = *word - '0';
word++;
while (MR_isdigit(*word)) {
*value = (*value * 10) + *word - '0';
word++;
}
if (*word == '\0') {
*value = *value * sign;
return MR_TRUE;
}
}
return MR_FALSE;
}
MR_bool
MR_trace_is_float(const char *word, MR_Float *value)
{
return ML_string_to_float((MR_String) word, value);
}
void
MR_print_stack_regs(FILE *fp, MR_Word *saved_regs)
{
#ifndef MR_HIGHLEVEL_CODE
fprintf(fp, "sp = ");
MR_print_detstackptr(fp, MR_saved_sp(saved_regs));
fprintf(fp, "\ncurfr = ");
MR_print_nondstackptr(fp, MR_saved_curfr(saved_regs));
fprintf(fp, "\nmaxfr = ");
MR_print_nondstackptr(fp, MR_saved_maxfr(saved_regs));
fprintf(fp, "\n");
#endif
}
void
MR_print_heap_regs(FILE *fp, MR_Word *saved_regs)
{
#ifndef MR_CONSERVATIVE_GC
fprintf(fp, "hp = ");
MR_print_heapptr(fp, MR_saved_hp(saved_regs));
fprintf(fp, "\nsol_hp = ");
MR_print_heapptr(fp, MR_saved_sol_hp(saved_regs));
fprintf(fp, "\nmin_hp_rec = ");
MR_print_heapptr(fp, MR_saved_min_hp_rec(saved_regs));
fprintf(fp, "\nglobal_hp = ");
MR_print_heapptr(fp, MR_saved_global_hp(saved_regs));
fprintf(fp, "\n");
#endif
}
void
MR_print_tabling_regs(FILE *fp, MR_Word *saved_regs)
{
#ifdef MR_USE_MINIMAL_MODEL
fprintf(fp, "gen_next = %ld\n", (long) MR_saved_gen_next(saved_regs));
fprintf(fp, "cut_next = %ld\n", (long) MR_saved_cut_next(saved_regs));
#endif
}
void
MR_print_succip_reg(FILE *fp, MR_Word *saved_regs)
{
#ifndef MR_HIGHLEVEL_CODE
fprintf(fp, "succip = ");
MR_print_label(fp, MR_saved_succip(saved_regs));
fprintf(fp, "\n");
#endif
}
void
MR_print_r_regs(FILE *fp, MR_Word *saved_regs)
{
#ifndef MR_HIGHLEVEL_CODE
fprintf(fp, "r1 = %ld (%lx)\n",
(long) MR_saved_reg(saved_regs, 1),
(long) MR_saved_reg(saved_regs, 1));
fprintf(fp, "r2 = %ld (%lx)\n",
(long) MR_saved_reg(saved_regs, 2),
(long) MR_saved_reg(saved_regs, 2));
fprintf(fp, "r3 = %ld (%lx)\n",
(long) MR_saved_reg(saved_regs, 3),
(long) MR_saved_reg(saved_regs, 3));
fprintf(fp, "r4 = %ld (%lx)\n",
(long) MR_saved_reg(saved_regs, 4),
(long) MR_saved_reg(saved_regs, 4));
fprintf(fp, "r5 = %ld (%lx)\n",
(long) MR_saved_reg(saved_regs, 5),
(long) MR_saved_reg(saved_regs, 5));
#endif
}