PandA-2024.02
memory_initialization_writer.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) 2018-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  */
43 
45 #include "Parameter.hpp"
46 
48 #include "testbench_generation.hpp"
49 
51 #include <string>
52 
54 #include <utility>
55 
57 #include "behavioral_helper.hpp"
58 #include "tree_helper.hpp"
59 #include "tree_manager.hpp"
60 #include "tree_node.hpp"
61 #include "tree_reindex.hpp"
62 
64 #include "dbgPrintHelper.hpp"
65 #include "exceptions.hpp"
66 #include "utility.hpp"
67 
69  std::ofstream& _output_stream, const tree_managerConstRef _TM, const BehavioralHelperConstRef _behavioral_helper,
70  const unsigned long int _reserved_mem_bytes, const tree_nodeConstRef _function_parameter,
71  const TestbenchGeneration_MemoryType _testbench_generation_memory_type, const ParameterConstRef _parameters)
72  : MemoryInitializationWriterBase(_TM, _behavioral_helper, _reserved_mem_bytes, _function_parameter,
73  _testbench_generation_memory_type, _parameters),
74  output_stream(_output_stream)
75 {
76  debug_level = _parameters->get_class_debug_level(GET_CLASS(*this));
77 }
78 
79 void MemoryInitializationWriter::Process(const std::string& content)
80 {
82  "-->Writing " + content + " in binary form to initialize memory");
83  tree_nodeConstRef base_type;
86  "---Currently writing " + GET_CONST_NODE(status.back().first)->get_kind_text());
87  switch(GET_CONST_NODE(status.back().first)->get_kind())
88  {
89  case pointer_type_K:
90  base_type = tree_helper::CGetPointedType(status.back().first);
91  break;
92  case real_type_K:
93  case integer_type_K:
94  base_type = status.back().first;
95  break;
96  case array_type_K:
97  case boolean_type_K:
98  case CharType_K:
99  case enumeral_type_K:
100  case complex_type_K:
101  case record_type_K:
102  case union_type_K:
103  case function_type_K:
104  case lang_type_K:
105  case method_type_K:
106  case nullptr_type_K:
107  case offset_type_K:
108  case qual_union_type_K:
109  case reference_type_K:
110  case set_type_K:
111  case template_type_parm_K:
112  case typename_type_K:
113  case type_argument_pack_K:
114  case type_pack_expansion_K:
115  case vector_type_K:
116  case void_type_K:
117  THROW_ERROR("Unexpected type in initializing parameter/variable: " +
118  GET_CONST_NODE(status.back().first)->get_kind_text());
119  break;
120  case aggr_init_expr_K:
121  case binfo_K:
122  case block_K:
123  case call_expr_K:
124  case case_label_expr_K:
125  case constructor_K:
126  case error_mark_K:
127  case identifier_node_K:
128  case ssa_name_K:
129  case statement_list_K:
130  case target_expr_K:
131  case target_mem_ref_K:
132  case target_mem_ref461_K:
133  case tree_list_K:
134  case tree_vec_K:
135  case lut_expr_K:
136  case CASE_CPP_NODES:
138  case CASE_CST_NODES:
139  case CASE_DECL_NODES:
140  case CASE_FAKE_NODES:
141  case CASE_GIMPLE_NODES:
142  case CASE_PRAGMA_NODES:
146  default:
148  "Not supported node: " + GET_CONST_NODE(status.back().first)->get_kind_text());
149  }
150  THROW_ASSERT(base_type, "");
151  std::string binary_value = "";
152  unsigned long long size = 0;
153  switch(GET_CONST_NODE(base_type)->get_kind())
154  {
155  case integer_type_K:
156  size = tree_helper::Size(base_type);
157  binary_value = ConvertInBinary(content, size, false, tree_helper::IsUnsignedIntegerType(base_type));
158  break;
159  case real_type_K:
160  size = tree_helper::Size(base_type);
161  binary_value = ConvertInBinary(content, size, true, false);
162  break;
163  case pointer_type_K:
164  case array_type_K:
165  case boolean_type_K:
166  case CharType_K:
167  case enumeral_type_K:
168  case complex_type_K:
169  case record_type_K:
170  case union_type_K:
171  case function_type_K:
172  case lang_type_K:
173  case method_type_K:
174  case nullptr_type_K:
175  case offset_type_K:
176  case qual_union_type_K:
177  case reference_type_K:
178  case set_type_K:
179  case template_type_parm_K:
180  case typename_type_K:
181  case type_argument_pack_K:
182  case type_pack_expansion_K:
183  case vector_type_K:
184  case void_type_K:
185  THROW_ERROR("Unexpected type in initializing parameter/variable: " +
186  GET_CONST_NODE(base_type)->get_kind_text());
187  break;
188  case aggr_init_expr_K:
189  case binfo_K:
190  case block_K:
191  case call_expr_K:
192  case case_label_expr_K:
193  case constructor_K:
194  case error_mark_K:
195  case identifier_node_K:
196  case ssa_name_K:
197  case statement_list_K:
198  case target_expr_K:
199  case target_mem_ref_K:
200  case target_mem_ref461_K:
201  case tree_list_K:
202  case tree_vec_K:
203  case lut_expr_K:
204  case CASE_CPP_NODES:
206  case CASE_CST_NODES:
207  case CASE_DECL_NODES:
208  case CASE_FAKE_NODES:
209  case CASE_GIMPLE_NODES:
210  case CASE_PRAGMA_NODES:
214  default:
216  "Not supported node: " + GET_CONST_NODE(base_type)->get_kind_text());
217  }
218  THROW_ASSERT(binary_value.size() % 8 == 0, "");
219  written_bytes += binary_value.size() / 8;
221  {
223  output_stream << "//parameter: " << behavioral_helper->PrintVariable(function_parameter->index)
224  << " value: " << content << std::endl;
225  output_stream << "p" << binary_value << std::endl;
226  break;
228  output_stream << "//expected value for output " + behavioral_helper->PrintVariable(function_parameter->index) +
229  ": "
230  << content << std::endl;
231  for(size_t bit = 0; bit < binary_value.size(); bit += 8)
232  {
233  output_stream << "o" << binary_value.substr(binary_value.size() - 8 - bit, 8) << std::endl;
234  }
235  break;
237  output_stream << "//memory initialization for variable " +
238  behavioral_helper->PrintVariable(function_parameter->index) + " value: " + content
239  << std::endl;
240  for(size_t bit = 0; bit < binary_value.size(); bit += 8)
241  {
242  output_stream << "m" << binary_value.substr(binary_value.size() - 8 - bit, 8) << std::endl;
243  }
244  break;
246  if(GetPointer<const type_node>(GET_CONST_NODE(function_parameter)))
247  {
248  output_stream << "//expected value for return value" << std::endl;
249  output_stream << "o" << binary_value << std::endl;
250  }
251  else
252  {
253  THROW_UNREACHABLE("");
254  }
255  break;
256  default:
257  THROW_UNREACHABLE("");
258  }
260  "<--Written " + content + " (" + STR(binary_value.size() / 8) +
261  " bytes) in binary form to initialize memory");
262 }
#define DEBUG_LEVEL_VERY_PEDANTIC
extremely verbose debugging print is performed.
#define INDENT_DBG_MEX(dbgLevel, curDbgLevel, mex)
We are producing a debug version of the program, so the message is printed;.
File containing functions and utilities to support the printing of debug messagges.
const tree_nodeConstRef function_parameter
The variable/parameter being printed.
#define CASE_BINARY_EXPRESSION
This macro collects all case labels for binary_expr objects.
Definition: tree_node.hpp:463
#define GET_CLASS(obj)
Macro returning the actual type of an object.
std::vector< std::pair< const tree_nodeConstRef, size_t > > status
The stack representing the current status of the parser; the content is the last dumped element...
#define CASE_DECL_NODES
NOTE that cast_expr is a unary expression but it could not be included in the CASE_UNARY_EXPRESSION b...
Definition: tree_node.hpp:672
Functor used to write initialization of the memory writer.
Generate HDL testbench for the top-level kernel testing.
exceptions managed by PandA
std::string ConvertInBinary(const std::string &C_value, unsigned long long precision, const bool real_type, bool unsigned_type)
Convert a string storing a number in decimal format into a string in binary format.
Node not yet supported.
Definition: exceptions.hpp:323
unsigned long int written_bytes
The number of bytes currently written.
#define STR(s)
Macro which performs a lexical_cast to a string.
void Process(const std::string &content) override
Process an element.
Functor used to write initialization of the memory.
static bool IsUnsignedIntegerType(const tree_nodeConstRef &type)
Return true if the treenode is of unsigned integer type.
#define THROW_UNREACHABLE(str_expr)
helper function used to specify that some points should never be reached
Definition: exceptions.hpp:292
static unsigned long long Size(const tree_nodeConstRef &tn)
Return the size of a tree object.
#define CASE_QUATERNARY_EXPRESSION
This macro collects all case labels for quaternary_expr objects.
Definition: tree_node.hpp:574
#define CASE_UNARY_EXPRESSION
This macro collects all case labels for unary_expr objects.
Definition: tree_node.hpp:371
const BehavioralHelperConstRef behavioral_helper
The behavioral helper.
#define GET_CONST_NODE(t)
Definition: tree_node.hpp:347
Classes specification of the tree_node data structures.
This file collects some utility functions and macros.
#define THROW_ERROR(str_expr)
helper function used to throw an error in a standard way
Definition: exceptions.hpp:263
std::string PrintVariable(unsigned int var) const
Print the name of the variable associated to the index.
MemoryInitializationWriter(std::ofstream &output_stream, const tree_managerConstRef TM, const BehavioralHelperConstRef behavioral_helper, const unsigned long int reserved_mem_bytes, const tree_nodeConstRef function_parameter, const TestbenchGeneration_MemoryType testbench_generation_memory_type, const ParameterConstRef parameters)
Constructor.
This file collects some utility functions.
#define CASE_CST_NODES
This macro collects all case labels for cast nodes.
Definition: tree_node.hpp:689
Class specification of the tree_reindex support class.
#define CASE_FAKE_NODES
This macro collects all case labels for fake or empty nodes.
Definition: tree_node.hpp:635
std::ofstream & output_stream
The stream corresponding to the memory initialization file.
const TestbenchGeneration_MemoryType testbench_generation_memory_type
The type of initialization being written.
#define THROW_ERROR_CODE(code, str_expr)
helper function used to throw an error with a code error
Definition: exceptions.hpp:266
static tree_nodeConstRef CGetPointedType(const tree_nodeConstRef &pointer)
Return the pointed type of a pointer object.
this class is used to manage the command-line or XML options.
#define CASE_CPP_NODES
This macro collects all case labels for cpp nodes.
Definition: tree_node.hpp:644
#define CASE_GIMPLE_NODES
This macro collects all cases labels for gimple nodes.
Definition: tree_node.hpp:700
TestbenchGeneration_MemoryType
Enum class used to specify which type of content has to be printed for memory initialization.
#define CASE_TERNARY_EXPRESSION
This macro collects all case labels for ternary_expr objects.
Definition: tree_node.hpp:550
Class specification of the manager of the tree structures extracted from the raw file.
#define CASE_PRAGMA_NODES
This macro collects all case labels for pragma objects.
Definition: tree_node.hpp:610
#define THROW_ASSERT(cond, str_expr)
helper function used to check an assert and if needed to throw an error in a standard way ...
Definition: exceptions.hpp:289

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