mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 06:38:20 +00:00
gen-zam: Add clang-format pre-commit hook
This commit is contained in:
parent
d5cc0d5135
commit
f6f84184ed
2 changed files with 3146 additions and 3514 deletions
File diff suppressed because it is too large
Load diff
|
@ -10,10 +10,10 @@
|
|||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <optional>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
|
@ -23,17 +23,15 @@ using std::string;
|
|||
using std::vector;
|
||||
|
||||
// An instruction can have one of four basic classes.
|
||||
enum ZAM_InstClass
|
||||
{
|
||||
enum ZAM_InstClass {
|
||||
ZIC_REGULAR, // a non-complicated instruction
|
||||
ZIC_COND, // a conditional branch
|
||||
ZIC_VEC, // a vector operation
|
||||
ZIC_FIELD, // a record field assignment
|
||||
};
|
||||
};
|
||||
|
||||
// For a given instruction operand, its general class.
|
||||
enum ZAM_OperandClass
|
||||
{
|
||||
enum ZAM_OperandClass {
|
||||
ZAM_OC_CONSTANT, // uses the instruction's associated constant
|
||||
ZAM_OC_EVENT_HANDLER, // uses the associated event handler
|
||||
ZAM_OC_INT, // directly specified integer
|
||||
|
@ -56,7 +54,7 @@ enum ZAM_OperandClass
|
|||
ZAM_OC_TBL_ITER,
|
||||
|
||||
ZAM_OC_NONE, // instruction has no direct operands
|
||||
};
|
||||
};
|
||||
|
||||
using OCVec = vector<ZAM_OperandClass>;
|
||||
|
||||
|
@ -64,8 +62,7 @@ using OCVec = vector<ZAM_OperandClass>;
|
|||
// of a given operand. The generator uses these to transform the operand's
|
||||
// low-level ZVal into a higher-level type expected by the associated
|
||||
// evaluation code.
|
||||
enum ZAM_Type
|
||||
{
|
||||
enum ZAM_Type {
|
||||
ZAM_TYPE_ADDR,
|
||||
ZAM_TYPE_ANY,
|
||||
ZAM_TYPE_DOUBLE,
|
||||
|
@ -91,7 +88,7 @@ enum ZAM_Type
|
|||
// expression deals directly with the operand's ZVal, rather
|
||||
// than the generator providing a higher-level version.
|
||||
ZAM_TYPE_NONE,
|
||||
};
|
||||
};
|
||||
|
||||
// We only use the following in the context where the vector's elements
|
||||
// are individual words from the same line. We don't use it in other
|
||||
|
@ -99,17 +96,15 @@ enum ZAM_Type
|
|||
using Words = vector<string>;
|
||||
|
||||
// Used for error-reporting.
|
||||
struct InputLoc
|
||||
{
|
||||
struct InputLoc {
|
||||
const char* file_name;
|
||||
int line_num = 0;
|
||||
};
|
||||
};
|
||||
|
||||
// An EmitTarget is a generated file to which code will be emitted.
|
||||
// The different values are used to instruct the generator which target
|
||||
// is currently of interest.
|
||||
enum EmitTarget
|
||||
{
|
||||
enum EmitTarget {
|
||||
// Indicates that no generated file has yet been specified.
|
||||
None,
|
||||
|
||||
|
@ -191,13 +186,12 @@ enum EmitTarget
|
|||
// output. For example, for OP_NEGATE_VV_I the corresponding
|
||||
// string is "negate-VV-I".
|
||||
OpName,
|
||||
};
|
||||
};
|
||||
|
||||
// A helper class for managing the (ordered) collection of ZAM_OperandClass's
|
||||
// associated with an instruction in order to generate C++ calling sequences
|
||||
// (both parameters for declarations, and arguments for invocations).
|
||||
class ArgsManager
|
||||
{
|
||||
class ArgsManager {
|
||||
public:
|
||||
// Constructed by providing the various ZAM_OperandClass's along
|
||||
// with the instruction's class.
|
||||
|
@ -230,8 +224,7 @@ private:
|
|||
// C++ type, and the name to use when providing it as a parameter.
|
||||
// We have two names because in some contexts record fields have
|
||||
// different names in declarations vs. in parameter lists.
|
||||
struct Arg
|
||||
{
|
||||
struct Arg {
|
||||
string decl_name;
|
||||
string decl_type;
|
||||
string param_name;
|
||||
|
@ -247,20 +240,19 @@ private:
|
|||
// See Decls() and Params() above.
|
||||
string full_decl;
|
||||
string full_params;
|
||||
};
|
||||
};
|
||||
|
||||
// There are two mutually interacting classes: ZAMGen is the overall driver
|
||||
// for the ZAM generator, while ZAM_OpTemplate represents a single operation
|
||||
// template, with subclasses for specific types of operations.
|
||||
class ZAMGen;
|
||||
|
||||
class ZAM_OpTemplate
|
||||
{
|
||||
class ZAM_OpTemplate {
|
||||
public:
|
||||
// Instantiated by passing in the ZAMGen driver and the generic
|
||||
// name for the operation.
|
||||
ZAM_OpTemplate(ZAMGen* _g, string _base_name);
|
||||
virtual ~ZAM_OpTemplate() { }
|
||||
virtual ~ZAM_OpTemplate() {}
|
||||
|
||||
// Constructs the template's data structures by parsing its
|
||||
// description (beyond the initial description of the type of
|
||||
|
@ -377,8 +369,7 @@ protected:
|
|||
// is used in a dead assignment, should be converted to a different
|
||||
// operation that explictly omits any assignment.
|
||||
bool HasAssignmentLess() const { return ! assignment_less_op.empty(); }
|
||||
void SetAssignmentLess(string op, string op_class)
|
||||
{
|
||||
void SetAssignmentLess(string op, string op_class) {
|
||||
assignment_less_op = op;
|
||||
assignment_less_op_class = op_class;
|
||||
}
|
||||
|
@ -425,8 +416,7 @@ protected:
|
|||
// a (potentially empty) string differentiating the method from
|
||||
// others for that operation, and "oc" and "zc" are the same
|
||||
// as above.
|
||||
void InstantiateMethod(const string& m, const string& suffix, const OCVec& oc,
|
||||
ZAM_InstClass zc);
|
||||
void InstantiateMethod(const string& m, const string& suffix, const OCVec& oc, ZAM_InstClass zc);
|
||||
|
||||
// Generates the main logic of an operation's method, given the
|
||||
// specific operand classes, an associated suffix for differentiating
|
||||
|
@ -436,27 +426,23 @@ protected:
|
|||
// Generates the specific code to create a ZInst for the given
|
||||
// operation, operands, parameters to "GenInst", and suffix and
|
||||
// class per the above.
|
||||
virtual void BuildInstruction(const OCVec& oc, const string& params,
|
||||
const string& suffix, ZAM_InstClass zc);
|
||||
virtual void BuildInstruction(const OCVec& oc, const string& params, const string& suffix, ZAM_InstClass zc);
|
||||
|
||||
// Expands $-parameters into their direct representations given the
|
||||
// operand classes and associated accessors.
|
||||
string ExpandParams(const OCVec& oc, string eval, const vector<string>& accessors) const;
|
||||
string ExpandParams(const OCVec& oc, string eval) const
|
||||
{
|
||||
string ExpandParams(const OCVec& oc, string eval) const {
|
||||
vector<string> no_accessors;
|
||||
return ExpandParams(oc, std::move(eval), no_accessors);
|
||||
}
|
||||
|
||||
// Top-level driver for generating the C++ evaluation code for
|
||||
// a given flavor of operation.
|
||||
virtual void InstantiateEval(const OCVec& oc, const string& suffix,
|
||||
ZAM_InstClass zc);
|
||||
virtual void InstantiateEval(const OCVec& oc, const string& suffix, ZAM_InstClass zc);
|
||||
|
||||
// Generates the C++ case statement for evaluating the given flavor
|
||||
// of operation.
|
||||
void GenEval(EmitTarget et, const string& ot_str, const string& op_suffix, const string& eval,
|
||||
ZAM_InstClass zc);
|
||||
void GenEval(EmitTarget et, const string& ot_str, const string& op_suffix, const string& eval, ZAM_InstClass zc);
|
||||
|
||||
// Generates a description of the ZAM operation suitable for
|
||||
// reflection.
|
||||
|
@ -476,8 +462,7 @@ protected:
|
|||
// Generates a C++ evaluation for an assignment of the type
|
||||
// corresponding to "accessor". If "is_managed" is true then
|
||||
// generates the associated memory management, too.
|
||||
void GenAssignOpCore(const OCVec& oc, const string& eval,
|
||||
const string& accessor, bool is_managed);
|
||||
void GenAssignOpCore(const OCVec& oc, const string& eval, const string& accessor, bool is_managed);
|
||||
|
||||
// The same, but for when there's an explicit assignment value.
|
||||
void GenAssignOpValCore(const OCVec& oc, const string& eval, const string& accessor, bool is_managed);
|
||||
|
@ -504,8 +489,7 @@ protected:
|
|||
void Emit(const string& s);
|
||||
|
||||
// Same, but temporarily indented up.
|
||||
void EmitUp(const string& s)
|
||||
{
|
||||
void EmitUp(const string& s) {
|
||||
IndentUp();
|
||||
Emit(s);
|
||||
IndentDown();
|
||||
|
@ -522,13 +506,11 @@ protected:
|
|||
// being used for brace-delimited code blocks.
|
||||
void IndentUp();
|
||||
void IndentDown();
|
||||
void BeginBlock()
|
||||
{
|
||||
void BeginBlock() {
|
||||
IndentUp();
|
||||
Emit("{");
|
||||
}
|
||||
void EndBlock()
|
||||
{
|
||||
void EndBlock() {
|
||||
Emit("}");
|
||||
IndentDown();
|
||||
}
|
||||
|
@ -628,27 +610,23 @@ protected:
|
|||
// and the operand class (like "OP_V") of that associated operation.
|
||||
string assignment_less_op;
|
||||
string assignment_less_op_class;
|
||||
};
|
||||
};
|
||||
|
||||
// A subclass used for "unary-op" templates.
|
||||
class ZAM_UnaryOpTemplate : public ZAM_OpTemplate
|
||||
{
|
||||
class ZAM_UnaryOpTemplate : public ZAM_OpTemplate {
|
||||
public:
|
||||
ZAM_UnaryOpTemplate(ZAMGen* _g, string _base_name) : ZAM_OpTemplate(_g, _base_name) { }
|
||||
ZAM_UnaryOpTemplate(ZAMGen* _g, string _base_name) : ZAM_OpTemplate(_g, _base_name) {}
|
||||
|
||||
protected:
|
||||
void Instantiate() override;
|
||||
};
|
||||
};
|
||||
|
||||
// A subclass for unary operations that are directly instantiated using
|
||||
// custom methods.
|
||||
class ZAM_DirectUnaryOpTemplate : public ZAM_OpTemplate
|
||||
{
|
||||
class ZAM_DirectUnaryOpTemplate : public ZAM_OpTemplate {
|
||||
public:
|
||||
ZAM_DirectUnaryOpTemplate(ZAMGen* _g, string _base_name, string _direct)
|
||||
: ZAM_OpTemplate(_g, _base_name), direct(_direct)
|
||||
{
|
||||
}
|
||||
: ZAM_OpTemplate(_g, _base_name), direct(_direct) {}
|
||||
|
||||
protected:
|
||||
void Instantiate() override;
|
||||
|
@ -656,13 +634,12 @@ protected:
|
|||
private:
|
||||
// The ZAMCompiler method to call to compile the operation.
|
||||
string direct;
|
||||
};
|
||||
};
|
||||
|
||||
// A helper class for the ZAM_ExprOpTemplate class (which follows).
|
||||
// This class tracks a single instance of creating an evaluation for
|
||||
// an AST expression.
|
||||
class EvalInstance
|
||||
{
|
||||
class EvalInstance {
|
||||
public:
|
||||
// Initialized using the types of the LHS (result) and the
|
||||
// first and second operand. Often all three types are the
|
||||
|
@ -671,8 +648,7 @@ public:
|
|||
// "is_def" is true if this instance is for the default catch-all
|
||||
// where the operand types don't match any of the explicitly
|
||||
// specified evaluations;
|
||||
EvalInstance(ZAM_Type lhs_et, ZAM_Type op1_et, ZAM_Type op2_et, string eval,
|
||||
bool is_def);
|
||||
EvalInstance(ZAM_Type lhs_et, ZAM_Type op1_et, ZAM_Type op2_et, string eval, bool is_def);
|
||||
|
||||
// Returns the accessor to use for assigning to the LHS. "is_ptr"
|
||||
// indicates whether the value to which we're applying the
|
||||
|
@ -703,11 +679,10 @@ private:
|
|||
ZAM_Type op2_et;
|
||||
string eval;
|
||||
bool is_def;
|
||||
};
|
||||
};
|
||||
|
||||
// A subclass for AST "Expr" nodes in reduced form.
|
||||
class ZAM_ExprOpTemplate : public ZAM_OpTemplate
|
||||
{
|
||||
class ZAM_ExprOpTemplate : public ZAM_OpTemplate {
|
||||
public:
|
||||
ZAM_ExprOpTemplate(ZAMGen* _g, string _base_name);
|
||||
|
||||
|
@ -718,10 +693,7 @@ public:
|
|||
const std::unordered_set<ZAM_Type>& ExprTypes() const { return expr_types; }
|
||||
|
||||
void AddEvalSet(ZAM_Type zt, string ev) { eval_set[zt] += ev; }
|
||||
void AddEvalSet(ZAM_Type et1, ZAM_Type et2, string ev)
|
||||
{
|
||||
eval_mixed_set[et1][et2] += ev;
|
||||
}
|
||||
void AddEvalSet(ZAM_Type et1, ZAM_Type et2, string ev) { eval_mixed_set[et1][et2] += ev; }
|
||||
|
||||
bool IncludesFieldOp() const override { return includes_field_op; }
|
||||
void SetIncludesFieldOp() { includes_field_op = true; }
|
||||
|
@ -739,10 +711,7 @@ protected:
|
|||
// expression suitable for assignment in a loop across the elements
|
||||
// of a Zeek "vector" type. "have_target" is true if the template
|
||||
// has an explicit "$$" assignment target.
|
||||
virtual const char* VecEvalRE(bool have_target) const
|
||||
{
|
||||
return have_target ? "$$$$ = ZVal($1)" : "ZVal($&)";
|
||||
}
|
||||
virtual const char* VecEvalRE(bool have_target) const { return have_target ? "$$$$ = ZVal($1)" : "ZVal($&)"; }
|
||||
|
||||
void Parse(const string& attr, const string& line, const Words& words) override;
|
||||
void Instantiate() override;
|
||||
|
@ -767,11 +736,10 @@ protected:
|
|||
|
||||
// Generates an if-else cascade element that matches one of the
|
||||
// specific Zeek types associated with the instruction.
|
||||
void GenMethodTest(ZAM_Type et1, ZAM_Type et2, const string& params,
|
||||
const string& suffix, bool do_else, ZAM_InstClass zc);
|
||||
void GenMethodTest(ZAM_Type et1, ZAM_Type et2, const string& params, const string& suffix, bool do_else,
|
||||
ZAM_InstClass zc);
|
||||
|
||||
void InstantiateEval(const OCVec& oc, const string& suffix,
|
||||
ZAM_InstClass zc) override;
|
||||
void InstantiateEval(const OCVec& oc, const string& suffix, ZAM_InstClass zc) override;
|
||||
|
||||
private:
|
||||
// The Zeek types that can appear as operands for the expression.
|
||||
|
@ -799,13 +767,12 @@ private:
|
|||
// If true, then the evaluations will take care of ensuring
|
||||
// proper result types when assigning to $$.
|
||||
bool explicit_res_type = false;
|
||||
};
|
||||
};
|
||||
|
||||
// A version of ZAM_ExprOpTemplate for unary expressions.
|
||||
class ZAM_UnaryExprOpTemplate : public ZAM_ExprOpTemplate
|
||||
{
|
||||
class ZAM_UnaryExprOpTemplate : public ZAM_ExprOpTemplate {
|
||||
public:
|
||||
ZAM_UnaryExprOpTemplate(ZAMGen* _g, string _base_name) : ZAM_ExprOpTemplate(_g, _base_name) { }
|
||||
ZAM_UnaryExprOpTemplate(ZAMGen* _g, string _base_name) : ZAM_ExprOpTemplate(_g, _base_name) {}
|
||||
|
||||
bool IncludesFieldOp() const override { return ExprTypes().count(ZAM_TYPE_NONE) == 0; }
|
||||
|
||||
|
@ -815,14 +782,12 @@ protected:
|
|||
void Parse(const string& attr, const string& line, const Words& words) override;
|
||||
void Instantiate() override;
|
||||
|
||||
void BuildInstruction(const OCVec& oc, const string& params,
|
||||
const string& suffix, ZAM_InstClass zc) override;
|
||||
};
|
||||
void BuildInstruction(const OCVec& oc, const string& params, const string& suffix, ZAM_InstClass zc) override;
|
||||
};
|
||||
|
||||
// A version of ZAM_UnaryExprOpTemplate where the point of the expression
|
||||
// is to capture a direct assignment operation.
|
||||
class ZAM_AssignOpTemplate : public ZAM_UnaryExprOpTemplate
|
||||
{
|
||||
class ZAM_AssignOpTemplate : public ZAM_UnaryExprOpTemplate {
|
||||
public:
|
||||
ZAM_AssignOpTemplate(ZAMGen* _g, string _base_name);
|
||||
|
||||
|
@ -837,13 +802,12 @@ protected:
|
|||
|
||||
private:
|
||||
bool field_op = false;
|
||||
};
|
||||
};
|
||||
|
||||
// A version of ZAM_ExprOpTemplate for binary expressions.
|
||||
class ZAM_BinaryExprOpTemplate : public ZAM_ExprOpTemplate
|
||||
{
|
||||
class ZAM_BinaryExprOpTemplate : public ZAM_ExprOpTemplate {
|
||||
public:
|
||||
ZAM_BinaryExprOpTemplate(ZAMGen* _g, string _base_name) : ZAM_ExprOpTemplate(_g, _base_name) { }
|
||||
ZAM_BinaryExprOpTemplate(ZAMGen* _g, string _base_name) : ZAM_ExprOpTemplate(_g, _base_name) {}
|
||||
|
||||
bool IncludesFieldOp() const override { return true; }
|
||||
|
||||
|
@ -852,27 +816,21 @@ public:
|
|||
protected:
|
||||
void Instantiate() override;
|
||||
|
||||
void BuildInstruction(const OCVec& oc, const string& params,
|
||||
const string& suffix, ZAM_InstClass zc) override;
|
||||
void BuildInstruction(const OCVec& oc, const string& params, const string& suffix, ZAM_InstClass zc) override;
|
||||
|
||||
void GenerateSecondTypeVars(const OCVec& oc, ZAM_InstClass zc);
|
||||
};
|
||||
};
|
||||
|
||||
// A version of ZAM_BinaryExprOpTemplate for relationals.
|
||||
class ZAM_RelationalExprOpTemplate : public ZAM_BinaryExprOpTemplate
|
||||
{
|
||||
class ZAM_RelationalExprOpTemplate : public ZAM_BinaryExprOpTemplate {
|
||||
public:
|
||||
ZAM_RelationalExprOpTemplate(ZAMGen* _g, string _base_name)
|
||||
: ZAM_BinaryExprOpTemplate(_g, _base_name)
|
||||
{
|
||||
}
|
||||
ZAM_RelationalExprOpTemplate(ZAMGen* _g, string _base_name) : ZAM_BinaryExprOpTemplate(_g, _base_name) {}
|
||||
|
||||
bool IncludesFieldOp() const override { return false; }
|
||||
bool IsConditionalOp() const override { return true; }
|
||||
|
||||
protected:
|
||||
const char* VecEvalRE(bool have_target) const override
|
||||
{
|
||||
const char* VecEvalRE(bool have_target) const override {
|
||||
if ( have_target )
|
||||
return "$$$$ = ZVal(zeek_int_t($1))";
|
||||
else
|
||||
|
@ -881,16 +839,14 @@ protected:
|
|||
|
||||
void Instantiate() override;
|
||||
|
||||
void BuildInstruction(const OCVec& oc, const string& params,
|
||||
const string& suffix, ZAM_InstClass zc) override;
|
||||
};
|
||||
void BuildInstruction(const OCVec& oc, const string& params, const string& suffix, ZAM_InstClass zc) override;
|
||||
};
|
||||
|
||||
// A version of ZAM_OpTemplate for operations used internally (and not
|
||||
// corresponding to AST elements).
|
||||
class ZAM_InternalOpTemplate : public ZAM_OpTemplate
|
||||
{
|
||||
class ZAM_InternalOpTemplate : public ZAM_OpTemplate {
|
||||
public:
|
||||
ZAM_InternalOpTemplate(ZAMGen* _g, string _base_name) : ZAM_OpTemplate(_g, _base_name) { }
|
||||
ZAM_InternalOpTemplate(ZAMGen* _g, string _base_name) : ZAM_OpTemplate(_g, _base_name) {}
|
||||
|
||||
bool IsInternalOp() const override { return true; }
|
||||
|
||||
|
@ -906,30 +862,23 @@ private:
|
|||
|
||||
// Refinement of is_indirect_call, when it's also via a local variable.
|
||||
bool is_local_indirect_call = false;
|
||||
};
|
||||
};
|
||||
|
||||
// An internal operation that assigns a result to a frame element.
|
||||
class ZAM_InternalAssignOpTemplate : public ZAM_InternalOpTemplate
|
||||
{
|
||||
class ZAM_InternalAssignOpTemplate : public ZAM_InternalOpTemplate {
|
||||
public:
|
||||
ZAM_InternalAssignOpTemplate(ZAMGen* _g, string _base_name)
|
||||
: ZAM_InternalOpTemplate(_g, _base_name)
|
||||
{
|
||||
}
|
||||
ZAM_InternalAssignOpTemplate(ZAMGen* _g, string _base_name) : ZAM_InternalOpTemplate(_g, _base_name) {}
|
||||
|
||||
bool IsAssignOp() const override { return true; }
|
||||
};
|
||||
};
|
||||
|
||||
// Helper classes for managing input from the template file, including
|
||||
// low-level scanning.
|
||||
|
||||
class TemplateInput
|
||||
{
|
||||
class TemplateInput {
|
||||
public:
|
||||
// Program name and file name are for generating error messages.
|
||||
TemplateInput(FILE* _f, const char* _prog_name, const char* _file_name)
|
||||
: f(_f), prog_name(_prog_name)
|
||||
{
|
||||
TemplateInput(FILE* _f, const char* _prog_name, const char* _file_name) : f(_f), prog_name(_prog_name) {
|
||||
loc.file_name = _file_name;
|
||||
}
|
||||
|
||||
|
@ -961,12 +910,11 @@ private:
|
|||
FILE* f;
|
||||
const char* prog_name;
|
||||
InputLoc loc;
|
||||
};
|
||||
};
|
||||
|
||||
// Driver class for the ZAM instruction generator.
|
||||
|
||||
class ZAMGen
|
||||
{
|
||||
class ZAMGen {
|
||||
public:
|
||||
ZAMGen(int argc, char** argv);
|
||||
|
||||
|
@ -981,8 +929,7 @@ public:
|
|||
// Generates a ZAM op-code for the given template, suffix, and
|
||||
// instruction class. Also creates auxiliary information associated
|
||||
// with the instruction.
|
||||
string GenOpCode(const ZAM_OpTemplate* ot, const string& suffix,
|
||||
ZAM_InstClass zc = ZIC_REGULAR);
|
||||
string GenOpCode(const ZAM_OpTemplate* ot, const string& suffix, ZAM_InstClass zc = ZIC_REGULAR);
|
||||
|
||||
// These methods provide low-level parsing (and error-reporting)
|
||||
// access to ZAM_OpTemplate objects.
|
||||
|
@ -1047,4 +994,4 @@ private:
|
|||
|
||||
// If true, refrain from appending a newline to any emitted lines.
|
||||
bool no_NL = false;
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue