Cache jump targets in a hash/set

This commit is contained in:
K. Lange 2024-03-03 22:10:07 +09:00
parent 1928fa6b0c
commit 864cda1bed
5 changed files with 13 additions and 2 deletions

View File

@ -133,6 +133,7 @@ src/debug.o: src/opcodes.h
src/value.o: src/opcodes.h
src/vm.o: src/opcodes.h
src/exceptions.o: src/opcodes.h
modules/dis.so: src/opcodes.h
%.o: %.c ${HEADERS}

View File

@ -197,6 +197,8 @@ static int isJumpTarget(KrkCodeObject * func, size_t startPoint) {
KrkChunk * chunk = &func->chunk;
size_t offset = 0;
if (IS_NONE(func->jumpTargets)) {
func->jumpTargets = krk_dict_of(0,NULL,0);
#define SIMPLE(opc) case opc: size = 1; break;
#define CONSTANT(opc,more) case opc: { size_t constant _unused = chunk->code[offset + 1]; size = 2; more; break; } \
case opc ## _LONG: { size_t constant _unused = (chunk->code[offset + 1] << 16) | \
@ -205,7 +207,7 @@ static int isJumpTarget(KrkCodeObject * func, size_t startPoint) {
#define OPERAND(opc,more) OPERANDB(opc,more) \
case opc ## _LONG: { size = 4; more; break; }
#define JUMP(opc,sign) case opc: { uint16_t jump = (chunk->code[offset + 1] << 8) | (chunk->code[offset + 2]); \
if ((size_t)(offset + 3 sign jump) == startPoint) return 1; \
krk_tableSet(AS_DICT(func->jumpTargets), INTEGER_VAL((size_t)(offset + 3 sign jump)), BOOLEAN_VAL(1)); \
size = 3; break; }
#define CLOSURE_MORE \
KrkCodeObject * function = AS_codeobject(chunk->constants.values[constant]); \
@ -228,7 +230,6 @@ static int isJumpTarget(KrkCodeObject * func, size_t startPoint) {
}
offset += size;
}
return 0;
#undef SIMPLE
#undef OPERANDB
#undef OPERAND
@ -240,6 +241,12 @@ static int isJumpTarget(KrkCodeObject * func, size_t startPoint) {
#undef FORMAT_VALUE_MORE
}
if (!IS_dict(func->jumpTargets)) return 0;
KrkValue garbage;
if (krk_tableGet(AS_DICT(func->jumpTargets), INTEGER_VAL(startPoint), &garbage)) return 1;
return 0;
}
#define OPARGS FILE * f, const char * fullName, size_t * size, size_t * offset, KrkCodeObject * func, KrkChunk * chunk
#define OPARG_VALS f,fullName,size,offset,func,chunk

View File

@ -173,6 +173,7 @@ typedef struct {
size_t expressionsCapacity; /**< @brief Capacity of @ref expressions */
size_t expressionsCount; /**< @brief Number of entries in @ref expressions */
KrkExpressionsMap * expressions; /**< @brief Mapping of bytecode offsets to expression spans for debugging */
KrkValue jumpTargets; /**< @brief Possibly a set of jump targets... */
} KrkCodeObject;

View File

@ -365,6 +365,7 @@ static void blackenObject(KrkObj * object) {
for (size_t i = 0; i < function->localNameCount; ++i) {
krk_markObject((KrkObj*)function->localNames[i].name);
}
krk_markValue(function->jumpTargets);
break;
}
case KRK_OBJ_UPVALUE:

View File

@ -274,6 +274,7 @@ KrkCodeObject * krk_newCodeObject(void) {
krk_initValueArray(&codeobject->positionalArgNames);
krk_initValueArray(&codeobject->keywordArgNames);
krk_initChunk(&codeobject->chunk);
codeobject->jumpTargets = NONE_VAL();
return codeobject;
}