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