removed INLIEN before global functions
[libfirm] / ir / ir / irprintf.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irprintf.c
4  * Purpose:     A little printf helper unterstanding firm types
5  * Author:      Sebastian Hack
6  * Created:     29.11.2004
7  * CVS-ID:      $Id$
8  * Copyright:   (c) 1998-2004 Universität Karlsruhe
9  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
10  */
11
12 /**
13  * @file irprinf.c
14  *
15  * A little printf helper unterstanding firm types.
16  * @author Sebastian Hack
17  * @date 29.11.2004
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #ifdef HAVE_STRING_H
25 #include <string.h>
26 #endif
27 #ifdef HAVE_INTTYPES_H
28 #include <inttypes.h>
29 #endif
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <stdarg.h>
34
35 #include <ctype.h>
36
37 #include "ident.h"
38 #include "irmode_t.h"
39 #include "irnode_t.h"
40 #include "entity_t.h"
41 #include "type_t.h"
42 #include "tv.h"
43 #include "irprintf.h"
44 #include "obst.h"
45 #include "pset.h"
46 #include "iterator.h"
47 #include "bitset.h"
48
49
50 /**
51  * Init the string.
52  */
53 static void str_init(void *object, size_t n)
54 {
55         strcpy(object, "");
56 }
57
58 /**
59  * append a char to a string buffer.
60  */
61 static void str_append_char(void *object, size_t n, char ch)
62 {
63         char buf[2];
64
65         buf[0] = ch;
66         buf[1] = 0;
67
68         strncat(object, buf, n);
69 }
70
71 /**
72  * append a string to a string buffer.
73  */
74 static void str_append_str(void *object, size_t n, const char *str)
75 {
76         strncat(object, str, n);
77 }
78
79
80 /**
81  * Init the file. i.e. do nothing.
82  */
83 static void file_init(void *object, size_t n)
84 {
85 }
86
87 /**
88  * append a char to a file.
89  */
90 static void file_append_char(void *object, size_t n, char ch)
91 {
92         fputc(ch, object);
93 }
94
95 /**
96  * append a string to a file.
97  */
98 static void file_append_str(void *object, size_t n, const char *str)
99 {
100         fputs(str, object);
101 }
102
103 /**
104  * Init the obstack. i.e. do nothing.
105  */
106 static void obst_init(void *object, size_t n)
107 {
108 }
109
110 /**
111  * append a char to a obstack.
112  */
113 static void obst_append_char(void *object, size_t n, char ch)
114 {
115         struct obstack *obst = object;
116         obstack_1grow(obst, ch);
117 }
118
119 /**
120  * append a string to a obstack.
121  */
122 static void obst_append_str(void *object, size_t n, const char *str)
123 {
124         struct obstack *obst = object;
125         obstack_grow(obst, str, strlen(str));
126 }
127
128
129 /**
130  * the file appender
131  */
132 static const appender_t file_appender = {
133         file_init,
134         file_append_char,
135         file_append_str
136 };
137
138 /**
139  * the string buffer appender
140  */
141 static const appender_t str_appender = {
142         str_init,
143         str_append_char,
144         str_append_str
145 };
146
147 /**
148  * the obstack appender.
149  */
150 static const appender_t obst_appender = {
151         obst_init,
152         obst_append_char,
153         obst_append_str
154 };
155
156 static void ir_common_vprintf(const appender_t *app, void *object,
157                 size_t limit, const char *fmt, va_list args);
158
159 static INLINE void ir_common_printf(const appender_t *app, void *object,
160                 size_t limit, const char *fmt, ...)
161 {
162         va_list args;
163
164         va_start(args, fmt);
165         ir_common_vprintf(app, object, limit, fmt, args);
166         va_end(args);
167 }
168
169 #if 0
170 static int is_std_fmt(const char *fmt)
171 {
172         static const char *fmt_re_str =
173                 "^[0 -+#']?[1-9]*(\\.[1-9]*)?[hlLqjzt]?[diouxXeEfFgGaAc]";
174
175         static regex_t fmt_re;
176         static int preapred_re = 0;
177
178         regmatch_t match[1];
179         int res;
180
181         if(!preapred_re) {
182                 int res = regcomp(&fmt_re, fmt_re_str, REG_EXTENDED);
183                 assert(res == 0 && "Could not prepare regex");
184                 preapred_re = 1;
185         }
186
187         res = regexec(&fmt_re, fmt, 1, &match[0], 0);
188
189 #if 0
190         if(res != 0) {
191                 char buf[256];
192                 regerror(res, &fmt_re, buf, sizeof(buf));
193                 printf("%s ", buf);
194         }
195
196         printf("res: %d, start: %d, end: %d\n",
197                         res, match[0].rm_so, match[0].rm_eo);
198 #endif
199
200         return res == 0 ? match[0].rm_eo : -1;
201 }
202 #endif
203
204 struct settings {
205         char pad;
206         int width;
207         int left_just;
208         int put_plus;
209         int alternate;
210 };
211
212 /* Length specifiers. */
213 enum {
214         len_char,
215         len_short,
216         len_int,
217         len_long,
218         len_long_long
219 };
220
221
222 #define MIN(x,y) ((x) < (y) ? (x) : (y))
223 #define MAX(x,y) ((x) > (y) ? (x) : (y))
224
225 static void dump_with_settings(const appender_t *app, void *object, size_t limit,
226                 const struct settings *settings, const char *str)
227 {
228         if(settings->width >= 0) {
229                 int i;
230                 size_t n = strlen(str);
231                 int lim = MIN(settings->width, (int)limit);
232                 int to_print = MIN(lim, (int)n);
233                 int to_pad = to_print - lim;
234
235                 if(!settings->left_just)
236                         for(i = 0; i < to_pad; ++i)
237                                 app->append_char(object, lim, settings->pad);
238
239                 app->append_str(object, to_print, str);
240
241                 if(!settings->left_just)
242                         for(i = 0; i < to_pad; ++i)
243                                 app->append_char(object, lim, settings->pad);
244         }
245
246         else
247                 app->append_str(object, limit, str);
248 }
249
250
251 /**
252  * A small printf helper routine for ir nodes.
253  * @param app An appender (this determines where the stuff is dumped
254  * to).
255  * @param object A target passed to the appender.
256  * @param limit The maximum number of characters to dump.
257  * @param fmt The format string.
258  * @param args A va_list.
259  */
260 static void ir_common_vprintf(const appender_t *app, void *object,
261                 size_t limit, const char *fmt, va_list args)
262 {
263         const char *str;
264         char buf[4096];
265         int i, n;
266
267 #define DUMP_STR(s) app->append_str(object, limit, s)
268 #define DUMP_CH(ch) app->append_char(object, limit, ch)
269
270         app->init(object, limit);
271
272         for(i = 0, n = strlen(fmt); i < n; ++i) {
273                 char ch = fmt[i];
274
275                 if(ch == '%') {
276                         int len;
277                         const char *len_str = "";
278
279                         struct settings settings;
280
281                         settings.alternate = 0;
282                         settings.pad = ' ';
283                         settings.width = -1;
284                         settings.left_just = 0;
285                         settings.put_plus = 0;
286
287                         ch = fmt[++i];
288
289                         /* Clear the temporary buffer */
290                         buf[0] = '\0';
291
292                         /* Set the string to print to the buffer by default. */
293                         str = buf;
294
295                         while(strchr("#0-+", ch)) {
296                                 switch(ch) {
297                                         case '#':
298                                                 settings.alternate = 1;
299                                                 break;
300                                         case '0':
301                                                 settings.pad = '0';
302                                                 break;
303                                         case '-':
304                                                 settings.left_just = 1;
305                                                 break;
306                                         case '+':
307                                                 settings.put_plus = 1;
308                                                 break;
309                                 }
310
311                                 ch = fmt[++i];
312                         }
313
314
315                         /* Read the field width */
316                         {
317                                 char *endptr;
318                                 int increase;
319
320                                 settings.width = (int) strtol(&fmt[i], &endptr, 10);
321                                 increase = (char *) endptr - &fmt[i];
322                                 ch = fmt[i += increase];
323                                 if(increase == 0)
324                                         settings.width = -1;
325                         }
326
327                         /* Ignore the precision */
328                         if(ch == '.')
329                                 while(isdigit(ch = fmt[++i]));
330
331                         /* read the length modifier. */
332                         switch(ch) {
333                                 case 'h':
334                                         len_str = "h";
335                                         len = len_short;
336                                         if((ch = fmt[++i]) == 'h') {
337                                                 len_str = "hh";
338                                                 len = len_char;
339                                         }
340                                         break;
341
342                                 case 'l':
343                                         len_str = "l";
344                                         len = len_long;
345                                         if((ch = fmt[++i]) == 'l') {
346                                                 len_str = "ll";
347                                                 len = len_long_long;
348                                         }
349                                         break;
350
351                                 default:
352                                         len = len_int;
353                         }
354
355                         /* Do the conversion specifier. */
356                         switch(ch) {
357
358                                 /* The percent itself */
359                                 case '%':
360                                         buf[0] = '%';
361                                         buf[1] = '\0';
362                                         break;
363
364                                 case 'c':
365                                         buf[0] = va_arg(args, int);
366                                         buf[1] = '\0';
367                                         break;
368
369                                 case 's':
370                                         str = va_arg(args, const char *);
371                                         break;
372
373                                 case 'p':
374                                         snprintf(buf, sizeof(buf), "%p", va_arg(args, void *));
375                                         break;
376
377                                 case 'd':
378                                 case 'x':
379                                 case 'X':
380                                 case 'o':
381                                         {
382                                                 char fmt_str[16];
383                                                 snprintf(fmt_str, sizeof(fmt_str), "%%%s%c", len_str, ch);
384
385                                                 switch(len) {
386                                                         case len_char:
387                                                         case len_short:
388                                                         case len_int:
389                                                                 {
390                                                                         int arg = va_arg(args, int);
391                                                                         snprintf(buf, sizeof(buf), fmt_str, arg);
392                                                                 }
393                                                                 break;
394
395                                                         case len_long:
396                                                                 {
397                                                                         long arg = va_arg(args, long);
398                                                                         snprintf(buf, sizeof(buf), fmt_str, arg);
399                                                                 }
400                                                                 break;
401
402                                                         case len_long_long:
403                                                                 {
404                                                                         int64_t arg = va_arg(args, int64_t);
405                                                                         snprintf(buf, sizeof(buf), fmt_str, arg);
406                                                                 }
407                                                                 break;
408                                                 }
409                                         }
410                                         break;
411
412                                 case 'I':
413                                         str = get_id_str(va_arg(args, ident *));
414                                         break;
415
416                                 case 't':
417                                         str = get_type_name(va_arg(args, type *));
418                                         break;
419
420                                 case 'e':
421                                         str = get_entity_name(va_arg(args, entity *));
422                                         break;
423
424                                 case 'E':
425                                         str = get_entity_ld_name(va_arg(args, entity *));
426                                         break;
427
428                                 case 'T':
429                                         tarval_snprintf(buf, sizeof(buf), va_arg(args, tarval *));
430                                         break;
431
432                                 case 'n':
433                                         {
434                                                 ir_node *irn = va_arg(args, ir_node *);
435                                                 snprintf(buf, sizeof(buf), "%s%s:%ld",
436                                                                 get_irn_opname(irn), get_mode_name(get_irn_mode(irn)), get_irn_node_nr(irn));
437                                         }
438                                         break;
439
440                                 case 'O':
441                                         str = get_irn_opname(va_arg(args, ir_node *));
442                                         break;
443
444                                 case 'N':
445                                         snprintf(buf, sizeof(buf), "%ld", get_irn_node_nr(va_arg(args, ir_node *)));
446                                         break;
447
448                                 case 'm':
449                                         str = get_mode_name(va_arg(args, ir_mode *));
450                                         break;
451
452                                 case 'B':
453                                         snprintf(buf, sizeof(buf), "%ld",
454                                                         get_irn_node_nr(get_nodes_block(va_arg(args, ir_node *))));
455                                         break;
456
457                                 case 'b':
458                                         {
459                                                 const bitset_t *bs = va_arg(args, const bitset_t *);
460                                                 const char *prefix = "";
461                                                 unsigned long i;
462
463                                                 DUMP_CH('[');
464                                                 for(i = bitset_next_set(bs, 0); i != -1; i = bitset_next_set(bs, i + 1)) {
465                                                         snprintf(buf, sizeof(buf), "%ld", i);
466                                                         DUMP_STR(prefix);
467                                                         DUMP_STR(buf);
468                                                         prefix = ", ";
469                                                 }
470                                                 DUMP_CH(']');
471                                                 buf[0] = '\0';
472                                         }
473                                         break;
474
475                                 case '*':
476                                         {
477                                                 iterator_t *it = va_arg(args, iterator_t *);
478                                                 void *collection = va_arg(args, void *);
479                                                 void *curr;
480                                                 const char *prefix = "";
481                                                 char format = fmt[++i];
482                                                 ir_printf_cb_t *cb = format == 'C' ? va_arg(args, ir_printf_cb_t *) : NULL;
483
484                                                 assert(is_iterator(it) && "Pass an iterator interface and the collection");
485
486                                                 snprintf(buf, sizeof(buf), "%%%c", format);
487
488                                                 DUMP_CH('[');
489                                                 for(curr = it->start(collection); curr; curr = it->next(collection, curr)) {
490                                                         DUMP_STR(prefix);
491
492                                                         if(cb)
493                                                                 cb(app, object, limit, curr);
494                                                         else
495                                                                 ir_common_printf(app, object, limit, buf, curr);
496
497                                                         prefix = ", ";
498                                                 }
499                                                 it->finish(collection, curr);
500
501                                                 DUMP_CH(']');
502                                         }
503
504                                         /* clean the buffer again */
505                                         buf[0] = '\0';
506                                         break;
507                         }
508
509                         dump_with_settings(app, object, limit, &settings, str);
510                 }
511
512                 else
513                         DUMP_CH(ch);
514         }
515
516 #undef DUMP_STR
517 #undef DUMP_CH
518 }
519
520 /**
521  * Convencience for stdout dumping.
522  */
523 void ir_printf(const char *fmt, ...)
524 {
525         va_list args;
526         va_start(args, fmt);
527         ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
528         va_end(args);
529 }
530
531 /**
532  * Convencience for file dumping.
533  */
534 void ir_fprintf(FILE *f, const char *fmt, ...)
535 {
536         va_list args;
537         va_start(args, fmt);
538         ir_common_vprintf(&file_appender, f, 0, fmt, args);
539         va_end(args);
540 }
541
542 /**
543  * Convencience for string dumping.
544  */
545 void ir_snprintf(char *buf, size_t len, const char *fmt, ...)
546 {
547         va_list args;
548         va_start(args, fmt);
549         ir_common_vprintf(&str_appender, buf, len, fmt, args);
550         va_end(args);
551 }
552
553 /**
554  * Convencience for string dumping.
555  */
556 void ir_obst_printf(struct obstack *obst, const char *fmt, ...)
557 {
558         va_list args;
559         va_start(args, fmt);
560         ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
561         va_end(args);
562 }
563
564 void ir_vprintf(const char *fmt, va_list args)
565 {
566         ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
567 }
568
569 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
570 {
571         ir_common_vprintf(&file_appender, f, 0, fmt, args);
572 }
573
574 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
575 {
576         ir_common_vprintf(&str_appender, buf, len, fmt, args);
577 }
578
579 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
580 {
581         ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
582 }