LIBINT  2.6.0
context.h
1 /*
2  * Copyright (C) 2004-2019 Edward F. Valeev
3  *
4  * This file is part of Libint.
5  *
6  * Libint is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Libint is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Libint. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #include <entity.h>
22 #include <default_params.h>
23 
24 #ifndef _libint2_src_bin_libint_codecontext_h_
25 #define _libint2_src_bin_libint_codecontext_h_
26 
27 namespace libint2 {
28 
29  class ForLoop;
30 
34  class CodeContext {
35  public:
36  virtual ~CodeContext() {}
37 
39  const SafePtr<CompilationParameters>& cparams() const;
40 
42  void turn_comments(bool on);
44  bool comments_on() const;
45 
47  virtual void reset();
48 
50  virtual std::string code_prefix() const =0;
52  virtual std::string code_postfix() const =0;
54  virtual std::string copyright() const =0;
56  virtual std::string std_header() const =0;
58  virtual std::string std_function_header() const =0;
60  virtual std::string label_to_name(const std::string& label) const =0;
64  virtual std::string declare(const std::string& type,
65  const std::string& name) const =0;
69  virtual std::string declare_v(const std::string& type,
70  const std::string& name,
71  const std::string& nelem) const =0;
75  virtual std::string decldef(const std::string& type,
76  const std::string& name,
77  const std::string& value) =0;
81  virtual std::string assign(const std::string& name,
82  const std::string& value) =0;
86  virtual std::string accumulate(const std::string& name,
87  const std::string& value) =0;
91  virtual std::string assign_binary_expr(const std::string& name,
92  const std::string& left,
93  const std::string& oper,
94  const std::string& right) =0;
98  virtual std::string assign_ternary_expr(const std::string& name,
99  const std::string& arg1,
100  const std::string& oper1,
101  const std::string& arg2,
102  const std::string& oper2,
103  const std::string& arg3) =0;
107  virtual std::string accumulate_binary_expr(const std::string& name,
108  const std::string& left,
109  const std::string& oper,
110  const std::string& right) =0;
114  virtual std::string accumulate_ternary_expr(const std::string& name,
115  const std::string& arg1,
116  const std::string& oper1,
117  const std::string& arg2,
118  const std::string& oper2,
119  const std::string& arg3) =0;
121  virtual std::string stack_address(const DGVertex::Address& a) const =0;
122 
124  virtual std::string macro_define(const std::string& name) const =0;
126  virtual std::string macro_define(const std::string& name, const std::string& value) const =0;
128  virtual std::string macro_if(const std::string& name) const =0;
130  virtual std::string macro_ifdef(const std::string& name) const =0;
132  virtual std::string macro_endif() const =0;
133 
135  virtual std::string comment(const std::string& statement) const =0;
137  virtual std::string open_block() const =0;
139  virtual std::string close_block() const =0;
141  virtual std::string end_of_stat() const =0;
143  virtual std::string value_to_pointer(const std::string& val) const =0;
144 
147  virtual SafePtr<ForLoop> for_loop(std::string& varname, const SafePtr<Entity>& less_than,
148  const SafePtr<Entity>& start_at = SafePtr<Entity>(new CTimeEntity<int>(0))) const =0;
149 
151  template <typename T>
152  std::string unique_name() const;
154  template <typename T>
155  std::string type_name() const;
157  virtual std::string inteval_type_name(const std::string& task) const =0;
159  virtual std::string inteval_spec_type_name(const std::string& task) const =0;
161  virtual std::string inteval_gen_type_name() const =0;
163  virtual std::string const_modifier() const =0;
165  virtual std::string mutable_modifier() const =0;
166 
167  protected:
169  CodeContext(const SafePtr<CompilationParameters>& cparams);
171  virtual std::string unique_fp_name() const =0;
173  virtual std::string unique_int_name() const =0;
174 
176  unsigned int next_fp_index() const;
178  unsigned int next_int_index() const;
180  static std::string replace_chars(const std::string& S,
181  const std::string& From,
182  const std::string& To);
183 
185  virtual std::string void_type() const =0;
187  virtual std::string int_type() const =0;
189  virtual std::string size_type() const =0;
191  virtual std::string fp_type() const =0;
193  virtual std::string ptr_fp_type() const =0;
194 
195  private:
196  SafePtr<CompilationParameters> cparams_;
197  mutable unsigned int next_index_[EntityTypes::ntypes];
198  void zero_out_counters() const;
199  bool comments_on_;
200 
201  };
202 
203 
207  class CppCodeContext : public CodeContext, public EnableSafePtrFromThis<CppCodeContext> {
208  public:
209  CppCodeContext(const SafePtr<CompilationParameters>& cparams, bool vectorize = false);
210  virtual ~CppCodeContext();
211 
213  std::string code_prefix() const;
215  std::string code_postfix() const;
217  std::string copyright() const;
219  std::string std_header() const;
221  std::string std_function_header() const;
223  std::string label_to_name(const std::string& label) const;
225  std::string declare(const std::string& type,
226  const std::string& name) const;
228  std::string declare_v(const std::string& type,
229  const std::string& name,
230  const std::string& nelem) const;
231  // Implementation of CodeContext::decldef()
232  std::string decldef(const std::string& type,
233  const std::string& name,
234  const std::string& value);
236  std::string assign(const std::string& name,
237  const std::string& value);
239  std::string accumulate(const std::string& name,
240  const std::string& value);
242  std::string assign_binary_expr(const std::string& name,
243  const std::string& left,
244  const std::string& oper,
245  const std::string& right);
246  // Implementation of CodeContext::assign_ternary_expr()
247  std::string assign_ternary_expr(const std::string& name,
248  const std::string& arg1,
249  const std::string& oper1,
250  const std::string& arg2,
251  const std::string& oper2,
252  const std::string& arg3);
254  std::string accumulate_binary_expr(const std::string& name,
255  const std::string& left,
256  const std::string& oper,
257  const std::string& right);
258  // Implementation of CodeContext::accumulate_ternary_expr()
259  std::string accumulate_ternary_expr(const std::string& name,
260  const std::string& arg1,
261  const std::string& oper1,
262  const std::string& arg2,
263  const std::string& oper2,
264  const std::string& arg3);
266  std::string stack_address(const DGVertex::Address& a) const;
267 
269  std::string macro_define(const std::string& name) const;
271  std::string macro_define(const std::string& name, const std::string& value) const;
273  virtual std::string macro_if(const std::string& name) const;
275  virtual std::string macro_ifdef(const std::string& name) const;
277  virtual std::string macro_endif() const;
278 
280  std::string comment(const std::string& statement) const;
282  std::string open_block() const;
284  std::string close_block() const;
286  std::string end_of_stat() const;
288  std::string value_to_pointer(const std::string& val) const;
290  SafePtr<ForLoop> for_loop(std::string& varname, const SafePtr<Entity>& less_than,
291  const SafePtr<Entity>& start_at) const;
292 
294  std::string inteval_type_name(const std::string& task) const;
296  std::string inteval_spec_type_name(const std::string& task) const;
298  std::string inteval_gen_type_name() const;
299 
300  private:
301  bool vectorize_;
302 
304  std::string unique_fp_name() const;
306  std::string unique_int_name() const;
308  std::string symbol_to_pointer(const std::string& symbol);
309 
310  std::string void_type() const;
311  std::string int_type() const;
312  std::string size_type() const;
313  std::string fp_type() const;
314  std::string ptr_fp_type() const;
315  std::string const_modifier() const;
316  std::string mutable_modifier() const;
317 
318  std::string start_expr() const;
319  std::string end_expr() const;
320 
322  std::string assign_(const std::string& name,
323  const std::string& value,
324  bool accum);
326  std::string assign_binary_expr_(const std::string& name,
327  const std::string& left,
328  const std::string& oper,
329  const std::string& right,
330  bool accum);
332  std::string assign_ternary_expr_(const std::string& name,
333  const std::string& arg1,
334  const std::string& oper1,
335  const std::string& arg2,
336  const std::string& oper2,
337  const std::string& arg3,
338  bool accum);
339  };
340 
341 };
342 
343 #endif
virtual std::string ptr_fp_type() const =0
returns name of pointer to floating-point type
virtual std::string inteval_spec_type_name(const std::string &task) const =0
returns the name of the specialized evaluator type for task 'task'
virtual std::string int_type() const =0
returns name of integer type
virtual std::string code_prefix() const =0
produces prefix to function declarations or definitions
virtual void reset()
this function resets the context to be used for the next source file
Definition: context.cc:120
const SafePtr< CompilationParameters > & cparams() const
Returns the CompilationParameters used to create this context.
Definition: context.cc:92
std::string unique_name() const
unique_name<T> returns a unique name for a variable of type T
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 close_block() const
Implementation of CodeContext::close_block()
Definition: context.cc:637
virtual std::string open_block() const =0
open a code block
std::string accumulate_binary_expr(const std::string &name, const std::string &left, const std::string &oper, const std::string &right)
Implementation of CodeContext::accumulate_binary_expr()
Definition: context.cc:364
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 'left oper right' 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:186
MemoryManager::Address Address
The address on the stack during computation is described using this type.
Definition: dgvertex.h:46
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)
accumulate_ternary_expr returns a statement which accumulates ternary expression 'arg1 oper1 arg2 ope...
Definition: context.cc:520
virtual std::string macro_ifdef(const std::string &name) const =0
#ifdef macro
CodeContext provides context for generating code.
Definition: context.h:34
virtual std::string inteval_type_name(const std::string &task) const =0
returns the name of the evaluator type for task 'task'
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:24
std::string inteval_gen_type_name() const
Implementation of CodeContext::inteval_spec_type_name()
Definition: context.cc:725
virtual std::string const_modifier() const =0
returns the modifier for constant variables
std::string inteval_spec_type_name(const std::string &task) const
Implementation of CodeContext::inteval_spec_type_name()
Definition: context.cc:717
std::string label_to_name(const std::string &label) const
Implementation of CodeContext::label_to_name(label)
Definition: context.cc:259
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:126
bool comments_on() const
returns true if to print comments
Definition: context.cc:98
virtual std::string declare(const std::string &type, const std::string &name) const =0
declare returns a statement which declares variable named 'name' of type 'type'
virtual std::string macro_endif() const
Implementation of CodeContext::macro_endif()
Definition: context.cc:614
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
virtual std::string mutable_modifier() const =0
returns the modifier for mutable variables
CodeContext(const SafePtr< CompilationParameters > &cparams)
Lone constructor takes CompilationParams.
Definition: context.cc:84
std::string accumulate(const std::string &name, const std::string &value)
Implementation of CodeContext::accumulate()
Definition: context.cc:311
std::string type_name() const
type_name<T> returns name for type T
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 'name' of type 'type' and defines its value...
virtual std::string fp_type() const =0
returns name of floating-point type
void turn_comments(bool on)
turn comments on and off (the default is on)
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 'arg1 oper1 arg2 ope...
virtual std::string accumulate(const std::string &name, const std::string &value)=0
accumulate returns a statement which assigns variable 'value' to variable 'name'
virtual std::string macro_if(const std::string &name) const =0
#if macro
virtual std::string macro_endif() const =0
#endif
std::string declare(const std::string &type, const std::string &name) const
Implementation of CodeContext::declare()
Definition: context.cc:269
virtual std::string stack_address(const DGVertex::Address &a) const =0
converts an address on the stack to its string representation
std::string declare_v(const std::string &type, const std::string &name, const std::string &nelem) const
Implementation of CodeContext::declare_v()
Definition: context.cc:280
virtual std::string end_of_stat() const =0
end a statement
unsigned int next_fp_index() const
next fp index
Definition: context.cc:101
virtual std::string unique_int_name() const =0
generates a unique name for an integer
CppCodeContext is an implementation of CodeContext for C++.
Definition: context.h:207
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)
assign_ternary_expr returns a statement which assigns ternary expression 'arg1 oper1 arg2 oper2 arg3'...
Definition: context.cc:424
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 'arg1 oper1 arg2 oper2 arg3'...
virtual std::string macro_define(const std::string &name) const =0
#define a macro
virtual std::string size_type() const =0
returns name of array size type (e.g. size_t)
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 'name' of 'nelem' elements of type 'type'
virtual std::string comment(const std::string &statement) const =0
comment(statement) returns commented statement
virtual std::string macro_ifdef(const std::string &name) const
Implementation of CodeContext::macro_ifdef()
Definition: context.cc:606
virtual std::string assign(const std::string &name, const std::string &value)=0
assign returns a statement which assigns variable 'value' to variable 'name'
virtual std::string std_header() const =0
std_header() returns declarations necessary for every source file
SafePtr< ForLoop > for_loop(std::string &varname, const SafePtr< Entity > &less_than, const SafePtr< Entity > &start_at) const
Implementation of CodeContext::for_loop()
Definition: context.cc:663
std::string decldef(const std::string &type, const std::string &name, const std::string &value)
decldef returns a statement which declares variable named 'name' of type 'type' and defines its value...
Definition: context.cc:292
virtual std::string void_type() const =0
returns name of void type
std::string comment(const std::string &statement) const
Implementation of CodeContext::comment(statement)
Definition: context.cc:622
std::string code_postfix() const
Implementation of CodeContext::code_postfix()
Definition: context.cc:206
std::string std_header() const
Implementation of CodeContext::std_header()
Definition: context.cc:242
std::string code_prefix() const
Implementation of CodeContext::code_prefix()
Definition: context.cc:197
virtual std::string code_postfix() const =0
produces postfix to function declarations or definitions
virtual std::string macro_if(const std::string &name) const
Implementation of CodeContext::macro_if()
Definition: context.cc:598
std::string copyright() const
Implementation of CodeContext::copyright()
Definition: context.cc:215
std::string value_to_pointer(const std::string &val) const
Implementation of CodeContext::value_to_pointer()
Definition: context.cc:650
std::string std_function_header() const
Implementation of CodeContext::std_function_header()
Definition: context.cc:249
unsigned int next_int_index() const
next int index
Definition: context.cc:107
std::string assign_binary_expr(const std::string &name, const std::string &left, const std::string &oper, const std::string &right)
Implementation of CodeContext::assign_binary_expr()
Definition: context.cc:355
std::string assign(const std::string &name, const std::string &value)
Implementation of CodeContext::assign()
Definition: context.cc:304
std::string inteval_type_name(const std::string &task) const
Implementation of CodeContext::inteval_type_name()
Definition: context.cc:708
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 'left oper right' to v...
std::string end_of_stat() const
Implementation of CodeContext::end_of_stat()
Definition: context.cc:643
std::string stack_address(const DGVertex::Address &a) const
Implementation of CodeContext::stack_address()
Definition: context.cc:570
virtual std::string copyright() const =0
copyright() returns the copyright statement
std::string open_block() const
Implementation of CodeContext::open_block()
Definition: context.cc:631
virtual std::string inteval_gen_type_name() const =0
returns the name of the generic evaluator type (works for any task)
virtual std::string value_to_pointer(const std::string &val) const =0
converts a value to a pointer
std::string macro_define(const std::string &name) const
Implementation of CodeContext::macro_define()
Definition: context.cc:581
virtual std::string close_block() const =0
close a code block
virtual std::string std_function_header() const =0
std_function_header() returns declarations and definitions necessary for every function
virtual std::string unique_fp_name() const =0
generates a unique name for a floating-point variable