the bulk of the compiler

This commit is contained in:
Vern Paxson 2021-04-19 16:32:04 -07:00
parent 158e82a2c1
commit 863be9436b
40 changed files with 7730 additions and 0 deletions

120
src/script_opt/CPP/Func.h Normal file
View file

@ -0,0 +1,120 @@
// See the file "COPYING" in the main distribution directory for copyright.
// Subclasses of Func and Stmt to support C++-generated code, along
// with tracking of that code to enable hooking into it at run-time.
#pragma once
#include "zeek/Func.h"
#include "zeek/script_opt/ProfileFunc.h"
namespace zeek {
namespace detail {
// A subclass of Func used for lambdas that the compiler creates for
// complex initializations (expressions used in type attributes).
// The usage is via derivation from this class, rather than direct
// use of it.
class CPPFunc : public Func {
public:
bool IsPure() const override { return is_pure; }
void Describe(ODesc* d) const override;
protected:
// Constructor used when deriving subclasses.
CPPFunc(const char* _name, bool _is_pure)
{
name = _name;
is_pure = _is_pure;
}
std::string name;
bool is_pure;
};
// A subclass of Stmt used to replace a function/event handler/hook body.
class CPPStmt : public Stmt {
public:
CPPStmt(const char* _name) : Stmt(STMT_CPP), name(_name) { }
const std::string& Name() { return name; }
// Sets/returns a hash associated with this statement. A value
// of 0 means "not set".
p_hash_type GetHash() const { return hash; }
void SetHash(p_hash_type h) { hash = h; }
// The following only get defined by lambda bodies.
virtual void SetLambdaCaptures(Frame* f) { }
virtual std::vector<ValPtr> SerializeLambdaCaptures() const
{ return std::vector<ValPtr>{}; }
virtual IntrusivePtr<CPPStmt> Clone()
{
return {NewRef{}, this};
}
protected:
// This method being called means that the inliner is running
// on compiled code, which shouldn't happen.
StmtPtr Duplicate() override { ASSERT(0); return ThisPtr(); }
TraversalCode Traverse(TraversalCallback* cb) const override
{ return TC_CONTINUE; }
std::string name;
p_hash_type hash = 0ULL;
};
using CPPStmtPtr = IntrusivePtr<CPPStmt>;
// For script-level lambdas, a ScriptFunc subclass that knows how to
// deal with its captures for serialization. Different from CPPFunc in
// that CPPFunc is for lambdas generated directly by the compiler,
// rather than those explicitly present in scripts.
class CPPLambdaFunc : public ScriptFunc {
public:
CPPLambdaFunc(std::string name, FuncTypePtr ft, CPPStmtPtr l_body);
bool HasCopySemantics() const override { return true; }
protected:
// Methods related to sending lambdas via Broker.
broker::expected<broker::data> SerializeClosure() const override;
void SetCaptures(Frame* f) override;
FuncPtr DoClone() override;
CPPStmtPtr l_body;
};
// Information associated with a given compiled script body: its
// Stmt subclass, priority, and any events that should be registered
// upon instantiating the body.
struct CompiledScript {
CPPStmtPtr body;
int priority;
std::vector<std::string> events;
};
// Maps hashes to compiled information.
extern std::unordered_map<p_hash_type, CompiledScript> compiled_scripts;
// Maps hashes to standalone script initialization callbacks.
extern std::unordered_map<p_hash_type, void (*)()> standalone_callbacks;
// Standalone callbacks marked for activation by calls to the
// load_CPP() BiF.
extern std::vector<void (*)()> standalone_activations;
} // namespace detail
} // namespace zeek