Slightly better decompiler behaviours.

git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@5651 fc73d0e0-1445-4013-8a0c-d673dee63da5
This commit is contained in:
Spoike 2020-03-15 06:58:03 +00:00
parent 2152eda3d0
commit 5016371660
3 changed files with 67 additions and 59 deletions

View file

@ -1705,7 +1705,7 @@ void DecompileIndent(int c)
void DecompileOpcode(dfunction_t *df, int a, int b, int c, char *opcode, QCC_type_t *typ1, QCC_type_t *typ2, QCC_type_t *typ3, int usebrackets, int *indent) void DecompileOpcode(dfunction_t *df, int a, int b, int c, char *opcode, QCC_type_t *typ1, QCC_type_t *typ2, QCC_type_t *typ3, int usebrackets, int *indent)
{ {
static char line[512]; static char line[8192];
char *arg1, *arg2, *arg3; char *arg1, *arg2, *arg3;
arg1 = DecompileGet(df, a, typ1); arg1 = DecompileGet(df, a, typ1);
arg2 = DecompileGet(df, b, typ2); arg2 = DecompileGet(df, b, typ2);
@ -1897,20 +1897,18 @@ void DecompileDecompileStatement(dfunction_t * df, dstatement_t * s, int *indent
if ((k->op >= OP_CALL0 && k->op <= OP_CALL8) || (k->op >= OP_CALL1H && k->op <= OP_CALL8H)) if ((k->op >= OP_CALL0 && k->op <= OP_CALL8) || (k->op >= OP_CALL1H && k->op <= OP_CALL8H))
{ {
//well, this is it. //well, this is it.
int fn; int fn = ((int*)pr_globals)[k->a];
dfunction_t *cf = &functions[fn];
int bi = DecompileBuiltin(cf);
int pn; int pn;
dfunction_t *cf;
QCC_ddef_t *def; QCC_ddef_t *def;
int bi = DecompileBuiltin(df); if (bi)
fn = ((int*)pr_globals)[k->a]; { //builtins don't have valid parm_start values
cf = &functions[fn];
if (bi) //builtins don't have this info.
{
QCC_type_t **p = builtins[bi].params[(s->b-ofs_parms[0])/ofs_size]; QCC_type_t **p = builtins[bi].params[(s->b-ofs_parms[0])/ofs_size];
parmtype = p?*p:NULL; parmtype = p?*p:NULL;
} }
else else
{ { //qc functions do, though.
fn = cf->parm_start; fn = cf->parm_start;
for (pn = 0; pn < (s->b-ofs_parms[0])/ofs_size; pn++) for (pn = 0; pn < (s->b-ofs_parms[0])/ofs_size; pn++)
fn += cf->parm_size[pn]; fn += cf->parm_size[pn];
@ -2286,6 +2284,9 @@ void DecompileDecompileStatement(dfunction_t * df, dstatement_t * s, int *indent
QCC_CatVFile(Decompileofile, "{\n"); QCC_CatVFile(Decompileofile, "{\n");
(*indent)++; (*indent)++;
//FIXME: look for the next control statement (ignoring writes to temps, maybe allow functions too if the following statement reads OFS_RETURN)
//if its an IFNOT (optionally with its own else) that ends at our else then output this as "}\nelse " instead
} }
else else
{ {
@ -2514,6 +2515,7 @@ const char *GetMatchingField(QCC_ddef_t *field)
int i; int i;
QCC_ddef_t *def; QCC_ddef_t *def;
int ld, lf; int ld, lf;
const char *ret = NULL;
def = NULL; def = NULL;
@ -2526,21 +2528,22 @@ const char *GetMatchingField(QCC_ddef_t *field)
if (((int*)pr_globals)[def->ofs] == field->ofs) if (((int*)pr_globals)[def->ofs] == field->ofs)
{ {
if (!strcmp(strings+def->s_name, strings+field->s_name)) if (!strcmp(strings+def->s_name, strings+field->s_name))
break; //found ourself, give up. return NULL; //found ourself, give up.
lf = strlen(strings + field->s_name); lf = strlen(strings + field->s_name);
ld = strlen(strings + def->s_name); ld = strlen(strings + def->s_name);
if (lf - 2 == ld) if (lf - 2 == ld)
{ {
if ((strings + field->s_name)[lf-2] == '_') if ((strings + field->s_name)[lf-2] == '_' && (strings + field->s_name)[lf-1] == 'x')
if (!strncmp(strings + field->s_name, strings + def->s_name, ld)) if (!strncmp(strings + field->s_name, strings + def->s_name, ld))
break; return NULL; //vector found foo_x
} }
return def->s_name+strings; if (!ret)
ret = def->s_name+strings;
} }
} }
} }
return NULL; return ret;
} }
QCC_ddef_t *GetField(const char *name) QCC_ddef_t *GetField(const char *name)
@ -3100,9 +3103,10 @@ void DecompileDecompileFunctions(const char *origcopyright)
unsigned int o; unsigned int o;
dfunction_t *d; dfunction_t *d;
pbool bogusname; pbool bogusname;
vfile_t *f; vfile_t *f = NULL;
char fname[512]; char fname[512];
int lastglob = 1; int lastglob = 1;
int lastfileofs = 0;
QCC_ddef_t *def; QCC_ddef_t *def;
DecompileCalcProfiles(); DecompileCalcProfiles();
@ -3148,58 +3152,62 @@ void DecompileDecompileFunctions(const char *origcopyright)
{ {
d = &functions[i]; d = &functions[i];
fname[0] = '\0'; if (d->s_file != lastfileofs || f == NULL)
if (d->s_file <= strofs && d->s_file >= 0)
sprintf(fname, "%s", strings + d->s_file);
// FrikaC -- not sure if this is cool or what?
bogusname = false;
if (strlen(fname) <= 0)
bogusname = true;
else for (o = 0; o < strlen(fname); o++)
{ {
if ((fname[o] < 'a' || fname[o] > 'z') && lastfileofs = d->s_file;
(fname[o] < '0' || fname[o] > '9') && fname[0] = '\0';
(fname[o] <'A' || fname[o] > 'Z') && if (d->s_file <= strofs && d->s_file >= 0)
(fname[o] != '.' && fname[o] != '!' && fname[o] != '_')) sprintf(fname, "%s", strings + d->s_file);
// FrikaC -- not sure if this is cool or what?
bogusname = false;
if (strlen(fname) <= 0)
bogusname = true;
else for (o = 0; o < strlen(fname); o++)
{ {
if (fname[o] == '/') if ((fname[o] < 'a' || fname[o] > 'z') &&
fname[o] = '.'; (fname[o] < '0' || fname[o] > '9') &&
else if (fname[o] == '\\') (fname[o] <'A' || fname[o] > 'Z') &&
fname[o] = '.'; (fname[o] != '.' && fname[o] != '!' && fname[o] != '_'))
else
{ {
bogusname = true; if (fname[o] == '/')
break; fname[o] = '.';
else if (fname[o] == '\\')
fname[o] = '.';
else
{
bogusname = true;
break;
}
} }
} }
}
if (bogusname) if (bogusname)
{
if (*fname && !DecompileAlreadySeen(fname, NULL))
{ {
synth_name[0] = 0; if (*fname && !DecompileAlreadySeen(fname, NULL))
{
synth_name[0] = 0;
}
if(!TrySynthName(qcva("%s", strings + d->s_name)) && !synth_name[0])
QC_snprintfz(synth_name, sizeof(synth_name), "frik%i.qc", fake_name++);
QC_snprintfz(fname, sizeof(fname), synth_name);
} }
if(!TrySynthName(qcva("%s", strings + d->s_name)) && !synth_name[0]) else
QC_snprintfz(synth_name, sizeof(synth_name), "frik%i.qc", fake_name++); synth_name[0] = 0;
QC_snprintfz(fname, sizeof(fname), synth_name);
}
else
synth_name[0] = 0;
if (!DecompileAlreadySeen(fname, &f)) if (!DecompileAlreadySeen(fname, &f))
{ {
printf("decompiling %s\n", fname); printf("decompiling %s\n", fname);
compilecb(); compilecb();
QCC_CatVFile(Decompileprogssrc, "%s\n", fname); QCC_CatVFile(Decompileprogssrc, "%s\n", fname);
} }
if (!f) if (!f)
{ {
printf("Fatal Error - Could not open \"%s\" for output.\n", fname); printf("Fatal Error - Could not open \"%s\" for output.\n", fname);
exit(1); exit(1);
}
} }
Decompileofile = f; Decompileofile = f;
DecompileFunction(strings + d->s_name, &lastglob); DecompileFunction(strings + d->s_name, &lastglob);

View file

@ -6589,7 +6589,7 @@ static QCC_sref_t QCC_PR_ParseFunctionCall (QCC_ref_t *funcref) //warning, the f
if (arg == 0) if (arg == 0)
{ {
// save information for model and sound caching // save information for model and sound caching
if (!strncmp(funcname,"precache_", 9) && e->cast->type == ev_string && e->type == REF_GLOBAL) if (!strncmp(funcname,"precache_", 9) && e->cast->type == ev_string && e->base.cast->type == ev_string && e->type == REF_GLOBAL)
{ {
const QCC_eval_t *eval = QCC_SRef_EvalConst(e->base); const QCC_eval_t *eval = QCC_SRef_EvalConst(e->base);
if (eval) if (eval)

View file

@ -1083,7 +1083,7 @@ vfile_t *QCC_AddVFile(const char *name, void *data, size_t size)
void QCC_CatVFile(vfile_t *f, const char *fmt, ...) void QCC_CatVFile(vfile_t *f, const char *fmt, ...)
{ {
va_list argptr; va_list argptr;
char msg[8192]; char msg[65536];
size_t n; size_t n;
va_start (argptr,fmt); va_start (argptr,fmt);
@ -1103,7 +1103,7 @@ void QCC_CatVFile(vfile_t *f, const char *fmt, ...)
void QCC_InsertVFile(vfile_t *f, size_t pos, const char *fmt, ...) void QCC_InsertVFile(vfile_t *f, size_t pos, const char *fmt, ...)
{ {
va_list argptr; va_list argptr;
char msg[8192]; char msg[65536];
size_t n; size_t n;
va_start (argptr,fmt); va_start (argptr,fmt);
QC_vsnprintf (msg,sizeof(msg)-1, fmt, argptr); QC_vsnprintf (msg,sizeof(msg)-1, fmt, argptr);