demotool/m-builder.cc

164 lines
3.7 KiB
C++
Raw Normal View History

#include "externals.hh"
2017-12-02 14:59:20 +01:00
#include "c-opast.hh"
#include "c-ops.hh"
#include "c-opcomp.hh"
2017-12-02 12:18:31 +01:00
#include "c-opopt.hh"
2017-12-02 14:59:20 +01:00
#include <getopt.h>
#include <ebcl/Files.hh>
#include <ebcl/SRDText.hh>
#include <ebcl/Algorithms.hh>
using namespace ebcl;
using namespace opast;
namespace {
2017-12-02 14:59:20 +01:00
const struct option CmdLineOpts_[] = {
{ "log-level" , required_argument , 0 , 'L' } ,
{ "source-path" , required_argument , 0 , 's' } ,
{ "config" , required_argument , 0 , 'c' } ,
{ 0 , 0 , 0 , 0 }
};
2017-11-07 13:24:01 +01:00
/*============================================================================*/
void PrintStreamError(
char const* const prefix ,
T_String const& name ,
X_StreamError const& error )
{
T_StringBuilder sb;
sb << prefix << " '" << name << "': " << error.what( );
if ( error.code( ) == E_StreamError::SYSTEM_ERROR ) {
sb << " (error code " << error.systemError( ) << ")";
}
sb << '\n' << '\0';
fprintf( stderr , "%s" , sb.data( ) );
}
void WriteSRDError(
T_StringBuilder& sb ,
T_SRDError const& error )
{
sb << error.location( ) << " - " << error.error( ) << "\n";
}
/*============================================================================*/
} // namespace
int main( int argc , char** argv )
{
2017-12-02 14:59:20 +01:00
// Parse command line
T_Optional< uint32_t > oLogLevel;
T_String oSrcPath;
int c;
while ( ( c = getopt_long( argc , argv , "L:s:c:" , CmdLineOpts_ , nullptr ) ) != -1 ) {
switch ( c ) {
case 'L':
oLogLevel = uint32_t( atoi( optarg ) );
break;
case 's':
if ( oSrcPath ) {
fprintf( stderr , "Duplicate source path\n" );
exit( EXIT_FAILURE );
}
oSrcPath = optarg;
break;
case 'c':
fprintf( stderr , "-c/--config option ignored for now\n" );
break;
}
}
// Logger setup
const uint32_t logLevel{ oLogLevel ? *oLogLevel : 0 };
2017-12-02 12:18:31 +01:00
const auto logger{ [=]( F_OPGenLog func , uint32_t level ) {
if ( level > logLevel ) {
return;
}
auto sb{ func( ) };
sb << '\0';
printf( "LOG{%d} %s\n" , level , sb.data( ) );
} };
// Open file
2017-12-02 14:59:20 +01:00
const T_String inputName( [&]() {
T_StringBuilder sb;
if ( oSrcPath ) {
sb << oSrcPath;
if ( !oSrcPath.endsWith( "/" ) ) {
sb << '/';
}
}
sb << "demo.srd";
return T_String{ std::move( sb ) };
}( ) );
T_File input( inputName , E_FileMode::READ_ONLY );
try {
input.open( );
} catch ( X_StreamError const& e ) {
PrintStreamError( "Could not open" , inputName , e );
return 1;
}
// Load SRD data
T_SRDMemoryTarget srdOut;
srdOut.clearComments( true ).clearFlushToken( true );
try {
T_SRDTextReader srdReader{ srdOut };
T_FileInputStream fis{ input };
srdReader.read( inputName , fis );
} catch ( X_StreamError const& e ) {
PrintStreamError( "Could not open" , inputName , e );
return 1;
} catch ( X_SRDErrors const& e ) {
T_StringBuilder sb;
const auto nErrors( e.errors.size( ) );
for ( auto i = 0u ; i < nErrors ; i ++ ) {
WriteSRDError( sb , e.errors[ i ] );
}
sb << "No parsing happened due to format errors\n" << '\0';
fprintf( stderr , "%s" , sb.data( ) );
return 2;
}
2017-12-02 12:18:31 +01:00
// Parse
2017-11-15 18:44:34 +01:00
T_OpsParser parser;
2017-12-02 12:18:31 +01:00
parser.setLogger( logger );
if ( !parser.parse( srdOut.list( ) ) ) {
T_StringBuilder sb;
for ( auto const& err : parser.errors( ) ) {
WriteSRDError( sb , err );
}
sb << "Parser failed\n" << '\0';
fprintf( stderr , "%s" , sb.data( ) );
return 3;
}
2017-12-02 12:18:31 +01:00
auto parsed{ parser.result( ) };
// Optimize
opopt::T_OptData od;
od.logger = logger;
//od.fixedSize = std::make_pair( 1280u , 720u );
bool doneStuff{ false };
do {
doneStuff = opopt::FoldConstants( *parsed , od );
doneStuff = opopt::RemoveDeadCode( *parsed , od ) || doneStuff;
} while ( doneStuff );
// Compile
T_OpsCompiler compiler;
compiler.compile( *parsed );
return 0;
2017-11-06 10:33:18 +01:00
}