LIBINT 2.7.2
context.h
1/*
2 * Copyright (C) 2004-2021 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
27namespace libint2 {
28
29 class ForLoop;
30
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 override;
215 std::string code_postfix() const override;
217 std::string copyright() const override;
219 std::string std_header() const override;
221 std::string std_function_header() const override;
223 std::string label_to_name(const std::string& label) const override;
225 std::string declare(const std::string& type,
226 const std::string& name) const override;
228 std::string declare_v(const std::string& type,
229 const std::string& name,
230 const std::string& nelem) const override;
231 // Implementation of CodeContext::decldef()
232 std::string decldef(const std::string& type,
233 const std::string& name,
234 const std::string& value) override;
236 std::string assign(const std::string& name,
237 const std::string& value) override;
239 std::string accumulate(const std::string& name,
240 const std::string& value) override;
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) override;
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) override;
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) override;
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) override;
266 std::string stack_address(const DGVertex::Address& a) const override;
267
269 std::string macro_define(const std::string& name) const override;
271 std::string macro_define(const std::string& name, const std::string& value) const override;
273 std::string macro_if(const std::string& name) const override;
275 std::string macro_ifdef(const std::string& name) const override;
277 std::string macro_endif() const override;
278
280 std::string comment(const std::string& statement) const override;
282 std::string open_block() const override;
284 std::string close_block() const override;
286 std::string end_of_stat() const override;
288 std::string value_to_pointer(const std::string& val) const override;
290 SafePtr<ForLoop> for_loop(std::string& varname, const SafePtr<Entity>& less_than,
291 const SafePtr<Entity>& start_at) const override;
292
294 std::string inteval_type_name(const std::string& task) const override;
296 std::string inteval_spec_type_name(const std::string& task) const override;
298 std::string inteval_gen_type_name() const override;
299
300 private:
301 bool vectorize_;
302
304 std::string unique_fp_name() const override;
306 std::string unique_int_name() const override;
308 std::string symbol_to_pointer(const std::string& symbol);
309
310 std::string void_type() const override;
311 std::string int_type() const override;
312 std::string size_type() const override;
313 std::string fp_type() const override;
314 std::string ptr_fp_type() const override;
315 std::string const_modifier() const override;
316 std::string mutable_modifier() const override;
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
CTimeEntity is an Entity of type T that exists at compile-time of the generated code (hence has a val...
Definition: entity.h:189
CodeContext provides context for generating code.
Definition: context.h:34
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...
virtual std::string macro_ifdef(const std::string &name) const =0
#ifdef macro
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 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.
void turn_comments(bool on)
turn comments on and off (the default is on)
CodeContext(const SafePtr< CompilationParameters > &cparams)
Lone constructor takes CompilationParams.
Definition: context.cc:85
virtual std::string comment(const std::string &statement) const =0
comment(statement) returns commented statement
virtual std::string inteval_gen_type_name() const =0
returns the name of the generic evaluator type (works for any task)
virtual std::string fp_type() const =0
returns name of 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 mutable_modifier() const =0
returns the modifier for mutable variables
virtual std::string macro_define(const std::string &name, const std::string &value) const =0
#define a macro
virtual std::string void_type() const =0
returns name of void type
virtual std::string unique_fp_name() const =0
generates a unique name for a floating-point variable
virtual std::string open_block() const =0
open a code block
virtual std::string code_prefix() const =0
produces prefix to function declarations or definitions
virtual std::string close_block() const =0
close a code block
const SafePtr< CompilationParameters > & cparams() const
Returns the CompilationParameters used to create this context.
Definition: context.cc:93
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 code_postfix() const =0
produces postfix to function declarations or definitions
virtual std::string macro_endif() const =0
#endif
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:127
virtual std::string macro_define(const std::string &name) const =0
#define a macro
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 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 ...
virtual std::string stack_address(const DGVertex::Address &a) const =0
converts an address on the stack to its string representation
virtual std::string value_to_pointer(const std::string &val) const =0
converts a value to a pointer
virtual std::string assign(const std::string &name, const std::string &value)=0
assign returns a statement which assigns variable 'value' to variable 'name'
std::string type_name() const
type_name<T> returns name for type T
virtual std::string std_function_header() const =0
std_function_header() returns declarations and definitions necessary for every function
unsigned int next_int_index() const
next int index
Definition: context.cc:108
bool comments_on() const
returns true if to print comments
Definition: context.cc:99
virtual std::string const_modifier() const =0
returns the modifier for constant variables
virtual std::string copyright() const =0
copyright() returns the copyright statement
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 macro_if(const std::string &name) const =0
#if macro
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 int_type() const =0
returns name of integer type
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 std_header() const =0
std_header() returns declarations necessary for every source file
virtual std::string inteval_type_name(const std::string &task) const =0
returns the name of the evaluator type for task 'task'
virtual std::string ptr_fp_type() const =0
returns name of pointer to floating-point type
unsigned int next_fp_index() const
next fp index
Definition: context.cc:102
virtual std::string size_type() const =0
returns name of array size type (e.g. size_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 void reset()
this function resets the context to be used for the next source file
Definition: context.cc:121
virtual std::string end_of_stat() const =0
end a statement
virtual std::string unique_int_name() const =0
generates a unique name for an integer
std::string unique_name() const
unique_name<T> returns a unique name for a variable of type T
CppCodeContext is an implementation of CodeContext for C++.
Definition: context.h:207
std::string end_of_stat() const override
Implementation of CodeContext::end_of_stat()
Definition: context.cc:644
std::string inteval_type_name(const std::string &task) const override
Implementation of CodeContext::inteval_type_name()
Definition: context.cc:709
std::string code_prefix() const override
Implementation of CodeContext::code_prefix()
Definition: context.cc:198
std::string assign_binary_expr(const std::string &name, const std::string &left, const std::string &oper, const std::string &right) override
Implementation of CodeContext::assign_binary_expr()
Definition: context.cc:356
std::string copyright() const override
Implementation of CodeContext::copyright()
Definition: context.cc:216
std::string accumulate(const std::string &name, const std::string &value) override
Implementation of CodeContext::accumulate()
Definition: context.cc:312
std::string value_to_pointer(const std::string &val) const override
Implementation of CodeContext::value_to_pointer()
Definition: context.cc:651
std::string inteval_spec_type_name(const std::string &task) const override
Implementation of CodeContext::inteval_spec_type_name()
Definition: context.cc:718
std::string accumulate_binary_expr(const std::string &name, const std::string &left, const std::string &oper, const std::string &right) override
Implementation of CodeContext::accumulate_binary_expr()
Definition: context.cc:365
std::string declare_v(const std::string &type, const std::string &name, const std::string &nelem) const override
Implementation of CodeContext::declare_v()
Definition: context.cc:281
std::string inteval_gen_type_name() const override
Implementation of CodeContext::inteval_spec_type_name()
Definition: context.cc:726
std::string decldef(const std::string &type, const std::string &name, const std::string &value) override
decldef returns a statement which declares variable named 'name' of type 'type' and defines its value...
Definition: context.cc:293
std::string macro_endif() const override
Implementation of CodeContext::macro_endif()
Definition: context.cc:615
std::string macro_ifdef(const std::string &name) const override
Implementation of CodeContext::macro_ifdef()
Definition: context.cc:607
std::string close_block() const override
Implementation of CodeContext::close_block()
Definition: context.cc:638
SafePtr< ForLoop > for_loop(std::string &varname, const SafePtr< Entity > &less_than, const SafePtr< Entity > &start_at) const override
Implementation of CodeContext::for_loop()
Definition: context.cc:664
std::string comment(const std::string &statement) const override
Implementation of CodeContext::comment(statement)
Definition: context.cc:623
std::string code_postfix() const override
Implementation of CodeContext::code_postfix()
Definition: context.cc:207
std::string stack_address(const DGVertex::Address &a) const override
Implementation of CodeContext::stack_address()
Definition: context.cc:571
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) override
assign_ternary_expr returns a statement which assigns ternary expression 'arg1 oper1 arg2 oper2 arg3'...
Definition: context.cc:425
std::string assign(const std::string &name, const std::string &value) override
Implementation of CodeContext::assign()
Definition: context.cc:305
std::string macro_define(const std::string &name) const override
Implementation of CodeContext::macro_define()
Definition: context.cc:582
std::string std_function_header() const override
Implementation of CodeContext::std_function_header()
Definition: context.cc:250
std::string label_to_name(const std::string &label) const override
Implementation of CodeContext::label_to_name(label)
Definition: context.cc:260
std::string macro_if(const std::string &name) const override
Implementation of CodeContext::macro_if()
Definition: context.cc:599
std::string declare(const std::string &type, const std::string &name) const override
Implementation of CodeContext::declare()
Definition: context.cc:270
std::string std_header() const override
Implementation of CodeContext::std_header()
Definition: context.cc:243
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) override
accumulate_ternary_expr returns a statement which accumulates ternary expression 'arg1 oper1 arg2 ope...
Definition: context.cc:521
std::string open_block() const override
Implementation of CodeContext::open_block()
Definition: context.cc:632
MemoryManager::Address Address
The address on the stack during computation is described using this type.
Definition: dgvertex.h:46
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:24