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 * File name: ir/ir/irprintf.c
23 * Purpose: A little printf helper unterstanding firm types
24 * Author: Sebastian Hack
27 * Copyright: (c) 1998-2006 Universität Karlsruhe
33 * A little printf helper unterstanding firm types.
34 * @author Sebastian Hack
45 #ifdef HAVE_INTTYPES_H
55 #include "firm_config.h"
67 #include "dbginfo_t.h"
69 #define STRNIL "(nil)"
74 static void str_init(void *object, size_t n)
80 * append a char to a string buffer.
82 static void str_append_char(void *object, size_t n, char ch)
89 strncat(object, buf, n);
93 * append a string to a string buffer.
95 static void str_append_str(void *object, size_t n, const char *str)
97 strncat(object, str, n);
102 * Init the file. i.e. do nothing.
104 static void file_init(void *object, size_t n)
109 * append a char to a file.
111 static void file_append_char(void *object, size_t n, char ch)
117 * append a string to a file.
119 static void file_append_str(void *object, size_t n, const char *str)
125 * Init the obstack. i.e. do nothing.
127 static void obst_init(void *object, size_t n)
132 * append a char to a obstack.
134 static void obst_append_char(void *object, size_t n, char ch)
136 struct obstack *obst = object;
137 obstack_1grow(obst, ch);
141 * append a string to a obstack.
143 static void obst_append_str(void *object, size_t n, const char *str)
145 struct obstack *obst = object;
146 obstack_grow(obst, str, strlen(str));
153 static const appender_t file_appender = {
160 * the string buffer appender
162 static const appender_t str_appender = {
169 * the obstack appender.
171 static const appender_t obst_appender = {
179 static void ir_common_vprintf(const appender_t *app, void *object,
180 size_t limit, const char *fmt, va_list args);
182 static INLINE void ir_common_printf(const appender_t *app, void *object,
183 size_t limit, const char *fmt, ...)
188 ir_common_vprintf(app, object, limit, fmt, args);
193 static int is_std_fmt(const char *fmt)
195 static const char *fmt_re_str =
196 "^[0 -+#']?[1-9]*(\\.[1-9]*)?[hlLqjzt]?[diouxXeEfFgGaAc]";
198 static regex_t fmt_re;
199 static int preapred_re = 0;
205 int res = regcomp(&fmt_re, fmt_re_str, REG_EXTENDED);
206 assert(res == 0 && "Could not prepare regex");
210 res = regexec(&fmt_re, fmt, 1, &match[0], 0);
215 regerror(res, &fmt_re, buf, sizeof(buf));
219 printf("res: %d, start: %d, end: %d\n",
220 res, match[0].rm_so, match[0].rm_eo);
223 return res == 0 ? match[0].rm_eo : -1;
235 /* Length specifiers. */
245 #define MIN(x,y) ((x) < (y) ? (x) : (y))
246 #define MAX(x,y) ((x) > (y) ? (x) : (y))
248 static void dump_with_settings(const appender_t *app, void *object, size_t limit,
249 const struct settings *settings, const char *str)
251 if (settings->width >= 0) {
253 size_t n = strlen(str);
254 int lim = MIN(settings->width, (int)limit);
255 int to_print = MIN(lim, (int)n);
256 int to_pad = to_print - lim;
258 if (!settings->flag_minus)
259 for(i = 0; i < to_pad; ++i)
260 app->append_char(object, lim, settings->flag_zero);
262 app->append_str(object, to_print, str);
264 if (!settings->flag_minus)
265 for(i = 0; i < to_pad; ++i)
266 app->append_char(object, lim, settings->flag_zero);
270 app->append_str(object, limit, str);
274 * Beware: do not set the entity ld_name
276 static const char *get_entity_ld_name_ex(entity *ent) {
278 return get_entity_ld_name(ent);
279 return get_entity_name(ent);
283 * emit a Firm object. Backported from irargs.
285 static void firm_emit(char *buf, int buflen, char conversion,
286 const struct settings *occ, void *X)
288 #define A(s) occ->flag_hash ? s " ": ""
301 strncpy(buf, "(null)", buflen);
305 snprintf(buf, buflen, "BAD");
306 snprintf(add, sizeof(add), "[%p]", X);
309 snprintf(buf, buflen, "%s%s", A("ent"),
310 isupper(conversion) ? get_entity_ld_name_ex(X): get_entity_name(X));
311 snprintf(add, sizeof(add), "[%ld]", get_entity_nr(X));
314 snprintf(buf, buflen, "%s%s:%s", A("type"), get_type_tpop_name(X), get_type_name(X));
315 snprintf(add, sizeof(add), "[%ld]", get_type_nr(X));
318 if (X == get_const_code_irg())
319 snprintf(buf, buflen, "const_code_irg ");
321 snprintf(buf, buflen, "%s%s", A("irg"), get_entity_name(get_irg_entity(X)));
322 snprintf(add, sizeof(add), "[%ld]", get_irg_graph_nr(X));
325 switch (conversion) {
327 block = is_no_Block(X) ? get_nodes_block(X) : X;
328 snprintf(buf, buflen, "%s%s%s", A("irn"), get_irn_opname(block),
329 get_mode_name(get_irn_mode(block)));
330 snprintf(add, sizeof(add), "[%ld]", get_irn_node_nr(block));
333 snprintf(buf, buflen, "%ld", get_irn_node_nr(X));
337 tarval *tv = get_Const_tarval(X);
340 tarval_snprintf(tv_buf, sizeof(tv_buf), tv);
341 snprintf(buf, buflen, "%s%s%s<%s>", A("irn"), get_irn_opname(X),
342 get_mode_name(get_irn_mode(X)), tv ? tv_buf : ">NULL<");
344 else if (get_irn_op(X) == op_SymConst) {
345 switch (get_SymConst_kind(X)) {
346 case symconst_type_tag: /* type tag */
347 snprintf(tv_buf, sizeof(tv_buf), "<ID:%s>", get_type_name(get_SymConst_type(X)));
349 case symconst_type_size: /* type size */
350 snprintf(tv_buf, sizeof(tv_buf), "<SIZE:%s>", get_type_name(get_SymConst_type(X)));
352 case symconst_type_align: /* type alignment */
353 snprintf(tv_buf, sizeof(tv_buf), "<ALIGN:%s>", get_type_name(get_SymConst_type(X)));
355 case symconst_addr_name: /* linker name */
356 snprintf(tv_buf, sizeof(tv_buf), "<EXT:%s>", get_id_str(get_SymConst_name(X)));
358 case symconst_addr_ent: /* entity name */
359 snprintf(tv_buf, sizeof(tv_buf), "<%s>", get_entity_name(get_SymConst_entity(X)));
361 case symconst_enum_const: /* enumeration constant */
362 snprintf(tv_buf, sizeof(tv_buf), "<ENUM:%s>", get_enumeration_name(get_SymConst_enum(X)));
367 snprintf(buf, buflen, "%s%s%s%s", A("irn"), get_irn_opname(X),
368 get_mode_name(get_irn_mode(X)), tv_buf);
371 snprintf(buf, buflen, "%s%s%s", A("irn"), get_irn_opname(X),
372 get_mode_name(get_irn_mode(X)));
373 snprintf(add, sizeof(add), "[%ld:%d]", get_irn_node_nr(X), get_irn_idx(X));
377 snprintf(buf, buflen, "%s%s", A("mode"), get_mode_name(X));
380 tarval_snprintf(tv_buf, sizeof(tv_buf), X);
381 snprintf(buf, buflen, "%s%s", A("tv"), tv_buf);
384 snprintf(buf, sizeof(buf), "loop[%d:%d]", get_loop_loop_nr(X), get_loop_depth(X));
387 snprintf(buf, buflen, "%s%s", A("op"), get_op_name(X));
389 case k_ir_compound_graph_path:
390 n = get_compound_graph_path_length(X);
392 for (i = 0; i < n; ++i) {
393 ent = get_compound_graph_path_node(X, i);
395 strncat(buf, ".", buflen);
396 strncat(buf, get_entity_name(ent), buflen);
397 if (is_Array_type(get_entity_owner(ent))) {
398 snprintf(add, sizeof(add), "[%d]",
399 get_compound_graph_path_array_index(X, i));
400 strncat(buf, add, buflen);
407 snprintf(buf, buflen, "UNKWN");
408 snprintf(add, sizeof(add), "[%p]", X);
413 strncat(buf, add, buflen);
419 * A small printf helper routine for ir nodes.
420 * @param app An appender (this determines where the stuff is dumped to).
421 * @param object A target passed to the appender.
422 * @param limit The maximum number of characters to dump.
423 * @param fmt The format string.
424 * @param args A va_list.
426 static void ir_common_vprintf(const appender_t *app, void *object,
427 size_t limit, const char *fmt, va_list args)
433 #define DUMP_STR(s) app->append_str(object, limit, s)
434 #define DUMP_CH(ch) app->append_char(object, limit, ch)
436 app->init(object, limit);
438 for (i = 0, n = strlen(fmt); i < n; ++i) {
443 const char *len_str = "";
445 struct settings settings;
447 settings.flag_hash = 0;
448 settings.flag_zero = ' ';
450 settings.flag_minus = 0;
451 settings.flag_plus = 0;
455 /* Clear the temporary buffer */
458 /* Set the string to print to the buffer by default. */
461 while (strchr("#0-+", ch)) {
464 settings.flag_hash = 1;
467 settings.flag_zero = '0';
470 settings.flag_minus = 1;
473 settings.flag_plus = 1;
481 /* Read the field width */
486 settings.width = (int) strtol(&fmt[i], &endptr, 10);
487 increase = (char *) endptr - &fmt[i];
488 ch = fmt[i += increase];
493 /* Ignore the precision */
495 while(isdigit(ch = fmt[++i]));
497 /* read the length modifier. */
503 if((ch = fmt[i]) == 'h') {
514 if ((ch = fmt[i]) == 'l') {
519 else if ((ch = fmt[i]) == 'u') {
530 /* Do the conversion specifier. */
533 /* The percent itself */
542 int i, n = va_arg(args, int);
543 for(i = 0; i < n && i < sizeof(buf) - 1; ++i)
551 buf[0] = va_arg(args, int);
556 str = va_arg(args, const char *);
560 snprintf(buf, sizeof(buf), "%p", va_arg(args, void *));
571 snprintf(fmt_str, sizeof(fmt_str), "%%%s%c", len_str, ch);
578 int arg = va_arg(args, int);
579 snprintf(buf, sizeof(buf), fmt_str, arg);
585 long arg = va_arg(args, long);
586 snprintf(buf, sizeof(buf), fmt_str, arg);
592 int64_t arg = va_arg(args, int64_t);
593 snprintf(buf, sizeof(buf), fmt_str, arg);
601 str = get_id_str(va_arg(args, ident *));
615 firm_emit(buf, sizeof(buf), ch, &settings, va_arg(args, void *));
620 const bitset_t *bs = va_arg(args, const bitset_t *);
621 const char *prefix = "";
625 for(i = bitset_next_set(bs, 0); i != -1; i = bitset_next_set(bs, i + 1)) {
626 snprintf(buf, sizeof(buf), "%ld", i);
638 iterator_t *it = va_arg(args, iterator_t *);
639 void *collection = va_arg(args, void *);
641 const char *prefix = "";
642 char format = fmt[++i];
643 ir_printf_cb_t *cb = format == 'C' ? va_arg(args, ir_printf_cb_t *) : NULL;
645 assert(is_iterator(it) && "Pass an iterator interface and the collection");
647 snprintf(buf, sizeof(buf), "%%%c", format);
650 for(curr = it->start(collection); curr; curr = it->next(collection, curr)) {
654 cb(app, object, limit, curr);
656 ir_common_printf(app, object, limit, buf, curr);
660 it->finish(collection, curr);
665 /* clean the buffer again */
670 str = get_pnc_string(va_arg(args, int));
674 ir_node *irn = va_arg(args, ir_node *);
675 dbg_info *dbg = get_irn_dbg_info(irn);
677 if (dbg && __dbg_info_snprint) {
678 if (__dbg_info_snprint(buf, sizeof(buf), dbg) <= 0)
685 dump_with_settings(app, object, limit, &settings, str);
696 * Convenience for stdout dumping.
698 void ir_printf(const char *fmt, ...)
702 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
707 * Convenience for file dumping.
709 void ir_fprintf(FILE *f, const char *fmt, ...)
713 ir_common_vprintf(&file_appender, f, 0, fmt, args);
718 * Convenience for string dumping.
720 void ir_snprintf(char *buf, size_t len, const char *fmt, ...)
724 ir_common_vprintf(&str_appender, buf, len, fmt, args);
729 * Convenience for string dumping.
731 void ir_obst_printf(struct obstack *obst, const char *fmt, ...)
735 ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
739 void ir_vprintf(const char *fmt, va_list args)
741 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
744 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
746 ir_common_vprintf(&file_appender, f, 0, fmt, args);
749 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
751 ir_common_vprintf(&str_appender, buf, len, fmt, args);
754 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
756 ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
759 #else /* WITH_LIBCORE */
761 #include "irargs_t.h"
763 void ir_printf(const char *fmt, ...)
768 lc_evprintf(firm_get_arg_env(), fmt, args);
772 void ir_fprintf(FILE *f, const char *fmt, ...)
777 lc_evfprintf(firm_get_arg_env(), f, fmt, args);
781 void ir_snprintf(char *buf, size_t n, const char *fmt, ...)
786 lc_evsnprintf(firm_get_arg_env(), buf, n, fmt, args);
790 void ir_vprintf(const char *fmt, va_list args)
792 lc_evprintf(firm_get_arg_env(), fmt, args);
795 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
797 lc_evfprintf(firm_get_arg_env(), f, fmt, args);
800 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
802 lc_evsnprintf(firm_get_arg_env(), buf, len, fmt, args);
805 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
807 lc_evoprintf(firm_get_arg_env(), obst, fmt, args);