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->left_just)
241 for(i = 0; i < to_pad; ++i)
242 app->append_char(object, lim, settings->pad);
244 app->append_str(object, to_print, str);
246 if(!settings->left_just)
247 for(i = 0; i < to_pad; ++i)
248 app->append_char(object, lim, settings->pad);
252 app->append_str(object, limit, str);
257 * A small printf helper routine for ir nodes.
258 * @param app An appender (this determines where the stuff is dumped
260 * @param object A target passed to the appender.
261 * @param limit The maximum number of characters to dump.
262 * @param fmt The format string.
263 * @param args A va_list.
265 static void ir_common_vprintf(const appender_t *app, void *object,
266 size_t limit, const char *fmt, va_list args)
272 #define DUMP_STR(s) app->append_str(object, limit, s)
273 #define DUMP_CH(ch) app->append_char(object, limit, ch)
275 app->init(object, limit);
277 for(i = 0, n = strlen(fmt); i < n; ++i) {
282 const char *len_str = "";
284 struct settings settings;
286 settings.alternate = 0;
289 settings.left_just = 0;
290 settings.put_plus = 0;
294 /* Clear the temporary buffer */
297 /* Set the string to print to the buffer by default. */
300 while(strchr("#0-+", ch)) {
303 settings.alternate = 1;
309 settings.left_just = 1;
312 settings.put_plus = 1;
320 /* Read the field width */
325 settings.width = (int) strtol(&fmt[i], &endptr, 10);
326 increase = (char *) endptr - &fmt[i];
327 ch = fmt[i += increase];
332 /* Ignore the precision */
334 while(isdigit(ch = fmt[++i]));
336 /* read the length modifier. */
341 if((ch = fmt[++i]) == 'h') {
350 if((ch = fmt[++i]) == 'l') {
360 /* Do the conversion specifier. */
363 /* The percent itself */
372 int i, n = va_arg(args, int);
373 for(i = 0; i < n && i < sizeof(buf) - 1; ++i)
381 buf[0] = va_arg(args, int);
386 str = va_arg(args, const char *);
390 snprintf(buf, sizeof(buf), "%p", va_arg(args, void *));
401 snprintf(fmt_str, sizeof(fmt_str), "%%%s%c", len_str, ch);
408 int arg = va_arg(args, int);
409 snprintf(buf, sizeof(buf), fmt_str, arg);
415 long arg = va_arg(args, long);
416 snprintf(buf, sizeof(buf), fmt_str, arg);
422 int64_t arg = va_arg(args, int64_t);
423 snprintf(buf, sizeof(buf), fmt_str, arg);
431 str = get_id_str(va_arg(args, ident *));
435 str = get_type_name(va_arg(args, type *));
439 str = get_entity_name(va_arg(args, entity *));
443 str = get_entity_ld_name(va_arg(args, entity *));
447 tarval_snprintf(buf, sizeof(buf), va_arg(args, tarval *));
452 ir_node *irn = va_arg(args, ir_node *);
456 tarval_snprintf(tbuf, sizeof(tbuf), get_Const_tarval(irn));
457 snprintf(buf, sizeof(buf), "%s%s<%s>:%ld",
458 get_irn_opname(irn), get_mode_name(get_irn_mode(irn)), tbuf, get_irn_node_nr(irn));
461 snprintf(buf, sizeof(buf), "%s%s:%ld",
462 get_irn_opname(irn), get_mode_name(get_irn_mode(irn)), get_irn_node_nr(irn));
465 strncpy(buf, STRNIL, sizeof(buf));
470 str = get_irn_opname(va_arg(args, ir_node *));
474 snprintf(buf, sizeof(buf), "%ld", get_irn_node_nr(va_arg(args, ir_node *)));
478 str = get_mode_name(va_arg(args, ir_mode *));
482 snprintf(buf, sizeof(buf), "%ld",
483 get_irn_node_nr(get_nodes_block(va_arg(args, ir_node *))));
488 const bitset_t *bs = va_arg(args, const bitset_t *);
489 const char *prefix = "";
493 for(i = bitset_next_set(bs, 0); i != -1; i = bitset_next_set(bs, i + 1)) {
494 snprintf(buf, sizeof(buf), "%ld", i);
506 iterator_t *it = va_arg(args, iterator_t *);
507 void *collection = va_arg(args, void *);
509 const char *prefix = "";
510 char format = fmt[++i];
511 ir_printf_cb_t *cb = format == 'C' ? va_arg(args, ir_printf_cb_t *) : NULL;
513 assert(is_iterator(it) && "Pass an iterator interface and the collection");
515 snprintf(buf, sizeof(buf), "%%%c", format);
518 for(curr = it->start(collection); curr; curr = it->next(collection, curr)) {
522 cb(app, object, limit, curr);
524 ir_common_printf(app, object, limit, buf, curr);
528 it->finish(collection, curr);
533 /* clean the buffer again */
538 str = get_pnc_string(va_arg(args, int));
542 ir_node *irn = va_arg(args, ir_node *);
543 dbg_info *dbg = get_irn_dbg_info(irn);
545 if (dbg && __dbg_info_snprint) {
546 if (__dbg_info_snprint(buf, sizeof(buf), dbg) <= 0)
553 compound_graph_path *path = va_arg(args, compound_graph_path *);
554 int i, l = get_compound_graph_path_length(path);
557 for (i = 0; i < l; ++i) {
558 ent = get_compound_graph_path_node(path, i);
561 DUMP_STR(get_entity_name(ent));
562 if (is_Array_type(get_entity_owner(ent))) {
563 snprintf(buf, sizeof(buf), "[%d]",
564 get_compound_graph_path_array_index(path, i));
569 /* clean the buffer again */
575 dump_with_settings(app, object, limit, &settings, str);
586 * Convenience for stdout dumping.
588 void ir_printf(const char *fmt, ...)
592 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
597 * Convenience for file dumping.
599 void ir_fprintf(FILE *f, const char *fmt, ...)
603 ir_common_vprintf(&file_appender, f, 0, fmt, args);
608 * Convenience for string dumping.
610 void ir_snprintf(char *buf, size_t len, const char *fmt, ...)
614 ir_common_vprintf(&str_appender, buf, len, fmt, args);
619 * Convenience for string dumping.
621 void ir_obst_printf(struct obstack *obst, const char *fmt, ...)
625 ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
629 void ir_vprintf(const char *fmt, va_list args)
631 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
634 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
636 ir_common_vprintf(&file_appender, f, 0, fmt, args);
639 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
641 ir_common_vprintf(&str_appender, buf, len, fmt, args);
644 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
646 ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
649 #else /* WITH_LIBCORE */
651 #include "irargs_t.h"
653 void ir_printf(const char *fmt, ...)
658 lc_evprintf(firm_get_arg_env(), fmt, args);
662 void ir_fprintf(FILE *f, const char *fmt, ...)
667 lc_evfprintf(firm_get_arg_env(), f, fmt, args);
671 void ir_snprintf(char *buf, size_t n, const char *fmt, ...)
676 lc_evsnprintf(firm_get_arg_env(), buf, n, fmt, args);
680 void ir_vprintf(const char *fmt, va_list args)
682 lc_evprintf(firm_get_arg_env(), fmt, args);
685 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
687 lc_evfprintf(firm_get_arg_env(), f, fmt, args);
690 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
692 lc_evsnprintf(firm_get_arg_env(), buf, len, fmt, args);
695 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
697 lc_evoprintf(firm_get_arg_env(), obst, fmt, args);