OP_ADC,
OP_SBB,
OP_BSF,
+ OP_BSR,
OP_INC,
OP_DEC,
OP_NEG,
#define mxAX (1 << xAX)
#define mxCX (1 << xCX)
#define mxDX (1 << xDX)
+#define mxSP (1 << xSP)
#define mxST0 (1 << xST0)
#define mxST1 (1 << xST1)
#define mxST1_0 (mxST1 | mxST0)
{ "adc", OP_ADC, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
{ "sbb", OP_SBB, 2, 2, OPF_DATA|OPF_FLAGS|OPF_CC, PFO_C },
{ "bsf", OP_BSF, 2, 2, OPF_DATA|OPF_FLAGS },
+ { "bsr", OP_BSR, 2, 2, OPF_DATA|OPF_FLAGS },
{ "inc", OP_INC, 1, 1, OPF_DATA|OPF_FLAGS },
{ "dec", OP_DEC, 1, 1, OPF_DATA|OPF_FLAGS },
{ "neg", OP_NEG, 1, 1, OPF_DATA|OPF_FLAGS },
int retval = -1;
int sf_ofs;
- if (po->flags & OPF_EBP_S)
+ if (g_bp_frame && (po->flags & OPF_EBP_S)
+ && !(po->regmask_src & mxSP))
ferr(po, "stack_frame_access while ebp is scratch\n");
parse_stack_access(po, name, ofs_reg, &offset,
if (*pp_found != NULL && pp != NULL && *pp_found != pp) {
if (!IS((*pp_found)->ret_type.name, pp->ret_type.name)
|| (*pp_found)->is_stdcall != pp->is_stdcall
- || (*pp_found)->is_fptr != pp->is_fptr
+ //|| (*pp_found)->is_fptr != pp->is_fptr
|| (*pp_found)->argc != pp->argc
|| (*pp_found)->argc_reg != pp->argc_reg
|| (*pp_found)->argc_stack != pp->argc_stack)
{ "__allshl", OPP_ALLSHL, OPF_DATA, mxAX|mxDX|mxCX, mxAX|mxDX },
{ "__allshr", OPP_ALLSHR, OPF_DATA, mxAX|mxDX|mxCX, mxAX|mxDX },
{ "__ftol", OPP_FTOL, OPF_FPOP, mxST0, mxAX | mxDX },
+ // more precise? Wine gets away with just __ftol handler
+ { "__ftol2", OPP_FTOL, OPF_FPOP, mxST0, mxAX | mxDX },
{ "__CIpow", OPP_CIPOW, OPF_FPOP, mxST0|mxST1, mxST0 },
};
const struct parsed_proto *pp_c;
}
}
-static void scan_prologue_epilogue(int opcnt)
+static void scan_prologue_epilogue(int opcnt, int *stack_align)
{
int ecx_push = 0, esp_sub = 0, pusha = 0;
int sandard_epilogue;
i++;
}
+ if (ops[i].op == OP_AND && ops[i].operand[0].reg == xSP
+ && ops[i].operand[1].type == OPT_CONST)
+ {
+ l = ops[i].operand[1].val;
+ j = ffs(l) - 1;
+ if (j == -1 || (l >> j) != -1)
+ ferr(&ops[i], "unhandled esp align: %x\n", l);
+ if (stack_align != NULL)
+ *stack_align = 1 << j;
+ ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
+ i++;
+ }
+
if (ops[i].op == OP_SUB && IS(opr_name(&ops[i], 0), "esp")) {
g_stack_fsz = opr_const(&ops[i], 1);
ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
pp->argc_reg++;
}
-static void output_std_flags(FILE *fout, struct parsed_op *po,
+static void output_std_flag_z(FILE *fout, struct parsed_op *po,
int *pfomask, const char *dst_opr_text)
{
if (*pfomask & (1 << PFO_Z)) {
lmod_cast_u(po, po->operand[0].lmod), dst_opr_text);
*pfomask &= ~(1 << PFO_Z);
}
+}
+
+static void output_std_flag_s(FILE *fout, struct parsed_op *po,
+ int *pfomask, const char *dst_opr_text)
+{
if (*pfomask & (1 << PFO_S)) {
fprintf(fout, "\n cond_s = (%s%s < 0);",
lmod_cast_s(po, po->operand[0].lmod), dst_opr_text);
}
}
+static void output_std_flags(FILE *fout, struct parsed_op *po,
+ int *pfomask, const char *dst_opr_text)
+{
+ output_std_flag_z(fout, po, pfomask, dst_opr_text);
+ output_std_flag_s(fout, po, pfomask, dst_opr_text);
+}
+
enum {
OPP_FORCE_NORETURN = (1 << 0),
OPP_SIMPLE_ARGS = (1 << 1),
int had_decl = 0;
int label_pending = 0;
int need_double = 0;
+ int stack_align = 0;
int regmask_save = 0; // used regs saved/restored in this func
int regmask_arg; // regs from this function args (fastcall, etc)
int regmask_ret; // regs needed on ret
// pass2:
// - handle ebp/esp frame, remove ops related to it
- scan_prologue_epilogue(opcnt);
+ scan_prologue_epilogue(opcnt, &stack_align);
// pass3:
// - remove dead labels
fprintf(fout, " u8 b[%d];", g_stack_fsz);
if (g_func_lmods & (1 << OPLM_QWORD))
fprintf(fout, " double q[%d];", (g_stack_fsz + 7) / 8);
+ if (stack_align > 8)
+ ferr(ops, "unhandled stack align of %d\n", stack_align);
+ else if (stack_align == 8)
+ fprintf(fout, " u64 align;");
fprintf(fout, " } sf;\n");
had_decl = 1;
}
break;
case OP_BSF:
+ case OP_BSR:
+ // on SKL, if src is 0, dst is left unchanged
assert_operand_cnt(2);
+ out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
- fprintf(fout, " %s = %s ? __builtin_ffs(%s) - 1 : 0;",
- out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
- buf2, buf2);
- output_std_flags(fout, po, &pfomask, buf1);
+ output_std_flag_z(fout, po, &pfomask, buf2);
+ if (po->op == OP_BSF)
+ snprintf(buf3, sizeof(buf3), "__builtin_ffs(%s) - 1", buf2);
+ else
+ snprintf(buf3, sizeof(buf3), "31 - __builtin_clz(%s)", buf2);
+ fprintf(fout, " if (%s) %s = %s;", buf2, buf1, buf3);
last_arith_dst = &po->operand[0];
delayed_flag_op = NULL;
- strcat(g_comment, " bsf");
+ strcat(g_comment, po->op == OP_BSF ? " bsf" : " bsr");
break;
case OP_DEC:
// pass2:
// - handle ebp/esp frame, remove ops related to it
- scan_prologue_epilogue(opcnt);
+ scan_prologue_epilogue(opcnt, NULL);
// pass3:
// - remove dead labels