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