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