Allow the Bad node to be set in set_store(). This is neccessary, because Bad might...
[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-2006 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_buf[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       if (X == get_const_code_irg())
301         snprintf(buf, buflen, "const_code_irg ");
302       else
303         snprintf(buf, buflen, "%s%s", A("irg"), get_entity_name(get_irg_entity(X)));
304       snprintf(add, sizeof(add), "[%ld]", get_irg_graph_nr(X));
305       break;
306     case k_ir_node:
307       switch (conversion) {
308       case 'B':
309         block = is_no_Block(X) ? get_nodes_block(X) : X;
310         snprintf(buf, buflen, "%s%s%s", A("irn"), get_irn_opname(block),
311             get_mode_name(get_irn_mode(block)));
312         snprintf(add, sizeof(add), "[%ld]", get_irn_node_nr(block));
313         break;
314       case 'N':
315         snprintf(buf, buflen, "%ld", get_irn_node_nr(X));
316         break;
317       default:
318         if (is_Const(X)) {
319           tarval *tv = get_Const_tarval(X);
320
321           if (tv)
322             tarval_snprintf(tv_buf, sizeof(tv_buf), tv);
323           snprintf(buf, buflen, "%s%s%s<%s>", A("irn"), get_irn_opname(X),
324             get_mode_name(get_irn_mode(X)), tv ? tv_buf : ">NULL<");
325         }
326         else if (get_irn_op(X) == op_SymConst) {
327           switch (get_SymConst_kind(X)) {
328           case symconst_type_tag:    /* type tag */
329             snprintf(tv_buf, sizeof(tv_buf), "<ID:%s>", get_type_name(get_SymConst_type(X)));
330             break;
331           case symconst_type_size:   /* type size */
332             snprintf(tv_buf, sizeof(tv_buf), "<SIZE:%s>", get_type_name(get_SymConst_type(X)));
333             break;
334           case symconst_type_align:  /* type alignment */
335             snprintf(tv_buf, sizeof(tv_buf), "<ALIGN:%s>", get_type_name(get_SymConst_type(X)));
336             break;
337           case symconst_addr_name:   /* linker name */
338             snprintf(tv_buf, sizeof(tv_buf), "<EXT:%s>", get_id_str(get_SymConst_name(X)));
339             break;
340           case symconst_addr_ent:    /* entity name */
341             snprintf(tv_buf, sizeof(tv_buf), "<%s>", get_entity_name(get_SymConst_entity(X)));
342             break;
343           case symconst_enum_const:  /* enumeration constant */
344             snprintf(tv_buf, sizeof(tv_buf), "<ENUM:%s>", get_enumeration_name(get_SymConst_enum(X)));
345             break;
346           default:
347             tv_buf[0] = '\0';
348           }
349           snprintf(buf, buflen, "%s%s%s%s", A("irn"), get_irn_opname(X),
350             get_mode_name(get_irn_mode(X)), tv_buf);
351         }
352         else
353           snprintf(buf, buflen, "%s%s%s", A("irn"), get_irn_opname(X),
354             get_mode_name(get_irn_mode(X)));
355         snprintf(add, sizeof(add), "[%ld:%d]", get_irn_node_nr(X), get_irn_idx(X));
356       }
357       break;
358     case k_ir_mode:
359       snprintf(buf, buflen, "%s%s", A("mode"), get_mode_name(X));
360       break;
361     case k_tarval:
362       tarval_snprintf(tv_buf, sizeof(tv_buf), X);
363       snprintf(buf, buflen, "%s%s", A("tv"), tv_buf);
364       break;
365     case k_ir_loop:
366       snprintf(buf, sizeof(buf), "loop[%d:%d]", get_loop_loop_nr(X), get_loop_depth(X));
367       break;
368     case k_ir_op:
369       snprintf(buf, buflen, "%s%s", A("op"), get_op_name(X));
370       break;
371     case k_ir_compound_graph_path:
372       n = get_compound_graph_path_length(X);
373
374       for (i = 0; i < n; ++i) {
375         ent = get_compound_graph_path_node(X, i);
376
377         strncat(buf, ".", buflen);
378         strncat(buf, get_entity_name(ent), buflen);
379         if (is_Array_type(get_entity_owner(ent))) {
380           snprintf(add, sizeof(add), "[%d]",
381             get_compound_graph_path_array_index(X, i));
382           strncat(buf, add, buflen);
383         }
384       }
385       add[0] = '\0';
386       break;
387
388     default:
389       snprintf(buf, buflen, "UNKWN");
390       snprintf(add, sizeof(add), "[%p]", X);
391     }
392   }
393
394   if (occ->flag_plus)
395         strncat(buf, add, buflen);
396
397 #undef A
398 }
399
400 /**
401  * A small printf helper routine for ir nodes.
402  * @param app An appender (this determines where the stuff is dumped to).
403  * @param object A target passed to the appender.
404  * @param limit The maximum number of characters to dump.
405  * @param fmt The format string.
406  * @param args A va_list.
407  */
408 static void ir_common_vprintf(const appender_t *app, void *object,
409                 size_t limit, const char *fmt, va_list args)
410 {
411         const char *str;
412         char buf[4096];
413         int i, n;
414
415 #define DUMP_STR(s) app->append_str(object, limit, s)
416 #define DUMP_CH(ch) app->append_char(object, limit, ch)
417
418         app->init(object, limit);
419
420         for (i = 0, n = strlen(fmt); i < n; ++i) {
421                 char ch = fmt[i];
422
423                 if (ch == '%') {
424                         int len;
425                         const char *len_str = "";
426
427                         struct settings settings;
428
429                         settings.flag_hash = 0;
430                         settings.flag_zero = ' ';
431                         settings.width = -1;
432                         settings.flag_minus = 0;
433                         settings.flag_plus  = 0;
434
435                         ch = fmt[++i];
436
437                         /* Clear the temporary buffer */
438                         buf[0] = '\0';
439
440                         /* Set the string to print to the buffer by default. */
441                         str = buf;
442
443                         while (strchr("#0-+", ch)) {
444                                 switch(ch) {
445                                         case '#':
446                                                 settings.flag_hash = 1;
447                                                 break;
448                                         case '0':
449                                                 settings.flag_zero = '0';
450                                                 break;
451                                         case '-':
452                                                 settings.flag_minus = 1;
453                                                 break;
454                                         case '+':
455                                                 settings.flag_plus = 1;
456                                                 break;
457                                 }
458
459                                 ch = fmt[++i];
460                         }
461
462
463                         /* Read the field width */
464                         {
465                                 char *endptr;
466                                 int increase;
467
468                                 settings.width = (int) strtol(&fmt[i], &endptr, 10);
469                                 increase = (char *) endptr - &fmt[i];
470                                 ch = fmt[i += increase];
471                                 if(increase == 0)
472                                         settings.width = -1;
473                         }
474
475                         /* Ignore the precision */
476                         if (ch == '.')
477                                 while(isdigit(ch = fmt[++i]));
478
479                         /* read the length modifier. */
480                         switch(ch) {
481                                 case 'h':
482                                         len_str = "h";
483                                         len = len_short;
484                                         ++i;
485                                         if((ch = fmt[i]) == 'h') {
486                                                 len_str = "hh";
487                                                 len = len_char;
488                                                 ++i;
489                                         }
490                                         break;
491
492                                 case 'l':
493                                         len_str = "l";
494                                         len = len_long;
495                                         ++i;
496                                         if ((ch = fmt[i]) == 'l') {
497                                                 len_str = "ll";
498                                                 len = len_long_long;
499                                                 ++i;
500                                         }
501                                         else if ((ch = fmt[i]) == 'u') {
502                                                 len_str = "lu";
503                                                 len = len_long_long;
504                                                 ++i;
505                                         }
506                                         break;
507
508                                 default:
509                                         len = len_int;
510                         }
511
512                         /* Do the conversion specifier. */
513                         switch (ch) {
514
515                                 /* The percent itself */
516                                 case '%':
517                                         buf[0] = '%';
518                                         buf[1] = '\0';
519                                         break;
520
521                                 /* Indent */
522                                 case '>':
523                                         {
524                                                 int i, n = va_arg(args, int);
525                                                 for(i = 0; i < n && i < sizeof(buf) - 1; ++i)
526                                                         buf[i] = ' ';
527
528                                                 buf[i] = '\0';
529                                         }
530                                         break;
531
532                                 case 'c':
533                                         buf[0] = va_arg(args, int);
534                                         buf[1] = '\0';
535                                         break;
536
537                                 case 's':
538                                         str = va_arg(args, const char *);
539                                         break;
540
541                                 case 'p':
542                                         snprintf(buf, sizeof(buf), "%p", va_arg(args, void *));
543                                         break;
544
545                                 case 'i':
546                                 case 'd':
547                                 case 'u':
548                                 case 'x':
549                                 case 'X':
550                                 case 'o':
551                                         {
552                                                 char fmt_str[16];
553                                                 snprintf(fmt_str, sizeof(fmt_str), "%%%s%c", len_str, ch);
554
555                                                 switch(len) {
556                                                         case len_char:
557                                                         case len_short:
558                                                         case len_int:
559                                                                 {
560                                                                         int arg = va_arg(args, int);
561                                                                         snprintf(buf, sizeof(buf), fmt_str, arg);
562                                                                 }
563                                                                 break;
564
565                                                         case len_long:
566                                                                 {
567                                                                         long arg = va_arg(args, long);
568                                                                         snprintf(buf, sizeof(buf), fmt_str, arg);
569                                                                 }
570                                                                 break;
571
572                                                         case len_long_long:
573                                                                 {
574                                                                         int64_t arg = va_arg(args, int64_t);
575                                                                         snprintf(buf, sizeof(buf), fmt_str, arg);
576                                                                 }
577                                                                 break;
578                                                 }
579                                         }
580                                         break;
581
582                                 case 'I':
583                                         str = get_id_str(va_arg(args, ident *));
584                                         break;
585
586                                 case 't':
587         case 'e':
588         case 'E':
589         case 'T':
590         case 'n':
591         case 'O':
592         case 'm':
593         case 'B':
594         case 'P':
595         case 'F':
596         case 'f':
597           firm_emit(buf, sizeof(buf), ch, &settings, va_arg(args, void *));
598                                         break;
599
600                                 case 'b':
601                                         {
602                                                 const bitset_t *bs = va_arg(args, const bitset_t *);
603                                                 const char *prefix = "";
604                                                 unsigned long i;
605
606                                                 DUMP_CH('[');
607                                                 for(i = bitset_next_set(bs, 0); i != -1; i = bitset_next_set(bs, i + 1)) {
608                                                         snprintf(buf, sizeof(buf), "%ld", i);
609                                                         DUMP_STR(prefix);
610                                                         DUMP_STR(buf);
611                                                         prefix = ", ";
612                                                 }
613                                                 DUMP_CH(']');
614                                                 buf[0] = '\0';
615                                         }
616                                         break;
617
618                                 case '*':
619                                         {
620                                                 iterator_t *it = va_arg(args, iterator_t *);
621                                                 void *collection = va_arg(args, void *);
622                                                 void *curr;
623                                                 const char *prefix = "";
624                                                 char format = fmt[++i];
625                                                 ir_printf_cb_t *cb = format == 'C' ? va_arg(args, ir_printf_cb_t *) : NULL;
626
627                                                 assert(is_iterator(it) && "Pass an iterator interface and the collection");
628
629                                                 snprintf(buf, sizeof(buf), "%%%c", format);
630
631                                                 DUMP_CH('[');
632                                                 for(curr = it->start(collection); curr; curr = it->next(collection, curr)) {
633                                                         DUMP_STR(prefix);
634
635                                                         if(cb)
636                                                                 cb(app, object, limit, curr);
637                                                         else
638                                                                 ir_common_printf(app, object, limit, buf, curr);
639
640                                                         prefix = ", ";
641                                                 }
642                                                 it->finish(collection, curr);
643
644                                                 DUMP_CH(']');
645                                         }
646
647                                         /* clean the buffer again */
648                                         buf[0] = '\0';
649                                         break;
650
651                                 case '=':
652                                         str = get_pnc_string(va_arg(args, int));
653                                         break;
654                                 case 'G':
655                                         {
656                                                 ir_node *irn = va_arg(args, ir_node *);
657                                                 dbg_info *dbg = get_irn_dbg_info(irn);
658                                                 buf[0] = '\0';
659                                                 if (dbg && __dbg_info_snprint) {
660                                                         if (__dbg_info_snprint(buf, sizeof(buf), dbg) <= 0)
661                                                                 buf[0] = '\0';
662                                                 }
663                                                 break;
664                                         }
665                         }
666
667                         dump_with_settings(app, object, limit, &settings, str);
668                 }
669                 else
670                         DUMP_CH(ch);
671         }
672
673 #undef DUMP_STR
674 #undef DUMP_CH
675 }
676
677 /**
678  * Convenience for stdout dumping.
679  */
680 void ir_printf(const char *fmt, ...)
681 {
682         va_list args;
683         va_start(args, fmt);
684         ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
685         va_end(args);
686 }
687
688 /**
689  * Convenience for file dumping.
690  */
691 void ir_fprintf(FILE *f, const char *fmt, ...)
692 {
693         va_list args;
694         va_start(args, fmt);
695         ir_common_vprintf(&file_appender, f, 0, fmt, args);
696         va_end(args);
697 }
698
699 /**
700  * Convenience for string dumping.
701  */
702 void ir_snprintf(char *buf, size_t len, const char *fmt, ...)
703 {
704         va_list args;
705         va_start(args, fmt);
706         ir_common_vprintf(&str_appender, buf, len, fmt, args);
707         va_end(args);
708 }
709
710 /**
711  * Convenience for string dumping.
712  */
713 void ir_obst_printf(struct obstack *obst, const char *fmt, ...)
714 {
715         va_list args;
716         va_start(args, fmt);
717         ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
718         va_end(args);
719 }
720
721 void ir_vprintf(const char *fmt, va_list args)
722 {
723         ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
724 }
725
726 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
727 {
728         ir_common_vprintf(&file_appender, f, 0, fmt, args);
729 }
730
731 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
732 {
733         ir_common_vprintf(&str_appender, buf, len, fmt, args);
734 }
735
736 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
737 {
738         ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
739 }
740
741 #else /* WITH_LIBCORE */
742
743 #include "irargs_t.h"
744
745 void ir_printf(const char *fmt, ...)
746 {
747         va_list args;
748
749         va_start(args, fmt);
750         lc_evprintf(firm_get_arg_env(), fmt, args);
751         va_end(args);
752 }
753
754 void ir_fprintf(FILE *f, const char *fmt, ...)
755 {
756         va_list args;
757
758         va_start(args, fmt);
759         lc_evfprintf(firm_get_arg_env(), f, fmt, args);
760         va_end(args);
761 }
762
763 void ir_snprintf(char *buf, size_t n, const char *fmt, ...)
764 {
765         va_list args;
766
767         va_start(args, fmt);
768         lc_evsnprintf(firm_get_arg_env(), buf, n, fmt, args);
769         va_end(args);
770 }
771
772 void ir_vprintf(const char *fmt, va_list args)
773 {
774         lc_evprintf(firm_get_arg_env(), fmt, args);
775 }
776
777 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
778 {
779         lc_evfprintf(firm_get_arg_env(), f, fmt, args);
780 }
781
782 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
783 {
784         lc_evsnprintf(firm_get_arg_env(), buf, len, fmt, args);
785 }
786
787 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
788 {
789         lc_evoprintf(firm_get_arg_env(), obst, fmt, args);
790 }
791
792 #endif