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