+ assert_operand_cnt(2);
+ fprintf(fout, " cond_z = (%seax == %sedi); edi %c= %d;",
+ lmod_cast_u(po, po->operand[1].lmod),
+ lmod_cast_u_ptr(po, po->operand[1].lmod), l, j);
+ strcpy(g_comment, "scas");
+ }
+ pfomask &= ~(1 << PFO_Z);
+ last_arith_dst = NULL;
+ delayed_flag_op = NULL;
+ break;
+
+ case OP_RDTSC:
+ fprintf(fout, " tmp64 = ext_rdtsc();\n");
+ fprintf(fout, " edx = tmp64 >> 32;\n");
+ fprintf(fout, " eax = tmp64;");
+ break;
+
+ case OP_CPUID:
+ fprintf(fout, " ext_cpuid(&eax, &ebx, &ecx, &edx);");
+ break;
+
+ // arithmetic w/flags
+ case OP_AND:
+ if (po->operand[1].type == OPT_CONST && !po->operand[1].val)
+ goto dualop_arith_const;
+ propagate_lmod(po, &po->operand[0], &po->operand[1]);
+ goto dualop_arith;
+
+ case OP_OR:
+ propagate_lmod(po, &po->operand[0], &po->operand[1]);
+ if (po->operand[1].type == OPT_CONST) {
+ j = lmod_bytes(po, po->operand[0].lmod);
+ if (((1ull << j * 8) - 1) == po->operand[1].val)
+ goto dualop_arith_const;
+ }
+ goto dualop_arith;
+
+ dualop_arith:
+ assert_operand_cnt(2);
+ fprintf(fout, " %s %s= %s;",
+ out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
+ op_to_c(po),
+ out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
+ output_std_flags(fout, po, &pfomask, buf1);
+ last_arith_dst = &po->operand[0];
+ delayed_flag_op = NULL;
+ break;
+
+ dualop_arith_const:
+ // and 0, or ~0 used instead mov
+ assert_operand_cnt(2);
+ fprintf(fout, " %s = %s;",
+ out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
+ out_src_opr(buf2, sizeof(buf2), po, &po->operand[1],
+ default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0));
+ output_std_flags(fout, po, &pfomask, buf1);
+ last_arith_dst = &po->operand[0];
+ delayed_flag_op = NULL;
+ break;
+
+ case OP_SHL:
+ case OP_SHR:
+ assert_operand_cnt(2);
+ out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
+ if (pfomask & (1 << PFO_C)) {
+ if (po->operand[1].type == OPT_CONST) {
+ l = lmod_bytes(po, po->operand[0].lmod) * 8;
+ j = po->operand[1].val;
+ j %= l;
+ if (j != 0) {
+ if (po->op == OP_SHL)
+ j = l - j;
+ else
+ j -= 1;
+ fprintf(fout, " cond_c = (%s >> %d) & 1;\n",
+ buf1, j);
+ }
+ else
+ ferr(po, "zero shift?\n");
+ }
+ else
+ ferr(po, "TODO\n");
+ pfomask &= ~(1 << PFO_C);
+ }
+ fprintf(fout, " %s %s= %s", buf1, op_to_c(po),
+ out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
+ if (po->operand[1].type != OPT_CONST)
+ fprintf(fout, " & 0x1f");
+ fprintf(fout, ";");
+ output_std_flags(fout, po, &pfomask, buf1);
+ last_arith_dst = &po->operand[0];
+ delayed_flag_op = NULL;
+ break;
+
+ case OP_SAR:
+ assert_operand_cnt(2);
+ out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
+ fprintf(fout, " %s = %s%s >> %s;", buf1,
+ lmod_cast_s(po, po->operand[0].lmod), buf1,
+ out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
+ output_std_flags(fout, po, &pfomask, buf1);
+ last_arith_dst = &po->operand[0];
+ delayed_flag_op = NULL;
+ break;
+
+ case OP_SHLD:
+ case OP_SHRD:
+ assert_operand_cnt(3);
+ propagate_lmod(po, &po->operand[0], &po->operand[1]);
+ l = lmod_bytes(po, po->operand[0].lmod) * 8;
+ out_src_opr_u32(buf3, sizeof(buf3), po, &po->operand[2]);
+ if (po->operand[2].type != OPT_CONST) {
+ // no handling for "undefined" case, hopefully not needed
+ snprintf(buf2, sizeof(buf2), "(%s & 0x1f)", buf3);
+ strcpy(buf3, buf2);
+ }
+ out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
+ out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
+ if (po->op == OP_SHLD) {
+ fprintf(fout, " %s <<= %s; %s |= %s >> (%d - %s);",
+ buf1, buf3, buf1, buf2, l, buf3);
+ strcpy(g_comment, "shld");
+ }
+ else {
+ fprintf(fout, " %s >>= %s; %s |= %s << (%d - %s);",
+ buf1, buf3, buf1, buf2, l, buf3);
+ strcpy(g_comment, "shrd");
+ }
+ output_std_flags(fout, po, &pfomask, buf1);
+ last_arith_dst = &po->operand[0];
+ delayed_flag_op = NULL;
+ break;
+
+ case OP_ROL:
+ case OP_ROR:
+ assert_operand_cnt(2);
+ out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
+ if (po->operand[1].type == OPT_CONST) {
+ j = po->operand[1].val;
+ j %= lmod_bytes(po, po->operand[0].lmod) * 8;
+ fprintf(fout, po->op == OP_ROL ?
+ " %s = (%s << %d) | (%s >> %d);" :
+ " %s = (%s >> %d) | (%s << %d);",
+ buf1, buf1, j, buf1,
+ lmod_bytes(po, po->operand[0].lmod) * 8 - j);
+ }
+ else
+ ferr(po, "TODO\n");
+ output_std_flags(fout, po, &pfomask, buf1);
+ last_arith_dst = &po->operand[0];
+ delayed_flag_op = NULL;
+ break;
+
+ case OP_RCL:
+ case OP_RCR:
+ assert_operand_cnt(2);
+ out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
+ l = lmod_bytes(po, po->operand[0].lmod) * 8;
+ if (po->operand[1].type == OPT_CONST) {
+ j = po->operand[1].val % l;
+ if (j == 0)
+ ferr(po, "zero rotate\n");
+ fprintf(fout, " tmp = (%s >> %d) & 1;\n",
+ buf1, (po->op == OP_RCL) ? (l - j) : (j - 1));
+ if (po->op == OP_RCL) {
+ fprintf(fout,
+ " %s = (%s << %d) | (cond_c << %d)",
+ buf1, buf1, j, j - 1);
+ if (j != 1)
+ fprintf(fout, " | (%s >> %d)", buf1, l + 1 - j);
+ }
+ else {
+ fprintf(fout,
+ " %s = (%s >> %d) | (cond_c << %d)",
+ buf1, buf1, j, l - j);
+ if (j != 1)
+ fprintf(fout, " | (%s << %d)", buf1, l + 1 - j);
+ }
+ fprintf(fout, ";\n");
+ fprintf(fout, " cond_c = tmp;");
+ }
+ else
+ ferr(po, "TODO\n");
+ strcpy(g_comment, (po->op == OP_RCL) ? "rcl" : "rcr");
+ output_std_flags(fout, po, &pfomask, buf1);
+ last_arith_dst = &po->operand[0];
+ delayed_flag_op = NULL;
+ break;
+
+ case OP_XOR:
+ assert_operand_cnt(2);
+ propagate_lmod(po, &po->operand[0], &po->operand[1]);
+ if (IS(opr_name(po, 0), opr_name(po, 1))) {
+ // special case for XOR
+ int z = PFOB_O | PFOB_C | PFOB_S | (1 << PFO_L);
+ for (j = 0; j <= PFO_LE; j++) {
+ if (pfomask & (1 << j)) {
+ fprintf(fout, " cond_%s = %d;\n",
+ parsed_flag_op_names[j], (1 << j) & z ? 0 : 1);
+ pfomask &= ~(1 << j);
+ }
+ }
+ fprintf(fout, " %s = 0;",
+ out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
+ last_arith_dst = &po->operand[0];
+ delayed_flag_op = NULL;
+ break;
+ }
+ goto dualop_arith;
+
+ case OP_ADD:
+ assert_operand_cnt(2);
+ propagate_lmod(po, &po->operand[0], &po->operand[1]);
+ if (pfomask & (1 << PFO_C)) {
+ out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
+ out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]);
+ if (po->operand[0].lmod == OPLM_DWORD) {
+ fprintf(fout, " tmp64 = (u64)%s + %s;\n", buf1, buf2);
+ fprintf(fout, " cond_c = tmp64 >> 32;\n");
+ fprintf(fout, " %s = (u32)tmp64;",
+ out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
+ strcat(g_comment, " add64");
+ }
+ else {
+ fprintf(fout, " cond_c = ((u32)%s + %s) >> %d;\n",
+ buf1, buf2, lmod_bytes(po, po->operand[0].lmod) * 8);
+ fprintf(fout, " %s += %s;",
+ out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
+ buf2);
+ }
+ pfomask &= ~(1 << PFO_C);
+ output_std_flags(fout, po, &pfomask, buf1);
+ last_arith_dst = &po->operand[0];
+ delayed_flag_op = NULL;
+ break;
+ }
+ if (pfomask & (1 << PFO_LE)) {
+ out_cmp_for_cc(buf1, sizeof(buf1), po, PFO_LE, 0, 1);
+ fprintf(fout, " cond_%s = %s;\n",
+ parsed_flag_op_names[PFO_LE], buf1);
+ pfomask &= ~(1 << PFO_LE);
+ }
+ goto dualop_arith;
+
+ case OP_SUB:
+ assert_operand_cnt(2);
+ propagate_lmod(po, &po->operand[0], &po->operand[1]);
+ if (pfomask & ~((1 << PFO_Z) | (1 << PFO_S))) {
+ for (j = 0; j <= PFO_LE; j++) {
+ if (!(pfomask & (1 << j)))
+ continue;
+ if (j == PFO_Z || j == PFO_S)
+ continue;
+
+ out_cmp_for_cc(buf1, sizeof(buf1), po, j, 0, 0);
+ fprintf(fout, " cond_%s = %s;\n",
+ parsed_flag_op_names[j], buf1);
+ pfomask &= ~(1 << j);
+ }
+ }
+ goto dualop_arith;
+
+ case OP_ADC:
+ case OP_SBB:
+ assert_operand_cnt(2);
+ propagate_lmod(po, &po->operand[0], &po->operand[1]);
+ out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
+ if (po->op == OP_SBB
+ && IS(po->operand[0].name, po->operand[1].name))
+ {
+ // avoid use of unitialized var
+ fprintf(fout, " %s = -cond_c;", buf1);
+ // carry remains what it was
+ pfomask &= ~(1 << PFO_C);
+ }
+ else {
+ fprintf(fout, " %s %s= %s + cond_c;", buf1, op_to_c(po),
+ out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[1]));
+ }
+ output_std_flags(fout, po, &pfomask, buf1);
+ last_arith_dst = &po->operand[0];
+ delayed_flag_op = NULL;
+ 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]);
+ 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, po->op == OP_BSF ? " bsf" : " bsr");
+ break;
+
+ case OP_DEC:
+ if (pfomask & ~(PFOB_S | PFOB_S | PFOB_C)) {
+ for (j = 0; j <= PFO_LE; j++) {
+ if (!(pfomask & (1 << j)))
+ continue;
+ if (j == PFO_Z || j == PFO_S || j == PFO_C)
+ continue;
+
+ out_cmp_for_cc(buf1, sizeof(buf1), po, j, 0, 0);
+ fprintf(fout, " cond_%s = %s;\n",
+ parsed_flag_op_names[j], buf1);
+ pfomask &= ~(1 << j);
+ }
+ }
+ // fallthrough
+
+ case OP_INC:
+ if (pfomask & (1 << PFO_C))
+ // carry is unaffected by inc/dec.. wtf?
+ ferr(po, "carry propagation needed\n");
+
+ out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
+ if (po->operand[0].type == OPT_REG) {
+ ferr_assert(po, !(po->flags & OPF_LOCK));
+ strcpy(buf2, po->op == OP_INC ? "++" : "--");
+ fprintf(fout, " %s%s;", buf1, buf2);
+ }
+ else if (po->flags & OPF_LOCK) {
+ out_src_opr(buf2, sizeof(buf2), po, &po->operand[0], "", 1);
+ fprintf(fout, " __sync_fetch_and_%s((%s *)(%s), 1);",
+ po->op == OP_INC ? "add" : "sub",
+ lmod_type_u(po, po->operand[0].lmod), buf2);
+ strcat(g_comment, " lock");
+ lock_handled = 1;
+ }
+ else {
+ strcpy(buf2, po->op == OP_INC ? "+" : "-");
+ fprintf(fout, " %s %s= 1;", buf1, buf2);
+ }
+ output_std_flags(fout, po, &pfomask, buf1);
+ last_arith_dst = &po->operand[0];
+ delayed_flag_op = NULL;
+ break;
+
+ case OP_NEG:
+ out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
+ out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]);
+ fprintf(fout, " %s = -%s%s;", buf1,
+ lmod_cast_s(po, po->operand[0].lmod), buf2);
+ last_arith_dst = &po->operand[0];
+ delayed_flag_op = NULL;
+ if (pfomask & PFOB_C) {
+ fprintf(fout, "\n cond_c = (%s != 0);", buf1);
+ pfomask &= ~PFOB_C;
+ }
+ output_std_flags(fout, po, &pfomask, buf1);
+ break;
+
+ case OP_IMUL:
+ if (po->operand_cnt == 2) {
+ propagate_lmod(po, &po->operand[0], &po->operand[1]);
+ goto dualop_arith;
+ }
+ if (po->operand_cnt == 3)
+ ferr(po, "TODO imul3\n");
+ // fallthrough
+ case OP_MUL:
+ assert_operand_cnt(1);
+ switch (po->operand[0].lmod) {
+ case OPLM_DWORD:
+ strcpy(buf1, po->op == OP_IMUL ? "(s64)(s32)" : "(u64)");
+ fprintf(fout, " tmp64 = %seax * %s%s;\n", buf1, buf1,
+ out_src_opr_u32(buf2, sizeof(buf2), po, &po->operand[0]));
+ fprintf(fout, " edx = tmp64 >> 32;\n");
+ fprintf(fout, " eax = tmp64;");
+ break;
+ case OPLM_BYTE:
+ strcpy(buf1, po->op == OP_IMUL ? "(s16)(s8)" : "(u16)(u8)");
+ fprintf(fout, " LOWORD(eax) = %seax * %s;", buf1,
+ out_src_opr(buf2, sizeof(buf2), po, &po->operand[0],
+ buf1, 0));
+ break;
+ default:
+ ferr(po, "TODO: unhandled mul type\n");
+ break;
+ }
+ last_arith_dst = NULL;
+ delayed_flag_op = NULL;
+ break;
+
+ case OP_DIV:
+ case OP_IDIV:
+ assert_operand_cnt(1);
+ out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
+ strcpy(cast, lmod_cast(po, po->operand[0].lmod,
+ po->op == OP_IDIV));
+ switch (po->operand[0].lmod) {
+ case OPLM_DWORD:
+ if (po->flags & OPF_32BIT)
+ snprintf(buf2, sizeof(buf2), "%seax", cast);
+ else {
+ fprintf(fout, " tmp64 = ((u64)edx << 32) | eax;\n");
+ snprintf(buf2, sizeof(buf2), "%stmp64",
+ (po->op == OP_IDIV) ? "(s64)" : "");
+ }
+ if (po->operand[0].type == OPT_REG
+ && po->operand[0].reg == xDX)
+ {
+ fprintf(fout, " eax = %s / %s%s;\n", buf2, cast, buf1);
+ fprintf(fout, " edx = %s %% %s%s;", buf2, cast, buf1);
+ }
+ else {
+ fprintf(fout, " edx = %s %% %s%s;\n", buf2, cast, buf1);
+ fprintf(fout, " eax = %s / %s%s;", buf2, cast, buf1);
+ }
+ break;
+ case OPLM_WORD:
+ fprintf(fout, " tmp = (edx << 16) | (eax & 0xffff);\n");
+ snprintf(buf2, sizeof(buf2), "%stmp",
+ (po->op == OP_IDIV) ? "(s32)" : "");
+ if (po->operand[0].type == OPT_REG
+ && po->operand[0].reg == xDX)
+ {
+ fprintf(fout, " LOWORD(eax) = %s / %s%s;\n",
+ buf2, cast, buf1);
+ fprintf(fout, " LOWORD(edx) = %s %% %s%s;",
+ buf2, cast, buf1);
+ }
+ else {
+ fprintf(fout, " LOWORD(edx) = %s %% %s%s;\n",
+ buf2, cast, buf1);
+ fprintf(fout, " LOWORD(eax) = %s / %s%s;",
+ buf2, cast, buf1);
+ }
+ strcat(g_comment, " div16");
+ break;
+ default:
+ ferr(po, "unhandled div lmod %d\n", po->operand[0].lmod);
+ }
+ last_arith_dst = NULL;
+ delayed_flag_op = NULL;
+ break;
+
+ case OP_TEST:
+ case OP_CMP:
+ propagate_lmod(po, &po->operand[0], &po->operand[1]);
+ if (pfomask != 0) {
+ for (j = 0; j < 8; j++) {
+ if (pfomask & (1 << j)) {
+ out_cmp_test(buf1, sizeof(buf1), po, j, 0);
+ fprintf(fout, " cond_%s = %s;",
+ parsed_flag_op_names[j], buf1);
+ }
+ }
+ pfomask = 0;
+ }
+ else
+ no_output = 1;
+ last_arith_dst = NULL;
+ delayed_flag_op = po;
+ break;
+
+ case OP_SCC:
+ // SETcc - should already be handled
+ break;
+
+ // note: we reuse OP_Jcc for SETcc, only flags differ
+ case OP_JCC:
+ fprintf(fout, "\n goto %s;", po->operand[0].name);
+ break;
+
+ case OP_JECXZ:
+ fprintf(fout, " if (ecx == 0)\n");
+ fprintf(fout, " goto %s;", po->operand[0].name);
+ strcat(g_comment, " jecxz");
+ break;
+
+ case OP_LOOP:
+ fprintf(fout, " if (--ecx != 0)\n");
+ fprintf(fout, " goto %s;", po->operand[0].name);
+ strcat(g_comment, " loop");
+ break;
+
+ case OP_JMP:
+ assert_operand_cnt(1);
+ last_arith_dst = NULL;
+ delayed_flag_op = NULL;
+
+ if (po->operand[0].type == OPT_REGMEM) {
+ ret = sscanf(po->operand[0].name, "%[^[][%[^*]*4]",
+ buf1, buf2);
+ if (ret != 2)
+ ferr(po, "parse failure for jmp '%s'\n",
+ po->operand[0].name);
+ fprintf(fout, " goto *jt_%s[%s];", buf1, buf2);
+ break;
+ }
+ else if (po->operand[0].type != OPT_LABEL)
+ ferr(po, "unhandled jmp type\n");
+
+ fprintf(fout, " goto %s;", po->operand[0].name);
+ break;
+
+ case OP_CALL:
+ assert_operand_cnt(1);
+ pp = po->pp;
+ my_assert_not(pp, NULL);
+
+ strcpy(buf3, " ");
+ if (po->flags & OPF_CC) {
+ // we treat conditional branch to another func
+ // (yes such code exists..) as conditional tailcall
+ strcat(buf3, " ");
+ fprintf(fout, " {\n");
+ }
+
+ if (pp->is_fptr && !pp->is_arg) {
+ fprintf(fout, "%s%s = %s;\n", buf3, pp->name,
+ out_src_opr(buf1, sizeof(buf1), po, &po->operand[0],
+ "(void *)", 0));
+ }
+ if (pp->is_fptr && (pp->is_unresolved || pp->is_guessed)) {
+ fprintf(fout, "%sunresolved_call(\"%s:%d\", %s);\n",
+ buf3, asmfn, po->asmln, pp->name);
+ }
+
+ fprintf(fout, "%s", buf3);
+ if (strstr(pp->ret_type.name, "int64")) {
+ if (po->flags & OPF_TAIL)
+ ferr(po, "int64 and tail?\n");
+ fprintf(fout, "tmp64 = ");
+ }
+ else if (!IS(pp->ret_type.name, "void")) {
+ if (po->flags & OPF_TAIL) {
+ if (regmask_ret & mxAX) {
+ fprintf(fout, "return ");
+ if (g_func_pp->ret_type.is_ptr != pp->ret_type.is_ptr)
+ fprintf(fout, "(%s)", g_func_pp->ret_type.name);
+ }
+ else if (regmask_ret & mxST0)
+ ferr(po, "float tailcall\n");
+ }
+ else if (po->regmask_dst & mxAX) {
+ fprintf(fout, "eax = ");
+ if (pp->ret_type.is_ptr)
+ fprintf(fout, "(u32)");
+ }
+ else if (po->regmask_dst & mxST0) {
+ ferr_assert(po, po->flags & OPF_FPUSH);
+ if (need_float_stack)
+ fprintf(fout, "f_st[--f_stp & 7] = ");
+ else
+ fprintf(fout, "f_st0 = ");
+ }
+ }
+
+ if (pp->name[0] == 0)
+ ferr(po, "missing pp->name\n");
+ fprintf(fout, "%s%s(", pp->name,
+ pp->has_structarg ? "_sa" : "");
+
+ if (po->flags & OPF_ATAIL) {
+ int check_compat =
+ g_func_pp->is_stdcall && g_func_pp->argc_stack > 0;
+ check_compat |= pp->argc_stack > 0;
+ if (check_compat
+ && (pp->argc_stack != g_func_pp->argc_stack
+ || pp->is_stdcall != g_func_pp->is_stdcall))
+ ferr(po, "incompatible arg-reuse tailcall\n");
+ if (g_func_pp->has_retreg)
+ ferr(po, "TODO: retreg+tailcall\n");
+
+ for (arg = j = 0; arg < pp->argc; arg++) {
+ if (arg > 0)
+ fprintf(fout, ", ");
+
+ cast[0] = 0;
+ if (pp->arg[arg].type.is_ptr)
+ snprintf(cast, sizeof(cast), "(%s)",
+ pp->arg[arg].type.name);
+
+ if (pp->arg[arg].reg != NULL) {
+ fprintf(fout, "%s%s", cast, pp->arg[arg].reg);
+ continue;
+ }
+ // stack arg
+ for (; j < g_func_pp->argc; j++)
+ if (g_func_pp->arg[j].reg == NULL)
+ break;
+ fprintf(fout, "%sa%d", cast, j + 1);
+ j++;
+ }
+ }
+ else {
+ for (arg = 0; arg < pp->argc; arg++) {
+ if (arg > 0)
+ fprintf(fout, ", ");
+
+ cast[0] = 0;
+ if (pp->arg[arg].type.is_ptr)
+ snprintf(cast, sizeof(cast), "(%s)",
+ pp->arg[arg].type.name);
+
+ if (pp->arg[arg].reg != NULL) {
+ if (pp->arg[arg].type.is_retreg)
+ fprintf(fout, "&%s", pp->arg[arg].reg);
+ else if (IS(pp->arg[arg].reg, "ebp")
+ && g_bp_frame && !(po->flags & OPF_EBP_S))
+ {
+ // rare special case
+ fprintf(fout, "%s(u32)&sf.b[sizeof(sf)]", cast);
+ strcat(g_comment, " bp_ref");
+ }
+ else
+ fprintf(fout, "%s%s", cast, pp->arg[arg].reg);
+ continue;
+ }
+
+ // stack arg
+ if (pp->arg[arg].push_ref_cnt == 0)
+ ferr(po, "parsed_op missing for arg%d\n", arg);
+ if (pp->arg[arg].push_ref_cnt > 1)
+ ferr_assert(po, pp->arg[arg].is_saved);
+ tmp_op = pp->arg[arg].push_refs[0];
+ ferr_assert(po, tmp_op != NULL);
+
+ if (tmp_op->flags & OPF_VAPUSH) {
+ fprintf(fout, "ap");
+ }
+ else if (tmp_op->op == OP_FST) {
+ fprintf(fout, "fs_%d", tmp_op->p_argnum);
+ if (tmp_op->operand[0].lmod == OPLM_QWORD)
+ arg++;
+ }
+ else if (pp->arg[arg].type.is_64bit) {
+ ferr_assert(po, tmp_op->p_argpass == 0);
+ ferr_assert(po, !pp->arg[arg].is_saved);
+ ferr_assert(po, !pp->arg[arg].type.is_float);
+ ferr_assert(po, cast[0] == 0);
+ out_src_opr(buf1, sizeof(buf1),
+ tmp_op, &tmp_op->operand[0], cast, 0);
+ arg++;
+ ferr_assert(po, pp->arg[arg].push_ref_cnt == 1);
+ tmp_op = pp->arg[arg].push_refs[0];
+ ferr_assert(po, tmp_op != NULL);
+ out_src_opr(buf2, sizeof(buf2),
+ tmp_op, &tmp_op->operand[0], cast, 0);
+ fprintf(fout, "((u64)(%s) << 32) | (%s)",
+ buf2, buf1);
+ }
+ else if (tmp_op->p_argpass != 0) {
+ ferr_assert(po, !pp->arg[arg].type.is_float);
+ fprintf(fout, "a%d", tmp_op->p_argpass);
+ }
+ else if (pp->arg[arg].is_saved) {
+ ferr_assert(po, tmp_op->p_argnum > 0);
+ ferr_assert(po, !pp->arg[arg].type.is_float);
+ fprintf(fout, "%s%s", cast,
+ saved_arg_name(buf1, sizeof(buf1),
+ tmp_op->p_arggrp, tmp_op->p_argnum));
+ }
+ else if (pp->arg[arg].type.is_float) {
+ ferr_assert(po, !pp->arg[arg].type.is_64bit);
+ fprintf(fout, "%s",
+ out_src_opr_float(buf1, sizeof(buf1),
+ tmp_op, &tmp_op->operand[0], need_float_stack));
+ }
+ else {
+ fprintf(fout, "%s",
+ out_src_opr(buf1, sizeof(buf1),
+ tmp_op, &tmp_op->operand[0], cast, 0));
+ }
+ }
+ }
+ fprintf(fout, ");");
+
+ if (strstr(pp->ret_type.name, "int64")) {
+ fprintf(fout, "\n");
+ fprintf(fout, "%sedx = tmp64 >> 32;\n", buf3);
+ fprintf(fout, "%seax = tmp64;", buf3);
+ }
+
+ if (pp->is_unresolved) {
+ snprintf(buf2, sizeof(buf2), " unresolved %dreg",
+ pp->argc_reg);
+ strcat(g_comment, buf2);
+ }
+
+ if (po->flags & OPF_TAIL) {
+ ret = 0;
+ if (i == opcnt - 1 || pp->is_noreturn)
+ ret = 0;
+ else if (IS(pp->ret_type.name, "void"))
+ ret = 1;
+ else if (!(regmask_ret & (1 << xAX)))
+ ret = 1;
+ // else already handled as 'return f()'
+
+ if (ret) {
+ fprintf(fout, "\n%sreturn;", buf3);
+ strcat(g_comment, " ^ tailcall");
+ }
+ else
+ strcat(g_comment, " tailcall");
+
+ if ((regmask_ret & (1 << xAX))
+ && IS(pp->ret_type.name, "void") && !pp->is_noreturn)
+ {
+ ferr(po, "int func -> void func tailcall?\n");
+ }
+ }
+ if (pp->is_noreturn)
+ strcat(g_comment, " noreturn");
+ if ((po->flags & OPF_ATAIL) && pp->argc_stack > 0)
+ strcat(g_comment, " argframe");
+ if (po->flags & OPF_CC)
+ strcat(g_comment, " cond");
+
+ if (po->flags & OPF_CC)
+ fprintf(fout, "\n }");
+
+ delayed_flag_op = NULL;
+ last_arith_dst = NULL;
+ break;
+
+ case OP_RET:
+ do_tail:
+ if (g_func_pp->is_vararg)
+ fprintf(fout, " va_end(ap);\n");
+ if (g_func_pp->has_retreg) {
+ for (arg = 0; arg < g_func_pp->argc; arg++)
+ if (g_func_pp->arg[arg].type.is_retreg)
+ fprintf(fout, " *r_%s = %s;\n",
+ g_func_pp->arg[arg].reg, g_func_pp->arg[arg].reg);
+ }
+
+ if (regmask_ret & mxST0) {
+ fprintf(fout, " return %s;", float_st0);
+ }
+ else if (!(regmask_ret & mxAX)) {
+ if (i != opcnt - 1 || label_pending)
+ fprintf(fout, " return;");
+ }
+ else if (g_func_pp->ret_type.is_ptr) {
+ fprintf(fout, " return (%s)eax;",
+ g_func_pp->ret_type.name);
+ }
+ else if (IS(g_func_pp->ret_type.name, "__int64"))
+ fprintf(fout, " return ((u64)edx << 32) | eax;");
+ else
+ fprintf(fout, " return eax;");
+
+ last_arith_dst = NULL;
+ delayed_flag_op = NULL;
+ break;
+
+ case OP_PUSH:
+ out_src_opr_u32(buf1, sizeof(buf1), po, &po->operand[0]);
+ if (po->p_argnum != 0) {
+ // special case - saved func arg
+ fprintf(fout, " %s = %s;",
+ saved_arg_name(buf2, sizeof(buf2),
+ po->p_arggrp, po->p_argnum), buf1);
+ break;
+ }
+ else if (po->flags & OPF_RSAVE) {
+ fprintf(fout, " s_%s = %s;", buf1, buf1);
+ break;
+ }
+ else if (po->flags & OPF_PPUSH) {
+ tmp_op = po->datap;
+ ferr_assert(po, tmp_op != NULL);
+ out_dst_opr(buf2, sizeof(buf2), po, &tmp_op->operand[0]);
+ fprintf(fout, " pp_%s = %s;", buf2, buf1);
+ break;
+ }
+ else if (g_func_pp->is_userstack) {
+ fprintf(fout, " *(--esp) = %s;", buf1);
+ break;
+ }
+ if (!(g_ida_func_attr & IDAFA_NORETURN))
+ ferr(po, "stray push encountered\n");
+ no_output = 1;
+ break;
+
+ case OP_POP:
+ out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
+ if (po->flags & OPF_RSAVE) {
+ fprintf(fout, " %s = s_%s;", buf1, buf1);
+ break;
+ }
+ else if (po->flags & OPF_PPUSH) {
+ // push/pop graph / non-const
+ ferr_assert(po, po->datap == NULL);
+ fprintf(fout, " %s = pp_%s;", buf1, buf1);
+ break;
+ }
+ else if (po->datap != NULL) {
+ // push/pop pair
+ tmp_op = po->datap;
+ fprintf(fout, " %s = %s;", buf1,
+ out_src_opr(buf2, sizeof(buf2),
+ tmp_op, &tmp_op->operand[0],
+ default_cast_to(buf3, sizeof(buf3), &po->operand[0]), 0));
+ break;
+ }
+ else if (g_func_pp->is_userstack) {
+ fprintf(fout, " %s = *esp++;", buf1);
+ break;
+ }
+ else
+ ferr(po, "stray pop encountered\n");
+ break;
+
+ case OP_NOP:
+ no_output = 1;
+ break;
+
+ // pseudo ops
+ case OPP_ALLSHL:
+ case OPP_ALLSHR:
+ fprintf(fout, " tmp64 = ((u64)edx << 32) | eax;\n");
+ fprintf(fout, " tmp64 = (s64)tmp64 %s LOBYTE(ecx);\n",
+ po->op == OPP_ALLSHL ? "<<" : ">>");
+ fprintf(fout, " edx = tmp64 >> 32; eax = tmp64;");
+ strcat(g_comment, po->op == OPP_ALLSHL
+ ? " allshl" : " allshr");
+ break;
+
+ // x87
+ case OP_FLD:
+ if (need_float_stack) {
+ out_src_opr_float(buf1, sizeof(buf1),
+ po, &po->operand[0], 1);
+ if (po->regmask_src & mxSTa) {
+ fprintf(fout, " f_st[(f_stp - 1) & 7] = %s; f_stp--;",
+ buf1);
+ }
+ else
+ fprintf(fout, " f_st[--f_stp & 7] = %s;", buf1);
+ }
+ else {
+ if (po->flags & OPF_FSHIFT)
+ fprintf(fout, " f_st1 = f_st0;");
+ if (po->operand[0].type == OPT_REG
+ && po->operand[0].reg == xST0)
+ {
+ strcat(g_comment, " fld st");
+ break;
+ }
+ fprintf(fout, " f_st0 = %s;",
+ out_src_opr_float(buf1, sizeof(buf1),
+ po, &po->operand[0], 0));
+ }
+ strcat(g_comment, " fld");
+ break;
+
+ case OP_FILD:
+ out_src_opr(buf1, sizeof(buf1), po, &po->operand[0],
+ lmod_cast(po, po->operand[0].lmod, 1), 0);
+ snprintf(buf2, sizeof(buf2), "(%s)%s", float_type, buf1);
+ if (need_float_stack) {
+ fprintf(fout, " f_st[--f_stp & 7] = %s;", buf2);
+ }
+ else {
+ if (po->flags & OPF_FSHIFT)
+ fprintf(fout, " f_st1 = f_st0;");
+ fprintf(fout, " f_st0 = %s;", buf2);
+ }
+ strcat(g_comment, " fild");
+ break;
+
+ case OP_FLDc:
+ if (need_float_stack)
+ fprintf(fout, " f_st[--f_stp & 7] = ");
+ else {
+ if (po->flags & OPF_FSHIFT)
+ fprintf(fout, " f_st1 = f_st0;");
+ fprintf(fout, " f_st0 = ");
+ }
+ switch (po->operand[0].val) {
+ case X87_CONST_1: fprintf(fout, "1.0;"); break;
+ case X87_CONST_L2T: fprintf(fout, "3.321928094887362;"); break;
+ case X87_CONST_L2E: fprintf(fout, "M_LOG2E;"); break;
+ case X87_CONST_PI: fprintf(fout, "M_PI;"); break;
+ case X87_CONST_LG2: fprintf(fout, "0.301029995663981;"); break;
+ case X87_CONST_LN2: fprintf(fout, "M_LN2;"); break;
+ case X87_CONST_Z: fprintf(fout, "0.0;"); break;
+ default: ferr_assert(po, 0); break;
+ }
+ break;
+
+ case OP_FST:
+ dead_dst = 0;
+ if (po->flags & OPF_FARG) {
+ // store to stack as func arg
+ fprintf(fout, " fs_%d = %s;", po->p_argnum, float_st0);
+ }
+ else if (po->operand[0].type == OPT_REG
+ && po->operand[0].reg == xST0)
+ {
+ dead_dst = 1;
+ }
+ else if (float_opr_needs_helper(po, &po->operand[0])) {
+ out_src_opr(buf1, sizeof(buf1), po, &po->operand[0], "", 1);
+ fprintf(fout, " %s_store(%s, %s);",
+ po->operand[0].lmod == OPLM_QWORD ? "double" : "float",
+ float_st0, buf1);
+ }
+ else {
+ out_dst_opr_float(buf1, sizeof(buf1), po, &po->operand[0],
+ need_float_stack);
+ fprintf(fout, " %s = %s;", buf1, float_st0);
+ }
+ if (po->flags & OPF_FSHIFT) {
+ if (need_float_stack)
+ fprintf(fout, " f_stp++;");
+ else
+ fprintf(fout, " f_st0 = f_st1;");
+ }
+ if (dead_dst && !(po->flags & OPF_FSHIFT))
+ no_output = 1;
+ else
+ strcat(g_comment, " fst");
+ break;
+
+ case OP_FIST:
+ fprintf(fout, " %s = %s%s;",
+ out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]),
+ lmod_cast(po, po->operand[0].lmod, 1), float_st0);
+ if (po->flags & OPF_FSHIFT) {
+ if (need_float_stack)
+ fprintf(fout, " f_stp++;");
+ else
+ fprintf(fout, " f_st0 = f_st1;");
+ }
+ strcat(g_comment, " fist");
+ break;
+
+ case OP_FABS:
+ fprintf(fout, " %s = fabs%s(%s);", float_st0,
+ need_double ? "" : "f", float_st0);
+ break;
+
+ case OP_FADD:
+ case OP_FDIV:
+ case OP_FMUL:
+ case OP_FSUB:
+ out_dst_opr_float(buf1, sizeof(buf1), po, &po->operand[0],
+ need_float_stack);
+ out_src_opr_float(buf2, sizeof(buf2), po, &po->operand[1],
+ need_float_stack);
+ dead_dst = (po->flags & OPF_FPOP)
+ && po->operand[0].type == OPT_REG
+ && po->operand[0].reg == xST0;
+ switch (po->op) {
+ case OP_FADD: j = '+'; break;
+ case OP_FDIV: j = '/'; break;
+ case OP_FMUL: j = '*'; break;
+ case OP_FSUB: j = '-'; break;
+ default: j = 'x'; break;
+ }
+ if (need_float_stack) {
+ if (!dead_dst)
+ fprintf(fout, " %s %c= %s;", buf1, j, buf2);
+ if (po->flags & OPF_FSHIFT)
+ fprintf(fout, " f_stp++;");
+ }
+ else {
+ if (po->flags & OPF_FSHIFT) {
+ // note: assumes only 2 regs handled
+ if (!dead_dst)
+ fprintf(fout, " f_st0 = f_st1 %c f_st0;", j);
+ else
+ fprintf(fout, " f_st0 = f_st1;");
+ }
+ else if (!dead_dst)
+ fprintf(fout, " %s %c= %s;", buf1, j, buf2);
+ }
+ no_output = (dead_dst && !(po->flags & OPF_FSHIFT));
+ break;
+
+ case OP_FDIVR:
+ case OP_FSUBR:
+ out_dst_opr_float(buf1, sizeof(buf1), po, &po->operand[0],
+ need_float_stack);
+ out_src_opr_float(buf2, sizeof(buf2), po, &po->operand[1],
+ need_float_stack);
+ out_src_opr_float(buf3, sizeof(buf3), po, &po->operand[0],
+ need_float_stack);
+ dead_dst = (po->flags & OPF_FPOP)
+ && po->operand[0].type == OPT_REG
+ && po->operand[0].reg == xST0;
+ j = po->op == OP_FDIVR ? '/' : '-';
+ if (need_float_stack) {
+ if (!dead_dst)
+ fprintf(fout, " %s = %s %c %s;", buf1, buf2, j, buf3);
+ if (po->flags & OPF_FSHIFT)
+ fprintf(fout, " f_stp++;");
+ }
+ else {
+ if (po->flags & OPF_FSHIFT) {
+ if (!dead_dst)
+ fprintf(fout, " f_st0 = f_st0 %c f_st1;", j);
+ else
+ fprintf(fout, " f_st0 = f_st1;");
+ }
+ else if (!dead_dst)
+ fprintf(fout, " %s = %s %c %s;", buf1, buf2, j, buf3);
+ }
+ no_output = (dead_dst && !(po->flags & OPF_FSHIFT));
+ break;
+
+ case OP_FIADD:
+ case OP_FIDIV:
+ case OP_FIMUL:
+ case OP_FISUB:
+ switch (po->op) {
+ case OP_FIADD: j = '+'; break;
+ case OP_FIDIV: j = '/'; break;
+ case OP_FIMUL: j = '*'; break;
+ case OP_FISUB: j = '-'; break;
+ default: j = 'x'; break;
+ }
+ fprintf(fout, " %s %c= (%s)%s;", float_st0,
+ j, float_type,
+ out_src_opr(buf1, sizeof(buf1), po, &po->operand[0],
+ lmod_cast(po, po->operand[0].lmod, 1), 0));
+ break;
+
+ case OP_FIDIVR:
+ case OP_FISUBR:
+ fprintf(fout, " %s = %s %c %s;", float_st0,
+ out_src_opr_float(buf2, sizeof(buf2), po, &po->operand[1],
+ need_float_stack),
+ po->op == OP_FIDIVR ? '/' : '-', float_st0);
+ break;
+
+ case OP_FCOM: {
+ int mask, z_check;
+ ferr_assert(po, po->datap != NULL);
+ mask = (long)po->datap & 0xffff;
+ z_check = ((long)po->datap >> 16) & 1;
+ out_src_opr_float(buf1, sizeof(buf1), po, &po->operand[0],
+ need_float_stack);
+ if (mask == 0x0100 || mask == 0x0500) { // C0 -> <
+ fprintf(fout, " f_sw = %s < %s ? 0x0100 : 0;",
+ float_st0, buf1);
+ }
+ else if (mask == 0x4000 || mask == 0x4400) { // C3 -> =
+ fprintf(fout, " f_sw = %s == %s ? 0x4000 : 0;",
+ float_st0, buf1);
+ }
+ else if (mask == 0x4100) { // C3, C0
+ if (z_check) {
+ fprintf(fout, " f_sw = %s <= %s ? 0x4100 : 0;",
+ float_st0, buf1);
+ strcat(g_comment, " z_chk_det");
+ }
+ else {
+ fprintf(fout, " f_sw = %s == %s ? 0x4000 : "
+ "(%s < %s ? 0x0100 : 0);",
+ float_st0, buf1, float_st0, buf1);
+ }
+ }
+ else
+ ferr(po, "unhandled sw mask: %x\n", mask);
+ if (po->flags & OPF_FSHIFT) {
+ if (need_float_stack) {
+ if (po->flags & OPF_FPOPP)
+ fprintf(fout, " f_stp += 2;");
+ else
+ fprintf(fout, " f_stp++;");
+ }
+ else {
+ ferr_assert(po, !(po->flags & OPF_FPOPP));
+ fprintf(fout, " f_st0 = f_st1;");
+ }
+ }
+ break;
+ }
+
+ case OP_FNSTSW:
+ fprintf(fout, " %s = f_sw;",
+ out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]));
+ break;
+
+ case OP_FCHS:
+ fprintf(fout, " %s = -%s;", float_st0, float_st0);
+ break;
+
+ case OP_FCOS:
+ fprintf(fout, " %s = cos%s(%s);", float_st0,
+ need_double ? "" : "f", float_st0);
+ break;
+
+ case OP_FPATAN:
+ if (need_float_stack) {
+ fprintf(fout, " %s = atan%s(%s / %s);", float_st1,
+ need_double ? "" : "f", float_st1, float_st0);
+ fprintf(fout, " f_stp++;");
+ }
+ else {
+ fprintf(fout, " f_st0 = atan%s(f_st1 / f_st0);",
+ need_double ? "" : "f");
+ }
+ break;
+
+ case OP_FYL2X:
+ if (need_float_stack) {
+ fprintf(fout, " %s = %s * log2%s(%s);", float_st1,
+ float_st1, need_double ? "" : "f", float_st0);
+ fprintf(fout, " f_stp++;");
+ }
+ else {
+ fprintf(fout, " f_st0 = f_st1 * log2%s(f_st0);",
+ need_double ? "" : "f");
+ }
+ strcat(g_comment, " fyl2x");
+ break;
+
+ case OP_FSIN:
+ fprintf(fout, " %s = sin%s(%s);", float_st0,
+ need_double ? "" : "f", float_st0);
+ break;
+
+ case OP_FSQRT:
+ fprintf(fout, " %s = sqrt%s(%s);", float_st0,
+ need_double ? "" : "f", float_st0);
+ break;
+
+ case OP_FXCH:
+ dead_dst = po->operand[0].type == OPT_REG
+ && po->operand[0].reg == xST0;
+ if (!dead_dst) {
+ out_src_opr_float(buf1, sizeof(buf1), po, &po->operand[0],
+ need_float_stack);
+ fprintf(fout, " { %s t = %s; %s = %s; %s = t; }", float_type,
+ float_st0, float_st0, buf1, buf1);
+ strcat(g_comment, " fxch");
+ }
+ else
+ no_output = 1;
+ break;
+
+ case OPP_FTOL:
+ ferr_assert(po, po->flags & OPF_32BIT);
+ fprintf(fout, " eax = (s32)%s;", float_st0);
+ if (po->flags & OPF_FSHIFT) {
+ if (need_float_stack)
+ fprintf(fout, " f_stp++;");
+ else
+ fprintf(fout, " f_st0 = f_st1;");
+ }
+ strcat(g_comment, " ftol");
+ goto tail_check;
+
+ case OPP_CIPOW:
+ if (need_float_stack) {
+ fprintf(fout, " %s = pow%s(%s, %s);", float_st1,
+ need_double ? "" : "f", float_st1, float_st0);
+ fprintf(fout, " f_stp++;");
+ }
+ else {
+ fprintf(fout, " f_st0 = pow%s(f_st1, f_st0);",
+ need_double ? "" : "f");