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
48 static void str_init(void *object, size_t n)
54 * append a char to a string buffer.
56 static void str_append_char(void *object, size_t n, char ch)
63 strncat(object, buf, n);
67 * append a string to a string buffer.
69 static void str_append_str(void *object, size_t n, const char *str)
71 strncat(object, str, n);
76 * Init the file. i.e. do nothing.
78 static void file_init(void *object, size_t n)
83 * append a char to a file.
85 static void file_append_char(void *object, size_t n, char ch)
91 * append a string to a file.
93 static void file_append_str(void *object, size_t n, const char *str)
101 static const appender_t file_appender = {
108 * the string buffer appender
110 static const appender_t str_appender = {
116 static void ir_common_vprintf(const appender_t *app, void *object,
117 size_t limit, const char *fmt, va_list args);
119 static INLINE void ir_common_printf(const appender_t *app, void *object,
120 size_t limit, const char *fmt, ...)
125 ir_common_vprintf(app, object, limit, fmt, args);
130 static int is_std_fmt(const char *fmt)
132 static const char *fmt_re_str =
133 "^[0 -+#']?[1-9]*(\\.[1-9]*)?[hlLqjzt]?[diouxXeEfFgGaAc]";
135 static regex_t fmt_re;
136 static int preapred_re = 0;
142 int res = regcomp(&fmt_re, fmt_re_str, REG_EXTENDED);
143 assert(res == 0 && "Could not prepare regex");
147 res = regexec(&fmt_re, fmt, 1, &match[0], 0);
152 regerror(res, &fmt_re, buf, sizeof(buf));
156 printf("res: %d, start: %d, end: %d\n",
157 res, match[0].rm_so, match[0].rm_eo);
160 return res == 0 ? match[0].rm_eo : -1;
172 /* Length specifiers. */
182 #define MIN(x,y) ((x) < (y) ? (x) : (y))
183 #define MAX(x,y) ((x) > (y) ? (x) : (y))
185 static void dump_with_settings(const appender_t *app, void *object, size_t limit,
186 const struct settings *settings, const char *str)
188 if(settings->width >= 0) {
190 size_t n = strlen(str);
191 int lim = MIN(settings->width, limit);
192 int to_print = MIN(lim, n);
193 int to_pad = to_print - lim;
195 if(!settings->left_just)
196 for(i = 0; i < to_pad; ++i)
197 app->append_char(object, lim, settings->pad);
199 app->append_str(object, to_print, str);
201 if(!settings->left_just)
202 for(i = 0; i < to_pad; ++i)
203 app->append_char(object, lim, settings->pad);
207 app->append_str(object, limit, str);
212 * A small printf helper routine for ir nodes.
213 * @param app An appender (this determines where the stuff is dumped
215 * @param object A target passed to the appender.
216 * @param limit The maximum number of characters to dump.
217 * @param fmt The format string.
218 * @param args A va_list.
220 static void ir_common_vprintf(const appender_t *app, void *object,
221 size_t limit, const char *fmt, va_list args)
227 #define DUMP_STR(s) app->append_str(object, limit, s)
228 #define DUMP_CH(ch) app->append_char(object, limit, ch)
230 app->init(object, limit);
232 for(i = 0, n = strlen(fmt); i < n; ++i) {
237 const char *len_str = "";
239 struct settings settings;
241 settings.alternate = 0;
244 settings.left_just = 0;
245 settings.put_plus = 0;
249 /* Clear the temporary buffer */
252 /* Set the string to print to the buffer by default. */
255 while(strchr("#0-+", ch)) {
258 settings.alternate = 1;
264 settings.left_just = 1;
267 settings.put_plus = 1;
275 /* Read the field width */
280 settings.width = (int) strtol(&fmt[i], &endptr, 10);
281 increase = (char *) endptr - &fmt[i];
282 ch = fmt[i += increase];
287 /* Ignore the precision */
289 while(isdigit(ch = fmt[++i]));
291 /* read the length modifier. */
296 if((ch = fmt[++i]) == 'h') {
305 if((ch = fmt[++i]) == 'l') {
315 /* Do the conversion specifier. */
318 /* The percent itself */
325 buf[0] = va_arg(args, int);
330 str = va_arg(args, const char *);
334 snprintf(buf, sizeof(buf), "%p", va_arg(args, void *));
343 snprintf(fmt_str, sizeof(fmt_str), "%%%s%c", len_str, ch);
350 int arg = va_arg(args, int);
351 snprintf(buf, sizeof(buf), fmt_str, arg);
357 long arg = va_arg(args, long);
358 snprintf(buf, sizeof(buf), fmt_str, arg);
364 long long arg = va_arg(args, long long);
365 snprintf(buf, sizeof(buf), fmt_str, arg);
373 str = get_id_str(va_arg(args, ident *));
377 str = get_entity_name(va_arg(args, entity *));
381 str = get_entity_ld_name(va_arg(args, entity *));
385 tarval_snprintf(buf, sizeof(buf), va_arg(args, tarval *));
390 ir_node *irn = va_arg(args, ir_node *);
391 snprintf(buf, sizeof(buf), "%s%s:%ld",
392 get_irn_opname(irn), get_mode_name(get_irn_mode(irn)), get_irn_node_nr(irn));
397 str = get_irn_opname(va_arg(args, ir_node *));
401 snprintf(buf, sizeof(buf), "%ld", get_irn_node_nr(va_arg(args, ir_node *)));
405 str = get_mode_name(va_arg(args, ir_mode *));
409 snprintf(buf, sizeof(buf), "%ld",
410 get_irn_node_nr(get_nodes_block(va_arg(args, ir_node *))));
415 const bitset_t *bs = va_arg(args, const bitset_t *);
416 const char *prefix = "";
420 for(i = bitset_next_set(bs, 0); i != -1; i = bitset_next_set(bs, i + 1)) {
421 snprintf(buf, sizeof(buf), "%ld", i);
433 iterator_t *it = va_arg(args, iterator_t *);
434 void *collection = va_arg(args, void *);
436 const char *prefix = "";
437 char format = fmt[++i];
438 ir_printf_cb_t *cb = format == 'C' ? va_arg(args, ir_printf_cb_t *) : NULL;
440 assert(is_iterator(it) && "Pass an iterator interface and the collection");
442 snprintf(buf, sizeof(buf), "%%%c", format);
445 for(curr = it->start(collection); curr; curr = it->next(collection, curr)) {
449 cb(app, object, limit, curr);
451 ir_common_printf(app, object, limit, buf, curr);
455 it->finish(collection, curr);
460 /* clean the buffer again */
465 dump_with_settings(app, object, limit, &settings, str);
477 * Convencience for stdout dumping.
479 void ir_printf(const char *fmt, ...)
483 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
488 * Convencience for file dumping.
490 void ir_fprintf(FILE *f, const char *fmt, ...)
494 ir_common_vprintf(&file_appender, f, 0, fmt, args);
499 * Convencience for string dumping.
501 void ir_snprintf(char *buf, size_t len, const char *fmt, ...)
505 ir_common_vprintf(&str_appender, buf, len, fmt, args);