+
+/**
+ * Calculate the value of strlen if possible.
+ *
+ * @param ent the entity
+ * @param res_tp the result type
+ *
+ * @return a Const node containing the strlen() result or NULL
+ * if the evaluation fails
+ */
+static ir_node *eval_strlen(ir_graph *irg, ir_entity *ent, ir_type *res_tp)
+{
+ ir_type *tp = get_entity_type(ent);
+ ir_mode *mode;
+ ir_initializer_t *initializer;
+ size_t size;
+ size_t i;
+
+ if (! is_Array_type(tp))
+ return NULL;
+ tp = get_array_element_type(tp);
+ if (! is_Primitive_type(tp))
+ return NULL;
+ mode = get_type_mode(tp);
+
+ /* FIXME: This is too restrict, as the type char might be more the 8bits */
+ if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
+ return NULL;
+
+ if (!has_entity_initializer(ent)) {
+ size_t i, n;
+
+ n = get_compound_ent_n_values(ent);
+ for (i = 0; i < n; ++i) {
+ ir_node *irn = get_compound_ent_value(ent, i);
+
+ if (! is_Const(irn))
+ return NULL;
+
+ if (is_Const_null(irn)) {
+ /* found the length */
+ ir_tarval *tv = new_tarval_from_long(i, get_type_mode(res_tp));
+ return new_r_Const(irg, tv);
+ }
+ }
+ return NULL;
+ }
+
+ initializer = get_entity_initializer(ent);
+ if (get_initializer_kind(initializer) != IR_INITIALIZER_COMPOUND)
+ return NULL;
+
+ size = get_initializer_compound_n_entries(initializer);
+ for (i = 0; i < size; ++i) {
+ ir_initializer_t *val = get_initializer_compound_value(initializer, i);
+ if (initializer_val_is_null(val)) {
+ ir_tarval *tv = new_tarval_from_long(i, get_type_mode(res_tp));
+ return new_r_Const(irg, tv);
+ }
+ }
+
+ return NULL;
+} /* eval_strlen */
+
+/* A mapper for strlen */
+int i_mapper_strlen(ir_node *call, void *ctx)
+{
+ ir_node *s = get_Call_param(call, 0);
+ ir_entity *ent = get_const_entity(s);
+
+ (void) ctx;
+
+ /* FIXME: this cannot handle constant strings inside struct initializers yet */
+ if (ent != NULL) {
+ /* a constant entity */
+ ir_type *tp = get_Call_type(call);
+ ir_node *irn;
+
+ tp = get_method_res_type(tp, 0);
+ irn = eval_strlen(get_irn_irg(call), ent, tp);
+
+ if (irn) {
+ ir_node *mem = get_Call_mem(call);
+ DBG_OPT_ALGSIM0(call, irn, FS_OPT_RTS_STRLEN);
+ replace_call(irn, call, mem, NULL, NULL);
+ return 1;
+ }
+ }
+ return 0;
+} /* i_mapper_strlen */
+
+/**
+ * Calculate the value of strlen if possible.
+ *
+ * @param left the left entity
+ * @param right the right entity
+ * @param res_tp the result type
+ *
+ * @return a Const node containing the strcmp() result or NULL
+ * if the evaluation fails
+ */
+static ir_node *eval_strcmp(ir_graph *irg, ir_entity *left, ir_entity *right,
+ ir_type *res_tp)
+{
+ ir_type *tp;
+ ir_mode *mode;
+
+ tp = get_entity_type(left);
+ if (! is_Array_type(tp))
+ return NULL;
+ tp = get_array_element_type(tp);
+ if (! is_Primitive_type(tp))
+ return NULL;
+ mode = get_type_mode(tp);
+
+ /* FIXME: This is too restrict, as the type char might be more the 8bits */
+ if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
+ return NULL;
+
+ tp = get_entity_type(right);
+ if (! is_Array_type(tp))
+ return NULL;
+ tp = get_array_element_type(tp);
+ if (! is_Primitive_type(tp))
+ return NULL;
+ mode = get_type_mode(tp);
+
+ /* FIXME: This is too restrict, as the type char might be more the 8bits */
+ if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
+ return NULL;
+
+ if (!has_entity_initializer(left) && !has_entity_initializer(right)) {
+ /* code that uses deprecated compound_graph_path stuff */
+ size_t n = get_compound_ent_n_values(left);
+ size_t n_r = get_compound_ent_n_values(right);
+ size_t i;
+ int res = 0;
+
+ if (n_r < n)
+ n = n_r;
+ for (i = 0; i < n; ++i) {
+ ir_node *irn;
+ long v_l, v_r;
+ ir_tarval *tv;
+
+ irn = get_compound_ent_value(left, i);
+ if (! is_Const(irn))
+ return NULL;
+ tv = get_Const_tarval(irn);
+ v_l = get_tarval_long(tv);
+
+ irn = get_compound_ent_value(right, i);
+ if (! is_Const(irn))
+ return NULL;
+ tv = get_Const_tarval(irn);
+ v_r = get_tarval_long(tv);
+
+ if (v_l < v_r) {
+ res = -1;
+ break;
+ }
+ if (v_l > v_r) {
+ res = +1;
+ break;
+ }
+
+ if (v_l == 0) {
+ res = 0;
+ break;
+ }
+ }
+ if (i < n) {
+ /* we found an end */
+ ir_tarval *tv = new_tarval_from_long(res, get_type_mode(res_tp));
+ return new_r_Const(irg, tv);
+ }
+ return NULL;
+ }
+
+ /* TODO */
+
+ return NULL;
+} /* eval_strcmp */
+
+/**
+ * Checks if an entity represents the empty string.
+ *
+ * @param ent the entity
+ *
+ * @return non-zero if ent represents the empty string
+ */
+static int is_empty_string(ir_entity *ent)
+{
+ ir_type *tp = get_entity_type(ent);
+ ir_mode *mode;
+ ir_node *irn;
+ ir_initializer_t *initializer;
+ ir_initializer_t *init0;
+
+ if (! is_Array_type(tp))
+ return 0;
+ tp = get_array_element_type(tp);
+ if (! is_Primitive_type(tp))
+ return 0;
+ mode = get_type_mode(tp);
+
+ /* FIXME: This is too restrict, as the type char might be more the 8bits */
+ if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
+ return 0;
+
+ if (!has_entity_initializer(ent)) {
+ /* code for deprecated compound_graph_path stuff */
+ size_t n = get_compound_ent_n_values(ent);
+ if (n < 1)
+ return 0;
+ irn = get_compound_ent_value(ent, 0);
+
+ return is_Const(irn) && is_Const_null(irn);
+ }
+
+ initializer = get_entity_initializer(ent);
+ if (get_initializer_kind(initializer) != IR_INITIALIZER_COMPOUND)
+ return 0;
+
+ if (get_initializer_compound_n_entries(initializer) < 1)
+ return 0;
+
+ init0 = get_initializer_compound_value(initializer, 0);
+ return initializer_val_is_null(init0);
+} /* is_empty_string */
+
+/* A mapper for strcmp */
+int i_mapper_strcmp(ir_node *call, void *ctx)
+{
+ ir_node *left = get_Call_param(call, 0);
+ ir_node *right = get_Call_param(call, 1);
+ ir_node *irn = NULL;
+ ir_node *exc = NULL;
+ ir_node *reg = NULL;
+ ir_type *call_tp = get_Call_type(call);
+ ir_type *res_tp = get_method_res_type(call_tp, 0);
+ ir_entity *ent_l, *ent_r;
+ ir_type *char_tp;
+ ir_node *v;
+
+ (void) ctx;
+
+ /* do some type checks first */
+ if (! is_Primitive_type(res_tp))
+ return 0;
+ char_tp = get_method_param_type(call_tp, 0);
+ if (char_tp != get_method_param_type(call_tp, 1))
+ return 0;
+ if (! is_Pointer_type(char_tp))
+ return 0;
+ char_tp = get_pointer_points_to_type(char_tp);
+
+ if (left == right) {
+ /* a strcmp(s, s) ==> 0 */
+ ir_graph *irg = get_irn_irg(call);
+ ir_node *mem = get_Call_mem(call);
+ ir_mode *mode = get_type_mode(res_tp);
+
+ irn = new_r_Const(irg, get_mode_null(mode));
+ DBG_OPT_ALGSIM0(call, irn, FS_OPT_RTS_STRCMP);
+ replace_call(irn, call, mem, NULL, NULL);
+ return 1;
+ }
+ ent_l = get_const_entity(left);
+ ent_r = get_const_entity(right);
+
+ if (ent_l != NULL && ent_r != NULL) {
+ /* both entities are const, try to evaluate */
+ irn = eval_strcmp(get_irn_irg(call), ent_l, ent_r, res_tp);
+ } else if (ent_l != NULL) {
+ if (is_empty_string(ent_l)) {
+ /* s strcmp("", s) ==> -(*s)*/
+ v = right;
+ goto replace_by_call;
+ }
+ } else if (ent_r != NULL) {
+ if (is_empty_string(ent_r)) {
+ /* s strcmp(s, "") ==> (*s) */
+ ir_node *mem, *block;
+ dbg_info *dbg;
+ ir_mode *mode;
+
+ v = left;
+replace_by_call:
+ mem = get_Call_mem(call);
+ block = get_nodes_block(call);
+ dbg = get_irn_dbg_info(call);
+ mode = get_type_mode(char_tp);
+
+ /* replace the strcmp by (*x) */
+ irn = new_rd_Load(dbg, block, mem, v, mode, cons_none);
+ mem = new_r_Proj(irn, mode_M, pn_Load_M);
+ exc = new_r_Proj(irn, mode_X, pn_Load_X_except);
+ reg = new_r_Proj(irn, mode_X, pn_Load_X_regular);
+ irn = new_r_Proj(irn, mode, pn_Load_res);
+
+ /* conv to the result mode */
+ mode = get_type_mode(res_tp);
+ irn = new_rd_Conv(dbg, block, irn, mode);
+
+ if (v == right) {
+ /* negate in the ("", s) case */
+ irn = new_rd_Minus(dbg, block, irn, mode);
+ }
+ }
+ }
+
+ if (irn != NULL) {
+ ir_node *mem = get_Call_mem(call);
+ DBG_OPT_ALGSIM0(call, irn, FS_OPT_RTS_STRCMP);
+ replace_call(irn, call, mem, reg, exc);
+ return 1;
+ }
+
+ return 0;
+} /* i_mapper_strcmp */
+
+/* A mapper for strncmp */
+int i_mapper_strncmp(ir_node *call, void *ctx)
+{
+ ir_node *left = get_Call_param(call, 0);
+ ir_node *right = get_Call_param(call, 1);
+ ir_node *len = get_Call_param(call, 2);
+ ir_node *irn;
+ (void) ctx;
+
+ if (left == right || (is_Const(len) && is_Const_null(len))) {
+ /* a strncmp(s, s, len) ==> 0 OR
+ a strncmp(a, b, 0) ==> 0 */
+ ir_graph *irg = get_irn_irg(call);
+ ir_node *mem = get_Call_mem(call);
+ ir_node *adr = get_Call_ptr(call);
+ ir_entity *ent = get_SymConst_entity(adr);
+ ir_type *call_tp = get_entity_type(ent);
+ ir_type *res_tp = get_method_res_type(call_tp, 0);
+ ir_mode *mode = get_type_mode(res_tp);
+
+ irn = new_r_Const(irg, get_mode_null(mode));
+ DBG_OPT_ALGSIM0(call, irn, FS_OPT_RTS_STRNCMP);
+ replace_call(irn, call, mem, NULL, NULL);
+ return 1;
+ }
+ return 0;
+} /* i_mapper_strncmp */
+
+/* A mapper for strcpy */
+int i_mapper_strcpy(ir_node *call, void *ctx)
+{
+ ir_node *dst = get_Call_param(call, 0);
+ ir_node *src = get_Call_param(call, 1);
+ (void) ctx;
+
+ if (dst == src) {
+ /* a strcpy(d, s) ==> d */
+ ir_node *mem = get_Call_mem(call);
+ ir_node *dst = get_Call_param(call, 0);
+
+ DBG_OPT_ALGSIM0(call, dst, FS_OPT_RTS_STRCPY);
+ replace_call(dst, call, mem, NULL, NULL);
+ return 1;
+ }
+ return 0;
+} /* i_mapper_strcpy */
+
+/* A mapper for memcpy */
+int i_mapper_memcpy(ir_node *call, void *ctx)
+{
+ ir_node *dst = get_Call_param(call, 0);
+ ir_node *src = get_Call_param(call, 1);
+ ir_node *len = get_Call_param(call, 2);
+ (void) ctx;
+
+ if (dst == src || (is_Const(len) && is_Const_null(len))) {
+ /* a memcpy(d, d, len) ==> d OR
+ a memcpy(d, s, 0) ==> d */
+ ir_node *mem = get_Call_mem(call);
+
+ DBG_OPT_ALGSIM0(call, dst, FS_OPT_RTS_MEMCPY);
+ replace_call(dst, call, mem, NULL, NULL);
+ return 1;
+ }
+ return 0;
+} /* i_mapper_memcpy */
+
+/* A mapper for mempcpy */
+int i_mapper_mempcpy(ir_node *call, void *ctx)
+{
+ ir_node *dst = get_Call_param(call, 0);
+ ir_node *src = get_Call_param(call, 1);
+ ir_node *len = get_Call_param(call, 2);
+ (void) ctx;
+
+ if (dst == src || (is_Const(len) && is_Const_null(len))) {
+ /* a memcpy(d, d, len) ==> d + len OR
+ a memcpy(d, s, 0) ==> d + 0 */
+ dbg_info *dbg = get_irn_dbg_info(call);
+ ir_node *mem = get_Call_mem(call);
+ ir_node *blk = get_nodes_block(call);
+ ir_mode *mode = get_irn_mode(dst);
+ ir_node *res = new_rd_Add(dbg, blk, dst, len, mode);
+
+ DBG_OPT_ALGSIM0(call, res, FS_OPT_RTS_MEMPCPY);
+ replace_call(res, call, mem, NULL, NULL);
+ return 1;
+ }
+ return 0;
+} /* i_mapper_mempcpy */
+
+/* A mapper for memmove */
+int i_mapper_memmove(ir_node *call, void *ctx)
+{
+ ir_node *dst = get_Call_param(call, 0);
+ ir_node *src = get_Call_param(call, 1);
+ ir_node *len = get_Call_param(call, 2);
+ (void) ctx;
+
+ if (dst == src || (is_Const(len) && is_Const_null(len))) {
+ /* a memmove(d, d, len) ==> d OR
+ a memmove(d, s, 0) ==> d */
+ ir_node *mem = get_Call_mem(call);
+
+ DBG_OPT_ALGSIM0(call, dst, FS_OPT_RTS_MEMMOVE);
+ replace_call(dst, call, mem, NULL, NULL);
+ return 1;
+ }
+ return 0;
+} /* i_mapper_memmove */
+
+/* A mapper for memset */
+int i_mapper_memset(ir_node *call, void *ctx)
+{
+ ir_node *len = get_Call_param(call, 2);
+ (void) ctx;
+
+ if (is_Const(len) && is_Const_null(len)) {
+ /* a memset(d, C, 0) ==> d */
+ ir_node *mem = get_Call_mem(call);
+ ir_node *dst = get_Call_param(call, 0);
+
+ DBG_OPT_ALGSIM0(call, dst, FS_OPT_RTS_MEMSET);
+ replace_call(dst, call, mem, NULL, NULL);
+ return 1;
+ }
+ return 0;
+} /* i_mapper_memset */
+
+/* A mapper for memcmp */
+int i_mapper_memcmp(ir_node *call, void *ctx)
+{
+ ir_node *left = get_Call_param(call, 0);
+ ir_node *right = get_Call_param(call, 1);
+ ir_node *len = get_Call_param(call, 2);
+ ir_node *irn;
+ (void) ctx;
+
+ if (left == right || (is_Const(len) && is_Const_null(len))) {
+ /* a memcmp(s, s, len) ==> 0 OR
+ a memcmp(a, b, 0) ==> 0 */
+ ir_graph *irg = get_irn_irg(call);
+ ir_node *mem = get_Call_mem(call);
+ ir_node *adr = get_Call_ptr(call);
+ ir_entity *ent = get_SymConst_entity(adr);
+ ir_type *call_tp = get_entity_type(ent);
+ ir_type *res_tp = get_method_res_type(call_tp, 0);
+ ir_mode *mode = get_type_mode(res_tp);
+
+ irn = new_r_Const(irg, get_mode_null(mode));
+ DBG_OPT_ALGSIM0(call, irn, FS_OPT_RTS_STRNCMP);
+ replace_call(irn, call, mem, NULL, NULL);
+ return 1;
+ }
+ return 0;
+} /* i_mapper_memcmp */
+
+/**
+ * Returns the result mode of a node.
+ */
+static ir_mode *get_irn_res_mode(ir_node *node)
+{
+ switch (get_irn_opcode(node)) {
+ case iro_Load: return get_Load_mode(node);
+ case iro_Div: return get_Div_resmode(node);
+ case iro_Mod: return get_Mod_resmode(node);
+ default: return NULL;
+ }
+} /* get_irn_res_mode */
+
+#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;
+ ir_mode *mode = get_irn_mode(node);
+
+ if (mode != rt->mode)
+ return 0;
+ /* check if the result modes match */
+ if (mode == mode_T && rt->res_mode != NULL) {
+ mode = get_irn_res_mode(node);
+ if (mode != rt->res_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->regular_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, mode_P_code, sym, symconst_addr_ent);
+ call = new_rd_Call(get_irn_dbg_info(node), bl, mem, addr, n_param, in, mtp);
+ set_irn_pinned(call, get_irn_pinned(node));
+
+ if (n_res > 0)
+ res_proj = new_r_Proj(call, mode_T, pn_Call_T_result);
+ else
+ res_proj = NULL;
+
+ 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(call, mode_M, pn_Call_M));
+ if (!is_NoMem(mem)) {
+ /* Exceptions can only be handled with real memory */
+ if (rt->regular_proj_nr >= 0)
+ set_Tuple_pred(node, rt->regular_proj_nr, new_r_Proj(call, mode_X, pn_Call_X_regular));
+ if (rt->exc_proj_nr >= 0)
+ set_Tuple_pred(node, rt->exc_proj_nr, new_r_Proj(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(call, mode_M, pn_Call_M));
+ }
+
+ 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(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(call, mode_T, pn_Call_T_result);
+ res_proj = new_r_Proj(res_proj, mode, 0);
+
+ exchange(node, res_proj);
+ return 1;
+ }
+ }
+ /* should not happen */
+ return 0;
+} /* i_mapper_RuntimeCall */