PandA-2024.02
c_writer.hpp
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  */
48 #ifndef CWRITER_HPP
49 #define CWRITER_HPP
50 
51 #include "custom_map.hpp"
52 #include "custom_set.hpp"
53 #include "graph.hpp"
54 #include "refcount.hpp"
55 
56 #include <deque>
57 #include <list>
58 #include <vector>
59 
76 REF_FORWARD_DECL(machine_node);
77 
78 class OpVertexSet;
79 class graph;
80 class instrumented_call_instr_writer;
81 
82 template <typename Graph>
83 class dominance;
84 
91 class CWriter
92 {
93  protected:
96 
99 
102 
105 
107 
110 
114 
116  bool verbose;
117 
120 
123 
126 
127  unsigned int fake_max_tree_node_id;
128 
130  std::map<unsigned int, std::string> basic_block_prefix;
131 
133  std::map<unsigned int, std::string> basic_block_tail;
134 
136  std::map<vertex, std::map<unsigned int, std::string>> renaming_table;
137 
140  std::map<unsigned int, std::string> basic_blocks_labels;
156  void writeRoutineInstructions_rec(vertex current_vertex, bool bracket, const unsigned int function_index);
157 
164  virtual void writePreInstructionInfo(const FunctionBehaviorConstRef, const vertex);
165 
172  virtual void writePostInstructionInfo(const FunctionBehaviorConstRef, const vertex);
173 
181  virtual void WriteBodyLoop(const unsigned int function_index, const unsigned int, vertex current_vertex,
182  bool bracket);
183 
187  void compute_phi_nodes(const FunctionBehaviorConstRef function_behavior, const OpVertexSet& instructions,
188  var_pp_functorConstRef variableFunctor);
189 
190  std::vector<std::string> additionalIncludes;
192 
200  void insert_copies(vertex b, const BBGraphConstRef bb_domGraph, const BBGraphConstRef bb_fcfgGraph,
201  var_pp_functorConstRef variableFunctor, const CustomSet<unsigned int>& phi_instructions,
202  std::map<unsigned int, unsigned int>& created_variables,
203  std::map<unsigned int, std::string>& symbol_table,
204  std::map<unsigned int, std::deque<std::string>>& array_of_stacks);
205 
209  void schedule_copies(vertex b, const BBGraphConstRef bb_domGraph, const BBGraphConstRef bb_fcfgGraph,
210  var_pp_functorConstRef variableFunctor, const CustomSet<unsigned int>& phi_instructions,
211  std::map<unsigned int, unsigned int>& created_variables,
212  std::map<unsigned int, std::string>& symbol_table, std::list<unsigned int>& pushed,
213  std::map<unsigned int, std::deque<std::string>>& array_of_stacks);
214 
220  unsigned int create_new_identifier(std::map<unsigned int, std::string>& symbol_table);
221 
229  void push_stack(std::string symbol_name, unsigned int dest_i, std::list<unsigned int>& pushed,
230  std::map<unsigned int, std::deque<std::string>>& array_of_stacks);
231 
237  void pop_stack(std::list<unsigned int>& pushed, std::map<unsigned int, std::deque<std::string>>& array_of_stacks);
238 
243 
244  /*
245  * writes code at the beginning of the basic block denoted by the
246  * identifier bb_number in the function function_index.
247  * the code is written before all the instructions
248  * of the BB, but after the goto label of the BB (if present)
249  */
250  virtual void WriteBBHeader(const unsigned int bb_number, const unsigned int function_index);
251 
252  protected:
261  CWriter(const HLS_managerConstRef _HLSMgr, const InstructionWriterRef instruction_writer,
262  const IndentedOutputStreamRef indented_output_stream, const ParameterConstRef Param, bool verbose = true);
263 
264  public:
265  virtual ~CWriter();
266 
275  static CWriterRef CreateCWriter(const CBackendInformationConstRef c_backend_information,
276  const HLS_managerConstRef hls_man,
277  const IndentedOutputStreamRef indented_output_stream,
278  const ParameterConstRef parameters, const bool verbose);
279 
283  virtual void Initialize();
284 
290 
296  const CustomSet<unsigned int> GetLocalVariables(const unsigned int function_id) const;
297 
302  virtual void WriteFunctionImplementation(unsigned int function_id);
303 
308  virtual void StartFunctionBody(const unsigned int function_id);
309 
314  virtual void WriteFunctionBody(const unsigned int function_id);
315 
320  virtual void EndFunctionBody(unsigned int funId);
321 
330  virtual void writeRoutineInstructions(const unsigned int function_index, const OpVertexSet& instructions,
331  var_pp_functorConstRef variableFunctor, vertex bb_start = NULL_VERTEX,
333 
337  virtual void WriteFunctionDeclaration(const unsigned int funId);
338 
342  virtual void WriteHeader();
343 
347  virtual void WriteGlobalDeclarations();
348 
353  virtual void writeInclude(const std::string& file_name);
354 
365  virtual void DeclareType(const tree_nodeConstRef& varType, const BehavioralHelperConstRef& behavioral_helper,
366  CustomSet<std::string>& locally_declared_type);
367 
377  virtual void DeclareVariable(const tree_nodeConstRef& curVar, CustomSet<unsigned int>& already_declared_variables,
378  CustomSet<std::string>& locally_declared_type,
379  const BehavioralHelperConstRef& behavioral_helper,
380  const var_pp_functorConstRef& varFunc);
381 
387  virtual void declare_cast_types(unsigned int funId, CustomSet<std::string>& locally_declared_types);
388 
398  virtual void DeclareLocalVariables(const CustomSet<unsigned int>& to_be_declared,
399  CustomSet<unsigned int>& already_declared_variables,
400  CustomSet<std::string>& already_declared_types,
401  const BehavioralHelperConstRef behavioral_helper,
402  const var_pp_functorConstRef varFunc);
403 
408  virtual void DeclareFunctionTypes(const tree_nodeConstRef& tn);
409 
414  virtual void WriteFile(const std::string& file_name);
415 
419  virtual void WriteBuiltinWaitCall();
420 };
421 
423 
424 #endif
OpGraphConstRef local_rec_cfgGraph
Definition: c_writer.hpp:148
const dominance< BBGraph > * dominators
Definition: c_writer.hpp:145
Dominator o post-dominator data structure.
Definition: Dominance.hpp:627
CustomOrderedSet< vertex > goto_list
Definition: c_writer.hpp:141
Definition of the node_info object for the basic_block graph.
int debug_level
the debug level
Definition: c_writer.hpp:122
const dominance< BBGraph > * post_dominators
Definition: c_writer.hpp:146
virtual void Initialize()
Initialize data structure.
Definition: c_writer.cpp:228
Class used to write the C code representing a program, this class can&#39;t be directly instantiated sinc...
Definition: c_writer.hpp:91
This class manages the tree structures extracted from the raw file.
const CustomSet< unsigned int > GetLocalVariables(const unsigned int function_id) const
Compute the local variables of a function.
Definition: c_writer.cpp:356
CustomOrderedSet< vertex > bb_frontier
Definition: c_writer.hpp:138
virtual void WriteBBHeader(const unsigned int bb_number, const unsigned int function_index)
Definition: c_writer.cpp:1918
void WriteHashTableImplementation()
Write the implementation of a hash table with long long int as key and long long int as value...
Definition: c_writer.cpp:1678
Class specification of the graph structures.
CustomSet< unsigned int > globallyDeclVars
Definition: c_writer.hpp:106
CONSTREF_FORWARD_DECL(BBGraph)
bool verbose
Verbosity means that a comment is printed for each line in the output file.
Definition: c_writer.hpp:116
void push_stack(std::string symbol_name, unsigned int dest_i, std::list< unsigned int > &pushed, std::map< unsigned int, std::deque< std::string >> &array_of_stacks)
push on the stack of temporary variables that has to replaced
Definition: c_writer.cpp:1658
const tree_managerConstRef TM
The tree manager.
Definition: c_writer.hpp:98
void pop_stack(std::list< unsigned int > &pushed, std::map< unsigned int, std::deque< std::string >> &array_of_stacks)
remove from the stack all the temporaries
Definition: c_writer.cpp:1666
redefinition of map to manage ordered/unordered structures
A set of operation vertices.
Definition: op_graph.hpp:654
virtual void WriteFunctionDeclaration(const unsigned int funId)
Writes the declaration of the function whose id in the tree is funId.
Definition: c_writer.cpp:372
Abstract pure class for the tree structure.
Definition: tree_node.hpp:139
void schedule_copies(vertex b, const BBGraphConstRef bb_domGraph, const BBGraphConstRef bb_fcfgGraph, var_pp_functorConstRef variableFunctor, const CustomSet< unsigned int > &phi_instructions, std::map< unsigned int, unsigned int > &created_variables, std::map< unsigned int, std::string > &symbol_table, std::list< unsigned int > &pushed, std::map< unsigned int, std::deque< std::string >> &array_of_stacks)
insert copies according the algorithm described in Briggs et.
Definition: c_writer.cpp:1470
virtual void WriteFile(const std::string &file_name)
Writes the final C file.
Definition: c_writer.cpp:1913
const IndentedOutputStreamRef indented_output_stream
Represents the stream we are currently writing to.
Definition: c_writer.hpp:101
Class used to describe a particular graph with basic blocks as nodes.
virtual void EndFunctionBody(unsigned int funId)
Writes the code necessary to close a function (this function was a function also present in the origi...
Definition: c_writer.cpp:409
unsigned map[NUM_VERTICES]
Definition: bfs.c:12
const ParameterConstRef Param
set of parameters
Definition: c_writer.hpp:119
virtual void StartFunctionBody(const unsigned int function_id)
Writes the body of the function to the specified stream.
Definition: c_writer.cpp:384
void compute_phi_nodes(const FunctionBehaviorConstRef function_behavior, const OpVertexSet &instructions, var_pp_functorConstRef variableFunctor)
Determines the instructions coming out from phi-node splitting.
Definition: c_writer.cpp:1110
CustomSet< std::string > globally_declared_types
This set contains the list of the non built_in types already declared in the global scope...
Definition: c_writer.hpp:104
virtual void DeclareType(const tree_nodeConstRef &varType, const BehavioralHelperConstRef &behavioral_helper, CustomSet< std::string > &locally_declared_type)
This method should be called only if the type associated with the variable is a non built_in type and...
Definition: c_writer.cpp:1278
redefinition of set to manage ordered/unordered structures
boost::graph_traits< graph >::vertex_descriptor vertex
vertex definition.
Definition: graph.hpp:1303
CWriter(const HLS_managerConstRef _HLSMgr, const InstructionWriterRef instruction_writer, const IndentedOutputStreamRef indented_output_stream, const ParameterConstRef Param, bool verbose=true)
Constructor of the class.
Definition: c_writer.cpp:140
General class used to describe a graph in PandA.
Definition: graph.hpp:771
virtual void DeclareVariable(const tree_nodeConstRef &curVar, CustomSet< unsigned int > &already_declared_variables, CustomSet< std::string > &locally_declared_type, const BehavioralHelperConstRef &behavioral_helper, const var_pp_functorConstRef &varFunc)
Declares the local variable; in case the variable used in the intialization of curVar hasn&#39;t been dec...
Definition: c_writer.cpp:1330
CustomOrderedSet< std::string > writtenIncludes
Definition: c_writer.hpp:191
Base class functor used by prettyPrintVertex to print variables.
void insert_copies(vertex b, const BBGraphConstRef bb_domGraph, const BBGraphConstRef bb_fcfgGraph, var_pp_functorConstRef variableFunctor, const CustomSet< unsigned int > &phi_instructions, std::map< unsigned int, unsigned int > &created_variables, std::map< unsigned int, std::string > &symbol_table, std::map< unsigned int, std::deque< std::string >> &array_of_stacks)
Compute the copy assignments needed by the phi nodes destruction Further details can be found in: ...
Definition: c_writer.cpp:1442
virtual void WriteBodyLoop(const unsigned int function_index, const unsigned int, vertex current_vertex, bool bracket)
Write the instructions belonging to a body loop.
Definition: c_writer.cpp:238
static CWriterRef CreateCWriter(const CBackendInformationConstRef c_backend_information, const HLS_managerConstRef hls_man, const IndentedOutputStreamRef indented_output_stream, const ParameterConstRef parameters, const bool verbose)
Factory method.
Definition: c_writer.cpp:159
Definition: loop.hpp:153
void writeRoutineInstructions_rec(vertex current_vertex, bool bracket, const unsigned int function_index)
Write recursively instructions belonging to a basic block of task or of a function.
Definition: c_writer.cpp:430
virtual void DeclareFunctionTypes(const tree_nodeConstRef &tn)
Declares the types of the parameters of a function.
Definition: c_writer.cpp:333
Template definition of refcount.
const HLS_managerConstRef HLSMgr
the hls manager
Definition: c_writer.hpp:95
virtual void WriteHeader()
Writes the header of the file.
Definition: c_writer.cpp:265
unsigned int create_new_identifier(std::map< unsigned int, std::string > &symbol_table)
create an identifier for the temporaries created by phi node destruction
Definition: c_writer.cpp:1641
std::map< unsigned int, std::string > basic_blocks_labels
Definition: c_writer.hpp:140
virtual void declare_cast_types(unsigned int funId, CustomSet< std::string > &locally_declared_types)
Declare all the types used in conversions.
Definition: c_writer.cpp:206
size_t bb_label_counter
Counter of the invocations of writeRoutineInstructions; this counter allows to print different labels...
Definition: c_writer.hpp:113
CustomOrderedSet< vertex > bb_analyzed
Definition: c_writer.hpp:139
FunctionBehaviorConstRef local_rec_function_behavior
Definition: c_writer.hpp:143
virtual void DeclareLocalVariables(const CustomSet< unsigned int > &to_be_declared, CustomSet< unsigned int > &already_declared_variables, CustomSet< std::string > &already_declared_types, const BehavioralHelperConstRef behavioral_helper, const var_pp_functorConstRef varFunc)
Declares the local variable; in case the variable used in the intialization of curVar hasn&#39;t been dec...
Definition: c_writer.cpp:1393
REF_FORWARD_DECL(CWriter)
virtual void WriteFunctionBody(const unsigned int function_id)
Writes the body of a function.
Definition: c_writer.cpp:243
Template borrowed from the ANTLR library by Terence Parr (http://www.jGuru.com - Software rights: htt...
Definition: refcount.hpp:94
virtual void WriteBuiltinWaitCall()
Writes implementation of __builtin_wait_call.
Definition: c_writer.cpp:1922
std::map< unsigned int, std::string > basic_block_prefix
string to be printed at the beginning of a given basic block
Definition: c_writer.hpp:130
virtual void writeInclude(const std::string &file_name)
Writes an include directive.
Definition: c_writer.cpp:1383
std::vector< std::string > additionalIncludes
Definition: c_writer.hpp:190
virtual void WriteGlobalDeclarations()
Writes the global declarations.
Definition: c_writer.cpp:307
unsigned int fake_max_tree_node_id
Definition: c_writer.hpp:127
virtual ~CWriter()
Class to print indented code.
std::map< vertex, std::map< unsigned int, std::string > > renaming_table
renaming table used by phi node destruction procedure
Definition: c_writer.hpp:136
Class used to describe a particular graph with operations as nodes.
Definition: op_graph.hpp:783
const InstructionWriterRef getInstructionWriter() const
Returns the instruction writer.
Definition: c_writer.cpp:1378
BBGraphConstRef local_rec_bb_fcfgGraph
Definition: c_writer.hpp:147
Base class to pass information to a c backend.
virtual void writeRoutineInstructions(const unsigned int function_index, const OpVertexSet &instructions, var_pp_functorConstRef variableFunctor, vertex bb_start=NULL_VERTEX, CustomOrderedSet< vertex > bb_end=CustomOrderedSet< vertex >())
Writes the instructions of the current routine, being it a task or a function of the original program...
Definition: c_writer.cpp:1150
BehavioralHelperConstRef local_rec_behavioral_helper
Definition: c_writer.hpp:149
var_pp_functorConstRef local_rec_variableFunctor
Definition: c_writer.hpp:142
#define NULL_VERTEX
null vertex definition
Definition: graph.hpp:1305
int output_level
the output level
Definition: c_writer.hpp:125
virtual void WriteFunctionImplementation(unsigned int function_id)
Write function implementation.
Definition: c_writer.cpp:258
virtual void writePostInstructionInfo(const FunctionBehaviorConstRef, const vertex)
Write additional information on the given statement vertex, after the statements itself.
Definition: c_writer.cpp:426
CustomOrderedSet< vertex > local_rec_instructions
Definition: c_writer.hpp:144
virtual void writePreInstructionInfo(const FunctionBehaviorConstRef, const vertex)
Write additional information on the given statement vertex, before the statement itself.
Definition: c_writer.cpp:422
std::map< unsigned int, std::string > basic_block_tail
string to be printed at the end of a given basic block
Definition: c_writer.hpp:133
const InstructionWriterRef instrWriter
Contains the class used to write instructions.
Definition: c_writer.hpp:109

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