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 * emit a Firm object. Backported from irargs.
258 static void firm_emit(char *buf, int buflen, char conversion,
259 const struct settings *occ, void *X)
261 #define A(s) occ->flag_hash ? s " ": ""
274 strncpy(buf, "(null)", buflen);
278 snprintf(buf, buflen, "BAD");
279 snprintf(add, sizeof(add), "[%p]", X);
282 snprintf(buf, buflen, "%s%s", A("ent"),
283 isupper(conversion) ? get_entity_ld_name(X): get_entity_name(X));
284 snprintf(add, sizeof(add), "[%ld]", get_entity_nr(X));
287 snprintf(buf, buflen, "%s%s:%s", A("type"), get_type_tpop_name(X), get_type_name(X));
288 snprintf(add, sizeof(add), "[%ld]", get_type_nr(X));
291 snprintf(buf, buflen, "%s%s", A("irg"), get_entity_name(get_irg_entity(X)));
292 snprintf(add, sizeof(add), "[%ld]", get_irg_graph_nr(X));
295 switch (conversion) {
297 block = is_no_Block(X) ? get_nodes_block(X) : X;
298 snprintf(buf, buflen, "%s%s%s", A("irn"), get_irn_opname(block),
299 get_mode_name(get_irn_mode(block)));
300 snprintf(add, sizeof(add), "[%ld]", get_irn_node_nr(block));
303 snprintf(buf, buflen, "%ld", get_irn_node_nr(X));
307 tarval_snprintf(tv, sizeof(tv), get_Const_tarval(X));
308 snprintf(buf, buflen, "%s%s%s<%s>", A("irn"), get_irn_opname(X),
309 get_mode_name(get_irn_mode(X)), tv);
312 snprintf(buf, buflen, "%s%s%s", A("irn"), get_irn_opname(X),
313 get_mode_name(get_irn_mode(X)));
314 snprintf(add, sizeof(add), "[%ld]", get_irn_node_nr(X));
318 snprintf(buf, buflen, "%s%s", A("mode"), get_mode_name(X));
321 tarval_snprintf(tv, sizeof(tv), X);
322 snprintf(buf, buflen, "%s%s", A("tv"), tv);
325 snprintf(buf, buflen, "ldepth[%d]", get_loop_depth(X));
328 snprintf(buf, buflen, "%s%s", A("op"), get_op_name(X));
330 case k_ir_compound_graph_path:
331 n = get_compound_graph_path_length(X);
333 for (i = 0; i < n; ++i) {
334 ent = get_compound_graph_path_node(X, i);
336 strncat(buf, ".", buflen);
337 strncat(buf, get_entity_name(ent), buflen);
338 if (is_Array_type(get_entity_owner(ent))) {
339 snprintf(add, sizeof(add), "[%d]",
340 get_compound_graph_path_array_index(X, i));
341 strncat(buf, add, buflen);
348 snprintf(buf, buflen, "UNKWN");
349 snprintf(add, sizeof(add), "[%p]", X);
354 strncat(buf, add, buflen);
360 * A small printf helper routine for ir nodes.
361 * @param app An appender (this determines where the stuff is dumped to).
362 * @param object A target passed to the appender.
363 * @param limit The maximum number of characters to dump.
364 * @param fmt The format string.
365 * @param args A va_list.
367 static void ir_common_vprintf(const appender_t *app, void *object,
368 size_t limit, const char *fmt, va_list args)
374 #define DUMP_STR(s) app->append_str(object, limit, s)
375 #define DUMP_CH(ch) app->append_char(object, limit, ch)
377 app->init(object, limit);
379 for (i = 0, n = strlen(fmt); i < n; ++i) {
384 const char *len_str = "";
386 struct settings settings;
388 settings.flag_hash = 0;
389 settings.flag_zero = ' ';
391 settings.flag_minus = 0;
392 settings.flag_plus = 0;
396 /* Clear the temporary buffer */
399 /* Set the string to print to the buffer by default. */
402 while (strchr("#0-+", ch)) {
405 settings.flag_hash = 1;
408 settings.flag_zero = '0';
411 settings.flag_minus = 1;
414 settings.flag_plus = 1;
422 /* Read the field width */
427 settings.width = (int) strtol(&fmt[i], &endptr, 10);
428 increase = (char *) endptr - &fmt[i];
429 ch = fmt[i += increase];
434 /* Ignore the precision */
436 while(isdigit(ch = fmt[++i]));
438 /* read the length modifier. */
443 if((ch = fmt[++i]) == 'h') {
452 if((ch = fmt[++i]) == 'l') {
462 /* Do the conversion specifier. */
465 /* The percent itself */
474 int i, n = va_arg(args, int);
475 for(i = 0; i < n && i < sizeof(buf) - 1; ++i)
483 buf[0] = va_arg(args, int);
488 str = va_arg(args, const char *);
492 snprintf(buf, sizeof(buf), "%p", va_arg(args, void *));
503 snprintf(fmt_str, sizeof(fmt_str), "%%%s%c", len_str, ch);
510 int arg = va_arg(args, int);
511 snprintf(buf, sizeof(buf), fmt_str, arg);
517 long arg = va_arg(args, long);
518 snprintf(buf, sizeof(buf), fmt_str, arg);
524 int64_t arg = va_arg(args, int64_t);
525 snprintf(buf, sizeof(buf), fmt_str, arg);
533 str = get_id_str(va_arg(args, ident *));
547 firm_emit(buf, sizeof(buf), ch, &settings, va_arg(args, void *));
552 const bitset_t *bs = va_arg(args, const bitset_t *);
553 const char *prefix = "";
557 for(i = bitset_next_set(bs, 0); i != -1; i = bitset_next_set(bs, i + 1)) {
558 snprintf(buf, sizeof(buf), "%ld", i);
570 iterator_t *it = va_arg(args, iterator_t *);
571 void *collection = va_arg(args, void *);
573 const char *prefix = "";
574 char format = fmt[++i];
575 ir_printf_cb_t *cb = format == 'C' ? va_arg(args, ir_printf_cb_t *) : NULL;
577 assert(is_iterator(it) && "Pass an iterator interface and the collection");
579 snprintf(buf, sizeof(buf), "%%%c", format);
582 for(curr = it->start(collection); curr; curr = it->next(collection, curr)) {
586 cb(app, object, limit, curr);
588 ir_common_printf(app, object, limit, buf, curr);
592 it->finish(collection, curr);
597 /* clean the buffer again */
602 str = get_pnc_string(va_arg(args, int));
606 ir_node *irn = va_arg(args, ir_node *);
607 dbg_info *dbg = get_irn_dbg_info(irn);
609 if (dbg && __dbg_info_snprint) {
610 if (__dbg_info_snprint(buf, sizeof(buf), dbg) <= 0)
617 dump_with_settings(app, object, limit, &settings, str);
628 * Convenience for stdout dumping.
630 void ir_printf(const char *fmt, ...)
634 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
639 * Convenience for file dumping.
641 void ir_fprintf(FILE *f, const char *fmt, ...)
645 ir_common_vprintf(&file_appender, f, 0, fmt, args);
650 * Convenience for string dumping.
652 void ir_snprintf(char *buf, size_t len, const char *fmt, ...)
656 ir_common_vprintf(&str_appender, buf, len, fmt, args);
661 * Convenience for string dumping.
663 void ir_obst_printf(struct obstack *obst, const char *fmt, ...)
667 ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
671 void ir_vprintf(const char *fmt, va_list args)
673 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
676 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
678 ir_common_vprintf(&file_appender, f, 0, fmt, args);
681 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
683 ir_common_vprintf(&str_appender, buf, len, fmt, args);
686 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
688 ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
691 #else /* WITH_LIBCORE */
693 #include "irargs_t.h"
695 void ir_printf(const char *fmt, ...)
700 lc_evprintf(firm_get_arg_env(), fmt, args);
704 void ir_fprintf(FILE *f, const char *fmt, ...)
709 lc_evfprintf(firm_get_arg_env(), f, fmt, args);
713 void ir_snprintf(char *buf, size_t n, const char *fmt, ...)
718 lc_evsnprintf(firm_get_arg_env(), buf, n, fmt, args);
722 void ir_vprintf(const char *fmt, va_list args)
724 lc_evprintf(firm_get_arg_env(), fmt, args);
727 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
729 lc_evfprintf(firm_get_arg_env(), f, fmt, args);
732 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
734 lc_evsnprintf(firm_get_arg_env(), buf, len, fmt, args);
737 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
739 lc_evoprintf(firm_get_arg_env(), obst, fmt, args);