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