2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Reflection for Firm operations.
23 * @author Sebastian Hack
27 #ifndef FIRM_IR_REFLECT_H
28 #define FIRM_IR_REFLECT_H
32 #include "firm_types.h"
35 #define RFLCT_MC(m) rflct_ms_ ## m
47 RFLCT_MC(Cf) = RFLCT_MC(X) | RFLCT_MC(BB),
49 RFLCT_MC(Int) = RFLCT_MC(IntS) | RFLCT_MC(IntU),
50 RFLCT_MC(Intb) = RFLCT_MC(Int) | RFLCT_MC(Bool),
51 RFLCT_MC(Num) = RFLCT_MC(Int) | RFLCT_MC(Float),
52 RFLCT_MC(NumP) = RFLCT_MC(Num) | RFLCT_MC(Ref),
53 RFLCT_MC(Data) = RFLCT_MC(NumP) | RFLCT_MC(Char),
54 RFLCT_MC(Datab) = RFLCT_MC(Data) | RFLCT_MC(Bool),
55 RFLCT_MC(DataM) = RFLCT_MC(Data) | RFLCT_MC(Mem),
56 RFLCT_MC(DataMX) = RFLCT_MC(Data) | RFLCT_MC(Mem) | RFLCT_MC(X),
57 RFLCT_MC(Lh) = RFLCT_MC(Mem) | RFLCT_MC(BB),
63 typedef struct _rflct_arg_t {
64 const char *name; /**< The name of the argument (just a description). */
66 int is_variadic; /**< non-zero, if this argument can have multiple parameters. */
67 rflct_mode_class_t accepted_modes; /**< The set of accepted modes. */
69 int mode_equals; /**< If not variadic: You can specify the index of
70 another argument meaning, that the mode of the
71 operand binding at this argument must be the same
72 as the mode of the operand binding to the argument
73 at index. If you don't want to express such a
74 dependency, just give -1 here.
76 If variadic: If true, the modes of all
77 variadic operands binding to this argument
78 must be the same. If false, they can differ. */
81 typedef struct _rflct_sig_t {
82 int defs; /**< The number of defined arguments in the signature. */
83 int uses; /**< The number of used arguments in the signature. */
85 rflct_arg_t *args; /**< The arguments array. */
88 #define RFLCT_ARG_IS_A(arg,modes) (((arg)->accepted_modes & modes) != 0)
89 #define RFLCT_ARG_VALID(arg) ((arg)->name != NULL)
90 #define RFLCT_SIG_VALID(sig) ((sig) != INT_MAX)
93 * Get the mode class for an IR mode.
94 * @param mode An IR mode.
95 * @return The corresponding smallest reflection mode class.
97 rflct_mode_class_t rflct_get_mode_class(const ir_mode *mode);
100 * Get the number of signatures for a Firm opcode.
101 * @param opc The opcode.
102 * @return The number of signatures for this opcode.
104 int rflct_get_signature_count(ir_opcode opc);
107 * Try to get the signature, that matches to a given instance
109 * @param irn The node.
110 * @return The first matching signature or -1, if no signature matches.
112 int rflct_get_signature(const ir_node *irn);
115 * Get the number of in arguments.
116 * An in argument is a use of a value.
117 * @param opc The opcode.
118 * @param sig The signature you are refering to.
119 * @return The number of arguments.
121 int rflct_get_in_args_count(ir_opcode opc, int sig);
124 * Get the number of out arguments.
125 * An out argument is a def of a value.
126 * @param opc The opcode.
127 * @param sig The signature you are refering to.
128 * @return The number of arguments.
130 int rflct_get_out_args_count(ir_opcode opc, int sig);
132 #define rflct_get_args_count(opc, sig, use) \
133 ((use) ? rflct_get_in_args_count(opc, sig) : rflct_get_out_args_count(opc, sig))
136 * Get the array of use args.
137 * The array is terminated with an entry for which
138 * <code>RFLCT_ARG_VALID</code> is 0.
139 * @param opc The opcode.
140 * @param sig The signature you are referring to (Must be between
141 * 0 and the signature count).
144 const rflct_arg_t *rflct_get_in_args(ir_opcode opc, int sig);
147 * Get the array of def args.
148 * The array is terminated with an entry for which
149 * <code>RFLCT_ARG_VALID</code> is 0.
150 * @param opc The opcode.
151 * @param sig The signature you are referring to (Must be between
152 * 0 and the signature count).
155 const rflct_arg_t *rflct_get_out_args(ir_opcode opc, int sig);
157 #define rflct_get_args(opc, sig, use) \
158 ((use) ? rflct_get_in_args(opc, sig) : rflct_get_out_args(opc, sig))
161 * Make a string representation of a signature of an opcode.
162 * @param buf The buffer to put the string to.
163 * @param n The size of buf.
164 * @param opc The opcode.
165 * @param sig The signature.
168 char *rflct_to_string(char *buf, int n, ir_opcode opc, int sig);
171 * Get a string representation of a mode class.
172 * @param str The buffer to put the string to.
173 * @param n The size of the buffer.
174 * @param mc The mode class.
177 char *rflct_mode_class_name(char *str, int n, rflct_mode_class_t mc);
180 * Create a new opcode.
181 * @param opc The Firm opcode.
182 * @param name A name.
183 * @param commutative non-zero, if the opcode is commutative.
185 void rflct_new_opcode(ir_opcode opc, const char *name, int commutative);
188 * Add a signature to the opcode.
189 * @param opc The opcode.
190 * @param sig The signature.
191 * @return non-zero, if the signature was added successfully, false if no
192 * more signatures can be added to the opcode.
194 int rflct_opcode_add_signature(ir_opcode opc, rflct_sig_t *sig);
197 * Allocate a new signature.
198 * @param defs Number of def arguments.
199 * @param uses Number of use arguments.
200 * @return An allocated signature.
202 rflct_sig_t *rflct_signature_allocate(int defs, int uses);
205 * Set an argument in a signature.
207 * @param sig The signature.
208 * @param is_use non-zero, if the argument is a use, else it is
210 * @param num The index of the argument.
211 * @param name The name of the argument.
212 * @param mc The mode class of the argument.
213 * @param is_variadic non-zero, if the argument is variadic.
214 * @param mode_equals This variable has following meaning: If the
215 * argument is variadic, a 1 indicates that all operands binding to this
216 * argument must have the same mode. A 0 indicates, that their mode must
217 * be of the specified mode class but can differ. If the argument is non
218 * variadic, a positive number indicates, that the mode of the operand
219 * binding to this argument must be the same as the one binding to
220 * argument indexed by mode_equals. If the mode should not be dependent
221 * to another mode, set -1 here.
222 * @return The index of the argument. Only use this index in mode_equals
223 * parameters of other arguments.
225 int rflct_signature_set_arg(rflct_sig_t *sig, int is_use, int num,
226 const char *name, rflct_mode_class_t mc, int is_variadic, int mode_equals);
229 * Get the arguments array index for an argument.
230 * @param sig The signature.
231 * @param is_use non-zero, if the argument indicates a use or def argument.
232 * @param num The number of the argument.
233 * @return The index into the arguments array.
235 int rflct_signature_get_index(const rflct_sig_t *sig, int is_use, int num);