4 * @author Sebastian Hack
5 * @brief Reflection for Firm operands.
26 #include "irreflect.h"
28 #define obstack_grow_str(obst,s) obstack_grow((obst), (s), strlen((s)))
29 #define obstack_grow_str_const(obst,s) obstack_grow((obst), (s), sizeof((s)))
31 extern int obstack_printf(struct obstack *obst, const char *fmt, ...);
34 * Get the number of bits set in a word.
36 static INLINE int pop(int i) {
37 unsigned x = (unsigned) i;
38 x = ((x >> 1) & 0x55555555);
39 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
40 x = (x + (x >> 4)) & 0x0f0f0f0f;
43 return (int) (x & 0x3f);
47 * Get the number of bits differing in two variables.
49 static INLINE int dist(int x, int y) {
54 #define MAX_SIG_COUNT 8
55 #define MAX_ARG_COUNT 10
58 int num; /**< A sequential number (one opcode can have multiple signatures. */
59 rflct_arg_t args[]; /**< The signature. */
67 rflct_arg_t *sigs[MAX_SIG_COUNT];
70 static struct obstack obst;
72 static rflct_opcode_t **opcodes = NULL;
74 static int opcodes_size = 0;
76 static INLINE void assure_opcode_capacity(int opcode)
78 if(opcode >= opcodes_size) {
79 int new_size = 2 * opcode;
80 rflct_opcode_t **new_opcodes = xcalloc(new_size, sizeof(new_opcodes[0]));
83 memcpy(new_opcodes, opcodes, sizeof(*opcodes) * opcodes_size);
87 opcodes = new_opcodes;
88 opcodes_size = new_size;
94 #define OPCODES_COUNT (sizeof(opcodes) / sizeof(opcodes[0]))
96 #define OPCODES_COUNT opcodes_size
99 rflct_mode_class_t rflct_get_mode_class(const ir_mode *mode) {
100 mode_sort sort = get_mode_sort(mode);
104 case irms_control_flow:
107 else if(mode == mode_X)
110 return RFLCT_MC(Mem);
111 case irms_internal_boolean:
112 return RFLCT_MC(Bool);
113 case irms_int_number:
114 return mode_is_signed(mode) ? RFLCT_MC(IntS) : RFLCT_MC(IntU);
115 case irms_float_number:
116 return RFLCT_MC(Float);
118 return RFLCT_MC(Ref);
120 return RFLCT_MC(Char);
123 return RFLCT_MC(None);
126 static INLINE const rflct_opcode_t *get_opcode(opcode opc) {
127 assert(opc >= 0 && opc < OPCODES_COUNT && "Invalid opcode");
131 static INLINE const rflct_arg_t *get_args(opcode opc, int sig) {
132 const rflct_opcode_t *opcode = get_opcode(opc);
133 assert(sig >= 0 && sig < opcode->sig_count
134 && "Invalid signature");
135 return opcode->sigs[sig];
138 #define GET_OPCODE(opc) get_opcode(opc)
139 #define GET_ARGS(opc,args) get_args(opc, args)
141 int rflct_get_signature_count(opcode opc) {
142 const rflct_opcode_t *opcode = GET_OPCODE(opc);
143 return opcode->sig_count;
146 int rflct_get_in_args_count(opcode opc, int sig) {
147 const rflct_arg_t *args = GET_ARGS(opc, sig);
150 for(i = 0; args[i].name != NULL; i++);
151 for(res = 0, i += 1; args[i].name != NULL; res++, i++);
155 int rflct_get_out_args_count(opcode opc, int sig) {
156 const rflct_arg_t *args = GET_ARGS(opc, sig);
158 for(i = 0; args[i].name != NULL; i++);
163 const rflct_arg_t *rflct_get_in_args(opcode opc, int sig) {
164 const rflct_arg_t *args = GET_ARGS(opc, sig);
167 for(i = 0; args[i].name != NULL; i++);
171 const rflct_arg_t *rflct_get_out_args(opcode opc, int sig) {
172 return GET_ARGS(opc, sig);
175 int rflct_signature_match(const ir_node *irn, int sig) {
176 opcode op = get_irn_opcode(irn);
177 const rflct_arg_t *args = rflct_get_in_args(op, sig);
181 for(i = 0, j = -1; RFLCT_ARG_VALID(&args[i])
182 && j < get_irn_arity(irn); j++) {
184 ir_node *child = get_irn_n(irn, j);
185 const rflct_arg_t *arg = &args[i];
186 rflct_mode_class_t mc = rflct_get_mode_class(get_irn_mode(child));
188 if(arg->accepted_modes & mc)
189 dst += dist(arg->accepted_modes, mc);
193 if(!arg->is_variadic)
200 int rflct_get_signature(const ir_node *irn) {
201 const rflct_opcode_t *opc = GET_OPCODE(get_irn_opcode(irn));
202 int min_dist = INT_MAX;
203 int min_sig = INT_MAX;
206 for(i = 0; i < opc->sig_count; i++) {
207 int dist = rflct_signature_match(irn, i);
208 if(dist < min_dist) {
217 static const char *rflct_mode_class_atomic_name(rflct_mode_class_t mc) {
218 #define XXX(_mc) case RFLCT_MC(_mc): return #_mc
245 static void rflct_mode_class_comb_name_obst(struct obstack *obst,
246 rflct_mode_class_t mc) {
247 const char *res = rflct_mode_class_atomic_name(mc);
249 if(strlen(res) == 0) {
250 const char *prefix = "";
253 obstack_1grow(obst, '{');
254 for(mask = 1; mask != 0; mask <<= 1) {
256 const char *s = rflct_mode_class_atomic_name(mask);
257 obstack_grow_str(obst, s);
258 obstack_grow_str(obst, prefix);
262 obstack_1grow(obst, '}');
265 obstack_grow(obst, res, strlen(res));
268 char *rflct_mode_class_name(char *str, int n, rflct_mode_class_t mc) {
274 rflct_mode_class_comb_name_obst(&obst, mc);
275 obstack_1grow(&obst, 0);
276 res = obstack_finish(&obst);
278 strncpy(str, res, n);
280 obstack_free(&obst, NULL);
285 static void rflct_obstack_grow_args(struct obstack *obst,
286 const rflct_arg_t *args) {
287 const rflct_arg_t *arg;
288 const char *prefix = "";
290 for(arg = args; RFLCT_ARG_VALID(arg); arg++) {
291 obstack_grow_str(obst, prefix);
292 obstack_grow_str(obst, arg->name);
294 obstack_1grow(obst, '*');
295 obstack_1grow(obst, ':');
296 rflct_mode_class_comb_name_obst(obst, arg->accepted_modes);
302 char *rflct_to_string(char *buf, int n, opcode opc, int sig) {
305 const rflct_opcode_t *opcode = GET_OPCODE(opc);
309 obstack_1grow(&obst, '(');
310 rflct_obstack_grow_args(&obst, rflct_get_out_args(opc, sig));
312 obstack_grow_str(&obst, ") = ");
313 obstack_grow_str(&obst, opcode->name);
314 obstack_1grow(&obst, '(');
316 rflct_obstack_grow_args(&obst, rflct_get_in_args(opc, sig));
318 obstack_1grow(&obst, ')');
319 obstack_1grow(&obst, 0);
320 s = obstack_finish(&obst);
322 obstack_free(&obst, NULL);
327 #define NON_VARIADIC 0
330 #define ARG(name,modes) \
331 _ARG(name, modes, NON_VARIADIC, -1)
333 #define ARG_SAME(name,modes,mode_same) \
334 _ARG(name, modes, NON_VARIADIC, mode_same)
336 #define VARG(name,modes) \
337 _ARG(name, modes, VARIADIC, 0)
339 #define VARG_SAME(name,modes) \
340 _ARG(name, modes, VARIADIC, 1)
343 _ARG(NULL, None, NON_VARIADIC, -1)
346 _ARG(NULL, None, NON_VARIADIC, 0)
348 #define BLOCK ARG("Block", BB)
350 static void init_ops(void) {
353 rflct_opcode_t *opcode;
357 assure_opcode_capacity(iro_MaxOpcode);
360 #define BEGIN_OP(op) \
362 opcode = obstack_alloc(&obst, sizeof(*opcode)); \
363 opcode->opc = iro_ ## op; \
364 opcode->name = #op; \
365 opcodes[opcode->opc] = opcode;
370 #define _ARG(_name,_modes,_variadic,_mode_equals) \
374 args.accepted_modes = RFLCT_MC(_modes); \
375 args.is_variadic = _variadic; \
376 args.mode_equals = _mode_equals; \
377 obstack_grow(&obst, &args, sizeof(args)); \
383 assert(curr_sig < MAX_SIG_COUNT && "Mind the maximum number of signatures"); \
384 opcode->sigs[curr_sig++] = obstack_finish(&obst); \
385 opcode->sig_count = curr_sig; \
390 #include "irreflect.def"
399 #define _ARG(_name,_modes,_var,_me) \
401 arg->accepted_modes = RFLCT_MC(_modes); \
402 arg->is_variadic = _var; \
403 arg->mode_equals = _me;
405 void rflct_new_opcode(opcode opc, const char *name, int commutative)
407 rflct_opcode_t *ropc = obstack_alloc(&obst, sizeof(*ropc));
412 memset(ropc->sigs, 0, sizeof(ropc->sigs));
414 assure_opcode_capacity(opc);
418 int rflct_opcode_add_signature(opcode opc, rflct_sig_t *sig)
420 rflct_arg_t *args = sig->args;
421 rflct_opcode_t *op = opcodes[opc];
424 assert(op && "Illegal opcode");
426 for(i = 0; i < MAX_SIG_COUNT && op->sigs[i] != NULL; i++);
428 if(i >= MAX_SIG_COUNT)
431 op->sigs[op->sig_count++] = args;
438 rflct_sig_t *rflct_signature_allocate(int defs, int uses)
440 rflct_sig_t *sig = xmalloc(sizeof(*sig));
443 obstack_alloc(&obst, sizeof(*args) * (defs + uses + 2));
445 rflct_arg_t *arg = args + defs;
448 arg = args + defs + uses + 1;
458 int rflct_signature_get_index(const rflct_sig_t *sig, int is_use, int num)
460 return is_use ? num + sig->defs + 1 : num;
464 #define _ARG(_name,_modes,_var,_me) \
466 arg->accepted_modes = _modes; \
467 arg->is_variadic = _var; \
468 arg->mode_equals = _me;
470 int rflct_signature_set_arg(rflct_sig_t *sig, int is_use, int num,
471 const char *name, rflct_mode_class_t mc, int is_variadic, int mode_equals)
473 int index = rflct_signature_get_index(sig, is_use, num);
474 rflct_arg_t *arg = sig->args + index;
475 _ARG(name, mc, is_variadic, mode_equals);
480 void firm_init_rflct(void) {