d4e1de810383411f92023cf8d7e6e6a97f76bc6a
[libfirm] / ir / ir / irargs.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irargs.c
4  * Purpose:     Support for libcore IR object output.
5  * Author:      Sebastian Hack
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2005 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #include <ctype.h>
18 #include <libcore/xprintf.h>
19
20 #include "firm_common.h"
21 #include "irnode.h"
22 #include "entity.h"
23 #include "irloop.h"
24
25 /**
26  * identify a firm object type
27  */
28 static int firm_get_arg_type(const arg_occ_t *occ) {
29   /* Firm objects are always pointer */
30   return arg_type_ptr;
31 }
32
33 /**
34  * emit a Firm object
35  */
36 static int firm_emit(appendable_t *app, const arg_occ_t *occ, const arg_value_t *arg)
37 {
38 #define A(s)    occ->hash ? s " ": ""
39
40   void *X = arg->v_ptr;
41   firm_kind *obj = X;
42
43   int i, n;
44   ir_node *block;
45   char buf[256];
46   char tv[256];
47
48   buf[0] = '\0';
49
50   if (! X)
51     strncpy(buf, "(null)", sizeof(buf));
52   else {
53     switch (*obj) {
54     case k_BAD:
55       snprintf(buf, sizeof(buf), "BAD[%p]", X);
56       break;
57     case k_entity:
58       snprintf(buf, sizeof(buf), "%s%s[%ld]", A("ent"),
59           isupper(occ->conversion) ? get_entity_ld_name(X): get_entity_name(X),
60           get_entity_nr(X));
61       break;
62     case k_type:
63       snprintf(buf, sizeof(buf), "%s%s:%s[%ld]", A("type"), get_type_tpop_name(X), get_type_name(X), get_type_nr(X));
64       break;
65     case k_ir_graph:
66       snprintf(buf, sizeof(buf), "%s%s[%ld]", A("irg"), get_entity_name(get_irg_entity(X)), get_irg_graph_nr(X));
67       break;
68     case k_ir_node:
69       switch (occ->conversion) {
70       case 'B':
71         block = is_no_Block(X) ? get_nodes_block(X) : X;
72         snprintf(buf, sizeof(buf), "%s%s%s[%ld]", A("irn"), get_irn_opname(block),
73             get_mode_name(get_irn_mode(block)), get_irn_node_nr(block));
74         break;
75       case 'N':
76         snprintf(buf, sizeof(buf), "%ld", get_irn_node_nr(X));
77         break;
78       default:
79         snprintf(buf, sizeof(buf), "%s%s%s[%ld]", A("irn"), get_irn_opname(X),
80             get_mode_name(get_irn_mode(X)), get_irn_node_nr(X));
81       }
82       break;
83     case k_ir_mode:
84       snprintf(buf, sizeof(buf), "%s%s", A("mode"), get_mode_name(X));
85       break;
86     case k_tarval:
87       tarval_snprintf(tv, sizeof(tv), X);
88       snprintf(buf, sizeof(buf), "%s%s", A("tv"), tv);
89       break;
90     case k_ir_loop:
91       snprintf(buf, sizeof(buf), "ldepth[%d]", get_loop_depth(X));
92       break;
93     case k_ir_op:
94       snprintf(buf, sizeof(buf), "%s%s", A("op"), get_op_name(X));
95       break;
96     case k_ir_compound_graph_path:
97       strncat(buf, A("cgp"), sizeof(buf));
98
99       n = get_compound_graph_path_length(X);
100       for (i = 0; i < n; ++i) {
101         entity *ent = get_compound_graph_path_node(X, i);
102         strncat(buf, get_entity_name(ent), sizeof(buf));
103         if (i < n - 1)
104           strncat(buf, ".", sizeof(buf));
105       }
106       break;
107     default:
108       snprintf(buf, sizeof(buf), "UNKWN[%p]", X);
109     }
110   }
111   return arg_append(app, occ, buf, strlen(buf));
112
113 #undef A
114 }
115
116 /**
117  * emit an ident
118  */
119 static int firm_emit_ident(appendable_t *app, const arg_occ_t *occ, const arg_value_t *arg)
120 {
121   ident *id = (ident *)arg->v_ptr;
122   const char *p = id ? get_id_str(id) : "(null)";
123
124   return arg_append(app, occ, p, strlen(p));
125 }
126
127
128 arg_env_t *firm_get_arg_env(void)
129 {
130 #define X(name, letter) {"firm:" name, letter}
131
132   static arg_env_t *env = NULL;
133
134   static arg_handler_t firm_handler  = { firm_get_arg_type, firm_emit };
135   static arg_handler_t ident_handler = { firm_get_arg_type, firm_emit_ident };
136
137   static struct {
138     const char *name;
139     char letter;
140   } args[] = {
141     X("type",      't'),
142     X("entity",    'e'),
143     X("entity_ld", 'E'),
144     X("tarval",    'T'),
145     X("irn",       'n'),
146     X("op",        'O'),
147     X("irn_nr",    'N'),
148     X("mode",      'm'),
149     X("block",     'B'),
150   };
151
152   int i;
153
154   if(env == NULL) {
155     env = arg_new_env();
156     arg_add_std(env);
157
158     for (i = 0; i < sizeof(args)/sizeof(args[0]); ++i)
159       arg_register(env, args[i].name, args[i].letter, &firm_handler);
160
161     arg_register(env, "ident", 'I', &ident_handler);
162   }
163
164   return env;
165 }