2002-01-28 17:15:36 +00:00
|
|
|
/*
|
|
|
|
pr_edict.c
|
|
|
|
|
|
|
|
entity dictionary
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
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
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2002-01-28 17:15:36 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "QF/cmd.h"
|
|
|
|
#include "QF/crc.h"
|
|
|
|
#include "QF/cvar.h"
|
2021-03-21 10:56:17 +00:00
|
|
|
#include "QF/darray.h"
|
2002-01-28 17:15:36 +00:00
|
|
|
#include "QF/hash.h"
|
|
|
|
#include "QF/progs.h"
|
|
|
|
#include "QF/qdefs.h"
|
|
|
|
#include "QF/qendian.h"
|
2002-08-27 07:16:28 +00:00
|
|
|
#include "QF/quakefs.h"
|
2002-01-28 17:15:36 +00:00
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/zone.h"
|
|
|
|
|
|
|
|
#include "compat.h"
|
|
|
|
|
2021-03-21 10:56:17 +00:00
|
|
|
typedef struct biblock_s DARRAY_TYPE (builtin_t *) biblock_t;
|
|
|
|
|
2002-01-28 17:15:36 +00:00
|
|
|
static const char *
|
2012-07-18 13:34:37 +00:00
|
|
|
builtin_get_key (const void *_bi, void *unused)
|
2002-01-28 17:15:36 +00:00
|
|
|
{
|
|
|
|
builtin_t *bi = (builtin_t *)_bi;
|
2004-01-06 05:51:09 +00:00
|
|
|
|
2002-01-28 17:15:36 +00:00
|
|
|
return bi->name;
|
|
|
|
}
|
|
|
|
|
2007-04-04 07:48:14 +00:00
|
|
|
static uintptr_t
|
2012-07-18 13:34:37 +00:00
|
|
|
builtin_get_hash (const void *_bi, void *unused)
|
2004-01-06 05:51:09 +00:00
|
|
|
{
|
|
|
|
builtin_t *bi = (builtin_t *)_bi;
|
|
|
|
|
|
|
|
return bi->binum;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-07-18 13:34:37 +00:00
|
|
|
builtin_compare (const void *_bia, const void *_bib, void *unused)
|
2004-01-06 05:51:09 +00:00
|
|
|
{
|
|
|
|
builtin_t *bia = (builtin_t *)_bia;
|
|
|
|
builtin_t *bib = (builtin_t *)_bib;
|
|
|
|
|
|
|
|
return bia->binum == bib->binum;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
builtin_next (progs_t *pr)
|
|
|
|
{
|
|
|
|
if (!pr->bi_next)
|
|
|
|
pr->bi_next = PR_AUTOBUILTIN;
|
|
|
|
if (pr->bi_next == 0x80000000)
|
|
|
|
PR_Error (pr, "too many auto-allocated builtins");
|
|
|
|
return pr->bi_next++;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2004-01-06 05:51:09 +00:00
|
|
|
PR_RegisterBuiltins (progs_t *pr, builtin_t *builtins)
|
2002-01-28 17:15:36 +00:00
|
|
|
{
|
2004-01-06 05:51:09 +00:00
|
|
|
builtin_t *bi;
|
2004-01-07 02:48:11 +00:00
|
|
|
int count;
|
2002-01-28 17:15:36 +00:00
|
|
|
|
2004-01-06 05:51:09 +00:00
|
|
|
if (!pr->builtin_hash) {
|
2021-03-21 10:56:17 +00:00
|
|
|
pr->builtin_blocks = malloc (sizeof (biblock_t));
|
|
|
|
DARRAY_INIT (pr->builtin_blocks, 16);
|
2020-03-25 06:43:16 +00:00
|
|
|
pr->builtin_hash = Hash_NewTable (1021, builtin_get_key, 0, pr,
|
|
|
|
pr->hashlink_freelist);
|
|
|
|
pr->builtin_num_hash = Hash_NewTable (1021, 0, 0, pr,
|
|
|
|
pr->hashlink_freelist);
|
2004-01-06 05:51:09 +00:00
|
|
|
Hash_SetHashCompare (pr->builtin_num_hash, builtin_get_hash,
|
|
|
|
builtin_compare);
|
2002-01-28 17:15:36 +00:00
|
|
|
}
|
|
|
|
|
2004-01-07 02:48:11 +00:00
|
|
|
// count = 1 for terminator
|
|
|
|
for (bi = builtins, count = 1; bi->name; bi++)
|
|
|
|
count++;
|
|
|
|
bi = malloc (count * sizeof (builtin_t));
|
2021-03-21 10:56:17 +00:00
|
|
|
DARRAY_APPEND (pr->builtin_blocks, bi);
|
2004-01-07 02:48:11 +00:00
|
|
|
memcpy (bi, builtins, count * sizeof (builtin_t));
|
|
|
|
builtins = bi;
|
|
|
|
|
2004-01-06 05:51:09 +00:00
|
|
|
while (builtins->name) {
|
|
|
|
if (builtins->binum == 0 || builtins->binum >= PR_AUTOBUILTIN)
|
|
|
|
PR_Error (pr, "bad builtin number: %s = #%d", builtins->name,
|
|
|
|
builtins->binum);
|
|
|
|
|
|
|
|
if (builtins->binum < 0)
|
|
|
|
builtins->binum = builtin_next (pr);
|
|
|
|
|
|
|
|
if ((bi = Hash_Find (pr->builtin_hash, builtins->name))
|
|
|
|
|| (bi = Hash_FindElement (pr->builtin_num_hash, builtins)))
|
|
|
|
PR_Error (pr, "builtin %s = #%d already defined (%s = #%d)",
|
|
|
|
builtins->name, builtins->binum,
|
|
|
|
bi->name, bi->binum);
|
|
|
|
|
|
|
|
Hash_Add (pr->builtin_hash, builtins);
|
|
|
|
Hash_AddElement (pr->builtin_num_hash, builtins);
|
|
|
|
|
|
|
|
builtins++;
|
2002-01-28 17:15:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE builtin_t *
|
2002-01-28 17:15:36 +00:00
|
|
|
PR_FindBuiltin (progs_t *pr, const char *name)
|
|
|
|
{
|
|
|
|
return (builtin_t *) Hash_Find (pr->builtin_hash, name);
|
|
|
|
}
|
|
|
|
|
2004-01-06 05:51:09 +00:00
|
|
|
builtin_t *
|
2007-04-04 12:30:49 +00:00
|
|
|
PR_FindBuiltinNum (progs_t *pr, pr_int_t num)
|
2004-01-06 05:51:09 +00:00
|
|
|
{
|
|
|
|
builtin_t bi;
|
|
|
|
|
|
|
|
bi.binum = num;
|
|
|
|
return (builtin_t *) Hash_FindElement (pr->builtin_num_hash, &bi);
|
|
|
|
}
|
|
|
|
|
2004-01-06 03:18:44 +00:00
|
|
|
static void
|
|
|
|
bi_no_function (progs_t *pr)
|
|
|
|
{
|
|
|
|
// no need for checking: the /only/ way to get here is via a function
|
|
|
|
// descriptor with a bad builtin number
|
|
|
|
dstatement_t *st = pr->pr_statements + pr->pr_xstatement;
|
2007-04-07 01:41:23 +00:00
|
|
|
dfunction_t *desc = pr->pr_functions + G_FUNCTION (pr, st->a);
|
|
|
|
const char *bi_name = PR_GetString (pr, desc->s_name);
|
|
|
|
int ind = -desc->first_statement;
|
2004-01-06 03:18:44 +00:00
|
|
|
|
|
|
|
PR_RunError (pr, "Bad builtin called: %s = #%d", bi_name, ind);
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE int
|
2002-01-28 17:15:36 +00:00
|
|
|
PR_RelocateBuiltins (progs_t *pr)
|
|
|
|
{
|
2020-02-25 12:23:13 +00:00
|
|
|
pr_uint_t i;
|
|
|
|
pr_int_t ind;
|
2004-01-05 08:52:20 +00:00
|
|
|
int bad = 0;
|
2007-04-07 01:41:23 +00:00
|
|
|
dfunction_t *desc;
|
|
|
|
bfunction_t *func;
|
2002-01-28 17:15:36 +00:00
|
|
|
builtin_t *bi;
|
2004-01-06 03:18:44 +00:00
|
|
|
builtin_proc proc;
|
2002-01-28 17:15:36 +00:00
|
|
|
const char *bi_name;
|
|
|
|
|
2007-04-07 01:41:23 +00:00
|
|
|
if (pr->function_table)
|
|
|
|
free (pr->function_table);
|
|
|
|
pr->function_table = calloc (pr->progs->numfunctions,
|
|
|
|
sizeof (bfunction_t));
|
|
|
|
|
2002-01-28 17:15:36 +00:00
|
|
|
for (i = 1; i < pr->progs->numfunctions; i++) {
|
2007-04-07 01:41:23 +00:00
|
|
|
desc = pr->pr_functions + i;
|
|
|
|
func = pr->function_table + i;
|
|
|
|
|
|
|
|
func->first_statement = desc->first_statement;
|
|
|
|
func->parm_start = desc->parm_start;
|
|
|
|
func->locals = desc->locals;
|
|
|
|
func->numparms = desc->numparms;
|
|
|
|
memcpy (func->parm_size, desc->parm_size, sizeof (func->parm_size));
|
|
|
|
func->descriptor = desc;
|
2004-01-05 08:08:46 +00:00
|
|
|
|
2007-04-07 01:41:23 +00:00
|
|
|
if (desc->first_statement > 0)
|
2002-01-28 17:15:36 +00:00
|
|
|
continue;
|
2004-01-05 08:08:46 +00:00
|
|
|
|
2007-04-07 01:41:23 +00:00
|
|
|
bi_name = PR_GetString (pr, desc->s_name);
|
2004-01-05 08:52:20 +00:00
|
|
|
|
2007-04-07 01:41:23 +00:00
|
|
|
if (!desc->first_statement) {
|
2004-01-05 08:08:46 +00:00
|
|
|
bi = PR_FindBuiltin (pr, bi_name);
|
|
|
|
if (!bi) {
|
|
|
|
Sys_Printf ("PR_RelocateBuiltins: %s: undefined builtin %s\n",
|
|
|
|
pr->progs_name, bi_name);
|
2004-01-05 08:52:20 +00:00
|
|
|
bad = 1;
|
|
|
|
continue;
|
2004-01-05 08:08:46 +00:00
|
|
|
}
|
2007-04-07 01:41:23 +00:00
|
|
|
desc->first_statement = -bi->binum;
|
2004-01-05 08:08:46 +00:00
|
|
|
}
|
|
|
|
|
2007-04-07 01:41:23 +00:00
|
|
|
ind = -desc->first_statement;
|
2004-01-06 06:08:59 +00:00
|
|
|
if (pr->bi_map)
|
|
|
|
ind = pr->bi_map (pr, ind);
|
|
|
|
bi = PR_FindBuiltinNum (pr, ind);
|
2004-01-06 05:51:09 +00:00
|
|
|
if (!bi || !(proc = bi->proc)) {
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_dev,
|
2010-11-23 05:09:30 +00:00
|
|
|
"WARNING: Bad builtin call number: %s = #%d\n",
|
|
|
|
bi_name, -desc->first_statement);
|
2004-01-06 03:18:44 +00:00
|
|
|
proc = bi_no_function;
|
2002-01-28 17:15:36 +00:00
|
|
|
}
|
2012-07-20 07:17:26 +00:00
|
|
|
if (!desc->s_name && bi) {
|
2012-07-13 01:22:25 +00:00
|
|
|
desc->s_name = PR_SetString (pr, bi->name);
|
2012-07-20 07:17:26 +00:00
|
|
|
Hash_Add (pr->function_hash, &pr->pr_functions[i]);
|
|
|
|
}
|
2007-04-07 01:41:23 +00:00
|
|
|
func->first_statement = desc->first_statement;
|
|
|
|
func->func = proc;
|
2002-01-28 17:15:36 +00:00
|
|
|
}
|
2004-01-05 08:52:20 +00:00
|
|
|
return !bad;
|
2002-01-28 17:15:36 +00:00
|
|
|
}
|