4 * @author Sebastian Hack
5 * @brief Reflection for Firm operands.
23 #define obstack_chunk_alloc malloc
24 #define obstack_chunk_free free
28 #include "irreflect.h"
30 #define obstack_grow_str(obst,s) obstack_grow((obst), (s), strlen((s)))
31 #define obstack_grow_str_const(obst,s) obstack_grow((obst), (s), sizeof((s)))
33 extern int obstack_printf(struct obstack *obst, const char *fmt, ...);
38 * Get the number of bits set in a word.
40 static INLINE int pop(int i) {
41 unsigned x = (unsigned) i;
42 x = ((x >> 1) & 0x55555555);
43 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
44 x = (x + (x >> 4)) & 0x0f0f0f0f;
47 return (int) (x & 0x3f);
51 * Get the number of bits differing in two variables.
53 static INLINE int dist(int x, int y) {
58 #define MAX_SIG_COUNT 8
59 #define MAX_ARG_COUNT 10
62 int num; /**< A sequential number (one opcode can have multiple signatures. */
63 rflct_arg_t args[]; /**< The signature. */
71 const rflct_arg_t *sigs[MAX_SIG_COUNT];
74 static struct obstack obst;
76 static rflct_opcode_t **opcodes = NULL;
78 static int opcodes_size = 0;
80 static INLINE void assure_opcode_capacity(int opcode)
82 if(opcode >= opcodes_size) {
83 int new_size = 2 * opcode;
84 rflct_opcode_t **new_opcodes = malloc(sizeof(*new_opcodes) * new_size);
86 memset(new_opcodes, 0, sizeof(*new_opcodes) * new_size);
89 memcpy(new_opcodes, opcodes, sizeof(*opcodes) * opcodes_size);
93 opcodes = new_opcodes;
94 opcodes_size = new_size;
99 #define OPCODES_COUNT (sizeof(opcodes) / sizeof(opcodes[0]))
102 rflct_mode_class_t rflct_get_mode_class(const ir_mode *mode) {
103 mode_sort sort = get_mode_sort(mode);
107 case irms_control_flow:
110 else if(mode == mode_X)
113 return RFLCT_MC(Mem);
114 case irms_internal_boolean:
115 return RFLCT_MC(Bool);
116 case irms_int_number:
117 return mode_is_signed(mode) ? RFLCT_MC(IntS) : RFLCT_MC(IntU);
118 case irms_float_number:
119 return RFLCT_MC(Float);
121 return RFLCT_MC(Ref);
123 return RFLCT_MC(Char);
126 return RFLCT_MC(None);
129 static INLINE const rflct_opcode_t *get_opcode(opcode opc) {
130 assert(opc >= 0 && opc < OPCODES_COUNT && "Invalid opcode");
134 static INLINE const rflct_arg_t *get_args(opcode opc, int sig) {
135 const rflct_opcode_t *opcode = get_opcode(opc);
136 assert(sig >= 0 && sig < opcode->sig_count
137 && "Invalid signature");
138 return opcode->sigs[sig];
141 #define GET_OPCODE(opc) get_opcode(opc)
142 #define GET_ARGS(opc,args) get_args(opc, args)
144 int rflct_get_signature_count(opcode opc) {
145 const rflct_opcode_t *opcode = GET_OPCODE(opc);
146 return opcode->sig_count;
149 int rflct_get_in_args_count(opcode opc, int sig) {
150 const rflct_arg_t *args = GET_ARGS(opc, sig);
153 for(i = 0; args[i].name != NULL; i++);
154 for(res = 0, i += 1; args[i].name != NULL; res++, i++);
158 int rflct_get_out_args_count(opcode opc, int sig) {
159 const rflct_arg_t *args = GET_ARGS(opc, sig);
161 for(i = 0; args[i].name != NULL; i++);
166 const rflct_arg_t *rflct_get_in_args(opcode opc, int sig) {
167 const rflct_arg_t *args = GET_ARGS(opc, sig);
170 for(i = 0; args[i].name != NULL; i++);
174 const rflct_arg_t *rflct_get_out_args(opcode opc, int sig) {
175 return GET_ARGS(opc, sig);
178 int rflct_signature_match(ir_node *irn, int sig) {
179 opcode op = get_irn_opcode(irn);
180 const rflct_arg_t *args = rflct_get_in_args(op, sig);
184 for(i = 0, j = -1; RFLCT_ARG_VALID(&args[i])
185 && j < get_irn_arity(irn); j++) {
187 ir_node *child = get_irn_n(irn, j);
188 const rflct_arg_t *arg = &args[i];
189 rflct_mode_class_t mc = rflct_get_mode_class(get_irn_mode(child));
191 if(arg->accepted_modes & mc)
192 dst += dist(arg->accepted_modes, mc);
196 if(!arg->is_variadic)
203 int rflct_get_signature(ir_node *irn) {
204 const rflct_opcode_t *opc = GET_OPCODE(get_irn_opcode(irn));
205 int min_dist = INT_MAX;
206 int min_sig = INT_MAX;
209 for(i = 0; i < opc->sig_count; i++) {
210 int dist = rflct_signature_match(irn, i);
211 if(dist < min_dist) {
220 static const char *rflct_mode_class_atomic_name(rflct_mode_class_t mc) {
221 #define XXX(_mc) case RFLCT_MC(_mc): return #_mc
248 static void rflct_mode_class_comb_name_obst(struct obstack *obst,
249 rflct_mode_class_t mc) {
250 const char *res = rflct_mode_class_atomic_name(mc);
252 if(strlen(res) == 0) {
253 const char *prefix = "";
256 obstack_1grow(obst, '{');
257 for(mask = 1; mask != 0; mask <<= 1) {
259 const char *s = rflct_mode_class_atomic_name(mask);
260 obstack_grow_str(obst, s);
261 obstack_grow_str(obst, prefix);
265 obstack_1grow(obst, '}');
268 obstack_grow(obst, res, strlen(res));
271 char *rflct_mode_class_name(char *str, int n, rflct_mode_class_t mc) {
277 rflct_mode_class_comb_name_obst(&obst, mc);
278 obstack_1grow(&obst, 0);
279 res = obstack_finish(&obst);
281 strncpy(str, res, n);
283 obstack_free(&obst, NULL);
288 static void rflct_obstack_grow_args(struct obstack *obst,
289 const rflct_arg_t *args) {
290 const rflct_arg_t *arg;
291 const char *prefix = "";
293 for(arg = args; RFLCT_ARG_VALID(arg); arg++) {
294 obstack_grow_str(obst, prefix);
295 obstack_grow_str(obst, arg->name);
297 obstack_1grow(obst, '*');
298 obstack_1grow(obst, ':');
299 rflct_mode_class_comb_name_obst(obst, arg->accepted_modes);
305 char *rflct_to_string(char *buf, int n, opcode opc, int sig) {
308 const rflct_opcode_t *opcode = GET_OPCODE(opc);
312 obstack_1grow(&obst, '(');
313 rflct_obstack_grow_args(&obst, rflct_get_out_args(opc, sig));
315 obstack_grow_str(&obst, ") = ");
316 obstack_grow_str(&obst, opcode->name);
317 obstack_1grow(&obst, '(');
319 rflct_obstack_grow_args(&obst, rflct_get_in_args(opc, sig));
321 obstack_1grow(&obst, ')');
322 obstack_1grow(&obst, 0);
323 s = obstack_finish(&obst);
325 obstack_free(&obst, NULL);
330 #define ARG(name,modes) \
331 _ARG(name, modes, false, -1)
333 #define ARG_SAME(name,modes,mode_same) \
334 _ARG(name, modes, false, mode_same)
336 #define VARG(name,modes) \
337 _ARG(name, modes, true, 0)
339 #define VARG_SAME(name,modes) \
340 _ARG(name, modes, true, 1)
343 _ARG(NULL, None, false, -1)
346 _ARG(NULL, None, false, 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, bool 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 bool rflct_opcode_add_signature(opcode opc, rflct_sig_t *sig)
420 const 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 = malloc(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, bool 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, bool is_use, int num,
471 const char *name, rflct_mode_class_t mc, bool 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 init_rflct(void) {