Commit 658edeeb authored by Matthias Braun's avatar Matthias Braun
Browse files

simplify tarval_snprintf API, move it to printf header

parent 58f24091
......@@ -15,6 +15,7 @@
#include <stdarg.h>
#include <stdio.h>
#include "begin.h"
#include "firm_types.h"
struct obstack;
......@@ -81,8 +82,24 @@ FIRM_API int ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args);
FIRM_API int ir_obst_vprintf(struct obstack *obst, const char *fmt,
va_list args);
/** @} */
/**
* Output a tarval in human readable format to a string buffer.
*
* This function is meant for debugging purposes, the string is formated in a
* way easily understandable for humans. You should not use this for storage or
* output for other tools (like assemblers), use get_tarval_sub_bits() for these
* cases.
*
* The final string in "buf" is guaranteed to be zero-terminated (if buflen is
* at least 1).
*
* @param buf the output buffer
* @param buflen the length of the buffer
* @param tv the tarval
*/
FIRM_API int tarval_snprintf(char *buf, size_t buflen, ir_tarval *tv);
/** @} */
#include "end.h"
#endif
......@@ -686,17 +686,6 @@ FIRM_API int get_tarval_lowest_bit(ir_tarval *tv);
*/
FIRM_API int get_tarval_highest_bit(ir_tarval *tv);
/**
* Output a tarval to a string buffer.
*
* @param buf the output buffer
* @param buflen the length of the buffer
* @param tv the tarval
* @param hex print value in hexadecimal if true, else decimal
*/
FIRM_API int tarval_snprintf(char *buf, size_t buflen, ir_tarval *tv,
int hex);
/**
* Returns non-zero if the mantissa of a floating point tarval is zero
* (i.e. 1.0Exxx)
......
......@@ -163,8 +163,7 @@ static int firm_emit(lc_appendable_t *app, const lc_arg_occ_t *occ,
if (is_Const(node)) {
ir_tarval *tv = get_Const_tarval(node);
if (tv)
tarval_snprintf(tv_buf, sizeof(tv_buf), tv,
!mode_is_float(get_tarval_mode(tv)));
tarval_snprintf(tv_buf, sizeof(tv_buf), tv);
else
strncpy(tv_buf, "(NULL)", sizeof(tv_buf));
snprintf(buf, sizeof(buf), "%s%s %s<%s>", A("irn"), get_irn_opname(node),
......@@ -197,9 +196,8 @@ static int firm_emit(lc_appendable_t *app, const lc_arg_occ_t *occ,
}
case k_tarval: {
ir_tarval *tarval = (ir_tarval*)X;
tarval_snprintf(tv_buf, sizeof(tv_buf), tarval,
!mode_is_float(get_tarval_mode(tarval)));
snprintf(buf, sizeof(buf), "%s0x%s", A("tv"), tv_buf);
tarval_snprintf(tv_buf, sizeof(tv_buf), tarval);
snprintf(buf, sizeof(buf), "%s%s", A("tv"), tv_buf);
break;
}
case k_ir_loop: {
......
......@@ -422,7 +422,7 @@ static void write_tarval(write_env_t *env, ir_tarval *tv)
} else {
fputs("0x", env->file);
char buf[1024];
tarval_snprintf(buf, sizeof(buf), tv, true);
tarval_snprintf(buf, sizeof(buf), tv);
fputs(buf, env->file);
fputc(' ', env->file);
}
......
......@@ -1225,7 +1225,7 @@ ir_tarval *tarval_shrs_unsigned(ir_tarval *a, unsigned b)
return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
}
int tarval_snprintf(char *buf, size_t len, ir_tarval *tv, int hex)
int tarval_snprintf(char *buf, size_t len, ir_tarval *tv)
{
switch (get_mode_sort(tv->mode)) {
case irms_reference:
......@@ -1233,17 +1233,13 @@ int tarval_snprintf(char *buf, size_t len, ir_tarval *tv, int hex)
return snprintf(buf, len, "NULL");
/* FALLTHROUGH */
case irms_int_number: {
const char *str;
if (hex)
str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
else
str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
return snprintf(buf, len, "%s", str);
unsigned bits = get_mode_size_bits(tv->mode);
const char *str = sc_print(tv->value, bits, SC_HEX, 0);
return snprintf(buf, len, "0x%s", str);
}
case irms_float_number:
return fc_print((const fp_value*)tv->value, buf, len,
hex ? FC_HEX : FC_DEC);
return fc_print((const fp_value*)tv->value, buf, len, FC_DEC);
case irms_internal_boolean:
return snprintf(buf, len, "%s",
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment