+
+#define LMAX(a, b) ((a) > (b) ? (a) : (b))
+
+/* A mapper for mapping unsupported instructions to runtime calls. */
+int i_mapper_RuntimeCall(ir_node *node, runtime_rt *rt) {
+ int i, j, arity, first, n_param, n_res;
+ long n_proj;
+ ir_type *mtp;
+ ir_node *mem, *bl, *call, *addr, *res_proj;
+ ir_node **in;
+ ir_graph *irg;
+ symconst_symbol sym;
+
+ /* check if the result modes match */
+ if (get_irn_mode(node) != rt->mode)
+ return 0;
+
+ arity = get_irn_arity(node);
+ if (arity <= 0)
+ return 0;
+
+ mtp = get_entity_type(rt->ent);
+ n_param = get_method_n_params(mtp);
+ irg = current_ir_graph;
+
+ mem = get_irn_n(node, 0);
+ if (get_irn_mode(mem) != mode_M) {
+ mem = new_r_NoMem(irg);
+ first = 0;
+ }
+ else
+ first = 1;
+
+ /* check if the modes of the predecessors match the parameter modes */
+ if (arity - first != n_param)
+ return 0;
+
+ for (i = first, j = 0; i < arity; ++i, ++j) {
+ ir_type *param_tp = get_method_param_type(mtp, j);
+ ir_node *pred = get_irn_n(node, i);
+
+ if (get_type_mode(param_tp) != get_irn_mode(pred))
+ return 0;
+ }
+
+ n_res = get_method_n_ress(mtp);
+
+ /* step 0: calculate the number of needed Proj's */
+ n_proj = 0;
+ n_proj = LMAX(n_proj, rt->mem_proj_nr + 1);
+ n_proj = LMAX(n_proj, rt->exc_proj_nr + 1);
+ n_proj = LMAX(n_proj, rt->exc_mem_proj_nr + 1);
+ n_proj = LMAX(n_proj, rt->res_proj_nr + 1);
+
+ if (n_proj > 0) {
+ if (rt->mode != mode_T) /* must be mode_T */
+ return 0;
+ }
+ else {
+ if (n_res > 0)
+ /* must match */
+ if (get_type_mode(get_method_res_type(mtp, 0)) != rt->mode)
+ return 0;
+ }
+
+ /* ok, when we are here, the number of predecessors match as well as the parameter modes */
+ bl = get_nodes_block(node);
+
+ in = NULL;
+ if (n_param > 0) {
+ NEW_ARR_A(ir_node *, in, n_param);
+ for (i = 0; i < n_param; ++i)
+ in[i] = get_irn_n(node, first + i);
+ }
+
+ /* step 1: create the call */
+ sym.entity_p = rt->ent;
+ addr = new_r_SymConst(irg, bl, sym, symconst_addr_ent);
+ call = new_rd_Call(get_irn_dbg_info(node), irg, bl, mem, addr, n_param, in, mtp);
+ set_irn_pinned(call, get_irn_pinned(node));
+
+ if (n_res > 0)
+ res_proj = new_r_Proj(irg, bl, call, mode_T, pn_Call_T_result);
+
+ if (n_proj > 0) {
+ n_proj += n_res - 1;
+
+ /* we are ready */
+ turn_into_tuple(node, n_proj);
+
+ for (i = 0; i < n_proj; ++i)
+ set_Tuple_pred(node, i, new_r_Bad(irg));
+ if (rt->mem_proj_nr >= 0)
+ set_Tuple_pred(node, rt->mem_proj_nr, new_r_Proj(irg, bl, call, mode_M, pn_Call_M_regular));
+ if (get_irn_op(mem) != op_NoMem) {
+ /* Exceptions can only be handled with real memory */
+ if (rt->exc_proj_nr >= 0)
+ set_Tuple_pred(node, rt->mem_proj_nr, new_r_Proj(irg, bl, call, mode_X, pn_Call_X_except));
+ if (rt->exc_mem_proj_nr >= 0)
+ set_Tuple_pred(node, rt->mem_proj_nr, new_r_Proj(irg, bl, call, mode_M, pn_Call_M_except));
+ }
+
+ if (rt->res_proj_nr >= 0)
+ for (i = 0; i < n_res; ++i)
+ set_Tuple_pred(node, rt->res_proj_nr + i,
+ new_r_Proj(irg, bl, res_proj, get_type_mode(get_method_res_type(mtp, i)), i));
+ return 1;
+ }
+ else {
+ /* only one return value supported */
+ if (n_res > 0) {
+ ir_mode *mode = get_type_mode(get_method_res_type(mtp, 0));
+
+ res_proj = new_r_Proj(irg, bl, call, mode_T, pn_Call_T_result);
+ res_proj = new_r_Proj(irg, bl, res_proj, mode, 0);
+
+ exchange(node, res_proj);
+ return 1;
+ }
+ }
+ /* should not happen */
+ return 0;
+}