From afe31a49af11cf83b48947de018a5ac147835762 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Fri, 23 Sep 2011 13:51:26 +0800 Subject: [PATCH] SpiderScript - Light speedups, planning for smarter operation code --- .../libspiderscript.so_src/exec_bytecode.c | 40 +++++++------ .../Libraries/libspiderscript.so_src/values.c | 60 +++++++++++++++++++ Usermode/include/spiderscript.h | 23 +++++++ 3 files changed, 104 insertions(+), 19 deletions(-) diff --git a/Usermode/Libraries/libspiderscript.so_src/exec_bytecode.c b/Usermode/Libraries/libspiderscript.so_src/exec_bytecode.c index 780bee55..4673bd28 100644 --- a/Usermode/Libraries/libspiderscript.so_src/exec_bytecode.c +++ b/Usermode/Libraries/libspiderscript.so_src/exec_bytecode.c @@ -41,8 +41,8 @@ struct sBC_StackEnt { uint8_t Type; union { - uint64_t Integer; - double Real; + int64_t Integer; + double Real; tSpiderValue *Reference; // Used for everything else tSpiderObject *Object; tSpiderNamespace *Namespace; @@ -412,30 +412,32 @@ int Bytecode_int_ExecuteFunction(tSpiderScript *Script, tScript_Function *Fcn, t break; // Variables - case BC_OP_LOADVAR: + case BC_OP_LOADVAR: { + int slot = OP_INDX(op); STATE_HDR(); - DEBUG_F("LOADVAR %i ", OP_INDX(op)); - if( OP_INDX(op) < 0 || OP_INDX(op) >= local_var_count ) { - AST_RuntimeError(NULL, "Loading from invalid slot %i", OP_INDX(op)); + DEBUG_F("LOADVAR %i ", slot); + if( slot < 0 || slot >= local_var_count ) { + AST_RuntimeError(NULL, "Loading from invalid slot %i", slot); return -1; } - DEBUG_F("("); PRINT_STACKVAL(local_vars[OP_INDX(op)]); DEBUG_F(")\n"); - PUT_STACKVAL(local_vars[OP_INDX(op)]); - Bytecode_int_RefStackValue( &local_vars[OP_INDX(op)] ); - break; - case BC_OP_SAVEVAR: + DEBUG_F("("); PRINT_STACKVAL(local_vars[slot]); DEBUG_F(")\n"); + PUT_STACKVAL(local_vars[slot]); + Bytecode_int_RefStackValue( &local_vars[slot] ); + } break; + case BC_OP_SAVEVAR: { + int slot = OP_INDX(op); STATE_HDR(); - DEBUG_F("SAVEVAR %i = ", OP_INDX(op)); - if( OP_INDX(op) < 0 || OP_INDX(op) >= local_var_count ) { - AST_RuntimeError(NULL, "Loading from invalid slot %i", OP_INDX(op)); + DEBUG_F("SAVEVAR %i = ", slot); + if( slot < 0 || slot >= local_var_count ) { + AST_RuntimeError(NULL, "Loading from invalid slot %i", slot); return -1; } - DEBUG_F("[Deref "); PRINT_STACKVAL(local_vars[OP_INDX(op)]); DEBUG_F("] "); - Bytecode_int_DerefStackValue( &local_vars[OP_INDX(op)] ); - GET_STACKVAL(local_vars[OP_INDX(op)]); - PRINT_STACKVAL(local_vars[OP_INDX(op)]); + DEBUG_F("[Deref "); PRINT_STACKVAL(local_vars[slot]); DEBUG_F("] "); + Bytecode_int_DerefStackValue( &local_vars[slot] ); + GET_STACKVAL(local_vars[slot]); + PRINT_STACKVAL(local_vars[slot]); DEBUG_F("\n"); - break; + } break; // Constants: case BC_OP_LOADINT: diff --git a/Usermode/Libraries/libspiderscript.so_src/values.c b/Usermode/Libraries/libspiderscript.so_src/values.c index 12e1bf0d..f72e2903 100644 --- a/Usermode/Libraries/libspiderscript.so_src/values.c +++ b/Usermode/Libraries/libspiderscript.so_src/values.c @@ -23,6 +23,12 @@ tSpiderValue *SpiderScript_CastValueTo(int Type, tSpiderValue *Source); int SpiderScript_IsValueTrue(tSpiderValue *Value); void SpiderScript_FreeValue(tSpiderValue *Value); char *SpiderScript_DumpValue(tSpiderValue *Value); +// --- Operations +tSpiderValue *SpiderScript_DoOp(tSpiderValue *Left, int Operation, int bCanCast, tSpiderValue *Right); +tSpiderValue *SpiderScript_int_DoOpInt(tSpiderValue *Left, int Operation, int bCanCast, tSpiderValue *Right); +tSpiderValue *SpiderScript_int_DoOpReal(tSpiderValue *Left, int Operation, int bCanCast, tSpiderValue *Right); +tSpiderValue *SpiderScript_int_DoOpString(tSpiderValue *Left, int Operation, int bCanCast, tSpiderValue *Right); + // === CODE === /** @@ -398,4 +404,58 @@ char *SpiderScript_DumpValue(tSpiderValue *Value) } +// --- +tSpiderValue *SpiderScript_DoOp(tSpiderValue *Left, int Operation, int bCanCast, tSpiderValue *Right) +{ + switch(Left->Type) + { + case SS_DATATYPE_INTEGER: + return SpiderScript_int_DoOpInt(Left, Operation, bCanCast, Right); + } + return NULL; +} + +tSpiderValue *SpiderScript_int_DoOpInt(tSpiderValue *Left, int Operation, int bCanCast, tSpiderValue *Right) +{ + tSpiderValue *oldright = Right; + tSpiderValue *ret = NULL; + int64_t rv; + + // Casting + if(Right && Right->Type != SS_DATATYPE_INTEGER) { + if(!bCanCast) return ERRPTR; + Right = SpiderScript_CastValueTo(Right, SS_DATATYPE_INTEGER); + } + + // Do the operation + switch(Operation) + { + case SS_VALUEOP_NEGATE: + if(Right) ret = ERRPTR; + else rv = -Left->Integer; + break; + case SS_VALUEOP_ADD: + if(!Right) ret = ERRPTR; + else rv = Left->Integer + Right->Integer; + break; + } + + // Delete temporary value + if( Right != oldright ) + SpiderScript_DereferenceValue(Right); + + // Return error if signaled + if(ret == ERRPTR) + return ERRPTR; + + // Reuse `Left` if possible, to reduce mallocs + if(Left->ReferenceCount == 1) { + SpiderScript_ReferenceValue(Left); + Left->Integer = rv; + return Left; + } + else { + return SpiderScript_CreateInteger(rv); + } +} diff --git a/Usermode/include/spiderscript.h b/Usermode/include/spiderscript.h index 4a492edc..e1dc5078 100644 --- a/Usermode/include/spiderscript.h +++ b/Usermode/include/spiderscript.h @@ -65,6 +65,27 @@ enum eSpiderScript_DataTypes NUM_SS_DATATYPES }; +enum eSpiderValueOps +{ + SS_VALUEOP_NOP, + + SS_VALUEOP_ADD, + SS_VALUEOP_SUBTRACT, + SS_VALUEOP_NEGATE, + SS_VALUEOP_MULIPLY, + SS_VALUEOP_DIVIDE, + SS_VALUEOP_MODULO, + + SS_VALUEOP_BITNOT, + SS_VALUEOP_BITAND, + SS_VALUEOP_BITOR, + SS_VALUEOP_BITXOR, + + SS_VALUEOP_SHIFTLEFT, + SS_VALUEOP_SHIFTRIGHT, + SS_VALUEOP_ROTATELEFT +}; + /** * \brief Namespace definition */ @@ -291,6 +312,8 @@ extern tSpiderValue *SpiderScript_CastValueTo(int Type, tSpiderValue *Source); extern int SpiderScript_IsValueTrue(tSpiderValue *Value); extern void SpiderScript_FreeValue(tSpiderValue *Value); extern char *SpiderScript_DumpValue(tSpiderValue *Value); + +extern tSpiderValue *SpiderScript_DoOp(tSpiderValue *Left, enum eSpiderValueOps Op, int bCanCast, tSpiderValue *Right); /** * \} */ -- 2.20.1