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