Added index -> node map to irgs
[libfirm] / ir / ir / irreflect.h
1 /**
2  * @file irreflect.h
3  * @date 9.9.2004
4  * @author Sebastian Hack
5  * @brief Reflection for Firm operations.
6  *
7  * $Id$
8  */
9
10 #ifndef _FIRM_REFLECT_H
11 #define _FIRM_REFLECT_H
12
13 #include <limits.h>
14
15 #include "irop.h"
16 #include "irnode.h"
17
18 #define RFLCT_MC(m) rflct_ms_ ## m
19 typedef enum {
20   RFLCT_MC(None)  = 0,
21   RFLCT_MC(Mem)   = 2,
22   RFLCT_MC(Bool)  = 4,
23   RFLCT_MC(IntS)  = 8,
24   RFLCT_MC(IntU)  = 16,
25   RFLCT_MC(Float) = 32,
26   RFLCT_MC(Ref)   = 64,
27   RFLCT_MC(Char)  = 128,
28   RFLCT_MC(X)     = 256,
29   RFLCT_MC(BB)    = 512,
30   RFLCT_MC(Cf)    = RFLCT_MC(X) | RFLCT_MC(BB),
31
32   RFLCT_MC(Int) = RFLCT_MC(IntS) | RFLCT_MC(IntU),
33   RFLCT_MC(Intb) = RFLCT_MC(Int) | RFLCT_MC(Bool),
34   RFLCT_MC(Num) = RFLCT_MC(Int) | RFLCT_MC(Float),
35   RFLCT_MC(NumP) = RFLCT_MC(Num) | RFLCT_MC(Ref),
36   RFLCT_MC(Data) = RFLCT_MC(NumP) | RFLCT_MC(Char),
37   RFLCT_MC(Datab) = RFLCT_MC(Data) | RFLCT_MC(Bool),
38   RFLCT_MC(DataM) = RFLCT_MC(Data) | RFLCT_MC(Mem),
39   RFLCT_MC(DataMX) = RFLCT_MC(Data) | RFLCT_MC(Mem) | RFLCT_MC(X),
40   RFLCT_MC(Lh) = RFLCT_MC(Mem) | RFLCT_MC(BB),
41
42   RFLCT_MC(Any) = -1
43
44 } rflct_mode_class_t;
45
46 typedef struct _rflct_arg_t {
47   const char *name;  /**< The name of the argument (just a description). */
48
49   int is_variadic; /**< non-zero, if this argument can have multiple parameters. */
50   rflct_mode_class_t accepted_modes; /**< The set of accepted modes. */
51
52   int mode_equals; /**< If not variadic: You can specify the index of
53                         another argument meaning, that the mode of the
54                         operand binding at this argument must be the same
55                         as the mode of the operand binding to the argument
56                         at index. If you don't want to express such a
57                         dependency, just give -1 here.
58
59                         If variadic: If true, the modes of all
60                         variadic operands binding to this argument
61                         must be the same. If false, they can differ. */
62 } rflct_arg_t;
63
64 typedef struct _rflct_sig_t {
65         int defs; /**< The number of defined arguments in the signature. */
66         int uses; /**< The number of used arguments in the signature. */
67
68         rflct_arg_t *args; /**< The arguments array. */
69 } rflct_sig_t;
70
71 #define RFLCT_ARG_IS_A(arg,modes) (((arg)->accepted_modes & modes) != 0)
72 #define RFLCT_ARG_VALID(arg) ((arg)->name != NULL)
73 #define RFLCT_SIG_VALID(sig) ((sig) != INT_MAX)
74
75 /**
76  * Get the mode class for an IR mode.
77  * @param mode An IR mode.
78  * @return The corresponding smallest reflection mode class.
79  */
80 rflct_mode_class_t rflct_get_mode_class(const ir_mode *mode);
81
82 /**
83  * Get the number of signatures for a Firm opcode.
84  * @param opc The opcode.
85  * @return The number of signatures for this opcode.
86  */
87 int rflct_get_signature_count(opcode opc);
88
89 /**
90  * Try to get the signature, that matches to a given instance
91  * of a Firm node.
92  * @param irn The node.
93  * @return The first matching signature or -1, if no signature matches.
94  */
95 int rflct_get_signature(const ir_node *irn);
96
97 /**
98  * Get the number of in arguments.
99  * An in argument is a use of a value.
100  * @param opc The opcode.
101  * @param sig The signature you are refering to.
102  * @return The number of arguments.
103  */
104 int rflct_get_in_args_count(opcode opc, int sig);
105
106 /**
107  * Get the number of out arguments.
108  * An out argument is a def of a value.
109  * @param opc The opcode.
110  * @param sig The signature you are refering to.
111  * @return The number of arguments.
112  */
113 int rflct_get_out_args_count(opcode opc, int sig);
114
115 #define rflct_get_args_count(opc, sig, use) \
116   ((use) ? rflct_get_in_args_count(opc, sig) : rflct_get_out_args_count(opc, sig))
117
118 /**
119  * Get the array of use args.
120  * The array is terminated with an entry for which
121  * <code>RFLCT_ARG_VALID</code> is 0.
122  * @param opc The opcode.
123  * @param sig The signature you are referring to (Must be between
124  * 0 and the signature count).
125  * @return The array.
126  */
127 const rflct_arg_t *rflct_get_in_args(opcode opc, int sig);
128
129 /**
130  * Get the array of def args.
131  * The array is terminated with an entry for which
132  * <code>RFLCT_ARG_VALID</code> is 0.
133  * @param opc The opcode.
134  * @param sig The signature you are referring to (Must be between
135  * 0 and the signature count).
136  * @return The array.
137  */
138 const rflct_arg_t *rflct_get_out_args(opcode opc, int sig);
139
140 #define rflct_get_args(opc, sig, use) \
141   ((use) ? rflct_get_in_args(opc, sig) : rflct_get_out_args(opc, sig))
142
143 /**
144  * Make a string representation of a signature of an opcode.
145  * @param buf The buffer to put the string to.
146  * @param n The size of buf.
147  * @param opc The opcode.
148  * @param sig The signature.
149  * @return buf.
150  */
151 char *rflct_to_string(char *buf, int n, opcode opc, int sig);
152
153 /**
154  * Get a string representation of a mode class.
155  * @param str The buffer to put the string to.
156  * @param n The size of the buffer.
157  * @param mc The mode class.
158  * @return str.
159  */
160 char *rflct_mode_class_name(char *str, int n, rflct_mode_class_t mc);
161
162 /**
163  * Create a new opcode.
164  * @param opc         The Firm opcode.
165  * @param name        A name.
166  * @param commutative non-zero, if the opcode is commutative.
167  */
168 void rflct_new_opcode(opcode opc, const char *name, int commutative);
169
170 /**
171  * Add a signature to the opcode.
172  * @param opc  The opcode.
173  * @param sig  The signature.
174  * @return non-zero, if the signature was added successfully, false if no
175  * more signatures can be added to the opcode.
176  */
177 int rflct_opcode_add_signature(opcode opc, rflct_sig_t *sig);
178
179 /**
180  * Allocate a new signature.
181  * @param defs Number of def arguments.
182  * @param uses Number of use arguments.
183  * @return An allocated signature.
184  */
185 rflct_sig_t *rflct_signature_allocate(int defs, int uses);
186
187 /**
188  * Set an argument in a signature.
189  *
190  * @param sig         The signature.
191  * @param is_use      non-zero, if the argument is a use, else it is
192  *                    considered a def.
193  * @param num         The index of the argument.
194  * @param name        The name of the argument.
195  * @param mc          The mode class of the argument.
196  * @param is_variadic non-zero, if the argument is variadic.
197  * @param mode_equals This variable has following meaning: If the
198  * argument is variadic, a 1 indicates that all operands binding to this
199  * argument must have the same mode. A 0 indicates, that their mode must
200  * be of the specified mode class but can differ. If the argument is non
201  * variadic, a positive number indicates, that the mode of the operand
202  * binding to this argument must be the same as the one binding to
203  * argument indexed by mode_equals. If the mode should not be dependent
204  * to another mode, set -1 here.
205  * @return The index of the argument. Only use this index in mode_equals
206  * parameters of other arguments.
207  */
208 int rflct_signature_set_arg(rflct_sig_t *sig, int is_use, int num,
209                 const char *name, rflct_mode_class_t mc, int is_variadic, int mode_equals);
210
211 /**
212  * Get the arguments array index for an argument.
213  * @param sig    The signature.
214  * @param is_use non-zero, if the argument indicates a use or def argument.
215  * @param num    The number of the argument.
216  * @return The index into the arguments array.
217  */
218 int rflct_signature_get_index(const rflct_sig_t *sig, int is_use, int num);
219
220
221 #endif /* _FIRM_REFLECT_H */