1 #include "pseudo_irg.h"
5 #include "bearch_ia32.h"
12 #ifdef obstack_chunk_alloc
13 # undef obstack_chunk_alloc
14 # define obstack_chunk_alloc malloc
16 # define obstack_chunk_alloc malloc
17 # define obstack_chunk_free free
20 #include "../bearch.h" /* the general register allocator interface */
22 #include "ia32_new_nodes.h" /* ia32 nodes interface */
23 #include "gen_ia32_regalloc_if.h" /* the generated interface (register type and class defenitions) */
24 #include "ia32_gen_decls.h" /* interface declaration emitter */
25 #include "ia32_transform.h"
26 #include "ia32_emitter.h"
27 #include "ia32_map_regs.h"
29 #define DEBUG_MODULE "ir.be.isa.ia32"
32 static set *cur_reg_set = NULL;
36 /**************************************************
39 * _ __ ___ __ _ __ _| | | ___ ___ _| |_
40 * | '__/ _ \/ _` | / _` | | |/ _ \ / __| | | _|
41 * | | | __/ (_| | | (_| | | | (_) | (__ | | |
42 * |_| \___|\__, | \__,_|_|_|\___/ \___| |_|_|
45 **************************************************/
47 static ir_node *my_skip_proj(const ir_node *n) {
54 * Return register requirements for an ia32 node.
55 * If the node returns a tuple (mode_T) then the proj's
56 * will be asked for this information.
58 static const arch_register_req_t *ia32_get_irn_reg_req(const arch_irn_ops_t *self, arch_register_req_t *req, const ir_node *irn, int pos) {
59 const arch_register_req_t **irn_req;
60 long node_pos = pos == -1 ? 0 : pos;
61 ir_mode *mode = get_irn_mode(irn);
62 firm_dbg_module_t *mod = firm_dbg_register(DEBUG_MODULE);
64 if (mode == mode_T || mode == mode_M) {
65 DBG((mod, LEVEL_1, "ignoring mode_T, mode_M node %+F\n", irn));
69 DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
73 node_pos = translate_proj_pos(irn);
77 irn = my_skip_proj(irn);
79 DBG((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
82 if (is_ia32_irn(irn)) {
84 irn_req = get_ia32_in_req(irn);
87 irn_req = get_ia32_out_req(irn);
91 DBG((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
93 memcpy(req, irn_req[pos], sizeof(*req));
97 /* treat Phi like Const with default requirements */
99 DBG((mod, LEVEL_1, "returning standard reqs for %+F\n", irn));
100 if (mode_is_float(mode))
101 memcpy(req, &ia32_default_req_ia32_floating_point, sizeof(*req));
102 else if (mode_is_int(mode) || mode_is_reference(mode))
103 memcpy(req, &ia32_default_req_ia32_general_purpose, sizeof(*req));
104 else if (mode == mode_T || mode == mode_M) {
105 DBG((mod, LEVEL_1, "ignoring Phi node %+F\n", irn));
109 assert(0 && "unsupported Phi-Mode");
111 else if (get_irn_op(irn) == op_Start) {
112 DBG((mod, LEVEL_1, "returning reqs none for ProjX -> Start (%+F )\n", irn));
114 case pn_Start_X_initial_exec:
115 case pn_Start_P_value_arg_base:
116 case pn_Start_P_globals:
117 memcpy(req, &ia32_default_req_none, sizeof(*req));
119 case pn_Start_P_frame_base:
120 memcpy(req, &ia32_default_req_none, sizeof(*req));
122 case pn_Start_T_args:
123 assert(0 && "ProjT(pn_Start_T_args) should not be asked");
126 else if (get_irn_op(irn) == op_Return && pos >= 0) {
127 DBG((mod, LEVEL_1, "returning reqs EAX for %+F\n", irn));
128 memcpy(req, &ia32_default_req_ia32_general_purpose_eax, sizeof(*req));
131 DBG((mod, LEVEL_1, "returning NULL for %+F (not ia32)\n", irn));
133 //memcpy(req, &ia32_dummy_register_req, sizeof(*req));
140 static void ia32_set_irn_reg(const arch_irn_ops_t *self, ir_node *irn, const arch_register_t *reg) {
144 pos = translate_proj_pos(irn);
145 irn = my_skip_proj(irn);
148 if (is_ia32_irn(irn)) {
149 const arch_register_t **slots;
151 slots = get_ia32_slots(irn);
155 ia32_set_firm_reg(self, irn, reg, cur_reg_set);
159 static const arch_register_t *ia32_get_irn_reg(const arch_irn_ops_t *self, const ir_node *irn) {
161 const arch_register_t *reg = NULL;
164 pos = translate_proj_pos(irn);
165 irn = my_skip_proj(irn);
168 if (is_ia32_irn(irn)) {
169 const arch_register_t **slots;
170 slots = get_ia32_slots(irn);
174 reg = ia32_get_firm_reg(self, irn, cur_reg_set);
180 static arch_irn_class_t ia32_classify(const arch_irn_ops_t *self, const ir_node *irn) {
181 irn = my_skip_proj(irn);
183 return arch_irn_class_branch;
184 else if (is_ia32_irn(irn))
185 return arch_irn_class_normal;
190 static arch_irn_flags_t ia32_get_flags(const arch_irn_ops_t *self, const ir_node *irn) {
191 irn = my_skip_proj(irn);
192 if (is_ia32_irn(irn))
193 return get_ia32_flags(irn);
195 ir_printf("don't know flags of %+F\n", irn);
200 /* fill register allocator interface */
202 static const arch_irn_ops_t ia32_irn_ops = {
203 ia32_get_irn_reg_req,
212 /**************************************************
215 * ___ ___ __| | ___ __ _ ___ _ __ _| |_
216 * / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \ | | _|
217 * | (_| (_) | (_| | __/ (_| | __/ | | | | | |
218 * \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
221 **************************************************/
223 typedef struct _ia32_isa_t {
224 const arch_isa_if_t *impl;
228 typedef struct _ia32_code_gen_t {
229 const arch_code_generator_if_t *impl; /* implementation */
230 ir_graph *irg; /* current irg */
231 FILE *out; /* output file */
232 const arch_env_t *arch_env; /* the arch env */
233 set *reg_set; /* set to memorize registers for non-ia32 nodes (e.g. phi nodes) */
234 firm_dbg_module_t *mod; /* debugging module */
239 * Transforms the standard firm graph into
242 static void ia32_prepare_graph(void *self) {
243 ia32_code_gen_t *cg = self;
245 if (! is_pseudo_ir_graph(cg->irg))
246 irg_walk_blkwise_graph(cg->irg, NULL, ia32_transform_node, cg->mod);
252 * Dummy functions for hooks we don't need but which must be filled.
254 static void ia32_before_sched(void *self) {
257 static void ia32_before_ra(void *self) {
263 * Emits the code, closes the output file and frees
264 * the code generator interface.
266 static void ia32_codegen(void *self) {
267 ia32_code_gen_t *cg = self;
268 ir_graph *irg = cg->irg;
271 if (cg->emit_decls) {
272 ia32_gen_decls(cg->out);
276 // ia32_finish_irg(irg);
277 ia32_gen_routine(out, irg, cg->arch_env);
281 /* de-allocate code generator */
282 del_set(cg->reg_set);
286 static void *ia32_cg_init(FILE *F, ir_graph *irg, const arch_env_t *arch_env);
288 static const arch_code_generator_if_t ia32_code_gen_if = {
291 ia32_before_sched, /* before scheduling hook */
292 ia32_before_ra, /* before register allocation hook */
293 ia32_codegen /* emit && done */
297 * Initializes the code generator.
299 static void *ia32_cg_init(FILE *F, ir_graph *irg, const arch_env_t *arch_env) {
300 ia32_isa_t *isa = (ia32_isa_t *)arch_env->isa;
301 ia32_code_gen_t *cg = malloc(sizeof(*cg));
303 cg->impl = &ia32_code_gen_if;
305 cg->reg_set = new_set(cmp_irn_reg_assoc, 1024);
306 cg->mod = firm_dbg_register("be.transform.ia32");
308 cg->arch_env = arch_env;
312 if (isa->num_codegens > 1)
317 cur_reg_set = cg->reg_set;
319 return (arch_code_generator_t *)cg;
324 /*****************************************************************
325 * ____ _ _ _____ _____
326 * | _ \ | | | | |_ _|/ ____| /\
327 * | |_) | __ _ ___| | _____ _ __ __| | | | | (___ / \
328 * | _ < / _` |/ __| |/ / _ \ '_ \ / _` | | | \___ \ / /\ \
329 * | |_) | (_| | (__| < __/ | | | (_| | _| |_ ____) / ____ \
330 * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/ \_\
332 *****************************************************************/
335 * Initializes the backend ISA and opens the output file.
337 static void *ia32_init(void) {
338 static int inited = 0;
339 ia32_isa_t *isa = malloc(sizeof(*isa));
341 isa->impl = &ia32_isa_if;
348 isa->num_codegens = 0;
350 ia32_register_init();
351 ia32_create_opcodes();
359 * Closes the output file and frees the ISA structure.
361 static void ia32_done(void *self) {
367 static int ia32_get_n_reg_class(const void *self) {
371 static const arch_register_class_t *ia32_get_reg_class(const void *self, int i) {
372 assert(i >= 0 && i < N_CLASSES && "Invalid ia32 register class requested.");
373 return &ia32_reg_classes[i];
376 static const arch_irn_ops_t *ia32_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
377 return &ia32_irn_ops;
380 const arch_irn_handler_t ia32_irn_handler = {
384 const arch_irn_handler_t *ia32_get_irn_handler(const void *self) {
385 return &ia32_irn_handler;
391 * Initializes the code generator interface.
393 static const arch_code_generator_if_t *ia32_get_code_generator_if(void *self) {
394 return &ia32_code_gen_if;
398 * Returns the default scheduler
400 static const list_sched_selector_t *ia32_get_list_sched_selector(const void *self) {
401 return trivial_selector;
405 static void ia32_register_options(lc_opt_entry_t *ent)
408 #endif /* WITH_LIBCORE */
410 const arch_isa_if_t ia32_isa_if = {
412 ia32_register_options,
416 ia32_get_n_reg_class,
418 ia32_get_irn_handler,
419 ia32_get_code_generator_if,
420 ia32_get_list_sched_selector