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
50 static void str_init(void *object, size_t n)
56 * append a char to a string buffer.
58 static void str_append_char(void *object, size_t n, char ch)
65 strncat(object, buf, n);
69 * append a string to a string buffer.
71 static void str_append_str(void *object, size_t n, const char *str)
73 strncat(object, str, n);
78 * Init the file. i.e. do nothing.
80 static void file_init(void *object, size_t n)
85 * append a char to a file.
87 static void file_append_char(void *object, size_t n, char ch)
93 * append a string to a file.
95 static void file_append_str(void *object, size_t n, const char *str)
101 * Init the obstack. i.e. do nothing.
103 static void obst_init(void *object, size_t n)
108 * append a char to a obstack.
110 static void obst_append_char(void *object, size_t n, char ch)
112 struct obstack *obst = object;
113 obstack_1grow(obst, ch);
117 * append a string to a obstack.
119 static void obst_append_str(void *object, size_t n, const char *str)
121 struct obstack *obst = object;
122 obstack_grow(obst, str, strlen(str));
129 static const appender_t file_appender = {
136 * the string buffer appender
138 static const appender_t str_appender = {
145 * the obstack appender.
147 static const appender_t obst_appender = {
153 static void ir_common_vprintf(const appender_t *app, void *object,
154 size_t limit, const char *fmt, va_list args);
156 static INLINE void ir_common_printf(const appender_t *app, void *object,
157 size_t limit, const char *fmt, ...)
162 ir_common_vprintf(app, object, limit, fmt, args);
167 static int is_std_fmt(const char *fmt)
169 static const char *fmt_re_str =
170 "^[0 -+#']?[1-9]*(\\.[1-9]*)?[hlLqjzt]?[diouxXeEfFgGaAc]";
172 static regex_t fmt_re;
173 static int preapred_re = 0;
179 int res = regcomp(&fmt_re, fmt_re_str, REG_EXTENDED);
180 assert(res == 0 && "Could not prepare regex");
184 res = regexec(&fmt_re, fmt, 1, &match[0], 0);
189 regerror(res, &fmt_re, buf, sizeof(buf));
193 printf("res: %d, start: %d, end: %d\n",
194 res, match[0].rm_so, match[0].rm_eo);
197 return res == 0 ? match[0].rm_eo : -1;
209 /* Length specifiers. */
219 #define MIN(x,y) ((x) < (y) ? (x) : (y))
220 #define MAX(x,y) ((x) > (y) ? (x) : (y))
222 static void dump_with_settings(const appender_t *app, void *object, size_t limit,
223 const struct settings *settings, const char *str)
225 if(settings->width >= 0) {
227 size_t n = strlen(str);
228 int lim = MIN(settings->width, limit);
229 int to_print = MIN(lim, n);
230 int to_pad = to_print - lim;
232 if(!settings->left_just)
233 for(i = 0; i < to_pad; ++i)
234 app->append_char(object, lim, settings->pad);
236 app->append_str(object, to_print, str);
238 if(!settings->left_just)
239 for(i = 0; i < to_pad; ++i)
240 app->append_char(object, lim, settings->pad);
244 app->append_str(object, limit, str);
249 * A small printf helper routine for ir nodes.
250 * @param app An appender (this determines where the stuff is dumped
252 * @param object A target passed to the appender.
253 * @param limit The maximum number of characters to dump.
254 * @param fmt The format string.
255 * @param args A va_list.
257 static void ir_common_vprintf(const appender_t *app, void *object,
258 size_t limit, const char *fmt, va_list args)
264 #define DUMP_STR(s) app->append_str(object, limit, s)
265 #define DUMP_CH(ch) app->append_char(object, limit, ch)
267 app->init(object, limit);
269 for(i = 0, n = strlen(fmt); i < n; ++i) {
274 const char *len_str = "";
276 struct settings settings;
278 settings.alternate = 0;
281 settings.left_just = 0;
282 settings.put_plus = 0;
286 /* Clear the temporary buffer */
289 /* Set the string to print to the buffer by default. */
292 while(strchr("#0-+", ch)) {
295 settings.alternate = 1;
301 settings.left_just = 1;
304 settings.put_plus = 1;
312 /* Read the field width */
317 settings.width = (int) strtol(&fmt[i], &endptr, 10);
318 increase = (char *) endptr - &fmt[i];
319 ch = fmt[i += increase];
324 /* Ignore the precision */
326 while(isdigit(ch = fmt[++i]));
328 /* read the length modifier. */
333 if((ch = fmt[++i]) == 'h') {
342 if((ch = fmt[++i]) == 'l') {
352 /* Do the conversion specifier. */
355 /* The percent itself */
362 buf[0] = va_arg(args, int);
367 str = va_arg(args, const char *);
371 snprintf(buf, sizeof(buf), "%p", va_arg(args, void *));
380 snprintf(fmt_str, sizeof(fmt_str), "%%%s%c", len_str, ch);
387 int arg = va_arg(args, int);
388 snprintf(buf, sizeof(buf), fmt_str, arg);
394 long arg = va_arg(args, long);
395 snprintf(buf, sizeof(buf), fmt_str, arg);
401 long long arg = va_arg(args, long long);
402 snprintf(buf, sizeof(buf), fmt_str, arg);
410 str = get_id_str(va_arg(args, ident *));
414 str = get_type_name(va_arg(args, type *));
418 str = get_entity_name(va_arg(args, entity *));
422 str = get_entity_ld_name(va_arg(args, entity *));
426 tarval_snprintf(buf, sizeof(buf), va_arg(args, tarval *));
431 ir_node *irn = va_arg(args, ir_node *);
432 snprintf(buf, sizeof(buf), "%s%s:%ld",
433 get_irn_opname(irn), get_mode_name(get_irn_mode(irn)), get_irn_node_nr(irn));
438 str = get_irn_opname(va_arg(args, ir_node *));
442 snprintf(buf, sizeof(buf), "%ld", get_irn_node_nr(va_arg(args, ir_node *)));
446 str = get_mode_name(va_arg(args, ir_mode *));
450 snprintf(buf, sizeof(buf), "%ld",
451 get_irn_node_nr(get_nodes_block(va_arg(args, ir_node *))));
456 const bitset_t *bs = va_arg(args, const bitset_t *);
457 const char *prefix = "";
461 for(i = bitset_next_set(bs, 0); i != -1; i = bitset_next_set(bs, i + 1)) {
462 snprintf(buf, sizeof(buf), "%ld", i);
474 iterator_t *it = va_arg(args, iterator_t *);
475 void *collection = va_arg(args, void *);
477 const char *prefix = "";
478 char format = fmt[++i];
479 ir_printf_cb_t *cb = format == 'C' ? va_arg(args, ir_printf_cb_t *) : NULL;
481 assert(is_iterator(it) && "Pass an iterator interface and the collection");
483 snprintf(buf, sizeof(buf), "%%%c", format);
486 for(curr = it->start(collection); curr; curr = it->next(collection, curr)) {
490 cb(app, object, limit, curr);
492 ir_common_printf(app, object, limit, buf, curr);
496 it->finish(collection, curr);
501 /* clean the buffer again */
506 dump_with_settings(app, object, limit, &settings, str);
518 * Convencience for stdout dumping.
520 void ir_printf(const char *fmt, ...)
524 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
529 * Convencience for file dumping.
531 void ir_fprintf(FILE *f, const char *fmt, ...)
535 ir_common_vprintf(&file_appender, f, 0, fmt, args);
540 * Convencience for string dumping.
542 void ir_snprintf(char *buf, size_t len, const char *fmt, ...)
546 ir_common_vprintf(&str_appender, buf, len, fmt, args);
551 * Convencience for string dumping.
553 void ir_obst_printf(struct obstack *obst, const char *fmt, ...)
557 ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
561 void ir_vprintf(const char *fmt, va_list args)
563 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
566 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
568 ir_common_vprintf(&file_appender, f, 0, fmt, args);
571 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
573 ir_common_vprintf(&str_appender, buf, len, fmt, args);
576 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
578 ir_common_vprintf(&obst_appender, obst, 0, fmt, args);