2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief A little printf helper unterstanding firm types
23 * @author Sebastian Hack
34 #ifdef HAVE_INTTYPES_H
44 #include "firm_config.h"
56 #include "dbginfo_t.h"
58 #define STRNIL "(nil)"
63 static void str_init(void *object, size_t n)
69 * append a char to a string buffer.
71 static void str_append_char(void *object, size_t n, char ch)
78 strncat(object, buf, n);
82 * append a string to a string buffer.
84 static void str_append_str(void *object, size_t n, const char *str)
86 strncat(object, str, n);
91 * Init the file. i.e. do nothing.
93 static void file_init(void *object, size_t n)
98 * append a char to a file.
100 static void file_append_char(void *object, size_t n, char ch)
106 * append a string to a file.
108 static void file_append_str(void *object, size_t n, const char *str)
114 * Init the obstack. i.e. do nothing.
116 static void obst_init(void *object, size_t n)
121 * append a char to a obstack.
123 static void obst_append_char(void *object, size_t n, char ch)
125 struct obstack *obst = object;
126 obstack_1grow(obst, ch);
130 * append a string to a obstack.
132 static void obst_append_str(void *object, size_t n, const char *str)
134 struct obstack *obst = object;
135 obstack_grow(obst, str, strlen(str));
142 static const appender_t file_appender = {
149 * the string buffer appender
151 static const appender_t str_appender = {
158 * the obstack appender.
160 static const appender_t obst_appender = {
168 static void ir_common_vprintf(const appender_t *app, void *object,
169 size_t limit, const char *fmt, va_list args);
171 static INLINE void ir_common_printf(const appender_t *app, void *object,
172 size_t limit, const char *fmt, ...)
177 ir_common_vprintf(app, object, limit, fmt, args);
182 static int is_std_fmt(const char *fmt)
184 static const char *fmt_re_str =
185 "^[0 -+#']?[1-9]*(\\.[1-9]*)?[hlLqjzt]?[diouxXeEfFgGaAc]";
187 static regex_t fmt_re;
188 static int preapred_re = 0;
194 int res = regcomp(&fmt_re, fmt_re_str, REG_EXTENDED);
195 assert(res == 0 && "Could not prepare regex");
199 res = regexec(&fmt_re, fmt, 1, &match[0], 0);
204 regerror(res, &fmt_re, buf, sizeof(buf));
208 printf("res: %d, start: %d, end: %d\n",
209 res, match[0].rm_so, match[0].rm_eo);
212 return res == 0 ? match[0].rm_eo : -1;
224 /* Length specifiers. */
234 #define MIN(x,y) ((x) < (y) ? (x) : (y))
235 #define MAX(x,y) ((x) > (y) ? (x) : (y))
237 static void dump_with_settings(const appender_t *app, void *object, size_t limit,
238 const struct settings *settings, const char *str)
240 if (settings->width >= 0) {
242 size_t n = strlen(str);
243 int lim = MIN(settings->width, (int)limit);
244 int to_print = MIN(lim, (int)n);
245 int to_pad = to_print - lim;
247 if (!settings->flag_minus)
248 for(i = 0; i < to_pad; ++i)
249 app->append_char(object, lim, settings->flag_zero);
251 app->append_str(object, to_print, str);
253 if (!settings->flag_minus)
254 for(i = 0; i < to_pad; ++i)
255 app->append_char(object, lim, settings->flag_zero);
259 app->append_str(object, limit, str);
263 * Beware: do not set the entity ld_name
265 static const char *get_entity_ld_name_ex(entity *ent) {
267 return get_entity_ld_name(ent);
268 return get_entity_name(ent);
272 * emit a Firm object. Backported from irargs.
274 static void firm_emit(char *buf, int buflen, char conversion,
275 const struct settings *occ, void *X)
277 #define A(s) occ->flag_hash ? s " ": ""
290 strncpy(buf, "(null)", buflen);
294 snprintf(buf, buflen, "BAD");
295 snprintf(add, sizeof(add), "[%p]", X);
298 snprintf(buf, buflen, "%s%s", A("ent"),
299 isupper(conversion) ? get_entity_ld_name_ex(X): get_entity_name(X));
300 snprintf(add, sizeof(add), "[%ld]", get_entity_nr(X));
303 snprintf(buf, buflen, "%s%s:%s", A("type"), get_type_tpop_name(X), get_type_name(X));
304 snprintf(add, sizeof(add), "[%ld]", get_type_nr(X));
307 if (X == get_const_code_irg())
308 snprintf(buf, buflen, "const_code_irg ");
310 snprintf(buf, buflen, "%s%s", A("irg"), get_entity_name(get_irg_entity(X)));
311 snprintf(add, sizeof(add), "[%ld]", get_irg_graph_nr(X));
314 switch (conversion) {
316 block = is_no_Block(X) ? get_nodes_block(X) : X;
317 snprintf(buf, buflen, "%s%s%s", A("irn"), get_irn_opname(block),
318 get_mode_name(get_irn_mode(block)));
319 snprintf(add, sizeof(add), "[%ld]", get_irn_node_nr(block));
322 snprintf(buf, buflen, "%ld", get_irn_node_nr(X));
326 tarval *tv = get_Const_tarval(X);
329 tarval_snprintf(tv_buf, sizeof(tv_buf), tv);
330 snprintf(buf, buflen, "%s%s%s<%s>", A("irn"), get_irn_opname(X),
331 get_mode_name(get_irn_mode(X)), tv ? tv_buf : ">NULL<");
333 else if (get_irn_op(X) == op_SymConst) {
334 switch (get_SymConst_kind(X)) {
335 case symconst_type_tag: /* type tag */
336 snprintf(tv_buf, sizeof(tv_buf), "<ID:%s>", get_type_name(get_SymConst_type(X)));
338 case symconst_type_size: /* type size */
339 snprintf(tv_buf, sizeof(tv_buf), "<SIZE:%s>", get_type_name(get_SymConst_type(X)));
341 case symconst_type_align: /* type alignment */
342 snprintf(tv_buf, sizeof(tv_buf), "<ALIGN:%s>", get_type_name(get_SymConst_type(X)));
344 case symconst_addr_name: /* linker name */
345 snprintf(tv_buf, sizeof(tv_buf), "<EXT:%s>", get_id_str(get_SymConst_name(X)));
347 case symconst_addr_ent: /* entity name */
348 snprintf(tv_buf, sizeof(tv_buf), "<%s>", get_entity_name(get_SymConst_entity(X)));
350 case symconst_enum_const: /* enumeration constant */
351 snprintf(tv_buf, sizeof(tv_buf), "<ENUM:%s>", get_enumeration_name(get_SymConst_enum(X)));
356 snprintf(buf, buflen, "%s%s%s%s", A("irn"), get_irn_opname(X),
357 get_mode_name(get_irn_mode(X)), tv_buf);
360 snprintf(buf, buflen, "%s%s%s", A("irn"), get_irn_opname(X),
361 get_mode_name(get_irn_mode(X)));
362 snprintf(add, sizeof(add), "[%ld:%d]", get_irn_node_nr(X), get_irn_idx(X));
366 snprintf(buf, buflen, "%s%s", A("mode"), get_mode_name(X));
369 tarval_snprintf(tv_buf, sizeof(tv_buf), X);
370 snprintf(buf, buflen, "%s%s", A("tv"), tv_buf);
373 snprintf(buf, sizeof(buf), "loop[%d:%d]", get_loop_loop_nr(X), get_loop_depth(X));
376 snprintf(buf, buflen, "%s%s", A("op"), get_op_name(X));
378 case k_ir_compound_graph_path:
379 n = get_compound_graph_path_length(X);
381 for (i = 0; i < n; ++i) {
382 ent = get_compound_graph_path_node(X, i);
384 strncat(buf, ".", buflen);
385 strncat(buf, get_entity_name(ent), buflen);
386 if (is_Array_type(get_entity_owner(ent))) {
387 snprintf(add, sizeof(add), "[%d]",
388 get_compound_graph_path_array_index(X, i));
389 strncat(buf, add, buflen);
396 snprintf(buf, buflen, "UNKWN");
397 snprintf(add, sizeof(add), "[%p]", X);
402 strncat(buf, add, buflen);
408 * A small printf helper routine for ir nodes.
409 * @param app An appender (this determines where the stuff is dumped to).
410 * @param object A target passed to the appender.
411 * @param limit The maximum number of characters to dump.
412 * @param fmt The format string.
413 * @param args A va_list.
415 static void ir_common_vprintf(const appender_t *app, void *object,
416 size_t limit, const char *fmt, va_list args)
422 #define DUMP_STR(s) app->append_str(object, limit, s)
423 #define DUMP_CH(ch) app->append_char(object, limit, ch)
425 app->init(object, limit);
427 for (i = 0, n = strlen(fmt); i < n; ++i) {
432 const char *len_str = "";
434 struct settings settings;
436 settings.flag_hash = 0;
437 settings.flag_zero = ' ';
439 settings.flag_minus = 0;
440 settings.flag_plus = 0;
444 /* Clear the temporary buffer */
447 /* Set the string to print to the buffer by default. */
450 while (strchr("#0-+", ch)) {
453 settings.flag_hash = 1;
456 settings.flag_zero = '0';
459 settings.flag_minus = 1;
462 settings.flag_plus = 1;
470 /* Read the field width */
475 settings.width = (int) strtol(&fmt[i], &endptr, 10);
476 increase = (char *) endptr - &fmt[i];
477 ch = fmt[i += increase];
482 /* Ignore the precision */
484 while(isdigit(ch = fmt[++i]));
486 /* read the length modifier. */
492 if((ch = fmt[i]) == 'h') {
503 if ((ch = fmt[i]) == 'l') {
508 else if ((ch = fmt[i]) == 'u') {
519 /* Do the conversion specifier. */
522 /* The percent itself */
531 int i, n = va_arg(args, int);
532 for(i = 0; i < n && i < sizeof(buf) - 1; ++i)
540 buf[0] = va_arg(args, int);
545 str = va_arg(args, const char *);
549 snprintf(buf, sizeof(buf), "%p", va_arg(args, void *));
560 snprintf(fmt_str, sizeof(fmt_str), "%%%s%c", len_str, ch);
567 int arg = va_arg(args, int);
568 snprintf(buf, sizeof(buf), fmt_str, arg);
574 long arg = va_arg(args, long);
575 snprintf(buf, sizeof(buf), fmt_str, arg);
581 int64_t arg = va_arg(args, int64_t);
582 snprintf(buf, sizeof(buf), fmt_str, arg);
590 str = get_id_str(va_arg(args, ident *));
604 firm_emit(buf, sizeof(buf), ch, &settings, va_arg(args, void *));
609 const bitset_t *bs = va_arg(args, const bitset_t *);
610 const char *prefix = "";
614 for(i = bitset_next_set(bs, 0); i != -1; i = bitset_next_set(bs, i + 1)) {
615 snprintf(buf, sizeof(buf), "%ld", i);
627 iterator_t *it = va_arg(args, iterator_t *);
628 void *collection = va_arg(args, void *);
630 const char *prefix = "";
631 char format = fmt[++i];
632 ir_printf_cb_t *cb = format == 'C' ? va_arg(args, ir_printf_cb_t *) : NULL;
634 assert(is_iterator(it) && "Pass an iterator interface and the collection");
636 snprintf(buf, sizeof(buf), "%%%c", format);
639 for(curr = it->start(collection); curr; curr = it->next(collection, curr)) {
643 cb(app, object, limit, curr);
645 ir_common_printf(app, object, limit, buf, curr);
649 it->finish(collection, curr);
654 /* clean the buffer again */
659 str = get_pnc_string(va_arg(args, int));
663 ir_node *irn = va_arg(args, ir_node *);
664 dbg_info *dbg = get_irn_dbg_info(irn);
666 if (dbg && __dbg_info_snprint) {
667 if (__dbg_info_snprint(buf, sizeof(buf), dbg) <= 0)
674 dump_with_settings(app, object, limit, &settings, str);
685 * Convenience for stdout dumping.
687 void ir_printf(const char *fmt, ...)
691 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
696 * Convenience for file dumping.
698 void ir_fprintf(FILE *f, const char *fmt, ...)
702 ir_common_vprintf(&file_appender, f, 0, fmt, args);
707 * Convenience for string dumping.
709 void ir_snprintf(char *buf, size_t len, const char *fmt, ...)
713 ir_common_vprintf(&str_appender, buf, len, fmt, args);
718 * Convenience for string dumping.
720 void ir_obst_printf(struct obstack *obst, const char *fmt, ...)
724 ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
728 void ir_vprintf(const char *fmt, va_list args)
730 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
733 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
735 ir_common_vprintf(&file_appender, f, 0, fmt, args);
738 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
740 ir_common_vprintf(&str_appender, buf, len, fmt, args);
743 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
745 ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
748 #else /* WITH_LIBCORE */
750 #include "irargs_t.h"
752 void ir_printf(const char *fmt, ...)
757 lc_evprintf(firm_get_arg_env(), fmt, args);
761 void ir_fprintf(FILE *f, const char *fmt, ...)
766 lc_evfprintf(firm_get_arg_env(), f, fmt, args);
770 void ir_snprintf(char *buf, size_t n, const char *fmt, ...)
775 lc_evsnprintf(firm_get_arg_env(), buf, n, fmt, args);
779 void ir_vprintf(const char *fmt, va_list args)
781 lc_evprintf(firm_get_arg_env(), fmt, args);
784 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
786 lc_evfprintf(firm_get_arg_env(), f, fmt, args);
789 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
791 lc_evsnprintf(firm_get_arg_env(), buf, len, fmt, args);
794 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
796 lc_evoprintf(firm_get_arg_env(), obst, fmt, args);