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