LIBINT  2.1.0-stable
context.h
1 /*
2  * This file is a part of Libint.
3  * Copyright (C) 2004-2014 Edward F. Valeev
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see http://www.gnu.org/licenses/.
17  *
18  */
19 
20 #include <entity.h>
21 #include <default_params.h>
22 
23 #ifndef _libint2_src_bin_libint_codecontext_h_
24 #define _libint2_src_bin_libint_codecontext_h_
25 
26 namespace libint2 {
27 
28  class ForLoop;
29 
33  class CodeContext {
34  public:
35  virtual ~CodeContext() {}
36 
38  const SafePtr<CompilationParameters>& cparams() const;
39 
41  void turn_comments(bool on);
43  bool comments_on() const;
44 
46  virtual void reset();
47 
49  virtual std::string code_prefix() const =0;
51  virtual std::string code_postfix() const =0;
53  virtual std::string std_header() const =0;
55  virtual std::string std_function_header() const =0;
57  virtual std::string label_to_name(const std::string& label) const =0;
61  virtual std::string declare(const std::string& type,
62  const std::string& name) const =0;
66  virtual std::string declare_v(const std::string& type,
67  const std::string& name,
68  const std::string& nelem) const =0;
72  virtual std::string decldef(const std::string& type,
73  const std::string& name,
74  const std::string& value) =0;
78  virtual std::string assign(const std::string& name,
79  const std::string& value) =0;
83  virtual std::string accumulate(const std::string& name,
84  const std::string& value) =0;
88  virtual std::string assign_binary_expr(const std::string& name,
89  const std::string& left,
90  const std::string& oper,
91  const std::string& right) =0;
95  virtual std::string assign_ternary_expr(const std::string& name,
96  const std::string& arg1,
97  const std::string& oper1,
98  const std::string& arg2,
99  const std::string& oper2,
100  const std::string& arg3) =0;
104  virtual std::string accumulate_binary_expr(const std::string& name,
105  const std::string& left,
106  const std::string& oper,
107  const std::string& right) =0;
111  virtual std::string accumulate_ternary_expr(const std::string& name,
112  const std::string& arg1,
113  const std::string& oper1,
114  const std::string& arg2,
115  const std::string& oper2,
116  const std::string& arg3) =0;
118  virtual std::string stack_address(const DGVertex::Address& a) const =0;
119 
121  virtual std::string macro_define(const std::string& name) const =0;
123  virtual std::string macro_define(const std::string& name, const std::string& value) const =0;
125  virtual std::string macro_if(const std::string& name) const =0;
127  virtual std::string macro_ifdef(const std::string& name) const =0;
129  virtual std::string macro_endif() const =0;
130 
132  virtual std::string comment(const std::string& statement) const =0;
134  virtual std::string open_block() const =0;
136  virtual std::string close_block() const =0;
138  virtual std::string end_of_stat() const =0;
140  virtual std::string value_to_pointer(const std::string& val) const =0;
141 
144  virtual SafePtr<ForLoop> for_loop(std::string& varname, const SafePtr<Entity>& less_than,
145  const SafePtr<Entity>& start_at = SafePtr<Entity>(new CTimeEntity<int>(0))) const =0;
146 
148  template <typename T>
149  std::string unique_name() const;
151  template <typename T>
152  std::string type_name() const;
154  virtual std::string inteval_type_name(const std::string& task) const =0;
156  virtual std::string inteval_spec_type_name(const std::string& task) const =0;
158  virtual std::string inteval_gen_type_name() const =0;
160  virtual std::string const_modifier() const =0;
162  virtual std::string mutable_modifier() const =0;
163 
164  protected:
166  CodeContext(const SafePtr<CompilationParameters>& cparams);
168  virtual std::string unique_fp_name() const =0;
170  virtual std::string unique_int_name() const =0;
171 
173  unsigned int next_fp_index() const;
175  unsigned int next_int_index() const;
177  static std::string replace_chars(const std::string& S,
178  const std::string& From,
179  const std::string& To);
180 
182  virtual std::string void_type() const =0;
184  virtual std::string int_type() const =0;
186  virtual std::string size_type() const =0;
188  virtual std::string fp_type() const =0;
190  virtual std::string ptr_fp_type() const =0;
191 
192  private:
193  SafePtr<CompilationParameters> cparams_;
194  mutable unsigned int next_index_[EntityTypes::ntypes];
195  void zero_out_counters() const;
196  bool comments_on_;
197 
198  };
199 
200 
204  class CppCodeContext : public CodeContext, public EnableSafePtrFromThis<CppCodeContext> {
205  public:
206  CppCodeContext(const SafePtr<CompilationParameters>& cparams, bool vectorize = false);
207  virtual ~CppCodeContext();
208 
210  std::string code_prefix() const;
212  std::string code_postfix() const;
214  std::string std_header() const;
216  std::string std_function_header() const;
218  std::string label_to_name(const std::string& label) const;
220  std::string declare(const std::string& type,
221  const std::string& name) const;
223  std::string declare_v(const std::string& type,
224  const std::string& name,
225  const std::string& nelem) const;
226  // Implementation of CodeContext::decldef()
227  std::string decldef(const std::string& type,
228  const std::string& name,
229  const std::string& value);
231  std::string assign(const std::string& name,
232  const std::string& value);
234  std::string accumulate(const std::string& name,
235  const std::string& value);
237  std::string assign_binary_expr(const std::string& name,
238  const std::string& left,
239  const std::string& oper,
240  const std::string& right);
241  // Implementation of CodeContext::assign_ternary_expr()
242  std::string assign_ternary_expr(const std::string& name,
243  const std::string& arg1,
244  const std::string& oper1,
245  const std::string& arg2,
246  const std::string& oper2,
247  const std::string& arg3);
249  std::string accumulate_binary_expr(const std::string& name,
250  const std::string& left,
251  const std::string& oper,
252  const std::string& right);
253  // Implementation of CodeContext::accumulate_ternary_expr()
254  std::string accumulate_ternary_expr(const std::string& name,
255  const std::string& arg1,
256  const std::string& oper1,
257  const std::string& arg2,
258  const std::string& oper2,
259  const std::string& arg3);
261  std::string stack_address(const DGVertex::Address& a) const;
262 
264  std::string macro_define(const std::string& name) const;
266  std::string macro_define(const std::string& name, const std::string& value) const;
268  virtual std::string macro_if(const std::string& name) const;
270  virtual std::string macro_ifdef(const std::string& name) const;
272  virtual std::string macro_endif() const;
273 
275  std::string comment(const std::string& statement) const;
277  std::string open_block() const;
279  std::string close_block() const;
281  std::string end_of_stat() const;
283  std::string value_to_pointer(const std::string& val) const;
285  SafePtr<ForLoop> for_loop(std::string& varname, const SafePtr<Entity>& less_than,
286  const SafePtr<Entity>& start_at) const;
287 
289  std::string inteval_type_name(const std::string& task) const;
291  std::string inteval_spec_type_name(const std::string& task) const;
293  std::string inteval_gen_type_name() const;
294 
295  private:
296  bool vectorize_;
297 
299  std::string unique_fp_name() const;
301  std::string unique_int_name() const;
303  std::string symbol_to_pointer(const std::string& symbol);
304 
305  std::string void_type() const;
306  std::string int_type() const;
307  std::string size_type() const;
308  std::string fp_type() const;
309  std::string ptr_fp_type() const;
310  std::string const_modifier() const;
311  std::string mutable_modifier() const;
312 
313  std::string start_expr() const;
314  std::string end_expr() const;
315 
317  std::string assign_(const std::string& name,
318  const std::string& value,
319  bool accum);
321  std::string assign_binary_expr_(const std::string& name,
322  const std::string& left,
323  const std::string& oper,
324  const std::string& right,
325  bool accum);
327  std::string assign_ternary_expr_(const std::string& name,
328  const std::string& arg1,
329  const std::string& oper1,
330  const std::string& arg2,
331  const std::string& oper2,
332  const std::string& arg3,
333  bool accum);
334  };
335 
336 };
337 
338 #endif
virtual void reset()
this function resets the context to be used for the next source file
Definition: context.cc:118
virtual std::string unique_fp_name() const =0
generates a unique name for a floating-point variable
virtual std::string comment(const std::string &statement) const =0
comment(statement) returns commented statement
virtual std::string assign_binary_expr(const std::string &name, const std::string &left, const std::string &oper, const std::string &right)=0
assign_binary_expr returns a statement which assigns binary expression &#39;left oper right&#39; to variable ...
CTimeEntity is an Entity of type T that exists at compile-time of the generated code (hence has a val...
Definition: entity.h:183
MemoryManager::Address Address
The address on the stack during computation is described using this type.
Definition: dgvertex.h:45
virtual std::string open_block() const =0
open a code block
virtual std::string std_function_header() const =0
std_function_header() returns declarations and definitions necessary for every function ...
CodeContext provides context for generating code.
Definition: context.h:33
virtual std::string code_postfix() const =0
produces postfix to function declarations or definitions
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:23
virtual std::string int_type() const =0
returns name of integer type
virtual std::string declare_v(const std::string &type, const std::string &name, const std::string &nelem) const =0
declare_v returns a statement which declares a vector &#39;name&#39; of &#39;nelem&#39; elements of type &#39;type&#39; ...
const SafePtr< CompilationParameters > & cparams() const
Returns the CompilationParameters used to create this context.
Definition: context.cc:90
virtual std::string size_type() const =0
returns name of array size type (e.g. size_t)
virtual std::string macro_if(const std::string &name) const =0
#if macro
virtual std::string const_modifier() const =0
returns the modifier for constant variables
virtual SafePtr< ForLoop > for_loop(std::string &varname, const SafePtr< Entity > &less_than, const SafePtr< Entity > &start_at=SafePtr< Entity >(new CTimeEntity< int >(0))) const =0
returns a ForLoop object.
std::string unique_name() const
unique_name<T> returns a unique name for a variable of type T
static std::string replace_chars(const std::string &S, const std::string &From, const std::string &To)
replaces every appearance of From with To in S
Definition: context.cc:124
virtual std::string inteval_gen_type_name() const =0
returns the name of the generic evaluator type (works for any task)
virtual std::string inteval_type_name(const std::string &task) const =0
returns the name of the evaluator type for task &#39;task&#39;
virtual std::string stack_address(const DGVertex::Address &a) const =0
converts an address on the stack to its string representation
virtual std::string fp_type() const =0
returns name of floating-point type
CodeContext(const SafePtr< CompilationParameters > &cparams)
Lone constructor takes CompilationParams.
Definition: context.cc:82
virtual std::string label_to_name(const std::string &label) const =0
label_to_name(label) converts label to a name valid within the context of the language ...
unsigned int next_fp_index() const
next fp index
Definition: context.cc:99
virtual std::string decldef(const std::string &type, const std::string &name, const std::string &value)=0
decldef returns a statement which declares variable named &#39;name&#39; of type &#39;type&#39; and defines its value...
void turn_comments(bool on)
turn comments on and off (the default is on)
virtual std::string std_header() const =0
std_header() returns declarations necessary for every source file
virtual std::string ptr_fp_type() const =0
returns name of pointer to floating-point type
virtual std::string mutable_modifier() const =0
returns the modifier for mutable variables
virtual std::string accumulate_ternary_expr(const std::string &name, const std::string &arg1, const std::string &oper1, const std::string &arg2, const std::string &oper2, const std::string &arg3)=0
accumulate_ternary_expr returns a statement which accumulates ternary expression &#39;arg1 oper1 arg2 ope...
virtual std::string accumulate(const std::string &name, const std::string &value)=0
accumulate returns a statement which assigns variable &#39;value&#39; to variable &#39;name&#39;
virtual std::string value_to_pointer(const std::string &val) const =0
converts a value to a pointer
virtual std::string macro_endif() const =0
#endif
virtual std::string code_prefix() const =0
produces prefix to function declarations or definitions
virtual std::string declare(const std::string &type, const std::string &name) const =0
declare returns a statement which declares variable named &#39;name&#39; of type &#39;type&#39;
std::string type_name() const
type_name<T> returns name for type T
CppCodeContext is an implementation of CodeContext for C++.
Definition: context.h:204
virtual std::string end_of_stat() const =0
end a statement
virtual std::string assign_ternary_expr(const std::string &name, const std::string &arg1, const std::string &oper1, const std::string &arg2, const std::string &oper2, const std::string &arg3)=0
assign_ternary_expr returns a statement which assigns ternary expression &#39;arg1 oper1 arg2 oper2 arg3&#39;...
virtual std::string macro_ifdef(const std::string &name) const =0
#ifdef macro
virtual std::string assign(const std::string &name, const std::string &value)=0
assign returns a statement which assigns variable &#39;value&#39; to variable &#39;name&#39;
virtual std::string inteval_spec_type_name(const std::string &task) const =0
returns the name of the specialized evaluator type for task &#39;task&#39;
unsigned int next_int_index() const
next int index
Definition: context.cc:105
virtual std::string unique_int_name() const =0
generates a unique name for an integer
bool comments_on() const
returns true if to print comments
Definition: context.cc:96
virtual std::string accumulate_binary_expr(const std::string &name, const std::string &left, const std::string &oper, const std::string &right)=0
accumulate_binary_expr returns a statement which accumulates binary expression &#39;left oper right&#39; to v...
virtual std::string macro_define(const std::string &name) const =0
#define a macro
virtual std::string void_type() const =0
returns name of void type
virtual std::string close_block() const =0
close a code block