added %= for printing a pn_Cmp value
[libfirm] / ir / ir / irprintf.c
index 96ded1a..1a27640 100644 (file)
@@ -24,6 +24,9 @@
 #ifdef HAVE_STRING_H
 #include <string.h>
 #endif
+#ifdef HAVE_INTTYPES_H
+#include <inttypes.h>
+#endif
 
 #include <stdlib.h>
 #include <stdio.h>
 
 #include <ctype.h>
 
+#include "firm_config.h"
 #include "ident.h"
 #include "irmode_t.h"
 #include "irnode_t.h"
 #include "entity_t.h"
-#include "tv.h"
+#include "type_t.h"
+#include "tv_t.h"
 #include "irprintf.h"
+#include "obst.h"
 #include "pset.h"
 #include "iterator.h"
 #include "bitset.h"
 
+#define STRNIL "(nil)"
 
 /**
- * append a char to a string buffer
+ * Init the string.
+ */
+static void str_init(void *object, size_t n)
+{
+       strcpy(object, "");
+}
+
+/**
+ * append a char to a string buffer.
  */
 static void str_append_char(void *object, size_t n, char ch)
 {
@@ -56,15 +71,23 @@ static void str_append_char(void *object, size_t n, char ch)
 }
 
 /**
- * append a string to a string buffer
+ * append a string to a string buffer.
  */
 static void str_append_str(void *object, size_t n, const char *str)
 {
        strncat(object, str, n);
 }
 
+
 /**
- * append a char to a file
+ * Init the file. i.e. do nothing.
+ */
+static void file_init(void *object, size_t n)
+{
+}
+
+/**
+ * append a char to a file.
  */
 static void file_append_char(void *object, size_t n, char ch)
 {
@@ -72,17 +95,44 @@ static void file_append_char(void *object, size_t n, char ch)
 }
 
 /**
- * append a string to a file
+ * append a string to a file.
  */
 static void file_append_str(void *object, size_t n, const char *str)
 {
        fputs(str, object);
 }
 
+/**
+ * Init the obstack. i.e. do nothing.
+ */
+static void obst_init(void *object, size_t n)
+{
+}
+
+/**
+ * append a char to a obstack.
+ */
+static void obst_append_char(void *object, size_t n, char ch)
+{
+       struct obstack *obst = object;
+       obstack_1grow(obst, ch);
+}
+
+/**
+ * append a string to a obstack.
+ */
+static void obst_append_str(void *object, size_t n, const char *str)
+{
+       struct obstack *obst = object;
+       obstack_grow(obst, str, strlen(str));
+}
+
+
 /**
  * the file appender
  */
 static const appender_t file_appender = {
+       file_init,
        file_append_char,
        file_append_str
 };
@@ -91,10 +141,22 @@ static const appender_t file_appender = {
  * the string buffer appender
  */
 static const appender_t str_appender = {
+       str_init,
        str_append_char,
        str_append_str
 };
 
+/**
+ * the obstack appender.
+ */
+static const appender_t obst_appender = {
+       obst_init,
+       obst_append_char,
+       obst_append_str
+};
+
+#ifndef WITH_LIBCORE
+
 static void ir_common_vprintf(const appender_t *app, void *object,
                size_t limit, const char *fmt, va_list args);
 
@@ -151,18 +213,27 @@ struct settings {
        int alternate;
 };
 
+/* Length specifiers. */
+enum {
+       len_char,
+       len_short,
+       len_int,
+       len_long,
+       len_long_long
+};
+
+
 #define MIN(x,y) ((x) < (y) ? (x) : (y))
 #define MAX(x,y) ((x) > (y) ? (x) : (y))
 
-
 static void dump_with_settings(const appender_t *app, void *object, size_t limit,
                const struct settings *settings, const char *str)
 {
        if(settings->width >= 0) {
                int i;
                size_t n = strlen(str);
-               int lim = MIN(settings->width, limit);
-               int to_print = MIN(lim, n);
+               int lim = MIN(settings->width, (int)limit);
+               int to_print = MIN(lim, (int)n);
                int to_pad = to_print - lim;
 
                if(!settings->left_just)
@@ -181,17 +252,6 @@ static void dump_with_settings(const appender_t *app, void *object, size_t limit
 }
 
 
-
-/* Length specifiers. */
-enum {
-       len_char,
-       len_short,
-       len_int,
-       len_long,
-       len_long_long
-};
-
-
 /**
  * A small printf helper routine for ir nodes.
  * @param app An appender (this determines where the stuff is dumped
@@ -211,6 +271,8 @@ static void ir_common_vprintf(const appender_t *app, void *object,
 #define DUMP_STR(s) app->append_str(object, limit, s)
 #define DUMP_CH(ch) app->append_char(object, limit, ch)
 
+       app->init(object, limit);
+
        for(i = 0, n = strlen(fmt); i < n; ++i) {
                char ch = fmt[i];
 
@@ -303,6 +365,17 @@ static void ir_common_vprintf(const appender_t *app, void *object,
                                        buf[1] = '\0';
                                        break;
 
+                               /* Indent */
+                               case '>':
+                                       {
+                                               int i, n = va_arg(args, int);
+                                               for(i = 0; i < n && i < sizeof(buf) - 1; ++i)
+                                                       buf[i] = ' ';
+
+                                               buf[i] = '\0';
+                                       }
+                                       break;
+
                                case 'c':
                                        buf[0] = va_arg(args, int);
                                        buf[1] = '\0';
@@ -343,7 +416,7 @@ static void ir_common_vprintf(const appender_t *app, void *object,
 
                                                        case len_long_long:
                                                                {
-                                                                       long long arg = va_arg(args, long long);
+                                                                       int64_t arg = va_arg(args, int64_t);
                                                                        snprintf(buf, sizeof(buf), fmt_str, arg);
                                                                }
                                                                break;
@@ -355,6 +428,10 @@ static void ir_common_vprintf(const appender_t *app, void *object,
                                        str = get_id_str(va_arg(args, ident *));
                                        break;
 
+                               case 't':
+                                       str = get_type_name(va_arg(args, type *));
+                                       break;
+
                                case 'e':
                                        str = get_entity_name(va_arg(args, entity *));
                                        break;
@@ -363,15 +440,25 @@ static void ir_common_vprintf(const appender_t *app, void *object,
                                        str = get_entity_ld_name(va_arg(args, entity *));
                                        break;
 
-                               case 't':
+                               case 'T':
                                        tarval_snprintf(buf, sizeof(buf), va_arg(args, tarval *));
                                        break;
 
                                case 'n':
                                        {
                                                ir_node *irn = va_arg(args, ir_node *);
-                                               snprintf(buf, sizeof(buf), "%s%s:%ld",
-                                                               get_irn_opname(irn), get_mode_name(get_irn_mode(irn)), get_irn_node_nr(irn));
+                                               if (irn)
+                                                       if (is_Const(irn)) {
+                                                               char tbuf[128];
+                                                               tarval_snprintf(tbuf, sizeof(tbuf), get_Const_tarval(irn));
+                                                               snprintf(buf, sizeof(buf), "%s%s<%s>:%ld",
+                                                                       get_irn_opname(irn), get_mode_name(get_irn_mode(irn)), tbuf, get_irn_node_nr(irn));
+                                                        }
+                                                        else
+                                                               snprintf(buf, sizeof(buf), "%s%s:%ld",
+                                                                       get_irn_opname(irn), get_mode_name(get_irn_mode(irn)), get_irn_node_nr(irn));
+                                               else
+                                                       strncpy(buf, STRNIL, sizeof(buf));
                                        }
                                        break;
 
@@ -442,6 +529,10 @@ static void ir_common_vprintf(const appender_t *app, void *object,
                                        /* clean the buffer again */
                                        buf[0] = '\0';
                                        break;
+
+                               case '=':
+                                       str = get_pnc_string(va_arg(args, int));
+                                       break;
                        }
 
                        dump_with_settings(app, object, limit, &settings, str);
@@ -456,7 +547,7 @@ static void ir_common_vprintf(const appender_t *app, void *object,
 }
 
 /**
- * Convencience for stdout dumping.
+ * Convenience for stdout dumping.
  */
 void ir_printf(const char *fmt, ...)
 {
@@ -467,7 +558,7 @@ void ir_printf(const char *fmt, ...)
 }
 
 /**
- * Convencience for file dumping.
+ * Convenience for file dumping.
  */
 void ir_fprintf(FILE *f, const char *fmt, ...)
 {
@@ -478,7 +569,7 @@ void ir_fprintf(FILE *f, const char *fmt, ...)
 }
 
 /**
- * Convencience for string dumping.
+ * Convenience for string dumping.
  */
 void ir_snprintf(char *buf, size_t len, const char *fmt, ...)
 {
@@ -487,3 +578,87 @@ void ir_snprintf(char *buf, size_t len, const char *fmt, ...)
        ir_common_vprintf(&str_appender, buf, len, fmt, args);
        va_end(args);
 }
+
+/**
+ * Convenience for string dumping.
+ */
+void ir_obst_printf(struct obstack *obst, const char *fmt, ...)
+{
+       va_list args;
+       va_start(args, fmt);
+       ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
+       va_end(args);
+}
+
+void ir_vprintf(const char *fmt, va_list args)
+{
+       ir_common_vprintf(&file_appender, stdout, 0, fmt, args);
+}
+
+void ir_vfprintf(FILE *f, const char *fmt, va_list args)
+{
+       ir_common_vprintf(&file_appender, f, 0, fmt, args);
+}
+
+void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
+{
+       ir_common_vprintf(&str_appender, buf, len, fmt, args);
+}
+
+void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
+{
+       ir_common_vprintf(&obst_appender, obst, 0, fmt, args);
+}
+
+#else /* WITH_LIBCORE */
+
+#include "irargs_t.h"
+
+void ir_printf(const char *fmt, ...)
+{
+       va_list args;
+
+       va_start(args, fmt);
+       lc_evprintf(firm_get_arg_env(), fmt, args);
+       va_end(args);
+}
+
+void ir_fprintf(FILE *f, const char *fmt, ...)
+{
+       va_list args;
+
+       va_start(args, fmt);
+       lc_evfprintf(firm_get_arg_env(), f, fmt, args);
+       va_end(args);
+}
+
+void ir_snprintf(char *buf, size_t n, const char *fmt, ...)
+{
+       va_list args;
+
+       va_start(args, fmt);
+       lc_evsnprintf(firm_get_arg_env(), buf, n, fmt, args);
+       va_end(args);
+}
+
+void ir_vprintf(const char *fmt, va_list args)
+{
+       lc_evprintf(firm_get_arg_env(), fmt, args);
+}
+
+void ir_vfprintf(FILE *f, const char *fmt, va_list args)
+{
+       lc_evfprintf(firm_get_arg_env(), f, fmt, args);
+}
+
+void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
+{
+       lc_evsnprintf(firm_get_arg_env(), buf, len, fmt, args);
+}
+
+void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
+{
+       lc_evoprintf(firm_get_arg_env(), obst, fmt, args);
+}
+
+#endif