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