PandA-2024.02
LatticeWrapper.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  */
44 #include "LatticeWrapper.hpp"
46 
47 #include "DesignParameters.hpp"
48 #include "Parameter.hpp"
49 #include "constant_strings.hpp"
50 #include "fileIO.hpp"
51 #include "string_manipulation.hpp"
52 #include "xml_dom_parser.hpp"
53 #include "xml_script_command.hpp"
54 #include <boost/algorithm/string.hpp>
55 #include <filesystem>
56 #include <fstream>
57 
58 LatticeWrapper::LatticeWrapper(const ParameterConstRef& _Param, const std::string& _tool_exec,
59  const generic_deviceRef& _device, const std::string& _output_dir,
60  const std::string& _default_output_dir)
61  : SynthesisTool(_Param, _tool_exec, _device, _output_dir, _default_output_dir)
62 {
63 }
64 
66 
67 void LatticeWrapper::generate_synthesis_script(const DesignParametersRef& dp, const std::string& file_name)
68 {
69  // Export reserved (constant) values to design parameters
70  for(auto& var : xml_reserved_vars)
71  {
72  dp->assign(var->name, getStringValue(var, dp), false);
73  }
74 
75  // Bare script generation
76  std::ostringstream script;
77  script << "##########################################################" << std::endl;
78  script << "# Automatically generated by the PandA framework #" << std::endl;
79  script << "##########################################################" << std::endl << std::endl;
81 
82  // Replace all reserved variables with their value
83  std::string script_string = script.str();
84  replace_parameters(dp, script_string);
86  remove_escaped(script_string);
87 
88  // Save the generated script
89  if(std::filesystem::exists(file_name))
90  {
91  std::filesystem::remove_all(file_name);
92  }
93  script_name = file_name;
94  std::ofstream file_stream(file_name.c_str());
95  file_stream << script_string << std::endl;
96  file_stream.close();
97 }
98 
100 {
101  std::ostringstream s;
102  s << get_tool_exec() << " " << script_name;
103  for(const auto& option : xml_tool_options)
104  {
105  if(option->checkCondition(dp))
106  {
107  std::string value = toString(option, dp);
108  replace_parameters(dp, value);
109  s << " " << value;
110  }
111  }
112  s << std::endl;
113  return s.str();
114 }
115 
117 {
118  switch(node->nodeType)
119  {
120  case NODE_VARIABLE:
121  {
122  std::string result;
123  const xml_set_variable_t* var = GetPointer<xml_set_variable_t>(node);
124  if(var->singleValue)
125  {
126  result += *(var->singleValue);
127  }
128  else if(!var->multiValues.empty())
129  {
130  result += "{";
131  for(auto it = var->multiValues.begin(); it != var->multiValues.end(); ++it)
132  {
133  const xml_set_entry_tRef e = *it;
134  if(it != var->multiValues.begin())
135  {
136  result += " ";
137  }
138  result += toString(e, dp);
139  }
140  result += "}";
141  }
142  else
143  {
144  result += "\"\"";
145  }
146  return result;
147  }
148  case NODE_UNKNOWN:
149  case NODE_ENTRY:
150  case NODE_PARAMETER:
151  case NODE_COMMAND:
152  case NODE_SHELL:
153  case NODE_ITE_BLOCK:
154  case NODE_FOREACH:
155  {
156  THROW_ERROR("Not supported node type: " + STR(node->nodeType));
157  break;
158  }
159  default:
160  {
161  THROW_UNREACHABLE("");
162  }
163  }
165  return "";
166 }
167 
169 {
170  switch(node->nodeType)
171  {
172  case NODE_ENTRY:
173  {
174  const xml_set_entry_t* ent = GetPointer<xml_set_entry_t>(node);
175  return ent->value;
176  }
177  case NODE_VARIABLE:
178  {
179  const xml_set_variable_t* var = GetPointer<xml_set_variable_t>(node);
180  return "set " + var->name + " " + getStringValue(node, dp) + ";";
181  }
182  case NODE_PARAMETER:
183  {
184  const xml_parameter_t* par = GetPointer<xml_parameter_t>(node);
185  std::string result;
186  if(par->name)
187  {
188  result += *(par->name);
189  }
190  if(par->name && (par->singleValue || !par->multiValues.empty()))
191  {
192  result += par->separator;
193  }
194  if(par->singleValue)
195  {
196  result += *(par->singleValue);
197  }
198  else if(!par->multiValues.empty())
199  {
200  result += par->curlyBrackets ? "{" : "\"";
201  for(auto it = par->multiValues.begin(); it != par->multiValues.end(); ++it)
202  {
203  const xml_set_entry_tRef p = *it;
204  if(it != par->multiValues.begin())
205  {
206  result += " ";
207  }
208  result += toString(p, dp);
209  }
210  result += par->curlyBrackets ? "}" : "\"";
211  }
212  return result;
213  }
214  case NODE_COMMAND:
215  {
216  const xml_command_t* comm = GetPointer<xml_command_t>(node);
217  // TODO: Evaluate the condition
218  std::string result;
219  if(comm->name)
220  {
221  result += *(comm->name);
222  }
223  if(comm->name && comm->value)
224  {
225  result += " ";
226  }
227  if(comm->value)
228  {
229  result += *(comm->value);
230  }
231  if(!comm->parameters.empty())
232  {
233  for(const auto& p : comm->parameters)
234  {
235  result += " " + toString(p, dp);
236  }
237  }
238  if(comm->output)
239  {
240  result += " >> " + *(comm->output);
241  }
242  return result;
243  }
244  case NODE_SHELL:
245  {
246  const xml_shell_t* sh = GetPointer<xml_shell_t>(node);
247  // TODO: Evaluate the condition
248  std::string result = "sh ";
249  if(sh->name)
250  {
251  result += *(sh->name);
252  }
253  if(sh->name && sh->value)
254  {
255  result += " ";
256  }
257  if(sh->value)
258  {
259  result += *(sh->value);
260  }
261  if(!sh->parameters.empty())
262  {
263  for(const auto& p : sh->parameters)
264  {
265  result += " " + toString(p, dp);
266  }
267  }
268  if(sh->output)
269  {
270  result += " >> " + *(sh->output);
271  }
272  return result;
273  }
274  case NODE_ITE_BLOCK:
275  {
276  const xml_ite_block_t* ite = GetPointer<xml_ite_block_t>(node);
277  std::string result;
278  bool conditionValue = xml_ite_block_t::evaluate_condition(&(ite->condition), dp), first = true;
279  const std::vector<xml_script_node_tRef>& block = conditionValue ? ite->thenNodes : ite->elseNodes;
280  for(const auto& n : block)
281  {
282  if(n->checkCondition(dp))
283  {
284  if(!first)
285  {
286  result += "\n";
287  }
288  first = false;
289  result += toString(n, dp);
290  }
291  }
292  return result;
293  }
294  case NODE_FOREACH:
295  case NODE_UNKNOWN:
296  {
297  THROW_ERROR("Not supported node type: " + STR(node->nodeType));
298  break;
299  }
300  default:
301  {
302  THROW_UNREACHABLE("");
303  }
304  }
306  return "";
307 }
~LatticeWrapper() override
Destructor.
void generate_synthesis_script(const DesignParametersRef &dp, const std::string &file_name) override
Creates the proper configuration script.
Command line parameter.
void remove_escaped(std::string &ioString)
Function converting all the escaped characters in the associated character.
std::string * value
LatticeWrapper(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::vector< xml_parameter_tRef > parameters
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.
Auxiliary methods for manipulating string.
static bool evaluate_condition(const std::string *condition)
Evaluates a string condition.
If/Then/Else block, evaluated at compile-time.
#define THROW_UNREACHABLE(str_expr)
helper function used to specify that some points should never be reached
Definition: exceptions.hpp:292
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.
Wrapper to synthesis tools by Lattice.
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
XML DOM parser.
utility function used to read files.
std::string * output
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
std::vector< xml_parameter_tRef > xml_tool_options
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.
std::string get_command_line(const DesignParametersRef &dp) const override
Returns the proper command line.
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
virtual std::string get_tool_exec() const
Returns the name of the tool executable.
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.
std::string toString(const xml_script_node_tRef node, const DesignParametersRef dp) const override
Returns the string-based representation of the XML element.
std::string * output
std::vector< xml_script_node_tRef > thenNodes
String entry of a multiple values variable (set).
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 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