From 22709f82252b60854e244632435904f08912dacc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Thu, 14 Dec 2023 14:52:50 +0100 Subject: [PATCH] Move bytecode stuff into a dedicated namespace, for better code separation --- src/Bytecode.cc | 15 +++-- src/Bytecode.hh | 96 +++++++++++++------------- src/DynamicModel.cc | 56 ++++++++-------- src/ExprNode.cc | 122 +++++++++++++++++---------------- src/ExprNode.hh | 42 ++++++------ src/ModelTree.hh | 159 ++++++++++++++++++++++++-------------------- src/StaticModel.cc | 48 ++++++------- 7 files changed, 286 insertions(+), 252 deletions(-) diff --git a/src/Bytecode.cc b/src/Bytecode.cc index d4b83823..50588012 100644 --- a/src/Bytecode.cc +++ b/src/Bytecode.cc @@ -24,7 +24,10 @@ #include "Bytecode.hh" -BytecodeWriter::BytecodeWriter(const filesystem::path& filename) +namespace Bytecode +{ + +Writer::Writer(const filesystem::path& filename) { open(filename, ios::out | ios::binary); if (!is_open()) @@ -35,8 +38,8 @@ BytecodeWriter::BytecodeWriter(const filesystem::path& filename) } template<> -BytecodeWriter& -operator<<(BytecodeWriter& code_file, const FCALL_& instr) +Writer& +operator<<(Writer& code_file, const FCALL_& instr) { code_file.instructions_positions.push_back(code_file.tellp()); @@ -65,8 +68,8 @@ operator<<(BytecodeWriter& code_file, const FCALL_& instr) } template<> -BytecodeWriter& -operator<<(BytecodeWriter& code_file, const FBEGINBLOCK_& instr) +Writer& +operator<<(Writer& code_file, const FBEGINBLOCK_& instr) { code_file.instructions_positions.push_back(code_file.tellp()); @@ -99,3 +102,5 @@ operator<<(BytecodeWriter& code_file, const FBEGINBLOCK_& instr) return code_file; } + +} diff --git a/src/Bytecode.hh b/src/Bytecode.hh index 9b74c53b..d8bb0c63 100644 --- a/src/Bytecode.hh +++ b/src/Bytecode.hh @@ -31,7 +31,10 @@ using namespace std; -// The different opcodes of bytecode +namespace Bytecode +{ + +// The different tags encoding a bytecode instruction enum class Tags { FLDZ, // Loads a zero onto the stack @@ -124,12 +127,12 @@ struct Block_contain_type int Equation, Variable, Own_Derivative; }; -class BytecodeWriter; +class Writer; -struct BytecodeInstruction +struct Instruction { const Tags op_code; - explicit BytecodeInstruction(Tags op_code_arg) : op_code {op_code_arg} + explicit Instruction(Tags op_code_arg) : op_code {op_code_arg} { } @@ -139,28 +142,27 @@ protected: would no longer be POD; its memory representation would also include runtime type information, and our crude serialization technique (copying the whole object from memory) would thus not work. */ - ~BytecodeInstruction() = default; + ~Instruction() = default; }; template -class TagWithOneArgument : public BytecodeInstruction +class TagWithOneArgument : public Instruction { protected: T1 arg1; public: - TagWithOneArgument(Tags op_code_arg, T1 arg_arg1) : - BytecodeInstruction {op_code_arg}, arg1 {arg_arg1} + TagWithOneArgument(Tags op_code_arg, T1 arg_arg1) : Instruction {op_code_arg}, arg1 {arg_arg1} { } protected: - // See BytecodeInstruction destructor for the rationale + // See Instruction destructor for the rationale ~TagWithOneArgument() = default; }; template -class TagWithTwoArguments : public BytecodeInstruction +class TagWithTwoArguments : public Instruction { protected: T1 arg1; @@ -168,17 +170,17 @@ protected: public: TagWithTwoArguments(Tags op_code_arg, T1 arg_arg1, T2 arg_arg2) : - BytecodeInstruction {op_code_arg}, arg1 {arg_arg1}, arg2 {arg_arg2} + Instruction {op_code_arg}, arg1 {arg_arg1}, arg2 {arg_arg2} { } protected: - // See BytecodeInstruction destructor for the rationale + // See Instruction destructor for the rationale ~TagWithTwoArguments() = default; }; template -class TagWithThreeArguments : public BytecodeInstruction +class TagWithThreeArguments : public Instruction { protected: T1 arg1; @@ -187,17 +189,17 @@ protected: public: TagWithThreeArguments(Tags op_code_arg, T1 arg_arg1, T2 arg_arg2, T3 arg_arg3) : - BytecodeInstruction {op_code_arg}, arg1 {arg_arg1}, arg2 {arg_arg2}, arg3 {arg_arg3} + Instruction {op_code_arg}, arg1 {arg_arg1}, arg2 {arg_arg2}, arg3 {arg_arg3} { } protected: - // See BytecodeInstruction destructor for the rationale + // See Instruction destructor for the rationale ~TagWithThreeArguments() = default; }; template -class TagWithFourArguments : public BytecodeInstruction +class TagWithFourArguments : public Instruction { protected: T1 arg1; @@ -207,7 +209,7 @@ protected: public: TagWithFourArguments(Tags op_code_arg, T1 arg_arg1, T2 arg_arg2, T3 arg_arg3, T4 arg_arg4) : - BytecodeInstruction {op_code_arg}, + Instruction {op_code_arg}, arg1 {arg_arg1}, arg2 {arg_arg2}, arg3 {move(arg_arg3)}, @@ -216,38 +218,38 @@ public: } protected: - // See BytecodeInstruction destructor for the rationale + // See Instruction destructor for the rationale ~TagWithFourArguments() = default; }; -class FLDZ_ final : public BytecodeInstruction +class FLDZ_ final : public Instruction { public: - FLDZ_() : BytecodeInstruction {Tags::FLDZ} + FLDZ_() : Instruction {Tags::FLDZ} { } }; -class FEND_ final : public BytecodeInstruction +class FEND_ final : public Instruction { public: - FEND_() : BytecodeInstruction {Tags::FEND} + FEND_() : Instruction {Tags::FEND} { } }; -class FENDBLOCK_ final : public BytecodeInstruction +class FENDBLOCK_ final : public Instruction { public: - FENDBLOCK_() : BytecodeInstruction {Tags::FENDBLOCK} + FENDBLOCK_() : Instruction {Tags::FENDBLOCK} { } }; -class FENDEQU_ final : public BytecodeInstruction +class FENDEQU_ final : public Instruction { public: - FENDEQU_() : BytecodeInstruction {Tags::FENDEQU} + FENDEQU_() : Instruction {Tags::FENDEQU} { } }; @@ -768,10 +770,10 @@ public: }; }; -class FCALL_ final : public BytecodeInstruction +class FCALL_ final : public Instruction { template - friend BytecodeWriter& operator<<(BytecodeWriter& code_file, const B& instr); + friend Writer& operator<<(Writer& code_file, const B& instr); private: int nb_output_arguments, nb_input_arguments, indx; @@ -783,7 +785,7 @@ private: public: FCALL_(int nb_output_arguments_arg, int nb_input_arguments_arg, string func_name_arg, int indx_arg, ExternalFunctionCallType call_type_arg) : - BytecodeInstruction {Tags::FCALL}, + Instruction {Tags::FCALL}, nb_output_arguments {nb_output_arguments_arg}, nb_input_arguments {nb_input_arguments_arg}, indx {indx_arg}, @@ -793,7 +795,7 @@ public: } /* Deserializing constructor. Updates the code pointer to point beyond the bytes read. */ - FCALL_(char*& code) : BytecodeInstruction {Tags::FCALL} + FCALL_(char*& code) : Instruction {Tags::FCALL} { code += sizeof(op_code); @@ -888,7 +890,7 @@ public: } }; -class FNUMEXPR_ final : public BytecodeInstruction +class FNUMEXPR_ final : public Instruction { private: ExpressionType expression_type; @@ -898,7 +900,7 @@ private: int lag1; // For derivatives, lead/lag of the derivation variable public: FNUMEXPR_(const ExpressionType expression_type_arg, int equation_arg) : - BytecodeInstruction {Tags::FNUMEXPR}, + Instruction {Tags::FNUMEXPR}, expression_type {expression_type_arg}, equation {equation_arg}, dvariable1 {0}, @@ -906,7 +908,7 @@ public: { } FNUMEXPR_(const ExpressionType expression_type_arg, int equation_arg, int dvariable1_arg) : - BytecodeInstruction {Tags::FNUMEXPR}, + Instruction {Tags::FNUMEXPR}, expression_type {expression_type_arg}, equation {equation_arg}, dvariable1 {dvariable1_arg}, @@ -915,7 +917,7 @@ public: } FNUMEXPR_(const ExpressionType expression_type_arg, int equation_arg, int dvariable1_arg, int lag1_arg) : - BytecodeInstruction {Tags::FNUMEXPR}, + Instruction {Tags::FNUMEXPR}, expression_type {expression_type_arg}, equation {equation_arg}, dvariable1 {dvariable1_arg}, @@ -944,10 +946,10 @@ public: }; }; -class FBEGINBLOCK_ final : public BytecodeInstruction +class FBEGINBLOCK_ final : public Instruction { template - friend BytecodeWriter& operator<<(BytecodeWriter& code_file, const B& instr); + friend Writer& operator<<(Writer& code_file, const B& instr); private: int size {0}; @@ -970,7 +972,7 @@ public: const vector& variable_arg, const vector& equation_arg, bool is_linear_arg, int u_count_int_arg, int nb_col_jacob_arg, int det_exo_size_arg, int exo_size_arg, vector det_exogenous_arg, vector exogenous_arg) : - BytecodeInstruction {Tags::FBEGINBLOCK}, + Instruction {Tags::FBEGINBLOCK}, size {size_arg}, type {type_arg}, variable {variable_arg.begin() + first_element, @@ -990,7 +992,7 @@ public: FBEGINBLOCK_(int size_arg, BlockSimulationType type_arg, int first_element, int block_size, const vector& variable_arg, const vector& equation_arg, bool is_linear_arg, int u_count_int_arg, int nb_col_jacob_arg) : - BytecodeInstruction {Tags::FBEGINBLOCK}, + Instruction {Tags::FBEGINBLOCK}, size {size_arg}, type {type_arg}, variable {variable_arg.begin() + first_element, @@ -1006,7 +1008,7 @@ public: } /* Deserializing constructor. Updates the code pointer to point beyond the bytes read. */ - FBEGINBLOCK_(char*& code) : BytecodeInstruction {Tags::FBEGINBLOCK} + FBEGINBLOCK_(char*& code) : Instruction {Tags::FBEGINBLOCK} { code += sizeof(op_code); @@ -1103,17 +1105,17 @@ public: }; // Superclass of std::ofstream for writing a sequence of bytecode instructions -class BytecodeWriter : private ofstream +class Writer : private ofstream { template - friend BytecodeWriter& operator<<(BytecodeWriter& code_file, const B& instr); + friend Writer& operator<<(Writer& code_file, const B& instr); private: // Stores the positions of all instructions in the byte stream vector instructions_positions; public: - BytecodeWriter(const filesystem::path& filename); + Writer(const filesystem::path& filename); // Returns the number of the next instruction to be written int getInstructionCounter() const @@ -1137,8 +1139,8 @@ public: // Overloads of operator<< for writing bytecode instructions template -BytecodeWriter& -operator<<(BytecodeWriter& code_file, const B& instr) +Writer& +operator<<(Writer& code_file, const B& instr) { code_file.instructions_positions.push_back(code_file.tellp()); code_file.write(reinterpret_cast(&instr), sizeof(B)); @@ -1146,9 +1148,11 @@ operator<<(BytecodeWriter& code_file, const B& instr) } template<> -BytecodeWriter& operator<<(BytecodeWriter& code_file, const FCALL_& instr); +Writer& operator<<(Writer& code_file, const FCALL_& instr); template<> -BytecodeWriter& operator<<(BytecodeWriter& code_file, const FBEGINBLOCK_& instr); +Writer& operator<<(Writer& code_file, const FBEGINBLOCK_& instr); + +} #endif diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc index bc403a0f..9a309270 100644 --- a/src/DynamicModel.cc +++ b/src/DynamicModel.cc @@ -164,11 +164,11 @@ DynamicModel::writeDynamicBytecode(const string& basename) const writeBytecodeBinFile(basename + "/model/bytecode/dynamic.bin", simulation_type == BlockSimulationType::solveTwoBoundariesComplete)}; - BytecodeWriter code_file {basename + "/model/bytecode/dynamic.cod"}; + Bytecode::Writer code_file {basename + "/model/bytecode/dynamic.cod"}; // Declare temporary terms - code_file << FDIMT_ {static_cast(temporary_terms_derivatives[0].size() - + temporary_terms_derivatives[1].size())}; + code_file << Bytecode::FDIMT_ {static_cast(temporary_terms_derivatives[0].size() + + temporary_terms_derivatives[1].size())}; // Declare the (single) block vector exo(symbol_table.exo_nbr()), exo_det(symbol_table.exo_det_nbr()); @@ -183,19 +183,19 @@ DynamicModel::writeDynamicBytecode(const string& basename) const vector endo_idx(symbol_table.endo_nbr()); iota(endo_idx.begin(), endo_idx.end(), 0); - code_file << FBEGINBLOCK_ {symbol_table.endo_nbr(), - simulation_type, - 0, - symbol_table.endo_nbr(), - endo_idx, - eq_idx, - false, - u_count_int, - jacobian_ncols_endo, - symbol_table.exo_det_nbr(), - symbol_table.exo_nbr(), - exo_det, - exo}; + code_file << Bytecode::FBEGINBLOCK_ {symbol_table.endo_nbr(), + simulation_type, + 0, + symbol_table.endo_nbr(), + endo_idx, + eq_idx, + false, + u_count_int, + jacobian_ncols_endo, + symbol_table.exo_det_nbr(), + symbol_table.exo_nbr(), + exo_det, + exo}; writeBytecodeHelper(code_file); } @@ -203,7 +203,7 @@ DynamicModel::writeDynamicBytecode(const string& basename) const void DynamicModel::writeDynamicBlockBytecode(const string& basename) const { - BytecodeWriter code_file {basename + "/model/bytecode/block/dynamic.cod"}; + Bytecode::Writer code_file {basename + "/model/bytecode/block/dynamic.cod"}; const filesystem::path bin_filename {basename + "/model/bytecode/block/dynamic.bin"}; ofstream bin_file {bin_filename, ios::out | ios::binary}; @@ -214,7 +214,7 @@ DynamicModel::writeDynamicBlockBytecode(const string& basename) const } // Temporary variables declaration - code_file << FDIMT_ {static_cast(blocks_temporary_terms_idxs.size())}; + code_file << Bytecode::FDIMT_ {static_cast(blocks_temporary_terms_idxs.size())}; temporary_terms_t temporary_terms_written; @@ -231,19 +231,19 @@ DynamicModel::writeDynamicBlockBytecode(const string& basename) const ? writeBlockBytecodeBinFile(bin_file, block) : 0}; - code_file << FBEGINBLOCK_ {blocks[block].mfs_size, - simulation_type, - blocks[block].first_equation, - blocks[block].size, - endo_idx_block2orig, - eq_idx_block2orig, - blocks[block].linear, - u_count, - static_cast(blocks_jacob_cols_endo[block].size())}; + code_file << Bytecode::FBEGINBLOCK_ {blocks[block].mfs_size, + simulation_type, + blocks[block].first_equation, + blocks[block].size, + endo_idx_block2orig, + eq_idx_block2orig, + blocks[block].linear, + u_count, + static_cast(blocks_jacob_cols_endo[block].size())}; writeBlockBytecodeHelper(code_file, block, temporary_terms_written); } - code_file << FEND_ {}; + code_file << Bytecode::FEND_ {}; } void diff --git a/src/ExprNode.cc b/src/ExprNode.cc index 1bf3a6ac..60800880 100644 --- a/src/ExprNode.cc +++ b/src/ExprNode.cc @@ -144,7 +144,7 @@ ExprNode::checkIfTemporaryTermThenWrite(ostream& output, ExprNodeOutputType outp bool ExprNode::checkIfTemporaryTermThenWriteBytecode( - BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, + Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs) const { @@ -163,10 +163,10 @@ ExprNode::checkIfTemporaryTermThenWriteBytecode( was initially not called with steady_dynamic=true). */ return false; case ExprNodeBytecodeOutputType::dynamicModel: - code_file << FLDT_ {it2->second}; + code_file << Bytecode::FLDT_ {it2->second}; break; case ExprNodeBytecodeOutputType::staticModel: - code_file << FLDST_ {it2->second}; + code_file << Bytecode::FLDST_ {it2->second}; break; case ExprNodeBytecodeOutputType::dynamicAssignmentLHS: case ExprNodeBytecodeOutputType::staticAssignmentLHS: @@ -267,7 +267,7 @@ ExprNode::writeJsonExternalFunctionOutput([[maybe_unused]] vector& efout void ExprNode::writeBytecodeExternalFunctionOutput( - [[maybe_unused]] BytecodeWriter& code_file, + [[maybe_unused]] Bytecode::Writer& code_file, [[maybe_unused]] ExprNodeBytecodeOutputType output_type, [[maybe_unused]] const temporary_terms_t& temporary_terms, [[maybe_unused]] const temporary_terms_idxs_t& temporary_terms_idxs, @@ -556,7 +556,8 @@ NumConstNode::eval([[maybe_unused]] const eval_context_t& eval_context) const no } void -NumConstNode::writeBytecodeOutput(BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, +NumConstNode::writeBytecodeOutput(Bytecode::Writer& code_file, + ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, [[maybe_unused]] const deriv_node_temp_terms_t& tef_terms) const @@ -564,7 +565,7 @@ NumConstNode::writeBytecodeOutput(BytecodeWriter& code_file, ExprNodeBytecodeOut assert(!isAssignmentLHSBytecodeOutput(output_type)); if (!checkIfTemporaryTermThenWriteBytecode(code_file, output_type, temporary_terms, temporary_terms_idxs)) - code_file << FLDC_ {datatree.num_constants.getDouble(id)}; + code_file << Bytecode::FLDC_ {datatree.num_constants.getDouble(id)}; } void @@ -1431,7 +1432,8 @@ VariableNode::eval(const eval_context_t& eval_context) const noexcept(false) } void -VariableNode::writeBytecodeOutput(BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, +VariableNode::writeBytecodeOutput(Bytecode::Writer& code_file, + ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, const deriv_node_temp_terms_t& tef_terms) const @@ -1450,19 +1452,19 @@ VariableNode::writeBytecodeOutput(BytecodeWriter& code_file, ExprNodeBytecodeOut switch (output_type) { case ExprNodeBytecodeOutputType::dynamicModel: - code_file << FLDV_ {type, tsid, lag}; + code_file << Bytecode::FLDV_ {type, tsid, lag}; break; case ExprNodeBytecodeOutputType::staticModel: - code_file << FLDSV_ {type, tsid}; + code_file << Bytecode::FLDSV_ {type, tsid}; break; case ExprNodeBytecodeOutputType::dynamicSteadyStateOperator: - code_file << FLDVS_ {type, tsid}; + code_file << Bytecode::FLDVS_ {type, tsid}; break; case ExprNodeBytecodeOutputType::dynamicAssignmentLHS: - code_file << FSTPV_ {type, tsid, lag}; + code_file << Bytecode::FSTPV_ {type, tsid, lag}; break; case ExprNodeBytecodeOutputType::staticAssignmentLHS: - code_file << FSTPSV_ {type, tsid}; + code_file << Bytecode::FSTPSV_ {type, tsid}; break; } } @@ -3160,7 +3162,7 @@ UnaryOpNode::writeJsonExternalFunctionOutput(vector& efout, } void -UnaryOpNode::writeBytecodeExternalFunctionOutput(BytecodeWriter& code_file, +UnaryOpNode::writeBytecodeExternalFunctionOutput(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, @@ -3250,7 +3252,8 @@ UnaryOpNode::eval(const eval_context_t& eval_context) const noexcept(false) } void -UnaryOpNode::writeBytecodeOutput(BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, +UnaryOpNode::writeBytecodeOutput(Bytecode::Writer& code_file, + ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, const deriv_node_temp_terms_t& tef_terms) const @@ -3283,7 +3286,7 @@ UnaryOpNode::writeBytecodeOutput(BytecodeWriter& code_file, ExprNodeBytecodeOutp { arg->writeBytecodeOutput(code_file, output_type, temporary_terms, temporary_terms_idxs, tef_terms); - code_file << FUNARY_ {op_code}; + code_file << Bytecode::FUNARY_ {op_code}; } } @@ -4547,7 +4550,8 @@ BinaryOpNode::eval(const eval_context_t& eval_context) const noexcept(false) } void -BinaryOpNode::writeBytecodeOutput(BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, +BinaryOpNode::writeBytecodeOutput(Bytecode::Writer& code_file, + ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, const deriv_node_temp_terms_t& tef_terms) const @@ -4558,12 +4562,12 @@ BinaryOpNode::writeBytecodeOutput(BytecodeWriter& code_file, ExprNodeBytecodeOut return; if (op_code == BinaryOpcode::powerDeriv) - code_file << FLDC_ {static_cast(powerDerivOrder)}; + code_file << Bytecode::FLDC_ {static_cast(powerDerivOrder)}; arg1->writeBytecodeOutput(code_file, output_type, temporary_terms, temporary_terms_idxs, tef_terms); arg2->writeBytecodeOutput(code_file, output_type, temporary_terms, temporary_terms_idxs, tef_terms); - code_file << FBINARY_ {op_code}; + code_file << Bytecode::FBINARY_ {op_code}; } bool @@ -5009,7 +5013,7 @@ BinaryOpNode::writeJsonExternalFunctionOutput(vector& efout, void BinaryOpNode::writeBytecodeExternalFunctionOutput( - BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, + Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, deriv_node_temp_terms_t& tef_terms) const { @@ -6288,7 +6292,7 @@ TrinaryOpNode::eval(const eval_context_t& eval_context) const noexcept(false) } void -TrinaryOpNode::writeBytecodeOutput(BytecodeWriter& code_file, +TrinaryOpNode::writeBytecodeOutput(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, @@ -6305,7 +6309,7 @@ TrinaryOpNode::writeBytecodeOutput(BytecodeWriter& code_file, tef_terms); arg3->writeBytecodeOutput(code_file, output_type, temporary_terms, temporary_terms_idxs, tef_terms); - code_file << FTRINARY_ {op_code}; + code_file << Bytecode::FTRINARY_ {op_code}; } bool @@ -6484,7 +6488,7 @@ TrinaryOpNode::writeJsonExternalFunctionOutput(vector& efout, void TrinaryOpNode::writeBytecodeExternalFunctionOutput( - BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, + Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, deriv_node_temp_terms_t& tef_terms) const { @@ -6940,7 +6944,7 @@ AbstractExternalFunctionNode::computeChainRuleDerivative( void AbstractExternalFunctionNode::writeBytecodeExternalFunctionArguments( - BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, + Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, const deriv_node_temp_terms_t& tef_terms) const { @@ -7437,7 +7441,7 @@ ExternalFunctionNode::composeDerivatives(const vector& dargs) } void -ExternalFunctionNode::writeBytecodeOutput(BytecodeWriter& code_file, +ExternalFunctionNode::writeBytecodeOutput(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, @@ -7456,14 +7460,14 @@ ExternalFunctionNode::writeBytecodeOutput(BytecodeWriter& code_file, return; if (!isAssignmentLHSBytecodeOutput(output_type)) - code_file << FLDTEF_ {getIndxInTefTerms(symb_id, tef_terms)}; + code_file << Bytecode::FLDTEF_ {getIndxInTefTerms(symb_id, tef_terms)}; else - code_file << FSTPTEF_ {getIndxInTefTerms(symb_id, tef_terms)}; + code_file << Bytecode::FSTPTEF_ {getIndxInTefTerms(symb_id, tef_terms)}; } void ExternalFunctionNode::writeBytecodeExternalFunctionOutput( - BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, + Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, deriv_node_temp_terms_t& tef_terms) const { @@ -7485,26 +7489,26 @@ ExternalFunctionNode::writeBytecodeExternalFunctionOutput( temporary_terms_idxs, tef_terms); int nb_output_arguments; - ExternalFunctionCallType call_type; + Bytecode::ExternalFunctionCallType call_type; if (symb_id == first_deriv_symb_id && symb_id == second_deriv_symb_id) { nb_output_arguments = 3; - call_type = ExternalFunctionCallType::levelWithFirstAndSecondDerivative; + call_type = Bytecode::ExternalFunctionCallType::levelWithFirstAndSecondDerivative; } else if (symb_id == first_deriv_symb_id) { nb_output_arguments = 2; - call_type = ExternalFunctionCallType::levelWithFirstDerivative; + call_type = Bytecode::ExternalFunctionCallType::levelWithFirstDerivative; } else { nb_output_arguments = 1; - call_type = ExternalFunctionCallType::levelWithoutDerivative; + call_type = Bytecode::ExternalFunctionCallType::levelWithoutDerivative; } - code_file << FCALL_ {nb_output_arguments, static_cast(arguments.size()), - datatree.symbol_table.getName(symb_id), indx, call_type} - << FSTPTEF_ {indx}; + code_file << Bytecode::FCALL_ {nb_output_arguments, static_cast(arguments.size()), + datatree.symbol_table.getName(symb_id), indx, call_type} + << Bytecode::FSTPTEF_ {indx}; } } @@ -7815,7 +7819,7 @@ FirstDerivExternalFunctionNode::writeOutput(ostream& output, ExprNodeOutputType void FirstDerivExternalFunctionNode::writeBytecodeOutput( - BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, + Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, const deriv_node_temp_terms_t& tef_terms) const { @@ -7835,9 +7839,9 @@ FirstDerivExternalFunctionNode::writeBytecodeOutput( assert(first_deriv_symb_id != ExternalFunctionsTable::IDSetButNoNameProvided); if (!isAssignmentLHSBytecodeOutput(output_type)) - code_file << FLDTEFD_ {getIndxInTefTerms(symb_id, tef_terms), inputIndex}; + code_file << Bytecode::FLDTEFD_ {getIndxInTefTerms(symb_id, tef_terms), inputIndex}; else - code_file << FSTPTEFD_ {getIndxInTefTerms(symb_id, tef_terms), inputIndex}; + code_file << Bytecode::FSTPTEFD_ {getIndxInTefTerms(symb_id, tef_terms), inputIndex}; } void @@ -7972,7 +7976,7 @@ FirstDerivExternalFunctionNode::writeJsonExternalFunctionOutput( void FirstDerivExternalFunctionNode::writeBytecodeExternalFunctionOutput( - BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, + Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, deriv_node_temp_terms_t& tef_terms) const { @@ -8000,12 +8004,12 @@ FirstDerivExternalFunctionNode::writeBytecodeExternalFunctionOutput( { int nb_input_arguments {0}; int nb_output_arguments {1}; - FCALL_ fcall {nb_output_arguments, nb_input_arguments, "jacob_element", indx, - ExternalFunctionCallType::numericalFirstDerivative}; + Bytecode::FCALL_ fcall {nb_output_arguments, nb_input_arguments, "jacob_element", indx, + Bytecode::ExternalFunctionCallType::numericalFirstDerivative}; fcall.set_arg_func_name(datatree.symbol_table.getName(symb_id)); fcall.set_row(inputIndex); fcall.set_nb_add_input_arguments(static_cast(arguments.size())); - code_file << fcall << FSTPTEFD_ {indx, inputIndex}; + code_file << fcall << Bytecode::FSTPTEFD_ {indx, inputIndex}; } else { @@ -8015,10 +8019,11 @@ FirstDerivExternalFunctionNode::writeBytecodeExternalFunctionOutput( int nb_output_arguments {1}; - code_file << FCALL_ {nb_output_arguments, static_cast(arguments.size()), - datatree.symbol_table.getName(first_deriv_symb_id), indx, - ExternalFunctionCallType::separatelyProvidedFirstDerivative} - << FSTPTEFD_ {indx, inputIndex}; + code_file << Bytecode:: + FCALL_ {nb_output_arguments, static_cast(arguments.size()), + datatree.symbol_table.getName(first_deriv_symb_id), indx, + Bytecode::ExternalFunctionCallType::separatelyProvidedFirstDerivative} + << Bytecode::FSTPTEFD_ {indx, inputIndex}; } } @@ -8319,7 +8324,7 @@ SecondDerivExternalFunctionNode::computeXrefs(EquationInfo& ei) const void SecondDerivExternalFunctionNode::writeBytecodeOutput( - BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, + Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, const deriv_node_temp_terms_t& tef_terms) const { @@ -8339,14 +8344,16 @@ SecondDerivExternalFunctionNode::writeBytecodeOutput( assert(second_deriv_symb_id != ExternalFunctionsTable::IDSetButNoNameProvided); if (!isAssignmentLHSBytecodeOutput(output_type)) - code_file << FLDTEFDD_ {getIndxInTefTerms(symb_id, tef_terms), inputIndex1, inputIndex2}; + code_file << Bytecode::FLDTEFDD_ {getIndxInTefTerms(symb_id, tef_terms), inputIndex1, + inputIndex2}; else - code_file << FSTPTEFDD_ {getIndxInTefTerms(symb_id, tef_terms), inputIndex1, inputIndex2}; + code_file << Bytecode::FSTPTEFDD_ {getIndxInTefTerms(symb_id, tef_terms), inputIndex1, + inputIndex2}; } void SecondDerivExternalFunctionNode::writeBytecodeExternalFunctionOutput( - BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, + Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, deriv_node_temp_terms_t& tef_terms) const { @@ -8372,22 +8379,23 @@ SecondDerivExternalFunctionNode::writeBytecodeExternalFunctionOutput( if (int indx = getIndxInTefTerms(symb_id, tef_terms); second_deriv_symb_id == ExternalFunctionsTable::IDNotSet) { - FCALL_ fcall {1, 0, "hess_element", indx, - ExternalFunctionCallType::numericalSecondDerivative}; + Bytecode::FCALL_ fcall {1, 0, "hess_element", indx, + Bytecode::ExternalFunctionCallType::numericalSecondDerivative}; fcall.set_arg_func_name(datatree.symbol_table.getName(symb_id)); fcall.set_row(inputIndex1); fcall.set_col(inputIndex2); fcall.set_nb_add_input_arguments(static_cast(arguments.size())); - code_file << fcall << FSTPTEFDD_ {indx, inputIndex1, inputIndex2}; + code_file << fcall << Bytecode::FSTPTEFDD_ {indx, inputIndex1, inputIndex2}; } else { tef_terms[{second_deriv_symb_id, arguments}] = static_cast(tef_terms.size()); - code_file << FCALL_ {1, static_cast(arguments.size()), - datatree.symbol_table.getName(second_deriv_symb_id), indx, - ExternalFunctionCallType::separatelyProvidedSecondDerivative} - << FSTPTEFDD_ {indx, inputIndex1, inputIndex2}; + code_file << Bytecode:: + FCALL_ {1, static_cast(arguments.size()), + datatree.symbol_table.getName(second_deriv_symb_id), indx, + Bytecode::ExternalFunctionCallType::separatelyProvidedSecondDerivative} + << Bytecode::FSTPTEFDD_ {indx, inputIndex1, inputIndex2}; } } @@ -8610,7 +8618,7 @@ SubModelNode::collectDynamicVariables([[maybe_unused]] SymbolType type_arg, void SubModelNode::writeBytecodeOutput( - [[maybe_unused]] BytecodeWriter& code_file, + [[maybe_unused]] Bytecode::Writer& code_file, [[maybe_unused]] ExprNodeBytecodeOutputType output_type, [[maybe_unused]] const temporary_terms_t& temporary_terms, [[maybe_unused]] const temporary_terms_idxs_t& temporary_terms_idxs, diff --git a/src/ExprNode.hh b/src/ExprNode.hh index 64adf4d6..dfdd75ed 100644 --- a/src/ExprNode.hh +++ b/src/ExprNode.hh @@ -321,7 +321,7 @@ protected: // Same as above, for the bytecode case bool - checkIfTemporaryTermThenWriteBytecode(BytecodeWriter& code_file, + checkIfTemporaryTermThenWriteBytecode(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs) const; @@ -488,7 +488,7 @@ public: bool isdynamic = true) const; virtual void writeBytecodeExternalFunctionOutput( - BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, + Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, deriv_node_temp_terms_t& tef_terms) const; @@ -537,7 +537,7 @@ public: [[nodiscard]] virtual double eval(const eval_context_t& eval_context) const noexcept(false) = 0; // Write output to bytecode file - virtual void writeBytecodeOutput(BytecodeWriter& code_file, + virtual void writeBytecodeOutput(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, @@ -995,7 +995,7 @@ public: void collectVARLHSVariable(set& result) const override; void collectDynamicVariables(SymbolType type_arg, set>& result) const override; [[nodiscard]] double eval(const eval_context_t& eval_context) const noexcept(false) override; - void writeBytecodeOutput(BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, + void writeBytecodeOutput(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, const deriv_node_temp_terms_t& tef_terms) const override; @@ -1095,7 +1095,7 @@ public: void collectVARLHSVariable(set& result) const override; void collectDynamicVariables(SymbolType type_arg, set>& result) const override; [[nodiscard]] double eval(const eval_context_t& eval_context) const noexcept(false) override; - void writeBytecodeOutput(BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, + void writeBytecodeOutput(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, const deriv_node_temp_terms_t& tef_terms) const override; @@ -1231,7 +1231,7 @@ public: const temporary_terms_t& temporary_terms, deriv_node_temp_terms_t& tef_terms, bool isdynamic) const override; - void writeBytecodeExternalFunctionOutput(BytecodeWriter& code_file, + void writeBytecodeExternalFunctionOutput(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, @@ -1240,7 +1240,7 @@ public: void collectDynamicVariables(SymbolType type_arg, set>& result) const override; static double eval_opcode(UnaryOpcode op_code, double v) noexcept(false); [[nodiscard]] double eval(const eval_context_t& eval_context) const noexcept(false) override; - void writeBytecodeOutput(BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, + void writeBytecodeOutput(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, const deriv_node_temp_terms_t& tef_terms) const override; @@ -1376,7 +1376,7 @@ public: const temporary_terms_t& temporary_terms, deriv_node_temp_terms_t& tef_terms, bool isdynamic) const override; - void writeBytecodeExternalFunctionOutput(BytecodeWriter& code_file, + void writeBytecodeExternalFunctionOutput(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, @@ -1386,7 +1386,7 @@ public: static double eval_opcode(double v1, BinaryOpcode op_code, double v2, int derivOrder) noexcept(false); [[nodiscard]] double eval(const eval_context_t& eval_context) const noexcept(false) override; - void writeBytecodeOutput(BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, + void writeBytecodeOutput(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, const deriv_node_temp_terms_t& tef_terms) const override; @@ -1569,7 +1569,7 @@ public: const temporary_terms_t& temporary_terms, deriv_node_temp_terms_t& tef_terms, bool isdynamic) const override; - void writeBytecodeExternalFunctionOutput(BytecodeWriter& code_file, + void writeBytecodeExternalFunctionOutput(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, @@ -1578,7 +1578,7 @@ public: void collectDynamicVariables(SymbolType type_arg, set>& result) const override; static double eval_opcode(double v1, TrinaryOpcode op_code, double v2, double v3) noexcept(false); [[nodiscard]] double eval(const eval_context_t& eval_context) const noexcept(false) override; - void writeBytecodeOutput(BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, + void writeBytecodeOutput(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, const deriv_node_temp_terms_t& tef_terms) const override; @@ -1694,7 +1694,7 @@ protected: void writeJsonExternalFunctionArguments(ostream& output, const temporary_terms_t& temporary_terms, const deriv_node_temp_terms_t& tef_terms, bool isdynamic) const; - void writeBytecodeExternalFunctionArguments(BytecodeWriter& code_file, + void writeBytecodeExternalFunctionArguments(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, @@ -1737,7 +1737,7 @@ public: deriv_node_temp_terms_t& tef_terms, bool isdynamic = true) const override = 0; - void writeBytecodeExternalFunctionOutput(BytecodeWriter& code_file, + void writeBytecodeExternalFunctionOutput(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, @@ -1746,7 +1746,7 @@ public: void collectVARLHSVariable(set& result) const override; void collectDynamicVariables(SymbolType type_arg, set>& result) const override; [[nodiscard]] double eval(const eval_context_t& eval_context) const noexcept(false) override; - void writeBytecodeOutput(BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, + void writeBytecodeOutput(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, const deriv_node_temp_terms_t& tef_terms) const override @@ -1842,12 +1842,12 @@ public: const temporary_terms_t& temporary_terms, deriv_node_temp_terms_t& tef_terms, bool isdynamic) const override; - void writeBytecodeExternalFunctionOutput(BytecodeWriter& code_file, + void writeBytecodeExternalFunctionOutput(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, deriv_node_temp_terms_t& tef_terms) const override; - void writeBytecodeOutput(BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, + void writeBytecodeOutput(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, const deriv_node_temp_terms_t& tef_terms) const override; @@ -1877,7 +1877,7 @@ public: void writeJsonAST(ostream& output) const override; void writeJsonOutput(ostream& output, const temporary_terms_t& temporary_terms, const deriv_node_temp_terms_t& tef_terms, bool isdynamic) const override; - void writeBytecodeOutput(BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, + void writeBytecodeOutput(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, const deriv_node_temp_terms_t& tef_terms) const override; @@ -1889,7 +1889,7 @@ public: const temporary_terms_t& temporary_terms, deriv_node_temp_terms_t& tef_terms, bool isdynamic) const override; - void writeBytecodeExternalFunctionOutput(BytecodeWriter& code_file, + void writeBytecodeExternalFunctionOutput(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, @@ -1922,7 +1922,7 @@ public: void writeJsonAST(ostream& output) const override; void writeJsonOutput(ostream& output, const temporary_terms_t& temporary_terms, const deriv_node_temp_terms_t& tef_terms, bool isdynamic) const override; - void writeBytecodeOutput(BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, + void writeBytecodeOutput(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, const deriv_node_temp_terms_t& tef_terms) const override; @@ -1934,7 +1934,7 @@ public: const temporary_terms_t& temporary_terms, deriv_node_temp_terms_t& tef_terms, bool isdynamic) const override; - void writeBytecodeExternalFunctionOutput(BytecodeWriter& code_file, + void writeBytecodeExternalFunctionOutput(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, @@ -1994,7 +1994,7 @@ public: expr_t substituteUnaryOpNodes(const lag_equivalence_table_t& nodes, subst_table_t& subst_table, vector& neweqs) const override; BinaryOpNode* normalizeEquationHelper(const set& contain_var, expr_t rhs) const override; - void writeBytecodeOutput(BytecodeWriter& code_file, ExprNodeBytecodeOutputType output_type, + void writeBytecodeOutput(Bytecode::Writer& code_file, ExprNodeBytecodeOutputType output_type, const temporary_terms_t& temporary_terms, const temporary_terms_idxs_t& temporary_terms_idxs, const deriv_node_temp_terms_t& tef_terms) const override; diff --git a/src/ModelTree.hh b/src/ModelTree.hh index 24655ac1..d3d6896c 100644 --- a/src/ModelTree.hh +++ b/src/ModelTree.hh @@ -294,9 +294,10 @@ protected: const string& concat) const; //! Writes temporary terms in bytecode template - void - writeBytecodeTemporaryTerms(const temporary_terms_t& tt, temporary_terms_t& temporary_terms_union, - BytecodeWriter& code_file, deriv_node_temp_terms_t& tef_terms) const; + void writeBytecodeTemporaryTerms(const temporary_terms_t& tt, + temporary_terms_t& temporary_terms_union, + Bytecode::Writer& code_file, + deriv_node_temp_terms_t& tef_terms) const; /* Adds information for (non-block) bytecode simulation in a separate .bin file. Returns the number of first derivatives w.r.t. endogenous variables */ @@ -343,11 +344,11 @@ protected: // Helper for writing bytecode (without block decomposition) template - void writeBytecodeHelper(BytecodeWriter& code_file) const; + void writeBytecodeHelper(Bytecode::Writer& code_file) const; // Helper for writing blocks in bytecode template - void writeBlockBytecodeHelper(BytecodeWriter& code_file, int block, + void writeBlockBytecodeHelper(Bytecode::Writer& code_file, int block, temporary_terms_t& temporary_terms_union) const; // Helper for writing sparse derivatives indices in MATLAB/Octave driver file @@ -387,7 +388,7 @@ protected: //! Writes model equations in bytecode template - void writeBytecodeModelEquations(BytecodeWriter& code_file, + void writeBytecodeModelEquations(Bytecode::Writer& code_file, const temporary_terms_t& temporary_terms, const deriv_node_temp_terms_t& tef_terms) const; @@ -1518,7 +1519,7 @@ template void ModelTree::writeBytecodeTemporaryTerms(const temporary_terms_t& tt, temporary_terms_t& temporary_terms_union, - BytecodeWriter& code_file, + Bytecode::Writer& code_file, deriv_node_temp_terms_t& tef_terms) const { for (auto it : tt) @@ -1528,16 +1529,16 @@ ModelTree::writeBytecodeTemporaryTerms(const temporary_terms_t& tt, temporary_terms_idxs, tef_terms); int idx {temporary_terms_idxs.at(it)}; - code_file << FNUMEXPR_ {ExpressionType::TemporaryTerm, idx}; + code_file << Bytecode::FNUMEXPR_ {Bytecode::ExpressionType::TemporaryTerm, idx}; it->writeBytecodeOutput(code_file, output_type, temporary_terms_union, temporary_terms_idxs, tef_terms); static_assert(output_type == ExprNodeBytecodeOutputType::dynamicModel || output_type == ExprNodeBytecodeOutputType::staticModel); if constexpr (output_type == ExprNodeBytecodeOutputType::dynamicModel) - code_file << FSTPT_ {idx}; + code_file << Bytecode::FSTPT_ {idx}; else - code_file << FSTPST_ {idx}; + code_file << Bytecode::FSTPST_ {idx}; temporary_terms_union.insert(it); } @@ -1545,7 +1546,7 @@ ModelTree::writeBytecodeTemporaryTerms(const temporary_terms_t& tt, template void -ModelTree::writeBytecodeModelEquations(BytecodeWriter& code_file, +ModelTree::writeBytecodeModelEquations(Bytecode::Writer& code_file, const temporary_terms_t& temporary_terms, const deriv_node_temp_terms_t& tef_terms) const { @@ -1553,7 +1554,7 @@ ModelTree::writeBytecodeModelEquations(BytecodeWriter& code_file, { BinaryOpNode* eq_node {equations[eq]}; expr_t lhs {eq_node->arg1}, rhs {eq_node->arg2}; - code_file << FNUMEXPR_ {ExpressionType::ModelEquation, eq}; + code_file << Bytecode::FNUMEXPR_ {Bytecode::ExpressionType::ModelEquation, eq}; // Test if the right hand side of the equation is empty. double vrhs {1.0}; try @@ -1571,20 +1572,20 @@ ModelTree::writeBytecodeModelEquations(BytecodeWriter& code_file, rhs->writeBytecodeOutput(code_file, output_type, temporary_terms, temporary_terms_idxs, tef_terms); - code_file << FBINARY_ {BinaryOpcode::minus} << FSTPR_ {eq}; + code_file << Bytecode::FBINARY_ {BinaryOpcode::minus} << Bytecode::FSTPR_ {eq}; } else // The right hand side of the equation is empty ⇒ residual=lhs { lhs->writeBytecodeOutput(code_file, output_type, temporary_terms, temporary_terms_idxs, tef_terms); - code_file << FSTPR_ {eq}; + code_file << Bytecode::FSTPR_ {eq}; } } } template void -ModelTree::writeBytecodeHelper(BytecodeWriter& code_file) const +ModelTree::writeBytecodeHelper(Bytecode::Writer& code_file) const { constexpr ExprNodeBytecodeOutputType output_type { dynamic ? ExprNodeBytecodeOutputType::dynamicModel : ExprNodeBytecodeOutputType::staticModel}; @@ -1596,7 +1597,7 @@ ModelTree::writeBytecodeHelper(BytecodeWriter& code_file) const code_file, tef_terms); writeBytecodeModelEquations(code_file, temporary_terms_union, tef_terms); - code_file << FENDEQU_ {}; + code_file << Bytecode::FENDEQU_ {}; // Temporary terms for the Jacobian writeBytecodeTemporaryTerms(temporary_terms_derivatives[1], temporary_terms_union, @@ -1604,7 +1605,7 @@ ModelTree::writeBytecodeHelper(BytecodeWriter& code_file) const // Get the current code_file position and jump if “evaluate” mode int pos_jmpifeval {code_file.getInstructionCounter()}; - code_file << FJMPIFEVAL_ {0}; // Use 0 as jump offset for the time being + code_file << Bytecode::FJMPIFEVAL_ {0}; // Use 0 as jump offset for the time being // The Jacobian in “simulate” mode vector>> my_derivatives(symbol_table.endo_nbr()); @@ -1618,49 +1619,53 @@ ModelTree::writeBytecodeHelper(BytecodeWriter& code_file) const int tsid {getTypeSpecificIDByDerivID(deriv_id)}; int lag {getLagByDerivID(deriv_id)}; if constexpr (dynamic) - code_file << FNUMEXPR_ {ExpressionType::FirstEndoDerivative, eq, tsid, lag}; + code_file << Bytecode::FNUMEXPR_ {Bytecode::ExpressionType::FirstEndoDerivative, eq, + tsid, lag}; else - code_file << FNUMEXPR_ {ExpressionType::FirstEndoDerivative, eq, tsid}; + code_file << Bytecode::FNUMEXPR_ {Bytecode::ExpressionType::FirstEndoDerivative, eq, + tsid}; if (!my_derivatives[eq].size()) my_derivatives[eq].clear(); my_derivatives[eq].emplace_back(tsid, lag, count_u); d1->writeBytecodeOutput(code_file, output_type, temporary_terms_union, temporary_terms_idxs, tef_terms); if constexpr (dynamic) - code_file << FSTPU_ {count_u}; + code_file << Bytecode::FSTPU_ {count_u}; else - code_file << FSTPSU_ {count_u}; + code_file << Bytecode::FSTPSU_ {count_u}; count_u++; } } for (int i {0}; i < symbol_table.endo_nbr(); i++) { - code_file << FLDR_ {i}; + code_file << Bytecode::FLDR_ {i}; if (my_derivatives[i].size()) { for (bool first_term {true}; const auto& [tsid, lag, uidx] : my_derivatives[i]) { if constexpr (dynamic) - code_file << FLDU_ {uidx} << FLDV_ {SymbolType::endogenous, tsid, lag}; + code_file << Bytecode::FLDU_ {uidx} + << Bytecode::FLDV_ {SymbolType::endogenous, tsid, lag}; else - code_file << FLDSU_ {uidx} << FLDSV_ {SymbolType::endogenous, tsid}; - code_file << FBINARY_ {BinaryOpcode::times}; + code_file << Bytecode::FLDSU_ {uidx} + << Bytecode::FLDSV_ {SymbolType::endogenous, tsid}; + code_file << Bytecode::FBINARY_ {BinaryOpcode::times}; if (!exchange(first_term, false)) - code_file << FBINARY_ {BinaryOpcode::plus}; + code_file << Bytecode::FBINARY_ {BinaryOpcode::plus}; } - code_file << FBINARY_ {BinaryOpcode::minus}; + code_file << Bytecode::FBINARY_ {BinaryOpcode::minus}; } if constexpr (dynamic) - code_file << FSTPU_ {i}; + code_file << Bytecode::FSTPU_ {i}; else - code_file << FSTPSU_ {i}; + code_file << Bytecode::FSTPSU_ {i}; } // Jump unconditionally after the block int pos_jmp {code_file.getInstructionCounter()}; - code_file << FJMP_ {0}; // Use 0 as jump offset for the time being + code_file << Bytecode::FJMP_ {0}; // Use 0 as jump offset for the time being // Update jump offset for previous JMPIFEVAL - code_file.overwriteInstruction(pos_jmpifeval, FJMPIFEVAL_ {pos_jmp - pos_jmpifeval}); + code_file.overwriteInstruction(pos_jmpifeval, Bytecode::FJMPIFEVAL_ {pos_jmp - pos_jmpifeval}); // The Jacobian in “evaluate” mode for (const auto& [indices, d1] : derivatives[1]) @@ -1672,28 +1677,29 @@ ModelTree::writeBytecodeHelper(BytecodeWriter& code_file) const if constexpr (dynamic) { - ExpressionType expr_type; + Bytecode::ExpressionType expr_type; switch (type) { case SymbolType::endogenous: - expr_type = ExpressionType::FirstEndoDerivative; + expr_type = Bytecode::ExpressionType::FirstEndoDerivative; break; case SymbolType::exogenous: - expr_type = ExpressionType::FirstExoDerivative; + expr_type = Bytecode::ExpressionType::FirstExoDerivative; break; case SymbolType::exogenousDet: - expr_type = ExpressionType::FirstExodetDerivative; + expr_type = Bytecode::ExpressionType::FirstExodetDerivative; break; default: assert(false); break; } - code_file << FNUMEXPR_ {expr_type, eq, tsid, lag}; + code_file << Bytecode::FNUMEXPR_ {expr_type, eq, tsid, lag}; } else { assert(type == SymbolType::endogenous); - code_file << FNUMEXPR_ {ExpressionType::FirstEndoDerivative, eq, tsid}; + code_file << Bytecode::FNUMEXPR_ {Bytecode::ExpressionType::FirstEndoDerivative, eq, + tsid}; } d1->writeBytecodeOutput(code_file, output_type, temporary_terms_union, temporary_terms_idxs, @@ -1702,22 +1708,22 @@ ModelTree::writeBytecodeHelper(BytecodeWriter& code_file) const { // Bytecode MEX uses a separate matrix for exogenous and exodet Jacobians int jacob_col {type == SymbolType::endogenous ? getJacobianCol(deriv_id, false) : tsid}; - code_file << FSTPG3_ {eq, tsid, lag, jacob_col}; + code_file << Bytecode::FSTPG3_ {eq, tsid, lag, jacob_col}; } else - code_file << FSTPG2_ {eq, tsid}; + code_file << Bytecode::FSTPG2_ {eq, tsid}; } // Update jump offset for previous JMP int pos_end_block {code_file.getInstructionCounter()}; - code_file.overwriteInstruction(pos_jmp, FJMP_ {pos_end_block - pos_jmp - 1}); + code_file.overwriteInstruction(pos_jmp, Bytecode::FJMP_ {pos_end_block - pos_jmp - 1}); - code_file << FENDBLOCK_ {} << FEND_ {}; + code_file << Bytecode::FENDBLOCK_ {} << Bytecode::FEND_ {}; } template void -ModelTree::writeBlockBytecodeHelper(BytecodeWriter& code_file, int block, +ModelTree::writeBlockBytecodeHelper(Bytecode::Writer& code_file, int block, temporary_terms_t& temporary_terms_union) const { constexpr ExprNodeBytecodeOutputType output_type { @@ -1740,13 +1746,14 @@ ModelTree::writeBlockBytecodeHelper(BytecodeWriter& code_file, int block, it->writeBytecodeExternalFunctionOutput(code_file, output_type, temporary_terms_union, blocks_temporary_terms_idxs, tef_terms); - code_file << FNUMEXPR_ {ExpressionType::TemporaryTerm, blocks_temporary_terms_idxs.at(it)}; + code_file << Bytecode::FNUMEXPR_ {Bytecode::ExpressionType::TemporaryTerm, + blocks_temporary_terms_idxs.at(it)}; it->writeBytecodeOutput(code_file, output_type, temporary_terms_union, blocks_temporary_terms_idxs, tef_terms); if constexpr (dynamic) - code_file << FSTPT_ {blocks_temporary_terms_idxs.at(it)}; + code_file << Bytecode::FSTPT_ {blocks_temporary_terms_idxs.at(it)}; else - code_file << FSTPST_ {blocks_temporary_terms_idxs.at(it)}; + code_file << Bytecode::FSTPST_ {blocks_temporary_terms_idxs.at(it)}; temporary_terms_union.insert(it); } }; @@ -1772,7 +1779,8 @@ ModelTree::writeBlockBytecodeHelper(BytecodeWriter& code_file, int block, } else assert(equ_type == EquationType::evaluate); - code_file << FNUMEXPR_ {ExpressionType::ModelEquation, getBlockEquationID(block, i)}; + code_file << Bytecode::FNUMEXPR_ {Bytecode::ExpressionType::ModelEquation, + getBlockEquationID(block, i)}; rhs->writeBytecodeOutput(code_file, output_type, temporary_terms_union, blocks_temporary_terms_idxs, tef_terms); lhs->writeBytecodeOutput(code_file, assignment_lhs_output_type, temporary_terms_union, @@ -1787,12 +1795,14 @@ ModelTree::writeBlockBytecodeHelper(BytecodeWriter& code_file, int block, [[fallthrough]]; case BlockSimulationType::solveBackwardSimple: case BlockSimulationType::solveForwardSimple: - code_file << FNUMEXPR_ {ExpressionType::ModelEquation, getBlockEquationID(block, i)}; + code_file << Bytecode::FNUMEXPR_ {Bytecode::ExpressionType::ModelEquation, + getBlockEquationID(block, i)}; lhs->writeBytecodeOutput(code_file, output_type, temporary_terms_union, blocks_temporary_terms_idxs, tef_terms); rhs->writeBytecodeOutput(code_file, output_type, temporary_terms_union, blocks_temporary_terms_idxs, tef_terms); - code_file << FBINARY_ {BinaryOpcode::minus} << FSTPR_ {i - block_recursive}; + code_file << Bytecode::FBINARY_ {BinaryOpcode::minus} + << Bytecode::FSTPR_ {i - block_recursive}; break; } } @@ -1807,11 +1817,11 @@ ModelTree::writeBlockBytecodeHelper(BytecodeWriter& code_file, int block, be needed in subsequent blocks. */ write_eq_tt(blocks[block].size); - code_file << FENDEQU_ {}; + code_file << Bytecode::FENDEQU_ {}; // Get the current code_file position and jump if evaluating int pos_jmpifeval {code_file.getInstructionCounter()}; - code_file << FJMPIFEVAL_ {0}; // Use 0 as jump offset for the time being + code_file << Bytecode::FJMPIFEVAL_ {0}; // Use 0 as jump offset for the time being /* Write the derivatives for the “simulate” mode (not needed if the block is of type “evaluate backward/forward”) */ @@ -1825,15 +1835,16 @@ ModelTree::writeBlockBytecodeHelper(BytecodeWriter& code_file, int block, { int eqr {getBlockEquationID(block, 0)}; int varr {getBlockVariableID(block, 0)}; - code_file << FNUMEXPR_ {ExpressionType::FirstEndoDerivative, eqr, varr, 0}; + code_file << Bytecode::FNUMEXPR_ {Bytecode::ExpressionType::FirstEndoDerivative, eqr, + varr, 0}; // Get contemporaneous derivative of the single variable in the block if (auto it {blocks_derivatives[block].find({0, 0, 0})}; it != blocks_derivatives[block].end()) it->second->writeBytecodeOutput(code_file, output_type, temporary_terms_union, blocks_temporary_terms_idxs, tef_terms); else - code_file << FLDZ_ {}; - code_file << FSTPG_ {0}; + code_file << Bytecode::FLDZ_ {}; + code_file << Bytecode::FSTPG_ {0}; } break; @@ -1858,13 +1869,14 @@ ModelTree::writeBlockBytecodeHelper(BytecodeWriter& code_file, int block, && (simulation_type == BlockSimulationType::solveForwardComplete || simulation_type == BlockSimulationType::solveBackwardComplete)) continue; - code_file << FNUMEXPR_ {ExpressionType::FirstEndoDerivative, eqr, varr, lag}; + code_file << Bytecode::FNUMEXPR_ {Bytecode::ExpressionType::FirstEndoDerivative, + eqr, varr, lag}; d1->writeBytecodeOutput(code_file, output_type, temporary_terms_union, blocks_temporary_terms_idxs, tef_terms); if constexpr (dynamic) - code_file << FSTPU_ {count_u}; + code_file << Bytecode::FSTPU_ {count_u}; else - code_file << FSTPSU_ {count_u}; + code_file << Bytecode::FSTPSU_ {count_u}; Uf[eqr].emplace_back(count_u, varr, lag); count_u++; } @@ -1872,22 +1884,25 @@ ModelTree::writeBlockBytecodeHelper(BytecodeWriter& code_file, int block, for (int i {0}; i < block_size; i++) if (i >= block_recursive) { - code_file << FLDR_ {i - block_recursive} << FLDZ_ {}; + code_file << Bytecode::FLDR_ {i - block_recursive} << Bytecode::FLDZ_ {}; int eqr {getBlockEquationID(block, i)}; for (const auto& [index_u, var, lag] : Uf[eqr]) { if constexpr (dynamic) - code_file << FLDU_ {index_u} << FLDV_ {SymbolType::endogenous, var, lag}; + code_file << Bytecode::FLDU_ {index_u} + << Bytecode::FLDV_ {SymbolType::endogenous, var, lag}; else - code_file << FLDSU_ {index_u} << FLDSV_ {SymbolType::endogenous, var}; - code_file << FBINARY_ {BinaryOpcode::times} << FBINARY_ {BinaryOpcode::plus}; + code_file << Bytecode::FLDSU_ {index_u} + << Bytecode::FLDSV_ {SymbolType::endogenous, var}; + code_file << Bytecode::FBINARY_ {BinaryOpcode::times} + << Bytecode::FBINARY_ {BinaryOpcode::plus}; } - code_file << FBINARY_ {BinaryOpcode::minus}; + code_file << Bytecode::FBINARY_ {BinaryOpcode::minus}; if constexpr (dynamic) - code_file << FSTPU_ {i - block_recursive}; + code_file << Bytecode::FSTPU_ {i - block_recursive}; else - code_file << FSTPSU_ {i - block_recursive}; + code_file << Bytecode::FSTPSU_ {i - block_recursive}; } } break; @@ -1898,9 +1913,9 @@ ModelTree::writeBlockBytecodeHelper(BytecodeWriter& code_file, int block, // Jump unconditionally after the block int pos_jmp {code_file.getInstructionCounter()}; - code_file << FJMP_ {0}; // Use 0 as jump offset for the time being + code_file << Bytecode::FJMP_ {0}; // Use 0 as jump offset for the time being // Update jump offset for previous JMPIFEVAL - code_file.overwriteInstruction(pos_jmpifeval, FJMPIFEVAL_ {pos_jmp - pos_jmpifeval}); + code_file.overwriteInstruction(pos_jmpifeval, Bytecode::FJMPIFEVAL_ {pos_jmp - pos_jmpifeval}); // Write the derivatives for the “evaluate” mode for (const auto& [indices, d] : blocks_derivatives[block]) @@ -1908,22 +1923,24 @@ ModelTree::writeBlockBytecodeHelper(BytecodeWriter& code_file, int block, const auto& [eq, var, lag] {indices}; int eqr {getBlockEquationID(block, eq)}; int varr {getBlockVariableID(block, var)}; - code_file << FNUMEXPR_ {ExpressionType::FirstEndoDerivative, eqr, varr, lag}; + code_file << Bytecode::FNUMEXPR_ {Bytecode::ExpressionType::FirstEndoDerivative, eqr, varr, + lag}; d->writeBytecodeOutput(code_file, output_type, temporary_terms_union, blocks_temporary_terms_idxs, tef_terms); assert(eq >= block_recursive); if constexpr (dynamic) - code_file << FSTPG3_ {eq - block_recursive, var, lag, - getBlockJacobianEndoCol(block, var, lag)}; + code_file << Bytecode::FSTPG3_ {eq - block_recursive, var, lag, + getBlockJacobianEndoCol(block, var, lag)}; else - code_file << FSTPG2_ {eq - block_recursive, getBlockJacobianEndoCol(block, var, lag)}; + code_file << Bytecode::FSTPG2_ {eq - block_recursive, + getBlockJacobianEndoCol(block, var, lag)}; } // Update jump offset for previous JMP int pos_end_block {code_file.getInstructionCounter()}; - code_file.overwriteInstruction(pos_jmp, FJMP_ {pos_end_block - pos_jmp - 1}); + code_file.overwriteInstruction(pos_jmp, Bytecode::FJMP_ {pos_end_block - pos_jmp - 1}); - code_file << FENDBLOCK_ {}; + code_file << Bytecode::FENDBLOCK_ {}; } template diff --git a/src/StaticModel.cc b/src/StaticModel.cc index 0ac4afa8..4f24655a 100644 --- a/src/StaticModel.cc +++ b/src/StaticModel.cc @@ -131,24 +131,24 @@ StaticModel::writeStaticBytecode(const string& basename) const // First write the .bin file int u_count_int {writeBytecodeBinFile(basename + "/model/bytecode/static.bin", false)}; - BytecodeWriter code_file {basename + "/model/bytecode/static.cod"}; + Bytecode::Writer code_file {basename + "/model/bytecode/static.cod"}; vector eq_idx(equations.size()); iota(eq_idx.begin(), eq_idx.end(), 0); vector endo_idx(symbol_table.endo_nbr()); iota(endo_idx.begin(), endo_idx.end(), 0); // Declare temporary terms and the (single) block - code_file << FDIMST_ {static_cast(temporary_terms_derivatives[0].size() - + temporary_terms_derivatives[1].size())} - << FBEGINBLOCK_ {symbol_table.endo_nbr(), - BlockSimulationType::solveForwardComplete, - 0, - symbol_table.endo_nbr(), - endo_idx, - eq_idx, - false, - u_count_int, - symbol_table.endo_nbr()}; + code_file << Bytecode::FDIMST_ {static_cast(temporary_terms_derivatives[0].size() + + temporary_terms_derivatives[1].size())} + << Bytecode::FBEGINBLOCK_ {symbol_table.endo_nbr(), + BlockSimulationType::solveForwardComplete, + 0, + symbol_table.endo_nbr(), + endo_idx, + eq_idx, + false, + u_count_int, + symbol_table.endo_nbr()}; writeBytecodeHelper(code_file); } @@ -156,7 +156,7 @@ StaticModel::writeStaticBytecode(const string& basename) const void StaticModel::writeStaticBlockBytecode(const string& basename) const { - BytecodeWriter code_file {basename + "/model/bytecode/block/static.cod"}; + Bytecode::Writer code_file {basename + "/model/bytecode/block/static.cod"}; const filesystem::path bin_filename {basename + "/model/bytecode/block/static.bin"}; ofstream bin_file {bin_filename, ios::out | ios::binary}; @@ -167,7 +167,7 @@ StaticModel::writeStaticBlockBytecode(const string& basename) const } // Temporary variables declaration - code_file << FDIMST_ {static_cast(blocks_temporary_terms_idxs.size())}; + code_file << Bytecode::FDIMST_ {static_cast(blocks_temporary_terms_idxs.size())}; temporary_terms_t temporary_terms_written; @@ -181,19 +181,19 @@ StaticModel::writeStaticBlockBytecode(const string& basename) const ? writeBlockBytecodeBinFile(bin_file, block) : 0}; - code_file << FBEGINBLOCK_ {blocks[block].mfs_size, - simulation_type, - blocks[block].first_equation, - block_size, - endo_idx_block2orig, - eq_idx_block2orig, - blocks[block].linear, - u_count, - block_size}; + code_file << Bytecode::FBEGINBLOCK_ {blocks[block].mfs_size, + simulation_type, + blocks[block].first_equation, + block_size, + endo_idx_block2orig, + eq_idx_block2orig, + blocks[block].linear, + u_count, + block_size}; writeBlockBytecodeHelper(code_file, block, temporary_terms_written); } - code_file << FEND_ {}; + code_file << Bytecode::FEND_ {}; } void