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