1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-14 17:50:22 +02:00

Indirect threaded. Performance is the same as before. Wow\!

This commit is contained in:
Keisuke Nishida 2000-09-27 23:29:45 +00:00
parent 015959cb4a
commit 4b482259c3
7 changed files with 65 additions and 109 deletions

View file

@ -10,7 +10,8 @@ stamp-h.in
Makefile Makefile
Makefile.in Makefile.in
*.x *.x
*.vi *.inst
*.op *.label
*.opcode
*.lo *.lo
*.la *.la

View file

@ -11,8 +11,9 @@ libguilevm_la_LDFLAGS = -version-info 0:0:0 -export-dynamic
noinst_HEADERS = vm.h vm_engine.h vm-snarf.h noinst_HEADERS = vm.h vm_engine.h vm-snarf.h
EXTRA_DIST = vm_engine.c vm_system.c vm_scheme.c vm_number.c \ EXTRA_DIST = vm_engine.c vm_system.c vm_scheme.c vm_number.c \
test.scm guile-compile.in test.scm guile-compile.in
BUILT_SOURCES = vm_system.vi vm_scheme.vi vm_number.vi \ BUILT_SOURCES = vm_system.inst vm_scheme.inst vm_number.inst \
vm_system.op vm_scheme.op vm_number.op vm.x vm_system.label vm_scheme.label vm_number.label \
vm_system.opcode vm_scheme.opcode vm_number.opcode vm.x
CFLAGS = -g -O2 -Wall CFLAGS = -g -O2 -Wall
INCLUDES = $(GUILE_CFLAGS) INCLUDES = $(GUILE_CFLAGS)
@ -21,16 +22,20 @@ DISTCLEANFILES = $(BUILT_SOURCES)
MAINTAINERCLEANFILES = Makefile.in config.h.in stamp-h.in MAINTAINERCLEANFILES = Makefile.in config.h.in stamp-h.in
SNARF = guile-snarf SNARF = guile-snarf
SUFFIXES = .x .vi .op SUFFIXES = .x .inst .label .opcode
.c.x: .c.x:
$(SNARF) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) $< > $@ \ $(SNARF) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) $< > $@ \
|| { rm $@; false; } || { rm $@; false; }
.c.vi: .c.inst:
$(SNARF) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) $< > $@ \ $(SNARF) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) $< > $@ \
|| { rm $@; false; } || { rm $@; false; }
.c.op: .c.label:
$(SNARF) -DSCM_SNARF_LABEL $(DEFS) $(INCLUDES) $(CPPFLAGS) \
$(CFLAGS) $< > $@ || { rm $@; false; }
.c.opcode:
$(SNARF) -DSCM_SNARF_OPCODE $(DEFS) $(INCLUDES) $(CPPFLAGS) \ $(SNARF) -DSCM_SNARF_OPCODE $(DEFS) $(INCLUDES) $(CPPFLAGS) \
$(CFLAGS) $< > $@ || { rm $@; false; } $(CFLAGS) $< > $@ || { rm $@; false; }

View file

@ -65,22 +65,36 @@
#else /* SCM_MAGIC_SNARFER */ #else /* SCM_MAGIC_SNARFER */
#ifndef SCM_SNARF_OPCODE #ifndef SCM_SNARF_OPCODE
#ifndef SCM_SNARF_LABEL
/* /*
* These will go to *.vi * These will go to *.inst
*/ */
#define SCM_DEFINE_INSTRUCTION(TAG,NAME,TYPE) \ #define SCM_DEFINE_INSTRUCTION(TAG,NAME,TYPE) \
SCM_SNARF_INIT_START {VM_OPCODE(TAG), TYPE, NAME, VM_ADDR(TAG), SCM_BOOL_F, NULL, 0, 0}, SCM_SNARF_INIT_START {VM_OPCODE(TAG), TYPE, NAME, SCM_BOOL_F, NULL, 0, 0},
#define SCM_DEFINE_VM_FUNCTION(TAG,SNAME,NAME,NARGS,RESTP) \ #define SCM_DEFINE_VM_FUNCTION(TAG,SNAME,NAME,NARGS,RESTP) \
SCM_SNARF_INIT_START {VM_OPCODE(TAG), INST_NONE, NAME, VM_ADDR(TAG), SCM_BOOL_F, SNAME, NARGS, RESTP}, SCM_SNARF_INIT_START {VM_OPCODE(TAG), INST_NONE, NAME, SCM_BOOL_F, SNAME, NARGS, RESTP},
#else /* SCM_SNARF_LABEL */
/*
* These will go to *.label
*/
#define SCM_DEFINE_INSTRUCTION(TAG,NAME,TYPE) \
SCM_SNARF_INIT_START VM_ADDR(TAG),
#define SCM_DEFINE_VM_FUNCTION(TAG,SNAME,NAME,NARGS,RESTP) \
SCM_SNARF_INIT_START VM_ADDR(TAG),
#endif /* SCM_SNARF_LABEL */
#else /* SCM_SNARF_OPCODE */ #else /* SCM_SNARF_OPCODE */
/* /*
* These will go to *.op * These will go to *.opcode
*/ */
#define SCM_DEFINE_INSTRUCTION(TAG,NAME,TYPE) SCM_SNARF_INIT_START VM_OPCODE(TAG), #define SCM_DEFINE_INSTRUCTION(TAG,NAME,TYPE) \
#define SCM_DEFINE_VM_FUNCTION(TAG,SNAME,NAME,NARGS,RESTP) SCM_SNARF_INIT_START VM_OPCODE(TAG), SCM_SNARF_INIT_START VM_OPCODE(TAG),
#define SCM_DEFINE_VM_FUNCTION(TAG,SNAME,NAME,NARGS,RESTP) \
SCM_SNARF_INIT_START VM_OPCODE(TAG),
#endif /* SCM_SNARF_OPCODE */ #endif /* SCM_SNARF_OPCODE */
#endif /* SCM_MAGIC_SNARFER */ #endif /* SCM_MAGIC_SNARFER */

View file

@ -114,22 +114,17 @@ init_name_property ()
* Instruction * Instruction
*/ */
#define INSTRUCTION_HASH_SIZE op_last
#define INSTRUCTION_HASH(ADDR) (((int) (ADDR) >> 1) % INSTRUCTION_HASH_SIZE)
/* These variables are defined in VM engines when they are first called. */
static struct scm_instruction *scm_regular_instruction_table = 0;
static struct scm_instruction *scm_debug_instruction_table = 0;
/* Hash table for finding instructions from addresses */
static struct inst_hash {
void *addr;
struct scm_instruction *inst;
struct inst_hash *next;
} *scm_instruction_hash_table[INSTRUCTION_HASH_SIZE];
static long scm_instruction_tag; static long scm_instruction_tag;
static struct scm_instruction scm_instruction_table[] = {
#include "vm_system.inst"
#include "vm_scheme.inst"
#include "vm_number.inst"
{op_last}
};
#define SCM_INSTRUCTION(OP) &scm_instruction_table[SCM_UNPACK (OP)]
static SCM static SCM
make_instruction (struct scm_instruction *instp) make_instruction (struct scm_instruction *instp)
{ {
@ -155,35 +150,15 @@ init_instruction_type ()
/* C interface */ /* C interface */
static struct scm_instruction * static struct scm_instruction *
find_instruction_by_name (const char *name) scm_lookup_instruction (const char *name)
{ {
struct scm_instruction *p; struct scm_instruction *p;
for (p = scm_regular_instruction_table; p->opcode != op_last; p++) for (p = scm_instruction_table; p->opcode != op_last; p++)
if (strcmp (name, p->name) == 0) if (strcmp (name, p->name) == 0)
return p; return p;
return 0; return 0;
} }
static struct scm_instruction *
find_instruction_by_code (SCM code)
{
struct inst_hash *p;
void *addr = SCM_CODE_TO_ADDR (code);
for (p = scm_instruction_hash_table[INSTRUCTION_HASH (addr)]; p; p = p->next)
if (p->addr == addr)
return p->inst;
return 0;
}
#ifdef HAVE_LABELS_AS_VALUES
static void *
instruction_code_to_debug_addr (SCM code)
{
struct scm_instruction *p = find_instruction_by_code (code);
return scm_debug_instruction_table[p->opcode].addr;
}
#endif
/* Scheme interface */ /* Scheme interface */
SCM_DEFINE (scm_instruction_p, "instruction?", 1, 0, 0, SCM_DEFINE (scm_instruction_p, "instruction?", 1, 0, 0,
@ -219,7 +194,7 @@ SCM_DEFINE (scm_instruction_name_p, "instruction-name?", 1, 0, 0,
#define FUNC_NAME s_scm_instruction_name_p #define FUNC_NAME s_scm_instruction_name_p
{ {
SCM_VALIDATE_SYMBOL (1, name); SCM_VALIDATE_SYMBOL (1, name);
return SCM_BOOL (find_instruction_by_name (SCM_SYMBOL_CHARS (name))); return SCM_BOOL (scm_lookup_instruction (SCM_SYMBOL_CHARS (name)));
} }
#undef FUNC_NAME #undef FUNC_NAME
@ -231,7 +206,7 @@ SCM_DEFINE (scm_symbol_to_instruction, "symbol->instruction", 1, 0, 0,
struct scm_instruction *p; struct scm_instruction *p;
SCM_VALIDATE_SYMBOL (1, name); SCM_VALIDATE_SYMBOL (1, name);
p = find_instruction_by_name (SCM_SYMBOL_CHARS (name)); p = scm_lookup_instruction (SCM_SYMBOL_CHARS (name));
if (!p) if (!p)
SCM_MISC_ERROR ("No such instruction: ~S", SCM_LIST1 (name)); SCM_MISC_ERROR ("No such instruction: ~S", SCM_LIST1 (name));
@ -246,7 +221,7 @@ SCM_DEFINE (scm_instruction_list, "instruction-list", 0, 0, 0,
{ {
SCM list = SCM_EOL; SCM list = SCM_EOL;
struct scm_instruction *p; struct scm_instruction *p;
for (p = scm_regular_instruction_table; p->opcode != op_last; p++) for (p = scm_instruction_table; p->opcode != op_last; p++)
list = scm_cons (p->obj, list); list = scm_cons (p->obj, list);
return scm_reverse_x (list, SCM_EOL); return scm_reverse_x (list, SCM_EOL);
} }
@ -338,7 +313,7 @@ mark_bytecode (SCM bytecode)
for (i = 0; i < size; i++) for (i = 0; i < size; i++)
{ {
p = find_instruction_by_code (base[i]); p = SCM_INSTRUCTION (base[i]);
switch (p->type) switch (p->type)
{ {
case INST_NONE: case INST_NONE:
@ -472,9 +447,9 @@ SCM_DEFINE (scm_make_bytecode, "make-bytecode", 1, 0, 0,
/* Process instruction */ /* Process instruction */
if (!SCM_SYMBOLP (old[i]) if (!SCM_SYMBOLP (old[i])
|| !(p = find_instruction_by_name (SCM_SYMBOL_CHARS (old[i])))) || !(p = scm_lookup_instruction (SCM_SYMBOL_CHARS (old[i]))))
SCM_MISC_ERROR ("Invalid instruction: ~S", SCM_LIST1 (old[i])); SCM_MISC_ERROR ("Invalid instruction: ~S", SCM_LIST1 (old[i]));
new[i] = SCM_ADDR_TO_CODE (p->addr); new[i] = SCM_PACK (p->opcode);
/* Process arguments */ /* Process arguments */
if (p->type == INST_NONE) if (p->type == INST_NONE)
@ -543,7 +518,7 @@ SCM_DEFINE (scm_bytecode_decode, "bytecode-decode", 1, 0, 0,
struct scm_instruction *p; struct scm_instruction *p;
/* Process instruction */ /* Process instruction */
p = find_instruction_by_code (old[i]); p = SCM_INSTRUCTION (old[i]);
if (!p) if (!p)
{ {
broken: broken:
@ -1010,7 +985,7 @@ SCM_DEFINE (scm_vm_fetch_code, "vm-fetch-code", 2, 0, 0,
p = SCM_VM_ADDRESS (addr); p = SCM_VM_ADDRESS (addr);
inst = find_instruction_by_code (*p); inst = SCM_INSTRUCTION (*p);
if (!inst) if (!inst)
SCM_MISC_ERROR ("Broken bytecode", SCM_LIST1 (addr)); SCM_MISC_ERROR ("Broken bytecode", SCM_LIST1 (addr));
@ -1135,7 +1110,7 @@ SCM_SYMBOL (sym_debug, "debug");
static SCM scm_regular_vm (SCM vm, SCM program); static SCM scm_regular_vm (SCM vm, SCM program);
static SCM scm_debug_vm (SCM vm, SCM program); static SCM scm_debug_vm (SCM vm, SCM program);
#define VM_CODE(name) SCM_ADDR_TO_CODE (find_instruction_by_name (name)->addr) #define VM_CODE(name) SCM_PACK (scm_lookup_instruction (name)->opcode)
SCM_DEFINE (scm_vm_run, "vm-run", 2, 0, 0, SCM_DEFINE (scm_vm_run, "vm-run", 2, 0, 0,
(SCM vm, SCM program), (SCM vm, SCM program),
@ -1256,7 +1231,6 @@ scm_init_vm ()
/* Initialize the module */ /* Initialize the module */
scm_module_vm = scm_make_module (scm_read_0str ("(vm vm)")); scm_module_vm = scm_make_module (scm_read_0str ("(vm vm)"));
old_module = scm_select_module (scm_module_vm); old_module = scm_select_module (scm_module_vm);
init_name_property (); init_name_property ();
init_instruction_type (); init_instruction_type ();
init_bytecode_type (); init_bytecode_type ();
@ -1264,37 +1238,15 @@ scm_init_vm ()
init_vm_frame_type (); init_vm_frame_type ();
init_vm_cont_type (); init_vm_cont_type ();
init_vm_type (); init_vm_type ();
#include "vm.x" #include "vm.x"
scm_select_module (old_module); scm_select_module (old_module);
/* Initialize instruction tables */
{ {
int i;
struct scm_instruction *p; struct scm_instruction *p;
for (p = scm_instruction_table; p->opcode != op_last; p++)
SCM vm = make_vm (0);
scm_regular_vm (vm, SCM_BOOL_F);
scm_debug_vm (vm, SCM_BOOL_F);
/* hash table */
for (i = 0; i < INSTRUCTION_HASH_SIZE; i++)
scm_instruction_hash_table[i] = NULL;
for (p = scm_regular_instruction_table; p->opcode != op_last; p++)
{ {
int hash; p->obj = scm_permanent_object (make_instruction (p));
struct inst_hash *data;
SCM inst = scm_permanent_object (make_instruction (p));
p->obj = inst;
if (p->restp) p->type = INST_INUM; if (p->restp) p->type = INST_INUM;
hash = INSTRUCTION_HASH (p->addr);
data = scm_must_malloc (sizeof (*data), "inst_hash");
data->addr = p->addr;
data->inst = p;
data->next = scm_instruction_hash_table[hash];
scm_instruction_hash_table[hash] = data;
} }
} }
} }

View file

@ -51,9 +51,9 @@
/* Opcode */ /* Opcode */
enum scm_opcode { enum scm_opcode {
#include "vm_system.op" #include "vm_system.opcode"
#include "vm_scheme.op" #include "vm_scheme.opcode"
#include "vm_number.op" #include "vm_number.opcode"
op_last op_last
}; };
@ -73,7 +73,6 @@ struct scm_instruction {
enum scm_opcode opcode; /* opcode */ enum scm_opcode opcode; /* opcode */
enum scm_inst_type type; /* argument type */ enum scm_inst_type type; /* argument type */
char *name; /* instruction name */ char *name; /* instruction name */
void *addr; /* instruction address */
SCM obj; /* instruction object */ SCM obj; /* instruction object */
/* fields for VM functions */ /* fields for VM functions */
char *sname; /* Scheme procedure name */ char *sname; /* Scheme procedure name */

View file

@ -45,14 +45,11 @@
/* VM names */ /* VM names */
#undef VM_NAME #undef VM_NAME
#undef VM_TABLE
#if VM_ENGINE == SCM_VM_REGULAR_ENGINE #if VM_ENGINE == SCM_VM_REGULAR_ENGINE
#define VM_NAME scm_regular_vm #define VM_NAME scm_regular_vm
#define VM_TABLE scm_regular_instruction_table
#else #else
#if VM_ENGINE == SCM_VM_DEBUG_ENGINE #if VM_ENGINE == SCM_VM_DEBUG_ENGINE
#define VM_NAME scm_debug_vm #define VM_NAME scm_debug_vm
#define VM_TABLE scm_debug_instruction_table
#endif #endif
#endif #endif
@ -79,20 +76,12 @@ VM_NAME (SCM vm, SCM program)
SCM hook_args = SCM_LIST1 (vm); SCM hook_args = SCM_LIST1 (vm);
#endif #endif
/* Initialize the instruction table at the first time. /* Jump talbe */
* This code must be here because the following table contains static void *jump_table[] = {
* pointers to the labels defined in this function. */ #include "vm_system.label"
if (!VM_TABLE) #include "vm_scheme.label"
{ #include "vm_number.label"
static struct scm_instruction table[] = { };
#include "vm_system.vi"
#include "vm_scheme.vi"
#include "vm_number.vi"
{ op_last }
};
VM_TABLE = table;
return SCM_UNSPECIFIED;
}
/* Initialize the VM */ /* Initialize the VM */
vmp = SCM_VM_DATA (vm); vmp = SCM_VM_DATA (vm);

View file

@ -348,11 +348,7 @@
#undef VM_GOTO_NEXT #undef VM_GOTO_NEXT
#if HAVE_LABELS_AS_VALUES #if HAVE_LABELS_AS_VALUES
#if VM_ENGINE == SCM_VM_DEBUG_ENGINE #define VM_GOTO_NEXT() goto *jump_table[SCM_UNPACK (FETCH ())]
#define VM_GOTO_NEXT() goto *SCM_CODE_TO_DEBUG_ADDR (FETCH ())
#else /* not SCM_VM_DEBUG_ENGINE */
#define VM_GOTO_NEXT() goto *SCM_CODE_TO_ADDR (FETCH ())
#endif
#else /* not HAVE_LABELS_AS_VALUES */ #else /* not HAVE_LABELS_AS_VALUES */
#define VM_GOTO_NEXT() goto vm_start #define VM_GOTO_NEXT() goto vm_start
#endif #endif