3 * File name: ir/ir/irprintf.c
4 * Purpose: A little printf helper unterstanding firm types
5 * Author: Sebastian Hack
8 * Copyright: (c) 1998-2004 Universität Karlsruhe
9 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
15 * A little printf helper unterstanding firm types.
16 * @author Sebastian Hack
27 #ifdef HAVE_INTTYPES_H
37 #include "firm_config.h"
49 #include "dbginfo_t.h"
51 #define STRNIL "(nil)"
56 static void str_init(void *object, size_t n)
62 * append a char to a string buffer.
64 static void str_append_char(void *object, size_t n, char ch)
71 strncat(object, buf, n);
75 * append a string to a string buffer.
77 static void str_append_str(void *object, size_t n, const char *str)
79 strncat(object, str, n);
84 * Init the file. i.e. do nothing.
86 static void file_init(void *object, size_t n)
91 * append a char to a file.
93 static void file_append_char(void *object, size_t n, char ch)
99 * append a string to a file.
101 static void file_append_str(void *object, size_t n, const char *str)
107 * Init the obstack. i.e. do nothing.
109 static void obst_init(void *object, size_t n)
114 * append a char to a obstack.
116 static void obst_append_char(void *object, size_t n, char ch)
118 struct obstack *obst = object;
119 obstack_1grow(obst, ch);
123 * append a string to a obstack.
125 static void obst_append_str(void *object, size_t n, const char *str)
127 struct obstack *obst = object;
128 obstack_grow(obst, str, strlen(str));
135 static const appender_t file_appender = {
142 * the string buffer appender
144 static const appender_t str_appender = {
151 * the obstack appender.
153 static const appender_t obst_appender = {
161 static void ir_common_vprintf(const appender_t *app, void *object,
162 size_t limit, const char *fmt, va_list args);
164 static INLINE void ir_common_printf(const appender_t *app, void *object,
165 size_t limit, const char *fmt, ...)
170 ir_common_vprintf(app, object, limit, fmt, args);
175 static int is_std_fmt(const char *fmt)
177 static const char *fmt_re_str =
178 "^[0 -+#']?[1-9]*(\\.[1-9]*)?[hlLqjzt]?[diouxXeEfFgGaAc]";
180 static regex_t fmt_re;
181 static int preapred_re = 0;
187 int res = regcomp(&fmt_re, fmt_re_str, REG_EXTENDED);
188 assert(res == 0 && "Could not prepare regex");
192 res = regexec(&fmt_re, fmt, 1, &match[0], 0);
197 regerror(res, &fmt_re, buf, sizeof(buf));
201 printf("res: %d, start: %d, end: %d\n",
202 res, match[0].rm_so, match[0].rm_eo);
205 return res == 0 ? match[0].rm_eo : -1;
217 /* Length specifiers. */
227 #define MIN(x,y) ((x) < (y) ? (x) : (y))
228 #define MAX(x,y) ((x) > (y) ? (x) : (y))
230 static void dump_with_settings(const appender_t *app, void *object, size_t limit,
231 const struct settings *settings, const char *str)
233 if (settings->width >= 0) {
235 size_t n = strlen(str);
236 int lim = MIN(settings->width, (int)limit);
237 int to_print = MIN(lim, (int)n);
238 int to_pad = to_print - lim;
240 if (!settings->flag_minus)
241 for(i = 0; i < to_pad; ++i)
242 app->append_char(object, lim, settings->flag_zero);
244 app->append_str(object, to_print, str);
246 if (!settings->flag_minus)
247 for(i = 0; i < to_pad; ++i)
248 app->append_char(object, lim, settings->flag_zero);
252 app->append_str(object, limit, str);
256 * Beware: do not set the entity ld_name
258 static const char *get_entity_ld_name_ex(entity *ent) {
260 return get_entity_ld_name(ent);
261 return get_entity_name(ent);
265 * emit a Firm object. Backported from irargs.
267 static void firm_emit(char *buf, int buflen, char conversion,
268 const struct settings *occ, void *X)
270 #define A(s) occ->flag_hash ? s " ": ""
283 strncpy(buf, "(null)", buflen);
287 snprintf(buf, buflen, "BAD");
288 snprintf(add, sizeof(add), "[%p]", X);
291 snprintf(buf, buflen, "%s%s", A("ent"),
292 isupper(conversion) ? get_entity_ld_name_ex(X): get_entity_name(X));
293 snprintf(add, sizeof(add), "[%ld]", get_entity_nr(X));
296 snprintf(buf, buflen, "%s%s:%s", A("type"), get_type_tpop_name(X), get_type_name(X));
297 snprintf(add, sizeof(add), "[%ld]", get_type_nr(X));
300 if (X == get_const_code_irg())
301 snprintf(buf, buflen, "const_code_irg ");
303 snprintf(buf, buflen, "%s%s", A("irg"), get_entity_name(get_irg_entity(X)));
304 snprintf(add, sizeof(add), "[%ld]", get_irg_graph_nr(X));
307 switch (conversion) {
309 block = is_no_Block(X) ? get_nodes_block(X) : X;
310 snprintf(buf, buflen, "%s%s%s", A("irn"), get_irn_opname(block),
311 get_mode_name(get_irn_mode(block)));
312 snprintf(add, sizeof(add), "[%ld]", get_irn_node_nr(block));
315 snprintf(buf, buflen, "%ld", get_irn_node_nr(X));
319 tarval *tv = get_Const_tarval(X);
322 tarval_snprintf(tv_buf, sizeof(tv_buf), tv);
323 snprintf(buf, buflen, "%s%s%s<%s>", A("irn"), get_irn_opname(X),
324 get_mode_name(get_irn_mode(X)), tv ? tv_buf : ">NULL<");
326 else if (get_irn_op(X) == op_SymConst) {
327 switch (get_SymConst_kind(X)) {
328 case symconst_type_tag: /* type tag */
329 snprintf(tv_buf, sizeof(tv_buf), "<ID:%s>", get_type_name(get_SymConst_type(X)));
331 case symconst_type_size: /* type size */
332 snprintf(tv_buf, sizeof(tv_buf), "<SIZE:%s>", get_type_name(get_SymConst_type(X)));
334 case symconst_type_align: /* type alignment */
335 snprintf(tv_buf, sizeof(tv_buf), "<ALIGN:%s>", get_type_name(get_SymConst_type(X)));
337 case symconst_addr_name: /* linker name */
338 snprintf(tv_buf, sizeof(tv_buf), "<EXT:%s>", get_id_str(get_SymConst_name(X)));
340 case symconst_addr_ent: /* entity name */
341 snprintf(tv_buf, sizeof(tv_buf), "<%s>", get_entity_name(get_SymConst_entity(X)));
346 snprintf(buf, buflen, "%s%s%s%s", A("irn"), get_irn_opname(X),
347 get_mode_name(get_irn_mode(X)), tv_buf);
350 snprintf(buf, buflen, "%s%s%s", A("irn"), get_irn_opname(X),
351 get_mode_name(get_irn_mode(X)));
352 snprintf(add, sizeof(add), "[%ld:%d]", get_irn_node_nr(X), get_irn_idx(X));
356 snprintf(buf, buflen, "%s%s", A("mode"), get_mode_name(X));
359 tarval_snprintf(tv_buf, sizeof(tv_buf), X);
360 snprintf(buf, buflen, "%s%s", A("tv"), tv_buf);
363 snprintf(buf, sizeof(buf), "loop[%d:%d]", get_loop_loop_nr(X), get_loop_depth(X));
366 snprintf(buf, buflen, "%s%s", A("op"), get_op_name(X));
368 case k_ir_compound_graph_path:
369 n = get_compound_graph_path_length(X);
371 for (i = 0; i < n; ++i) {
372 ent = get_compound_graph_path_node(X, i);
374 strncat(buf, ".", buflen);
375 strncat(buf, get_entity_name(ent), buflen);
376 if (is_Array_type(get_entity_owner(ent))) {
377 snprintf(add, sizeof(add), "[%d]",
378 get_compound_graph_path_array_index(X, i));
379 strncat(buf, add, buflen);
386 snprintf(buf, buflen, "UNKWN");
387 snprintf(add, sizeof(add), "[%p]", X);
392 strncat(buf, add, buflen);
398 * A small printf helper routine for ir nodes.
399 * @param app An appender (this determines where the stuff is dumped to).
400 * @param object A target passed to the appender.
401 * @param limit The maximum number of characters to dump.
402 * @param fmt The format string.
403 * @param args A va_list.
405 static void ir_common_vprintf(const appender_t *app, void *object,
406 size_t limit, const char *fmt, va_list args)
412 #define DUMP_STR(s) app->append_str(object, limit, s)
413 #define DUMP_CH(ch) app->append_char(object, limit, ch)
415 app->init(object, limit);
417 for (i = 0, n = strlen(fmt); i < n; ++i) {
422 const char *len_str = "";
424 struct settings settings;
426 settings.flag_hash = 0;
427 settings.flag_zero = ' ';
429 settings.flag_minus = 0;
430 settings.flag_plus = 0;
434 /* Clear the temporary buffer */
437 /* Set the string to print to the buffer by default. */
440 while (strchr("#0-+", ch)) {
443 settings.flag_hash = 1;
446 settings.flag_zero = '0';
449 settings.flag_minus = 1;
452 settings.flag_plus = 1;
460 /* Read the field width */
465 settings.width = (int) strtol(&fmt[i], &endptr, 10);
466 increase = (char *) endptr - &fmt[i];
467 ch = fmt[i += increase];
472 /* Ignore the precision */
474 while(isdigit(ch = fmt[++i]));
476 /* read the length modifier. */
482 if((ch = fmt[i]) == 'h') {
493 if ((ch = fmt[i]) == 'l') {
498 else if ((ch = fmt[i]) == 'u') {
509 /* Do the conversion specifier. */
512 /* The percent itself */
521 int i, n = va_arg(args, int);
522 for(i = 0; i < n && i < sizeof(buf) - 1; ++i)
530 buf[0] = va_arg(args, int);
535 str = va_arg(args, const char *);
539 snprintf(buf, sizeof(buf), "%p", va_arg(args, void *));
550 snprintf(fmt_str, sizeof(fmt_str), "%%%s%c", len_str, ch);
557 int arg = va_arg(args, int);
558 snprintf(buf, sizeof(buf), fmt_str, arg);
564 long arg = va_arg(args, long);
565 snprintf(buf, sizeof(buf), fmt_str, arg);
571 int64_t arg = va_arg(args, int64_t);
572 snprintf(buf, sizeof(buf), fmt_str, arg);
580 str = get_id_str(va_arg(args, ident *));
594 firm_emit(buf, sizeof(buf), ch, &settings, va_arg(args, void *));
599 const bitset_t *bs = va_arg(args, const bitset_t *);
600 const char *prefix = "";
604 for(i = bitset_next_set(bs, 0); i != -1; i = bitset_next_set(bs, i + 1)) {
605 snprintf(buf, sizeof(buf), "%ld", i);
617 iterator_t *it = va_arg(args, iterator_t *);
618 void *collection = va_arg(args, void *);
620 const char *prefix = "";
621 char format = fmt[++i];
622 ir_printf_cb_t *cb = format == 'C' ? va_arg(args, ir_printf_cb_t *) : NULL;
624 assert(is_iterator(it) && "Pass an iterator interface and the collection");
626 snprintf(buf, sizeof(buf), "%%%c", format);
629 for(curr = it->start(collection); curr; curr = it->next(collection, curr)) {
633 cb(app, object, limit, curr);
635 ir_common_printf(app, object, limit, buf, curr);
639 it->finish(collection, curr);
644 /* clean the buffer again */
649 str = get_pnc_string(va_arg(args, int));
653 ir_node *irn = va_arg(args, ir_node *);
654 dbg_info *dbg = get_irn_dbg_info(irn);
656 if (dbg && __dbg_info_snprint) {
657 if (__dbg_info_snprint(buf, sizeof(buf), dbg) <= 0)
664 dump_with_settings(app, object, limit, &settings, str);
675 * Convenience for stdout dumping.
677 void ir_printf(const char *fmt, ...)
681 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
686 * Convenience for file dumping.
688 void ir_fprintf(FILE *f, const char *fmt, ...)
692 ir_common_vprintf(&file_appender, f, 0, fmt, args);
697 * Convenience for string dumping.
699 void ir_snprintf(char *buf, size_t len, const char *fmt, ...)
703 ir_common_vprintf(&str_appender, buf, len, fmt, args);
708 * Convenience for string dumping.
710 void ir_obst_printf(struct obstack *obst, const char *fmt, ...)
714 ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
718 void ir_vprintf(const char *fmt, va_list args)
720 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
723 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
725 ir_common_vprintf(&file_appender, f, 0, fmt, args);
728 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
730 ir_common_vprintf(&str_appender, buf, len, fmt, args);
733 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
735 ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
738 #else /* WITH_LIBCORE */
740 #include "irargs_t.h"
742 void ir_printf(const char *fmt, ...)
747 lc_evprintf(firm_get_arg_env(), fmt, args);
751 void ir_fprintf(FILE *f, const char *fmt, ...)
756 lc_evfprintf(firm_get_arg_env(), f, fmt, args);
760 void ir_snprintf(char *buf, size_t n, const char *fmt, ...)
765 lc_evsnprintf(firm_get_arg_env(), buf, n, fmt, args);
769 void ir_vprintf(const char *fmt, va_list args)
771 lc_evprintf(firm_get_arg_env(), fmt, args);
774 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
776 lc_evfprintf(firm_get_arg_env(), f, fmt, args);
779 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
781 lc_evsnprintf(firm_get_arg_env(), buf, len, fmt, args);
784 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
786 lc_evoprintf(firm_get_arg_env(), obst, fmt, args);