PandA-2024.02
NanoXploreWrapper.cpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2004-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
42 #include "NanoXploreWrapper.hpp"
44 
45 #include "DesignParameters.hpp"
46 #include "Parameter.hpp"
47 #include "constant_strings.hpp"
48 #include "fileIO.hpp"
49 #include "utility.hpp"
50 #include "xml_dom_parser.hpp"
51 #include "xml_script_command.hpp"
52 #include <boost/algorithm/string.hpp>
53 #include <filesystem>
54 #include <fstream>
55 
56 NanoXploreWrapper::NanoXploreWrapper(const ParameterConstRef& _Param, const std::string& _tool_exec,
57  const generic_deviceRef& _device, const std::string& _output_dir,
58  const std::string& _default_output_dir)
59  : SynthesisTool(_Param, _tool_exec, _device, _output_dir, _default_output_dir)
60 {
61 }
62 
64 
65 void NanoXploreWrapper::generate_synthesis_script(const DesignParametersRef& dp, const std::string& file_name)
66 {
67  // Export reserved (constant) values to design parameters
68  for(auto& var : xml_reserved_vars)
69  {
70  dp->assign(var->name, getStringValue(var, dp), false);
71  }
72 
73  // Bare script generation
74  std::ostringstream script;
75  script << "##########################################################" << std::endl;
76  script << "# Automatically generated by the PandA framework #" << std::endl;
77  script << "##########################################################" << std::endl << std::endl;
79 
80  // Replace all reserved variables with their value
81  std::string script_string = script.str();
82  replace_parameters(dp, script_string);
84  remove_escaped(script_string);
85 
86  // Save the generated script
87  if(std::filesystem::exists(file_name))
88  {
89  std::filesystem::remove_all(file_name);
90  }
91  script_name = file_name;
92  std::ofstream file_stream(file_name.c_str());
93  file_stream << script_string << std::endl;
94  file_stream.close();
95 }
96 
98 {
99  switch(node->nodeType)
100  {
101  case NODE_VARIABLE:
102  {
103  std::string result;
104  const xml_set_variable_t* var = GetPointer<xml_set_variable_t>(node);
105  if(var->singleValue)
106  {
107  result += *(var->singleValue);
108  }
109  else if(!var->multiValues.empty())
110  {
111  result += "{";
112  for(auto it = var->multiValues.begin(); it != var->multiValues.end(); ++it)
113  {
114  const xml_set_entry_tRef e = *it;
115  if(it != var->multiValues.begin())
116  {
117  result += " ";
118  }
119  result += toString(e, dp);
120  }
121  result += "}";
122  }
123  else
124  {
125  result += "\"\"";
126  }
127  return result;
128  }
129  case NODE_COMMAND:
130  case NODE_ENTRY:
131  case NODE_FOREACH:
132  case NODE_ITE_BLOCK:
133  case NODE_PARAMETER:
134  case NODE_SHELL:
135  case NODE_UNKNOWN:
136  default:
137  THROW_ERROR("Not supported node type: " + STR(node->nodeType));
138  }
140  return "";
141 }
142 
144 {
145  switch(node->nodeType)
146  {
147  case NODE_ENTRY:
148  {
149  const xml_set_entry_t* ent = GetPointer<xml_set_entry_t>(node);
150  return ent->value;
151  }
152  case NODE_VARIABLE:
153  {
154  const xml_set_variable_t* var = GetPointer<xml_set_variable_t>(node);
155  return "set " + var->name + " " + getStringValue(node, dp) + ";";
156  }
157  case NODE_PARAMETER:
158  {
159  const xml_parameter_t* par = GetPointer<xml_parameter_t>(node);
160  std::string result;
161  if(par->name)
162  {
163  result += *(par->name);
164  }
165  if(par->name && (par->singleValue || !par->multiValues.empty()))
166  {
167  result += par->separator;
168  }
169  if(par->singleValue)
170  {
171  result += *(par->singleValue);
172  }
173  else if(!par->multiValues.empty())
174  {
175  result += par->curlyBrackets ? "{" : "\"";
176  for(auto it = par->multiValues.begin(); it != par->multiValues.end(); ++it)
177  {
178  const xml_set_entry_tRef p = *it;
179  if(it != par->multiValues.begin())
180  {
181  result += " ";
182  }
183  result += toString(p, dp);
184  }
185  result += par->curlyBrackets ? "}" : "\"";
186  }
187  return result;
188  }
189  case NODE_COMMAND:
190  {
191  const xml_command_t* comm = GetPointer<xml_command_t>(node);
192  // TODO: Evaluate the condition
193  std::string result;
194  if(comm->name)
195  {
196  result += *(comm->name);
197  }
198  if(comm->name && comm->value)
199  {
200  result += " ";
201  }
202  if(comm->value)
203  {
204  result += *(comm->value);
205  }
206  if(!comm->parameters.empty())
207  {
208  for(const auto& p : comm->parameters)
209  {
210  result += " " + toString(p, dp);
211  }
212  }
213  if(comm->output)
214  {
215  result += " >> " + *(comm->output);
216  }
217  return result;
218  }
219  case NODE_SHELL:
220  {
221  const xml_shell_t* sh = GetPointer<xml_shell_t>(node);
222  // TODO: Evaluate the condition
223  std::string result = "sh ";
224  if(sh->name)
225  {
226  result += *(sh->name);
227  }
228  if(sh->name && sh->value)
229  {
230  result += " ";
231  }
232  if(sh->value)
233  {
234  result += *(sh->value);
235  }
236  if(!sh->parameters.empty())
237  {
238  for(const auto& p : sh->parameters)
239  {
240  result += " " + toString(p, dp);
241  }
242  }
243  if(sh->output)
244  {
245  result += " >> " + *(sh->output);
246  }
247  return result;
248  }
249  case NODE_ITE_BLOCK:
250  {
251  const xml_ite_block_t* ite = GetPointer<xml_ite_block_t>(node);
252  std::string result;
253  bool conditionValue = xml_ite_block_t::evaluate_condition(&(ite->condition), dp), first = true;
254  const std::vector<xml_script_node_tRef>& block = conditionValue ? ite->thenNodes : ite->elseNodes;
255  for(const auto& n : block)
256  {
257  if(n->checkCondition(dp))
258  {
259  if(!first)
260  {
261  result += "\n";
262  }
263  first = false;
264  result += toString(n, dp);
265  }
266  }
267  return result;
268  }
269  case NODE_UNKNOWN:
270  case NODE_FOREACH:
271  default:
272  THROW_ERROR("Not supported node type: " + STR(node->nodeType));
273  }
275  return "";
276 }
Command line parameter.
void remove_escaped(std::string &ioString)
Function converting all the escaped characters in the associated character.
std::string * value
std::vector< xml_parameter_tRef > parameters
NanoXploreWrapper(const ParameterConstRef &Param, const std::string &tool_exec, const generic_deviceRef &device, const std::string &output_dir, const std::string &default_output_dir)
Constructor.
std::string * name
int sh
Definition: adpcm.c:176
std::vector< xml_script_node_tRef > elseNodes
#define STR(s)
Macro which performs a lexical_cast to a string.
static bool evaluate_condition(const std::string *condition)
Evaluates a string condition.
If/Then/Else block, evaluated at compile-time.
std::vector< xml_script_node_tRef > xml_script_nodes
std::string * name
void assign(const std::string &name, const std::string &value, bool checkExisting)
Assigns a value to a saved parameter.
std::vector< xml_set_variable_tRef > xml_reserved_vars
This file contains the definition of the parameters for the synthesis tools.
std::string script_name
name of the script
std::string * value
std::string toString(const xml_script_node_tRef node, const DesignParametersRef dp) const override
Returns the string-based representation of the XML element.
XML DOM parser.
utility function used to read files.
std::string * output
This file collects some utility functions and macros.
void replace_parameters(const DesignParametersRef &dp, std::string &script) const
Replaces occurrences of parameters inside a script.
#define THROW_ERROR(str_expr)
helper function used to throw an error in a standard way
Definition: exceptions.hpp:263
int result[SIZE]
Definition: adpcm.c:800
This struct specifies the block node.
Definition: tree_node.hpp:1820
std::vector< xml_parameter_tRef > parameters
Classes for handling configuration files.
Wrapper to synthesis tools by NanoXplore.
void generate_synthesis_script(const DesignParametersRef &dp, const std::string &file_name) override
Creates the proper configuration script.
std::string getStringValue(const xml_script_node_tRef node, const DesignParametersRef &dp) const override
Returns the string-based representation of the XML element.
std::string * singleValue
std::string value
std::vector< xml_set_entry_tRef > multiValues
std::vector< xml_set_entry_tRef > multiValues
Variable assignment, either single value or multiple entries set.
Template borrowed from the ANTLR library by Terence Parr (http://www.jGuru.com - Software rights: htt...
Definition: refcount.hpp:94
Command line of the synthesis tool.
this class is used to manage the command-line or XML options.
constant strings
Command line of the native shell.
~NanoXploreWrapper() override
Destructor.
std::string * output
std::vector< xml_script_node_tRef > thenNodes
String entry of a multiple values variable (set).
std::string generate_bare_script(const std::vector< xml_script_node_tRef > &nodes, const DesignParametersRef &dp)

Generated on Mon Feb 12 2024 13:02:56 for PandA-2024.02 by doxygen 1.8.13