- drop dependency of predefined modes
[libfirm] / ir / lower / lower_calls.c
index 02b572a..e0447c2 100644 (file)
@@ -24,9 +24,7 @@
  * @version $Id$
  */
 
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 
 #include "lowering.h"
 #include "irprog_t.h"
 #include "ircons.h"
 #include "irgmod.h"
 #include "irgwalk.h"
+#include "irmemory.h"
 #include "irtools.h"
 #include "iroptimize.h"
-#include "array.h"
+#include "array_t.h"
 #include "pmap.h"
-#include "xmalloc.h"
+#include "error.h"
 
 /** A type map for def_find_pointer_type. */
 static pmap *type_map;
@@ -60,7 +59,7 @@ static ir_type *def_find_pointer_type(ir_type *e_type, ir_mode *mode, int alignm
        if (e)
                res = e->value;
        else {
-               res = new_type_pointer(mangle_u(get_type_ident(e_type), new_id_from_chars("Ptr", 3)), e_type, mode);
+               res = new_type_pointer(id_mangle_u(get_type_ident(e_type), new_id_from_chars("Ptr", 3)), e_type, mode);
                set_type_alignment_bytes(res, alignment);
                pmap_insert(type_map, e_type, res);
        }
@@ -123,7 +122,7 @@ static ir_type *create_modified_mtd_type(const lower_params_t *lp, ir_type *mtp)
 
                                if (n_regs > 0) {
                                        /* this compound will be returned solely in registers */
-                                       assert(0);
+                                       panic("Returning compounds in registers not yet implemented");
                                }
                                else {
                                        /* this compound will be allocated on callers stack and its
@@ -164,7 +163,7 @@ static ir_type *create_modified_mtd_type(const lower_params_t *lp, ir_type *mtp)
        }
 
        /* create the new type */
-       id = mangle_u(new_id_from_chars("L", 1), get_type_ident(mtp));
+       id = id_mangle_u(new_id_from_chars("L", 1), get_type_ident(mtp));
        lowered = new_d_type_method(id, nn_params, nn_ress, get_type_dbg_info(mtp));
 
        /* fill it */
@@ -179,9 +178,10 @@ static ir_type *create_modified_mtd_type(const lower_params_t *lp, ir_type *mtp)
                set_method_first_variadic_param_index(lowered, first_variadic);
 
        /* associate the lowered type with the original one for easier access */
-       if(changed) {
+       if (changed) {
                set_method_calling_convention(lowered, get_method_calling_convention(mtp) | cc_compound_ret);
        }
+
        set_lowered_type(mtp, lowered);
 
        return lowered;
@@ -208,6 +208,8 @@ typedef struct _wlk_env_t {
        pmap                 *dummy_map;       /**< A map for finding the dummy arguments. */
        unsigned             dnr;              /**< The dummy index number. */
        const lower_params_t *params;          /**< Lowering parameters. */
+       ir_type              *lowered_mtp;     /**< The lowered method type of the current irg if any. */
+       ir_type              *value_params;    /**< The value params type if any. */
        unsigned             only_local_mem:1; /**< Set if only local memory access was found. */
        unsigned             changed:1;        /**< Set if the current graph was changed. */
 } wlk_env;
@@ -295,6 +297,20 @@ static void fix_args_and_collect_calls(ir_node *n, void *ctx) {
        ir_node *ptr;
 
        switch (get_irn_opcode(n)) {
+       case iro_Sel:
+               if (env->lowered_mtp != NULL && env->value_params != NULL) {
+                       ir_entity *ent = get_Sel_entity(n);
+
+                       if (get_entity_owner(ent) == env->value_params) {
+                               int pos = get_struct_member_index(env->value_params, ent) + env->arg_shift;
+                               ir_entity *new_ent;
+
+                               new_ent = get_method_value_param_ent(env->lowered_mtp, pos);
+                               set_entity_ident(new_ent, get_entity_ident(ent));
+                               set_Sel_entity(n, new_ent);
+                       }
+               }
+               break;
        case iro_Load:
        case iro_Store:
                if (env->only_local_mem) {
@@ -658,12 +674,15 @@ static void transform_irg(const lower_params_t *lp, ir_graph *irg)
        } else {
                /* we must only search for calls */
                env.arg_shift = 0;
+               lowered_mtp   = NULL;
        }
        obstack_init(&env.obst);
        env.cl_list        = NULL;
        env.dummy_map      = pmap_create_ex(8);
        env.dnr            = 0;
        env.params         = lp;
+       env.lowered_mtp    = lowered_mtp;
+       env.value_params   = get_method_value_param_type(mtp);
        env.only_local_mem = 1;
        env.changed        = 0;
 
@@ -807,14 +826,14 @@ static int must_be_lowered(const lower_params_t *lp, ir_type *tp) {
  * type-walker: lower all method types of entities
  * and points-to types.
  */
-static void lower_method_types(type_or_ent *tore, void *env)
+static void lower_method_types(type_or_ent tore, void *env)
 {
        const lower_params_t *lp = env;
        ir_type *tp;
 
        /* fix method entities */
-       if (is_entity(tore)) {
-               ir_entity *ent = (ir_entity *)tore;
+       if (is_entity(tore.ent)) {
+               ir_entity *ent = tore.ent;
                tp = get_entity_type(ent);
 
                if (must_be_lowered(lp, tp)) {
@@ -822,7 +841,7 @@ static void lower_method_types(type_or_ent *tore, void *env)
                        set_entity_type(ent, tp);
                }
        } else {
-               tp = (ir_type *)tore;
+               tp = tore.typ;
 
                /* fix pointer to methods */
                if (is_Pointer_type(tp)) {
@@ -866,9 +885,6 @@ void lower_calls_with_compounds(const lower_params_t *params)
        for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
                irg = get_irp_irg(i);
 
-               if (irg == get_const_code_irg())
-                       continue;
-
                transform_irg(&param, irg);
        }