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_size: /* type size */
332 snprintf(tv_buf, sizeof(tv_buf), "<SIZE:%s>", get_type_name(get_SymConst_type(X)));
334 case symconst_addr_name: /* linker name */
335 snprintf(tv_buf, sizeof(tv_buf), "<EXT:%s>", get_id_str(get_SymConst_name(X)));
337 case symconst_addr_ent: /* entity name */
338 snprintf(tv_buf, sizeof(tv_buf), "<%s>", get_entity_name(get_SymConst_entity(X)));
343 snprintf(buf, buflen, "%s%s%s%s", A("irn"), get_irn_opname(X),
344 get_mode_name(get_irn_mode(X)), tv_buf);
347 snprintf(buf, buflen, "%s%s%s", A("irn"), get_irn_opname(X),
348 get_mode_name(get_irn_mode(X)));
349 snprintf(add, sizeof(add), "[%ld:%d]", get_irn_node_nr(X), get_irn_idx(X));
353 snprintf(buf, buflen, "%s%s", A("mode"), get_mode_name(X));
356 tarval_snprintf(tv_buf, sizeof(tv_buf), X);
357 snprintf(buf, buflen, "%s%s", A("tv"), tv_buf);
360 snprintf(buf, sizeof(buf), "loop[%d:%d]", get_loop_loop_nr(X), get_loop_depth(X));
363 snprintf(buf, buflen, "%s%s", A("op"), get_op_name(X));
365 case k_ir_compound_graph_path:
366 n = get_compound_graph_path_length(X);
368 for (i = 0; i < n; ++i) {
369 ent = get_compound_graph_path_node(X, i);
371 strncat(buf, ".", buflen);
372 strncat(buf, get_entity_name(ent), buflen);
373 if (is_Array_type(get_entity_owner(ent))) {
374 snprintf(add, sizeof(add), "[%d]",
375 get_compound_graph_path_array_index(X, i));
376 strncat(buf, add, buflen);
383 snprintf(buf, buflen, "UNKWN");
384 snprintf(add, sizeof(add), "[%p]", X);
389 strncat(buf, add, buflen);
395 * A small printf helper routine for ir nodes.
396 * @param app An appender (this determines where the stuff is dumped to).
397 * @param object A target passed to the appender.
398 * @param limit The maximum number of characters to dump.
399 * @param fmt The format string.
400 * @param args A va_list.
402 static void ir_common_vprintf(const appender_t *app, void *object,
403 size_t limit, const char *fmt, va_list args)
409 #define DUMP_STR(s) app->append_str(object, limit, s)
410 #define DUMP_CH(ch) app->append_char(object, limit, ch)
412 app->init(object, limit);
414 for (i = 0, n = strlen(fmt); i < n; ++i) {
419 const char *len_str = "";
421 struct settings settings;
423 settings.flag_hash = 0;
424 settings.flag_zero = ' ';
426 settings.flag_minus = 0;
427 settings.flag_plus = 0;
431 /* Clear the temporary buffer */
434 /* Set the string to print to the buffer by default. */
437 while (strchr("#0-+", ch)) {
440 settings.flag_hash = 1;
443 settings.flag_zero = '0';
446 settings.flag_minus = 1;
449 settings.flag_plus = 1;
457 /* Read the field width */
462 settings.width = (int) strtol(&fmt[i], &endptr, 10);
463 increase = (char *) endptr - &fmt[i];
464 ch = fmt[i += increase];
469 /* Ignore the precision */
471 while(isdigit(ch = fmt[++i]));
473 /* read the length modifier. */
478 if((ch = fmt[++i]) == 'h') {
487 if((ch = fmt[++i]) == 'l') {
497 /* Do the conversion specifier. */
500 /* The percent itself */
509 int i, n = va_arg(args, int);
510 for(i = 0; i < n && i < sizeof(buf) - 1; ++i)
518 buf[0] = va_arg(args, int);
523 str = va_arg(args, const char *);
527 snprintf(buf, sizeof(buf), "%p", va_arg(args, void *));
538 snprintf(fmt_str, sizeof(fmt_str), "%%%s%c", len_str, ch);
545 int arg = va_arg(args, int);
546 snprintf(buf, sizeof(buf), fmt_str, arg);
552 long arg = va_arg(args, long);
553 snprintf(buf, sizeof(buf), fmt_str, arg);
559 int64_t arg = va_arg(args, int64_t);
560 snprintf(buf, sizeof(buf), fmt_str, arg);
568 str = get_id_str(va_arg(args, ident *));
582 firm_emit(buf, sizeof(buf), ch, &settings, va_arg(args, void *));
587 const bitset_t *bs = va_arg(args, const bitset_t *);
588 const char *prefix = "";
592 for(i = bitset_next_set(bs, 0); i != -1; i = bitset_next_set(bs, i + 1)) {
593 snprintf(buf, sizeof(buf), "%ld", i);
605 iterator_t *it = va_arg(args, iterator_t *);
606 void *collection = va_arg(args, void *);
608 const char *prefix = "";
609 char format = fmt[++i];
610 ir_printf_cb_t *cb = format == 'C' ? va_arg(args, ir_printf_cb_t *) : NULL;
612 assert(is_iterator(it) && "Pass an iterator interface and the collection");
614 snprintf(buf, sizeof(buf), "%%%c", format);
617 for(curr = it->start(collection); curr; curr = it->next(collection, curr)) {
621 cb(app, object, limit, curr);
623 ir_common_printf(app, object, limit, buf, curr);
627 it->finish(collection, curr);
632 /* clean the buffer again */
637 str = get_pnc_string(va_arg(args, int));
641 ir_node *irn = va_arg(args, ir_node *);
642 dbg_info *dbg = get_irn_dbg_info(irn);
644 if (dbg && __dbg_info_snprint) {
645 if (__dbg_info_snprint(buf, sizeof(buf), dbg) <= 0)
652 dump_with_settings(app, object, limit, &settings, str);
663 * Convenience for stdout dumping.
665 void ir_printf(const char *fmt, ...)
669 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
674 * Convenience for file dumping.
676 void ir_fprintf(FILE *f, const char *fmt, ...)
680 ir_common_vprintf(&file_appender, f, 0, fmt, args);
685 * Convenience for string dumping.
687 void ir_snprintf(char *buf, size_t len, const char *fmt, ...)
691 ir_common_vprintf(&str_appender, buf, len, fmt, args);
696 * Convenience for string dumping.
698 void ir_obst_printf(struct obstack *obst, const char *fmt, ...)
702 ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
706 void ir_vprintf(const char *fmt, va_list args)
708 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
711 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
713 ir_common_vprintf(&file_appender, f, 0, fmt, args);
716 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
718 ir_common_vprintf(&str_appender, buf, len, fmt, args);
721 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
723 ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
726 #else /* WITH_LIBCORE */
728 #include "irargs_t.h"
730 void ir_printf(const char *fmt, ...)
735 lc_evprintf(firm_get_arg_env(), fmt, args);
739 void ir_fprintf(FILE *f, const char *fmt, ...)
744 lc_evfprintf(firm_get_arg_env(), f, fmt, args);
748 void ir_snprintf(char *buf, size_t n, const char *fmt, ...)
753 lc_evsnprintf(firm_get_arg_env(), buf, n, fmt, args);
757 void ir_vprintf(const char *fmt, va_list args)
759 lc_evprintf(firm_get_arg_env(), fmt, args);
762 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
764 lc_evfprintf(firm_get_arg_env(), f, fmt, args);
767 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
769 lc_evsnprintf(firm_get_arg_env(), buf, len, fmt, args);
772 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
774 lc_evoprintf(firm_get_arg_env(), obst, fmt, args);