+ *pp_i = po - ops;
+ }
+}
+
+static void add_label_ref(struct label_ref *lr, int op_i)
+{
+ struct label_ref *lr_new;
+
+ if (lr->i == -1) {
+ lr->i = op_i;
+ return;
+ }
+
+ lr_new = calloc(1, sizeof(*lr_new));
+ lr_new->i = op_i;
+ lr_new->next = lr->next;
+ lr->next = lr_new;
+}
+
+static struct parsed_data *try_resolve_jumptab(int i, int opcnt)
+{
+ struct parsed_op *po = &ops[i];
+ struct parsed_data *pd;
+ char label[NAMELEN], *p;
+ int len, j, l;
+
+ p = strchr(po->operand[0].name, '[');
+ if (p == NULL)
+ return NULL;
+
+ len = p - po->operand[0].name;
+ strncpy(label, po->operand[0].name, len);
+ label[len] = 0;
+
+ for (j = 0, pd = NULL; j < g_func_pd_cnt; j++) {
+ if (IS(g_func_pd[j].label, label)) {
+ pd = &g_func_pd[j];
+ break;
+ }
+ }
+ if (pd == NULL)
+ //ferr(po, "label '%s' not parsed?\n", label);
+ return NULL;
+
+ if (pd->type != OPT_OFFSET)
+ ferr(po, "label '%s' with non-offset data?\n", label);
+
+ // find all labels, link
+ for (j = 0; j < pd->count; j++) {
+ for (l = 0; l < opcnt; l++) {
+ if (g_labels[l] != NULL && IS(g_labels[l], pd->d[j].u.label)) {
+ add_label_ref(&g_label_refs[l], i);
+ pd->d[j].bt_i = l;
+ break;
+ }
+ }
+ }
+
+ return pd;
+}
+
+static void clear_labels(int count)
+{
+ int i;
+
+ for (i = 0; i < count; i++) {
+ if (g_labels[i] != NULL) {
+ free(g_labels[i]);
+ g_labels[i] = NULL;
+ }
+ }
+}
+
+static int get_pp_arg_regmask_src(const struct parsed_proto *pp)
+{
+ int regmask = 0;
+ int i, reg;
+
+ for (i = 0; i < pp->argc; i++) {
+ if (pp->arg[i].reg != NULL) {
+ reg = char_array_i(regs_r32,
+ ARRAY_SIZE(regs_r32), pp->arg[i].reg);
+ if (reg < 0)
+ ferr(ops, "arg '%s' of func '%s' is not a reg?\n",
+ pp->arg[i].reg, pp->name);
+ regmask |= 1 << reg;
+ }
+ }
+
+ return regmask;
+}
+
+static int get_pp_arg_regmask_dst(const struct parsed_proto *pp)
+{
+ int regmask = 0;
+ int i, reg;
+
+ if (pp->has_retreg) {
+ for (i = 0; i < pp->argc; i++) {
+ if (pp->arg[i].type.is_retreg) {
+ reg = char_array_i(regs_r32,
+ ARRAY_SIZE(regs_r32), pp->arg[i].reg);
+ ferr_assert(ops, reg >= 0);
+ regmask |= 1 << reg;
+ }
+ }
+ }
+
+ if (strstr(pp->ret_type.name, "int64"))
+ return regmask | (1 << xAX) | (1 << xDX);
+ if (IS(pp->ret_type.name, "float")
+ || IS(pp->ret_type.name, "double"))
+ {
+ return regmask | mxST0;
+ }
+ if (strcasecmp(pp->ret_type.name, "void") == 0)
+ return regmask;
+
+ return regmask | mxAX;
+}
+
+static int are_ops_same(struct parsed_op *po1, struct parsed_op *po2)
+{
+ return po1->op == po2->op && po1->operand_cnt == po2->operand_cnt
+ && memcmp(po1->operand, po2->operand,
+ sizeof(po1->operand[0]) * po1->operand_cnt) == 0;
+}
+
+static void resolve_branches_parse_calls(int opcnt)
+{
+ static const struct {
+ const char *name;
+ enum op_op op;
+ unsigned int flags;
+ unsigned int regmask_src;
+ unsigned int regmask_dst;
+ } pseudo_ops[] = {
+ { "__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 },
+ { "__CIpow", OPP_CIPOW, OPF_FPOP, mxST0|mxST1, mxST0 },
+ };
+ const struct parsed_proto *pp_c;
+ struct parsed_proto *pp;
+ struct parsed_data *pd;
+ struct parsed_op *po;
+ const char *tmpname;
+ int i, l;
+ int ret;
+
+ for (i = 0; i < opcnt; i++)
+ {
+ po = &ops[i];
+ po->bt_i = -1;
+ po->btj = NULL;
+
+ if (po->datap != NULL) {
+ pp = calloc(1, sizeof(*pp));
+ my_assert_not(pp, NULL);
+
+ ret = parse_protostr(po->datap, pp);
+ if (ret < 0)
+ ferr(po, "bad protostr supplied: %s\n", (char *)po->datap);
+ free(po->datap);
+ po->datap = NULL;
+ po->pp = pp;
+ }
+
+ if (po->op == OP_CALL) {
+ pp = NULL;
+
+ if (po->pp != NULL)
+ pp = po->pp;
+ else if (po->operand[0].type == OPT_LABEL)
+ {
+ tmpname = opr_name(po, 0);
+ if (IS_START(tmpname, "loc_"))
+ ferr(po, "call to loc_*\n");
+ if (IS(tmpname, "__alloca_probe"))
+ continue;
+
+ // convert some calls to pseudo-ops
+ for (l = 0; l < ARRAY_SIZE(pseudo_ops); l++) {
+ if (!IS(tmpname, pseudo_ops[l].name))
+ continue;
+
+ po->op = pseudo_ops[l].op;
+ po->operand_cnt = 0;
+ po->regmask_src = pseudo_ops[l].regmask_src;
+ po->regmask_dst = pseudo_ops[l].regmask_dst;
+ po->flags = pseudo_ops[l].flags;
+ po->flags |= po->regmask_dst ? OPF_DATA : 0;
+ break;
+ }
+ if (l < ARRAY_SIZE(pseudo_ops))
+ continue;
+
+ pp_c = proto_parse(g_fhdr, tmpname, g_header_mode);
+ if (!g_header_mode && pp_c == NULL)
+ ferr(po, "proto_parse failed for call '%s'\n", tmpname);
+
+ if (pp_c != NULL) {
+ pp = proto_clone(pp_c);
+ my_assert_not(pp, NULL);
+ }
+ }
+
+ if (pp != NULL) {
+ if (pp->is_fptr)
+ check_func_pp(po, pp, "fptr var call");
+ if (pp->is_noreturn)
+ po->flags |= OPF_TAIL;
+ }
+ po->pp = pp;
+ continue;
+ }
+
+ if (!(po->flags & OPF_JMP) || po->op == OP_RET)
+ continue;
+
+ if (po->operand[0].type == OPT_REGMEM) {
+ pd = try_resolve_jumptab(i, opcnt);
+ if (pd == NULL)
+ goto tailcall;
+
+ po->btj = pd;
+ continue;
+ }
+
+ for (l = 0; l < opcnt; l++) {
+ if (g_labels[l] != NULL
+ && IS(po->operand[0].name, g_labels[l]))
+ {
+ if (l == i + 1 && po->op == OP_JMP) {
+ // yet another alignment type..
+ po->flags |= OPF_RMD|OPF_DONE;
+ break;
+ }
+ add_label_ref(&g_label_refs[l], i);
+ po->bt_i = l;
+ break;
+ }
+ }
+
+ if (po->bt_i != -1 || (po->flags & OPF_RMD))
+ continue;
+
+ if (po->operand[0].type == OPT_LABEL)
+ // assume tail call
+ goto tailcall;
+
+ ferr(po, "unhandled branch\n");
+
+tailcall:
+ po->op = OP_CALL;
+ po->flags |= OPF_TAIL;
+ if (i > 0 && ops[i - 1].op == OP_POP)
+ po->flags |= OPF_ATAIL;
+ i--; // reprocess
+ }
+}
+
+static void scan_prologue_epilogue(int opcnt)
+{
+ int ecx_push = 0, esp_sub = 0, pusha = 0;
+ int sandard_epilogue;
+ int found;
+ int i, j, l;
+
+ if (ops[0].op == OP_PUSH && IS(opr_name(&ops[0], 0), "ebp")
+ && ops[1].op == OP_MOV
+ && IS(opr_name(&ops[1], 0), "ebp")
+ && IS(opr_name(&ops[1], 1), "esp"))
+ {
+ g_bp_frame = 1;
+ ops[0].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
+ ops[1].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
+ i = 2;
+
+ if (ops[i].op == OP_PUSHA) {
+ ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
+ pusha = 1;
+ 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;
+ i++;
+ }
+ else {
+ // another way msvc builds stack frame..
+ while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
+ g_stack_fsz += 4;
+ ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
+ ecx_push++;
+ i++;
+ }
+ // and another way..
+ if (i == 2 && ops[i].op == OP_MOV && ops[i].operand[0].reg == xAX
+ && ops[i].operand[1].type == OPT_CONST
+ && ops[i + 1].op == OP_CALL
+ && IS(opr_name(&ops[i + 1], 0), "__alloca_probe"))
+ {
+ g_stack_fsz += ops[i].operand[1].val;
+ ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
+ i++;
+ ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
+ i++;
+ }
+ }
+
+ found = 0;
+ do {
+ for (; i < opcnt; i++)
+ if (ops[i].flags & OPF_TAIL)
+ break;
+ j = i - 1;
+ if (i == opcnt && (ops[j].flags & OPF_JMP)) {
+ if (ops[j].bt_i != -1 || ops[j].btj != NULL)
+ break;
+ i--;
+ j--;
+ }
+
+ sandard_epilogue = 0;
+ if (ops[j].op == OP_POP && IS(opr_name(&ops[j], 0), "ebp"))
+ {
+ ops[j].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
+ // the standard epilogue is sometimes even used without a sf
+ if (ops[j - 1].op == OP_MOV
+ && IS(opr_name(&ops[j - 1], 0), "esp")
+ && IS(opr_name(&ops[j - 1], 1), "ebp"))
+ sandard_epilogue = 1;
+ }
+ else if (ops[j].op == OP_LEAVE)
+ {
+ ops[j].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
+ sandard_epilogue = 1;
+ }
+ else if (ops[i].op == OP_CALL && ops[i].pp != NULL
+ && ops[i].pp->is_noreturn)
+ {
+ // on noreturn, msvc sometimes cleans stack, sometimes not
+ i++;
+ found = 1;
+ continue;
+ }
+ else if (!(g_ida_func_attr & IDAFA_NORETURN))
+ ferr(&ops[j], "'pop ebp' expected\n");
+
+ if (g_stack_fsz != 0 || sandard_epilogue) {
+ if (ops[j].op == OP_LEAVE)
+ j--;
+ else if (sandard_epilogue) // mov esp, ebp
+ {
+ ops[j - 1].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
+ j -= 2;
+ }
+ else if (!(g_ida_func_attr & IDAFA_NORETURN))
+ {
+ ferr(&ops[j], "esp restore expected\n");
+ }
+
+ if (ecx_push && j >= 0 && ops[j].op == OP_POP
+ && IS(opr_name(&ops[j], 0), "ecx"))
+ {
+ ferr(&ops[j], "unexpected ecx pop\n");
+ }
+ }
+
+ if (pusha) {
+ if (ops[j].op == OP_POPA)
+ ops[j].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
+ else
+ ferr(&ops[j], "popa expected\n");
+ }
+
+ found = 1;
+ i++;
+ } while (i < opcnt);
+
+ if (!found)
+ ferr(ops, "missing ebp epilogue\n");
+ return;
+ }
+
+ // non-bp frame
+ i = 0;
+ while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
+ ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
+ g_stack_fsz += 4;
+ ecx_push++;
+ i++;
+ }
+
+ for (; i < opcnt; i++) {
+ if (ops[i].op == OP_PUSH || (ops[i].flags & (OPF_JMP|OPF_TAIL)))
+ break;
+ if (ops[i].op == OP_SUB && ops[i].operand[0].reg == xSP
+ && ops[i].operand[1].type == OPT_CONST)
+ {
+ g_stack_fsz = ops[i].operand[1].val;
+ ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
+ i++;
+ esp_sub = 1;
+ break;
+ }
+ else if (ops[i].op == OP_MOV && ops[i].operand[0].reg == xAX
+ && ops[i].operand[1].type == OPT_CONST
+ && ops[i + 1].op == OP_CALL
+ && IS(opr_name(&ops[i + 1], 0), "__alloca_probe"))
+ {
+ g_stack_fsz += ops[i].operand[1].val;
+ ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
+ i++;
+ ops[i].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
+ i++;
+ esp_sub = 1;
+ break;
+ }
+ }
+
+ if (ecx_push && !esp_sub) {
+ // could actually be args for a call..
+ for (; i < opcnt; i++)
+ if (ops[i].op != OP_PUSH)
+ break;
+
+ if (ops[i].op == OP_CALL && ops[i].operand[0].type == OPT_LABEL) {
+ const struct parsed_proto *pp;
+ pp = proto_parse(g_fhdr, opr_name(&ops[i], 0), 1);
+ j = pp ? pp->argc_stack : 0;
+ while (i > 0 && j > 0) {
+ i--;
+ if (ops[i].op == OP_PUSH) {
+ ops[i].flags &= ~(OPF_RMD | OPF_DONE | OPF_NOREGS);
+ j--;
+ }
+ }
+ if (j != 0)
+ ferr(&ops[i], "unhandled prologue\n");
+
+ // recheck
+ i = g_stack_fsz = ecx_push = 0;
+ while (ops[i].op == OP_PUSH && IS(opr_name(&ops[i], 0), "ecx")) {
+ if (!(ops[i].flags & OPF_RMD))
+ break;
+ g_stack_fsz += 4;
+ ecx_push++;
+ i++;
+ }
+ }
+ }
+
+ found = 0;
+ if (ecx_push || esp_sub)
+ {
+ g_sp_frame = 1;
+
+ do {
+ for (; i < opcnt; i++)
+ if (ops[i].flags & OPF_TAIL)
+ break;
+
+ j = i - 1;
+ if (i == opcnt && (ops[j].flags & OPF_JMP)) {
+ if (ops[j].bt_i != -1 || ops[j].btj != NULL)
+ break;
+ i--;
+ j--;
+ }
+
+ if (ecx_push > 0) {
+ for (l = 0; l < ecx_push; l++) {
+ if (ops[j].op == OP_POP && IS(opr_name(&ops[j], 0), "ecx"))
+ /* pop ecx */;
+ else if (ops[j].op == OP_ADD
+ && IS(opr_name(&ops[j], 0), "esp")
+ && ops[j].operand[1].type == OPT_CONST)
+ {
+ /* add esp, N */
+ l += ops[j].operand[1].val / 4 - 1;
+ }
+ else
+ ferr(&ops[j], "'pop ecx' expected\n");
+
+ ops[j].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
+ j--;
+ }
+ if (l != ecx_push)
+ ferr(&ops[j], "epilogue scan failed\n");
+
+ found = 1;
+ }
+
+ if (esp_sub) {
+ if (ops[j].op != OP_ADD
+ || !IS(opr_name(&ops[j], 0), "esp")
+ || ops[j].operand[1].type != OPT_CONST
+ || ops[j].operand[1].val != g_stack_fsz)
+ {
+ if (ops[i].op == OP_CALL && ops[i].pp != NULL
+ && ops[i].pp->is_noreturn)
+ {
+ // noreturn tailcall with no epilogue
+ i++;
+ continue;
+ }
+ ferr(&ops[j], "'add esp' expected\n");
+ }
+
+ ops[j].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
+ ops[j].operand[1].val = 0; // hack for stack arg scanner
+ found = 1;
+ }
+
+ i++;
+ } while (i < opcnt);
+
+ if (!found)
+ ferr(ops, "missing esp epilogue\n");
+ }
+}
+
+// find an instruction that changed opr before i op
+// *op_i must be set to -1 by the caller
+// *is_caller is set to 1 if one source is determined to be g_func arg
+// returns 1 if found, *op_i is then set to origin
+// returns -1 if multiple origins are found
+static int resolve_origin(int i, const struct parsed_opr *opr,
+ int magic, int *op_i, int *is_caller)
+{
+ struct label_ref *lr;
+ int ret = 0;
+
+ while (1) {
+ if (g_labels[i] != NULL) {
+ lr = &g_label_refs[i];
+ for (; lr != NULL; lr = lr->next) {
+ check_i(&ops[i], lr->i);
+ ret |= resolve_origin(lr->i, opr, magic, op_i, is_caller);
+ }
+ if (i > 0 && LAST_OP(i - 1))
+ return ret;
+ }
+
+ i--;
+ if (i < 0) {
+ if (is_caller != NULL)
+ *is_caller = 1;
+ return -1;
+ }
+
+ if (ops[i].cc_scratch == magic)
+ return ret;
+ ops[i].cc_scratch = magic;
+
+ if (!(ops[i].flags & OPF_DATA))
+ continue;
+ if (!is_opr_modified(opr, &ops[i]))
+ continue;
+
+ if (*op_i >= 0) {
+ if (*op_i == i || are_ops_same(&ops[*op_i], &ops[i]))
+ return ret | 1;
+
+ return -1;
+ }
+
+ *op_i = i;
+ return ret | 1;
+ }
+}
+
+// find an instruction that previously referenced opr
+// if multiple results are found - fail
+// *op_i must be set to -1 by the caller
+// returns 1 if found, *op_i is then set to referencer insn
+static int resolve_last_ref(int i, const struct parsed_opr *opr,
+ int magic, int *op_i)
+{
+ struct label_ref *lr;
+ int ret = 0;
+
+ while (1) {
+ if (g_labels[i] != NULL) {
+ lr = &g_label_refs[i];
+ for (; lr != NULL; lr = lr->next) {
+ check_i(&ops[i], lr->i);
+ ret |= resolve_last_ref(lr->i, opr, magic, op_i);
+ }
+ if (i > 0 && LAST_OP(i - 1))
+ return ret;
+ }
+
+ i--;
+ if (i < 0)
+ return -1;
+
+ if (ops[i].cc_scratch == magic)
+ return 0;
+ ops[i].cc_scratch = magic;
+
+ if (!is_opr_referenced(opr, &ops[i]))
+ continue;
+
+ if (*op_i >= 0)
+ return -1;
+
+ *op_i = i;
+ return 1;
+ }
+}
+
+// adjust datap of all reachable 'op' insns when moving back
+// returns 1 if at least 1 op was found
+// returns -1 if path without an op was found
+static int adjust_prev_op(int i, enum op_op op, int magic, void *datap)
+{
+ struct label_ref *lr;
+ int ret = 0;
+
+ if (ops[i].cc_scratch == magic)
+ return 0;
+ ops[i].cc_scratch = magic;
+
+ while (1) {
+ if (g_labels[i] != NULL) {
+ lr = &g_label_refs[i];
+ for (; lr != NULL; lr = lr->next) {
+ check_i(&ops[i], lr->i);
+ ret |= adjust_prev_op(lr->i, op, magic, datap);
+ }
+ if (i > 0 && LAST_OP(i - 1))
+ return ret;
+ }
+
+ i--;
+ if (i < 0)
+ return -1;
+
+ if (ops[i].cc_scratch == magic)
+ return 0;
+ ops[i].cc_scratch = magic;
+
+ if (ops[i].op != op)
+ continue;
+
+ ops[i].datap = datap;
+ return 1;
+ }
+}
+
+// find next instruction that reads opr
+// *op_i must be set to -1 by the caller
+// on return, *op_i is set to first referencer insn
+// returns 1 if exactly 1 referencer is found
+static int find_next_read(int i, int opcnt,
+ const struct parsed_opr *opr, int magic, int *op_i)
+{
+ struct parsed_op *po;
+ int j, ret = 0;
+
+ for (; i < opcnt; i++)
+ {
+ if (ops[i].cc_scratch == magic)
+ return ret;
+ ops[i].cc_scratch = magic;
+
+ po = &ops[i];
+ if ((po->flags & OPF_JMP) && po->op != OP_CALL) {
+ if (po->btj != NULL) {
+ // jumptable
+ for (j = 0; j < po->btj->count; j++) {
+ check_i(po, po->btj->d[j].bt_i);
+ ret |= find_next_read(po->btj->d[j].bt_i, opcnt, opr,
+ magic, op_i);
+ }
+ return ret;
+ }
+
+ if (po->flags & OPF_RMD)
+ continue;
+ check_i(po, po->bt_i);
+ if (po->flags & OPF_CJMP) {
+ ret |= find_next_read(po->bt_i, opcnt, opr, magic, op_i);
+ if (ret < 0)
+ return ret;
+ }
+ else
+ i = po->bt_i - 1;
+ continue;
+ }
+
+ if (!is_opr_read(opr, po)) {
+ int full_opr = 1;
+ if (opr->type == OPT_REG && po->operand[0].type == OPT_REG
+ && opr->reg == po->operand[0].reg && (po->flags & OPF_DATA))
+ {
+ full_opr = po->operand[0].lmod >= opr->lmod;
+ }
+ if (is_opr_modified(opr, po) && full_opr) {
+ // it's overwritten
+ return ret;
+ }
+ if (po->flags & OPF_TAIL)
+ return ret;
+ continue;
+ }
+
+ if (*op_i >= 0)
+ return -1;
+
+ *op_i = i;
+ return 1;
+ }
+
+ return 0;
+}
+
+// find next instruction that reads opr
+// *op_i must be set to -1 by the caller
+// on return, *op_i is set to first flag user insn
+// returns 1 if exactly 1 flag user is found
+static int find_next_flag_use(int i, int opcnt, int magic, int *op_i)
+{
+ struct parsed_op *po;
+ int j, ret = 0;
+
+ for (; i < opcnt; i++)
+ {
+ if (ops[i].cc_scratch == magic)
+ return ret;
+ ops[i].cc_scratch = magic;
+
+ po = &ops[i];
+ if (po->op == OP_CALL)
+ return -1;
+ if (po->flags & OPF_JMP) {
+ if (po->btj != NULL) {
+ // jumptable
+ for (j = 0; j < po->btj->count; j++) {
+ check_i(po, po->btj->d[j].bt_i);
+ ret |= find_next_flag_use(po->btj->d[j].bt_i, opcnt,
+ magic, op_i);
+ }
+ return ret;
+ }
+
+ if (po->flags & OPF_RMD)
+ continue;
+ check_i(po, po->bt_i);
+ if (po->flags & OPF_CJMP)
+ goto found;
+ else
+ i = po->bt_i - 1;
+ continue;
+ }
+
+ if (!(po->flags & OPF_CC)) {
+ if (po->flags & OPF_FLAGS)
+ // flags changed
+ return ret;
+ if (po->flags & OPF_TAIL)
+ return ret;
+ continue;
+ }
+
+found:
+ if (*op_i >= 0)
+ return -1;
+
+ *op_i = i;
+ return 1;
+ }
+
+ return 0;
+}
+
+static int try_resolve_const(int i, const struct parsed_opr *opr,
+ int magic, unsigned int *val)
+{
+ int s_i = -1;
+ int ret;
+
+ ret = resolve_origin(i, opr, magic, &s_i, NULL);
+ if (ret == 1) {
+ i = s_i;
+ if (ops[i].op != OP_MOV && ops[i].operand[1].type != OPT_CONST)
+ return -1;
+
+ *val = ops[i].operand[1].val;
+ return 1;
+ }
+
+ return -1;
+}
+
+static int resolve_used_bits(int i, int opcnt, int reg,
+ int *mask, int *is_z_check)
+{
+ struct parsed_opr opr = OPR_INIT(OPT_REG, OPLM_WORD, reg);
+ int j = -1, k = -1;
+ int ret;
+
+ ret = find_next_read(i, opcnt, &opr, i + opcnt * 20, &j);
+ if (ret != 1)
+ return -1;
+
+ find_next_read(j + 1, opcnt, &opr, i + opcnt * 20 + 1, &k);
+ if (k != -1) {
+ fnote(&ops[j], "(first read)\n");
+ ferr(&ops[k], "TODO: bit resolve: multiple readers\n");
+ }
+
+ if (ops[j].op != OP_TEST || ops[j].operand[1].type != OPT_CONST)
+ ferr(&ops[j], "TODO: bit resolve: not a const test\n");
+
+ ferr_assert(&ops[j], ops[j].operand[0].type == OPT_REG);
+ ferr_assert(&ops[j], ops[j].operand[0].reg == reg);
+
+ *mask = ops[j].operand[1].val;
+ if (ops[j].operand[0].lmod == OPLM_BYTE
+ && ops[j].operand[0].name[1] == 'h')
+ {
+ *mask <<= 8;
+ }
+ ferr_assert(&ops[j], (*mask & ~0xffff) == 0);
+
+ *is_z_check = 0;
+ ret = find_next_flag_use(j + 1, opcnt, i + opcnt * 20 + 2, &k);
+ if (ret == 1)
+ *is_z_check = ops[k].pfo == PFO_Z;
+
+ return 0;
+}
+
+static const struct parsed_proto *resolve_deref(int i, int magic,
+ struct parsed_opr *opr, int level)
+{
+ struct parsed_opr opr_s = OPR_INIT(OPT_REG, OPLM_DWORD, 0);
+ const struct parsed_proto *pp = NULL;
+ int from_caller = 0;
+ char s_reg[4];
+ int offset = 0;
+ int len = 0;
+ int j = -1;
+ int k = -1;
+ int reg;
+ int ret;
+
+ ret = sscanf(opr->name, "%3s+%x%n", s_reg, &offset, &len);
+ if (ret != 2 || len != strlen(opr->name)) {
+ ret = sscanf(opr->name, "%3s%n", s_reg, &len);
+ if (ret != 1 || len != strlen(opr->name))
+ return NULL;
+ }
+
+ reg = char_array_i(regs_r32, ARRAY_SIZE(regs_r32), s_reg);
+ if (reg < 0)
+ return NULL;
+
+ opr_s.reg = reg;
+ ret = resolve_origin(i, &opr_s, i + magic, &j, NULL);
+ if (ret != 1)
+ return NULL;
+
+ if (ops[j].op == OP_MOV && ops[j].operand[1].type == OPT_REGMEM
+ && strlen(ops[j].operand[1].name) == 3
+ && ops[j].operand[0].lmod == OPLM_DWORD
+ && ops[j].pp == NULL // no hint
+ && level == 0)
+ {
+ // allow one simple dereference (com/directx)
+ reg = char_array_i(regs_r32, ARRAY_SIZE(regs_r32),
+ ops[j].operand[1].name);
+ if (reg < 0)
+ return NULL;
+ opr_s.reg = reg;
+ ret = resolve_origin(j, &opr_s, j + magic, &k, NULL);
+ if (ret != 1)
+ return NULL;
+ j = k;
+ }
+ if (ops[j].op != OP_MOV || ops[j].operand[0].lmod != OPLM_DWORD)
+ return NULL;
+
+ if (ops[j].pp != NULL) {
+ // type hint in asm
+ pp = ops[j].pp;
+ }
+ else if (ops[j].operand[1].type == OPT_REGMEM) {
+ pp = try_recover_pp(&ops[j], &ops[j].operand[1], 0, NULL);
+ if (pp == NULL) {
+ // maybe structure ptr in structure
+ pp = resolve_deref(j, magic, &ops[j].operand[1], level + 1);
+ }
+ }
+ else if (ops[j].operand[1].type == OPT_LABEL)
+ pp = proto_parse(g_fhdr, ops[j].operand[1].name, g_quiet_pp);
+ else if (ops[j].operand[1].type == OPT_REG) {
+ // maybe arg reg?
+ k = -1;
+ ret = resolve_origin(j, &ops[j].operand[1], i + magic,
+ &k, &from_caller);
+ if (ret != 1 && from_caller && k == -1 && g_func_pp != NULL) {
+ for (k = 0; k < g_func_pp->argc; k++) {
+ if (g_func_pp->arg[k].reg == NULL)
+ continue;
+ if (IS(g_func_pp->arg[k].reg, ops[j].operand[1].name)) {
+ pp = g_func_pp->arg[k].pp;
+ break;
+ }
+ }
+ }
+ }
+
+ if (pp == NULL)
+ return NULL;
+ if (pp->is_func || pp->is_fptr || !pp->type.is_struct) {
+ if (offset != 0)
+ ferr(&ops[j], "expected struct, got '%s %s'\n",
+ pp->type.name, pp->name);
+ return NULL;
+ }
+
+ return proto_lookup_struct(g_fhdr, pp->type.name, offset);