From 5452b00bbfb08175d12d1bf78f91867440a6f201 Mon Sep 17 00:00:00 2001 From: kalashnikovni Date: Thu, 28 Sep 2023 21:13:27 -0300 Subject: [PATCH] Info support for SBG --- sbg/info.cpp | 163 +++++++++++++++++++++++++++++++++++++++++++++++++++ sbg/info.hpp | 127 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 290 insertions(+) create mode 100755 sbg/info.cpp create mode 100755 sbg/info.hpp diff --git a/sbg/info.cpp b/sbg/info.cpp new file mode 100755 index 0000000..fd930a8 --- /dev/null +++ b/sbg/info.cpp @@ -0,0 +1,163 @@ +/******************************************************************************* + + This file is part of Set--Based Graph Library. + + SBG Library 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. + + SBG Library 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 SBG Library. If not, see . + + ******************************************************************************/ + +#include "sbg/info.hpp" + +namespace SBG { + +namespace LIB { + +DeclRanged::DeclRanged() : prefix_(), name_(), reps_() {} +DeclRanged::DeclRanged(std::string prefix, std::string name, SetPiece reps) + : prefix_(prefix), name_(name), reps_(reps) {} + +member_imp(DeclRanged, std::string, prefix); +member_imp(DeclRanged, std::string, name); +member_imp(DeclRanged, SetPiece, reps); + +std::ostream &operator<<(std::ostream &out, const DeclRanged &dr) +{ + out << dr.prefix() << " " << dr.name(); + + SetPiece mdi = dr.reps(); + unsigned int sz = mdi.size(); + if (sz > 0) { + out << "["; + unsigned int j = 0; + for (; j < sz - 1; j++) { + out << cardinal(mdi[j]) << ", "; + } + out << cardinal(mdi[j]); + out << "]"; + } + out << ";"; + + return out; +} + +std::ostream &operator<<(std::ostream &out, const DeclsRanged &ddr) +{ + BOOST_FOREACH (DeclRanged dr, ddr) out << dr << "\n"; + + return out; +} + +VertexInfo::VertexInfo() : decl_() {} +VertexInfo::VertexInfo(DeclRanged decl) : decl_(decl) {} + +member_imp(VertexInfo, DeclRanged, decl); + +std::ostream &operator<<(std::ostream &out, const VertexInfo &vi) +{ + out << vi.decl(); + + return out; +} + +std::ostream &operator<<(std::ostream &out, const VerticesInfo &vvi) +{ + BOOST_FOREACH (VertexInfo vi, vvi) out << vi << "\n"; + + return out; +} + +UseRanged::UseRanged() : prefix_(), name_(), subs_(), suffix_() {} +UseRanged::UseRanged(std::string prefix, std::string name, MDLExp subs, std::string suffix) + : prefix_(prefix), name_(name), subs_(subs), suffix_(suffix) {} + +member_imp(UseRanged, std::string, prefix); +member_imp(UseRanged, std::string, name); +member_imp(UseRanged, MDLExp, subs); +member_imp(UseRanged, std::string, suffix); + +std::ostream &operator<<(std::ostream &out, const UseRanged &ur) +{ + out << ur.prefix() << ur.name(); + + MDLExp mdle = ur.subs(); + unsigned int sz = mdle.size(); + if (sz > 0) { + out << "["; + unsigned int j = 0; + for (; j < sz - 1; j++) out << mdle[j] << ", "; + out << mdle[j]; + out << "]"; + } + + out << ur.suffix(); + + return out; +} + +std::ostream &operator<<(std::ostream &out, const UsesRanged &uur) +{ + BOOST_FOREACH (UseRanged ur, uur) out << ur; + + return out; +} + +RangedExpr::RangedExpr() : range_(), expr_() {} +RangedExpr::RangedExpr(SetPiece range, UsesRanged expr) : range_(range), expr_(expr) {} + +member_imp(RangedExpr, SetPiece, range); +member_imp(RangedExpr, UsesRanged, expr); + +std::string iterators[7] = {"i", "j", "k", "l", "m", "n", "o"}; + +std::ostream &operator<<(std::ostream &out, const RangedExpr &re) +{ + SetPiece mdi = re.range(); + unsigned int sz = mdi.size(); + if (sz > 0) { + out << "for "; + + unsigned int j = 0; + for (; j < sz - 1; j++) out << iterators[j] << " in " << mdi[j] << ", "; + out << iterators[j] << " in " << mdi[j]; + + out << " loop\n" << re.expr() << "\nend for;"; + } + + else out << re.expr(); + + return out; +} + +EdgeInfo::EdgeInfo() : expr_() {} +EdgeInfo::EdgeInfo(RangedExpr expr) : expr_(expr) {} + +member_imp(EdgeInfo, RangedExpr, expr); + +std::ostream &operator<<(std::ostream &out, const EdgeInfo &ei) +{ + out << ei.expr(); + + return out; +} + +std::ostream &operator<<(std::ostream &out, const EdgesInfo &eei) +{ + BOOST_FOREACH (EdgeInfo ei, eei) out << ei << "\n"; + + return out; +} + +} // namespace LIB + +} // namespace SBG diff --git a/sbg/info.hpp b/sbg/info.hpp new file mode 100755 index 0000000..4c89b30 --- /dev/null +++ b/sbg/info.hpp @@ -0,0 +1,127 @@ +/** @file info.hpp + + @brief SBG information structures + + These structures were designed to keep information about variable names and + equations definitions in the ModelicaCC context. In sense, they are an + interface between Modelica code and the SBG representation. They store + information to print Modelica-like code after each flatter/causalization + stage in which an SBG is used. + +
+ + This file is part of Set--Based Graph Library. + + SBG Library 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. + + SBG Library 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 SBG Library. If not, see . + + ******************************************************************************/ + +#ifndef SBG_INFO_HPP +#define SBG_INFO_HPP + +#include +#include + +namespace SBG { + +namespace LIB { + +/** @struct DeclRanged + * + * @brief DeclRanged is used to represent the declaration of an array of + * objects. + * + * In the Modelica context, the declaration of Real a[100] will result in the + * creation of interval [700:799], so there will be a VertexInfo vi with a + * DeclRanged dr such that dr.prefix = "Real", dr.name = "a", + * dr.reps = [700:799]. + */ + +struct DeclRanged { + member_class(std::string, prefix); + member_class(std::string, name); + member_class(SetPiece, reps); + + DeclRanged(); + DeclRanged(std::string prefix, std::string name, SetPiece reps); +}; +std::ostream &operator<<(std::ostream &out, const DeclRanged &rd); + +typedef std::vector DeclsRanged; +std::ostream &operator<<(std::ostream &out, const DeclsRanged &rd); + +struct VertexInfo { + member_class(DeclRanged, decl); + + VertexInfo(); + VertexInfo(DeclRanged decl); +}; +std::ostream &operator<<(std::ostream &out, const VertexInfo &vi); + +typedef std::vector VerticesInfo; +std::ostream &operator<<(std::ostream &out, const VerticesInfo &vvi); + +/** @struct UseRanged + * + * @brief UseRanged should keep info about the use of an array of variables. + * The trail member is used to specify any possible code between two + * consecutive arrays. + * + * For example, when the array "a" is used by a loop: + * for i [5:5:50] loop + * a[i] = b[i]; + * end for; + * the corresponding UseRanged ur will be such that ur.prefix = "", + * ur.name = "a", ur.subs = 1*x+0, ur.suffix = " = " + */ + +struct UseRanged { + member_class(std::string, prefix); + member_class(std::string, name); + member_class(MDLExp, subs); + member_class(std::string, suffix); + + UseRanged(); + UseRanged(std::string prefix, std::string name, MDLExp subs, std::string suffix); +}; +std::ostream &operator<<(std::ostream &out, const UseRanged &rd); + +typedef std::vector UsesRanged; +std::ostream &operator<<(std::ostream &out, const UsesRanged &uur); + +struct RangedExpr { + member_class(SetPiece, range); + member_class(UsesRanged, expr); + + RangedExpr(); + RangedExpr(SetPiece range, UsesRanged expr); +}; +std::ostream &operator<<(std::ostream &out, const RangedExpr &re); + +struct EdgeInfo { + member_class(RangedExpr, expr); + + EdgeInfo(); + EdgeInfo(RangedExpr expr); +}; +std::ostream &operator<<(std::ostream &out, const EdgeInfo &ri); + +typedef std::vector EdgesInfo; +std::ostream &operator<<(std::ostream &out, const EdgesInfo &eei); + +} // namespace LIB + +} // namespace SBG + +#endif