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)
70 * append a char to a string buffer.
72 static void str_append_char(void *object, size_t n, char ch)
79 strncat(object, buf, n);
83 * append a string to a string buffer.
85 static void str_append_str(void *object, size_t n, const char *str)
87 strncat(object, str, n);
92 * Init the file. i.e. do nothing.
94 static void file_init(void *object, size_t n)
101 * append a char to a file.
103 static void file_append_char(void *object, size_t n, char ch)
110 * append a string to a file.
112 static void file_append_str(void *object, size_t n, const char *str)
119 * Init the obstack. i.e. do nothing.
121 static void obst_init(void *object, size_t n)
128 * append a char to a obstack.
130 static void obst_append_char(void *object, size_t n, char ch)
132 struct obstack *obst = object;
134 obstack_1grow(obst, ch);
138 * append a string to a obstack.
140 static void obst_append_str(void *object, size_t n, const char *str)
142 struct obstack *obst = object;
144 obstack_grow(obst, str, strlen(str));
151 static const appender_t file_appender = {
158 * the string buffer appender
160 static const appender_t str_appender = {
167 * the obstack appender.
169 static const appender_t obst_appender = {
177 static void ir_common_vprintf(const appender_t *app, void *object,
178 size_t limit, const char *fmt, va_list args);
180 static INLINE void ir_common_printf(const appender_t *app, void *object,
181 size_t limit, const char *fmt, ...)
186 ir_common_vprintf(app, object, limit, fmt, args);
191 static int is_std_fmt(const char *fmt)
193 static const char *fmt_re_str =
194 "^[0 -+#']?[1-9]*(\\.[1-9]*)?[hlLqjzt]?[diouxXeEfFgGaAc]";
196 static regex_t fmt_re;
197 static int preapred_re = 0;
203 int res = regcomp(&fmt_re, fmt_re_str, REG_EXTENDED);
204 assert(res == 0 && "Could not prepare regex");
208 res = regexec(&fmt_re, fmt, 1, &match[0], 0);
213 regerror(res, &fmt_re, buf, sizeof(buf));
217 printf("res: %d, start: %d, end: %d\n",
218 res, match[0].rm_so, match[0].rm_eo);
221 return res == 0 ? match[0].rm_eo : -1;
233 /* Length specifiers. */
243 #define MIN(x,y) ((x) < (y) ? (x) : (y))
244 #define MAX(x,y) ((x) > (y) ? (x) : (y))
246 static void dump_with_settings(const appender_t *app, void *object, size_t limit,
247 const struct settings *settings, const char *str)
249 if (settings->width >= 0) {
251 size_t n = strlen(str);
252 int lim = MIN(settings->width, (int)limit);
253 int to_print = MIN(lim, (int)n);
254 int to_pad = to_print - lim;
256 if (!settings->flag_minus)
257 for(i = 0; i < to_pad; ++i)
258 app->append_char(object, lim, settings->flag_zero);
260 app->append_str(object, to_print, str);
262 if (!settings->flag_minus)
263 for(i = 0; i < to_pad; ++i)
264 app->append_char(object, lim, settings->flag_zero);
268 app->append_str(object, limit, str);
272 * Beware: do not set the entity ld_name
274 static const char *get_entity_ld_name_ex(ir_entity *ent) {
276 return get_entity_ld_name(ent);
277 return get_entity_name(ent);
281 * emit a Firm object. Backported from irargs.
283 static void firm_emit(char *buf, int buflen, char conversion,
284 const struct settings *occ, void *X)
286 #define A(s) occ->flag_hash ? s " ": ""
299 strncpy(buf, "(null)", buflen);
303 snprintf(buf, buflen, "BAD");
304 snprintf(add, sizeof(add), "[%p]", X);
307 snprintf(buf, buflen, "%s%s", A("ent"),
308 isupper(conversion) ? get_entity_ld_name_ex(X): get_entity_name(X));
309 snprintf(add, sizeof(add), "[%ld]", get_entity_nr(X));
312 snprintf(buf, buflen, "%s%s:%s", A("type"), get_type_tpop_name(X), get_type_name(X));
313 snprintf(add, sizeof(add), "[%ld]", get_type_nr(X));
316 if (X == get_const_code_irg())
317 snprintf(buf, buflen, "const_code_irg ");
319 snprintf(buf, buflen, "%s%s", A("irg"), get_entity_name(get_irg_entity(X)));
320 snprintf(add, sizeof(add), "[%ld]", get_irg_graph_nr(X));
323 switch (conversion) {
325 block = is_no_Block(X) ? get_nodes_block(X) : X;
326 snprintf(buf, buflen, "%s%s%s", A("irn"), get_irn_opname(block),
327 get_mode_name(get_irn_mode(block)));
328 snprintf(add, sizeof(add), "[%ld]", get_irn_node_nr(block));
331 snprintf(buf, buflen, "%ld", get_irn_node_nr(X));
335 tarval *tv = get_Const_tarval(X);
338 tarval_snprintf(tv_buf, sizeof(tv_buf), tv);
339 snprintf(buf, buflen, "%s%s%s<%s>", A("irn"), get_irn_opname(X),
340 get_mode_name(get_irn_mode(X)), tv ? tv_buf : ">NULL<");
342 else if (get_irn_op(X) == op_SymConst) {
343 switch (get_SymConst_kind(X)) {
344 case symconst_type_tag: /* type tag */
345 snprintf(tv_buf, sizeof(tv_buf), "<ID:%s>", get_type_name(get_SymConst_type(X)));
347 case symconst_type_size: /* type size */
348 snprintf(tv_buf, sizeof(tv_buf), "<SIZE:%s>", get_type_name(get_SymConst_type(X)));
350 case symconst_type_align: /* type alignment */
351 snprintf(tv_buf, sizeof(tv_buf), "<ALIGN:%s>", get_type_name(get_SymConst_type(X)));
353 case symconst_addr_name: /* linker name */
354 snprintf(tv_buf, sizeof(tv_buf), "<EXT:%s>", get_id_str(get_SymConst_name(X)));
356 case symconst_addr_ent: /* entity name */
357 snprintf(tv_buf, sizeof(tv_buf), "<%s>", get_entity_name(get_SymConst_entity(X)));
359 case symconst_enum_const: /* enumeration constant */
360 snprintf(tv_buf, sizeof(tv_buf), "<ENUM:%s>", get_enumeration_name(get_SymConst_enum(X)));
362 case symconst_label: /* label */
363 snprintf(tv_buf, sizeof(tv_buf), "<LABEL:%lu>", get_SymConst_label(X));
368 snprintf(buf, buflen, "%s%s%s%s", A("irn"), get_irn_opname(X),
369 get_mode_name(get_irn_mode(X)), tv_buf);
372 snprintf(buf, buflen, "%s%s%s", A("irn"), get_irn_opname(X),
373 get_mode_name(get_irn_mode(X)));
374 snprintf(add, sizeof(add), "[%ld:%d]", get_irn_node_nr(X), get_irn_idx(X));
378 snprintf(buf, buflen, "%s%s", A("mode"), get_mode_name(X));
381 tarval_snprintf(tv_buf, sizeof(tv_buf), X);
382 snprintf(buf, buflen, "%s%s", A("tv"), tv_buf);
385 snprintf(buf, sizeof(buf), "loop[%d:%d]", get_loop_loop_nr(X), get_loop_depth(X));
388 snprintf(buf, buflen, "%s%s", A("op"), get_op_name(X));
390 case k_ir_compound_graph_path:
391 n = get_compound_graph_path_length(X);
393 for (i = 0; i < n; ++i) {
394 ent = get_compound_graph_path_node(X, i);
396 strncat(buf, ".", buflen);
397 strncat(buf, get_entity_name(ent), buflen);
398 if (is_Array_type(get_entity_owner(ent))) {
399 snprintf(add, sizeof(add), "[%d]",
400 get_compound_graph_path_array_index(X, i));
401 strncat(buf, add, buflen);
408 snprintf(buf, buflen, "UNKWN");
409 snprintf(add, sizeof(add), "[%p]", X);
414 strncat(buf, add, buflen);
420 * A small printf helper routine for ir nodes.
421 * @param app An appender (this determines where the stuff is dumped to).
422 * @param object A target passed to the appender.
423 * @param limit The maximum number of characters to dump.
424 * @param fmt The format string.
425 * @param args A va_list.
427 static void ir_common_vprintf(const appender_t *app, void *object,
428 size_t limit, const char *fmt, va_list args)
434 #define DUMP_STR(s) app->append_str(object, limit, s)
435 #define DUMP_CH(ch) app->append_char(object, limit, ch)
437 app->init(object, limit);
439 for (i = 0, n = strlen(fmt); i < n; ++i) {
444 const char *len_str = "";
446 struct settings settings;
448 settings.flag_hash = 0;
449 settings.flag_zero = ' ';
451 settings.flag_minus = 0;
452 settings.flag_plus = 0;
456 /* Clear the temporary buffer */
459 /* Set the string to print to the buffer by default. */
462 while (strchr("#0-+", ch)) {
465 settings.flag_hash = 1;
468 settings.flag_zero = '0';
471 settings.flag_minus = 1;
474 settings.flag_plus = 1;
482 /* Read the field width */
487 settings.width = (int) strtol(&fmt[i], &endptr, 10);
488 increase = (char *) endptr - &fmt[i];
489 ch = fmt[i += increase];
494 /* Ignore the precision */
496 while(isdigit(ch = fmt[++i]));
498 /* read the length modifier. */
504 if((ch = fmt[i]) == 'h') {
515 if ((ch = fmt[i]) == 'l') {
520 else if ((ch = fmt[i]) == 'u') {
531 /* Do the conversion specifier. */
534 /* The percent itself */
543 int i, n = va_arg(args, int);
544 for(i = 0; i < n && i < sizeof(buf) - 1; ++i)
552 buf[0] = va_arg(args, int);
557 str = va_arg(args, const char *);
561 snprintf(buf, sizeof(buf), "%p", va_arg(args, void *));
572 snprintf(fmt_str, sizeof(fmt_str), "%%%s%c", len_str, ch);
579 int arg = va_arg(args, int);
580 snprintf(buf, sizeof(buf), fmt_str, arg);
586 long arg = va_arg(args, long);
587 snprintf(buf, sizeof(buf), fmt_str, arg);
593 int64_t arg = va_arg(args, int64_t);
594 snprintf(buf, sizeof(buf), fmt_str, arg);
602 str = get_id_str(va_arg(args, ident *));
616 firm_emit(buf, sizeof(buf), ch, &settings, va_arg(args, void *));
621 const bitset_t *bs = va_arg(args, const bitset_t *);
622 const char *prefix = "";
626 for(i = bitset_next_set(bs, 0); i != -1; i = bitset_next_set(bs, i + 1)) {
627 snprintf(buf, sizeof(buf), "%ld", i);
639 iterator_t *it = va_arg(args, iterator_t *);
640 void *collection = va_arg(args, void *);
642 const char *prefix = "";
643 char format = fmt[++i];
644 ir_printf_cb_t *cb = format == 'C' ? va_arg(args, ir_printf_cb_t *) : NULL;
646 assert(is_iterator(it) && "Pass an iterator interface and the collection");
648 snprintf(buf, sizeof(buf), "%%%c", format);
651 for(curr = it->start(collection); curr; curr = it->next(collection, curr)) {
655 cb(app, object, limit, curr);
657 ir_common_printf(app, object, limit, buf, curr);
661 it->finish(collection, curr);
666 /* clean the buffer again */
671 str = get_pnc_string(va_arg(args, int));
675 ir_node *irn = va_arg(args, ir_node *);
676 dbg_info *dbg = get_irn_dbg_info(irn);
678 if (dbg && __dbg_info_snprint) {
679 if (__dbg_info_snprint(buf, sizeof(buf), dbg) <= 0)
686 dump_with_settings(app, object, limit, &settings, str);
697 * Convenience for stdout dumping.
699 void ir_printf(const char *fmt, ...)
703 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
708 * Convenience for file dumping.
710 void ir_fprintf(FILE *f, const char *fmt, ...)
714 ir_common_vprintf(&file_appender, f, 0, fmt, args);
719 * Convenience for string dumping.
721 void ir_snprintf(char *buf, size_t len, const char *fmt, ...)
725 ir_common_vprintf(&str_appender, buf, len, fmt, args);
730 * Convenience for string dumping.
732 void ir_obst_printf(struct obstack *obst, const char *fmt, ...)
736 ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
740 void ir_vprintf(const char *fmt, va_list args)
742 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
745 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
747 ir_common_vprintf(&file_appender, f, 0, fmt, args);
750 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
752 ir_common_vprintf(&str_appender, buf, len, fmt, args);
755 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
757 ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
760 #else /* WITH_LIBCORE */
762 #include "irargs_t.h"
764 void ir_printf(const char *fmt, ...)
769 lc_evprintf(firm_get_arg_env(), fmt, args);
773 void ir_fprintf(FILE *f, const char *fmt, ...)
778 lc_evfprintf(firm_get_arg_env(), f, fmt, args);
782 void ir_snprintf(char *buf, size_t n, const char *fmt, ...)
787 lc_evsnprintf(firm_get_arg_env(), buf, n, fmt, args);
791 void ir_vprintf(const char *fmt, va_list args)
793 lc_evprintf(firm_get_arg_env(), fmt, args);
796 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
798 lc_evfprintf(firm_get_arg_env(), f, fmt, args);
801 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
803 lc_evsnprintf(firm_get_arg_env(), buf, len, fmt, args);
806 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
808 lc_evoprintf(firm_get_arg_env(), obst, fmt, args);