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