initial work on actually writing .qfo files. seems to work, but only test.qfo

is ever writtin :)
This commit is contained in:
Bill Currie 2002-06-21 20:46:56 +00:00
parent a83ecbd23a
commit 9fee1d07dd
18 changed files with 343 additions and 51 deletions

View file

@ -1,5 +1,5 @@
AUTOMAKE_OPTIONS= foreign
EXTRA_DIST= class.h cmdlib.h cpp.h debug.h def.h expr.h function.h idstuff.h \
immediate.h method.h opcodes.h options.h qfcc.h reloc.h struct.h \
switch.h type.h
immediate.h method.h obj_file.h opcodes.h options.h qfcc.h \
reloc.h struct.h switch.h type.h

View file

@ -63,6 +63,8 @@ typedef struct def_s {
struct defspace_s *space;
struct def_s *parent; // vector/quaternion member
int obj_def; // index to def in qfo defs
void *return_addr; // who allocated this
} def_t;

View file

@ -35,16 +35,17 @@
#include "QF/pr_comp.h"
typedef struct function_s {
struct function_s *next;
dfunction_t *dfunc;
pr_auxfunction_t *aux; // debug info;
int builtin; // if non 0, call an internal function
int code; // first statement
int function_num;
const char *file; // source file with definition
int file_line;
struct def_s *def;
struct function_s *next;
dfunction_t *dfunc;
pr_auxfunction_t *aux; // debug info;
int builtin; // if non 0, call an internal function
int code; // first statement
int function_num;
const char *file; // source file with definition
int file_line;
struct def_s *def;
struct scope_s *scope;
struct reloc_s *refs;
} function_t;
extern function_t *current_func;
@ -67,9 +68,9 @@ param_t *reverse_params (param_t *params);
struct type_s *parse_params (struct type_s *type, param_t *params);
void build_scope (function_t *f, struct def_s *func, param_t *params);
function_t *new_function (void);
void build_builtin_function (struct def_s *def, struct expr_s *bi_val);
function_t *build_builtin_function (struct def_s *def, struct expr_s *bi_val);
void build_function (function_t *f);
void finish_function (function_t *f);
void emit_function (function_t *f, expr_t *e);
void emit_function (function_t *f, struct expr_s *e);
#endif//__function_h

View file

@ -41,6 +41,7 @@ typedef struct method_s {
param_t *params;
struct type_s *type;
struct def_s *def;
struct function_s *func;
char *name;
char *types;
} method_t;

View file

@ -33,6 +33,8 @@
#ifndef __obj_file_h
#define __obj_file_h
#include "QF/pr_comp.h"
#define QFO "QFO"
#define QFO_VERSION 0x00001001 // MMmmmRRR 0.001.001 (hex)
@ -101,6 +103,16 @@ typedef struct qfo_function_s {
int num_parms;
byte parm_size[MAX_PARMS];
int refs;
int num_refs;
} qfo_function_t;
typedef struct qfo_ref_s {
int ofs;
int type;
} qfo_ref_t;
int write_obj_file (const char *filename);
#endif//__obj_file_h

View file

@ -63,6 +63,7 @@ typedef struct {
qboolean save_temps; // save temporary files
qboolean files_dat; // generate files.dat
qboolean traditional; // behave more like qcc
qboolean compile; // serparate compilation mode
int strip_path; // number of leading path elements to strip
// from source file names
} options_t;

View file

@ -86,6 +86,8 @@ extern int pr_error_count;
#define G_POINTER(t,o) ((t *)(pr.near_data->data + o))
#define G_STRUCT(t,o) (*G_POINTER (t, o))
#define POINTER_OFS(p) ((pr_type_t *) (p) - pr.near_data->data)
extern string_t s_file; // filename for function definition
const char *strip_path (const char *filename);

View file

@ -42,6 +42,7 @@ typedef enum {
rel_op_c_op,
rel_def_op,
rel_def_def,
rel_def_func,
} reloc_type;
typedef struct reloc_s {

View file

@ -39,8 +39,8 @@ bin_PROGRAMS= qfcc
qfcc_SOURCES= \
class.c cmdlib.c cpp.c debug.c def.c emit.c expr.c function.c idstuff.c \
immediate.c method.c opcodes.c options.c qc-lex.l qc-parse.y qfcc.c \
reloc.c struct.c switch.c type.c
immediate.c method.c obj_file.c opcodes.c options.c qc-lex.l qc-parse.y \
qfcc.c reloc.c struct.c switch.c type.c
qfcc_LDADD= $(QFCC_LIBS)
qfcc_DEPENDENCIES= $(QFCC_DEPS)

View file

@ -53,6 +53,7 @@ static const char rcsid[] =
#include "expr.h"
#include "immediate.h"
#include "method.h"
#include "reloc.h"
#include "struct.h"
#include "type.h"
@ -476,12 +477,14 @@ class_finish_module (void)
exec_class_func = new_function ();
exec_class_func->builtin = 0;
exec_class_func->def = exec_class_def;
exec_class_func->refs = new_reloc (exec_class_def->ofs, rel_def_func);
build_function (exec_class_func);
finish_function (exec_class_func);
init_def = get_def (&type_function, ".ctor", pr.scope, 1);
init_func = new_function ();
init_func->def = init_def;
init_func->refs = new_reloc (init_def->ofs, rel_def_func);
init_func->code = pr.num_statements;
build_scope (init_func, init_def, 0);
build_function (init_func);

View file

@ -51,6 +51,7 @@ static const char rcsid[] =
#include "function.h"
#include "immediate.h"
#include "opcodes.h"
#include "reloc.h"
#include "type.h"
param_t *
@ -175,19 +176,19 @@ new_function (void)
return f;
}
void
function_t *
build_builtin_function (def_t *def, expr_t *bi_val)
{
function_t *f;
if (def->type->type != ev_func) {
error (bi_val, "%s is not a function", def->name);
return;
return 0;
}
if (bi_val->type != ex_integer && bi_val->type != ex_float) {
error (bi_val, "invalid constant for = #");
return;
return 0;
}
f = new_function ();
@ -195,8 +196,10 @@ build_builtin_function (def_t *def, expr_t *bi_val)
f->builtin = bi_val->type == ex_integer ? bi_val->e.integer_val
: (int)bi_val->e.float_val;
f->def = def;
f->refs = new_reloc (def->ofs, rel_def_func);
build_function (f);
finish_function (f);
return f;
}
void

View file

@ -134,7 +134,7 @@ ReuseString (const char *str)
{
int s;
if (!*str)
if (!str || !*str)
return 0;
if (!strings_tab)
return CopyString (str);

View file

@ -53,6 +53,7 @@ static const char rcsid[] =
#include "def.h"
#include "immediate.h"
#include "method.h"
#include "reloc.h"
#include "struct.h"
#include "type.h"
@ -303,6 +304,7 @@ emit_methods (methodlist_t *_methods, const char *name, int instance)
methods->method_next = 0;
methods->method_count = count;
for (i = 0, method = _methods->head; method; method = method->next) {
reloc_t *ref;
if (!method->instance != !instance || !method->def)
continue;
methods->method_list[i].method_name.sel_id = ReuseString (method->name);
@ -311,6 +313,12 @@ emit_methods (methodlist_t *_methods, const char *name, int instance)
methods->method_list[i].method_types =
methods->method_list[i].method_name.sel_types;
methods->method_list[i].method_imp = G_FUNCTION (method->def->ofs);
if (method->func) {
ref = new_reloc (POINTER_OFS (methods->method_list[i].method_imp),
rel_def_func);
ref->next = method->func->refs;
method->func->refs = ref;
}
i++;
}
return methods_def->ofs;

View file

@ -0,0 +1,243 @@
/*
obj_file.c
qfcc object file support
Copyright (C) 2001 Bill Currie <bill@taniwha.org>
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"
#include "def.h"
#include "function.h"
#include "immediate.h"
#include "obj_file.h"
#include "reloc.h"
#include "qfcc.h"
#include "type.h"
static qfo_def_t *defs;
static int num_defs;
static qfo_function_t *functions;
static int num_functions;
static qfo_ref_t *refs;
static int num_refs;
static int
count_refs (reloc_t *ref)
{
int num = 0;
while (ref) {
num++;
ref = ref->next;
}
return num;
}
static void
allocate_stuff (void)
{
def_t *def;
function_t *func;
num_defs = pr.scope->num_defs;
num_functions = pr.num_functions;
num_refs = 0;
for (def = pr.scope->head; def; def = def->def_next) {
num_refs += count_refs (def->refs);
}
for (func = pr.func_head; func; func = func->next) {
num_refs += count_refs (func->refs);
num_defs += func->scope->num_defs;
for (def = func->scope->head; def; def = def->def_next) {
num_refs += count_refs (def->refs);
}
}
defs = calloc (num_defs, sizeof (qfo_def_t));
functions = calloc (num_functions, sizeof (qfo_function_t));
refs = calloc (num_refs, sizeof (qfo_ref_t));
}
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);
return ReuseString (encoding->str);
}
static int
flags (def_t *d)
{
int flags = 0;
if (d->initialized)
flags |= QFOD_INITIALIZED;
if (d->constant)
flags |= QFOD_CONSTANT;
if (d->global)
flags |= QFOD_GLOBAL;
if (d->absolute)
flags |= QFOD_ABSOLUTE;
return flags;
}
static void
write_refs (reloc_t *r, qfo_ref_t **ref)
{
while (r) {
(*ref)->ofs = LittleLong (r->ofs);
(*ref)->type = LittleLong (r->type);
(*ref)++;
r = r->next;
}
}
static void
write_def (def_t *d, qfo_def_t *def, qfo_ref_t **ref)
{
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);
def->refs = LittleLong (*ref - refs);
def->num_refs = LittleLong (count_refs (d->refs));
def->flags = LittleLong (flags (d));
def->file = LittleLong (d->file);
def->line = LittleLong (d->line);
write_refs (d->refs, ref);
}
static void
setup_data (void)
{
qfo_def_t *def = defs;
def_t *d;
qfo_function_t *func = functions;
function_t *f;
qfo_ref_t *ref = refs;
for (d = pr.scope->head; d; d = d->def_next)
write_def (d, def++, &ref);
for (f = pr.func_head; f; f = f->next) {
func->name = LittleLong (f->dfunc->s_name);
func->file = LittleLong (f->dfunc->s_file);
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);
write_def (f->def, def++, &ref);
}
func->locals_size = LittleLong (f->scope->space->size);
func->local_defs = LittleLong (def - defs);
func->num_local_defs = LittleLong (f->scope->num_defs);
//func->line_info
//func->num_lines
func->num_parms = LittleLong (f->dfunc->numparms);
memcpy (func->parm_size, f->dfunc->parm_size, MAX_PARMS);
func->refs = LittleLong (ref - refs);
write_refs (f->refs, &ref);
for (d = f->scope->head; d; d = d->def_next)
write_def (d, def++, &ref);
}
}
int
write_obj_file (const char *filename)
{
qfo_header_t hdr;
VFile *file;
int ofs;
allocate_stuff ();
setup_data ();
file = Qopen (filename, "wbz9");
pr.strofs = (pr.strofs + 3) & ~3;
memset (&hdr, 0, sizeof (hdr));
ofs = Qwrite (file, &hdr, sizeof (hdr));
memcpy (hdr.qfo, QFO, sizeof (hdr.qfo));
hdr.version = LittleLong (QFO_VERSION);
hdr.code_ofs = LittleLong (ofs);
hdr.code_size = LittleLong (pr.num_statements);
ofs += Qwrite (file, pr.statements,
pr.num_statements * sizeof (dstatement_t));
hdr.data_ofs = LittleLong (ofs);
hdr.data_size = LittleLong (pr.near_data->size);
ofs += Qwrite (file, pr.near_data->data,
pr.near_data->size * sizeof (pr_type_t));
if (pr.far_data) {
hdr.far_data_ofs = LittleLong (ofs);
hdr.far_data_size = LittleLong (pr.far_data->size);
ofs += Qwrite (file, pr.far_data->data,
pr.far_data->size * sizeof (pr_type_t));
}
hdr.strings_ofs = LittleLong (ofs);
hdr.strings_size = LittleLong (pr.strofs);
ofs += Qwrite (file, pr.strings, pr.strofs);
hdr.relocs_ofs = LittleLong (ofs);
hdr.num_relocs = LittleLong (num_refs);
ofs += Qwrite (file, refs, num_refs * sizeof (qfo_ref_t));
hdr.defs_ofs = LittleLong (ofs);
hdr.num_defs = LittleLong (num_defs);
ofs += Qwrite (file, defs, num_defs * sizeof (qfo_def_t));
hdr.functions_ofs = LittleLong (ofs);
hdr.num_functions = LittleLong (num_functions);
Qwrite (file, defs, num_functions * sizeof (qfo_function_t));
Qseek (file, 0, SEEK_SET);
Qwrite (file, &hdr, sizeof (hdr));
Qseek (file, 0, SEEK_END);
Qclose (file);
return 0;
}

View file

@ -75,23 +75,24 @@ static struct option const long_options[] = {
};
static const char *short_options =
"s:" // source dir
"P:" // progs.src name
"F" // generate files.dat
"q" // quiet
"v" // verbose
"g" // debug
"C:" // code options
"W:" // warning options
"h" // help
"V" // version
"p:" // strip path
"S" // save temps
"D:" // define
"I:" // set includes
"U:" // undefine
"N:" // notice options
;
"c" // separate compilation
"s:" // source dir
"P:" // progs.src name
"F" // generate files.dat
"q" // quiet
"v" // verbose
"g" // debug
"C:" // code options
"W:" // warning options
"h" // help
"V" // version
"p:" // strip path
"S" // save temps
"D:" // define
"I:" // set includes
"U:" // undefine
"N:" // notice options
;
static void
usage (int status)
@ -174,6 +175,9 @@ DecodeArgs (int argc, char **argv)
options.traditional = true;
options.code.progsversion = PROG_ID_VERSION;
break;
case 'c': // traditional
options.compile = true;
break;
case 'C':{ // code options
char *opts = strdup (optarg);
char *temp = strtok (opts, ",");

View file

@ -45,7 +45,7 @@ static const char rcsid[] =
#include <QF/hash.h>
#include <QF/sys.h>
#include "qfcc.h"
#include "class.h"
#include "debug.h"
#include "def.h"
#include "expr.h"
@ -53,7 +53,8 @@ static const char rcsid[] =
#include "immediate.h"
#include "method.h"
#include "options.h"
#include "class.h"
#include "qfcc.h"
#include "reloc.h"
#include "struct.h"
#include "switch.h"
#include "type.h"
@ -480,6 +481,7 @@ begin_function
{
$$ = current_func = new_function ();
$$->def = current_def;
$$->refs = new_reloc ($$->def->ofs, rel_def_func);
$$->code = pr.num_statements;
if (options.code.debug) {
pr_lineno_t *lineno = new_lineno ();
@ -512,14 +514,15 @@ statement_block
statements '}'
{
def_t *defs = current_scope->head;
int num_defs = current_scope->num_defs;
flush_scope (current_scope, 1);
current_scope = current_scope->parent;
current_scope->num_defs += num_defs;
*current_scope->tail = defs;
while (*current_scope->tail) {
current_scope->tail = &(*current_scope->tail)->def_next;
current_scope->num_defs++;
}
$$ = $3;
}
@ -1085,6 +1088,7 @@ methoddef
}
begin_function statement_block end_function
{
$2->func = $6;
build_function ($6);
if ($4) {
$4->next = $7;
@ -1100,7 +1104,7 @@ methoddef
$2 = class_find_method (current_class, $2);
$2->def = method_def (current_class, $2);
build_builtin_function ($2->def, $5);
$2->func = build_builtin_function ($2->def, $5);
}
| '-' methoddecl
{
@ -1114,6 +1118,7 @@ methoddef
}
begin_function statement_block end_function
{
$2->func = $6;
build_function ($6);
if ($4) {
$4->next = $7;
@ -1129,7 +1134,7 @@ methoddef
$2 = class_find_method (current_class, $2);
$2->def = method_def (current_class, $2);
build_builtin_function ($2->def, $5);
$2->func = build_builtin_function ($2->def, $5);
}
;

View file

@ -71,6 +71,7 @@ static const char rcsid[] =
#include "function.h"
#include "idstuff.h"
#include "immediate.h"
#include "obj_file.h"
#include "opcodes.h"
#include "options.h"
#include "reloc.h"
@ -494,19 +495,23 @@ main (int argc, char **argv)
return 1;
}
if (!finish_compilation ())
Error ("compilation errors");
if (options.compile) {
write_obj_file ("test.qfo");
} else {
if (!finish_compilation ())
Error ("compilation errors");
// write progdefs.h
if (options.code.progsversion == PROG_ID_VERSION)
crc = WriteProgdefs ("progdefs.h");
// write progdefs.h
if (options.code.progsversion == PROG_ID_VERSION)
crc = WriteProgdefs ("progdefs.h");
// write data file
WriteData (crc);
// write data file
WriteData (crc);
// write files.dat
if (options.files_dat)
WriteFiles (sourcedir);
// write files.dat
if (options.files_dat)
WriteFiles (sourcedir);
}
stop = Sys_DoubleTime ();
if (options.verbosity >= 0)

View file

@ -95,6 +95,7 @@ relocate_refs (reloc_t *refs, int ofs)
G_INT (refs->ofs) = ofs;
break;
case rel_def_def:
case rel_def_func:
G_INT (refs->ofs) = ofs;
break;
}