2017-11-12 10:50:05 +01:00
|
|
|
#include "externals.hh"
|
|
|
|
#include "control.hh"
|
|
|
|
#include <ebcl/Algorithms.hh>
|
|
|
|
|
|
|
|
#define INVASIVE_TRACES
|
|
|
|
|
|
|
|
using namespace ebcl;
|
|
|
|
using namespace ops;
|
|
|
|
using namespace opast;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct T_CompilerImpl_
|
|
|
|
{
|
2017-11-12 17:01:13 +01:00
|
|
|
P_OpProgram compile(
|
|
|
|
T_ParserOutput const& input ) noexcept;
|
|
|
|
|
|
|
|
private:
|
2017-11-12 10:50:05 +01:00
|
|
|
T_Visitor< A_Node > astVisitor{ ASTVisitorBrowser };
|
|
|
|
T_Set< uint32_t > constants{ UseTag< IndexBacked< > >( ) };
|
2017-11-12 17:01:13 +01:00
|
|
|
T_KeyValueTable< T_String , uint32_t > locations;
|
2017-11-12 10:50:05 +01:00
|
|
|
|
|
|
|
T_ParserOutput* input;
|
|
|
|
P_OpProgram output;
|
|
|
|
|
|
|
|
uint32_t fiVariables , fiFramebuffers ,
|
|
|
|
fiPipelines , fiPrograms ,
|
|
|
|
fiSamplers , fiTextures;
|
|
|
|
|
2017-11-12 17:01:13 +01:00
|
|
|
uint32_t sdMain , sdFPU;
|
|
|
|
T_AutoArray< uint32_t , 32 > condJumps;
|
2017-11-12 10:50:05 +01:00
|
|
|
|
|
|
|
void gatherConstants( ) noexcept;
|
|
|
|
void countAssets( ) noexcept;
|
|
|
|
|
|
|
|
bool compileNode( uint32_t funcIndex ,
|
|
|
|
A_Node& node ,
|
|
|
|
bool exit ) noexcept;
|
2017-11-12 17:01:13 +01:00
|
|
|
void processFunction(
|
|
|
|
bool exit ,
|
|
|
|
uint32_t args ,
|
|
|
|
uint32_t lvars ,
|
|
|
|
T_SRDLocation const& location ) noexcept;
|
|
|
|
bool processIdentifier(
|
|
|
|
uint32_t funcIndex ,
|
|
|
|
T_IdentifierExprNode const& node ) noexcept;
|
|
|
|
|
|
|
|
void addInstruction(
|
|
|
|
E_OpType op ,
|
2017-11-12 17:46:47 +01:00
|
|
|
T_SRDLocation const& location ) noexcept;
|
|
|
|
void addInstruction(
|
|
|
|
E_OpType op ,
|
|
|
|
uint32_t arg0 ,
|
|
|
|
T_SRDLocation const& location ) noexcept;
|
|
|
|
void addInstruction(
|
|
|
|
E_OpType op ,
|
|
|
|
uint32_t arg0 , uint32_t arg1 ,
|
|
|
|
T_SRDLocation const& location ) noexcept;
|
2017-11-12 18:38:31 +01:00
|
|
|
void applyStackEffects(
|
|
|
|
T_Op const& op ) noexcept;
|
2017-11-12 10:50:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
P_OpProgram T_CompilerImpl_::compile(
|
|
|
|
T_ParserOutput const& in ) noexcept
|
|
|
|
{
|
|
|
|
input = const_cast< T_ParserOutput* >( &in );
|
|
|
|
output = NewOwned< T_OpProgram >( );
|
|
|
|
|
|
|
|
// Gather all constants used in expressions, count resources
|
|
|
|
gatherConstants( );
|
|
|
|
countAssets( );
|
|
|
|
|
|
|
|
// Get function indices
|
|
|
|
// FIXME ideally we should remap functions so that init is always 0
|
|
|
|
// and frame is always 1
|
|
|
|
output->init = input->root.functionIndex( "*init*" );
|
|
|
|
output->frame = input->root.functionIndex( "*frame*" );
|
|
|
|
#ifdef INVASIVE_TRACES
|
|
|
|
printf( "function indices\n\t%d\tinit\n\t%d\tframe\n" ,
|
|
|
|
output->init , output->frame );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Compile each function
|
2017-11-12 17:01:13 +01:00
|
|
|
#ifdef INVASIVE_TRACES
|
|
|
|
uint32_t nInstr = 0;
|
|
|
|
#endif
|
2017-11-12 10:50:05 +01:00
|
|
|
uint32_t cfi;
|
|
|
|
for ( cfi = 0u ; cfi < input->root.nFunctions( ) ; cfi ++ ) {
|
|
|
|
output->ops.next( );
|
|
|
|
auto& func( input->root.function( cfi ) );
|
|
|
|
#ifdef INVASIVE_TRACES
|
|
|
|
printf( "compiling function %s\n" ,
|
|
|
|
func.name( ).toOSString( ).data( ) );
|
|
|
|
#endif
|
2017-11-12 17:01:13 +01:00
|
|
|
sdMain = sdFPU = 0;
|
2017-11-12 10:50:05 +01:00
|
|
|
astVisitor.visit( func ,
|
|
|
|
[=]( A_Node& node , const bool exit ) -> bool {
|
|
|
|
return compileNode( cfi , node , exit );
|
|
|
|
} );
|
2017-11-12 17:01:13 +01:00
|
|
|
#ifdef INVASIVE_TRACES
|
2017-11-12 17:46:47 +01:00
|
|
|
T_StringBuilder dump , temp;
|
|
|
|
for ( auto i = 0u ; i < output->ops.sizeOf( cfi ) ; i ++ ) {
|
|
|
|
temp << "(" << output->ops.get( cfi , i ) << ")";
|
|
|
|
while ( temp.length( ) < 30 ) {
|
|
|
|
temp << ' ';
|
|
|
|
}
|
|
|
|
dump << "\t\t" << temp << "{ " << output->ops.get( cfi , i ).location << " }\n";
|
|
|
|
temp.clear( );
|
|
|
|
}
|
|
|
|
dump << '\t' << output->ops.sizeOf( cfi ) << " instructions\n"
|
|
|
|
<< '\0';
|
|
|
|
printf( "%s" , dump.data( ) );
|
2017-11-12 17:01:13 +01:00
|
|
|
nInstr += output->ops.sizeOf( cfi );
|
|
|
|
#endif
|
2017-11-12 10:50:05 +01:00
|
|
|
}
|
2017-11-12 17:01:13 +01:00
|
|
|
#ifdef INVASIVE_TRACES
|
|
|
|
printf( "total %d instructions\n" , nInstr );
|
|
|
|
#endif
|
2017-11-12 10:50:05 +01:00
|
|
|
|
|
|
|
return std::move( output );
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_CompilerImpl_::gatherConstants( ) noexcept
|
|
|
|
{
|
|
|
|
constants.clear( );
|
|
|
|
astVisitor.visit( input->root , [&]( A_Node& node , const bool exit ) {
|
|
|
|
if ( exit && node.type( ) == A_Node::EXPR_CONST ) {
|
|
|
|
T_OpValue value;
|
|
|
|
value.f = dynamic_cast< T_ConstantExprNode& >( node ).floatValue( );
|
2017-11-12 17:01:13 +01:00
|
|
|
constants.add( value.u );
|
2017-11-12 10:50:05 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} );
|
|
|
|
|
|
|
|
#ifdef INVASIVE_TRACES
|
|
|
|
printf( "%d constants\n" , constants.size( ) );
|
|
|
|
for ( auto i = 0u ; i < constants.size( ) ; i ++ ) {
|
|
|
|
printf( " %08x" , constants[ i ] );
|
|
|
|
if ( i % 4 == 3 ) {
|
|
|
|
printf( "\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( constants.size( ) % 4 ) {
|
|
|
|
printf( "\n" );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_CompilerImpl_::countAssets( ) noexcept
|
|
|
|
{
|
2017-11-12 17:01:13 +01:00
|
|
|
locations.clear( );
|
|
|
|
locations.add( T_String::Pooled( "time" ) , 0u );
|
|
|
|
locations.add( T_String::Pooled( "width" ) , 1u );
|
|
|
|
locations.add( T_String::Pooled( "height" ) , 2u );
|
|
|
|
|
2017-11-12 10:50:05 +01:00
|
|
|
auto const nt{ input->types.size( ) };
|
|
|
|
for ( auto i = 0u ; i < nt ; i ++ ) {
|
|
|
|
const auto t{ input->types.values( )[ i ] };
|
2017-11-12 17:01:13 +01:00
|
|
|
auto const& n{ input->types.keys( )[ i ] };
|
2017-11-12 10:50:05 +01:00
|
|
|
switch ( t ) {
|
|
|
|
case E_DataType::FRAMEBUFFER:
|
2017-11-12 17:01:13 +01:00
|
|
|
locations.add( n , output->nFramebuffers );
|
2017-11-12 10:50:05 +01:00
|
|
|
output->nFramebuffers ++;
|
|
|
|
break;
|
|
|
|
case E_DataType::PIPELINE:
|
2017-11-12 17:01:13 +01:00
|
|
|
locations.add( n , output->nPipelines );
|
2017-11-12 10:50:05 +01:00
|
|
|
output->nPipelines ++;
|
|
|
|
break;
|
|
|
|
case E_DataType::PROGRAM:
|
2017-11-12 17:01:13 +01:00
|
|
|
locations.add( n , output->nPrograms );
|
2017-11-12 10:50:05 +01:00
|
|
|
output->nPrograms ++;
|
|
|
|
break;
|
|
|
|
case E_DataType::SAMPLER:
|
2017-11-12 17:01:13 +01:00
|
|
|
locations.add( n , output->nSamplers );
|
2017-11-12 10:50:05 +01:00
|
|
|
output->nSamplers ++;
|
|
|
|
break;
|
|
|
|
case E_DataType::TEXTURE:
|
2017-11-12 17:01:13 +01:00
|
|
|
locations.add( n , output->nTextures );
|
2017-11-12 10:50:05 +01:00
|
|
|
output->nTextures ++;
|
|
|
|
break;
|
|
|
|
case E_DataType::VARIABLE:
|
2017-11-12 17:01:13 +01:00
|
|
|
locations.add( n , output->nVariables + 3 );
|
2017-11-12 10:50:05 +01:00
|
|
|
output->nVariables ++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case E_DataType::INPUT:
|
|
|
|
assert( !output->inputs.contains(
|
|
|
|
input->types.keys( )[ i ] ) );
|
|
|
|
output->inputs.add( input->types.keys( )[ i ] );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case E_DataType::BUILTIN:
|
|
|
|
case E_DataType::UNKNOWN:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fiVariables = 3 + constants.size( );
|
|
|
|
fiFramebuffers = fiVariables + output->nVariables;
|
|
|
|
fiPipelines = fiFramebuffers + output->nFramebuffers;
|
|
|
|
fiPrograms = fiPipelines + output->nPipelines;
|
|
|
|
fiSamplers = fiPrograms + output->nPrograms;
|
|
|
|
fiTextures = fiSamplers + output->nSamplers;
|
|
|
|
|
2017-11-12 17:01:13 +01:00
|
|
|
for ( auto i = 0u ; i < nt ; i ++ ) {
|
|
|
|
const auto li{ locations.indexOf( input->types.keys( )[ i ] ) };
|
|
|
|
if ( li == T_HashIndex::INVALID_INDEX ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto t{ input->types.values( )[ i ] };
|
|
|
|
auto& pos{ locations[ li ] };
|
|
|
|
switch ( t ) {
|
|
|
|
case E_DataType::FRAMEBUFFER: pos += fiFramebuffers; break;
|
|
|
|
case E_DataType::PIPELINE: pos += fiPipelines; break;
|
|
|
|
case E_DataType::PROGRAM: pos += fiPrograms; break;
|
|
|
|
case E_DataType::SAMPLER: pos += fiSamplers; break;
|
|
|
|
case E_DataType::TEXTURE: pos += fiTextures; break;
|
|
|
|
|
|
|
|
case E_DataType::VARIABLE:
|
|
|
|
case E_DataType::INPUT:
|
|
|
|
case E_DataType::BUILTIN:
|
|
|
|
case E_DataType::UNKNOWN:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-12 10:50:05 +01:00
|
|
|
#ifdef INVASIVE_TRACES
|
|
|
|
printf( "assets\n\t%d framebuffers\n\t%d pipelines\n"
|
|
|
|
"\t%d programs\n\t%d samplers\n\t%d textures\n"
|
|
|
|
"\t%d variables\n\t%d inputs\n" ,
|
|
|
|
output->nFramebuffers , output->nPipelines ,
|
|
|
|
output->nPrograms , output->nSamplers ,
|
|
|
|
output->nTextures , output->nVariables ,
|
|
|
|
output->inputs.size( ) );
|
|
|
|
printf( "table ranges\n\t0\t2\tBuilt-ins\n"
|
|
|
|
"\t3\t%d\tConstants\n"
|
|
|
|
"\t%d\t%d\tVariables\n"
|
|
|
|
"\t%d\t%d\tFramebuffers\n"
|
|
|
|
"\t%d\t%d\tPipelines\n"
|
|
|
|
"\t%d\t%d\tPrograms\n"
|
|
|
|
"\t%d\t%d\tSamplers\n"
|
|
|
|
"\t%d\t%d\tTextures\n" ,
|
|
|
|
fiVariables - 1 , fiVariables , fiFramebuffers - 1 , fiFramebuffers ,
|
|
|
|
fiPipelines - 1 , fiPipelines , fiPrograms - 1 , fiPrograms ,
|
|
|
|
fiSamplers - 1 , fiSamplers , fiTextures - 1 , fiTextures ,
|
|
|
|
fiTextures + output->nTextures - 1 );
|
2017-11-12 17:01:13 +01:00
|
|
|
|
|
|
|
T_Array< uint32_t > indices( locations.size( ) );
|
|
|
|
indices.ensureCapacity( locations.size( ) );
|
|
|
|
for ( auto i = 0u ; i < locations.size( ) ; i ++ ) {
|
|
|
|
indices.add( i );
|
|
|
|
}
|
|
|
|
indices.sort( [this]( uint32_t a , uint32_t b ) {
|
|
|
|
return T_Comparator< uint32_t >::compare(
|
|
|
|
locations.values( )[ a ] ,
|
|
|
|
locations.values( )[ b ] );
|
|
|
|
} );
|
|
|
|
T_StringBuilder lmap;
|
|
|
|
lmap << "location map (constants not included)\n";
|
|
|
|
for ( auto idx : indices ) {
|
|
|
|
lmap << '\t' << locations.values( )[ idx ]
|
|
|
|
<< '\t' << locations.keys( )[ idx ]
|
|
|
|
<< '\n';
|
|
|
|
}
|
|
|
|
lmap << '\0';
|
|
|
|
printf( "%s" , lmap.data( ) );
|
2017-11-12 10:50:05 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool T_CompilerImpl_::compileNode(
|
|
|
|
const uint32_t funcIndex ,
|
|
|
|
A_Node& node ,
|
|
|
|
const bool exit ) noexcept
|
|
|
|
{
|
2017-11-12 17:01:13 +01:00
|
|
|
switch ( node.type( ) ) {
|
|
|
|
|
|
|
|
case A_Node::ROOT:
|
|
|
|
fprintf( stderr , "Internal error: root node found during compilation\n" );
|
|
|
|
std::abort( );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case A_Node::DECL_FN:
|
|
|
|
{
|
|
|
|
T_FuncNode& fn( (T_FuncNode&) node );
|
|
|
|
const auto args( fn.arguments( ) );
|
|
|
|
processFunction( exit , args , fn.locals( ) - args , node.location( ) );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case A_Node::DECL_INIT:
|
|
|
|
case A_Node::DECL_FRAME:
|
|
|
|
{
|
|
|
|
A_FuncNode& fn( (A_FuncNode&) node );
|
|
|
|
processFunction( exit , 0 , fn.locals( ) , node.location( ) );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case A_Node::OP_CALL:
|
|
|
|
if ( exit ) {
|
|
|
|
auto& call( (T_CallInstrNode&) node );
|
|
|
|
const auto fi( input->root.functionIndex( call.id( ) ) );
|
|
|
|
assert( fi >= 0 );
|
|
|
|
auto& callee( input->root.function( fi ) );
|
|
|
|
assert( callee.type( ) == A_Node::DECL_FN );
|
|
|
|
auto& fcallee( (T_FuncNode&) callee );
|
|
|
|
const auto args( fcallee.arguments( ) );
|
|
|
|
assert( sdMain > args );
|
2017-11-12 17:46:47 +01:00
|
|
|
addInstruction( OP_CALL , fi , node.location( ) );
|
2017-11-12 17:01:13 +01:00
|
|
|
sdMain -= args;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case A_Node::TN_ARG:
|
|
|
|
{
|
|
|
|
auto& n( (T_CallInstrNode::T_Argument&)node );
|
|
|
|
if ( n.isIdentifier( ) && !exit ) {
|
|
|
|
const bool main{ processIdentifier( funcIndex ,
|
|
|
|
(T_IdentifierExprNode&) n.expression( ) ) };
|
|
|
|
if ( !main ) {
|
|
|
|
addInstruction( OP_PUSH , node.location( ) );
|
2017-11-12 17:46:47 +01:00
|
|
|
addInstruction( OP_FP_SSTORE , 0 , node.location( ) );
|
2017-11-12 17:01:13 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else if ( exit && !n.isIdentifier( ) ) {
|
|
|
|
addInstruction( OP_PUSH , node.location( ) );
|
2017-11-12 17:46:47 +01:00
|
|
|
addInstruction( OP_FP_SSTORE , 0 , node.location( ) );
|
2017-11-12 17:01:13 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case A_Node::OP_SET:
|
|
|
|
if ( exit ) {
|
|
|
|
auto& id( ((T_SetInstrNode&)node).id( ) );
|
|
|
|
auto& func{ input->root.function( funcIndex ) };
|
|
|
|
if ( func.hasLocal( id ) ) {
|
|
|
|
const auto pos( func.getLocalIndex( id ) + 1 );
|
2017-11-12 17:46:47 +01:00
|
|
|
addInstruction( OP_FP_SSTORE , sdMain - pos - 1 ,
|
|
|
|
node.location( ) );
|
2017-11-12 17:01:13 +01:00
|
|
|
} else {
|
2017-11-12 17:46:47 +01:00
|
|
|
addInstruction( OP_FP_STORE ,
|
|
|
|
*locations.get( ((T_SetInstrNode&)node).id( ) ) ,
|
|
|
|
node.location( ) );
|
2017-11-12 17:01:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case A_Node::OP_COND:
|
|
|
|
if ( exit ) {
|
2017-11-12 17:46:47 +01:00
|
|
|
addInstruction( OP_POP , 0 , node.location( ) );
|
2017-11-12 17:01:13 +01:00
|
|
|
sdMain --;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case A_Node::TN_CONDITION:
|
|
|
|
if ( exit ) {
|
|
|
|
addInstruction( OP_PUSH , node.location( ) );
|
2017-11-12 17:46:47 +01:00
|
|
|
addInstruction( OP_FP_SSTORE_INT , 0 , node.location( ) );
|
2017-11-12 17:01:13 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case A_Node::TN_CASE:
|
|
|
|
if ( exit ) {
|
|
|
|
const auto cpos( output->ops.sizeOf( funcIndex ) );
|
|
|
|
const auto diff( cpos - condJumps.last( ) );
|
|
|
|
output->ops.get( funcIndex , condJumps.last( ) ).arg1 = diff - 1;
|
|
|
|
condJumps.removeLast( );
|
|
|
|
#ifdef INVASIVE_TRACES
|
|
|
|
printf( "\tCOND JUMP UPDATED: %d\n" , diff - 1 );
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
auto& c( (T_CondInstrNode::T_ValuedCase&) node );
|
|
|
|
condJumps.add( output->ops.sizeOf( funcIndex ) );
|
2017-11-12 17:46:47 +01:00
|
|
|
addInstruction( OP_COND_JUMP , 0 , c.value( ) ,
|
|
|
|
node.location( ) );
|
2017-11-12 17:01:13 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case A_Node::EXPR_CMP_EQ: case A_Node::EXPR_CMP_NE:
|
|
|
|
case A_Node::EXPR_CMP_GT: case A_Node::EXPR_CMP_GE:
|
|
|
|
case A_Node::EXPR_CMP_LT: case A_Node::EXPR_CMP_LE:
|
|
|
|
if ( exit ) {
|
|
|
|
const uint32_t op( dynamic_cast< T_BinaryOperatorNode& >( node ).op( )
|
|
|
|
- T_BinaryOperatorNode::CMP_EQ );
|
2017-11-12 17:46:47 +01:00
|
|
|
addInstruction( OP_FP_CMP , op , node.location( ) );
|
2017-11-12 17:01:13 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case A_Node::EXPR_ADD:
|
|
|
|
if ( exit ) {
|
|
|
|
addInstruction( OP_FP_ADD , node.location( ) );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case A_Node::EXPR_SUB:
|
|
|
|
if ( exit ) {
|
|
|
|
addInstruction( OP_FP_SUB , node.location( ) );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case A_Node::EXPR_MUL:
|
|
|
|
if ( exit ) {
|
|
|
|
addInstruction( OP_FP_MUL , node.location( ) );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case A_Node::EXPR_DIV:
|
|
|
|
if ( exit ) {
|
|
|
|
addInstruction( OP_FP_DIV , node.location( ) );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case A_Node::EXPR_CONST:
|
|
|
|
if ( !exit ) {
|
|
|
|
T_OpValue value;
|
|
|
|
value.f = dynamic_cast< T_ConstantExprNode& >( node ).floatValue( );
|
2017-11-12 17:46:47 +01:00
|
|
|
addInstruction( OP_FP_LOAD ,
|
|
|
|
constants.indexOf( value.u ) + 3 ,
|
|
|
|
node.location( ) );
|
2017-11-12 17:01:13 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case A_Node::EXPR_ID:
|
|
|
|
if ( !exit ) {
|
|
|
|
processIdentifier( funcIndex ,
|
|
|
|
dynamic_cast< T_IdentifierExprNode& >( node ) );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_CompilerImpl_::processFunction(
|
|
|
|
const bool exit ,
|
|
|
|
const uint32_t args ,
|
|
|
|
const uint32_t lvars ,
|
|
|
|
T_SRDLocation const& location ) noexcept
|
|
|
|
{
|
|
|
|
if ( exit ) {
|
|
|
|
assert( sdMain == args + lvars + 1 );
|
|
|
|
if ( lvars ) {
|
2017-11-12 17:46:47 +01:00
|
|
|
addInstruction( OP_POP , lvars - 1 , location );
|
2017-11-12 17:01:13 +01:00
|
|
|
sdMain -= lvars;
|
|
|
|
}
|
2017-11-12 18:38:31 +01:00
|
|
|
sdMain -= args;
|
2017-11-12 17:46:47 +01:00
|
|
|
addInstruction( OP_RET , args , location );
|
2017-11-12 17:01:13 +01:00
|
|
|
assert( sdMain == 0 );
|
|
|
|
} else {
|
|
|
|
if ( lvars ) {
|
2017-11-12 17:46:47 +01:00
|
|
|
addInstruction( OP_RES_STACK , lvars - 1 , location );
|
2017-11-12 17:01:13 +01:00
|
|
|
sdMain += lvars;
|
|
|
|
}
|
|
|
|
sdMain += 1 + args;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the identifier caused a push to the main stack, false
|
|
|
|
// if it pushed to the FPU stack.
|
|
|
|
bool T_CompilerImpl_::processIdentifier(
|
|
|
|
const uint32_t funcIndex ,
|
|
|
|
T_IdentifierExprNode const& node ) noexcept
|
|
|
|
{
|
|
|
|
auto& func{ input->root.function( funcIndex ) };
|
|
|
|
if ( func.hasLocal( node.id( ) ) ) {
|
|
|
|
const E_DataType dt{ func.getLocalType( node.id( ) ) };
|
|
|
|
assert( dt != E_DataType::UNKNOWN );
|
|
|
|
|
|
|
|
uint32_t stackPos;
|
|
|
|
if ( func.isArgument( node.id( ) ) ) {
|
|
|
|
auto const& fn( (T_FuncNode&) func );
|
|
|
|
const auto nArgs( fn.arguments( ) );
|
|
|
|
stackPos = nArgs - 1 - func.getLocalIndex( node.id( ) );
|
|
|
|
} else {
|
|
|
|
stackPos = func.getLocalIndex( node.id( ) ) + 1;
|
|
|
|
}
|
|
|
|
assert( stackPos < sdMain );
|
|
|
|
|
|
|
|
const auto p( sdMain - ( stackPos + 1 ) );
|
|
|
|
if ( dt == E_DataType::VARIABLE ) {
|
2017-11-12 17:46:47 +01:00
|
|
|
addInstruction( OP_FP_SLOAD , p , node.location( ) );
|
2017-11-12 17:01:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-12 17:46:47 +01:00
|
|
|
addInstruction( OP_SLOAD , p , node.location( ) );
|
2017-11-12 17:01:13 +01:00
|
|
|
addInstruction( OP_PUSH , node.location( ) );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert( input->types.contains( node.id( ) ) );
|
|
|
|
const E_DataType dt{ *( input->types.get( node.id( ) ) ) };
|
|
|
|
assert( dt != E_DataType::UNKNOWN );
|
|
|
|
assert( locations.contains( node.id( ) ) );
|
|
|
|
if ( dt == E_DataType::VARIABLE || dt == E_DataType::BUILTIN ) {
|
2017-11-12 17:46:47 +01:00
|
|
|
addInstruction( OP_FP_LOAD , *locations.get( node.id( ) ) ,
|
|
|
|
node.location( ) );
|
2017-11-12 17:01:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-12 17:46:47 +01:00
|
|
|
addInstruction( OP_LOAD , *locations.get( node.id( ) ) ,
|
|
|
|
node.location( ) );
|
2017-11-12 17:01:13 +01:00
|
|
|
addInstruction( OP_PUSH , node.location( ) );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void T_CompilerImpl_::addInstruction(
|
|
|
|
const E_OpType op ,
|
2017-11-12 17:46:47 +01:00
|
|
|
T_SRDLocation const& location ) noexcept
|
|
|
|
{
|
|
|
|
assert( ArgumentsFor( op ) == 0 );
|
2017-11-12 18:38:31 +01:00
|
|
|
applyStackEffects( output->ops.addNew( op , location ) );
|
2017-11-12 17:46:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void T_CompilerImpl_::addInstruction(
|
|
|
|
const E_OpType op ,
|
2017-11-12 17:01:13 +01:00
|
|
|
const uint32_t arg0 ,
|
2017-11-12 17:46:47 +01:00
|
|
|
T_SRDLocation const& location ) noexcept
|
2017-11-12 17:01:13 +01:00
|
|
|
{
|
2017-11-12 17:46:47 +01:00
|
|
|
assert( ArgumentsFor( op ) == 1 );
|
2017-11-12 18:38:31 +01:00
|
|
|
applyStackEffects( output->ops.addNew( op , location , arg0 ) );
|
2017-11-12 17:46:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void T_CompilerImpl_::addInstruction(
|
|
|
|
const E_OpType op ,
|
|
|
|
const uint32_t arg0 ,
|
|
|
|
const uint32_t arg1 ,
|
|
|
|
T_SRDLocation const& location ) noexcept
|
|
|
|
{
|
|
|
|
assert( ArgumentsFor( op ) == 2 );
|
2017-11-12 18:38:31 +01:00
|
|
|
applyStackEffects( output->ops.addNew( op , location , arg0 , arg1 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_CompilerImpl_::applyStackEffects(
|
|
|
|
T_Op const& op ) noexcept
|
|
|
|
{
|
|
|
|
const auto m( DeltaMainStack( op.op ) );
|
|
|
|
const auto f( DeltaFPUStack( op.op ) );
|
|
|
|
|
|
|
|
#ifdef INVASIVE_TRACES
|
|
|
|
T_StringBuilder sb;
|
|
|
|
sb << "applying stack effects for (" << op << ") - sdMain " << sdMain
|
|
|
|
<< " (" << m << ") sdFPU " << sdFPU << " (" << f << ")" << '\n' << '\0';
|
|
|
|
printf( "%s" , sb.data( ) );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if ( m ) {
|
|
|
|
assert( m > 0 || sdMain >= uint32_t( -m ) );
|
|
|
|
sdMain += m;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( f ) {
|
|
|
|
assert( f > 0 || sdFPU >= uint32_t( -m ) );
|
|
|
|
sdFPU += m;
|
|
|
|
}
|
2017-11-12 10:50:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*= T_Compiler =================================================================*/
|
|
|
|
|
|
|
|
T_Compiler::T_Compiler( ) noexcept
|
|
|
|
: A_PrivateImplementation( new T_CompilerImpl_( ) )
|
|
|
|
{ }
|
|
|
|
|
|
|
|
P_OpProgram T_Compiler::compile(
|
|
|
|
T_ParserOutput const& input ) noexcept
|
|
|
|
{
|
|
|
|
return p< T_CompilerImpl_ >( ).compile( input );
|
|
|
|
}
|