As an experiment about the linearization of builtins, try this easy one (and statically expand it if the argument is constant). Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx> --- builtin.c | 21 +++++++++++++++++++++ linearize.c | 22 ++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/builtin.c b/builtin.c index acc49871644c..5c7321cad3e4 100644 --- a/builtin.c +++ b/builtin.c @@ -305,6 +305,26 @@ static struct symbol_op fp_unop_op = { }; +static int expand_isdigit(struct expression *expr, int cost) +{ + struct expression *arg = first_expression(expr->args); + long long val = get_expression_value_silent(arg); + + if (cost) + return cost; + + expr->value = (val >= '0') && (val <= '9'); + expr->type = EXPR_VALUE; + expr->taint = 0; + return 0; +} + +static struct symbol_op isdigit_op = { + .evaluate = evaluate_pure_unop, + .expand = expand_isdigit, +}; + + static int evaluate_overflow_gen(struct expression *expr, int ptr) { struct expression *arg; @@ -552,6 +572,7 @@ static const struct builtin_fn builtins_common[] = { { "__builtin_inf", &double_ctype, 0 }, { "__builtin_inff", &float_ctype, 0 }, { "__builtin_infl", &ldouble_ctype, 0 }, + { "__builtin_isdigit", &int_ctype, 0, { &int_ctype }, .op = &isdigit_op }, { "__builtin_isfinite", &int_ctype, 1, .op = &fp_unop_op }, { "__builtin_isgreater", &int_ctype, 0, { &float_ctype, &float_ctype }}, { "__builtin_isgreaterequal", &int_ctype, 0, { &float_ctype, &float_ctype }}, diff --git a/linearize.c b/linearize.c index bf9b6ecdc92e..b74651765f6c 100644 --- a/linearize.c +++ b/linearize.c @@ -2602,6 +2602,27 @@ static pseudo_t linearize_fma(struct entrypoint *ep, struct expression *expr) return insn->target = alloc_pseudo(insn); } +static pseudo_t linearize_isdigit(struct entrypoint *ep, struct expression *expr) +{ + struct instruction *insn; + pseudo_t src; + + insn = alloc_typed_instruction(OP_SUB, &int_ctype); + src = linearize_expression(ep, first_expression(expr->args)); + use_pseudo(insn, src, &insn->src1); + insn->src2 = value_pseudo('0'); + src = insn->target = alloc_pseudo(insn); + add_one_insn(ep, insn); + + insn = alloc_typed_instruction(OP_SET_BE, &int_ctype); + use_pseudo(insn, src, &insn->src1); + insn->src2 = value_pseudo(9); + insn->target = alloc_pseudo(insn); + add_one_insn(ep, insn); + + return insn->target; +} + static pseudo_t linearize_unreachable(struct entrypoint *ep, struct expression *exp) { add_unreachable(ep); @@ -2617,6 +2638,7 @@ static struct sym_init { { "__builtin_fma", linearize_fma }, { "__builtin_fmaf", linearize_fma }, { "__builtin_fmal", linearize_fma }, + { "__builtin_isdigit", linearize_isdigit }, { "__builtin_unreachable", linearize_unreachable }, { } }; -- 2.29.1