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"
50 #define STRNIL "(nil)"
55 static void str_init(void *object, size_t n)
61 * append a char to a string buffer.
63 static void str_append_char(void *object, size_t n, char ch)
70 strncat(object, buf, n);
74 * append a string to a string buffer.
76 static void str_append_str(void *object, size_t n, const char *str)
78 strncat(object, str, n);
83 * Init the file. i.e. do nothing.
85 static void file_init(void *object, size_t n)
90 * append a char to a file.
92 static void file_append_char(void *object, size_t n, char ch)
98 * append a string to a file.
100 static void file_append_str(void *object, size_t n, const char *str)
106 * Init the obstack. i.e. do nothing.
108 static void obst_init(void *object, size_t n)
113 * append a char to a obstack.
115 static void obst_append_char(void *object, size_t n, char ch)
117 struct obstack *obst = object;
118 obstack_1grow(obst, ch);
122 * append a string to a obstack.
124 static void obst_append_str(void *object, size_t n, const char *str)
126 struct obstack *obst = object;
127 obstack_grow(obst, str, strlen(str));
134 static const appender_t file_appender = {
141 * the string buffer appender
143 static const appender_t str_appender = {
150 * the obstack appender.
152 static const appender_t obst_appender = {
160 static void ir_common_vprintf(const appender_t *app, void *object,
161 size_t limit, const char *fmt, va_list args);
163 static INLINE void ir_common_printf(const appender_t *app, void *object,
164 size_t limit, const char *fmt, ...)
169 ir_common_vprintf(app, object, limit, fmt, args);
174 static int is_std_fmt(const char *fmt)
176 static const char *fmt_re_str =
177 "^[0 -+#']?[1-9]*(\\.[1-9]*)?[hlLqjzt]?[diouxXeEfFgGaAc]";
179 static regex_t fmt_re;
180 static int preapred_re = 0;
186 int res = regcomp(&fmt_re, fmt_re_str, REG_EXTENDED);
187 assert(res == 0 && "Could not prepare regex");
191 res = regexec(&fmt_re, fmt, 1, &match[0], 0);
196 regerror(res, &fmt_re, buf, sizeof(buf));
200 printf("res: %d, start: %d, end: %d\n",
201 res, match[0].rm_so, match[0].rm_eo);
204 return res == 0 ? match[0].rm_eo : -1;
216 /* Length specifiers. */
226 #define MIN(x,y) ((x) < (y) ? (x) : (y))
227 #define MAX(x,y) ((x) > (y) ? (x) : (y))
229 static void dump_with_settings(const appender_t *app, void *object, size_t limit,
230 const struct settings *settings, const char *str)
232 if(settings->width >= 0) {
234 size_t n = strlen(str);
235 int lim = MIN(settings->width, (int)limit);
236 int to_print = MIN(lim, (int)n);
237 int to_pad = to_print - lim;
239 if(!settings->left_just)
240 for(i = 0; i < to_pad; ++i)
241 app->append_char(object, lim, settings->pad);
243 app->append_str(object, to_print, str);
245 if(!settings->left_just)
246 for(i = 0; i < to_pad; ++i)
247 app->append_char(object, lim, settings->pad);
251 app->append_str(object, limit, str);
256 * A small printf helper routine for ir nodes.
257 * @param app An appender (this determines where the stuff is dumped
259 * @param object A target passed to the appender.
260 * @param limit The maximum number of characters to dump.
261 * @param fmt The format string.
262 * @param args A va_list.
264 static void ir_common_vprintf(const appender_t *app, void *object,
265 size_t limit, const char *fmt, va_list args)
271 #define DUMP_STR(s) app->append_str(object, limit, s)
272 #define DUMP_CH(ch) app->append_char(object, limit, ch)
274 app->init(object, limit);
276 for(i = 0, n = strlen(fmt); i < n; ++i) {
281 const char *len_str = "";
283 struct settings settings;
285 settings.alternate = 0;
288 settings.left_just = 0;
289 settings.put_plus = 0;
293 /* Clear the temporary buffer */
296 /* Set the string to print to the buffer by default. */
299 while(strchr("#0-+", ch)) {
302 settings.alternate = 1;
308 settings.left_just = 1;
311 settings.put_plus = 1;
319 /* Read the field width */
324 settings.width = (int) strtol(&fmt[i], &endptr, 10);
325 increase = (char *) endptr - &fmt[i];
326 ch = fmt[i += increase];
331 /* Ignore the precision */
333 while(isdigit(ch = fmt[++i]));
335 /* read the length modifier. */
340 if((ch = fmt[++i]) == 'h') {
349 if((ch = fmt[++i]) == 'l') {
359 /* Do the conversion specifier. */
362 /* The percent itself */
371 int i, n = va_arg(args, int);
372 for(i = 0; i < n && i < sizeof(buf) - 1; ++i)
380 buf[0] = va_arg(args, int);
385 str = va_arg(args, const char *);
389 snprintf(buf, sizeof(buf), "%p", va_arg(args, void *));
400 snprintf(fmt_str, sizeof(fmt_str), "%%%s%c", len_str, ch);
407 int arg = va_arg(args, int);
408 snprintf(buf, sizeof(buf), fmt_str, arg);
414 long arg = va_arg(args, long);
415 snprintf(buf, sizeof(buf), fmt_str, arg);
421 int64_t arg = va_arg(args, int64_t);
422 snprintf(buf, sizeof(buf), fmt_str, arg);
430 str = get_id_str(va_arg(args, ident *));
434 str = get_type_name(va_arg(args, type *));
438 str = get_entity_name(va_arg(args, entity *));
442 str = get_entity_ld_name(va_arg(args, entity *));
446 tarval_snprintf(buf, sizeof(buf), va_arg(args, tarval *));
451 ir_node *irn = va_arg(args, ir_node *);
455 tarval_snprintf(tbuf, sizeof(tbuf), get_Const_tarval(irn));
456 snprintf(buf, sizeof(buf), "%s%s<%s>:%ld",
457 get_irn_opname(irn), get_mode_name(get_irn_mode(irn)), tbuf, get_irn_node_nr(irn));
460 snprintf(buf, sizeof(buf), "%s%s:%ld",
461 get_irn_opname(irn), get_mode_name(get_irn_mode(irn)), get_irn_node_nr(irn));
463 strncpy(buf, STRNIL, sizeof(buf));
468 str = get_irn_opname(va_arg(args, ir_node *));
472 snprintf(buf, sizeof(buf), "%ld", get_irn_node_nr(va_arg(args, ir_node *)));
476 str = get_mode_name(va_arg(args, ir_mode *));
480 snprintf(buf, sizeof(buf), "%ld",
481 get_irn_node_nr(get_nodes_block(va_arg(args, ir_node *))));
486 const bitset_t *bs = va_arg(args, const bitset_t *);
487 const char *prefix = "";
491 for(i = bitset_next_set(bs, 0); i != -1; i = bitset_next_set(bs, i + 1)) {
492 snprintf(buf, sizeof(buf), "%ld", i);
504 iterator_t *it = va_arg(args, iterator_t *);
505 void *collection = va_arg(args, void *);
507 const char *prefix = "";
508 char format = fmt[++i];
509 ir_printf_cb_t *cb = format == 'C' ? va_arg(args, ir_printf_cb_t *) : NULL;
511 assert(is_iterator(it) && "Pass an iterator interface and the collection");
513 snprintf(buf, sizeof(buf), "%%%c", format);
516 for(curr = it->start(collection); curr; curr = it->next(collection, curr)) {
520 cb(app, object, limit, curr);
522 ir_common_printf(app, object, limit, buf, curr);
526 it->finish(collection, curr);
531 /* clean the buffer again */
536 str = get_pnc_string(va_arg(args, int));
540 dump_with_settings(app, object, limit, &settings, str);
552 * Convenience for stdout dumping.
554 void ir_printf(const char *fmt, ...)
558 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
563 * Convenience for file dumping.
565 void ir_fprintf(FILE *f, const char *fmt, ...)
569 ir_common_vprintf(&file_appender, f, 0, fmt, args);
574 * Convenience for string dumping.
576 void ir_snprintf(char *buf, size_t len, const char *fmt, ...)
580 ir_common_vprintf(&str_appender, buf, len, fmt, args);
585 * Convenience for string dumping.
587 void ir_obst_printf(struct obstack *obst, const char *fmt, ...)
591 ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
595 void ir_vprintf(const char *fmt, va_list args)
597 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
600 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
602 ir_common_vprintf(&file_appender, f, 0, fmt, args);
605 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
607 ir_common_vprintf(&str_appender, buf, len, fmt, args);
610 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
612 ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
615 #else /* WITH_LIBCORE */
617 #include "irargs_t.h"
619 void ir_printf(const char *fmt, ...)
624 lc_evprintf(firm_get_arg_env(), fmt, args);
628 void ir_fprintf(FILE *f, const char *fmt, ...)
633 lc_evfprintf(firm_get_arg_env(), f, fmt, args);
637 void ir_snprintf(char *buf, size_t n, const char *fmt, ...)
642 lc_evsnprintf(firm_get_arg_env(), buf, n, fmt, args);
646 void ir_vprintf(const char *fmt, va_list args)
648 lc_evprintf(firm_get_arg_env(), fmt, args);
651 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
653 lc_evfprintf(firm_get_arg_env(), f, fmt, args);
656 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
658 lc_evsnprintf(firm_get_arg_env(), buf, len, fmt, args);
661 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
663 lc_evoprintf(firm_get_arg_env(), obst, fmt, args);