*
* This work is licensed under the terms of 3-clause BSD license.
* See COPYING file in the top-level directory.
+ *
+ * recognized asm hint comments:
+ * sctattr - function attributes (see code)
+ * sctend - force end of function/chunk
+ * sctpatch: <p> - replace current asm line with <p>
+ * sctproto: <p> - prototype of ref'd function or struct
+ * sctref - variable is referenced, make global
+ * sctskip_start - start of skipped code chunk (inclusive)
+ * sctskip_end - end of skipped code chunk (inclusive)
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <errno.h>
#include "my_assert.h"
#include "my_str.h"
#include "common.h"
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
-#define IS(w, y) !strcmp(w, y)
-#define IS_START(w, y) !strncmp(w, y, strlen(y))
-
#include "protoparse.h"
static const char *asmfn;
}
if ('0' <= w[0] && w[0] <= '9') {
- number = parse_number(w);
+ number = parse_number(w, 0);
printf_number(d, sizeof(cvtbuf) - (d - cvtbuf), number);
continue;
}
if (len < sizeof(buf) - 1) {
strncpy(buf, s, len);
buf[len] = 0;
+ errno = 0;
val = strtol(buf, &endp, 16);
- if (val == 0 || *endp != 0) {
+ if (val == 0 || *endp != 0 || errno != 0) {
aerr("%s num parse fail for '%s'\n", __func__, buf);
return NULL;
}
else if (('0' <= words[w][0] && words[w][0] <= '9')
|| words[w][0] == '-')
{
- number = parse_number(words[w]);
+ number = parse_number(words[w], 0);
opr->type = OPT_CONST;
opr->val = number;
printf_number(opr->name, sizeof(opr->name), number);
if (namelen <= 0)
ferr(po, "equ parse failed for '%s'\n", name);
+ errno = 0;
*extra_offs = strtol(p, &endp, 16);
- if (*endp != 0)
+ if (*endp != 0 || errno != 0)
ferr(po, "equ parse failed for '%s'\n", name);
}
p = name + 4;
if (IS_START(p, "0x"))
p += 2;
+ errno = 0;
offset = strtoul(p, &endp, 16);
if (name[3] == '-')
offset = -offset;
- if (*endp != 0)
+ if (*endp != 0 || errno != 0)
ferr(po, "ebp- parse of '%s' failed\n", name);
}
else {
// just plain offset?
if (!IS_START(name, "esp+"))
return -1;
+ errno = 0;
offset = strtol(name + 4, &endp, 0);
- if (endp == NULL || *endp != 0)
+ if (endp == NULL || *endp != 0 || errno != 0)
return -1;
*offset_out = offset;
return 0;
if (po->pp != NULL && po->pp->is_noreturn)
seen_noreturn = 1;
else
- return -1;
+ goto out;
}
else
- return -1; // deadend
+ goto out;
}
if (po->flags & (OPF_RMD|OPF_DONE|OPF_FARG))
}
}
+out:
// for noreturn, assume msvc skipped stack cleanup
return seen_noreturn ? 1 : -1;
}
return -1;
}
- // nothing interesting on this path
- return 0;
+ // nothing interesting on this path,
+ // still return ret for something recursive calls could find
+ return ret;
}
static void find_reachable_exits(int i, int opcnt, int magic,
{
static int exits[MAX_EXITS];
static int exit_count;
- int j, ret;
+ int found = 0;
+ int e, j, ret;
if (!set_flags) {
exit_count = 0;
}
for (j = 0; j < exit_count; j++) {
- ret = scan_for_rsave_pop_reg(exits[j], i + opcnt * 16 + set_flags,
+ e = exits[j];
+ ret = scan_for_rsave_pop_reg(e, i + opcnt * 16 + set_flags,
reg, set_flags);
- if (ret == -1)
- return -1;
+ if (ret != -1) {
+ found |= ret;
+ continue;
+ }
+ if (ops[e].op == OP_CALL && ops[e].pp != NULL
+ && ops[e].pp->is_noreturn)
+ {
+ // assume stack cleanup was skipped
+ continue;
+ }
+ return -1;
}
- return 1;
+ return found;
}
// scan for one or more pop of push <const>
struct parsed_data *pd;
struct parsed_op *po;
const char *tmpname;
+ enum op_op prev_op;
int i, l;
int ret;
if (pp != NULL) {
if (pp->is_fptr)
check_func_pp(po, pp, "fptr var call");
- if (pp->is_noreturn)
+ if (pp->is_noreturn) {
po->flags |= OPF_TAIL;
+ po->flags &= ~OPF_ATAIL; // most likely...
+ }
}
po->pp = pp;
continue;
tailcall:
po->op = OP_CALL;
po->flags |= OPF_TAIL;
- if (i > 0 && ops[i - 1].op == OP_POP)
+ prev_op = i > 0 ? ops[i - 1].op : OP_UD2;
+ if (prev_op == OP_POP)
po->flags |= OPF_ATAIL;
+ if (g_stack_fsz + g_bp_frame == 0 && prev_op != OP_PUSH
+ && (g_func_pp == NULL || g_func_pp->argc_stack > 0))
+ {
+ po->flags |= OPF_ATAIL;
+ }
i--; // reprocess
}
}
l += ops[j].operand[1].val / 4 - 1;
}
else
- ferr(&ops[j], "'pop ecx' expected\n");
+ break;
ops[j].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
j--;
}
- if (l != ecx_push)
+ if (l != ecx_push) {
+ if (i < opcnt && ops[i].op == OP_CALL
+ && ops[i].pp != NULL && ops[i].pp->is_noreturn)
+ {
+ // noreturn tailcall with no epilogue
+ i++;
+ found = 1;
+ continue;
+ }
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)
+ || ops[j].operand[1].type != OPT_CONST)
{
- if (ops[i].op == OP_CALL && ops[i].pp != NULL
- && ops[i].pp->is_noreturn)
+ if (i < opcnt && ops[i].op == OP_CALL
+ && ops[i].pp != NULL && ops[i].pp->is_noreturn)
{
// noreturn tailcall with no epilogue
i++;
+ found = 1;
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
+ if (ops[j].operand[1].val < g_stack_fsz)
+ ferr(&ops[j], "esp adj is too low (need %d)\n", g_stack_fsz);
+
+ ops[j].operand[1].val -= g_stack_fsz; // for stack arg scanner
+ if (ops[j].operand[1].val == 0)
+ ops[j].flags |= OPF_RMD | OPF_DONE | OPF_NOREGS;
found = 1;
}
ferr(po, "too many args for '%s'\n", tmpname);
}
if (pp->argc_stack > adj / 4) {
+ if (pp->is_noreturn)
+ // assume no stack adjust was emited
+ goto out;
fnote(po, "(this call)\n");
ferr(&ops[ret], "stack tracking failed for '%s': %x %x\n",
tmpname, pp->argc_stack * 4, adj);
ferr(po, "missing esp_adjust for vararg func '%s'\n",
pp->name);
+out:
return pp;
}
ret = parse_stack_esp_offset(po, po->operand[0].name, &offset);
if (ret != 0)
continue;
- if (offset < 0 || offset >= pp->argc_stack * 4 || (offset & 3))
- ferr(po, "bad offset %d (%d args)\n", offset, pp->argc_stack);
+ if (offset < 0 || offset >= pp->argc_stack * 4 || (offset & 3)) {
+ //ferr(po, "offset %d, %d args\n", offset, pp->argc_stack);
+ continue;
+ }
arg = base_arg + offset / 4;
po->p_argnext = -1;
pp_tmp = ops[j].pp;
if (pp_tmp == NULL)
- ferr(po, "arg collect hit unparsed call '%s'\n",
- ops[j].operand[0].name);
+ ferr(po, "arg collect %d/%d hit unparsed call '%s'\n",
+ arg, pp->argc, ops[j].operand[0].name);
if (may_reuse && pp_tmp->argc_stack > 0)
ferr(po, "arg collect %d/%d hit '%s' with %d stack args\n",
arg, pp->argc, opr_name(&ops[j], 0), pp_tmp->argc_stack);
lmod_cast_s(po, po->operand[0].lmod), buf2);
last_arith_dst = &po->operand[0];
delayed_flag_op = NULL;
- if (pfomask & (1 << PFO_C)) {
+ if (pfomask & PFOB_C) {
fprintf(fout, "\n cond_c = (%s != 0);", buf1);
- pfomask &= ~(1 << PFO_C);
+ pfomask &= ~PFOB_C;
}
+ output_std_flags(fout, po, &pfomask, buf1);
break;
case OP_IMUL:
pp->has_structarg ? "_sa" : "");
if (po->flags & OPF_ATAIL) {
- if (pp->argc_stack != g_func_pp->argc_stack
- || (pp->argc_stack > 0
- && pp->is_stdcall != g_func_pp->is_stdcall))
- ferr(po, "incompatible tailcall\n");
+ 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");
if (pp->arg[arg].type.is_retreg)
fprintf(fout, "&%s", pp->arg[arg].reg);
else if (IS(pp->arg[arg].reg, "ebp")
- && !(po->flags & OPF_EBP_S))
+ && g_bp_frame && !(po->flags & OPF_EBP_S))
{
// rare special case
fprintf(fout, "%s(u32)&sf.b[sizeof(sf)]", cast);
po->regmask_dst |= 1 << xAX;
dep = hg_fp_find_dep(fp, po->operand[0].name);
- if (dep != NULL)
+ if (dep != NULL) {
dep->regmask_live = regmask_save | regmask_dst;
+ if (g_bp_frame && !(po->flags & OPF_EBP_S))
+ dep->regmask_live |= 1 << xBP;
+ }
}
else if (po->op == OP_RET) {
if (po->operand_cnt > 0) {
// noreturn OS functions
break;
}
- if (ops[i].op != OP_NOP)
+ if (ops[i].op != OP_NOP && ops[i].op != OPP_ABORT)
ferr(&ops[i], "unreachable code\n");
}
long pos;
p = strrchr(p, ';');
- if (p != NULL && *p == ';' && IS_START(p + 2, "DATA XREF: ")) {
- p += 13;
- if (is_xref_needed(p, rlist, rlist_len))
+ if (p != NULL && *p == ';') {
+ if (IS_START(p + 2, "sctref"))
return 1;
+ if (IS_START(p + 2, "DATA XREF: ")) {
+ p += 13;
+ if (is_xref_needed(p, rlist, rlist_len))
+ return 1;
+ }
}
pos = ftell(fasm);
p = strrchr(p, ';');
p += 2;
+
+ if (IS_START(p, "sctref")) {
+ found_need = 1;
+ break;
+ }
+
// it's printed once, but no harm to check again
if (IS_START(p, "DATA XREF: "))
p += 11;
return strcmp(c1->name, c2->name);
}
-static void scan_ahead(FILE *fasm)
+static void scan_ahead_for_chunks(FILE *fasm)
{
char words[2][256];
char line[256];
if (addr > f_addr && !scanned_ahead) {
//anote("scan_ahead caused by '%s', addr %lx\n",
// g_func, addr);
- scan_ahead(fasm);
+ scan_ahead_for_chunks(fasm);
scanned_ahead = 1;
func_chunks_sorted = 0;
}
if (pd->type == OPT_OFFSET)
pd->d[pd->count].u.label = strdup(words[i]);
else
- pd->d[pd->count].u.val = parse_number(words[i]);
+ pd->d[pd->count].u.val = parse_number(words[i], 0);
pd->d[pd->count].bt_i = -1;
pd->count++;
}
else
aerr("bad lmod: '%s'\n", words[2]);
- g_eqs[g_eqcnt].offset = parse_number(words[4]);
+ g_eqs[g_eqcnt].offset = parse_number(words[4], 0);
g_eqcnt++;
continue;
}