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. // 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() ) if( aString.empty() )
{ {
m_parseFinished = true; m_parseFinished = true;
return generateUCode( aCode ); return generateUCode( aCode, aPreflightContext );
} }
do do
@ -265,7 +265,7 @@ bool COMPILER::Compile( const std::string& aString, UCODE* aCode )
} }
} while( tok.token ); } 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::vector<TREE_NODE*> stack;
std::set<TREE_NODE*> visitedNodes; std::set<TREE_NODE*> visitedNodes;
@ -689,22 +689,22 @@ bool COMPILER::generateUCode( UCODE* aCode )
} }
// Preflight the function call // Preflight the function call
CONTEXT ctx; VALUE* param = aPreflightContext->AllocValue();
VALUE* param = ctx.AllocValue();
param->Set( node->value.str ); param->Set( node->value.str );
ctx.Push( param ); aPreflightContext->Push( param );
try try
{ {
func( aCode, &ctx, vref ); func( aPreflightContext, vref );
aPreflightContext->Pop(); // return value
} }
catch( ... ) 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.stage = ERROR_STATUS::CST_CODEGEN;
m_errorStatus.srcPos = node->leaf[1]->leaf[0]->srcPos + 1; m_errorStatus.srcPos = node->leaf[1]->leaf[0]->srcPos + 1;
return false; 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 ) switch( m_op )
{ {
case TR_UOP_PUSH_VAR: case TR_UOP_PUSH_VAR:
{ {
auto value = ctx->AllocValue(); 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 ); ctx->Push( value );
} }
break; break;
@ -798,7 +798,7 @@ void UOP::Exec( CONTEXT* ctx, UCODE* ucode )
case TR_OP_METHOD_CALL: case TR_OP_METHOD_CALL:
//printf("CALL METHOD %s\n" ); //printf("CALL METHOD %s\n" );
m_func( ucode, ctx, m_arg ); m_func( ctx, m_arg );
return; return;
default: 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 ) for( UOP* op : m_ucode )
op->Exec( &ctx, this ); op->Exec( ctx );
assert( ctx.SP() == 1 ); assert( ctx->SP() == 1 );
return ctx.Pop(); return ctx->Pop();
} }

View File

@ -249,7 +249,7 @@ class VAR_REF
{ {
public: public:
virtual VAR_TYPE_T GetType() = 0; 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 class UCODE
{ {
public: public:
typedef std::function<void( UCODE*, CONTEXT*, void* )> FUNC_PTR; typedef std::function<void( CONTEXT*, void* )> FUNC_PTR;
void AddOp( UOP* uop ) void AddOp( UOP* uop )
{ {
m_ucode.push_back(uop); m_ucode.push_back(uop);
} }
VALUE* Run(); VALUE* Run( CONTEXT* ctx );
std::string Dump() const; std::string Dump() const;
virtual VAR_REF* createVarRef( COMPILER* aCompiler, const std::string& var, virtual VAR_REF* createVarRef( COMPILER* aCompiler, const std::string& var,
@ -344,7 +344,7 @@ public:
m_func( std::move( func ) ) m_func( std::move( func ) )
{}; {};
void Exec( CONTEXT* ctx, UCODE *ucode ); void Exec( CONTEXT* ctx );
std::string Format() const; std::string Format() const;
@ -428,7 +428,7 @@ public:
void setRoot( LIBEVAL::TREE_NODE root ); 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 ); void ReportError( const wxString& aErrorMsg );
ERROR_STATUS GetErrorStatus() const { return m_errorStatus; } ERROR_STATUS GetErrorStatus() const { return m_errorStatus; }
@ -441,7 +441,7 @@ protected:
LEXER_STATE m_lexerState; LEXER_STATE m_lexerState;
bool generateUCode( UCODE* aCode ); bool generateUCode( UCODE* aCode, CONTEXT* aPreflightContext );
/* Token type used by the tokenizer */ /* 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 ) ) if( rule->m_Condition.EvaluateFor( aItem, bItem ) )
return rule; 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* a = const_cast<BOARD_ITEM*>( aItemA );
BOARD_ITEM* b = aItemB ? const_cast<BOARD_ITEM*>( aItemB ) : DELETED_BOARD_ITEM::GetInstance(); 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) if (!m_ucode)
m_ucode = new PCB_EXPR_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 ) if( ok )
return true; 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* arg = aCtx->Pop();
LIBEVAL::VALUE* result = aCtx->AllocValue(); 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 ); 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 ) ) if( item && item->IsOnLayer( layer ) )
result->Set( 1.0 ); 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(); LIBEVAL::VALUE* result = aCtx->AllocValue();
@ -69,7 +69,7 @@ static void isPlated( LIBEVAL::UCODE* aUcode, LIBEVAL::CONTEXT* aCtx, void *self
aCtx->Push( result ); aCtx->Push( result );
PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( 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;
D_PAD* pad = dynamic_cast<D_PAD*>( item ); D_PAD* pad = dynamic_cast<D_PAD*>( item );
if( pad && pad->GetAttribute() == PAD_ATTRIB_STANDARD ) 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 ); const PCB_EXPR_CONTEXT* ctx = static_cast<const PCB_EXPR_CONTEXT*>( aCtx );
BOARD_ITEM* item = ucode->GetItem( m_itemIndex ); BOARD_ITEM* item = ctx->GetItem( m_itemIndex );
return item; 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 ) ); auto it = m_matchingTypes.find( TYPE_HASH( *item ) );
if( it == m_matchingTypes.end() ) if( it == m_matchingTypes.end() )
@ -264,16 +264,18 @@ PCB_EXPR_EVALUATOR::~PCB_EXPR_EVALUATOR()
bool PCB_EXPR_EVALUATOR::Evaluate( const wxString& aExpr ) 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(); m_errorStatus = m_compiler.GetErrorStatus();
return false; return false;
} }
// fixme: handle error conditions // fixme: handle error conditions
LIBEVAL::VALUE* result = ucode.Run(); LIBEVAL::CONTEXT ctx;
LIBEVAL::VALUE* result = ucode.Run( &ctx );
if( result->GetType() == LIBEVAL::VT_NUMERIC ) if( result->GetType() == LIBEVAL::VT_NUMERIC )
m_result = KiROUND( result->AsDouble() ); m_result = KiROUND( result->AsDouble() );

View File

@ -40,18 +40,23 @@ class PCB_EXPR_VAR_REF;
class PCB_EXPR_UCODE : public LIBEVAL::UCODE class PCB_EXPR_UCODE : public LIBEVAL::UCODE
{ {
public: public:
PCB_EXPR_UCODE()
{
m_items[0] = nullptr;
m_items[1] = nullptr;
}
virtual LIBEVAL::VAR_REF* createVarRef( LIBEVAL::COMPILER *aCompiler, virtual LIBEVAL::VAR_REF* createVarRef( LIBEVAL::COMPILER *aCompiler,
const std::string& aVar, const std::string& aVar,
const std::string& aField ) override; const std::string& aField ) override;
virtual FUNC_PTR createFuncCall( LIBEVAL::COMPILER* aCompiler, virtual FUNC_PTR createFuncCall( LIBEVAL::COMPILER* aCompiler,
const std::string& name ) override; 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 ) void SetItems( BOARD_ITEM* a, BOARD_ITEM* b = nullptr )
{ {
@ -91,9 +96,9 @@ public:
m_matchingTypes[type_hash] = prop; 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: private:
std::unordered_map<TYPE_ID, PROPERTY_BASE*> m_matchingTypes; 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* a = const_cast<BOARD_ITEM*>( aItemA );
BOARD_ITEM* b = aItemB ? const_cast<BOARD_ITEM*>( aItemB ) : DELETED_BOARD_ITEM::GetInstance(); 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;
} }