quakeforge/tools/qfcc/source/dump_globals.c

405 lines
10 KiB
C
Raw Normal View History

/*
2011-02-24 04:00:19 +00:00
dump_globals.c
Dump progs globals information.
Copyright (C) 2002 Bill Currie <bill@taniwha.org>
Author: Bill Currie <bill@taniwha.org>
Date: 2002/05/16
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
static __attribute__ ((used)) const char rcsid[] =
"$Id$";
#include <stdlib.h>
2003-04-23 20:10:51 +00:00
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
2007-04-09 07:47:51 +00:00
#include "QF/dstring.h"
#include "QF/progs.h"
#include "QF/va.h"
2007-04-09 07:47:51 +00:00
#include "obj_file.h"
#include "qfprogs.h"
2007-04-09 13:33:59 +00:00
#include "reloc.h"
2003-04-23 20:10:51 +00:00
static int
cmp (const void *_a, const void *_b)
{
const ddef_t *a = (const ddef_t *)_a;
const ddef_t *b = (const ddef_t *)_b;
return a->ofs - b->ofs;
}
static void
dump_def (progs_t *pr, ddef_t *def, int indent)
{
const char *name;
const char *type;
int offset;
const char *comment;
int saveglobal;
if (!def->type && !def->ofs && !def->s_name)
return;
name = PR_GetString (pr, def->s_name);
type = pr_type_name[def->type & ~DEF_SAVEGLOBAL];
saveglobal = (def->type & DEF_SAVEGLOBAL) != 0;
offset = def->ofs;
comment = "";
switch (def->type & ~DEF_SAVEGLOBAL) {
case ev_void:
break;
case ev_string:
comment = va (" %d \"%s\"", G_INT (pr, offset),
pr->pr_strings + G_INT (pr, offset));
break;
case ev_float:
comment = va (" %g", G_FLOAT (pr, offset));
break;
case ev_vector:
comment = va (" '%g %g %g",
G_VECTOR (pr, offset)[0],
G_VECTOR (pr, offset)[1],
G_VECTOR (pr, offset)[2]);
break;
case ev_entity:
break;
case ev_field:
comment = va (" %x", G_INT (pr, offset));
break;
case ev_func:
{
func_t func = G_FUNCTION (pr, offset);
int start;
if (func >= 0 && func < pr->progs->numfunctions) {
start = pr->pr_functions[func].first_statement;
if (start > 0)
comment = va (" %d @ %x", func, start);
else
comment = va (" %d = #%d", func, -start);
} else {
comment = va (" %d = illegal function", func);
}
}
break;
case ev_pointer:
comment = va (" %x", G_INT (pr, offset));
break;
case ev_quat:
comment = va (" '%g %g %g %g",
G_QUAT (pr, offset)[0],
G_QUAT (pr, offset)[1],
G_QUAT (pr, offset)[2],
G_QUAT (pr, offset)[3]);
break;
case ev_integer:
comment = va (" %d", G_INT (pr, offset));
break;
case ev_short:
break;
case ev_invalid:
comment = " struct?";
break;
case ev_type_count:
break;
}
printf ("%*s %x %d %s %s%s\n", indent * 12, "",
offset, saveglobal, name, type, comment);
}
void
dump_globals (progs_t *pr)
{
unsigned int i;
2003-04-23 20:10:51 +00:00
ddef_t *global_defs = pr->pr_globaldefs;
2003-04-23 20:10:51 +00:00
if (sorted) {
global_defs = malloc (pr->progs->numglobaldefs * sizeof (ddef_t));
memcpy (global_defs, pr->pr_globaldefs,
pr->progs->numglobaldefs * sizeof (ddef_t));
qsort (global_defs, pr->progs->numglobaldefs, sizeof (ddef_t), cmp);
}
for (i = 0; i < pr->progs->numglobaldefs; i++) {
2003-04-23 20:10:51 +00:00
ddef_t *def = &global_defs[i];
dump_def (pr, def, 0);
}
}
2002-06-09 16:28:19 +00:00
void
dump_fields (progs_t *pr)
{
2003-04-17 00:01:48 +00:00
unsigned int i;
2002-06-09 16:28:19 +00:00
const char *name;
const char *type;
int offset;
const char *comment;
2002-06-09 16:28:19 +00:00
for (i = 0; i < pr->progs->numfielddefs; i++) {
ddef_t *def = &pr->pr_fielddefs[i];
name = PR_GetString (pr, def->s_name);
type = pr_type_name[def->type & ~DEF_SAVEGLOBAL];
offset = def->ofs;
2007-04-09 07:47:51 +00:00
comment = "";
2002-06-09 16:28:19 +00:00
2007-04-09 07:47:51 +00:00
printf ("%d %s %s%s\n", offset, name, type, comment);
2002-06-09 16:28:19 +00:00
}
}
void
dump_functions (progs_t *pr)
{
2002-06-27 22:51:42 +00:00
int i, j;
2002-06-09 16:28:19 +00:00
const char *name;
2002-06-27 22:51:42 +00:00
int start, count;
const char *comment;
2002-06-09 16:28:19 +00:00
for (i = 0; i < pr->progs->numfunctions; i++) {
dfunction_t *func = &pr->pr_functions[i];
name = PR_GetString (pr, func->s_name);
2007-04-09 07:47:51 +00:00
comment = "";
2002-06-09 16:28:19 +00:00
start = func->first_statement;
if (start > 0)
2011-01-09 15:00:18 +00:00
comment = va (" @ %x", start);
2002-06-09 16:28:19 +00:00
else
comment = va (" = #%d", -start);
printf ("%-5d %s%s: %d (", i, name, comment, func->numparms);
2002-06-27 22:51:42 +00:00
if (func->numparms < 0)
count = -func->numparms - 1;
else
count = func->numparms;
for (j = 0; j < count; j++)
printf (" %d", func->parm_size[j]);
2011-01-28 09:03:26 +00:00
printf (") %d @ %x", func->locals, func->parm_start);
2002-06-27 22:51:42 +00:00
puts ("");
if (pr->debug) {
pr_auxfunction_t *aux = pr->auxfunction_map[i];
if (!aux)
continue;
printf (" %d %s:%d %d %d %d %x\n", aux->function,
PR_GetString (pr, func->s_file), aux->source_line,
aux->line_info,
aux->local_defs, aux->num_locals,
aux->return_type);
for (j = 0; j < (int)aux->num_locals; j++)
dump_def (pr, pr->local_defs + aux->local_defs + j, 1);
}
2002-06-09 16:28:19 +00:00
}
}
2007-04-09 07:47:51 +00:00
2011-02-25 00:39:07 +00:00
static const char *
flags_string (pr_uint_t flags)
{
static dstring_t *str;
if (!str)
str = dstring_newstr ();
dstring_clearstr (str);
dstring_appendstr (str, (flags & QFOD_INITIALIZED) ? "I" : "-");
dstring_appendstr (str, (flags & QFOD_CONSTANT) ? "C" : "-");
dstring_appendstr (str, (flags & QFOD_ABSOLUTE) ? "A" : "-");
dstring_appendstr (str, (flags & QFOD_GLOBAL) ? "G" : "-");
dstring_appendstr (str, (flags & QFOD_EXTERNAL) ? "E" : "-");
dstring_appendstr (str, (flags & QFOD_LOCAL) ? "L" : "-");
dstring_appendstr (str, (flags & QFOD_SYSTEM) ? "S" : "-");
dstring_appendstr (str, (flags & QFOD_NOSAVE) ? "N" : "-");
return str->str;
}
2007-04-09 07:47:51 +00:00
void
qfo_globals (qfo_t *qfo)
{
qfo_def_t *def;
int i;
2011-02-25 00:39:07 +00:00
int space;
int count = 0;
for (space = 0; space < qfo->num_spaces; space++) {
for (i = 0; i < qfo->spaces[space].num_defs; i++, count++) {
def = &qfo->spaces[space].defs[i];
printf ("%-5d %2d:%-5x %s %s %s", count, space, def->offset,
flags_string (def->flags),
QFO_GETSTR (qfo, def->name),
QFO_TYPESTR (qfo, def->type));
if (!(def->flags & QFOD_EXTERNAL) && qfo->spaces[space].d.data)
2011-02-25 00:39:07 +00:00
printf (" %d",
qfo->spaces[space].d.data[def->offset].integer_var);
puts ("");
}
2007-04-09 07:47:51 +00:00
}
}
2007-04-09 13:33:59 +00:00
void
qfo_relocs (qfo_t *qfo)
{
qfo_reloc_t *reloc;
qfo_def_t *def;
qfo_func_t *func;
int i;
for (i = 0; i < qfo->num_relocs; i++) {
reloc = qfo->relocs + i;
if ((unsigned) reloc->type > rel_def_field_ofs) {
printf ("%d unknown reloc: %d\n", i, reloc->type);
continue;
}
printf ("%s", reloc_names[reloc->type]);
def = 0;
func = 0;
switch ((reloc_type) reloc->type) {
case rel_none:
break;
case rel_op_a_def:
case rel_op_b_def:
case rel_op_c_def:
def = qfo->defs + reloc->target;
2011-03-05 07:25:56 +00:00
printf (" op.%c@%x def#%d %s",
2011-02-25 04:29:09 +00:00
reloc->type - rel_op_a_def + 'a',
reloc->offset, reloc->target,
2011-02-25 04:29:09 +00:00
QFO_GETSTR (qfo, def->name));
2007-04-09 13:33:59 +00:00
break;
case rel_op_a_op:
case rel_op_b_op:
case rel_op_c_op:
2011-03-05 07:25:56 +00:00
printf (" op.%c op@%x", reloc->type - rel_op_a_def + 'a',
reloc->offset);
2007-04-09 13:33:59 +00:00
break;
case rel_def_op:
2011-02-25 04:29:09 +00:00
printf (" def@%d:%x op@%d", reloc->space, reloc->offset,
reloc->target);
2007-04-09 13:33:59 +00:00
break;
case rel_def_def:
def = qfo->defs + reloc->target;
2011-02-25 04:29:09 +00:00
printf (" def@%d:%x def#%d %s", reloc->space, reloc->offset,
reloc->target, QFO_GETSTR (qfo, def->name));
2007-04-09 13:33:59 +00:00
break;
case rel_def_func:
func = qfo->funcs + reloc->target;
//func = qfo->funcs + QFO_FUNCTION (qfo, reloc->space,
// reloc->offset);
2011-02-25 04:29:09 +00:00
printf (" def@%d:%x func#%d %s %x",
reloc->space, reloc->offset,
QFO_FUNCTION (qfo, reloc->space, reloc->offset),
QFO_GETSTR (qfo, func->name),
reloc->target);
2007-04-09 13:33:59 +00:00
break;
case rel_def_string:
2011-02-25 04:29:09 +00:00
printf (" def@%d:%x string:%x `%s'",
reloc->space, reloc->offset, reloc->target,
QFO_GETSTR (qfo, reloc->target));
2007-04-09 13:33:59 +00:00
break;
case rel_def_field:
def = qfo->defs + reloc->target;
2011-02-25 04:29:09 +00:00
printf (" def@%d:%x def#%d %s", reloc->space, reloc->offset,
reloc->target, QFO_GETSTR (qfo, def->name));
2007-04-09 13:33:59 +00:00
break;
case rel_op_a_def_ofs:
case rel_op_b_def_ofs:
case rel_op_c_def_ofs:
def = qfo->defs + reloc->target;
2011-02-25 04:29:09 +00:00
printf (" op.%c@%x def#%d %s",
reloc->type - rel_op_a_def_ofs + 'a',
reloc->offset, reloc->target,
2011-02-25 04:29:09 +00:00
QFO_GETSTR (qfo, def->name));
2007-04-09 13:33:59 +00:00
break;
case rel_def_def_ofs:
def = qfo->defs + reloc->target;
2011-02-25 04:29:09 +00:00
printf (" def@%d:%x def#%d+%d %s+%d",
reloc->space, reloc->offset, reloc->target,
2011-02-25 04:29:09 +00:00
QFO_INT (qfo, reloc->space, reloc->offset),
QFO_GETSTR (qfo, def->name),
QFO_INT (qfo, reloc->space, reloc->offset));
2007-04-09 13:33:59 +00:00
break;
case rel_def_field_ofs:
def = qfo->defs + reloc->target;
printf (" def@%d:%x def#%d+%d %s(%d)+%d",
reloc->space, reloc->offset, reloc->target,
2011-02-25 04:29:09 +00:00
QFO_INT (qfo, reloc->space, reloc->offset),
QFO_GETSTR (qfo, def->name), def->offset,
2011-02-25 04:29:09 +00:00
QFO_INT (qfo, reloc->space, reloc->offset));
2007-04-09 13:33:59 +00:00
break;
}
if (def && def->flags & QFOD_EXTERNAL)
printf (" external");
if (func && qfo->defs[func->def].flags & QFOD_EXTERNAL)
printf (" external");
if (def && (i < def->relocs || i >= def->relocs + def->num_relocs))
2011-02-25 04:29:09 +00:00
printf (" BOGUS def reloc! %d %d %d",
i, def->relocs, def->num_relocs);
2007-04-09 13:33:59 +00:00
if (func && (i < func->relocs || i >= func->relocs + func->num_relocs))
2011-02-25 04:29:09 +00:00
printf (" BOGUS func reloc! %d %d %d",
i, func->relocs, func->num_relocs);
2007-04-09 13:33:59 +00:00
puts ("");
}
}
void
qfo_functions (qfo_t *qfo)
{
qfo_def_t *def;
qfo_func_t *func;
2011-02-25 04:37:23 +00:00
int i, d;
int space;
for (i = 0; i < qfo->num_funcs; i++) {
func = &qfo->funcs[i];
def = &qfo->defs[func->def];
2011-02-25 04:37:23 +00:00
for (space = 0; space < qfo->num_spaces; space++) {
if (!qfo->spaces[space].num_defs)
continue;
d = qfo->spaces[space].defs - qfo->defs;
if (func->def >= d && func->def < d + qfo->spaces[space].num_defs)
break;
}
if (space == qfo->num_spaces)
space = qfo_near_data_space;
printf ("%-5d %-5d %s %s %d %s", i, def->offset,
flags_string (def->flags),
QFO_GETSTR (qfo, func->name), func->def,
QFO_GETSTR (qfo, def->name));
if (!(def->flags & QFOD_EXTERNAL))
printf (" %d", QFO_FUNCTION (qfo, space, def->offset));
if (func->code > 0)
2011-01-09 15:00:18 +00:00
printf (" @ %x", func->code);
else
printf (" = #%d", -func->code);
puts ("");
}
}