mark methods with lowered struct params, correctly handle x86 abi for this case
[libfirm] / ir / lower / lower_calls.c
1 /*
2  * Copyright (C) 1995-2007 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   Lowering of Calls with compound parameters and return types.
23  * @author  Michael Beck
24  * @version $Id$
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include "lowering.h"
32 #include "irprog_t.h"
33 #include "irnode_t.h"
34 #include "type_t.h"
35 #include "irmode_t.h"
36 #include "ircons.h"
37 #include "irgmod.h"
38 #include "irgwalk.h"
39 #include "irtools.h"
40 #include "iroptimize.h"
41 #include "array.h"
42 #include "pmap.h"
43 #include "xmalloc.h"
44
45 /** A type map for def_find_pointer_type. */
46 static pmap *type_map;
47
48 /**
49  * Default implementation for finding a pointer type for a given element type.
50  * Simple create a new one.
51  */
52 static ir_type *def_find_pointer_type(ir_type *e_type, ir_mode *mode, int alignment)
53 {
54   ir_type *res;
55   pmap_entry *e;
56
57   /* Mode and alignment are always identical in all calls to def_find_pointer_type(), so
58      we simply can use a map from the element type to the pointer type. */
59   e = pmap_find(type_map, e_type);
60   if (e)
61     res = e->value;
62   else {
63     res = new_type_pointer(mangle_u(get_type_ident(e_type), new_id_from_chars("Ptr", 3)), e_type, mode);
64     set_type_alignment_bytes(res, alignment);
65     pmap_insert(type_map, e_type, res);
66   }
67   return res;
68 }
69
70 /**
71  * Creates a new lowered type for a method type with compound
72  * arguments. The new type is associated to the old one and returned.
73  *
74  * @param lp   parameter struct
75  * @param mtp  the method type to lower
76  *
77  * The current implementation expects that a lowered type already
78  * includes the necessary changes ...
79  */
80 static ir_type *create_modified_mtd_type(const lower_params_t *lp, ir_type *mtp)
81 {
82   ir_type *lowered, *ptr_tp;
83   ir_type **params, **results, *res_tp;
84   ir_mode *modes[MAX_REGISTER_RET_VAL];
85   int n_ress, n_params, nn_ress, nn_params, i, first_variadic;
86   ident *id;
87   add_hidden hidden_params;
88   variadicity var;
89
90   if (is_lowered_type(mtp)) {
91     /* the type is already lowered. Not handled yet. */
92     assert(0 && "lowered types NYI");
93   }
94
95   lowered = get_associated_type(mtp);
96   if (lowered)
97     return lowered;
98
99   n_ress   = get_method_n_ress(mtp);
100   NEW_ARR_A(ir_type *, results, n_ress);
101
102   n_params = get_method_n_params(mtp);
103   NEW_ARR_A(ir_type *, params, n_params + n_ress);
104
105   first_variadic = get_method_first_variadic_param_index(mtp);
106
107   hidden_params = lp->hidden_params;
108   if (hidden_params == ADD_HIDDEN_SMART &&
109     get_method_variadicity(mtp) == variadicity_variadic)
110         hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
111
112   if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
113     /* add hidden in front */
114     for (nn_ress = nn_params = i = 0; i < n_ress; ++i) {
115       res_tp = get_method_res_type(mtp, i);
116
117       if (is_compound_type(res_tp)) {
118         int n_regs = 0;
119
120         if (lp->flags & LF_SMALL_CMP_IN_REGS)
121           n_regs = lp->ret_compound_in_regs(res_tp, modes);
122
123         if (n_regs > 0) {
124           /* this compound will be returned solely in registers */
125           assert(0);
126         }
127         else {
128           /* this compound will be allocated on callers stack and its
129              address will be transmitted as a hidden parameter. */
130           ptr_tp = lp->find_pointer_type(res_tp, get_modeP_data(), lp->def_ptr_alignment);
131           params[nn_params++] = ptr_tp;
132           if (lp->flags & LF_RETURN_HIDDEN)
133             results[nn_ress++] = ptr_tp;
134         }
135       }
136       else
137         results[nn_ress++] = res_tp;
138     }
139
140     /* move the index of the first variadic parameter */
141     first_variadic += nn_params;
142
143     for (i = 0; i < n_params; ++i)
144       params[nn_params++] = get_method_param_type(mtp, i);
145   }
146   else {
147     /* add hidden parameters last */
148     assert(get_method_variadicity(mtp) == variadicity_non_variadic &&
149       "Cannot add hidden parameters at end of variadic function");
150
151     for (nn_params = 0; nn_params < n_params; ++nn_params)
152       params[nn_params] = get_method_param_type(mtp, nn_params);
153
154     for (nn_ress = i = 0; i < n_ress; ++i) {
155       res_tp = get_method_res_type(mtp, i);
156
157       if (is_compound_type(res_tp))
158         params[nn_params++] = lp->find_pointer_type(res_tp, get_modeP_data(), lp->def_ptr_alignment);
159       else
160         results[nn_ress++] = res_tp;
161     }
162   }
163
164   /* create the new type */
165   id = mangle_u(new_id_from_chars("L", 1), get_type_ident(mtp));
166   lowered = new_d_type_method(id, nn_params, nn_ress, get_type_dbg_info(mtp));
167
168   /* fill it */
169   for (i = 0; i < nn_params; ++i)
170     set_method_param_type(lowered, i, params[i]);
171   for (i = 0; i < nn_ress; ++i)
172     set_method_res_type(lowered, i, results[i]);
173
174   var = get_method_variadicity(mtp);
175   set_method_variadicity(lowered, var);
176   if (var == variadicity_variadic)
177     set_method_first_variadic_param_index(lowered, first_variadic);
178
179   /* associate the lowered type with the original one for easier access */
180   set_method_calling_convention(lowered,
181                   get_method_calling_convention(mtp) | cc_compound_ret);
182   set_lowered_type(mtp, lowered);
183
184   return lowered;
185 }
186
187 /**
188  * A call list entry.
189  */
190 typedef struct cl_entry cl_entry;
191 struct cl_entry {
192   cl_entry *next;   /**< Pointer to the next entry. */
193   ir_node  *call;   /**< Pointer to the Call node. */
194   ir_node  *copyb;  /**< List of all CopyB nodes. */
195 };
196
197 /**
198  * Walker environment for fix_args_and_collect_calls().
199  */
200 typedef struct _wlk_env_t {
201   int                  arg_shift;     /**< The Argument index shift for parameters. */
202   int                  first_hidden;  /**< The index of the first hidden argument. */
203   struct obstack       obst;          /**< An obstack to allocate the data on. */
204   cl_entry             *cl_list;      /**< The call list. */
205   pmap                 *dummy_map;    /**< A map for finding the dummy arguments. */
206   unsigned             dnr;           /**< The dummy index number. */
207   const lower_params_t *params;       /**< lowering parameters */
208 } wlk_env;
209
210 /**
211  * Return the call list entry of a call node.
212  * If no entry exists yet, allocate one and enter the node into
213  * the call list of the environment.
214  *
215  * @param call   A Call node.
216  * @param env    The environment.
217  */
218 static cl_entry *get_Call_entry(ir_node *call, wlk_env *env) {
219   cl_entry *res = get_irn_link(call);
220   if (res == NULL) {
221     cl_entry *res = obstack_alloc(&env->obst, sizeof(*res));
222     res->next  = env->cl_list;
223     res->call  = call;
224     res->copyb = NULL;
225     set_irn_link(call, res);
226     env->cl_list = res;
227   }
228   return res;
229 }
230
231 /**
232  * Post walker: shift all parameter indeces
233  * and collect Calls with compound returns in the call list.
234  */
235 static void fix_args_and_collect_calls(ir_node *n, void *ctx) {
236   wlk_env *env = ctx;
237   int i;
238   ir_type *ctp;
239   ir_op *op = get_irn_op(n);
240
241   if (env->arg_shift > 0 && op == op_Proj) {
242     ir_node *pred = get_Proj_pred(n);
243
244     /* Fix the argument numbers */
245     if (pred == get_irg_args(current_ir_graph)) {
246       long pnr = get_Proj_proj(n);
247       set_Proj_proj(n, pnr + env->arg_shift);
248     }
249   }
250   else if (op == op_Call) {
251     ctp = get_Call_type(n);
252     if (env->params->flags & LF_COMPOUND_RETURN) {
253       /* check for compound returns */
254       for (i = get_method_n_ress(ctp) -1; i >= 0; --i) {
255         if (is_compound_type(get_method_res_type(ctp, i))) {
256           /*
257            * This is a call with a compound return. As the result
258            * might be ignored, we must put it in the list.
259            */
260           (void)get_Call_entry(n, env);
261           break;
262         }
263       }
264     }
265   }
266   else if (op == op_CopyB && env->params->flags & LF_COMPOUND_RETURN) {
267     /* check for compound returns */
268     ir_node *src = get_CopyB_src(n);
269     /* older scheme using value_res_ent */
270     if (is_Sel(src)) {
271       ir_node *proj = get_Sel_ptr(src);
272       if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_P_value_res_base) {
273         ir_node *call = get_Proj_pred(proj);
274         if (is_Call(call)) {
275           /* found a CopyB from compound Call result */
276           cl_entry *e = get_Call_entry(call, env);
277           set_irn_link(n, e->copyb);
278           e->copyb = n;
279         }
280       }
281     } else
282     /* new scheme: compound results are determined by the call type only */
283     if (is_Proj(src)) {
284       ir_node *proj = get_Proj_pred(src);
285       if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_T_result) {
286         ir_node *call = get_Proj_pred(proj);
287         if (is_Call(call)) {
288           ctp = get_Call_type(call);
289           if (is_compound_type(get_method_res_type(ctp, get_Proj_proj(src)))) {
290             /* found a CopyB from compound Call result */
291             cl_entry *e = get_Call_entry(call, env);
292             set_irn_link(n, e->copyb);
293             e->copyb = n;
294           }
295         }
296       }
297     }
298   }
299 }
300
301 /**
302  * Returns non-zero if a node is a compound address
303  * of a frame-type entity.
304  *
305  * @param ft   the frame type
306  * @param adr  the node
307  */
308 static int is_compound_address(ir_type *ft, ir_node *adr)
309 {
310   ir_entity *ent;
311
312   if (! is_Sel(adr))
313     return 0;
314   if (get_Sel_n_indexs(adr) != 0)
315     return 0;
316   ent = get_Sel_entity(adr);
317   return get_entity_owner(ent) == ft;
318 }
319
320 /** A pair for the copy-return-optimization. */
321 typedef struct cr_pair {
322   ir_entity *ent; /**< the entity than can be removed from the frame */
323   ir_node *arg;   /**< the argument that replaces the entities address */
324 } cr_pair;
325
326 /**
327  * Post walker: fixes all entities addresses for the copy-return
328  * optimization.
329  *
330  * Note: We expect the length of the cr_pair array (ie number of compound
331  * return values) to be 1 (C, C++) in almost all cases, so ignore the
332  * linear search complexity here.
333  */
334 static void do_copy_return_opt(ir_node *n, void *ctx) {
335   cr_pair *arr = ctx;
336   int i;
337
338   if (is_Sel(n)) {
339     ir_entity *ent = get_Sel_entity(n);
340
341     for (i = ARR_LEN(arr) - 1; i >= 0; --i) {
342       if (ent == arr[i].ent) {
343         exchange(n, arr[i].arg);
344         break;
345       }
346     }
347   }
348 }
349
350 /**
351  * Return a Sel node that selects a dummy argument of type tp.
352  * Dummy arguments are only needed once and we use a map
353  * to store them.
354  * We could even assign all dummy arguments the same offset
355  * in the frame type ...
356  *
357  * @param irg    the graph
358  * @param block  the block where a newly create Sel should be placed
359  * @param tp     the type of the dummy entity that should be create
360  * @param env    the environment
361  */
362 static ir_node *get_dummy_sel(ir_graph *irg, ir_node *block, ir_type *tp, wlk_env *env)
363 {
364   ir_entity *ent;
365   pmap_entry *e;
366
367   /* use a map the check if we already create such an entity */
368   e = pmap_find(env->dummy_map, tp);
369   if (e)
370     ent = e->value;
371   else {
372     ir_type *ft = get_irg_frame_type(irg);
373     char buf[16];
374
375     snprintf(buf, sizeof(buf), "dummy.%u", env->dnr++);
376     ent = new_entity(ft, new_id_from_str(buf), tp);
377     pmap_insert(env->dummy_map, tp, ent);
378
379     if (get_type_state(ft) == layout_fixed) {
380       /* Fix the layout again */
381       assert(0 && "Fixed layout not implemented");
382     }
383   }
384   return new_r_simpleSel(
385     irg,
386     block,
387     get_irg_no_mem(irg),
388     get_irg_frame(irg),
389     ent);
390 }
391
392 /**
393  * Add the hidden parameter from the CopyB node to the Call node.
394  *
395  * @param irg    the graph
396  * @param n_com  number of compound results (will be number of hidden parameters)
397  * @param ins    in array to store the hidden parameters into
398  * @param entry  the call list
399  * @param env    the environment
400  */
401 static void add_hidden_param(ir_graph *irg, int n_com, ir_node **ins, cl_entry *entry, wlk_env *env)
402 {
403   ir_node *p, *n, *src, *mem, *blk;
404   ir_entity *ent;
405   ir_type *owner;
406   int idx, n_args;
407
408   n_args = 0;
409   for (p = entry->copyb; p; p = n) {
410     n   = get_irn_link(p);
411     src = get_CopyB_src(p);
412
413     /* old scheme using value_res_ent */
414     if (is_Sel(src)) {
415       ent = get_Sel_entity(src);
416       owner = get_entity_owner(ent);
417
418       /* find the hidden parameter index */
419       for (idx = 0; idx < get_struct_n_members(owner); ++idx)
420         if (get_struct_member(owner, idx) == ent)
421           break;
422       assert(idx < get_struct_n_members(owner));
423     }
424     else
425
426     /* new scheme: compound returns are determined by the call type and are Proj's */
427     idx = get_Proj_proj(src);
428
429     ins[idx] = get_CopyB_dst(p);
430     mem      = get_CopyB_mem(p);
431     blk      = get_nodes_block(p);
432
433     /* get rid of the CopyB */
434     turn_into_tuple(p, pn_CopyB_max);
435     set_Tuple_pred(p, pn_CopyB_M_regular, mem);
436     set_Tuple_pred(p, pn_CopyB_M_except,  get_irg_bad(irg));
437     set_Tuple_pred(p, pn_CopyB_X_regular, new_r_Jmp(irg, blk));
438     set_Tuple_pred(p, pn_CopyB_X_except,  get_irg_bad(irg));
439     ++n_args;
440   }
441
442   /* now create dummy entities for function with ignored return value */
443   if (n_args < n_com) {
444     ir_type *ctp = get_Call_type(entry->call);
445     int i, j;
446
447     if (is_lowered_type(ctp))
448       ctp = get_associated_type(ctp);
449
450     for (j = i = 0; i < get_method_n_ress(ctp); ++i) {
451       ir_type *rtp = get_method_res_type(ctp, i);
452       if (is_compound_type(rtp)) {
453         if (ins[j] == NULL)
454           ins[j] = get_dummy_sel(irg, get_nodes_block(entry->call), rtp, env);
455         ++j;
456       }
457     }
458   }
459 }
460
461 /**
462  * Fix all calls on a call list by adding hidden parameters.
463  *
464  * @param irg  the graph
465  * @param env  the environment
466  */
467 static void fix_call_list(ir_graph *irg, wlk_env *env) {
468   const lower_params_t *lp = env->params;
469   cl_entry *p;
470   ir_node *call, **new_in;
471   ir_type *ctp, *lowered_mtp;
472   add_hidden hidden_params;
473   int i, n_params, n_com, pos;
474
475   new_in = NEW_ARR_F(ir_node *, 0);
476   for (p = env->cl_list; p; p = p->next) {
477     call = p->call;
478     ctp = get_Call_type(call);
479     lowered_mtp = create_modified_mtd_type(lp, ctp);
480     set_Call_type(call, lowered_mtp);
481
482     hidden_params = lp->hidden_params;
483     if (hidden_params == ADD_HIDDEN_SMART &&
484       get_method_variadicity(ctp) == variadicity_variadic)
485       hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
486
487     n_params = get_Call_n_params(call);
488
489     n_com = 0;
490     for (i = get_method_n_ress(ctp) - 1; i >= 0; --i) {
491       if (is_compound_type(get_method_res_type(ctp, i)))
492         ++n_com;
493     }
494     pos = 2;
495     ARR_RESIZE(ir_node *, new_in, n_params + n_com + pos);
496     memset(new_in, 0, sizeof(*new_in) * (n_params + n_com + pos));
497     if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
498       add_hidden_param(irg, n_com, &new_in[pos], p, env);
499       pos += n_com;
500     }
501     /* copy all other parameters */
502     for (i = 0; i < n_params; ++i)
503       new_in[pos++] = get_Call_param(call, i);
504     if (hidden_params == ADD_HIDDEN_ALWAYS_LAST) {
505       add_hidden_param(irg, n_com, &new_in[pos], p, env);
506       pos += n_com;
507     }
508     new_in[0] = get_Call_mem(call);
509     new_in[1] = get_Call_ptr(call);
510
511     set_irn_in(call, n_params + n_com + 2, new_in);
512   }
513 }
514
515 /**
516  * Transform a graph. If it has compound parameter returns,
517  * remove them and use the hidden parameter instead.
518  * If it calls methods with compound parameter returns, add hidden
519  * parameters.
520  *
521  * @param lp   parameter struct
522  * @param irg  the graph to transform
523  */
524 static void transform_irg(const lower_params_t *lp, ir_graph *irg)
525 {
526   ir_entity *ent = get_irg_entity(irg);
527   ir_type *mtp, *lowered_mtp, *tp, *ft;
528   int i, j, k, n_ress = 0, n_ret_com = 0, n_cr_opt;
529   ir_node **new_in, *ret, *endbl, *bl, *mem, *copy;
530   cr_pair *cr_opt;
531   wlk_env env;
532   add_hidden hidden_params;
533
534   assert(ent && "Cannot tranform graph without an entity");
535   assert(get_irg_phase_state(irg) == phase_high && "call lowering must be done in phase high");
536
537   mtp = get_entity_type(ent);
538
539   if (lp->flags & LF_COMPOUND_RETURN) {
540     /* calculate the number of compound returns */
541     n_ress = get_method_n_ress(mtp);
542     for (n_ret_com = i = 0; i < n_ress; ++i) {
543       tp = get_method_res_type(mtp, i);
544
545       if (is_compound_type(tp))
546         ++n_ret_com;
547     }
548   }
549
550   if (n_ret_com) {
551     /* much easier if we have only one return */
552     normalize_one_return(irg);
553
554     /* This graph has a compound argument. Create a new type */
555     lowered_mtp = create_modified_mtd_type(lp, mtp);
556     set_entity_type(ent, lowered_mtp);
557
558     hidden_params = lp->hidden_params;
559     if (hidden_params == ADD_HIDDEN_SMART &&
560       get_method_variadicity(mtp) == variadicity_variadic)
561       hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
562
563     if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) {
564       /* hidden arguments are added first */
565       env.arg_shift    = n_ret_com;
566       env.first_hidden = 0;
567     }
568     else {
569       /* hidden arguments are added last */
570       env.arg_shift    = 0;
571       env.first_hidden = get_method_n_params(mtp);
572     }
573   }
574   else {
575     /* we must only search for calls */
576     env.arg_shift = 0;
577   }
578   obstack_init(&env.obst);
579   env.cl_list   = NULL;
580   env.dummy_map = pmap_create_ex(8);
581   env.dnr       = 0;
582   env.params    = lp;
583
584   /* scan the code, fix argument numbers and collect calls. */
585   irg_walk_graph(irg, firm_clear_link, fix_args_and_collect_calls, &env);
586
587   /* fix all calls */
588   if (env.cl_list)
589     fix_call_list(irg, &env);
590
591   if (n_ret_com) {
592     /*
593      * Now fix the Return node of the current graph.
594      */
595
596     /* STEP 1: find the return. This is simple, we have normalized the graph. */
597     endbl = get_irg_end_block(irg);
598     ret = NULL;
599     for (i = get_Block_n_cfgpreds(endbl) - 1; i >= 0; --i) {
600       ir_node *pred = get_Block_cfgpred(endbl, i);
601
602       if (is_Return(pred)) {
603         ret = pred;
604         break;
605       }
606     }
607     /* there should always be a return */
608     assert(ret);
609
610     /*
611      * STEP 2: fix it. For all compound return values add a CopyB,
612      * all others are copied.
613      */
614     NEW_ARR_A(ir_node *, new_in, n_ress + 1);
615
616     bl  = get_nodes_block(ret);
617     mem = get_Return_mem(ret);
618
619     ft = get_irg_frame_type(irg);
620     NEW_ARR_A(cr_pair, cr_opt, n_ret_com);
621     n_cr_opt = 0;
622     for (j = 1, i = k = 0; i < n_ress; ++i) {
623       ir_node *pred = get_Return_res(ret, i);
624       tp = get_method_res_type(mtp, i);
625
626       if (is_compound_type(tp)) {
627         ir_node *arg = get_irg_args(irg);
628         arg = new_r_Proj(irg, get_nodes_block(arg), arg, mode_P_data, env.first_hidden + k);
629         ++k;
630
631         if (is_compound_address(ft, pred)) {
632           /* we can do the copy-return optimization here */
633           cr_opt[n_cr_opt].ent = get_Sel_entity(pred);
634           cr_opt[n_cr_opt].arg = arg;
635           ++n_cr_opt;
636         }
637         else { /* copy-return optimization is impossible, do the copy. */
638           copy = new_r_CopyB(
639                   irg, bl,
640                   mem,
641                   arg,
642                   pred,
643                   tp
644                  );
645           mem = new_r_Proj(irg, bl, copy, mode_M, pn_CopyB_M_regular);
646         }
647         if (lp->flags & LF_RETURN_HIDDEN) {
648           new_in[j] = arg;
649           ++j;
650         }
651       }
652       else { /* scalar return value */
653         new_in[j] = pred;
654         ++j;
655       }
656     }
657     /* replace the in of the Return */
658     new_in[0] = mem;
659     set_irn_in(ret, j, new_in);
660
661     if (n_cr_opt > 0) {
662       irg_walk_graph(irg, NULL, do_copy_return_opt, cr_opt);
663
664       for (i = ARR_LEN(cr_opt) - 1; i >= 0; --i) {
665         remove_class_member(ft, cr_opt[i].ent);
666       }
667     }
668   } /* if (n_ret_com) */
669
670   pmap_destroy(env.dummy_map);
671   obstack_free(&env.obst, NULL);
672 }
673
674 /**
675  * Returns non-zero if the given type is a method
676  * type that must be lowered.
677  *
678  * @param lp  lowering parameters
679  * @param tp  The type.
680  */
681 static int must_be_lowered(const lower_params_t *lp, ir_type *tp) {
682   int i, n_ress;
683   ir_type *res_tp;
684
685   if (is_Method_type(tp)) {
686     if (lp->flags & LF_COMPOUND_RETURN) {
687       /* check for compound returns */
688       n_ress = get_method_n_ress(tp);
689       for (i = 0; i < n_ress; ++i) {
690         res_tp = get_method_res_type(tp, i);
691
692         if (is_compound_type(res_tp))
693           return 1;
694       }
695     }
696   }
697   return 0;
698 }
699
700 /**
701  * type-walker: lower all method types of entities
702  * and points-to types.
703  */
704 static void lower_method_types(type_or_ent *tore, void *env)
705 {
706   const lower_params_t *lp = env;
707   ir_type *tp;
708
709   /* fix method entities */
710   if (is_entity(tore)) {
711     ir_entity *ent = (ir_entity *)tore;
712     tp = get_entity_type(ent);
713
714     if (must_be_lowered(lp, tp)) {
715       tp = create_modified_mtd_type(lp, tp);
716       set_entity_type(ent, tp);
717     }
718   }
719   else {
720     tp = (ir_type *)tore;
721
722     /* fix pointer to methods */
723     if (is_Pointer_type(tp)) {
724       ir_type *etp = get_pointer_points_to_type(tp);
725       if (must_be_lowered(lp, etp)) {
726         etp = create_modified_mtd_type(lp, etp);
727         set_pointer_points_to_type(tp, etp);
728       }
729     }
730   }
731 }
732
733 /*
734  * Lower calls with compound parameters and return types.
735  * This function does the following transformations:
736  *
737  * - Adds a new (hidden) pointer parameter for
738  *   any return compound type.
739  *
740  * - Use of the hidden parameters in the function code.
741  *
742  * - Change all calls to functions with compound return
743  *   by providing space for the hidden parameter on the callers
744  *   stack.
745  *
746  * - Replace a possible block copy after the function call.
747  */
748 void lower_calls_with_compounds(const lower_params_t *params)
749 {
750   int i;
751   ir_graph *irg;
752   lower_params_t param = *params;
753
754   if (param.find_pointer_type == NULL) {
755     param.find_pointer_type = def_find_pointer_type;
756     type_map = pmap_create_ex(8);
757   }
758   else
759     type_map = NULL;
760
761   /* first step: Transform all graphs */
762   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
763     irg = get_irp_irg(i);
764
765     if (irg == get_const_code_irg())
766       continue;
767
768     transform_irg(&param, irg);
769   }
770
771   /* second step: Lower all method types of visible entities */
772   type_walk(NULL, lower_method_types, &param);
773
774   if (type_map)
775     pmap_destroy(type_map);
776 }