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