mirror of
https://git.code.sf.net/p/quake/quakeforge
synced 2025-05-31 00:30:57 +00:00
move to using a hash table for builtin number -> builtin lookup so sparse
ranges can be used efficiently. move the auto-allocated builtins to 0x10000000-0x7fffffff. should be more than enough :) use static builtin tables ("nul" terminated) instead of a series of function calls to add builtins to a vm. should be more memory efficient.
This commit is contained in:
parent
a87fc16d12
commit
acd54afff7
23 changed files with 620 additions and 526 deletions
|
@ -54,52 +54,82 @@ static __attribute__ ((unused)) const char rcsid[] =
|
|||
|
||||
#include "compat.h"
|
||||
|
||||
/*
|
||||
Builtins are arranged into groups of 65536 to allow for diffent expantion
|
||||
sets. eg, stock id is 0x0000xxxx, quakeforge is 0x000fxxxx. predefined
|
||||
groups go up to 0x0fffxxxx allowing 4096 different groups. Builtins
|
||||
0x10000000 to 0x7fffffff are reserved for auto-allocation. The range
|
||||
0x8000000 to 0xffffffff is unavailable due to the builtin number being
|
||||
a negative statement address.
|
||||
*/
|
||||
#define PR_AUTOBUILTIN 0x10000000
|
||||
|
||||
static const char *
|
||||
builtin_get_key (void *_bi, void *unused)
|
||||
{
|
||||
builtin_t *bi = (builtin_t *)_bi;
|
||||
|
||||
return bi->name;
|
||||
}
|
||||
|
||||
#define PR_AUTOBUILTIN 120
|
||||
void
|
||||
PR_AddBuiltin (progs_t *pr, const char *name, builtin_proc builtin, int num)
|
||||
static unsigned long
|
||||
builtin_get_hash (void *_bi, void *unused)
|
||||
{
|
||||
int i;
|
||||
builtin_t *bi = (builtin_t *)_bi;
|
||||
|
||||
if (!pr->builtin_hash)
|
||||
return bi->binum;
|
||||
}
|
||||
|
||||
static int
|
||||
builtin_compare (void *_bia, void *_bib, void *unused)
|
||||
{
|
||||
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++;
|
||||
}
|
||||
|
||||
void
|
||||
PR_RegisterBuiltins (progs_t *pr, builtin_t *builtins)
|
||||
{
|
||||
builtin_t *bi;
|
||||
|
||||
if (!pr->builtin_hash) {
|
||||
pr->builtin_hash = Hash_NewTable (1021, builtin_get_key, 0, pr);
|
||||
|
||||
if (pr->numbuiltins == 0) {
|
||||
pr->builtins = calloc (PR_AUTOBUILTIN, sizeof (builtin_t*));
|
||||
pr->numbuiltins = PR_AUTOBUILTIN;
|
||||
if (!pr->builtins)
|
||||
PR_Error (pr, "PR_AddBuiltin: memory allocation error!");
|
||||
pr->builtin_num_hash = Hash_NewTable (1021, 0, 0, pr);
|
||||
Hash_SetHashCompare (pr->builtin_num_hash, builtin_get_hash,
|
||||
builtin_compare);
|
||||
}
|
||||
|
||||
if (num < 0) {
|
||||
for (i = PR_AUTOBUILTIN;
|
||||
i < pr->numbuiltins && pr->builtins[i]; i++)
|
||||
;
|
||||
if (i >= pr->numbuiltins) {
|
||||
pr->numbuiltins++;
|
||||
pr->builtins = realloc (pr->builtins,
|
||||
pr->numbuiltins * sizeof (builtin_t*));
|
||||
if (!pr->builtins)
|
||||
PR_Error (pr, "PR_AddBuiltin: memory allocation error!");
|
||||
}
|
||||
} else {
|
||||
if (num >= PR_AUTOBUILTIN || num == 0)
|
||||
PR_Error (pr, "PR_AddBuiltin: invalid builtin number.");
|
||||
if (pr->builtins[num])
|
||||
PR_Error (pr, "PR_AddBuiltin: builtin number already exists.");
|
||||
i = num;
|
||||
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++;
|
||||
}
|
||||
pr->builtins[i] = malloc (sizeof (builtin_t));
|
||||
pr->builtins[i]->proc = builtin;
|
||||
pr->builtins[i]->name = name;
|
||||
pr->builtins[i]->binum = i;
|
||||
Hash_Add (pr->builtin_hash, pr->builtins[i]);
|
||||
}
|
||||
|
||||
builtin_t *
|
||||
|
@ -108,6 +138,15 @@ PR_FindBuiltin (progs_t *pr, const char *name)
|
|||
return (builtin_t *) Hash_Find (pr->builtin_hash, name);
|
||||
}
|
||||
|
||||
builtin_t *
|
||||
PR_FindBuiltinNum (progs_t *pr, int num)
|
||||
{
|
||||
builtin_t bi;
|
||||
|
||||
bi.binum = num;
|
||||
return (builtin_t *) Hash_FindElement (pr->builtin_num_hash, &bi);
|
||||
}
|
||||
|
||||
static void
|
||||
bi_no_function (progs_t *pr)
|
||||
{
|
||||
|
@ -150,9 +189,8 @@ PR_RelocateBuiltins (progs_t *pr)
|
|||
func->first_statement = -bi->binum;
|
||||
}
|
||||
|
||||
ind = -func->first_statement;
|
||||
if (ind >= pr->numbuiltins || !(bi = pr->builtins[ind])
|
||||
|| !(proc = bi->proc)) {
|
||||
bi = PR_FindBuiltinNum (pr, -func->first_statement);
|
||||
if (!bi || !(proc = bi->proc)) {
|
||||
Sys_DPrintf ("WARNING: Bad builtin call number: %s = #%d\n",
|
||||
bi_name, ind);
|
||||
proc = bi_no_function;
|
||||
|
|
|
@ -53,21 +53,16 @@ static __attribute__ ((unused)) const char rcsid[] =
|
|||
static void
|
||||
call_function (progs_t *pr, func_t func)
|
||||
{
|
||||
dfunction_t *newf;
|
||||
dfunction_t *f;
|
||||
|
||||
if (!func)
|
||||
PR_RunError (pr, "NULL function");
|
||||
newf = pr->pr_functions + func;
|
||||
if (newf->first_statement < 0) {
|
||||
f = pr->pr_functions + func;
|
||||
if (f->first_statement < 0) {
|
||||
// negative statements are built in functions
|
||||
int i = -newf->first_statement;
|
||||
|
||||
if (i >= pr->numbuiltins || !pr->builtins[i]
|
||||
|| !pr->builtins[i]->proc)
|
||||
PR_RunError (pr, "Bad builtin call number");
|
||||
pr->builtins[i]->proc (pr);
|
||||
((bfunction_t *) f)->func (pr);
|
||||
} else {
|
||||
PR_EnterFunction (pr, newf);
|
||||
PR_EnterFunction (pr, f);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -919,79 +914,73 @@ pr__c_Object__conformsToProtocol_ (progs_t *pr)
|
|||
|
||||
//====================================================================
|
||||
|
||||
static struct {
|
||||
const char *name;
|
||||
void (*func)(progs_t *pr);
|
||||
} obj_methods [] = {
|
||||
{"__obj_exec_class", pr___obj_exec_class},
|
||||
static builtin_t obj_methods [] = {
|
||||
{"__obj_exec_class", pr___obj_exec_class, -1},
|
||||
|
||||
{"obj_error", pr_obj_error},
|
||||
{"obj_verror", pr_obj_verror},
|
||||
{"obj_set_error_handler", pr_obj_set_error_handler},
|
||||
{"obj_msg_lookup", pr_obj_msg_lookup},
|
||||
{"obj_msg_lookup_super", pr_obj_msg_lookup_super},
|
||||
{"obj_msg_sendv", pr_obj_msg_sendv},
|
||||
{"obj_malloc", pr_obj_malloc},
|
||||
{"obj_atomic_malloc", pr_obj_atomic_malloc},
|
||||
{"obj_valloc", pr_obj_valloc},
|
||||
{"obj_realloc", pr_obj_realloc},
|
||||
{"obj_calloc", pr_obj_calloc},
|
||||
{"obj_free", pr_obj_free},
|
||||
{"obj_get_uninstalled_dtable", pr_obj_get_uninstalled_dtable},
|
||||
{"obj_msgSend", pr_obj_msgSend},
|
||||
{"obj_msgSend_super", pr_obj_msgSend_super},
|
||||
{"obj_error", pr_obj_error, -1},
|
||||
{"obj_verror", pr_obj_verror, -1},
|
||||
{"obj_set_error_handler", pr_obj_set_error_handler, -1},
|
||||
{"obj_msg_lookup", pr_obj_msg_lookup, -1},
|
||||
{"obj_msg_lookup_super", pr_obj_msg_lookup_super, -1},
|
||||
{"obj_msg_sendv", pr_obj_msg_sendv, -1},
|
||||
{"obj_malloc", pr_obj_malloc, -1},
|
||||
{"obj_atomic_malloc", pr_obj_atomic_malloc, -1},
|
||||
{"obj_valloc", pr_obj_valloc, -1},
|
||||
{"obj_realloc", pr_obj_realloc, -1},
|
||||
{"obj_calloc", pr_obj_calloc, -1},
|
||||
{"obj_free", pr_obj_free, -1},
|
||||
{"obj_get_uninstalled_dtable", pr_obj_get_uninstalled_dtable, -1},
|
||||
{"obj_msgSend", pr_obj_msgSend, -1},
|
||||
{"obj_msgSend_super", pr_obj_msgSend_super, -1},
|
||||
|
||||
{"obj_get_class", pr_obj_get_class, -1},
|
||||
{"obj_lookup_class", pr_obj_lookup_class, -1},
|
||||
{"obj_next_class", pr_obj_next_class, -1},
|
||||
|
||||
{"obj_get_class", pr_obj_get_class},
|
||||
{"obj_lookup_class", pr_obj_lookup_class},
|
||||
{"obj_next_class", pr_obj_next_class},
|
||||
{"sel_get_name", pr_sel_get_name, -1},
|
||||
{"sel_get_type", pr_sel_get_type, -1},
|
||||
{"sel_get_uid", pr_sel_get_uid, -1},
|
||||
{"sel_register_name", pr_sel_register_name, -1},
|
||||
{"sel_is_mapped", pr_sel_is_mapped, -1},
|
||||
|
||||
{"sel_get_name", pr_sel_get_name},
|
||||
{"sel_get_type", pr_sel_get_type},
|
||||
{"sel_get_uid", pr_sel_get_uid},
|
||||
{"sel_register_name", pr_sel_register_name},
|
||||
{"sel_is_mapped", pr_sel_is_mapped},
|
||||
{"class_get_class_method", pr_class_get_class_method, -1},
|
||||
{"class_get_instance_method", pr_class_get_instance_method, -1},
|
||||
{"class_pose_as", pr_class_pose_as, -1},
|
||||
{"class_create_instance", pr_class_create_instance, -1},
|
||||
{"class_get_class_name", pr_class_get_class_name, -1},
|
||||
{"class_get_instance_size", pr_class_get_instance_size, -1},
|
||||
{"class_get_meta_class", pr_class_get_meta_class, -1},
|
||||
{"class_get_super_class", pr_class_get_super_class, -1},
|
||||
{"class_get_version", pr_class_get_version, -1},
|
||||
{"class_is_class", pr_class_is_class, -1},
|
||||
{"class_is_meta_class", pr_class_is_meta_class, -1},
|
||||
{"class_set_version", pr_class_set_version, -1},
|
||||
{"class_get_gc_object_type", pr_class_get_gc_object_type, -1},
|
||||
{"class_ivar_set_gcinvisible", pr_class_ivar_set_gcinvisible, -1},
|
||||
|
||||
{"class_get_class_method", pr_class_get_class_method},
|
||||
{"class_get_instance_method", pr_class_get_instance_method},
|
||||
{"class_pose_as", pr_class_pose_as},
|
||||
{"class_create_instance", pr_class_create_instance},
|
||||
{"class_get_class_name", pr_class_get_class_name},
|
||||
{"class_get_instance_size", pr_class_get_instance_size},
|
||||
{"class_get_meta_class", pr_class_get_meta_class},
|
||||
{"class_get_super_class", pr_class_get_super_class},
|
||||
{"class_get_version", pr_class_get_version},
|
||||
{"class_is_class", pr_class_is_class},
|
||||
{"class_is_meta_class", pr_class_is_meta_class},
|
||||
{"class_set_version", pr_class_set_version},
|
||||
{"class_get_gc_object_type", pr_class_get_gc_object_type},
|
||||
{"class_ivar_set_gcinvisible", pr_class_ivar_set_gcinvisible},
|
||||
{"method_get_imp", pr_method_get_imp, -1},
|
||||
{"get_imp", pr_get_imp, -1},
|
||||
|
||||
{"method_get_imp", pr_method_get_imp},
|
||||
{"get_imp", pr_get_imp},
|
||||
{"object_copy", pr_object_copy, -1},
|
||||
{"object_dispose", pr_object_dispose, -1},
|
||||
{"object_get_class", pr_object_get_class, -1},
|
||||
{"object_get_class_name", pr_object_get_class_name, -1},
|
||||
{"object_get_meta_class", pr_object_get_meta_class, -1},
|
||||
{"object_get_super_class", pr_object_get_super_class, -1},
|
||||
{"object_is_class", pr_object_is_class, -1},
|
||||
{"object_is_instance", pr_object_is_instance, -1},
|
||||
{"object_is_meta_class", pr_object_is_meta_class, -1},
|
||||
|
||||
{"object_copy", pr_object_copy},
|
||||
{"object_dispose", pr_object_dispose},
|
||||
{"object_get_class", pr_object_get_class},
|
||||
{"object_get_class_name", pr_object_get_class_name},
|
||||
{"object_get_meta_class", pr_object_get_meta_class},
|
||||
{"object_get_super_class", pr_object_get_super_class},
|
||||
{"object_is_class", pr_object_is_class},
|
||||
{"object_is_instance", pr_object_is_instance},
|
||||
{"object_is_meta_class", pr_object_is_meta_class},
|
||||
|
||||
{"_i_Object__hash", pr__i_Object__hash},
|
||||
{"_i_Object_error_error_", pr__i_Object_error_error_},
|
||||
{"_c_Object__conformsToProtocol_", pr__c_Object__conformsToProtocol_},
|
||||
{"_i_Object__hash", pr__i_Object__hash, -1},
|
||||
{"_i_Object_error_error_", pr__i_Object_error_error_, -1},
|
||||
{"_c_Object__conformsToProtocol_", pr__c_Object__conformsToProtocol_, -1},
|
||||
{0}
|
||||
};
|
||||
|
||||
void
|
||||
PR_Obj_Progs_Init (progs_t *pr)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof (obj_methods) / sizeof (obj_methods[0]); i++) {
|
||||
PR_AddBuiltin (pr, obj_methods[i].name, obj_methods[i].func, -1);
|
||||
}
|
||||
PR_RegisterBuiltins (pr, obj_methods);
|
||||
}
|
||||
|
||||
int
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue