Added iterator and callback functionality.
[libfirm] / ir / ir / irprintf.h
1 /**
2  * A little printf understanding some firm types.
3  * @author Sebastian Hack
4  * @date 29.11.2004
5  */
6
7 #ifndef _IRPRINTF_H
8 #define _IRPRINTF_H
9
10 #include "config.h"
11
12 /**
13  * Something that can append strings and chars to something.
14  */
15 typedef struct _appender_t {
16         void (*append_char)(void *object, size_t n, char ch);
17         void (*append_str)(void *object, size_t n, const char *str);
18 } appender_t;
19
20 /**
21  * A callback function type to add something to an appender.
22  * @param app The appender.
23  * @param object The object for the appender.
24  * @param limit The limit for the appender.
25  * @param arg The thing to append.
26  */
27 typedef void (ir_printf_cb_t)(const appender_t *app, void *object, size_t limit, const void *arg);
28
29 /**
30  * A string formatting routine for ir nodes.
31  * This function rudimentarily implements a kind of printf(3) for ir
32  * nodes. Following conversion specifiers. No length, special or field
33  * width specifiers are accepted.
34  * - %p A pointer.
35  * - %s A string.
36  * - %n A full description of a node.
37  * - %o The opcode name of an ir node.
38  * - %m The mode name of an ir mode.
39  * - %N The node number of an ir node.
40  * - %b The block node number of the nodes block.
41  * - %t A tarval.
42  *
43  * Each of these can be prepended by a '+' which means, that the given
44  * pointer is a collection of items specified by the format. In this
45  * case you also have to pass an iterator interface to ir_printf
46  * suitable for the instance of the collection. So, imagine you have a
47  * <tt>pset</tt> of ir_nodes and want to dump it, you write:
48  * <code>
49  *   pset *nodes;
50  *   ...
51  *   ir_printf("Some nodes: %+n\n", it_pset, nodes);
52  * </code>
53  * The <tt>it_pset</tt> is an iterator interface (of type
54  * <tt>iterator_t</tt> that allows the dumper to traverse the set.
55  *
56  * As special case when working with collections, you can also give a
57  * callback function which will be invoked on each element in the
58  * collection. It gets the appender (the thing where the textual
59  * representation of the element is written to) and its parameters
60  * passed by the dumping function. Suppose you have your own datatype
61  * <tt>xyz_t</tt> and want to dump a pset of it, you have:
62  * <code>
63  *   void xyz_dump(const appender_t *app, void *object, size_t limit,
64  *       const void *arg)
65  *   {
66  *     const xyz_t *xyz = arg;
67  *     app->append_str(object, limit, xyz->name);
68  *   }
69  *   ...
70  *   pset *xyzs;
71  *
72  *   ir_printf("A set of xyz\'s: %+C\n", it_pset, xyzs, xyz_dump);
73  * </code>
74  *
75  * @param fmt The format string.
76  */
77 void ir_printf(const char *fmt, ...);
78
79 /**
80  * @see irn_printf.
81  */
82 void ir_fprintf(FILE *f, const char *fmt, ...);
83
84 /**
85  * @see irn_printf.
86  */
87 void ir_snprintf(char *buf, size_t n, const char *fmt, ...);
88
89 #ifdef DEBUG_libfirm
90
91 #define ir_debugf ir_printf
92 #define ir_fdebugf ir_fprintf
93
94 #else
95
96 static INLINE void ir_debugf(const char *fmt, ...)
97 {
98 }
99
100 static INLINE void ir_fdebugf(FILE *, const char *fmt, ...)
101 {
102 }
103
104
105 #endif
106
107 #endif