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 @@ ...@@ -15,6 +15,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include "begin.h" #include "begin.h"
#include "firm_types.h"
struct obstack; struct obstack;
...@@ -81,8 +82,24 @@ FIRM_API int ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args); ...@@ -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, FIRM_API int ir_obst_vprintf(struct obstack *obst, const char *fmt,
va_list args); 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" #include "end.h"
#endif #endif
...@@ -686,17 +686,6 @@ FIRM_API int get_tarval_lowest_bit(ir_tarval *tv); ...@@ -686,17 +686,6 @@ FIRM_API int get_tarval_lowest_bit(ir_tarval *tv);
*/ */
FIRM_API int get_tarval_highest_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 * Returns non-zero if the mantissa of a floating point tarval is zero
* (i.e. 1.0Exxx) * (i.e. 1.0Exxx)
......
...@@ -163,8 +163,7 @@ static int firm_emit(lc_appendable_t *app, const lc_arg_occ_t *occ, ...@@ -163,8 +163,7 @@ static int firm_emit(lc_appendable_t *app, const lc_arg_occ_t *occ,
if (is_Const(node)) { if (is_Const(node)) {
ir_tarval *tv = get_Const_tarval(node); ir_tarval *tv = get_Const_tarval(node);
if (tv) if (tv)
tarval_snprintf(tv_buf, sizeof(tv_buf), tv, tarval_snprintf(tv_buf, sizeof(tv_buf), tv);
!mode_is_float(get_tarval_mode(tv)));
else else
strncpy(tv_buf, "(NULL)", sizeof(tv_buf)); strncpy(tv_buf, "(NULL)", sizeof(tv_buf));
snprintf(buf, sizeof(buf), "%s%s %s<%s>", A("irn"), get_irn_opname(node), 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, ...@@ -197,9 +196,8 @@ static int firm_emit(lc_appendable_t *app, const lc_arg_occ_t *occ,
} }
case k_tarval: { case k_tarval: {
ir_tarval *tarval = (ir_tarval*)X; ir_tarval *tarval = (ir_tarval*)X;
tarval_snprintf(tv_buf, sizeof(tv_buf), tarval, tarval_snprintf(tv_buf, sizeof(tv_buf), tarval);
!mode_is_float(get_tarval_mode(tarval))); snprintf(buf, sizeof(buf), "%s%s", A("tv"), tv_buf);
snprintf(buf, sizeof(buf), "%s0x%s", A("tv"), tv_buf);
break; break;
} }
case k_ir_loop: { case k_ir_loop: {
......
...@@ -422,7 +422,7 @@ static void write_tarval(write_env_t *env, ir_tarval *tv) ...@@ -422,7 +422,7 @@ static void write_tarval(write_env_t *env, ir_tarval *tv)
} else { } else {
fputs("0x", env->file); fputs("0x", env->file);
char buf[1024]; char buf[1024];
tarval_snprintf(buf, sizeof(buf), tv, true); tarval_snprintf(buf, sizeof(buf), tv);
fputs(buf, env->file); fputs(buf, env->file);
fputc(' ', env->file); fputc(' ', env->file);
} }
......
...@@ -1225,7 +1225,7 @@ ir_tarval *tarval_shrs_unsigned(ir_tarval *a, unsigned b) ...@@ -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); 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)) { switch (get_mode_sort(tv->mode)) {
case irms_reference: case irms_reference:
...@@ -1233,17 +1233,13 @@ int tarval_snprintf(char *buf, size_t len, ir_tarval *tv, int hex) ...@@ -1233,17 +1233,13 @@ int tarval_snprintf(char *buf, size_t len, ir_tarval *tv, int hex)
return snprintf(buf, len, "NULL"); return snprintf(buf, len, "NULL");
/* FALLTHROUGH */ /* FALLTHROUGH */
case irms_int_number: { case irms_int_number: {
const char *str; unsigned bits = get_mode_size_bits(tv->mode);
if (hex) const char *str = sc_print(tv->value, bits, SC_HEX, 0);
str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0); return snprintf(buf, len, "0x%s", str);
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);
} }
case irms_float_number: case irms_float_number:
return fc_print((const fp_value*)tv->value, buf, len, return fc_print((const fp_value*)tv->value, buf, len, FC_DEC);
hex ? FC_HEX : FC_DEC);
case irms_internal_boolean: case irms_internal_boolean:
return snprintf(buf, len, "%s", return snprintf(buf, len, "%s",
......
Markdown is supported
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