PandA-2024.02
var_pp_functor.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 "var_pp_functor.hpp"
46 #include "behavioral_helper.hpp"
47 #include <cstddef> // for size_t
48 
50  bool _add_restrict)
51  : pointer_based_variables(vars), BH(_BH), std_functor(std_var_pp_functor(_BH)), add_restrict(_add_restrict)
52 {
53 }
54 
55 std::string std_var_pp_functor::operator()(unsigned int var) const
56 {
57  if(BH->is_an_indirect_ref(var))
58  {
59  return BH->PrintVariable(var);
60  }
61  if(BH->is_an_array_ref(var))
62  {
63  unsigned int array = BH->get_array_ref_array(var);
64  unsigned int index = BH->get_array_ref_index(var);
65  if(BH->is_a_mem_ref(array))
66  {
67  unsigned int base = BH->get_mem_ref_base(array);
68  unsigned int offset = BH->get_mem_ref_offset(array);
69  unsigned int type = BH->get_type(array);
70  std::string offset_str = this->operator()(offset);
71  std::string type_string = BH->print_type(type);
72  if(BH->is_an_array(type) && !BH->is_a_struct(type) && !BH->is_an_union(type))
73  {
74  size_t found_square_bracket = type_string.find('[');
75  if(found_square_bracket != std::string::npos)
76  {
77  type_string.insert(found_square_bracket, "(*)");
78  }
79  else
80  {
81  type_string = type_string + "*";
82  }
83  }
84  else
85  {
86  type_string = type_string + "*";
87  }
88  if(offset_str == "0")
89  {
90  return "(*((" + type_string + ")(" + this->operator()(base) + ")))" + "[" + this->operator()(index) + "]";
91  }
92  else
93  {
94  return "(*((" + type_string + ")((unsigned char*)" + this->operator()(base) + " + " + offset_str + ")))" +
95  "[" + this->operator()(index) + "]";
96  }
97  }
98  else
99  {
100  return this->operator()(array) + "[" + this->operator()(index) + "]";
101  }
102  }
103  if(BH->is_a_component_ref(var))
104  {
105  unsigned int record = BH->get_component_ref_record(var);
106  unsigned int field = BH->get_component_ref_field(var);
107  return "(" + this->operator()(record) + ")." + this->operator()(field);
108  }
109  if(BH->is_an_addr_expr(var))
110  {
111  unsigned int pointed = BH->get_operand_from_unary_expr(var);
112  if(BH->is_a_mem_ref(pointed))
113  {
114  unsigned int base = BH->get_mem_ref_base(pointed);
115  unsigned int offset = BH->get_mem_ref_offset(pointed);
116  unsigned int type = BH->get_type(pointed);
117  std::string type_string = BH->print_type(type);
118  if(BH->is_an_array(type) && !BH->is_a_struct(type) && !BH->is_an_union(type))
119  {
120  size_t found_square_bracket = type_string.find('[');
121  if(found_square_bracket != std::string::npos)
122  {
123  type_string.insert(found_square_bracket, "(*)");
124  }
125  else
126  {
127  type_string = type_string + "*";
128  }
129  }
130  else
131  {
132  type_string = type_string + "*";
133  }
134  return "((" + type_string + ")((unsigned char*)" + this->operator()(base) + " + " + this->operator()(offset) +
135  "))";
136  }
137  else
138  {
139  return "(&(" + this->operator()(pointed) + "))";
140  }
141  }
142  if(BH->is_a_realpart_expr(var))
143  {
144  unsigned int complex = BH->get_operand_from_unary_expr(var);
145  return "__real__ " + this->operator()(complex);
146  }
147  if(BH->is_a_imagpart_expr(var))
148  {
149  unsigned int complex = BH->get_operand_from_unary_expr(var);
150  return "__imag__ " + this->operator()(complex);
151  }
152  return BH->PrintVariable(var);
153 }
154 
155 std::string pointer_var_pp_functor::operator()(unsigned int var) const
156 {
157  if(pointer_based_variables.find(var) == pointer_based_variables.end() and
158  (not BH->IsDefaultSsaName(var) or
160  {
161  if(BH->is_an_indirect_ref(var))
162  {
163  unsigned int pointer = BH->get_indirect_ref_var(var);
164  if(pointer_based_variables.find(pointer) != pointer_based_variables.end())
165  {
166  if(add_restrict)
167  {
168  return "*__restrict__ " + BH->PrintVariable(var);
169  }
170  else
171  {
172  return "*" + BH->PrintVariable(var);
173  }
174  }
175  else
176  {
177  return BH->PrintVariable(var);
178  }
179  }
180  if(BH->is_an_array_ref(var))
181  {
182  unsigned int array = BH->get_array_ref_array(var);
183  unsigned int index = BH->get_array_ref_index(var);
184  return std_functor(array) + "[" + this->operator()(index) + "]";
185  }
186  if(BH->is_a_component_ref(var))
187  {
188  unsigned int record = BH->get_component_ref_record(var);
189  unsigned int field = BH->get_component_ref_field(var);
190  return "(" + this->operator()(record) + ")." + this->operator()(field);
191  }
192  if(BH->is_an_addr_expr(var))
193  {
194  unsigned int pointed = BH->get_operand_from_unary_expr(var);
195  if(BH->is_an_array(pointed) && !BH->is_a_struct(pointed) && !BH->is_an_union(pointed))
196  {
197  return this->operator()(pointed);
198  }
199  else
200  {
201  return "&(" + this->operator()(pointed) + ")";
202  }
203  }
204  if(BH->is_a_realpart_expr(var))
205  {
206  unsigned int complex = BH->get_operand_from_unary_expr(var);
207  return "__real__ " + this->operator()(complex);
208  }
209  if(BH->is_a_imagpart_expr(var))
210  {
211  unsigned int complex = BH->get_operand_from_unary_expr(var);
212  return "__imag__ " + this->operator()(complex);
213  }
214  return BH->PrintVariable(var);
215  }
216  else
217  {
218  if(BH->is_an_array(var) && !BH->is_a_struct(var) && !BH->is_an_union(var))
219  {
220  return BH->PrintVariable(var);
221  }
222  else if(add_restrict)
223  {
224  return "*__restrict__ " + BH->PrintVariable(var);
225  }
226  else
227  {
228  return "*" + BH->PrintVariable(var);
229  }
230  }
231 }
232 
234  const CustomSet<unsigned int> pointer_vars)
235  : addr_based_variables(vars), pointer_based_variables(pointer_vars), BH(_BH)
236 {
237 }
238 
239 std::string address_var_pp_functor::operator()(unsigned int var) const
240 {
243  if(pointer_based_variables.find(var) != pointer_based_variables.end())
244  {
245  if(addr_based_variables.find(var) != addr_based_variables.end())
246  {
247  return BH->PrintVariable(var);
248  }
249  else
250  {
251  return "*" + BH->PrintVariable(var);
252  }
253  }
254  else
255  {
256  if(addr_based_variables.find(var) != addr_based_variables.end())
257  {
258  if(BH->is_an_array(var) && !BH->is_a_struct(var) && !BH->is_an_union(var))
259  {
260  return BH->PrintVariable(var);
261  }
262  else
263  {
264  return "&" + BH->PrintVariable(var);
265  }
266  }
267  else
268  {
269  return BH->PrintVariable(var);
270  }
271  }
272  // not reachable point
273  return "";
274 }
275 
276 std::string isolated_var_pp_functor::operator()(unsigned int var) const
277 {
278  if(BH->is_an_indirect_ref(var))
279  {
280  if(repl_var == var)
281  {
282  return var_string;
283  }
284  else
285  {
286  return BH->PrintVariable(var);
287  }
288  }
289  if(BH->is_an_array_ref(var))
290  {
291  unsigned int array = BH->get_array_ref_array(var);
292  unsigned int index = BH->get_array_ref_index(var);
293  if(BH->is_a_mem_ref(array))
294  {
295  unsigned int base = BH->get_mem_ref_base(array);
296  unsigned int offset = BH->get_mem_ref_offset(array);
297  unsigned int type = BH->get_type(array);
298  std::string offset_str = this->operator()(offset);
299  std::string type_string = BH->print_type(type);
300  if(BH->is_an_array(type) && !BH->is_a_struct(type) && !BH->is_an_union(type))
301  {
302  size_t found_square_bracket = type_string.find('[');
303  if(found_square_bracket != std::string::npos)
304  {
305  type_string.insert(found_square_bracket, "(*)");
306  }
307  else
308  {
309  type_string = type_string + "*";
310  }
311  }
312  else
313  {
314  type_string = type_string + "*";
315  }
316  if(offset_str == "0")
317  {
318  return "(*((" + type_string + ")(" + this->operator()(base) + ")))" + "[" + this->operator()(index) + "]";
319  }
320  else
321  {
322  return "(*((" + type_string + ")((unsigned char*)" + this->operator()(base) + " + " + offset_str + ")))" +
323  "[" + this->operator()(index) + "]";
324  }
325  }
326  else
327  {
328  return this->operator()(array) + "[" + this->operator()(index) + "]";
329  }
330  }
331  if(BH->is_a_component_ref(var))
332  {
333  unsigned int record = BH->get_component_ref_record(var);
334  unsigned int field = BH->get_component_ref_field(var);
335  return "(" + this->operator()(record) + ")." + this->operator()(field);
336  }
337  if(BH->is_an_addr_expr(var))
338  {
339  unsigned int pointed = BH->get_operand_from_unary_expr(var);
340  if(BH->is_a_mem_ref(pointed))
341  {
342  unsigned int base = BH->get_mem_ref_base(pointed);
343  unsigned int offset = BH->get_mem_ref_offset(pointed);
344  unsigned int type = BH->get_type(pointed);
345  std::string type_string = BH->print_type(type);
346  if(BH->is_an_array(type) && !BH->is_a_struct(type) && !BH->is_an_union(type))
347  {
348  size_t found_square_bracket = type_string.find('[');
349  if(found_square_bracket != std::string::npos)
350  {
351  type_string.insert(found_square_bracket, "(*)");
352  }
353  else
354  {
355  type_string = type_string + "*";
356  }
357  }
358  else
359  {
360  type_string = type_string + "*";
361  }
362  return "((" + type_string + ")((unsigned char*)" + this->operator()(base) + " + " + this->operator()(offset) +
363  "))";
364  }
365  else
366  {
367  return "(&(" + this->operator()(pointed) + "))";
368  }
369  }
370  if(BH->is_a_realpart_expr(var))
371  {
372  unsigned int complex = BH->get_operand_from_unary_expr(var);
373  return "__real__ " + this->operator()(complex);
374  }
375  if(BH->is_a_imagpart_expr(var))
376  {
377  unsigned int complex = BH->get_operand_from_unary_expr(var);
378  return "__imag__ " + this->operator()(complex);
379  }
380  if(repl_var == var)
381  {
382  return var_string;
383  }
384  else
385  {
386  return BH->PrintVariable(var);
387  }
388 }
virtual bool is_an_array_ref(unsigned int variable) const
Return true if the index is an array ref.
const CustomSet< unsigned int > addr_based_variables
reference to the set of variable that has to have a star in front when returned by operator() ...
address_var_pp_functor(const BehavioralHelperConstRef _BH, const CustomSet< unsigned int > vars, const CustomSet< unsigned int > pointer_vars)
Constructor.
virtual bool is_a_mem_ref(unsigned int variable) const
Return true if the index is a mem_ref.
virtual unsigned int get_component_ref_record(unsigned int obj) const
Return the record variable of a component ref.
char base
This version is stamped on May 10, 2016.
Definition: nussinov.c:24
Definition: fft.h:13
virtual unsigned int get_mem_ref_offset(unsigned int obj) const
Return the offset of a mem ref.
virtual std::string print_type(unsigned int type, bool global=false, bool print_qualifiers=false, bool print_storage=false, unsigned int var=0, const var_pp_functorConstRef vppf=var_pp_functorConstRef(), const std::string &prefix="", const std::string &tail="") const
Print a type and its variable in case var is not zero.
virtual unsigned int get_operand_from_unary_expr(unsigned int obj) const
Return the index of the operand if index is addr_expr, a realpart_expr or a imagpart_expr.
const CustomSet< unsigned int > pointer_based_variables
reference to the set of variable that has to have a star in front when returned by operator() ...
virtual bool is_an_array(unsigned int variable) const
Return true if index is a variable or a type of type array.
bool IsDefaultSsaName(const unsigned int ssa_name_index) const
Return true if node is the default ssa_name.
virtual bool is_a_realpart_expr(unsigned int obj) const
Return true if index is a realpart_expr.
std::string operator()(unsigned int var) const override
return the name of the variable with a star as a prefix.
const std_var_pp_functor std_functor
standard functor used for print array variable
Standard functor that returns the name of a variable.
virtual unsigned int get_array_ref_array(unsigned int obj) const
Return the array variable of an array ref.
const BehavioralHelperConstRef BH
behavioral helper
bool add_restrict
it controls the addition to the parameters declarations of the restrict keyword.
#define index(x, y)
Definition: Keccak.c:74
virtual unsigned int get_mem_ref_base(unsigned int obj) const
Return the base of a mem ref.
unsigned offset[NUM_VERTICES+1]
Definition: graph.h:3
std::string PrintVariable(unsigned int var) const
Print the name of the variable associated to the index.
std::string operator()(unsigned int var) const override
return the name of the variable with a star as a prefix.
virtual bool is_an_addr_expr(unsigned int variable) const
Return true if the index is an addr_expr.
virtual bool is_a_struct(unsigned int variable) const
Return true if index is a variable or a type of type struct.
std::string operator()(unsigned int var) const override
return the name of the variable.
pointer_var_pp_functor(const BehavioralHelperConstRef _BH, const CustomSet< unsigned int > vars, bool _add_restrict=false)
Constructor.
virtual unsigned int GetVarFromSsa(unsigned int index) const
Return the index of the variable base of a ssa var.
virtual bool is_a_imagpart_expr(unsigned int obj) const
Return true if index is a imagpart_expr.
virtual bool is_an_union(unsigned int variable) const
Return true if index is a variable of a type of type union.
virtual unsigned int get_indirect_ref_var(unsigned int obj) const
Return the variable of an indirect ref.
virtual unsigned int get_type(const unsigned int var) const
Return the type of the variable.
virtual unsigned int get_component_ref_field(unsigned int obj) const
Return the field index of a component ref.
virtual bool is_a_component_ref(unsigned int variable) const
Return true if the index is a component ref.
virtual bool is_an_indirect_ref(unsigned int variable) const
Return true if the index is an indirect ref.
const BehavioralHelperConstRef BH
behavioral helper
int array[ARRAY_SIZE]
Definition: add.h:1
std::string operator()(unsigned int var) const override
return the name of the variable.
virtual unsigned int get_array_ref_index(unsigned int obj) const
Return the index variable of an array ref.
const CustomSet< unsigned int > pointer_based_variables
reference to the set of variable that has to have a star in front when returned by operator() ...

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