mirror of
https://github.com/polserver/polserver
synced 2026-08-13 08:23:08 -04:00
* added test which will fail * disabled short-circuit jump combiner * docs * reactivating logical convert remove * dedicated step to optimize jumps based on instructions removed disabled ShortCircuit code * added more tests, adapted corechanges * added dedicated CodeSection optimizer
56 lines
1.4 KiB
C++
56 lines
1.4 KiB
C++
#include "bscript/compiler/optimizer/CodeSectionOptimizer.h"
|
|
#include "bscript/StoredToken.h"
|
|
#include "bscript/compilercfg.h"
|
|
#include "bscript/tokens.h"
|
|
|
|
namespace Pol::Bscript::Compiler
|
|
{
|
|
void CodeSectionOptimizer::optimize( CodeSection& code ) const
|
|
{
|
|
if ( compilercfg.ShortCircuitEvaluation )
|
|
short_circuit_jumps( code );
|
|
}
|
|
|
|
void CodeSectionOptimizer::short_circuit_jumps( CodeSection& code ) const
|
|
{
|
|
// recursivly check if a logical jump would jump to another jump and update the final jump
|
|
// location
|
|
auto combine = [&]( StoredToken& jump, auto&& combine )
|
|
{
|
|
const auto& loc = code[jump.offset];
|
|
if ( loc.id == RSV_JMPIFFALSE )
|
|
{
|
|
if ( jump.type == TYP_LOGICAL_JUMP_FALSE )
|
|
jump.offset = loc.offset;
|
|
else
|
|
jump.offset++;
|
|
}
|
|
else if ( loc.id == RSV_JMPIFTRUE )
|
|
{
|
|
if ( jump.type != TYP_LOGICAL_JUMP_FALSE )
|
|
jump.offset = loc.offset;
|
|
else
|
|
jump.offset++;
|
|
}
|
|
else if ( loc.id == INS_LOGICAL_JUMP )
|
|
{
|
|
if ( loc.type == jump.type )
|
|
jump.offset = loc.offset;
|
|
else
|
|
jump.offset++;
|
|
}
|
|
else if ( loc.id == INS_LOGICAL_CONVERT )
|
|
{
|
|
jump.offset++;
|
|
}
|
|
else
|
|
{
|
|
return;
|
|
}
|
|
return combine( jump, combine );
|
|
};
|
|
for ( auto& c : code )
|
|
if ( c.id == INS_LOGICAL_JUMP )
|
|
combine( c, combine );
|
|
}
|
|
} // namespace Pol::Bscript::Compiler
|