Don't store context in the uCode. (It's not thread-safe.)

Although it does give some pretty funny results when filling zones.
This commit is contained in:
Jeff Young 2020-07-23 21:47:38 +01:00
parent b60303efdb
commit 6529e339a9
6 changed files with 62 additions and 50 deletions

View File

@ -223,7 +223,7 @@ void COMPILER::parseOk()
}
bool COMPILER::Compile( const std::string& aString, UCODE* aCode )
bool COMPILER::Compile( const std::string& aString, UCODE* aCode, CONTEXT* aPreflightContext )
{
// Feed parser token after token until end of input.
@ -238,7 +238,7 @@ bool COMPILER::Compile( const std::string& aString, UCODE* aCode )
if( aString.empty() )
{
m_parseFinished = true;
return generateUCode( aCode );
return generateUCode( aCode, aPreflightContext );
}
do
@ -265,7 +265,7 @@ bool COMPILER::Compile( const std::string& aString, UCODE* aCode )
}
} while( tok.token );
return generateUCode( aCode );
return generateUCode( aCode, aPreflightContext );
}
@ -592,7 +592,7 @@ void COMPILER::setRoot( TREE_NODE root )
}
bool COMPILER::generateUCode( UCODE* aCode )
bool COMPILER::generateUCode( UCODE* aCode, CONTEXT* aPreflightContext )
{
std::vector<TREE_NODE*> stack;
std::set<TREE_NODE*> visitedNodes;
@ -689,22 +689,22 @@ bool COMPILER::generateUCode( UCODE* aCode )
}
// Preflight the function call
CONTEXT ctx;
VALUE* param = ctx.AllocValue();
VALUE* param = aPreflightContext->AllocValue();
param->Set( node->value.str );
ctx.Push( param );
aPreflightContext->Push( param );
try
{
func( aCode, &ctx, vref );
func( aPreflightContext, vref );
aPreflightContext->Pop(); // return value
}
catch( ... )
{
}
if( ctx.GetErrorStatus().pendingError )
if( aPreflightContext->GetErrorStatus().pendingError )
{
m_errorStatus = ctx.GetErrorStatus();
m_errorStatus = aPreflightContext->GetErrorStatus();
m_errorStatus.stage = ERROR_STATUS::CST_CODEGEN;
m_errorStatus.srcPos = node->leaf[1]->leaf[0]->srcPos + 1;
return false;
@ -780,14 +780,14 @@ bool COMPILER::generateUCode( UCODE* aCode )
}
void UOP::Exec( CONTEXT* ctx, UCODE* ucode )
void UOP::Exec( CONTEXT* ctx )
{
switch( m_op )
{
case TR_UOP_PUSH_VAR:
{
auto value = ctx->AllocValue();
value->Set( reinterpret_cast<VAR_REF*>( m_arg )->GetValue( ucode ) );
value->Set( reinterpret_cast<VAR_REF*>( m_arg )->GetValue( ctx ) );
ctx->Push( value );
}
break;
@ -798,7 +798,7 @@ void UOP::Exec( CONTEXT* ctx, UCODE* ucode )
case TR_OP_METHOD_CALL:
//printf("CALL METHOD %s\n" );
m_func( ucode, ctx, m_arg );
m_func( ctx, m_arg );
return;
default:
@ -866,15 +866,13 @@ void UOP::Exec( CONTEXT* ctx, UCODE* ucode )
}
VALUE* UCODE::Run()
VALUE* UCODE::Run( CONTEXT* ctx )
{
CONTEXT ctx;
for( UOP* op : m_ucode )
op->Exec( &ctx, this );
op->Exec( ctx );
assert( ctx.SP() == 1 );
return ctx.Pop();
assert( ctx->SP() == 1 );
return ctx->Pop();
}

View File

@ -249,7 +249,7 @@ class VAR_REF
{
public:
virtual VAR_TYPE_T GetType() = 0;
virtual VALUE GetValue( UCODE* aUcode ) = 0;
virtual VALUE GetValue( CONTEXT* aCtx ) = 0;
};
@ -304,14 +304,14 @@ private:
class UCODE
{
public:
typedef std::function<void( UCODE*, CONTEXT*, void* )> FUNC_PTR;
typedef std::function<void( CONTEXT*, void* )> FUNC_PTR;
void AddOp( UOP* uop )
{
m_ucode.push_back(uop);
}
VALUE* Run();
VALUE* Run( CONTEXT* ctx );
std::string Dump() const;
virtual VAR_REF* createVarRef( COMPILER* aCompiler, const std::string& var,
@ -344,7 +344,7 @@ public:
m_func( std::move( func ) )
{};
void Exec( CONTEXT* ctx, UCODE *ucode );
void Exec( CONTEXT* ctx );
std::string Format() const;
@ -428,7 +428,7 @@ public:
void setRoot( LIBEVAL::TREE_NODE root );
bool Compile( const std::string& aString, UCODE* aCode );
bool Compile( const std::string& aString, UCODE* aCode, CONTEXT* aPreflightContext );
void ReportError( const wxString& aErrorMsg );
ERROR_STATUS GetErrorStatus() const { return m_errorStatus; }
@ -441,7 +441,7 @@ protected:
LEXER_STATE m_lexerState;
bool generateUCode( UCODE* aCode );
bool generateUCode( UCODE* aCode, CONTEXT* aPreflightContext );
/* Token type used by the tokenizer */

View File

@ -89,6 +89,9 @@ DRC_RULE* GetRule( const BOARD_ITEM* aItem, const BOARD_ITEM* bItem, int aConstr
{
if( rule->m_Condition.EvaluateFor( aItem, bItem ) )
return rule;
if( bItem && rule->m_Condition.EvaluateFor( bItem, aItem ) )
return rule;
}
}
@ -113,9 +116,10 @@ bool DRC_RULE_CONDITION::EvaluateFor( const BOARD_ITEM* aItemA, const BOARD_ITEM
BOARD_ITEM* a = const_cast<BOARD_ITEM*>( aItemA );
BOARD_ITEM* b = aItemB ? const_cast<BOARD_ITEM*>( aItemB ) : DELETED_BOARD_ITEM::GetInstance();
m_ucode->SetItems( a, b );
PCB_EXPR_CONTEXT ctx;
ctx.SetItems( a, b );
return m_ucode->Run()->AsDouble() != 0.0;
return m_ucode->Run( &ctx )->AsDouble() != 0.0;
}
@ -126,7 +130,9 @@ bool DRC_RULE_CONDITION::Compile()
if (!m_ucode)
m_ucode = new PCB_EXPR_UCODE;
bool ok = compiler.Compile( (const char*) m_Expression.c_str(), m_ucode );
PCB_EXPR_CONTEXT preflightContext;
bool ok = compiler.Compile( (const char*) m_Expression.c_str(), m_ucode, &preflightContext );
if( ok )
return true;

View File

@ -30,7 +30,7 @@
static void onLayer( LIBEVAL::UCODE* aUcode, LIBEVAL::CONTEXT* aCtx, void *self )
static void onLayer( LIBEVAL::CONTEXT* aCtx, void *self )
{
LIBEVAL::VALUE* arg = aCtx->Pop();
LIBEVAL::VALUE* result = aCtx->AllocValue();
@ -54,14 +54,14 @@ static void onLayer( LIBEVAL::UCODE* aUcode, LIBEVAL::CONTEXT* aCtx, void *self
}
PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
BOARD_ITEM* item = vref ? vref->GetObject( aUcode ) : nullptr;
BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
if( item && item->IsOnLayer( layer ) )
result->Set( 1.0 );
}
static void isPlated( LIBEVAL::UCODE* aUcode, LIBEVAL::CONTEXT* aCtx, void *self )
static void isPlated( LIBEVAL::CONTEXT* aCtx, void *self )
{
LIBEVAL::VALUE* result = aCtx->AllocValue();
@ -69,7 +69,7 @@ static void isPlated( LIBEVAL::UCODE* aUcode, LIBEVAL::CONTEXT* aCtx, void *self
aCtx->Push( result );
PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
BOARD_ITEM* item = vref ? vref->GetObject( aUcode ) : nullptr;
BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
D_PAD* pad = dynamic_cast<D_PAD*>( item );
if( pad && pad->GetAttribute() == PAD_ATTRIB_STANDARD )
@ -91,17 +91,17 @@ PCB_EXPR_BUILTIN_FUNCTIONS::PCB_EXPR_BUILTIN_FUNCTIONS()
}
BOARD_ITEM* PCB_EXPR_VAR_REF::GetObject( LIBEVAL::UCODE* aUcode ) const
BOARD_ITEM* PCB_EXPR_VAR_REF::GetObject( LIBEVAL::CONTEXT* aCtx ) const
{
const PCB_EXPR_UCODE* ucode = static_cast<const PCB_EXPR_UCODE*>( aUcode );
BOARD_ITEM* item = ucode->GetItem( m_itemIndex );
const PCB_EXPR_CONTEXT* ctx = static_cast<const PCB_EXPR_CONTEXT*>( aCtx );
BOARD_ITEM* item = ctx->GetItem( m_itemIndex );
return item;
}
LIBEVAL::VALUE PCB_EXPR_VAR_REF::GetValue( LIBEVAL::UCODE* aUcode )
LIBEVAL::VALUE PCB_EXPR_VAR_REF::GetValue( LIBEVAL::CONTEXT* aCtx )
{
BOARD_ITEM* item = const_cast<BOARD_ITEM*>( GetObject( aUcode ) );
BOARD_ITEM* item = const_cast<BOARD_ITEM*>( GetObject( aCtx ) );
auto it = m_matchingTypes.find( TYPE_HASH( *item ) );
if( it == m_matchingTypes.end() )
@ -264,16 +264,18 @@ PCB_EXPR_EVALUATOR::~PCB_EXPR_EVALUATOR()
bool PCB_EXPR_EVALUATOR::Evaluate( const wxString& aExpr )
{
PCB_EXPR_UCODE ucode;
PCB_EXPR_UCODE ucode;
LIBEVAL::CONTEXT preflightContext;
if( !m_compiler.Compile( (const char*) aExpr.c_str(), &ucode ) )
if( !m_compiler.Compile( (const char*) aExpr.c_str(), &ucode, &preflightContext ) )
{
m_errorStatus = m_compiler.GetErrorStatus();
return false;
}
// fixme: handle error conditions
LIBEVAL::VALUE* result = ucode.Run();
LIBEVAL::CONTEXT ctx;
LIBEVAL::VALUE* result = ucode.Run( &ctx );
if( result->GetType() == LIBEVAL::VT_NUMERIC )
m_result = KiROUND( result->AsDouble() );

View File

@ -40,18 +40,23 @@ class PCB_EXPR_VAR_REF;
class PCB_EXPR_UCODE : public LIBEVAL::UCODE
{
public:
PCB_EXPR_UCODE()
{
m_items[0] = nullptr;
m_items[1] = nullptr;
}
virtual LIBEVAL::VAR_REF* createVarRef( LIBEVAL::COMPILER *aCompiler,
const std::string& aVar,
const std::string& aField ) override;
virtual FUNC_PTR createFuncCall( LIBEVAL::COMPILER* aCompiler,
const std::string& name ) override;
};
class PCB_EXPR_CONTEXT : public LIBEVAL::CONTEXT
{
public:
PCB_EXPR_CONTEXT()
{
m_items[0] = nullptr;
m_items[1] = nullptr;
}
void SetItems( BOARD_ITEM* a, BOARD_ITEM* b = nullptr )
{
@ -91,9 +96,9 @@ public:
m_matchingTypes[type_hash] = prop;
}
virtual LIBEVAL::VALUE GetValue( LIBEVAL::UCODE* aUcode ) override;
virtual LIBEVAL::VALUE GetValue( LIBEVAL::CONTEXT* aCtx ) override;
BOARD_ITEM* GetObject( LIBEVAL::UCODE* aUcode ) const;
BOARD_ITEM* GetObject( LIBEVAL::CONTEXT* aCtx ) const;
private:
std::unordered_map<TYPE_ID, PROPERTY_BASE*> m_matchingTypes;

View File

@ -56,9 +56,10 @@ bool test::DRC_RULE_CONDITION::EvaluateFor( const BOARD_ITEM* aItemA, const BOAR
BOARD_ITEM* a = const_cast<BOARD_ITEM*>( aItemA );
BOARD_ITEM* b = aItemB ? const_cast<BOARD_ITEM*>( aItemB ) : DELETED_BOARD_ITEM::GetInstance();
m_ucode->SetItems( a, b );
PCB_EXPR_CONTEXT ctx;
ctx.SetItems( a, b );
return m_ucode->Run()->AsDouble() != 0.0;
return m_ucode->Run( &ctx )->AsDouble() != 0.0;
}