polserver/pol-core/bscript/compiler/codegen/CodeEmitter.cpp

31 lines
729 B
C++
Raw Permalink Normal View History

#include "bscript/compiler/codegen/CodeEmitter.h"
#include "bscript/StoredToken.h"
namespace Pol::Bscript::Compiler
{
CodeEmitter::CodeEmitter( CodeSection& code ) : code( code ) {}
unsigned CodeEmitter::append( const StoredToken& st )
{
auto index = next_address();
code.push_back( st );
return index;
}
void CodeEmitter::update_offset( unsigned index, unsigned offset )
{
code[index].offset = static_cast<unsigned short>( offset );
}
unsigned CodeEmitter::next_address() const
{
size_t index = code.size();
if ( index > std::numeric_limits<unsigned>::max() ) {
throw std::runtime_error( "Instruction count overflow" );
}
return static_cast<unsigned>( index );
}
} // namespace Pol::Bscript::Compiler