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 *));
398 snprintf(fmt_str, sizeof(fmt_str), "%%%s%c", len_str, ch);
405 int arg = va_arg(args, int);
406 snprintf(buf, sizeof(buf), fmt_str, arg);
412 long arg = va_arg(args, long);
413 snprintf(buf, sizeof(buf), fmt_str, arg);
419 int64_t arg = va_arg(args, int64_t);
420 snprintf(buf, sizeof(buf), fmt_str, arg);
428 str = get_id_str(va_arg(args, ident *));
432 str = get_type_name(va_arg(args, type *));
436 str = get_entity_name(va_arg(args, entity *));
440 str = get_entity_ld_name(va_arg(args, entity *));
444 tarval_snprintf(buf, sizeof(buf), va_arg(args, tarval *));
449 ir_node *irn = va_arg(args, ir_node *);
451 snprintf(buf, sizeof(buf), "%s%s:%ld",
452 get_irn_opname(irn), get_mode_name(get_irn_mode(irn)), get_irn_node_nr(irn));
454 strncpy(buf, STRNIL, sizeof(buf));
459 str = get_irn_opname(va_arg(args, ir_node *));
463 snprintf(buf, sizeof(buf), "%ld", get_irn_node_nr(va_arg(args, ir_node *)));
467 str = get_mode_name(va_arg(args, ir_mode *));
471 snprintf(buf, sizeof(buf), "%ld",
472 get_irn_node_nr(get_nodes_block(va_arg(args, ir_node *))));
477 const bitset_t *bs = va_arg(args, const bitset_t *);
478 const char *prefix = "";
482 for(i = bitset_next_set(bs, 0); i != -1; i = bitset_next_set(bs, i + 1)) {
483 snprintf(buf, sizeof(buf), "%ld", i);
495 iterator_t *it = va_arg(args, iterator_t *);
496 void *collection = va_arg(args, void *);
498 const char *prefix = "";
499 char format = fmt[++i];
500 ir_printf_cb_t *cb = format == 'C' ? va_arg(args, ir_printf_cb_t *) : NULL;
502 assert(is_iterator(it) && "Pass an iterator interface and the collection");
504 snprintf(buf, sizeof(buf), "%%%c", format);
507 for(curr = it->start(collection); curr; curr = it->next(collection, curr)) {
511 cb(app, object, limit, curr);
513 ir_common_printf(app, object, limit, buf, curr);
517 it->finish(collection, curr);
522 /* clean the buffer again */
527 dump_with_settings(app, object, limit, &settings, str);
539 * Convencience for stdout dumping.
541 void ir_printf(const char *fmt, ...)
545 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
550 * Convencience for file dumping.
552 void ir_fprintf(FILE *f, const char *fmt, ...)
556 ir_common_vprintf(&file_appender, f, 0, fmt, args);
561 * Convencience for string dumping.
563 void ir_snprintf(char *buf, size_t len, const char *fmt, ...)
567 ir_common_vprintf(&str_appender, buf, len, fmt, args);
572 * Convencience for string dumping.
574 void ir_obst_printf(struct obstack *obst, const char *fmt, ...)
578 ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
582 void ir_vprintf(const char *fmt, va_list args)
584 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
587 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
589 ir_common_vprintf(&file_appender, f, 0, fmt, args);
592 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
594 ir_common_vprintf(&str_appender, buf, len, fmt, args);
597 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
599 ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
604 #include "irargs_t.h"
606 void ir_printf(const char *fmt, ...)
611 lc_evprintf(firm_get_arg_env(), fmt, args);
615 void ir_fprintf(FILE *f, const char *fmt, ...)
620 lc_evfprintf(firm_get_arg_env(), f, fmt, args);
624 void ir_snprintf(char *buf, size_t n, const char *fmt, ...)
629 lc_evsnprintf(firm_get_arg_env(), buf, n, fmt, args);
633 void ir_vprintf(const char *fmt, va_list args)
635 lc_evprintf(firm_get_arg_env(), fmt, args);
638 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
640 lc_evfprintf(firm_get_arg_env(), f, fmt, args);
643 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
645 lc_evsnprintf(firm_get_arg_env(), buf, len, fmt, args);
648 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
650 lc_evoprintf(firm_get_arg_env(), obst, fmt, args);