implemented jump table dispatch

- currently only enabled on supported platforms (GNU C Compiler + Clang)
- when enabled, sped up examples/fibtest.cosmo by about 20% (avg of 11.179s prior and 8.799 after)

NOTE: malicious dumps can trivially cause crashes now by having junk function chunks
This commit is contained in:
CPunch 2023-06-03 01:17:28 -05:00 committed by cpunch
parent 7bca6927a9
commit 2b3825d258
4 changed files with 647 additions and 589 deletions

View File

@ -64,7 +64,7 @@ typedef enum
OP_FALSE, OP_FALSE,
OP_NIL, OP_NIL,
OP_RETURN OP_RETURN,
} COPCODE; // there can be a max of 256 instructions } COPCODE; // there can be a max of 256 instructions
#endif #endif

View File

@ -14,7 +14,15 @@
performance, however this will produce undefined behavior as you reach the stack limit (and may performance, however this will produce undefined behavior as you reach the stack limit (and may
cause a seg fault!). It is recommended to keep this enabled. cause a seg fault!). It is recommended to keep this enabled.
*/ */
#define SAFE_STACK // #define SAFE_STACK
/*
NAN_BOXXED:
if undefined, the interpreter will use a tagged union to store values. This is the default.
Note that even though the sizeof(CValue) is 8 bytes for NAN_BOXXED (as opposed to 16 bytes for
the tagged union) no performance benefits were measured. I recommend keeping this undefined for
now.
*/
// #define NAN_BOXXED // #define NAN_BOXXED
// forward declare *most* stuff so our headers are cleaner // forward declare *most* stuff so our headers are cleaner

1211
src/cvm.c

File diff suppressed because it is too large Load Diff

View File

@ -8,6 +8,19 @@
// #define VM_DEBUG // #define VM_DEBUG
/*
if we're using GNUC or clang, we can use computed gotos which speeds up
cosmoV_execute by about 20% from benchmarking. of course, if you know
your compiler supports computed gotos, you can define VM_JUMPTABLE
BTW: be weary of maliciously crafted cosmo dumps!! it's very easy to crash
cosmo with this enabled and reading invalid opcodes due to us just using the
opcode as an index into the jump table
*/
#if defined(__GNUC__) || defined(__clang__)
# define VM_JUMPTABLE
#endif
typedef enum typedef enum
{ {
COSMOVM_OK, COSMOVM_OK,