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, ...);
36 * Get the number of bits set in a word.
38 static INLINE int pop(int i) {
39 unsigned x = (unsigned) i;
40 x = ((x >> 1) & 0x55555555);
41 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
42 x = (x + (x >> 4)) & 0x0f0f0f0f;
45 return (int) (x & 0x3f);
49 * Get the number of bits differing in two variables.
51 static INLINE int dist(int x, int y) {
56 #define MAX_SIG_COUNT 8
57 #define MAX_ARG_COUNT 10
60 int num; /**< A sequential number (one opcode can have multiple signatures. */
61 rflct_arg_t args[]; /**< The signature. */
69 const rflct_arg_t *sigs[MAX_SIG_COUNT];
72 static struct obstack obst;
74 static rflct_opcode_t **opcodes = NULL;
76 static int opcodes_size = 0;
78 static INLINE void assure_opcode_capacity(int opcode)
80 if(opcode >= opcodes_size) {
81 int new_size = 2 * opcode;
82 rflct_opcode_t **new_opcodes = xmalloc(sizeof(new_opcodes[0]) * new_size);
84 memset(new_opcodes, 0, sizeof(*new_opcodes) * new_size);
87 memcpy(new_opcodes, opcodes, sizeof(*opcodes) * opcodes_size);
91 opcodes = new_opcodes;
92 opcodes_size = new_size;
97 #define OPCODES_COUNT (sizeof(opcodes) / sizeof(opcodes[0]))
100 rflct_mode_class_t rflct_get_mode_class(const ir_mode *mode) {
101 mode_sort sort = get_mode_sort(mode);
105 case irms_control_flow:
108 else if(mode == mode_X)
111 return RFLCT_MC(Mem);
112 case irms_internal_boolean:
113 return RFLCT_MC(Bool);
114 case irms_int_number:
115 return mode_is_signed(mode) ? RFLCT_MC(IntS) : RFLCT_MC(IntU);
116 case irms_float_number:
117 return RFLCT_MC(Float);
119 return RFLCT_MC(Ref);
121 return RFLCT_MC(Char);
124 return RFLCT_MC(None);
127 static INLINE const rflct_opcode_t *get_opcode(opcode opc) {
128 assert(opc >= 0 && opc < OPCODES_COUNT && "Invalid opcode");
132 static INLINE const rflct_arg_t *get_args(opcode opc, int sig) {
133 const rflct_opcode_t *opcode = get_opcode(opc);
134 assert(sig >= 0 && sig < opcode->sig_count
135 && "Invalid signature");
136 return opcode->sigs[sig];
139 #define GET_OPCODE(opc) get_opcode(opc)
140 #define GET_ARGS(opc,args) get_args(opc, args)
142 int rflct_get_signature_count(opcode opc) {
143 const rflct_opcode_t *opcode = GET_OPCODE(opc);
144 return opcode->sig_count;
147 int rflct_get_in_args_count(opcode opc, int sig) {
148 const rflct_arg_t *args = GET_ARGS(opc, sig);
151 for(i = 0; args[i].name != NULL; i++);
152 for(res = 0, i += 1; args[i].name != NULL; res++, i++);
156 int rflct_get_out_args_count(opcode opc, int sig) {
157 const rflct_arg_t *args = GET_ARGS(opc, sig);
159 for(i = 0; args[i].name != NULL; i++);
164 const rflct_arg_t *rflct_get_in_args(opcode opc, int sig) {
165 const rflct_arg_t *args = GET_ARGS(opc, sig);
168 for(i = 0; args[i].name != NULL; i++);
172 const rflct_arg_t *rflct_get_out_args(opcode opc, int sig) {
173 return GET_ARGS(opc, sig);
176 int rflct_signature_match(ir_node *irn, int sig) {
177 opcode op = get_irn_opcode(irn);
178 const rflct_arg_t *args = rflct_get_in_args(op, sig);
182 for(i = 0, j = -1; RFLCT_ARG_VALID(&args[i])
183 && j < get_irn_arity(irn); j++) {
185 ir_node *child = get_irn_n(irn, j);
186 const rflct_arg_t *arg = &args[i];
187 rflct_mode_class_t mc = rflct_get_mode_class(get_irn_mode(child));
189 if(arg->accepted_modes & mc)
190 dst += dist(arg->accepted_modes, mc);
194 if(!arg->is_variadic)
201 int rflct_get_signature(ir_node *irn) {
202 const rflct_opcode_t *opc = GET_OPCODE(get_irn_opcode(irn));
203 int min_dist = INT_MAX;
204 int min_sig = INT_MAX;
207 for(i = 0; i < opc->sig_count; i++) {
208 int dist = rflct_signature_match(irn, i);
209 if(dist < min_dist) {
218 static const char *rflct_mode_class_atomic_name(rflct_mode_class_t mc) {
219 #define XXX(_mc) case RFLCT_MC(_mc): return #_mc
246 static void rflct_mode_class_comb_name_obst(struct obstack *obst,
247 rflct_mode_class_t mc) {
248 const char *res = rflct_mode_class_atomic_name(mc);
250 if(strlen(res) == 0) {
251 const char *prefix = "";
254 obstack_1grow(obst, '{');
255 for(mask = 1; mask != 0; mask <<= 1) {
257 const char *s = rflct_mode_class_atomic_name(mask);
258 obstack_grow_str(obst, s);
259 obstack_grow_str(obst, prefix);
263 obstack_1grow(obst, '}');
266 obstack_grow(obst, res, strlen(res));
269 char *rflct_mode_class_name(char *str, int n, rflct_mode_class_t mc) {
275 rflct_mode_class_comb_name_obst(&obst, mc);
276 obstack_1grow(&obst, 0);
277 res = obstack_finish(&obst);
279 strncpy(str, res, n);
281 obstack_free(&obst, NULL);
286 static void rflct_obstack_grow_args(struct obstack *obst,
287 const rflct_arg_t *args) {
288 const rflct_arg_t *arg;
289 const char *prefix = "";
291 for(arg = args; RFLCT_ARG_VALID(arg); arg++) {
292 obstack_grow_str(obst, prefix);
293 obstack_grow_str(obst, arg->name);
295 obstack_1grow(obst, '*');
296 obstack_1grow(obst, ':');
297 rflct_mode_class_comb_name_obst(obst, arg->accepted_modes);
303 char *rflct_to_string(char *buf, int n, opcode opc, int sig) {
306 const rflct_opcode_t *opcode = GET_OPCODE(opc);
310 obstack_1grow(&obst, '(');
311 rflct_obstack_grow_args(&obst, rflct_get_out_args(opc, sig));
313 obstack_grow_str(&obst, ") = ");
314 obstack_grow_str(&obst, opcode->name);
315 obstack_1grow(&obst, '(');
317 rflct_obstack_grow_args(&obst, rflct_get_in_args(opc, sig));
319 obstack_1grow(&obst, ')');
320 obstack_1grow(&obst, 0);
321 s = obstack_finish(&obst);
323 obstack_free(&obst, NULL);
328 #define ARG(name,modes) \
329 _ARG(name, modes, false, -1)
331 #define ARG_SAME(name,modes,mode_same) \
332 _ARG(name, modes, false, mode_same)
334 #define VARG(name,modes) \
335 _ARG(name, modes, true, 0)
337 #define VARG_SAME(name,modes) \
338 _ARG(name, modes, true, 1)
341 _ARG(NULL, None, false, -1)
344 _ARG(NULL, None, false, 0)
346 #define BLOCK ARG("Block", BB)
348 static void init_ops(void) {
351 rflct_opcode_t *opcode;
355 assure_opcode_capacity(iro_MaxOpcode);
358 #define BEGIN_OP(op) \
360 opcode = obstack_alloc(&obst, sizeof(*opcode)); \
361 opcode->opc = iro_ ## op; \
362 opcode->name = #op; \
363 opcodes[opcode->opc] = opcode;
368 #define _ARG(_name,_modes,_variadic,_mode_equals) \
372 args.accepted_modes = RFLCT_MC(_modes); \
373 args.is_variadic = _variadic; \
374 args.mode_equals = _mode_equals; \
375 obstack_grow(&obst, &args, sizeof(args)); \
381 assert(curr_sig < MAX_SIG_COUNT && "Mind the maximum number of signatures"); \
382 opcode->sigs[curr_sig++] = obstack_finish(&obst); \
383 opcode->sig_count = curr_sig; \
388 #include "irreflect.def"
397 #define _ARG(_name,_modes,_var,_me) \
399 arg->accepted_modes = RFLCT_MC(_modes); \
400 arg->is_variadic = _var; \
401 arg->mode_equals = _me;
403 void rflct_new_opcode(opcode opc, const char *name, bool commutative)
405 rflct_opcode_t *ropc = obstack_alloc(&obst, sizeof(*ropc));
410 memset(ropc->sigs, 0, sizeof(ropc->sigs));
412 assure_opcode_capacity(opc);
416 bool rflct_opcode_add_signature(opcode opc, rflct_sig_t *sig)
418 const rflct_arg_t *args = sig->args;
419 rflct_opcode_t *op = opcodes[opc];
422 assert(op && "Illegal opcode");
424 for(i = 0; i < MAX_SIG_COUNT && op->sigs[i] != NULL; i++);
426 if(i >= MAX_SIG_COUNT)
429 op->sigs[op->sig_count++] = args;
436 rflct_sig_t *rflct_signature_allocate(int defs, int uses)
438 rflct_sig_t *sig = xmalloc(sizeof(*sig));
441 obstack_alloc(&obst, sizeof(*args) * (defs + uses + 2));
443 rflct_arg_t *arg = args + defs;
446 arg = args + defs + uses + 1;
456 int rflct_signature_get_index(const rflct_sig_t *sig, bool is_use, int num)
458 return is_use ? num + sig->defs + 1 : num;
462 #define _ARG(_name,_modes,_var,_me) \
464 arg->accepted_modes = _modes; \
465 arg->is_variadic = _var; \
466 arg->mode_equals = _me;
468 int rflct_signature_set_arg(rflct_sig_t *sig, bool is_use, int num,
469 const char *name, rflct_mode_class_t mc, bool is_variadic, int mode_equals)
471 int index = rflct_signature_get_index(sig, is_use, num);
472 rflct_arg_t *arg = sig->args + index;
473 _ARG(name, mc, is_variadic, mode_equals);
478 void init_rflct(void) {