2026-07-25 23:01:18 +02:00
|
|
|
#include "bscript/compiler/codegen/CodeEmitter.h"
|
2020-08-11 11:50:16 -07:00
|
|
|
|
2026-07-25 23:01:18 +02:00
|
|
|
#include "bscript/StoredToken.h"
|
2020-08-11 11:50:16 -07:00
|
|
|
|
|
|
|
|
namespace Pol::Bscript::Compiler
|
|
|
|
|
{
|
|
|
|
|
CodeEmitter::CodeEmitter( CodeSection& code ) : code( code ) {}
|
|
|
|
|
|
|
|
|
|
unsigned CodeEmitter::append( const StoredToken& st )
|
|
|
|
|
{
|
2020-12-01 02:23:03 -08:00
|
|
|
auto index = next_address();
|
2020-08-11 11:50:16 -07:00
|
|
|
code.push_back( st );
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeEmitter::update_offset( unsigned index, unsigned offset )
|
|
|
|
|
{
|
2021-08-28 22:26:15 +02:00
|
|
|
code[index].offset = static_cast<unsigned short>( offset );
|
2020-08-11 11:50:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned CodeEmitter::next_address() const
|
|
|
|
|
{
|
2020-12-01 02:23:03 -08:00
|
|
|
size_t index = code.size();
|
|
|
|
|
if ( index > std::numeric_limits<unsigned>::max() ) {
|
|
|
|
|
throw std::runtime_error( "Instruction count overflow" );
|
|
|
|
|
}
|
|
|
|
|
return static_cast<unsigned>( index );
|
2020-08-11 11:50:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Pol::Bscript::Compiler
|