/* * Copyright © 2022 Dynare Team * * This file is part of Dynare. * * Dynare is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Dynare is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Dynare. If not, see . */ #include #include #include #include "Bytecode.hh" BytecodeWriter::BytecodeWriter(const string &filename) { open(filename, ios::out | ios::binary); if (!is_open()) { cerr << R"(Error : Can't open file ")" << filename << R"(" for writing)" << endl; exit(EXIT_FAILURE); } } template<> BytecodeWriter & operator<<(BytecodeWriter &code_file, const FCALL_ &instr) { code_file.instructions_positions.push_back(code_file.tellp()); code_file.write(reinterpret_cast(&instr.op_code), sizeof instr.op_code); code_file.write(reinterpret_cast(&instr.nb_output_arguments), sizeof instr.nb_output_arguments); code_file.write(reinterpret_cast(&instr.nb_input_arguments), sizeof instr.nb_input_arguments); code_file.write(reinterpret_cast(&instr.indx), sizeof instr.indx); code_file.write(reinterpret_cast(&instr.add_input_arguments), sizeof instr.add_input_arguments); code_file.write(reinterpret_cast(&instr.row), sizeof instr.row); code_file.write(reinterpret_cast(&instr.col), sizeof instr.col); code_file.write(reinterpret_cast(&instr.call_type), sizeof instr.call_type); int size = static_cast(instr.func_name.size()); code_file.write(reinterpret_cast(&size), sizeof size); const char *name = instr.func_name.c_str(); code_file.write(name, size); size = static_cast(instr.arg_func_name.size()); code_file.write(reinterpret_cast(&size), sizeof size); name = instr.arg_func_name.c_str(); code_file.write(name, size); return code_file; } template<> BytecodeWriter & operator<<(BytecodeWriter &code_file, const FBEGINBLOCK_ &instr) { code_file.instructions_positions.push_back(code_file.tellp()); code_file.write(reinterpret_cast(&instr.op_code), sizeof instr.op_code); code_file.write(reinterpret_cast(&instr.size), sizeof instr.size); code_file.write(reinterpret_cast(&instr.type), sizeof instr.type); for (int i = 0; i < instr.size; i++) { code_file.write(reinterpret_cast(&instr.variable[i]), sizeof instr.variable[i]); code_file.write(reinterpret_cast(&instr.equation[i]), sizeof instr.equation[i]); } if (instr.type == BlockSimulationType::solveTwoBoundariesSimple || instr.type == BlockSimulationType::solveTwoBoundariesComplete || instr.type == BlockSimulationType::solveBackwardComplete || instr.type == BlockSimulationType::solveForwardComplete) { code_file.write(reinterpret_cast(&instr.is_linear), sizeof instr.is_linear); code_file.write(reinterpret_cast(&instr.endo_nbr), sizeof instr.endo_nbr); code_file.write(reinterpret_cast(&instr.Max_Lag), sizeof instr.Max_Lag); code_file.write(reinterpret_cast(&instr.Max_Lead), sizeof instr.Max_Lead); code_file.write(reinterpret_cast(&instr.u_count_int), sizeof instr.u_count_int); } code_file.write(reinterpret_cast(&instr.nb_col_jacob), sizeof instr.nb_col_jacob); code_file.write(reinterpret_cast(&instr.det_exo_size), sizeof instr.det_exo_size); code_file.write(reinterpret_cast(&instr.nb_col_det_exo_jacob), sizeof instr.nb_col_det_exo_jacob); code_file.write(reinterpret_cast(&instr.exo_size), sizeof instr.exo_size); code_file.write(reinterpret_cast(&instr.nb_col_exo_jacob), sizeof instr.nb_col_exo_jacob); code_file.write(reinterpret_cast(&instr.other_endo_size), sizeof instr.other_endo_size); code_file.write(reinterpret_cast(&instr.nb_col_other_endo_jacob), sizeof instr.nb_col_other_endo_jacob); for (int i{0}; i < instr.det_exo_size; i++) code_file.write(reinterpret_cast(&instr.det_exogenous[i]), sizeof instr.det_exogenous[i]); for (int i{0}; i < instr.exo_size; i++) code_file.write(reinterpret_cast(&instr.exogenous[i]), sizeof instr.exogenous[i]); for (int i{0}; i < instr.other_endo_size; i++) code_file.write(reinterpret_cast(&instr.other_endogenous[i]), sizeof instr.other_endogenous[i]); return code_file; }