simplify lambda information in ZInstAux

This commit is contained in:
Vern Paxson 2024-06-26 17:13:30 -07:00
parent 42ccb6a5a6
commit 17c4b6d982
7 changed files with 28 additions and 28 deletions

View file

@ -228,8 +228,8 @@ private:
const ZAMStmt CompileIndex(const NameExpr* n1, int n2_slot, const TypePtr& n2_type, const ListExpr* l, const ZAMStmt CompileIndex(const NameExpr* n1, int n2_slot, const TypePtr& n2_type, const ListExpr* l,
bool in_when); bool in_when);
const ZAMStmt BuildLambda(const NameExpr* n, LambdaExpr* le); const ZAMStmt BuildLambda(const NameExpr* n, ExprPtr le);
const ZAMStmt BuildLambda(int n_slot, LambdaExpr* le); const ZAMStmt BuildLambda(int n_slot, ExprPtr le);
// Second argument is which instruction slot holds the branch target. // Second argument is which instruction slot holds the branch target.
const ZAMStmt GenCond(const Expr* e, int& branch_v); const ZAMStmt GenCond(const Expr* e, int& branch_v);

View file

@ -189,7 +189,7 @@ const ZAMStmt ZAMCompiler::CompileAssignExpr(const AssignExpr* e) {
} }
if ( rhs->Tag() == EXPR_LAMBDA ) if ( rhs->Tag() == EXPR_LAMBDA )
return BuildLambda(lhs, rhs->AsLambdaExpr()); return BuildLambda(lhs, op2);
if ( rhs->Tag() == EXPR_COND && r1->GetType()->Tag() == TYPE_VECTOR ) if ( rhs->Tag() == EXPR_COND && r1->GetType()->Tag() == TYPE_VECTOR )
return Bool_Vec_CondVVVV(lhs, r1->AsNameExpr(), r2->AsNameExpr(), r3->AsNameExpr()); return Bool_Vec_CondVVVV(lhs, r1->AsNameExpr(), r2->AsNameExpr(), r3->AsNameExpr());
@ -807,18 +807,18 @@ const ZAMStmt ZAMCompiler::CompileIndex(const NameExpr* n1, int n2_slot, const T
return AddInst(z); return AddInst(z);
} }
const ZAMStmt ZAMCompiler::BuildLambda(const NameExpr* n, LambdaExpr* le) { const ZAMStmt ZAMCompiler::BuildLambda(const NameExpr* n, ExprPtr e) {
return BuildLambda(Frame1Slot(n, OP1_WRITE), le); return BuildLambda(Frame1Slot(n, OP1_WRITE), std::move(e));
} }
const ZAMStmt ZAMCompiler::BuildLambda(int n_slot, LambdaExpr* le) { const ZAMStmt ZAMCompiler::BuildLambda(int n_slot, ExprPtr e) {
auto& captures = le->GetCaptures(); auto lambda = cast_intrusive<LambdaExpr>(e);
auto& captures = lambda->GetCaptures();
int ncaptures = captures ? captures->size() : 0; int ncaptures = captures ? captures->size() : 0;
auto aux = new ZInstAux(ncaptures); auto aux = new ZInstAux(ncaptures);
aux->primary_func = le->PrimaryFunc(); aux->lambda = cast_intrusive<LambdaExpr>(std::move(e));
aux->lambda_name = le->Name(); aux->id_val = lambda->Ingredients()->GetID();
aux->id_val = le->Ingredients()->GetID();
for ( int i = 0; i < ncaptures; ++i ) { for ( int i = 0; i < ncaptures; ++i ) {
auto& id_i = (*captures)[i].Id(); auto& id_i = (*captures)[i].Id();
@ -829,7 +829,7 @@ const ZAMStmt ZAMCompiler::BuildLambda(int n_slot, LambdaExpr* le) {
aux->Add(i, FrameSlot(id_i), id_i->GetType()); aux->Add(i, FrameSlot(id_i), id_i->GetType());
} }
auto z = ZInstI(OP_LAMBDA_Vi, n_slot, le->PrimaryFunc()->FrameSize()); auto z = ZInstI(OP_LAMBDA_Vi, n_slot, lambda->PrimaryFunc()->FrameSize());
z.op_type = OP_VV_I2; z.op_type = OP_VV_I2;
z.aux = aux; z.aux = aux;
@ -1168,7 +1168,7 @@ const ZAMStmt ZAMCompiler::ConstructTable(const NameExpr* n, const Expr* e) {
if ( ! def_attr || def_attr->GetExpr()->Tag() != EXPR_LAMBDA ) if ( ! def_attr || def_attr->GetExpr()->Tag() != EXPR_LAMBDA )
return zstmt; return zstmt;
auto def_lambda = def_attr->GetExpr()->AsLambdaExpr(); auto def_lambda = cast_intrusive<LambdaExpr>(def_attr->GetExpr());
auto dl_t = def_lambda->GetType()->AsFuncType(); auto dl_t = def_lambda->GetType()->AsFuncType();
auto& captures = dl_t->GetCaptures(); auto& captures = dl_t->GetCaptures();

View file

@ -19,8 +19,8 @@ macro Z_TYPE2 z.GetType2()
macro Z_AUX z.aux macro Z_AUX z.aux
macro Z_AUX_ID z.aux->id_val macro Z_AUX_ID z.aux->id_val
macro Z_AUX_FUNC z.aux->func macro Z_AUX_FUNC z.aux->func
macro Z_AUX_PRIMARY_FUNC z.aux->primary_func macro Z_AUX_PRIMARY_FUNC z.aux->lambda->PrimaryFunc()
macro Z_AUX_LAMBDA_NAME z.aux->lambda_name macro Z_AUX_LAMBDA_NAME z.aux->lambda->Name()
# Location in the original script. # Location in the original script.
macro Z_LOC z.loc macro Z_LOC z.loc

View file

@ -1062,7 +1062,7 @@ const ZAMStmt ZAMCompiler::CompileWhen(const WhenStmt* ws) {
auto timeout = wi->TimeoutExpr(); auto timeout = wi->TimeoutExpr();
auto lambda = NewSlot(true); auto lambda = NewSlot(true);
(void)BuildLambda(lambda, wi->Lambda().get()); (void)BuildLambda(lambda, wi->Lambda());
std::vector<IDPtr> local_aggr_slots; std::vector<IDPtr> local_aggr_slots;
for ( auto& l : wi->WhenExprLocals() ) for ( auto& l : wi->WhenExprLocals() )

View file

@ -326,8 +326,11 @@ TraversalCode ZInstAux::Traverse(TraversalCallback* cb) const {
HANDLE_TC_STMT_PRE(tc); HANDLE_TC_STMT_PRE(tc);
} }
if ( func ) { // Don't traverse the "func" field, as if it's a recursive function
tc = func->Traverse(cb); // we can wind up right back here.
if ( lambda ) {
tc = lambda->Traverse(cb);
HANDLE_TC_STMT_PRE(tc); HANDLE_TC_STMT_PRE(tc);
} }

View file

@ -133,9 +133,9 @@ protected:
// for 't2' but keep them together for consistency. // for 't2' but keep them together for consistency.
// Type, usually for interpreting the constant. // Type, usually for interpreting the constant.
TypePtr t = nullptr; TypePtr t;
TypePtr t2 = nullptr; // just a few ops need two types TypePtr t2; // just a few ops need two types
public: public:
const TypePtr& GetType() const { return t; } const TypePtr& GetType() const { return t; }
@ -494,11 +494,8 @@ public:
AuxElem* elems = nullptr; AuxElem* elems = nullptr;
bool elems_has_slots = true; bool elems_has_slots = true;
// Ingredients associated with lambdas ... // Info for constructing lambdas.
ScriptFuncPtr primary_func; LambdaExprPtr lambda;
// ... and its name.
std::string lambda_name;
// For "when" statements. // For "when" statements.
std::shared_ptr<WhenInfo> wi; std::shared_ptr<WhenInfo> wi;
@ -507,11 +504,11 @@ public:
std::unique_ptr<CatArg>* cat_args = nullptr; std::unique_ptr<CatArg>* cat_args = nullptr;
// Used for accessing function names. // Used for accessing function names.
IDPtr id_val = nullptr; IDPtr id_val;
// Interpreter call expression associated with this instruction, // Interpreter call expression associated with this instruction,
// for error reporting and stack backtraces. // for error reporting and stack backtraces.
CallExprPtr call_expr = nullptr; CallExprPtr call_expr;
// Used for direct calls. // Used for direct calls.
Func* func = nullptr; Func* func = nullptr;
@ -526,7 +523,7 @@ public:
EventHandler* event_handler = nullptr; EventHandler* event_handler = nullptr;
// Used for things like constructors. // Used for things like constructors.
AttributesPtr attrs = nullptr; AttributesPtr attrs;
// Whether the instruction can lead to globals/captures changing. // Whether the instruction can lead to globals/captures changing.
// Currently only needed by the optimizer, but convenient to // Currently only needed by the optimizer, but convenient to

View file

@ -1,2 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. ### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
1226 valid, 1830 tested, 413 skipped 1226 valid, 1834 tested, 423 skipped