PandA-2024.02
memory_initialization_writer_base.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  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)
72  : TM(_TM),
73  behavioral_helper(_behavioral_helper),
74  reserved_mem_bytes(_reserved_mem_bytes),
75  written_bytes(0),
76  function_parameter(_function_parameter),
77  testbench_generation_memory_type(_testbench_generation_memory_type)
78 {
79  const auto parameter_type = tree_helper::CGetType(function_parameter);
80  status.push_back(std::make_pair(parameter_type, 0));
81 }
82 
84 {
86  {
87  THROW_ERROR("Not enough bytes written: " + STR(written_bytes) + " vs. " + STR(reserved_mem_bytes));
88  }
90  if(status.size() != 1)
91  {
92  THROW_ERROR("Missing data in C initialization string. Status is " + PrintStatus());
93  }
94 }
95 
97 {
98  THROW_ASSERT(status.size() >= 2, "");
99  status.pop_back();
100  status.back().second++;
101  size_t expected_size = 0;
102 
103  INDENT_DBG_MEX(DEBUG_LEVEL_VERY_PEDANTIC, debug_level, "<--GoUp - New status is " + PrintStatus());
104 
106  switch(GET_CONST_NODE(status.back().first)->get_kind())
107  {
108  case array_type_K:
109  {
110  const auto array_dims = tree_helper::GetArrayDimensions(status.back().first);
111  expected_size = array_dims.front();
112  break;
113  }
114  case complex_type_K:
115  expected_size = 2;
116  break;
117  case pointer_type_K:
118  expected_size = 0;
119  break;
120  case reference_type_K:
121  expected_size = 0;
122  break;
123  case record_type_K:
124  expected_size = tree_helper::CGetFieldTypes(status.back().first).size();
125  break;
126  case union_type_K:
127  expected_size = tree_helper::CGetFieldTypes(status.back().first).size();
128  if(expected_size > 1)
129  {
130  expected_size = 1;
131  }
132  break;
133  case boolean_type_K:
134  case CharType_K:
135  case enumeral_type_K:
136  case integer_type_K:
137  case real_type_K:
138  case function_type_K:
139  case lang_type_K:
140  case method_type_K:
141  case nullptr_type_K:
142  case offset_type_K:
143  case qual_union_type_K:
144  case set_type_K:
145  case template_type_parm_K:
146  case typename_type_K:
147  case type_argument_pack_K:
148  case type_pack_expansion_K:
149  case vector_type_K:
150  case void_type_K:
151  THROW_ERROR("Unexpected type in initializing parameter/variable: " +
152  GET_CONST_NODE(status.back().first)->get_kind_text());
153  break;
154  case aggr_init_expr_K:
155  case binfo_K:
156  case block_K:
157  case call_expr_K:
158  case case_label_expr_K:
159  case constructor_K:
160  case error_mark_K:
161  case identifier_node_K:
162  case ssa_name_K:
163  case statement_list_K:
164  case target_expr_K:
165  case target_mem_ref_K:
166  case target_mem_ref461_K:
167  case tree_list_K:
168  case tree_vec_K:
169  case lut_expr_K:
170  case CASE_CPP_NODES:
172  case CASE_CST_NODES:
173  case CASE_DECL_NODES:
174  case CASE_FAKE_NODES:
175  case CASE_GIMPLE_NODES:
176  case CASE_PRAGMA_NODES:
180  default:
182  "Not supported node: " + GET_CONST_NODE(status.back().first)->get_kind_text());
183  }
184  if(expected_size != 0 && (expected_size != status.back().second))
185  {
186  THROW_ERROR("Missing data in C initialization for node of type " +
187  GET_CONST_NODE(status.back().first)->get_kind_text() + " " + STR(expected_size) + " vs. " +
188  STR(status.back().second));
189  }
190 }
191 
193 {
194  THROW_ASSERT(!status.empty(), "");
195  const auto& type_node = status.back().first;
196  const auto new_type = [&]() -> tree_nodeConstRef {
197  if(tree_helper::IsStructType(type_node) || tree_helper::IsUnionType(type_node))
198  {
199  const auto fields = tree_helper::CGetFieldTypes(type_node);
200  THROW_ASSERT(fields.size() > status.back().second, STR(fields.size()) + " vs. " + STR(status.back().second));
201  return fields[status.back().second];
202  }
203  if(tree_helper::IsArrayType(type_node))
204  {
205  return tree_helper::CGetElements(type_node);
206  }
207  if(tree_helper::IsPointerType(type_node))
208  {
209  return tree_helper::CGetPointedType(type_node);
210  }
211  THROW_ERROR("Unexpected nested initialization " + GET_CONST_NODE(type_node)->get_kind_text() +
212  " - Current status is " + PrintStatus());
213  return tree_nodeConstRef();
214  }();
215  status.push_back(std::make_pair(new_type, 0));
216  INDENT_DBG_MEX(DEBUG_LEVEL_VERY_PEDANTIC, debug_level, "-->Going down. New level " + PrintStatus());
217 }
218 
220 {
221  INDENT_DBG_MEX(DEBUG_LEVEL_VERY_PEDANTIC, debug_level, "-->Updating the status from " + PrintStatus());
222  THROW_ASSERT(status.size() >= 2, "");
223  const auto& upper_type = status[status.size() - 2].first;
224  if(tree_helper::IsStructType(upper_type))
225  {
226  INDENT_DBG_MEX(DEBUG_LEVEL_VERY_PEDANTIC, debug_level, "---Read field of a record");
228  const auto record_fields = tree_helper::CGetFieldTypes(upper_type);
229 
231 #ifndef NDEBUG
232  const auto read_fields = status[status.size() - 2].second;
233  THROW_ASSERT(read_fields < record_fields.size(), "");
234 #endif
235  status.pop_back();
236  status.back().second++;
237  const auto new_type = record_fields[status[status.size() - 1].second];
238  status.push_back(std::make_pair(new_type, 0));
239  }
240  else
241  {
243  GET_CONST_NODE(upper_type)->get_kind_text());
244  status[status.size() - 2].second++;
245  status[status.size() - 1].second = 0;
246  }
247  INDENT_DBG_MEX(DEBUG_LEVEL_VERY_PEDANTIC, debug_level, "<--Updated the status to " + PrintStatus());
248 }
249 
251 {
252  std::string ret;
253  for(const auto& level : status)
254  {
255  if(ret != "")
256  {
257  ret += ":";
258  }
259  ret += GET_CONST_NODE(level.first)->get_kind_text() + "[" + STR(level.second) + "]";
260  }
261  return ret;
262 }
static std::vector< tree_nodeConstRef > CGetFieldTypes(const tree_nodeConstRef &type)
Return the fields type of a variable of type struct.
#define DEBUG_LEVEL_VERY_PEDANTIC
extremely verbose debugging print is performed.
static bool IsUnionType(const tree_nodeConstRef &type)
Return if treenode is an union.
#define INDENT_DBG_MEX(dbgLevel, curDbgLevel, mex)
We are producing a debug version of the program, so the message is printed;.
static std::vector< unsigned long long > GetArrayDimensions(const tree_nodeConstRef &node)
Return the dimension of the array.
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
const unsigned long int reserved_mem_bytes
The number of bytes to be written.
void GoUp() override
Ends the initialization of the current aggregated data structure.
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...
static bool IsArrayEquivType(const tree_nodeConstRef &type)
Return true if treenode is an array or it is equivalent to an array (record recursively having a sing...
#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
static tree_nodeConstRef CGetElements(const tree_nodeConstRef &type)
Given an array or a vector return the element type.
Generate HDL testbench for the top-level kernel testing.
exceptions managed by PandA
const std::string PrintStatus() const
Print the current status.
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.
static bool IsArrayType(const tree_nodeConstRef &type)
Return true if treenode is an array.
#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
Abstract base class for functor used to write initialization of the memory.
#define GET_CONST_NODE(t)
Definition: tree_node.hpp:347
refcount< const tree_node > tree_nodeConstRef
Definition: tree_node.hpp:213
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
This file collects some utility functions.
void CheckEnd() override
Check that all the necessary information was present in the initialization string.
#define CASE_CST_NODES
This macro collects all case labels for cast nodes.
Definition: tree_node.hpp:689
struct definition of the type node structures.
Definition: tree_node.hpp:1318
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
Template borrowed from the ANTLR library by Terence Parr (http://www.jGuru.com - Software rights: htt...
Definition: refcount.hpp:94
static tree_nodeConstRef CGetType(const tree_nodeConstRef &node)
Return the treenode of the type of node.
static bool IsStructType(const tree_nodeConstRef &type)
Return true if treenode is a record.
#define THROW_ERROR_CODE(code, str_expr)
helper function used to throw an error with a code error
Definition: exceptions.hpp:266
MemoryInitializationWriterBase(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.
int level
Definition: main.c:98
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
static bool IsPointerType(const tree_nodeConstRef &type)
Return true if treenode index is a pointer.
void GoNext() override
Consume an element of an aggregated data structure.
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.
void GoDown() override
Start the initialization of a new aggregated data structure.
#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