put global statements into a quasi-function to support script optimization

This commit is contained in:
Vern Paxson 2021-03-24 21:22:03 -07:00
parent 8fb30f1d62
commit 95b89be571
4 changed files with 34 additions and 4 deletions

View file

@ -266,7 +266,7 @@ static bool expr_is_table_type_name(const zeek::detail::Expr* expr)
%%
bro:
zeek:
decl_list stmt_list
{
if ( zeek::detail::stmts )

View file

@ -3,6 +3,7 @@
#include "zeek/Options.h"
#include "zeek/Reporter.h"
#include "zeek/Desc.h"
#include "zeek/module_util.h"
#include "zeek/script_opt/ScriptOpt.h"
#include "zeek/script_opt/ProfileFunc.h"
#include "zeek/script_opt/Inline.h"
@ -153,6 +154,28 @@ void analyze_func(ScriptFuncPtr f)
funcs.emplace_back(f, ScopePtr{NewRef{}, f->GetScope()}, f->CurrentBody());
}
const FuncInfo* analyze_global_stmts(Stmt* stmts)
{
// We ignore analysis_options.only_func - if it's in use, later
// logic will keep this function from being compiled, but it's handy
// now to enter it into "funcs" so we have a FuncInfo to return.
auto id = install_ID("<global-stmts>", GLOBAL_MODULE_NAME, true, false);
auto empty_args_t = make_intrusive<RecordType>(nullptr);
auto func_t = make_intrusive<FuncType>(empty_args_t, nullptr, FUNC_FLAVOR_FUNCTION);
id->SetType(func_t);
auto sc = current_scope();
std::vector<IDPtr> empty_inits;
StmtPtr stmts_p{NewRef{}, stmts};
auto sf = make_intrusive<ScriptFunc>(id, stmts_p, empty_inits,
sc->Length(), 0);
funcs.emplace_back(sf, ScopePtr{NewRef{}, sc}, stmts_p);
return &funcs.back();
}
static void check_env_opt(const char* opt, bool& opt_flag)
{
if ( getenv(opt) )

View file

@ -96,6 +96,11 @@ extern std::unordered_set<const Func*> non_recursive_funcs;
// Analyze a given function for optimization.
extern void analyze_func(ScriptFuncPtr f);
// Analyze the given top-level statement(s) for optimization. Returns
// a pointer to a FuncInfo for an argument-less quasi-function that can
// be Invoked, or its body executed directly, to execute the statements.
extern const FuncInfo* analyze_global_stmts(Stmt* stmts);
// Analyze all of the parsed scripts collectively for optimization.
extern void analyze_scripts();

View file

@ -752,6 +752,8 @@ SetupResult setup(int argc, char** argv, Options* zopts)
}
}
auto init_stmts = stmts ? analyze_global_stmts(stmts) : nullptr;
analyze_scripts();
if ( analysis_options.report_recursive )
@ -838,15 +840,15 @@ SetupResult setup(int argc, char** argv, Options* zopts)
// cause more severe problems.
ZEEK_LSAN_ENABLE();
if ( stmts )
if ( init_stmts )
{
StmtFlowType flow;
Frame f(current_scope()->Length(), nullptr, nullptr);
Frame f(init_stmts->Scope()->Length(), nullptr, nullptr);
g_frame_stack.push_back(&f);
try
{
stmts->Exec(&f, flow);
init_stmts->Body()->Exec(&f, flow);
}
catch ( InterpreterException& )
{