2002-06-21 20:46:56 +00:00
|
|
|
/*
|
|
|
|
obj_file.c
|
|
|
|
|
|
|
|
qfcc object file support
|
|
|
|
|
2002-07-05 20:02:10 +00:00
|
|
|
Copyright (C) 2002 Bill Currie <bill@taniwha.org>
|
2002-06-21 20:46:56 +00:00
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2002/6/21
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
*/
|
|
|
|
static const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "QF/dstring.h"
|
|
|
|
#include "QF/qendian.h"
|
|
|
|
#include "QF/vfile.h"
|
|
|
|
|
2002-06-24 22:36:04 +00:00
|
|
|
#include "debug.h"
|
2002-06-21 20:46:56 +00:00
|
|
|
#include "def.h"
|
2002-07-08 20:31:59 +00:00
|
|
|
#include "emit.h"
|
2002-06-21 20:46:56 +00:00
|
|
|
#include "function.h"
|
|
|
|
#include "immediate.h"
|
|
|
|
#include "obj_file.h"
|
|
|
|
#include "qfcc.h"
|
2002-07-05 20:02:10 +00:00
|
|
|
#include "reloc.h"
|
|
|
|
#include "strpool.h"
|
2002-06-21 20:46:56 +00:00
|
|
|
#include "type.h"
|
|
|
|
|
|
|
|
static qfo_def_t *defs;
|
|
|
|
static int num_defs;
|
2002-07-15 21:37:52 +00:00
|
|
|
static qfo_func_t *funcs;
|
|
|
|
static int num_funcs;
|
2002-06-22 05:10:34 +00:00
|
|
|
static qfo_reloc_t *relocs;
|
|
|
|
static int num_relocs;
|
2002-07-08 05:26:50 +00:00
|
|
|
static strpool_t *types;
|
2002-06-21 20:46:56 +00:00
|
|
|
|
|
|
|
static int
|
2002-06-22 05:10:34 +00:00
|
|
|
count_relocs (reloc_t *reloc)
|
2002-06-21 20:46:56 +00:00
|
|
|
{
|
|
|
|
int num = 0;
|
2002-06-22 05:10:34 +00:00
|
|
|
while (reloc) {
|
2002-06-21 20:46:56 +00:00
|
|
|
num++;
|
2002-06-22 05:10:34 +00:00
|
|
|
reloc = reloc->next;
|
2002-06-21 20:46:56 +00:00
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
allocate_stuff (void)
|
|
|
|
{
|
|
|
|
def_t *def;
|
|
|
|
function_t *func;
|
|
|
|
|
|
|
|
num_defs = pr.scope->num_defs;
|
2002-07-15 21:37:52 +00:00
|
|
|
num_funcs = pr.num_functions - 1;
|
2002-06-22 05:10:34 +00:00
|
|
|
num_relocs = 0;
|
2002-06-21 20:46:56 +00:00
|
|
|
for (def = pr.scope->head; def; def = def->def_next) {
|
2002-06-22 05:10:34 +00:00
|
|
|
num_relocs += count_relocs (def->refs);
|
2002-06-21 20:46:56 +00:00
|
|
|
}
|
|
|
|
for (func = pr.func_head; func; func = func->next) {
|
2002-06-22 05:10:34 +00:00
|
|
|
num_relocs += count_relocs (func->refs);
|
2002-06-28 16:38:05 +00:00
|
|
|
if (func->scope) {
|
|
|
|
num_defs += func->scope->num_defs;
|
|
|
|
for (def = func->scope->head; def; def = def->def_next) {
|
|
|
|
num_relocs += count_relocs (def->refs);
|
|
|
|
}
|
2002-06-21 20:46:56 +00:00
|
|
|
}
|
|
|
|
}
|
2002-07-11 22:38:23 +00:00
|
|
|
num_relocs += count_relocs (pr.relocs);
|
2002-06-21 20:46:56 +00:00
|
|
|
defs = calloc (num_defs, sizeof (qfo_def_t));
|
2002-07-15 21:37:52 +00:00
|
|
|
funcs = calloc (num_funcs, sizeof (qfo_func_t));
|
2002-06-22 05:10:34 +00:00
|
|
|
relocs = calloc (num_relocs, sizeof (qfo_reloc_t));
|
2002-06-21 20:46:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static string_t
|
|
|
|
type_encoding (type_t *type)
|
|
|
|
{
|
|
|
|
static dstring_t *encoding;
|
|
|
|
|
|
|
|
if (!encoding)
|
|
|
|
encoding = dstring_newstr ();
|
|
|
|
else
|
|
|
|
dstring_clearstr (encoding);
|
|
|
|
encode_type (encoding, type);
|
2002-07-08 05:26:50 +00:00
|
|
|
return strpool_addstr (types, encoding->str);
|
2002-06-21 20:46:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
flags (def_t *d)
|
|
|
|
{
|
|
|
|
int flags = 0;
|
|
|
|
if (d->initialized)
|
|
|
|
flags |= QFOD_INITIALIZED;
|
|
|
|
if (d->constant)
|
|
|
|
flags |= QFOD_CONSTANT;
|
|
|
|
if (d->absolute)
|
|
|
|
flags |= QFOD_ABSOLUTE;
|
2002-06-28 17:59:32 +00:00
|
|
|
if (d->global)
|
|
|
|
flags |= QFOD_GLOBAL;
|
|
|
|
if (d->external)
|
|
|
|
flags |= QFOD_EXTERNAL;
|
2002-07-12 06:17:24 +00:00
|
|
|
if (d->local)
|
|
|
|
flags |= QFOD_LOCAL;
|
2002-06-21 20:46:56 +00:00
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2002-07-23 05:44:45 +00:00
|
|
|
static void
|
|
|
|
write_one_reloc (reloc_t *r, qfo_reloc_t **reloc, int def)
|
|
|
|
{
|
|
|
|
(*reloc)->ofs = LittleLong (r->ofs);
|
|
|
|
(*reloc)->type = LittleLong (r->type);
|
|
|
|
(*reloc)->def = LittleLong (def);
|
|
|
|
(*reloc)++;
|
|
|
|
}
|
|
|
|
|
2002-06-21 20:46:56 +00:00
|
|
|
static void
|
2002-07-12 06:17:24 +00:00
|
|
|
write_relocs (reloc_t *r, qfo_reloc_t **reloc, int def)
|
2002-06-21 20:46:56 +00:00
|
|
|
{
|
|
|
|
while (r) {
|
2002-07-23 05:44:45 +00:00
|
|
|
write_one_reloc (r, reloc, def);
|
2002-06-21 20:46:56 +00:00
|
|
|
r = r->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-06-22 05:10:34 +00:00
|
|
|
write_def (def_t *d, qfo_def_t *def, qfo_reloc_t **reloc)
|
2002-06-21 20:46:56 +00:00
|
|
|
{
|
|
|
|
d->obj_def = def - defs;
|
|
|
|
def->basic_type = LittleLong (d->type->type);
|
|
|
|
def->full_type = LittleLong (type_encoding (d->type));
|
|
|
|
def->name = LittleLong (ReuseString (d->name));
|
|
|
|
def->ofs = LittleLong (d->ofs);
|
2002-06-22 05:10:34 +00:00
|
|
|
def->relocs = LittleLong (*reloc - relocs);
|
|
|
|
def->num_relocs = LittleLong (count_relocs (d->refs));
|
2002-06-21 20:46:56 +00:00
|
|
|
def->flags = LittleLong (flags (d));
|
|
|
|
def->file = LittleLong (d->file);
|
|
|
|
def->line = LittleLong (d->line);
|
2002-07-12 06:17:24 +00:00
|
|
|
write_relocs (d->refs, reloc, d->obj_def);
|
2002-06-21 20:46:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
setup_data (void)
|
|
|
|
{
|
|
|
|
qfo_def_t *def = defs;
|
|
|
|
def_t *d;
|
2002-07-15 21:37:52 +00:00
|
|
|
qfo_func_t *func = funcs;
|
2002-06-24 21:41:05 +00:00
|
|
|
function_t *f;
|
|
|
|
qfo_reloc_t *reloc = relocs;
|
2002-07-23 05:44:45 +00:00
|
|
|
reloc_t *r;
|
2002-06-24 21:41:05 +00:00
|
|
|
dstatement_t *st;
|
|
|
|
pr_type_t *var;
|
2002-06-24 22:36:04 +00:00
|
|
|
pr_lineno_t *line;
|
2002-06-21 20:46:56 +00:00
|
|
|
|
2002-07-11 20:17:26 +00:00
|
|
|
for (d = pr.scope->head; d; d = d->def_next, def++)
|
|
|
|
write_def (d, def, &reloc);
|
|
|
|
for (f = pr.func_head; f; f = f->next, func++) {
|
2002-06-27 22:48:28 +00:00
|
|
|
func->name = LittleLong (f->s_name);
|
|
|
|
func->file = LittleLong (f->s_file);
|
2002-06-21 20:46:56 +00:00
|
|
|
func->line = LittleLong (f->def->line);
|
|
|
|
func->builtin = LittleLong (f->builtin);
|
|
|
|
func->code = LittleLong (f->code);
|
|
|
|
if (f->def->obj_def)
|
|
|
|
func->def = LittleLong (f->def->obj_def);
|
|
|
|
else {
|
|
|
|
func->def = LittleLong (def - defs);
|
2002-06-22 05:10:34 +00:00
|
|
|
write_def (f->def, def++, &reloc);
|
2002-06-21 20:46:56 +00:00
|
|
|
}
|
2002-06-28 16:38:05 +00:00
|
|
|
if (f->scope) {
|
|
|
|
func->locals_size = LittleLong (f->scope->space->size);
|
|
|
|
func->local_defs = LittleLong (def - defs);
|
|
|
|
func->num_local_defs = LittleLong (f->scope->num_defs);
|
|
|
|
}
|
2002-06-24 22:36:04 +00:00
|
|
|
if (f->aux)
|
|
|
|
func->line_info = LittleLong (f->aux->line_info);
|
2002-06-27 22:48:28 +00:00
|
|
|
func->num_parms = LittleLong (function_parms (f, func->parm_size));
|
2002-06-24 22:36:04 +00:00
|
|
|
func->relocs = LittleLong (reloc - relocs);
|
2002-07-02 23:08:44 +00:00
|
|
|
func->num_relocs = LittleLong (count_relocs (f->refs));
|
2002-07-15 21:37:52 +00:00
|
|
|
write_relocs (f->refs, &reloc, func - funcs);
|
2002-06-21 20:46:56 +00:00
|
|
|
|
2002-06-28 16:38:05 +00:00
|
|
|
if (f->scope)
|
|
|
|
for (d = f->scope->head; d; d = d->def_next)
|
|
|
|
write_def (d, def++, &reloc);
|
2002-06-21 20:46:56 +00:00
|
|
|
}
|
2002-07-23 05:44:45 +00:00
|
|
|
for (r = pr.relocs; r; r = r->next)
|
|
|
|
if (r->type == rel_def_op)
|
|
|
|
write_one_reloc (r, &reloc, G_INT (r->ofs));
|
|
|
|
else
|
|
|
|
write_one_reloc (r, &reloc, 0);
|
2002-07-08 20:31:59 +00:00
|
|
|
for (st = pr.code->code; st - pr.code->code < pr.code->size; st++) {
|
2002-06-24 21:41:05 +00:00
|
|
|
st->op = LittleLong (st->op);
|
|
|
|
st->a = LittleLong (st->a);
|
|
|
|
st->b = LittleLong (st->b);
|
|
|
|
st->c = LittleLong (st->c);
|
|
|
|
}
|
|
|
|
for (var = pr.near_data->data;
|
|
|
|
var - pr.near_data->data < pr.near_data->size; var++)
|
|
|
|
var->integer_var = LittleLong (var->integer_var);
|
|
|
|
if (pr.far_data)
|
|
|
|
for (var = pr.far_data->data;
|
|
|
|
var - pr.far_data->data < pr.far_data->size; var++)
|
|
|
|
var->integer_var = LittleLong (var->integer_var);
|
2002-07-17 14:21:52 +00:00
|
|
|
for (line = pr.linenos; line - pr.linenos < pr.num_linenos; line++) {
|
2002-06-24 22:36:04 +00:00
|
|
|
line->fa.addr = LittleLong (line->fa.addr);
|
|
|
|
line->line = LittleLong (line->line);
|
|
|
|
}
|
2002-06-21 20:46:56 +00:00
|
|
|
}
|
|
|
|
|
2002-07-16 06:40:34 +00:00
|
|
|
static void
|
|
|
|
round_strings (strpool_t *strings)
|
|
|
|
{
|
|
|
|
memset (strings->strings + strings->size, 0,
|
|
|
|
strings->max_size - strings->size);
|
|
|
|
strings->size = RUP (strings->size, 4);
|
|
|
|
}
|
|
|
|
|
2002-07-11 20:17:26 +00:00
|
|
|
qfo_t *
|
|
|
|
qfo_from_progs (pr_info_t *pr)
|
2002-06-21 20:46:56 +00:00
|
|
|
{
|
2002-07-11 20:17:26 +00:00
|
|
|
qfo_t *qfo;
|
2002-06-21 20:46:56 +00:00
|
|
|
|
2002-07-08 05:26:50 +00:00
|
|
|
types = strpool_new ();
|
2002-06-21 20:46:56 +00:00
|
|
|
allocate_stuff ();
|
|
|
|
setup_data ();
|
|
|
|
|
2002-07-16 06:40:34 +00:00
|
|
|
round_strings (pr->strings);
|
|
|
|
round_strings (types);
|
2002-07-11 20:17:26 +00:00
|
|
|
|
|
|
|
qfo = qfo_new ();
|
|
|
|
qfo_add_code (qfo, pr->code->code, pr->code->size);
|
|
|
|
qfo_add_data (qfo, pr->near_data->data, pr->near_data->size);
|
|
|
|
if (pr->far_data)
|
|
|
|
qfo_add_far_data (qfo, pr->far_data->data, pr->far_data->size);
|
|
|
|
qfo_add_strings (qfo, pr->strings->strings, pr->strings->size);
|
|
|
|
qfo_add_relocs (qfo, relocs, num_relocs);
|
|
|
|
qfo_add_defs (qfo, defs, num_defs);
|
2002-07-15 21:37:52 +00:00
|
|
|
qfo_add_funcs (qfo, funcs, num_funcs);
|
2002-07-17 14:21:52 +00:00
|
|
|
qfo_add_lines (qfo, pr->linenos, pr->num_linenos);
|
2002-07-11 20:17:26 +00:00
|
|
|
qfo_add_types (qfo, types->strings, types->size);
|
2002-07-16 21:59:47 +00:00
|
|
|
qfo->entity_fields = pr->entity_data->size;
|
2002-06-21 20:46:56 +00:00
|
|
|
|
2002-07-11 20:17:26 +00:00
|
|
|
free (defs);
|
|
|
|
free (relocs);
|
2002-07-15 21:37:52 +00:00
|
|
|
free (funcs);
|
2002-07-11 20:17:26 +00:00
|
|
|
strpool_delete (types);
|
|
|
|
return qfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
qfo_write (qfo_t *qfo, const char *filename)
|
|
|
|
{
|
|
|
|
qfo_header_t hdr;
|
|
|
|
VFile *file;
|
|
|
|
|
|
|
|
file = Qopen (filename, "wbz9");
|
2002-06-21 20:46:56 +00:00
|
|
|
|
|
|
|
memset (&hdr, 0, sizeof (hdr));
|
|
|
|
|
|
|
|
memcpy (hdr.qfo, QFO, sizeof (hdr.qfo));
|
|
|
|
hdr.version = LittleLong (QFO_VERSION);
|
2002-07-11 20:17:26 +00:00
|
|
|
hdr.code_size = LittleLong (qfo->code_size);
|
|
|
|
hdr.data_size = LittleLong (qfo->data_size);
|
|
|
|
hdr.far_data_size = LittleLong (qfo->far_data_size);
|
|
|
|
hdr.strings_size = LittleLong (qfo->strings_size);
|
|
|
|
hdr.num_relocs = LittleLong (qfo->num_relocs);
|
|
|
|
hdr.num_defs = LittleLong (qfo->num_defs);
|
2002-07-15 21:37:52 +00:00
|
|
|
hdr.num_funcs = LittleLong (qfo->num_funcs);
|
2002-07-11 20:17:26 +00:00
|
|
|
hdr.num_lines = LittleLong (qfo->num_lines);
|
|
|
|
hdr.types_size = LittleLong (qfo->types_size);
|
2002-07-16 21:59:47 +00:00
|
|
|
hdr.entity_fields = LittleLong (qfo->entity_fields);
|
2002-06-21 20:46:56 +00:00
|
|
|
|
|
|
|
Qwrite (file, &hdr, sizeof (hdr));
|
2002-07-11 20:17:26 +00:00
|
|
|
if (qfo->code_size)
|
|
|
|
Qwrite (file, qfo->code, qfo->code_size * sizeof (dstatement_t));
|
|
|
|
if (qfo->data_size)
|
|
|
|
Qwrite (file, qfo->data, qfo->data_size * sizeof (pr_type_t));
|
|
|
|
if (qfo->far_data_size)
|
|
|
|
Qwrite (file, qfo->far_data, qfo->far_data_size * sizeof (pr_type_t));
|
|
|
|
if (qfo->strings_size)
|
|
|
|
Qwrite (file, qfo->strings, qfo->strings_size);
|
|
|
|
if (qfo->num_relocs)
|
|
|
|
Qwrite (file, qfo->relocs, qfo->num_relocs * sizeof (qfo_reloc_t));
|
|
|
|
if (qfo->num_defs)
|
|
|
|
Qwrite (file, qfo->defs, qfo->num_defs * sizeof (qfo_def_t));
|
2002-07-15 21:37:52 +00:00
|
|
|
if (qfo->num_funcs)
|
|
|
|
Qwrite (file, qfo->funcs, qfo->num_funcs * sizeof (qfo_func_t));
|
2002-07-11 20:17:26 +00:00
|
|
|
if (qfo->num_lines)
|
|
|
|
Qwrite (file, qfo->lines, qfo->num_lines * sizeof (pr_lineno_t));
|
|
|
|
if (qfo->types_size)
|
|
|
|
Qwrite (file, qfo->types, qfo->types_size);
|
2002-06-22 05:10:34 +00:00
|
|
|
|
|
|
|
Qclose (file);
|
2002-06-26 16:27:23 +00:00
|
|
|
|
2002-06-22 05:10:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
qfo_t *
|
2002-07-21 07:12:17 +00:00
|
|
|
qfo_read (VFile *file)
|
2002-06-22 05:10:34 +00:00
|
|
|
{
|
|
|
|
qfo_header_t hdr;
|
|
|
|
qfo_t *qfo;
|
2002-06-24 22:36:04 +00:00
|
|
|
qfo_def_t *def;
|
2002-07-15 21:37:52 +00:00
|
|
|
qfo_func_t *func;
|
2002-06-24 22:36:04 +00:00
|
|
|
qfo_reloc_t *reloc;
|
|
|
|
dstatement_t *st;
|
|
|
|
pr_type_t *var;
|
|
|
|
pr_lineno_t *line;
|
2002-06-22 05:10:34 +00:00
|
|
|
|
|
|
|
Qread (file, &hdr, sizeof (hdr));
|
|
|
|
|
|
|
|
if (strcmp (hdr.qfo, QFO)) {
|
|
|
|
fprintf (stderr, "not a qfo file\n");
|
|
|
|
Qclose (file);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
qfo = calloc (1, sizeof (qfo_t));
|
|
|
|
|
|
|
|
hdr.version = LittleLong (hdr.version);
|
|
|
|
qfo->code_size = LittleLong (hdr.code_size);
|
|
|
|
qfo->data_size = LittleLong (hdr.data_size);
|
|
|
|
qfo->far_data_size = LittleLong (hdr.far_data_size);
|
|
|
|
qfo->strings_size = LittleLong (hdr.strings_size);
|
|
|
|
qfo->num_relocs = LittleLong (hdr.num_relocs);
|
|
|
|
qfo->num_defs = LittleLong (hdr.num_defs);
|
2002-07-15 21:37:52 +00:00
|
|
|
qfo->num_funcs = LittleLong (hdr.num_funcs);
|
2002-06-24 22:36:04 +00:00
|
|
|
qfo->num_lines = LittleLong (hdr.num_lines);
|
2002-07-08 05:26:50 +00:00
|
|
|
qfo->types_size = LittleLong (hdr.types_size);
|
2002-07-16 21:59:47 +00:00
|
|
|
qfo->entity_fields = LittleLong (hdr.entity_fields);
|
2002-06-22 05:10:34 +00:00
|
|
|
|
|
|
|
if (hdr.version != QFO_VERSION) {
|
|
|
|
fprintf (stderr, "can't read version %x.%03x.%03x\n",
|
|
|
|
(hdr.version >> 24) & 0xff, (hdr.version >> 12) & 0xfff,
|
|
|
|
hdr.version & 0xfff);
|
|
|
|
Qclose (file);
|
|
|
|
free (qfo);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
qfo->code = malloc (qfo->code_size * sizeof (dstatement_t));
|
|
|
|
qfo->data = malloc (qfo->data_size * sizeof (pr_type_t));
|
2002-06-24 22:36:04 +00:00
|
|
|
if (qfo->far_data_size)
|
|
|
|
qfo->far_data = malloc (qfo->far_data_size * sizeof (pr_type_t));
|
2002-06-22 05:10:34 +00:00
|
|
|
qfo->strings = malloc (qfo->strings_size);
|
|
|
|
qfo->relocs = malloc (qfo->num_relocs * sizeof (qfo_reloc_t));
|
|
|
|
qfo->defs = malloc (qfo->num_defs * sizeof (qfo_def_t));
|
2002-07-15 21:37:52 +00:00
|
|
|
qfo->funcs = malloc (qfo->num_funcs * sizeof (qfo_func_t));
|
2002-07-03 20:16:32 +00:00
|
|
|
qfo->lines = malloc (qfo->num_lines * sizeof (pr_lineno_t));
|
2002-07-08 05:26:50 +00:00
|
|
|
qfo->types = malloc (qfo->types_size);
|
2002-06-22 05:10:34 +00:00
|
|
|
|
|
|
|
Qread (file, qfo->code, qfo->code_size * sizeof (dstatement_t));
|
|
|
|
Qread (file, qfo->data, qfo->data_size * sizeof (pr_type_t));
|
2002-06-24 22:36:04 +00:00
|
|
|
if (qfo->far_data_size)
|
|
|
|
Qread (file, qfo->far_data, qfo->far_data_size * sizeof (pr_type_t));
|
2002-06-22 05:10:34 +00:00
|
|
|
Qread (file, qfo->strings, qfo->strings_size);
|
|
|
|
Qread (file, qfo->relocs, qfo->num_relocs * sizeof (qfo_reloc_t));
|
|
|
|
Qread (file, qfo->defs, qfo->num_defs * sizeof (qfo_def_t));
|
2002-07-15 21:37:52 +00:00
|
|
|
Qread (file, qfo->funcs, qfo->num_funcs * sizeof (qfo_func_t));
|
2002-06-24 22:36:04 +00:00
|
|
|
if (qfo->num_lines)
|
|
|
|
Qread (file, qfo->lines, qfo->num_lines * sizeof (pr_lineno_t));
|
2002-07-08 05:26:50 +00:00
|
|
|
Qread (file, qfo->types, qfo->types_size);
|
2002-06-22 05:10:34 +00:00
|
|
|
|
2002-06-24 22:36:04 +00:00
|
|
|
for (st = qfo->code; st - qfo->code < qfo->code_size; st++) {
|
|
|
|
st->op = LittleLong (st->op);
|
|
|
|
st->a = LittleLong (st->a);
|
|
|
|
st->b = LittleLong (st->b);
|
|
|
|
st->c = LittleLong (st->c);
|
|
|
|
}
|
|
|
|
for (var = qfo->data;
|
|
|
|
var - qfo->data < qfo->data_size; var++)
|
|
|
|
var->integer_var = LittleLong (var->integer_var);
|
|
|
|
if (qfo->far_data)
|
|
|
|
for (var = qfo->far_data;
|
|
|
|
var - qfo->far_data < qfo->far_data_size; var++)
|
|
|
|
var->integer_var = LittleLong (var->integer_var);
|
|
|
|
for (reloc = qfo->relocs; reloc - qfo->relocs < qfo->num_relocs; reloc++) {
|
|
|
|
reloc->ofs = LittleLong (reloc->ofs);
|
|
|
|
reloc->type = LittleLong (reloc->type);
|
|
|
|
}
|
|
|
|
for (def = qfo->defs; def - qfo->defs < qfo->num_defs; def++) {
|
|
|
|
def->basic_type = LittleLong (def->basic_type);
|
|
|
|
def->full_type = LittleLong (def->full_type);
|
|
|
|
def->name = LittleLong (def->name);
|
|
|
|
def->ofs = LittleLong (def->ofs);
|
|
|
|
def->relocs = LittleLong (def->relocs);
|
|
|
|
def->num_relocs = LittleLong (def->num_relocs);
|
|
|
|
def->flags = LittleLong (def->flags);
|
|
|
|
def->file = LittleLong (def->file);
|
|
|
|
def->line = LittleLong (def->line);
|
|
|
|
}
|
2002-07-15 21:37:52 +00:00
|
|
|
for (func = qfo->funcs;
|
|
|
|
func - qfo->funcs < qfo->num_funcs; func++) {
|
2002-06-24 22:36:04 +00:00
|
|
|
func->name = LittleLong (func->name);
|
|
|
|
func->file = LittleLong (func->file);
|
|
|
|
func->line = LittleLong (func->line);
|
|
|
|
func->builtin = LittleLong (func->builtin);
|
|
|
|
func->code = LittleLong (func->code);
|
|
|
|
func->def = LittleLong (func->def);
|
|
|
|
func->locals_size = LittleLong (func->locals_size);
|
|
|
|
func->local_defs = LittleLong (func->local_defs);
|
|
|
|
func->num_local_defs = LittleLong (func->num_local_defs);
|
|
|
|
func->line_info = LittleLong (func->line_info);
|
|
|
|
func->num_parms = LittleLong (func->num_parms);
|
|
|
|
func->relocs = LittleLong (func->relocs);
|
|
|
|
func->num_relocs = LittleLong (func->num_relocs);
|
|
|
|
}
|
|
|
|
for (line = qfo->lines; line - qfo->lines < qfo->num_lines; line++) {
|
|
|
|
line->fa.addr = LittleLong (line->fa.addr);
|
|
|
|
line->line = LittleLong (line->line);
|
|
|
|
}
|
|
|
|
|
2002-07-03 20:16:32 +00:00
|
|
|
return qfo;
|
2002-06-21 20:46:56 +00:00
|
|
|
}
|
2002-07-05 20:02:10 +00:00
|
|
|
|
2002-07-21 07:12:17 +00:00
|
|
|
qfo_t *qfo_open (const char *filename)
|
|
|
|
{
|
|
|
|
qfo_t *qfo;
|
|
|
|
VFile *file;
|
|
|
|
|
|
|
|
file = Qopen (filename, "rbz");
|
|
|
|
if (!file) {
|
|
|
|
perror (filename);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
qfo = qfo_read (file);
|
|
|
|
Qclose (file);
|
|
|
|
return qfo;
|
|
|
|
}
|
|
|
|
|
2002-07-05 20:02:10 +00:00
|
|
|
defspace_t *
|
|
|
|
init_space (int size, pr_type_t *data)
|
|
|
|
{
|
|
|
|
defspace_t *space = new_defspace ();
|
2002-07-11 22:38:23 +00:00
|
|
|
space->size = size;
|
|
|
|
space->max_size = RUP (space->size, 65536);
|
2002-07-05 20:02:10 +00:00
|
|
|
if (size && data) {
|
2002-07-11 22:38:23 +00:00
|
|
|
space->data = malloc (space->max_size * sizeof (pr_type_t));
|
2002-07-05 20:02:10 +00:00
|
|
|
memcpy (space->data, data, size * sizeof (pr_type_t));
|
|
|
|
}
|
|
|
|
return space;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
qfo_to_progs (qfo_t *qfo, pr_info_t *pr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
function_t *pf;
|
2002-07-15 21:37:52 +00:00
|
|
|
qfo_func_t *qf;
|
2002-07-05 20:02:10 +00:00
|
|
|
def_t *pd;
|
|
|
|
qfo_def_t *qd;
|
|
|
|
reloc_t *relocs;
|
|
|
|
|
|
|
|
relocs = calloc (qfo->num_relocs, sizeof (reloc_t));
|
|
|
|
for (i = 0; i < qfo->num_relocs; i++) {
|
|
|
|
if (i + 1 < qfo->num_relocs)
|
|
|
|
relocs[i].next = &relocs[i + 1];
|
|
|
|
relocs[i].ofs = qfo->relocs[i].ofs;
|
|
|
|
relocs[i].type = qfo->relocs[i].type;
|
|
|
|
}
|
|
|
|
|
|
|
|
pr->strings = strpool_build (qfo->strings, qfo->strings_size);
|
|
|
|
|
2002-07-08 20:31:59 +00:00
|
|
|
pr->code = codespace_new ();
|
|
|
|
codespace_addcode (pr->code, qfo->code, qfo->code_size);
|
2002-07-05 20:02:10 +00:00
|
|
|
|
|
|
|
pr->near_data = init_space (qfo->data_size, qfo->data);
|
|
|
|
pr->far_data = init_space (qfo->far_data_size, qfo->far_data);
|
2002-07-11 22:38:23 +00:00
|
|
|
pr->entity_data = new_defspace ();
|
2002-07-05 20:02:10 +00:00
|
|
|
pr->scope = new_scope (sc_global, pr->near_data, 0);
|
|
|
|
pr->scope->num_defs = qfo->num_defs;
|
|
|
|
pr->scope->head = calloc (pr->scope->num_defs, sizeof (def_t));
|
|
|
|
for (i = 0, pd = pr->scope->head, qd = qfo->defs;
|
2002-07-12 20:43:11 +00:00
|
|
|
i < pr->scope->num_defs; i++, pd++, qd++) {
|
2002-07-05 20:02:10 +00:00
|
|
|
*pr->scope->tail = pd;
|
|
|
|
pr->scope->tail = &pd->def_next;
|
2002-07-08 05:26:50 +00:00
|
|
|
pd->type = parse_type (qfo->types + qd->full_type);
|
2002-07-16 06:40:34 +00:00
|
|
|
pd->name = qd->name ? qfo->strings + qd->name : 0;
|
2002-07-05 20:02:10 +00:00
|
|
|
pd->ofs = qd->ofs;
|
|
|
|
if (qd->num_relocs) {
|
|
|
|
pd->refs = relocs + qd->relocs;
|
|
|
|
pd->refs[qd->num_relocs - 1].next = 0;
|
|
|
|
}
|
|
|
|
pd->initialized = (qd->flags & QFOD_INITIALIZED) != 0;
|
|
|
|
pd->constant = (qd->flags & QFOD_CONSTANT) != 0;
|
|
|
|
pd->absolute = (qd->flags & QFOD_ABSOLUTE) != 0;
|
|
|
|
pd->global = (qd->flags & QFOD_GLOBAL) != 0;
|
|
|
|
pd->external = (qd->flags & QFOD_EXTERNAL) != 0;
|
2002-07-12 06:17:24 +00:00
|
|
|
pd->local = (qd->flags & QFOD_LOCAL) != 0;
|
2002-07-05 20:02:10 +00:00
|
|
|
pd->file = qd->file;
|
|
|
|
pd->line = qd->line;
|
|
|
|
}
|
2002-07-17 14:21:52 +00:00
|
|
|
pr->scope->tail = &pr->scope->head;
|
|
|
|
for (i = 0, pd = pr->scope->head; i < pr->scope->num_defs; i++, pd++) {
|
|
|
|
if ((*pr->scope->tail)->local) {
|
|
|
|
*pr->scope->tail = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pr->scope->tail = &(*pr->scope->tail)->def_next;
|
|
|
|
}
|
2002-07-05 20:02:10 +00:00
|
|
|
|
2002-07-15 21:37:52 +00:00
|
|
|
pr->num_functions = qfo->num_funcs + 1;
|
|
|
|
pr->func_head = calloc (qfo->num_funcs, sizeof (function_t));
|
2002-07-05 20:02:10 +00:00
|
|
|
pr->func_tail = &pr->func_head;
|
2002-07-15 21:37:52 +00:00
|
|
|
for (i = 0, pf = pr->func_head, qf = qfo->funcs;
|
|
|
|
i < qfo->num_funcs; i++, pf++, qf++) {
|
2002-07-05 20:02:10 +00:00
|
|
|
*pr->func_tail = pf;
|
|
|
|
pr->func_tail = &pf->next;
|
2002-07-08 16:13:06 +00:00
|
|
|
pf->aux = new_auxfunction ();
|
2002-07-17 14:21:52 +00:00
|
|
|
pf->aux->function = i + 1;
|
2002-07-08 16:13:06 +00:00
|
|
|
pf->aux->source_line = qf->line;
|
|
|
|
pf->aux->line_info = qf->line_info;
|
2002-07-17 15:40:08 +00:00
|
|
|
pf->aux->local_defs = 0;
|
|
|
|
pf->aux->num_locals = 0;
|
2002-07-05 20:02:10 +00:00
|
|
|
pf->builtin = qf->builtin;
|
|
|
|
pf->code = qf->code;
|
2002-07-17 14:21:52 +00:00
|
|
|
pf->function_num = i + 1;
|
2002-07-05 20:02:10 +00:00
|
|
|
pf->s_file = qf->file;
|
2002-07-11 22:38:23 +00:00
|
|
|
pf->s_name = qf->name;
|
2002-07-05 20:02:10 +00:00
|
|
|
pf->file_line = qf->line;
|
2002-07-11 22:38:23 +00:00
|
|
|
pf->def = pr->scope->head + qf->def;
|
2002-07-05 20:02:10 +00:00
|
|
|
pf->scope = new_scope (sc_params, init_space (qf->locals_size, 0),
|
|
|
|
pr->scope);
|
|
|
|
if (qf->num_local_defs) {
|
|
|
|
pf->scope->head = pr->scope->head + qf->local_defs;
|
|
|
|
pf->scope->tail = &pf->scope->head[qf->num_local_defs - 1].def_next;
|
2002-07-11 22:38:23 +00:00
|
|
|
*pf->scope->tail = 0;
|
2002-07-17 15:40:08 +00:00
|
|
|
pf->aux->local_defs = pr->num_locals;
|
|
|
|
for (pd = pf->scope->head; pd; pd = pd->def_next) {
|
|
|
|
if (pd->name) {
|
|
|
|
def_to_ddef (pd, new_local (), 0);
|
|
|
|
pf->aux->num_locals++;
|
|
|
|
}
|
|
|
|
}
|
2002-07-05 20:02:10 +00:00
|
|
|
}
|
|
|
|
if (qf->num_relocs) {
|
|
|
|
pf->refs = relocs + qf->relocs;
|
|
|
|
pf->refs[qf->num_relocs - 1].next = 0;
|
|
|
|
}
|
|
|
|
}
|
2002-07-17 14:21:52 +00:00
|
|
|
pr->num_linenos = pr->linenos_size = qfo->num_lines;
|
|
|
|
if (pr->num_linenos) {
|
|
|
|
pr->linenos = malloc (pr->num_linenos * sizeof (pr_lineno_t));
|
|
|
|
memcpy (pr->linenos, qfo->lines,
|
|
|
|
pr->num_linenos * sizeof (pr_lineno_t));
|
|
|
|
}
|
2002-07-16 21:59:47 +00:00
|
|
|
pr->entity_data = init_space (qfo->entity_fields, 0);
|
2002-07-05 20:02:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-07-11 17:06:15 +00:00
|
|
|
|
|
|
|
qfo_t *
|
|
|
|
qfo_new (void)
|
|
|
|
{
|
|
|
|
return calloc (1, sizeof (qfo_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
qfo_add_code (qfo_t *qfo, dstatement_t *code, int code_size)
|
|
|
|
{
|
2002-07-11 21:04:09 +00:00
|
|
|
if (!code_size)
|
|
|
|
return;
|
2002-07-11 17:06:15 +00:00
|
|
|
qfo->code = malloc (code_size * sizeof (dstatement_t));
|
2002-07-11 20:17:26 +00:00
|
|
|
qfo->code_size = code_size;
|
2002-07-11 17:06:15 +00:00
|
|
|
memcpy (qfo->code, code, code_size * sizeof (dstatement_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
qfo_add_data (qfo_t *qfo, pr_type_t *data, int data_size)
|
|
|
|
{
|
2002-07-11 21:04:09 +00:00
|
|
|
if (!data_size)
|
|
|
|
return;
|
2002-07-11 17:06:15 +00:00
|
|
|
qfo->data = malloc (data_size * sizeof (pr_type_t));
|
2002-07-11 20:17:26 +00:00
|
|
|
qfo->data_size = data_size;
|
2002-07-11 17:06:15 +00:00
|
|
|
memcpy (qfo->data, data, data_size * sizeof (pr_type_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
qfo_add_far_data (qfo_t *qfo, pr_type_t *far_data, int far_data_size)
|
|
|
|
{
|
2002-07-11 21:04:09 +00:00
|
|
|
if (!far_data_size)
|
|
|
|
return;
|
2002-07-11 17:06:15 +00:00
|
|
|
qfo->far_data = malloc (far_data_size * sizeof (pr_type_t));
|
2002-07-11 20:17:26 +00:00
|
|
|
qfo->far_data_size = far_data_size;
|
2002-07-11 17:06:15 +00:00
|
|
|
memcpy (qfo->far_data, far_data, far_data_size * sizeof (pr_type_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
qfo_add_strings (qfo_t *qfo, const char *strings, int strings_size)
|
|
|
|
{
|
2002-07-11 21:04:09 +00:00
|
|
|
if (!strings_size)
|
|
|
|
return;
|
2002-07-11 17:06:15 +00:00
|
|
|
qfo->strings = malloc (strings_size);
|
2002-07-11 20:17:26 +00:00
|
|
|
qfo->strings_size = strings_size;
|
2002-07-11 17:06:15 +00:00
|
|
|
memcpy (qfo->strings, strings, strings_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
qfo_add_relocs (qfo_t *qfo, qfo_reloc_t *relocs, int num_relocs)
|
|
|
|
{
|
2002-07-11 21:04:09 +00:00
|
|
|
if (!num_relocs)
|
|
|
|
return;
|
2002-07-11 17:06:15 +00:00
|
|
|
qfo->relocs = malloc (num_relocs * sizeof (qfo_reloc_t));
|
2002-07-11 20:17:26 +00:00
|
|
|
qfo->num_relocs = num_relocs;
|
2002-07-11 17:06:15 +00:00
|
|
|
memcpy (qfo->relocs, relocs, num_relocs * sizeof (qfo_reloc_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
qfo_add_defs (qfo_t *qfo, qfo_def_t *defs, int num_defs)
|
|
|
|
{
|
2002-07-11 21:04:09 +00:00
|
|
|
if (!num_defs)
|
|
|
|
return;
|
2002-07-11 17:06:15 +00:00
|
|
|
qfo->defs = malloc (num_defs * sizeof (qfo_def_t));
|
2002-07-11 20:17:26 +00:00
|
|
|
qfo->num_defs = num_defs;
|
2002-07-11 17:06:15 +00:00
|
|
|
memcpy (qfo->defs, defs, num_defs * sizeof (qfo_def_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-07-15 21:37:52 +00:00
|
|
|
qfo_add_funcs (qfo_t *qfo, qfo_func_t *funcs, int num_funcs)
|
2002-07-11 17:06:15 +00:00
|
|
|
{
|
2002-07-15 21:37:52 +00:00
|
|
|
if (!num_funcs)
|
2002-07-11 21:04:09 +00:00
|
|
|
return;
|
2002-07-15 21:37:52 +00:00
|
|
|
qfo->funcs = malloc (num_funcs * sizeof (qfo_func_t));
|
|
|
|
qfo->num_funcs = num_funcs;
|
|
|
|
memcpy (qfo->funcs, funcs, num_funcs * sizeof (qfo_func_t));
|
2002-07-11 17:06:15 +00:00
|
|
|
}
|
|
|
|
|
2002-07-11 20:17:26 +00:00
|
|
|
void
|
|
|
|
qfo_add_lines (qfo_t *qfo, pr_lineno_t *lines, int num_lines)
|
|
|
|
{
|
2002-07-11 21:04:09 +00:00
|
|
|
if (!num_lines)
|
|
|
|
return;
|
2002-07-11 20:17:26 +00:00
|
|
|
qfo->lines = malloc (num_lines * sizeof (pr_lineno_t));
|
|
|
|
qfo->num_lines = num_lines;
|
|
|
|
memcpy (qfo->lines, lines, num_lines * sizeof (pr_lineno_t));
|
|
|
|
}
|
|
|
|
|
2002-07-11 17:06:15 +00:00
|
|
|
void
|
|
|
|
qfo_add_types (qfo_t *qfo, const char *types, int types_size)
|
|
|
|
{
|
2002-07-11 21:04:09 +00:00
|
|
|
if (!types_size)
|
|
|
|
return;
|
2002-07-11 17:06:15 +00:00
|
|
|
qfo->types = malloc (types_size);
|
2002-07-11 20:17:26 +00:00
|
|
|
qfo->types_size = types_size;
|
2002-07-11 17:06:15 +00:00
|
|
|
memcpy (qfo->types, types, types_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
qfo_delete (qfo_t *qfo)
|
|
|
|
{
|
|
|
|
if (!qfo)
|
|
|
|
return;
|
|
|
|
if (qfo->code)
|
|
|
|
free (qfo->code);
|
|
|
|
if (qfo->data)
|
|
|
|
free (qfo->data);
|
|
|
|
if (qfo->far_data)
|
|
|
|
free (qfo->far_data);
|
|
|
|
if (qfo->strings)
|
|
|
|
free (qfo->strings);
|
|
|
|
if (qfo->relocs)
|
|
|
|
free (qfo->relocs);
|
|
|
|
if (qfo->defs)
|
|
|
|
free (qfo->defs);
|
2002-07-15 21:37:52 +00:00
|
|
|
if (qfo->funcs)
|
|
|
|
free (qfo->funcs);
|
2002-07-11 20:17:26 +00:00
|
|
|
if (qfo->lines)
|
|
|
|
free (qfo->lines);
|
2002-07-11 17:06:15 +00:00
|
|
|
if (qfo->types)
|
|
|
|
free (qfo->types);
|
|
|
|
free (qfo);
|
|
|
|
}
|