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
49 #define STRNIL "(nil)"
54 static void str_init(void *object, size_t n)
60 * append a char to a string buffer.
62 static void str_append_char(void *object, size_t n, char ch)
69 strncat(object, buf, n);
73 * append a string to a string buffer.
75 static void str_append_str(void *object, size_t n, const char *str)
77 strncat(object, str, n);
82 * Init the file. i.e. do nothing.
84 static void file_init(void *object, size_t n)
89 * append a char to a file.
91 static void file_append_char(void *object, size_t n, char ch)
97 * append a string to a file.
99 static void file_append_str(void *object, size_t n, const char *str)
105 * Init the obstack. i.e. do nothing.
107 static void obst_init(void *object, size_t n)
112 * append a char to a obstack.
114 static void obst_append_char(void *object, size_t n, char ch)
116 struct obstack *obst = object;
117 obstack_1grow(obst, ch);
121 * append a string to a obstack.
123 static void obst_append_str(void *object, size_t n, const char *str)
125 struct obstack *obst = object;
126 obstack_grow(obst, str, strlen(str));
133 static const appender_t file_appender = {
140 * the string buffer appender
142 static const appender_t str_appender = {
149 * the obstack appender.
151 static const appender_t obst_appender = {
157 static void ir_common_vprintf(const appender_t *app, void *object,
158 size_t limit, const char *fmt, va_list args);
160 static INLINE void ir_common_printf(const appender_t *app, void *object,
161 size_t limit, const char *fmt, ...)
166 ir_common_vprintf(app, object, limit, fmt, args);
171 static int is_std_fmt(const char *fmt)
173 static const char *fmt_re_str =
174 "^[0 -+#']?[1-9]*(\\.[1-9]*)?[hlLqjzt]?[diouxXeEfFgGaAc]";
176 static regex_t fmt_re;
177 static int preapred_re = 0;
183 int res = regcomp(&fmt_re, fmt_re_str, REG_EXTENDED);
184 assert(res == 0 && "Could not prepare regex");
188 res = regexec(&fmt_re, fmt, 1, &match[0], 0);
193 regerror(res, &fmt_re, buf, sizeof(buf));
197 printf("res: %d, start: %d, end: %d\n",
198 res, match[0].rm_so, match[0].rm_eo);
201 return res == 0 ? match[0].rm_eo : -1;
213 /* Length specifiers. */
223 #define MIN(x,y) ((x) < (y) ? (x) : (y))
224 #define MAX(x,y) ((x) > (y) ? (x) : (y))
226 static void dump_with_settings(const appender_t *app, void *object, size_t limit,
227 const struct settings *settings, const char *str)
229 if(settings->width >= 0) {
231 size_t n = strlen(str);
232 int lim = MIN(settings->width, (int)limit);
233 int to_print = MIN(lim, (int)n);
234 int to_pad = to_print - lim;
236 if(!settings->left_just)
237 for(i = 0; i < to_pad; ++i)
238 app->append_char(object, lim, settings->pad);
240 app->append_str(object, to_print, str);
242 if(!settings->left_just)
243 for(i = 0; i < to_pad; ++i)
244 app->append_char(object, lim, settings->pad);
248 app->append_str(object, limit, str);
253 * A small printf helper routine for ir nodes.
254 * @param app An appender (this determines where the stuff is dumped
256 * @param object A target passed to the appender.
257 * @param limit The maximum number of characters to dump.
258 * @param fmt The format string.
259 * @param args A va_list.
261 static void ir_common_vprintf(const appender_t *app, void *object,
262 size_t limit, const char *fmt, va_list args)
268 #define DUMP_STR(s) app->append_str(object, limit, s)
269 #define DUMP_CH(ch) app->append_char(object, limit, ch)
271 app->init(object, limit);
273 for(i = 0, n = strlen(fmt); i < n; ++i) {
278 const char *len_str = "";
280 struct settings settings;
282 settings.alternate = 0;
285 settings.left_just = 0;
286 settings.put_plus = 0;
290 /* Clear the temporary buffer */
293 /* Set the string to print to the buffer by default. */
296 while(strchr("#0-+", ch)) {
299 settings.alternate = 1;
305 settings.left_just = 1;
308 settings.put_plus = 1;
316 /* Read the field width */
321 settings.width = (int) strtol(&fmt[i], &endptr, 10);
322 increase = (char *) endptr - &fmt[i];
323 ch = fmt[i += increase];
328 /* Ignore the precision */
330 while(isdigit(ch = fmt[++i]));
332 /* read the length modifier. */
337 if((ch = fmt[++i]) == 'h') {
346 if((ch = fmt[++i]) == 'l') {
356 /* Do the conversion specifier. */
359 /* The percent itself */
368 int i, n = va_arg(args, int);
369 for(i = 0; i < n && i < sizeof(buf) - 1; ++i)
377 buf[0] = va_arg(args, int);
382 str = va_arg(args, const char *);
386 snprintf(buf, sizeof(buf), "%p", va_arg(args, void *));
395 snprintf(fmt_str, sizeof(fmt_str), "%%%s%c", len_str, ch);
402 int arg = va_arg(args, int);
403 snprintf(buf, sizeof(buf), fmt_str, arg);
409 long arg = va_arg(args, long);
410 snprintf(buf, sizeof(buf), fmt_str, arg);
416 int64_t arg = va_arg(args, int64_t);
417 snprintf(buf, sizeof(buf), fmt_str, arg);
425 str = get_id_str(va_arg(args, ident *));
429 str = get_type_name(va_arg(args, type *));
433 str = get_entity_name(va_arg(args, entity *));
437 str = get_entity_ld_name(va_arg(args, entity *));
441 tarval_snprintf(buf, sizeof(buf), va_arg(args, tarval *));
446 ir_node *irn = va_arg(args, ir_node *);
448 snprintf(buf, sizeof(buf), "%s%s:%ld",
449 get_irn_opname(irn), get_mode_name(get_irn_mode(irn)), get_irn_node_nr(irn));
451 strncpy(buf, STRNIL, sizeof(buf));
456 str = get_irn_opname(va_arg(args, ir_node *));
460 snprintf(buf, sizeof(buf), "%ld", get_irn_node_nr(va_arg(args, ir_node *)));
464 str = get_mode_name(va_arg(args, ir_mode *));
468 snprintf(buf, sizeof(buf), "%ld",
469 get_irn_node_nr(get_nodes_block(va_arg(args, ir_node *))));
474 const bitset_t *bs = va_arg(args, const bitset_t *);
475 const char *prefix = "";
479 for(i = bitset_next_set(bs, 0); i != -1; i = bitset_next_set(bs, i + 1)) {
480 snprintf(buf, sizeof(buf), "%ld", i);
492 iterator_t *it = va_arg(args, iterator_t *);
493 void *collection = va_arg(args, void *);
495 const char *prefix = "";
496 char format = fmt[++i];
497 ir_printf_cb_t *cb = format == 'C' ? va_arg(args, ir_printf_cb_t *) : NULL;
499 assert(is_iterator(it) && "Pass an iterator interface and the collection");
501 snprintf(buf, sizeof(buf), "%%%c", format);
504 for(curr = it->start(collection); curr; curr = it->next(collection, curr)) {
508 cb(app, object, limit, curr);
510 ir_common_printf(app, object, limit, buf, curr);
514 it->finish(collection, curr);
519 /* clean the buffer again */
524 dump_with_settings(app, object, limit, &settings, str);
536 * Convencience for stdout dumping.
538 void ir_printf(const char *fmt, ...)
542 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
547 * Convencience for file dumping.
549 void ir_fprintf(FILE *f, const char *fmt, ...)
553 ir_common_vprintf(&file_appender, f, 0, fmt, args);
558 * Convencience for string dumping.
560 void ir_snprintf(char *buf, size_t len, const char *fmt, ...)
564 ir_common_vprintf(&str_appender, buf, len, fmt, args);
569 * Convencience for string dumping.
571 void ir_obst_printf(struct obstack *obst, const char *fmt, ...)
575 ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
579 void ir_vprintf(const char *fmt, va_list args)
581 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
584 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
586 ir_common_vprintf(&file_appender, f, 0, fmt, args);
589 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
591 ir_common_vprintf(&str_appender, buf, len, fmt, args);
594 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
596 ir_common_vprintf(&obst_appender, obst, 0, fmt, args);