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