renamed modecode, mode_sort, mode_arithmetic to ir_*
[libfirm] / ir / be / ppc32 / ppc32_transform_conv.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   The codegenerator (transform FIRM Conv nodes into ppc FIRM)
23  * @author  Moritz Kroll, Jens Mueller
24  * @version $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "irnode_t.h"
31 #include "irgraph_t.h"
32 #include "irmode_t.h"
33 #include "irgmod.h"
34 #include "iredges.h"
35 #include "iredges_t.h"
36 #include "irvrfy.h"
37 #include "ircons.h"
38 #include "iropt_t.h"
39 #include "debug.h"
40
41 #include "../benode_t.h"
42 #include "bearch_ppc32_t.h"
43
44 #include "ppc32_nodes_attr.h"
45 //#include "../arch/archop.h"     /* we need this for Min and Max nodes */
46 #include "ppc32_transform_conv.h"
47 #include "ppc32_new_nodes.h"
48 #include "ppc32_map_regs.h"
49
50 #include "gen_ppc32_regalloc_if.h"
51
52 typedef struct
53 {
54         ir_node *first_conv;
55         ir_node **convs;
56         int conv_count;
57 } cw_block_attr;
58
59
60 ir_node *current_block;
61 int conv_nodes_found;
62 ir_entity *memslot;
63 ir_node *memory;
64
65 /**
66  * Conv walker initialization
67  */
68 void ppc32_init_conv_walk(void)
69 {
70         current_block = NULL;
71         conv_nodes_found = 0;
72         memslot = NULL;
73 }
74
75 static ir_node *own_gen_convert_call(ppc32_transform_env_t *env, ir_node *op, const char *funcname,
76                                                                          ir_mode *from_mode, ir_mode *to_mode)
77 {
78         ir_type *method_type;
79         ir_entity *method_ent;
80         ir_node *in[1];
81         ir_node *call, *callee, *call_results;
82
83         in[0] = op;
84
85         method_type = new_type_method(new_id_from_str("convert_call_type"), 1, 1);
86         set_method_param_type(method_type, 0, new_type_primitive(new_id_from_str("conv_param"), from_mode));
87         set_method_res_type(method_type, 0, new_type_primitive(new_id_from_str("conv_result"), to_mode));
88
89         method_ent   = new_entity(get_glob_type(), new_id_from_str(funcname), method_type);
90         callee       = new_rd_SymConst_addr_ent(env->dbg, env->irg, mode_P_code, method_ent, method_type);
91         call         = new_rd_Call(env->dbg, env->irg, env->block, memory, callee, 1, in, method_type);
92         call_results = new_rd_Proj(env->dbg, env->irg, env->block, call, mode_T, pn_Call_T_result);
93         memory       = new_rd_Proj(env->dbg, env->irg, env->block, call, mode_M, pn_Call_M_regular);
94
95         return new_rd_Proj(env->dbg, env->irg, env->block, call_results, to_mode, 0);
96 }
97
98 /**
99  * Transforms a Conv node.
100  *
101  * @param mod     the debug module
102  * @param block   the block the new node should belong to
103  * @param node    the ir Conv node
104  * @param op      operator
105  * @param mode    node mode
106  * @return the created ppc Conv node
107  */
108 static ir_node *gen_Conv(ppc32_transform_env_t *env, ir_node *op) {
109         ir_mode *from_mode = get_irn_mode(get_irn_n(env->irn,0));
110         ir_mode *to_mode = env->mode;
111         ir_modecode from_modecode=get_mode_modecode(from_mode);
112         ir_modecode to_modecode=get_mode_modecode(to_mode);
113
114         switch(from_modecode){
115                 case irm_F:
116                         op = new_rd_Conv(env->dbg, env->irg, env->block, op, mode_D);
117                         // fall through
118                 case irm_D:
119                 {
120                         ir_node *res;
121                         if (mode_is_signed(to_mode))  // Float to integer
122                         {
123                                 ir_node *fctiw = new_rd_ppc32_fCtiw(env->dbg, env->irg, env->block, op, from_mode);
124                                 ir_node *stfd = new_rd_ppc32_Stfd(env->dbg, env->irg, env->block, get_irg_frame(env->irg),
125                                         fctiw, memory);
126                                 ir_node *storememproj = new_rd_Proj(env->dbg, env->irg, env->block, stfd, mode_M, pn_Store_M);
127                                 ir_node *lwz = new_rd_ppc32_Lwz(env->dbg, env->irg, env->block, get_irg_frame(env->irg),
128                                         storememproj);
129                                 set_ppc32_frame_entity(stfd, memslot);
130                                 set_ppc32_offset_mode(stfd, ppc32_ao_Lo16);     // TODO: only allows a 16-bit offset on stack
131                                 set_ppc32_frame_entity(lwz, memslot);
132                                 set_ppc32_offset_mode(stfd, ppc32_ao_Lo16);     // TODO: only allows a 16-bit offset on stack
133                                 memory = new_rd_Proj(env->dbg, env->irg, env->block, lwz, mode_M, pn_Store_M);
134                                 res = new_rd_Proj(env->dbg, env->irg, env->block, lwz, to_mode, pn_Load_res);
135
136                         }
137                         else
138                         {
139                                 res = own_gen_convert_call(env, op, "conv_double_to_unsigned_int", mode_D, mode_Iu);
140                         }
141
142                         switch(to_modecode)
143                         {
144                                 case irm_Bs:
145                                 case irm_Hs:
146                                 case irm_Bu:
147                                 case irm_Hu:
148                                         return new_rd_Conv(env->dbg, env->irg, env->block, res, to_mode);
149                                 case irm_Is:
150                                 case irm_Iu:
151                                         return res;
152                                 default:
153                                         break;
154                         }
155                         break;
156                 }
157                 case irm_Hs:
158                 case irm_Bs:
159                         op = new_rd_Conv(env->dbg, env->irg, env->block, op, mode_Is);
160                 case irm_Is:
161                         return own_gen_convert_call(env, op, (to_mode == mode_D) ? "conv_int_to_double" : "conv_int_to_single", mode_Is, to_mode);
162
163
164                 case irm_Hu:
165                 case irm_Bu:
166                         op = new_rd_Conv(env->dbg, env->irg, env->block, op, mode_Iu);
167                 case irm_Iu:
168                         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);
169
170                 case irm_P:
171                         break;
172
173                 default:
174                         break;
175         }
176         fprintf(stderr, "Mode for Conv not supported: %s -> %s\n", get_mode_name(from_mode), get_mode_name(to_mode));
177         assert(0);
178         return 0;
179
180         // return op;
181 }
182
183 int search_from_node_in_block(ir_node *from, ir_node *to)
184 {
185         int n = get_irn_arity(from), i;
186         for(i=0;i<n;i++)
187         {
188                 ir_node *pred = get_irn_n(from, i);
189                 if(pred==to) return 1;
190                 if(get_irn_n(pred, -1)==current_block)
191                 {
192                         if(search_from_node_in_block(pred, to)) return 1;
193                 }
194         }
195         return 0;
196 }
197
198 int nodes_dependency_order(ir_node **a, ir_node **b)
199 {
200         if(search_from_node_in_block(*a,*b)) return 1;
201         if(search_from_node_in_block(*b,*a)) return -1;
202         return 0;
203 }
204
205 void finalize_block(ppc32_code_gen_t *cgenv)
206 {
207         int i;
208         ir_node *current_conv;
209         cw_block_attr *attr = current_block->link;
210         ppc32_transform_env_t tenv;
211
212         if(!attr->conv_count) return;
213
214         if(!memslot)
215         {
216                 ir_type *frame_type = get_irg_frame_type(cgenv->irg);
217                 memslot = frame_alloc_area(frame_type, get_mode_size_bytes(mode_D), 4, 0);
218         }
219
220         attr->convs = xmalloc(attr->conv_count * sizeof(ir_node *));
221
222         for (i = 0, current_conv = attr->first_conv; i < attr->conv_count; i++, current_conv = current_conv->link)
223         {
224                 attr->convs[i] = current_conv;
225         }
226
227         qsort(attr->convs, attr->conv_count, sizeof(ir_node *),
228                 (int (*)(const void *, const void *)) nodes_dependency_order);
229
230         tenv.block    = current_block;
231         tenv.irg      = current_ir_graph;
232         DEBUG_ONLY(tenv.mod      = cgenv->mod;)
233
234         memory = get_irg_no_mem(current_ir_graph);
235         for(i = 0; i < attr->conv_count; i++)
236         {
237                 tenv.dbg      = get_irn_dbg_info(attr->convs[i]);
238                 tenv.irn      = attr->convs[i];
239                 tenv.mode     = get_irn_mode(attr->convs[i]);
240
241                 exchange(attr->convs[i], gen_Conv(&tenv, get_Conv_op(attr->convs[i])));
242         }
243 }
244
245 void init_block(void)
246 {
247         cw_block_attr *attr = xmalloc(sizeof(cw_block_attr));
248         attr->first_conv    = NULL;
249         attr->convs         = NULL; /* attr->convs is set in finalize_block() */
250         attr->conv_count    = 0;
251         current_block->link = attr;
252 }
253
254 /**
255  * Constant generating code
256  */
257
258 #if 0
259 struct tv_ent {
260         ir_entity *ent;
261         tarval *tv;
262 };
263
264 /* Compares two (entity, tarval) combinations */
265 static int cmp_tv_ent(const void *a, const void *b, size_t len) {
266         const struct tv_ent *e1 = a;
267         const struct tv_ent *e2 = b;
268
269         return !(e1->tv == e2->tv);
270 }
271
272 /* Generates a SymConst node for a known FP const */
273 static ir_node *gen_fp_known_symconst(ppc32_transform_env_t *env, tarval *known_const) {
274         static set    *const_set = NULL;
275         static ir_type *tp = NULL;
276         struct tv_ent  key;
277         struct tv_ent *entry;
278         ir_node       *cnst;
279         ir_graph      *rem;
280         ir_entity     *ent;
281
282         if(!const_set)
283                 const_set = new_set(cmp_tv_ent, 10);
284         if(!tp)
285                 tp = new_type_primitive(new_id_from_str("const_double_t"), env->mode);
286
287
288         key.tv  = known_const;
289         key.ent = NULL;
290
291         entry = set_insert(const_set, &key, sizeof(key), HASH_PTR(key.tv));
292
293         if(!entry->ent) {
294                 char buf[80];
295                 sprintf(buf, "const_%ld", get_irn_node_nr(env->irn));
296                 ent = new_entity(get_glob_type(), new_id_from_str(buf), tp);
297
298                 set_entity_ld_ident(ent, get_entity_ident(ent));
299                 set_entity_visibility(ent, visibility_local);
300                 set_entity_variability(ent, variability_constant);
301                 set_entity_allocation(ent, allocation_static);
302
303                 /* we create a new entity here: It's initialization must resist on the
304                     const code irg */
305                 rem = current_ir_graph;
306                 current_ir_graph = get_const_code_irg();
307                 cnst = new_Const(env->mode, key.tv);
308                 current_ir_graph = rem;
309
310                 set_atomic_ent_value(ent, cnst);
311
312                 /* set the entry for hashmap */
313                 entry->ent = ent;
314         }
315
316         return new_rd_SymConst_addr_ent(env->dbg, env->irg, ent, tp);
317 }
318 #endif
319
320 /**
321  * Transforms a Const
322  *
323  * @param env transformation environment
324  * @return the created ppc Const node
325  */
326 static ir_node *gen_Const(ppc32_transform_env_t *env) {
327         tarval *tv_const = get_Const_tarval(env->irn);
328         ir_node *constant;
329
330         if (mode_is_float(env->mode))
331                 constant = new_rd_ppc32_fConst(env->dbg, env->irg, env->block, env->mode);
332         else
333                 constant = new_rd_ppc32_Const(env->dbg, env->irg, env->block, env->mode);
334         set_ppc32_constant_tarval(constant, tv_const);
335         return constant;
336 }
337
338 /**
339  * Transforms a SymConst.
340  *
341  * @param env transformation environment
342  * @return the created ppc SymConst node
343  */
344 static ir_node *gen_SymConst(ppc32_transform_env_t *env) {
345         ir_node *symconst;
346         symconst = new_rd_ppc32_SymConst(env->dbg, env->irg, env->block, env->mode);
347         set_ppc32_frame_entity(symconst, get_SymConst_entity(env->irn));
348         return symconst;
349 }
350
351 /*********************************************************
352  *                  _             _      _
353  *                 (_)           | |    (_)
354  *  _ __ ___   __ _ _ _ __     __| |_ __ ___   _____ _ __
355  * | '_ ` _ \ / _` | | '_ \   / _` | '__| \ \ / / _ \ '__|
356  * | | | | | | (_| | | | | | | (_| | |  | |\ V /  __/ |
357  * |_| |_| |_|\__,_|_|_| |_|  \__,_|_|  |_| \_/ \___|_|
358  *
359  *********************************************************/
360
361
362
363 /**
364  * Transforms all conv nodes into ppc convs before abi
365  *
366  * @param node    the firm node
367  * @param env     the debug module
368  */
369 void ppc32_conv_walk(ir_node *node, void *env) {
370         ppc32_code_gen_t *cgenv = (ppc32_code_gen_t *)env;
371         ir_opcode  code         = get_irn_opcode(node);
372         ppc32_transform_env_t tenv;
373
374         if (is_Block(node))
375         {
376                 if(current_block != NULL)
377                         finalize_block(cgenv);
378
379                 current_block = node;
380                 init_block();
381
382                 return;
383         }
384
385         tenv.irg = current_ir_graph;
386         DEBUG_ONLY(tenv.mod = cgenv->mod;)
387
388         if (code == iro_Conv)
389         {
390                 ir_modecode from_mode=get_mode_modecode(get_irn_mode(get_irn_n(node,0)));
391                 ir_modecode to_mode=get_mode_modecode(get_irn_mode(node));
392                 cw_block_attr *attr;
393
394                 if(from_mode == to_mode) return;
395                 if(from_mode == irm_F || from_mode == irm_D)
396                 {
397                         switch(to_mode)
398                         {
399                                 case irm_Bs:
400                                 case irm_Bu:
401                                 case irm_Hs:
402                                 case irm_Hu:
403                                 case irm_Is:
404                                 case irm_Iu:
405                                         break;
406                                 default:
407                                         return;
408
409                         }
410                 }
411                 else if(to_mode == irm_F || to_mode == irm_D)
412                 {
413                         switch(from_mode)
414                         {
415                                 case irm_Bs:
416                                 case irm_Bu:
417                                 case irm_Hs:
418                                 case irm_Hu:
419                                 case irm_Is:
420                                 case irm_Iu:
421                                         break;
422                                 default:
423                                         return;
424                         }
425                 }
426                 else return;
427
428                 /* in Liste eintragen */
429                 attr = get_irn_link(current_block);
430                 set_irn_link(node, attr->first_conv);
431                 attr->first_conv = node;
432                 attr->conv_count++;
433                 conv_nodes_found++;
434         }
435         else if (code == iro_Call) {
436                 int i, size = 0;
437                 ir_type *tp = get_Call_type(node);
438                 ir_type *ptp;
439                 int stack_alignment = 4;
440
441                 for (i = get_Call_n_params(node) - 1; i >= 0; --i) {
442                         ir_mode *mode = get_irn_mode(get_Call_param(node, i));
443                         int s;
444
445                         if (mode_is_reference(mode)) {
446                                 /* might be a compound parameter */
447                                 ptp = get_method_param_type(tp, i);
448
449                                 if (is_compound_type(ptp)) {
450                                         s = (get_type_size_bytes(ptp) + stack_alignment - 1) & -stack_alignment;
451
452                                         size += s;
453                                         continue;
454                                 }
455                         }
456                         s = (get_mode_size_bytes(mode) + stack_alignment - 1) & -stack_alignment;
457                         size += s;
458                 }
459                 if ((unsigned) size > cgenv->area_size)
460                         cgenv->area_size = size;
461         }
462 }
463
464 /**
465  * Transforms all const nodes into ppc const nodes inside the using block
466  *
467  * @param node    the firm node
468  * @param env     the debug module
469  */
470 void ppc32_pretransform_walk(ir_node *node, void *env) {
471         ppc32_code_gen_t *cgenv = (ppc32_code_gen_t *)env;
472         ir_opcode  code         = get_irn_opcode(node);
473         ppc32_transform_env_t tenv;
474
475         if (is_Block(node))
476         {
477                 current_block = node;
478                 return;
479         }
480
481         tenv.irg = current_ir_graph;
482         DEBUG_ONLY(tenv.mod = cgenv->mod;)
483
484         if(code == iro_Const || code == iro_SymConst)
485         {
486                 ir_node *newconst;
487
488                 tenv.block    = cgenv->start_succ_block;
489                 tenv.irn      = node;
490                 tenv.mode     = get_irn_mode(node);
491                 tenv.dbg      = get_irn_dbg_info(node);
492
493                 if(code == iro_Const)
494                         newconst = gen_Const(&tenv);
495                 else
496                         newconst = gen_SymConst(&tenv);
497
498                 exchange(node, newconst);
499         }
500 }