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
46 * append a char to a string buffer
48 static void str_append_char(void *object, size_t n, char ch)
55 strncat(object, buf, n);
59 * append a string to a string buffer
61 static void str_append_str(void *object, size_t n, const char *str)
63 strncat(object, str, n);
67 * append a char to a file
69 static void file_append_char(void *object, size_t n, char ch)
75 * append a string to a file
77 static void file_append_str(void *object, size_t n, const char *str)
85 static const appender_t file_appender = {
91 * the string buffer appender
93 static const appender_t str_appender = {
98 static void ir_common_vprintf(const appender_t *app, void *object,
99 size_t limit, const char *fmt, va_list args);
101 static INLINE void ir_common_printf(const appender_t *app, void *object,
102 size_t limit, const char *fmt, ...)
107 ir_common_vprintf(app, object, limit, fmt, args);
112 static int is_std_fmt(const char *fmt)
114 static const char *fmt_re_str =
115 "^[0 -+#']?[1-9]*(\\.[1-9]*)?[hlLqjzt]?[diouxXeEfFgGaAc]";
117 static regex_t fmt_re;
118 static int preapred_re = 0;
124 int res = regcomp(&fmt_re, fmt_re_str, REG_EXTENDED);
125 assert(res == 0 && "Could not prepare regex");
129 res = regexec(&fmt_re, fmt, 1, &match[0], 0);
134 regerror(res, &fmt_re, buf, sizeof(buf));
138 printf("res: %d, start: %d, end: %d\n",
139 res, match[0].rm_so, match[0].rm_eo);
142 return res == 0 ? match[0].rm_eo : -1;
154 #define MIN(x,y) ((x) < (y) ? (x) : (y))
155 #define MAX(x,y) ((x) > (y) ? (x) : (y))
158 static void dump_with_settings(const appender_t *app, void *object, size_t limit,
159 const struct settings *settings, const char *str)
161 if(settings->width >= 0) {
163 size_t n = strlen(str);
164 int lim = MIN(settings->width, limit);
165 int to_print = MIN(lim, n);
166 int to_pad = to_print - lim;
168 if(!settings->left_just)
169 for(i = 0; i < to_pad; ++i)
170 app->append_char(object, lim, settings->pad);
172 app->append_str(object, to_print, str);
174 if(!settings->left_just)
175 for(i = 0; i < to_pad; ++i)
176 app->append_char(object, lim, settings->pad);
180 app->append_str(object, limit, str);
185 /* Length specifiers. */
196 * A small printf helper routine for ir nodes.
197 * @param app An appender (this determines where the stuff is dumped
199 * @param object A target passed to the appender.
200 * @param limit The maximum number of characters to dump.
201 * @param fmt The format string.
202 * @param args A va_list.
204 static void ir_common_vprintf(const appender_t *app, void *object,
205 size_t limit, const char *fmt, va_list args)
211 #define DUMP_STR(s) app->append_str(object, limit, s)
212 #define DUMP_CH(ch) app->append_char(object, limit, ch)
214 for(i = 0, n = strlen(fmt); i < n; ++i) {
219 const char *len_str = "";
221 struct settings settings;
223 settings.alternate = 0;
226 settings.left_just = 0;
227 settings.put_plus = 0;
231 /* Clear the temporary buffer */
234 /* Set the string to print to the buffer by default. */
237 while(strchr("#0-+", ch)) {
240 settings.alternate = 1;
246 settings.left_just = 1;
249 settings.put_plus = 1;
257 /* Read the field width */
262 settings.width = (int) strtol(&fmt[i], &endptr, 10);
263 increase = (char *) endptr - &fmt[i];
264 ch = fmt[i += increase];
269 /* Ignore the precision */
271 while(isdigit(ch = fmt[++i]));
273 /* read the length modifier. */
278 if((ch = fmt[++i]) == 'h') {
287 if((ch = fmt[++i]) == 'l') {
297 /* Do the conversion specifier. */
300 /* The percent itself */
307 buf[0] = va_arg(args, int);
312 str = va_arg(args, const char *);
316 snprintf(buf, sizeof(buf), "%p", va_arg(args, void *));
325 snprintf(fmt_str, sizeof(fmt_str), "%%%s%c", len_str, ch);
332 int arg = va_arg(args, int);
333 snprintf(buf, sizeof(buf), fmt_str, arg);
339 long arg = va_arg(args, long);
340 snprintf(buf, sizeof(buf), fmt_str, arg);
346 long long arg = va_arg(args, long long);
347 snprintf(buf, sizeof(buf), fmt_str, arg);
355 str = get_id_str(va_arg(args, ident *));
359 str = get_entity_name(va_arg(args, entity *));
363 str = get_entity_ld_name(va_arg(args, entity *));
367 tarval_snprintf(buf, sizeof(buf), va_arg(args, tarval *));
372 ir_node *irn = va_arg(args, ir_node *);
373 snprintf(buf, sizeof(buf), "%s%s:%ld",
374 get_irn_opname(irn), get_mode_name(get_irn_mode(irn)), get_irn_node_nr(irn));
379 str = get_irn_opname(va_arg(args, ir_node *));
383 snprintf(buf, sizeof(buf), "%ld", get_irn_node_nr(va_arg(args, ir_node *)));
387 str = get_mode_name(va_arg(args, ir_mode *));
391 snprintf(buf, sizeof(buf), "%ld",
392 get_irn_node_nr(get_nodes_block(va_arg(args, ir_node *))));
397 const bitset_t *bs = va_arg(args, const bitset_t *);
398 const char *prefix = "";
402 for(i = bitset_next_set(bs, 0); i != -1; i = bitset_next_set(bs, i + 1)) {
403 snprintf(buf, sizeof(buf), "%ld", i);
415 iterator_t *it = va_arg(args, iterator_t *);
416 void *collection = va_arg(args, void *);
418 const char *prefix = "";
419 char format = fmt[++i];
420 ir_printf_cb_t *cb = format == 'C' ? va_arg(args, ir_printf_cb_t *) : NULL;
422 assert(is_iterator(it) && "Pass an iterator interface and the collection");
424 snprintf(buf, sizeof(buf), "%%%c", format);
427 for(curr = it->start(collection); curr; curr = it->next(collection, curr)) {
431 cb(app, object, limit, curr);
433 ir_common_printf(app, object, limit, buf, curr);
437 it->finish(collection, curr);
442 /* clean the buffer again */
447 dump_with_settings(app, object, limit, &settings, str);
459 * Convencience for stdout dumping.
461 void ir_printf(const char *fmt, ...)
465 ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
470 * Convencience for file dumping.
472 void ir_fprintf(FILE *f, const char *fmt, ...)
476 ir_common_vprintf(&file_appender, f, 0, fmt, args);
481 * Convencience for string dumping.
483 void ir_snprintf(char *buf, size_t len, const char *fmt, ...)
487 ir_common_vprintf(&str_appender, buf, len, fmt, args);