2 * Copyright (C) 1995-2008 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 The codegenerator (transform FIRM Conv nodes into ppc FIRM)
23 * @author Moritz Kroll, Jens Mueller
31 #include "irgraph_t.h"
35 #include "iredges_t.h"
42 #include "../benode_t.h"
43 #include "bearch_ppc32_t.h"
45 #include "ppc32_nodes_attr.h"
46 //#include "../arch/archop.h" /* we need this for Min and Max nodes */
47 #include "ppc32_transform_conv.h"
48 #include "ppc32_new_nodes.h"
49 #include "ppc32_map_regs.h"
51 #include "gen_ppc32_regalloc_if.h"
61 ir_node *current_block;
67 * Conv walker initialization
69 void ppc32_init_conv_walk(void)
76 static ir_node *own_gen_convert_call(ppc32_transform_env_t *env, ir_node *op, const char *funcname,
77 ir_mode *from_mode, ir_mode *to_mode)
80 ir_entity *method_ent;
82 ir_node *call, *callee, *call_results;
86 method_type = new_type_method(new_id_from_str("convert_call_type"), 1, 1);
87 set_method_param_type(method_type, 0, new_type_primitive(new_id_from_str("conv_param"), from_mode));
88 set_method_res_type(method_type, 0, new_type_primitive(new_id_from_str("conv_result"), to_mode));
90 method_ent = new_entity(get_glob_type(), new_id_from_str(funcname), method_type);
91 callee = new_rd_SymConst_addr_ent(env->dbg, env->irg, mode_P_code, method_ent, method_type);
92 call = new_rd_Call(env->dbg, env->irg, env->block, memory, callee, 1, in, method_type);
93 call_results = new_rd_Proj(env->dbg, env->irg, env->block, call, mode_T, pn_Call_T_result);
94 memory = new_rd_Proj(env->dbg, env->irg, env->block, call, mode_M, pn_Call_M_regular);
96 return new_rd_Proj(env->dbg, env->irg, env->block, call_results, to_mode, 0);
100 * Transforms a Conv node.
102 * @param mod the debug module
103 * @param block the block the new node should belong to
104 * @param node the ir Conv node
106 * @param mode node mode
107 * @return the created ppc Conv node
109 static ir_node *gen_Conv(ppc32_transform_env_t *env, ir_node *op) {
110 ir_mode *from_mode = get_irn_mode(get_irn_n(env->irn,0));
111 ir_mode *to_mode = env->mode;
112 ir_modecode from_modecode=get_mode_modecode(from_mode);
113 ir_modecode to_modecode=get_mode_modecode(to_mode);
115 switch(from_modecode){
117 op = new_rd_Conv(env->dbg, env->irg, env->block, op, mode_D);
122 if (mode_is_signed(to_mode)) // Float to integer
124 ir_node *fctiw = new_rd_ppc32_fCtiw(env->dbg, env->irg, env->block, op, from_mode);
125 ir_node *stfd = new_rd_ppc32_Stfd(env->dbg, env->irg, env->block, get_irg_frame(env->irg),
127 ir_node *storememproj = new_rd_Proj(env->dbg, env->irg, env->block, stfd, mode_M, pn_Store_M);
128 ir_node *lwz = new_rd_ppc32_Lwz(env->dbg, env->irg, env->block, get_irg_frame(env->irg),
130 set_ppc32_frame_entity(stfd, memslot);
131 set_ppc32_offset_mode(stfd, ppc32_ao_Lo16); // TODO: only allows a 16-bit offset on stack
132 set_ppc32_frame_entity(lwz, memslot);
133 set_ppc32_offset_mode(stfd, ppc32_ao_Lo16); // TODO: only allows a 16-bit offset on stack
134 memory = new_rd_Proj(env->dbg, env->irg, env->block, lwz, mode_M, pn_Store_M);
135 res = new_rd_Proj(env->dbg, env->irg, env->block, lwz, to_mode, pn_Load_res);
140 res = own_gen_convert_call(env, op, "conv_double_to_unsigned_int", mode_D, mode_Iu);
149 return new_rd_Conv(env->dbg, env->irg, env->block, res, to_mode);
160 op = new_rd_Conv(env->dbg, env->irg, env->block, op, mode_Is);
162 return own_gen_convert_call(env, op, (to_mode == mode_D) ? "conv_int_to_double" : "conv_int_to_single", mode_Is, to_mode);
167 op = new_rd_Conv(env->dbg, env->irg, env->block, op, mode_Iu);
169 return own_gen_convert_call(env, op, (to_mode == mode_D) ? "conv_unsigned_int_to_double": "conv_unsigned_int_to_single", mode_Iu, to_mode);
177 panic("Mode for Conv not supported: %F -> %F", from_mode, to_mode);
180 int search_from_node_in_block(ir_node *from, ir_node *to)
182 int n = get_irn_arity(from), i;
185 ir_node *pred = get_irn_n(from, i);
186 if(pred==to) return 1;
187 if(get_irn_n(pred, -1)==current_block)
189 if(search_from_node_in_block(pred, to)) return 1;
195 int nodes_dependency_order(ir_node **a, ir_node **b)
197 if(search_from_node_in_block(*a,*b)) return 1;
198 if(search_from_node_in_block(*b,*a)) return -1;
202 void finalize_block(ppc32_code_gen_t *cgenv)
205 ir_node *current_conv;
206 cw_block_attr *attr = current_block->link;
207 ppc32_transform_env_t tenv;
209 if(!attr->conv_count) return;
213 ir_type *frame_type = get_irg_frame_type(cgenv->irg);
214 memslot = frame_alloc_area(frame_type, get_mode_size_bytes(mode_D), 4, 0);
217 attr->convs = XMALLOCN(ir_node*, attr->conv_count);
219 for (i = 0, current_conv = attr->first_conv; i < attr->conv_count; i++, current_conv = current_conv->link)
221 attr->convs[i] = current_conv;
224 qsort(attr->convs, attr->conv_count, sizeof(ir_node *),
225 (int (*)(const void *, const void *)) nodes_dependency_order);
227 tenv.block = current_block;
228 tenv.irg = current_ir_graph;
229 DEBUG_ONLY(tenv.mod = cgenv->mod;)
231 memory = get_irg_no_mem(current_ir_graph);
232 for(i = 0; i < attr->conv_count; i++)
234 tenv.dbg = get_irn_dbg_info(attr->convs[i]);
235 tenv.irn = attr->convs[i];
236 tenv.mode = get_irn_mode(attr->convs[i]);
238 exchange(attr->convs[i], gen_Conv(&tenv, get_Conv_op(attr->convs[i])));
242 void init_block(void)
244 cw_block_attr *attr = XMALLOC(cw_block_attr);
245 attr->first_conv = NULL;
246 attr->convs = NULL; /* attr->convs is set in finalize_block() */
247 attr->conv_count = 0;
248 current_block->link = attr;
252 * Constant generating code
261 /* Compares two (entity, tarval) combinations */
262 static int cmp_tv_ent(const void *a, const void *b, size_t len) {
263 const struct tv_ent *e1 = a;
264 const struct tv_ent *e2 = b;
266 return !(e1->tv == e2->tv);
269 /* Generates a SymConst node for a known FP const */
270 static ir_node *gen_fp_known_symconst(ppc32_transform_env_t *env, tarval *known_const) {
271 static set *const_set = NULL;
272 static ir_type *tp = NULL;
274 struct tv_ent *entry;
280 const_set = new_set(cmp_tv_ent, 10);
282 tp = new_type_primitive(new_id_from_str("const_double_t"), env->mode);
285 key.tv = known_const;
288 entry = set_insert(const_set, &key, sizeof(key), HASH_PTR(key.tv));
292 sprintf(buf, "const_%ld", get_irn_node_nr(env->irn));
293 ent = new_entity(get_glob_type(), new_id_from_str(buf), tp);
295 set_entity_ld_ident(ent, get_entity_ident(ent));
296 set_entity_visibility(ent, visibility_local);
297 set_entity_variability(ent, variability_constant);
298 set_entity_allocation(ent, allocation_static);
300 /* we create a new entity here: It's initialization must resist on the
302 rem = current_ir_graph;
303 current_ir_graph = get_const_code_irg();
304 cnst = new_Const(env->mode, key.tv);
305 current_ir_graph = rem;
307 set_atomic_ent_value(ent, cnst);
309 /* set the entry for hashmap */
313 return new_rd_SymConst_addr_ent(env->dbg, env->irg, ent, tp);
320 * @param env transformation environment
321 * @return the created ppc Const node
323 static ir_node *gen_Const(ppc32_transform_env_t *env) {
324 tarval *tv_const = get_Const_tarval(env->irn);
327 if (mode_is_float(env->mode))
328 constant = new_rd_ppc32_fConst(env->dbg, env->irg, env->block, env->mode);
330 constant = new_rd_ppc32_Const(env->dbg, env->irg, env->block, env->mode);
331 set_ppc32_constant_tarval(constant, tv_const);
336 * Transforms a SymConst.
338 * @param env transformation environment
339 * @return the created ppc SymConst node
341 static ir_node *gen_SymConst(ppc32_transform_env_t *env) {
343 symconst = new_rd_ppc32_SymConst(env->dbg, env->irg, env->block, env->mode);
344 set_ppc32_frame_entity(symconst, get_SymConst_entity(env->irn));
348 /*********************************************************
351 * _ __ ___ __ _ _ _ __ __| |_ __ ___ _____ _ __
352 * | '_ ` _ \ / _` | | '_ \ / _` | '__| \ \ / / _ \ '__|
353 * | | | | | | (_| | | | | | | (_| | | | |\ V / __/ |
354 * |_| |_| |_|\__,_|_|_| |_| \__,_|_| |_| \_/ \___|_|
356 *********************************************************/
361 * Transforms all conv nodes into ppc convs before abi
363 * @param node the firm node
364 * @param env the debug module
366 void ppc32_conv_walk(ir_node *node, void *env) {
367 ppc32_code_gen_t *cgenv = (ppc32_code_gen_t *)env;
368 ir_opcode code = get_irn_opcode(node);
369 ppc32_transform_env_t tenv;
373 if(current_block != NULL)
374 finalize_block(cgenv);
376 current_block = node;
382 tenv.irg = current_ir_graph;
383 DEBUG_ONLY(tenv.mod = cgenv->mod;)
385 if (code == iro_Conv)
387 ir_modecode from_mode=get_mode_modecode(get_irn_mode(get_irn_n(node,0)));
388 ir_modecode to_mode=get_mode_modecode(get_irn_mode(node));
391 if(from_mode == to_mode) return;
392 if(from_mode == irm_F || from_mode == irm_D)
408 else if(to_mode == irm_F || to_mode == irm_D)
425 /* in Liste eintragen */
426 attr = get_irn_link(current_block);
427 set_irn_link(node, attr->first_conv);
428 attr->first_conv = node;
432 else if (code == iro_Call) {
434 ir_type *tp = get_Call_type(node);
436 int stack_alignment = 4;
438 for (i = get_Call_n_params(node) - 1; i >= 0; --i) {
439 ir_mode *mode = get_irn_mode(get_Call_param(node, i));
442 if (mode_is_reference(mode)) {
443 /* might be a compound parameter */
444 ptp = get_method_param_type(tp, i);
446 if (is_compound_type(ptp)) {
447 s = (get_type_size_bytes(ptp) + stack_alignment - 1) & -stack_alignment;
453 s = (get_mode_size_bytes(mode) + stack_alignment - 1) & -stack_alignment;
456 if ((unsigned) size > cgenv->area_size)
457 cgenv->area_size = size;
462 * Transforms all const nodes into ppc const nodes inside the using block
464 * @param node the firm node
465 * @param env the debug module
467 void ppc32_pretransform_walk(ir_node *node, void *env) {
468 ppc32_code_gen_t *cgenv = (ppc32_code_gen_t *)env;
469 ir_opcode code = get_irn_opcode(node);
470 ppc32_transform_env_t tenv;
474 current_block = node;
478 tenv.irg = current_ir_graph;
479 DEBUG_ONLY(tenv.mod = cgenv->mod;)
481 if(code == iro_Const || code == iro_SymConst)
485 tenv.block = cgenv->start_succ_block;
487 tenv.mode = get_irn_mode(node);
488 tenv.dbg = get_irn_dbg_info(node);
490 if(code == iro_Const)
491 newconst = gen_Const(&tenv);
493 newconst = gen_SymConst(&tenv);
495 exchange(node, newconst);