From 531a99a40ad92eff318753d42b14b52565220025 Mon Sep 17 00:00:00 2001 From: Sebastian Hack Date: Mon, 29 Nov 2004 13:33:18 +0000 Subject: [PATCH] Added iterator and callback functionality. [r4494] --- ir/ir/irprintf.c | 79 ++++++++++++++++++++++++++++++++++++------------ ir/ir/irprintf.h | 49 ++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 19 deletions(-) diff --git a/ir/ir/irprintf.c b/ir/ir/irprintf.c index 8c123f279..7bbf9cbe7 100644 --- a/ir/ir/irprintf.c +++ b/ir/ir/irprintf.c @@ -12,38 +12,34 @@ #include "irnode.h" #include "tv.h" #include "irprintf.h" +#include "pset.h" +#include "iterator.h" + -/** - * Something that can append strings and chars to somewhere. - */ -typedef struct _appender_t { - void (*append_char)(void *subject, size_t n, char ch); - void (*append_str)(void *subject, size_t n, const char *str); -} appender_t; -static void str_append_char(void *subject, size_t n, char ch) +static void str_append_char(void *object, size_t n, char ch) { char buf[2]; buf[0] = ch; buf[1] = 0; - strncat(subject, buf, n); + strncat(object, buf, n); } -static void str_append_str(void *subject, size_t n, const char *str) +static void str_append_str(void *object, size_t n, const char *str) { - strncat(subject, str, n); + strncat(object, str, n); } -static void file_append_char(void *subject, size_t n, char ch) +static void file_append_char(void *object, size_t n, char ch) { - fputc(ch, subject); + fputc(ch, object); } -static void file_append_str(void *subject, size_t n, const char *str) +static void file_append_str(void *object, size_t n, const char *str) { - fputs(str, subject); + fputs(str, object); } static const appender_t file_appender = { @@ -56,24 +52,36 @@ static const appender_t str_appender = { str_append_str }; +static void ir_common_vprintf(const appender_t *app, void *object, + size_t limit, const char *fmt, va_list args); + +static INLINE void ir_common_printf(const appender_t *app, void *object, + size_t limit, const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + ir_common_vprintf(app, object, limit, fmt, args); + va_end(args); +} /** * A small printf helper routine for ir nodes. * @param app An appender (this determines where the stuff is dumped * to). - * @param subject A target passed to the appender. + * @param object A target passed to the appender. * @param limit The maximum number of characters to dump. * @param fmt The format string. * @param args A va_list. */ -static void ir_common_vprintf(const appender_t *app, void *subject, +static void ir_common_vprintf(const appender_t *app, void *object, size_t limit, const char *fmt, va_list args) { char buf[256]; int i, n; -#define DUMP_STR(s) app->append_str(subject, limit, s) -#define DUMP_CH(ch) app->append_char(subject, limit, ch) +#define DUMP_STR(s) app->append_str(object, limit, s) +#define DUMP_CH(ch) app->append_char(object, limit, ch) for(i = 0, n = strlen(fmt); i < n; ++i) { char ch = fmt[i]; @@ -124,6 +132,39 @@ static void ir_common_vprintf(const appender_t *app, void *subject, snprintf(buf, sizeof(buf), "%ld", get_irn_node_nr(get_nodes_block(va_arg(args, ir_node *)))); break; + + case '+': + { + iterator_t *it = va_arg(args, iterator_t *); + void *collection = va_arg(args, void *); + void *curr; + const char *prefix = ""; + char format = fmt[++i]; + ir_printf_cb_t *cb = format == 'C' ? va_arg(args, ir_printf_cb_t *) : NULL; + + assert(is_iterator(it) && "Pass an iterator interface and the collection"); + + snprintf(buf, sizeof(buf), "%%%c", format); + + DUMP_CH('['); + for(curr = it->start(collection); curr; curr = it->next(collection, curr)) { + DUMP_STR(prefix); + + if(cb) + cb(app, object, limit, curr); + else + ir_common_printf(app, object, limit, buf, curr); + + prefix = ", "; + } + it->finish(collection, curr); + + DUMP_CH(']'); + } + + /* clean the buffer again */ + buf[0] = '\0'; + break; } /* Dump the temporary buffer, if something is in it. */ diff --git a/ir/ir/irprintf.h b/ir/ir/irprintf.h index a46a50d38..8dbaa2b74 100644 --- a/ir/ir/irprintf.h +++ b/ir/ir/irprintf.h @@ -9,6 +9,23 @@ #include "config.h" +/** + * Something that can append strings and chars to something. + */ +typedef struct _appender_t { + void (*append_char)(void *object, size_t n, char ch); + void (*append_str)(void *object, size_t n, const char *str); +} appender_t; + +/** + * A callback function type to add something to an appender. + * @param app The appender. + * @param object The object for the appender. + * @param limit The limit for the appender. + * @param arg The thing to append. + */ +typedef void (ir_printf_cb_t)(const appender_t *app, void *object, size_t limit, const void *arg); + /** * A string formatting routine for ir nodes. * This function rudimentarily implements a kind of printf(3) for ir @@ -23,6 +40,38 @@ * - %b The block node number of the nodes block. * - %t A tarval. * + * Each of these can be prepended by a '+' which means, that the given + * pointer is a collection of items specified by the format. In this + * case you also have to pass an iterator interface to ir_printf + * suitable for the instance of the collection. So, imagine you have a + * pset of ir_nodes and want to dump it, you write: + * + * pset *nodes; + * ... + * ir_printf("Some nodes: %+n\n", it_pset, nodes); + * + * The it_pset is an iterator interface (of type + * iterator_t that allows the dumper to traverse the set. + * + * As special case when working with collections, you can also give a + * callback function which will be invoked on each element in the + * collection. It gets the appender (the thing where the textual + * representation of the element is written to) and its parameters + * passed by the dumping function. Suppose you have your own datatype + * xyz_t and want to dump a pset of it, you have: + * + * void xyz_dump(const appender_t *app, void *object, size_t limit, + * const void *arg) + * { + * const xyz_t *xyz = arg; + * app->append_str(object, limit, xyz->name); + * } + * ... + * pset *xyzs; + * + * ir_printf("A set of xyz\'s: %+C\n", it_pset, xyzs, xyz_dump); + * + * * @param fmt The format string. */ void ir_printf(const char *fmt, ...); -- 2.20.1