2012-11-22 06:40:16 +00:00
|
|
|
/*
|
|
|
|
test-harness.c
|
|
|
|
|
|
|
|
Program for testing qfcc generated code.
|
|
|
|
|
|
|
|
Copyright (C) 2012 Bill Currie <bill@taniwha.org>
|
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2012/11/22
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-03-25 13:01:31 +00:00
|
|
|
#include <string.h>
|
2012-11-22 06:40:16 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <QF/cmd.h>
|
|
|
|
#include <QF/cvar.h>
|
|
|
|
#include <QF/progs.h>
|
|
|
|
#include <QF/quakefs.h>
|
|
|
|
#include "QF/ruamoko.h"
|
|
|
|
#include <QF/sys.h>
|
|
|
|
#include "QF/va.h"
|
|
|
|
#include <QF/zone.h>
|
|
|
|
|
|
|
|
#include "test-bi.h"
|
|
|
|
|
|
|
|
#define MAX_EDICTS 64 // 64 edicts should be enough for testing
|
|
|
|
#define MAX_HEAP 1024*1024 // 1MB should be enough for testing
|
2021-03-25 13:01:31 +00:00
|
|
|
#define MAX_STACK 64*1024 // 64kB should be enough for testing
|
2012-11-22 06:40:16 +00:00
|
|
|
|
|
|
|
// keep me sane when adding long options :P
|
|
|
|
enum {
|
|
|
|
start_opts = 255, // not used, starts the enum.
|
|
|
|
OPT_DEVELOPER,
|
2012-11-22 11:47:18 +00:00
|
|
|
OPT_FLOAT,
|
2012-11-22 06:40:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct option const long_options[] = {
|
|
|
|
{"developer", required_argument, 0, OPT_DEVELOPER},
|
2012-11-22 11:47:18 +00:00
|
|
|
{"float", no_argument, 0, OPT_FLOAT},
|
2012-11-22 06:40:16 +00:00
|
|
|
{"trace", no_argument, 0, 't'},
|
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
{"version", no_argument, 0, 'V'},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *short_options =
|
|
|
|
"+" // magic option parsing mode doohicky (must come first)
|
|
|
|
"h" // help
|
|
|
|
"t" // tracing
|
|
|
|
"V" // version
|
|
|
|
;
|
|
|
|
|
2021-03-25 13:01:31 +00:00
|
|
|
static edict_t test_edicts[MAX_EDICTS];
|
|
|
|
|
2012-11-22 06:40:16 +00:00
|
|
|
static edict_t *edicts;
|
2022-01-16 13:15:18 +00:00
|
|
|
static pr_uint_t num_edicts;
|
|
|
|
static pr_uint_t reserved_edicts;
|
2021-03-25 13:01:31 +00:00
|
|
|
static progs_t test_pr;
|
2012-11-22 06:40:16 +00:00
|
|
|
static const char *this_program;
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
int developer;
|
|
|
|
int trace;
|
2012-11-22 11:47:18 +00:00
|
|
|
int flote;
|
2012-11-22 06:40:16 +00:00
|
|
|
} options;
|
|
|
|
|
|
|
|
static QFile *
|
|
|
|
open_file (const char *path, int *len)
|
|
|
|
{
|
|
|
|
QFile *file = Qopen (path, "rbz");
|
|
|
|
|
|
|
|
if (!file) {
|
|
|
|
perror (path);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*len = Qfilesize (file);
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
2020-02-21 12:17:28 +00:00
|
|
|
load_file (progs_t *pr, const char *name, off_t *_size)
|
2012-11-22 06:40:16 +00:00
|
|
|
{
|
|
|
|
QFile *file;
|
|
|
|
int size;
|
|
|
|
char *sym;
|
|
|
|
|
|
|
|
file = open_file (name, &size);
|
|
|
|
if (!file) {
|
2021-02-05 12:42:35 +00:00
|
|
|
file = open_file (va (0, "%s.gz", name), &size);
|
2012-11-22 06:40:16 +00:00
|
|
|
if (!file) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sym = malloc (size + 1);
|
|
|
|
sym[size] = 0;
|
|
|
|
Qread (file, sym, size);
|
2020-02-21 12:17:28 +00:00
|
|
|
*_size = size;
|
2012-11-22 06:40:16 +00:00
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
2022-01-26 07:57:34 +00:00
|
|
|
#define ALIGN 32
|
|
|
|
|
2012-11-22 06:40:16 +00:00
|
|
|
static void *
|
|
|
|
allocate_progs_mem (progs_t *pr, int size)
|
|
|
|
{
|
2022-01-26 07:57:34 +00:00
|
|
|
intptr_t mem = (intptr_t) malloc (size + ALIGN);
|
|
|
|
mem = (mem + ALIGN - 1) & ~(ALIGN - 1);
|
|
|
|
return (void *) mem;
|
2012-11-22 06:40:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_progs_mem (progs_t *pr, void *mem)
|
|
|
|
{
|
|
|
|
free (mem);
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:01:31 +00:00
|
|
|
static int
|
|
|
|
init_edicts (progs_t *pr)
|
|
|
|
{
|
|
|
|
memset (test_edicts, 0, sizeof (test_edicts));
|
|
|
|
|
|
|
|
// init the data field of the edicts
|
|
|
|
for (int i = 0; i < MAX_EDICTS; i++) {
|
|
|
|
edict_t *ent = EDICT_NUM (&test_pr, i);
|
|
|
|
ent->pr = &test_pr;
|
|
|
|
ent->entnum = i;
|
|
|
|
ent->edict = EDICT_TO_PROG (&test_pr, ent);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-11-22 06:40:16 +00:00
|
|
|
static void
|
|
|
|
init_qf (void)
|
|
|
|
{
|
|
|
|
Sys_Init ();
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
developer = options.developer;
|
2012-11-22 06:40:16 +00:00
|
|
|
|
2021-02-03 04:19:19 +00:00
|
|
|
Memory_Init (Sys_Alloc (1024 * 1024), 1024 * 1024);
|
2012-11-22 06:40:16 +00:00
|
|
|
|
2021-03-25 13:01:31 +00:00
|
|
|
test_pr.pr_edicts = &edicts;
|
|
|
|
test_pr.num_edicts = &num_edicts;
|
|
|
|
test_pr.reserved_edicts = &reserved_edicts;
|
|
|
|
test_pr.load_file = load_file;
|
|
|
|
test_pr.allocate_progs_mem = allocate_progs_mem;
|
|
|
|
test_pr.free_progs_mem = free_progs_mem;
|
|
|
|
test_pr.no_exec_limit = 0; // absolutely want a limit!
|
2012-11-22 06:40:16 +00:00
|
|
|
|
|
|
|
PR_Init_Cvars ();
|
2021-03-25 13:01:31 +00:00
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
pr_debug = options.trace > 1 ? 4 : 2;
|
|
|
|
pr_boundscheck = 2;
|
|
|
|
pr_deadbeef_locals = 1;
|
|
|
|
|
2021-03-25 13:01:31 +00:00
|
|
|
PR_AddLoadFunc (&test_pr, init_edicts);
|
|
|
|
PR_Init (&test_pr);
|
|
|
|
|
|
|
|
RUA_Init (&test_pr, 0);
|
|
|
|
PR_Cmds_Init(&test_pr);
|
|
|
|
BI_Init (&test_pr);
|
2012-11-22 06:40:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
load_progs (const char *name)
|
|
|
|
{
|
|
|
|
QFile *file;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
file = open_file (name, &size);
|
|
|
|
if (!file) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-03-25 13:01:31 +00:00
|
|
|
test_pr.progs_name = name;
|
|
|
|
test_pr.max_edicts = MAX_EDICTS;
|
|
|
|
test_pr.zone_size = MAX_HEAP;
|
|
|
|
test_pr.stack_size = MAX_STACK;
|
|
|
|
edicts = test_edicts;
|
|
|
|
|
|
|
|
PR_LoadProgsFile (&test_pr, file, size);
|
2012-11-22 06:40:16 +00:00
|
|
|
Qclose (file);
|
2021-03-25 13:01:31 +00:00
|
|
|
if (!PR_RunLoadFuncs (&test_pr))
|
|
|
|
PR_Error (&test_pr, "unable to load %s", test_pr.progs_name);
|
|
|
|
if (!PR_RunPostLoadFuncs (&test_pr))
|
|
|
|
PR_Error (&test_pr, "unable to load %s", test_pr.progs_name);
|
|
|
|
test_pr.pr_trace_depth = -1;
|
|
|
|
test_pr.pr_trace = options.trace;
|
2012-11-22 06:40:16 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage (int exitval)
|
|
|
|
{
|
|
|
|
printf ("%s - QuakeForge VM test harness\n", this_program);
|
|
|
|
printf ("Usage: %s [options] [progs.dat [progs options]]", this_program);
|
|
|
|
printf (
|
|
|
|
"Options:\n"
|
|
|
|
" --developer FLAGS Set the developer cvar to FLAGS.\n"
|
2012-11-22 11:47:18 +00:00
|
|
|
" --float main () returns float instead of int.\n"
|
2012-11-22 06:40:16 +00:00
|
|
|
" -h, --help Display this help and exit\n"
|
|
|
|
" -t, --trace Set the trace flag in the VM.\n"
|
|
|
|
" -V, --version Output version information and exit\n"
|
|
|
|
);
|
|
|
|
exit (exitval);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_options (int argc, char **argv)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
this_program = argv[0];
|
|
|
|
while ((c = getopt_long (argc, argv, short_options, long_options, 0))
|
|
|
|
!= -1) {
|
|
|
|
switch (c) {
|
|
|
|
case OPT_DEVELOPER:
|
|
|
|
options.developer = atoi (optarg);
|
|
|
|
break;
|
2012-11-22 11:47:18 +00:00
|
|
|
case OPT_FLOAT:
|
|
|
|
options.flote = 1;
|
|
|
|
break;
|
2012-11-22 06:40:16 +00:00
|
|
|
case 't':
|
2020-02-14 11:08:59 +00:00
|
|
|
options.trace++;
|
2012-11-22 06:40:16 +00:00
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
usage (0);
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
printf ("%s version %s\n", PACKAGE_NAME, PACKAGE_VERSION);
|
|
|
|
exit (0);
|
|
|
|
default:
|
|
|
|
usage (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return optind;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
dfunction_t *dfunc;
|
2022-01-18 06:32:43 +00:00
|
|
|
pr_func_t main_func = 0;
|
2012-11-22 06:40:16 +00:00
|
|
|
const char *name = "progs.dat";
|
2022-01-18 03:11:14 +00:00
|
|
|
pr_string_t *pr_argv;
|
2012-11-22 06:40:16 +00:00
|
|
|
int pr_argc = 1, i;
|
|
|
|
|
|
|
|
i = parse_options (argc, argv);
|
|
|
|
argc -= i;
|
|
|
|
argv += i;
|
|
|
|
|
|
|
|
init_qf ();
|
|
|
|
|
|
|
|
if (argc > 0)
|
|
|
|
name = argv[0];
|
|
|
|
|
|
|
|
if (!load_progs (name))
|
|
|
|
Sys_Error ("couldn't load %s", name);
|
|
|
|
|
2022-01-22 06:31:26 +00:00
|
|
|
if ((dfunc = PR_FindFunction (&test_pr, ".main"))
|
|
|
|
|| (dfunc = PR_FindFunction (&test_pr, "main"))) {
|
|
|
|
main_func = dfunc - test_pr.pr_functions;
|
|
|
|
} else {
|
|
|
|
PR_Undefined (&test_pr, "function", "main");
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:01:31 +00:00
|
|
|
PR_PushFrame (&test_pr);
|
2022-01-22 06:31:26 +00:00
|
|
|
if (argc) {
|
2018-08-23 11:22:29 +00:00
|
|
|
pr_argc = argc;
|
2022-01-22 06:31:26 +00:00
|
|
|
}
|
2021-03-25 13:01:31 +00:00
|
|
|
pr_argv = PR_Zone_Malloc (&test_pr, (pr_argc + 1) * 4);
|
|
|
|
pr_argv[0] = PR_SetTempString (&test_pr, name);
|
2022-01-22 06:31:26 +00:00
|
|
|
for (i = 1; i < pr_argc; i++) {
|
2021-03-25 13:01:31 +00:00
|
|
|
pr_argv[i] = PR_SetTempString (&test_pr, argv[i]);
|
2022-01-22 06:31:26 +00:00
|
|
|
}
|
2012-11-22 06:40:16 +00:00
|
|
|
pr_argv[i] = 0;
|
|
|
|
|
2021-03-25 13:01:31 +00:00
|
|
|
PR_RESET_PARAMS (&test_pr);
|
|
|
|
P_INT (&test_pr, 0) = pr_argc;
|
|
|
|
P_POINTER (&test_pr, 1) = PR_SetPointer (&test_pr, pr_argv);
|
|
|
|
test_pr.pr_argc = 2;
|
2022-01-22 06:31:26 +00:00
|
|
|
|
2021-03-25 13:01:31 +00:00
|
|
|
PR_ExecuteProgram (&test_pr, main_func);
|
|
|
|
PR_PopFrame (&test_pr);
|
2012-11-22 11:47:18 +00:00
|
|
|
if (options.flote)
|
2021-03-25 13:01:31 +00:00
|
|
|
return R_FLOAT (&test_pr);
|
|
|
|
return R_INT (&test_pr);
|
2012-11-22 06:40:16 +00:00
|
|
|
}
|