LIBINT 2.9.0
context.h
1/*
2 * Copyright (C) 2004-2024 Edward F. Valeev
3 *
4 * This file is part of Libint compiler.
5 *
6 * Libint compiler 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 compiler 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 compiler. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <default_params.h>
22#include <entity.h>
23
24#ifndef _libint2_src_bin_libint_codecontext_h_
25#define _libint2_src_bin_libint_codecontext_h_
26
27namespace libint2 {
28
29class ForLoop;
30
35 public:
36 virtual ~CodeContext() {}
37
39 const std::shared_ptr<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;
59 virtual std::string std_function_header() const = 0;
62 virtual std::string label_to_name(const std::string& label) const = 0;
66 virtual std::string declare(const std::string& type,
67 const std::string& name) const = 0;
71 virtual std::string declare_v(const std::string& type,
72 const std::string& name,
73 const std::string& nelem) const = 0;
77 virtual std::string decldef(const std::string& type, const std::string& name,
78 const std::string& value) = 0;
82 virtual std::string assign(const std::string& name,
83 const std::string& value) = 0;
87 virtual std::string accumulate(const std::string& name,
88 const std::string& value) = 0;
92 virtual std::string assign_binary_expr(const std::string& name,
93 const std::string& left,
94 const std::string& oper,
95 const std::string& right) = 0;
99 virtual std::string assign_ternary_expr(const std::string& name,
100 const std::string& arg1,
101 const std::string& oper1,
102 const std::string& arg2,
103 const std::string& oper2,
104 const std::string& arg3) = 0;
108 virtual std::string accumulate_binary_expr(const std::string& name,
109 const std::string& left,
110 const std::string& oper,
111 const std::string& right) = 0;
115 virtual std::string accumulate_ternary_expr(const std::string& name,
116 const std::string& arg1,
117 const std::string& oper1,
118 const std::string& arg2,
119 const std::string& oper2,
120 const std::string& arg3) = 0;
122 virtual std::string stack_address(const DGVertex::Address& a) const = 0;
123
125 virtual std::string macro_define(const std::string& name) const = 0;
127 virtual std::string macro_define(const std::string& name,
128 const std::string& value) const = 0;
130 virtual std::string macro_if(const std::string& name) const = 0;
132 virtual std::string macro_ifdef(const std::string& name) const = 0;
134 virtual std::string macro_endif() const = 0;
135
137 virtual std::string comment(const std::string& statement) const = 0;
139 virtual std::string open_block() const = 0;
141 virtual std::string close_block() const = 0;
143 virtual std::string end_of_stat() const = 0;
145 virtual std::string value_to_pointer(const std::string& val) const = 0;
146
149 virtual std::shared_ptr<ForLoop> for_loop(
150 std::string& varname, const std::shared_ptr<Entity>& less_than,
151 const std::shared_ptr<Entity>& start_at =
152 std::shared_ptr<Entity>(new CTimeEntity<int>(0))) const = 0;
153
155 template <typename T>
156 std::string unique_name() const;
158 template <typename T>
159 std::string type_name() const;
161 virtual std::string inteval_type_name(const std::string& task) const = 0;
163 virtual std::string inteval_spec_type_name(const std::string& task) const = 0;
165 virtual std::string inteval_gen_type_name() const = 0;
167 virtual std::string const_modifier() const = 0;
169 virtual std::string mutable_modifier() const = 0;
170
171 protected:
173 CodeContext(const std::shared_ptr<CompilationParameters>& cparams);
175 virtual std::string unique_fp_name() const = 0;
177 virtual std::string unique_int_name() const = 0;
178
180 unsigned int next_fp_index() const;
182 unsigned int next_int_index() const;
184 static std::string replace_chars(const std::string& S,
185 const std::string& From,
186 const std::string& To);
187
189 virtual std::string void_type() const = 0;
191 virtual std::string int_type() const = 0;
193 virtual std::string size_type() const = 0;
195 virtual std::string fp_type() const = 0;
197 virtual std::string ptr_fp_type() const = 0;
198
199 private:
200 std::shared_ptr<CompilationParameters> cparams_;
201 mutable unsigned int next_index_[EntityTypes::ntypes];
202 void zero_out_counters() const;
203 bool comments_on_;
204};
205
210 public std::enable_shared_from_this<CppCodeContext> {
211 public:
212 CppCodeContext(const std::shared_ptr<CompilationParameters>& cparams,
213 bool vectorize = false);
214 virtual ~CppCodeContext();
215
217 std::string code_prefix() const override;
219 std::string code_postfix() const override;
221 std::string copyright() const override;
223 std::string std_header() const override;
225 std::string std_function_header() const override;
227 std::string label_to_name(const std::string& label) const override;
229 std::string declare(const std::string& type,
230 const std::string& name) const override;
232 std::string declare_v(const std::string& type, const std::string& name,
233 const std::string& nelem) const override;
234 // Implementation of CodeContext::decldef()
235 std::string decldef(const std::string& type, const std::string& name,
236 const std::string& value) override;
238 std::string assign(const std::string& name,
239 const std::string& value) override;
241 std::string accumulate(const std::string& name,
242 const std::string& value) override;
244 std::string assign_binary_expr(const std::string& name,
245 const std::string& left,
246 const std::string& oper,
247 const std::string& right) override;
248 // Implementation of CodeContext::assign_ternary_expr()
249 std::string assign_ternary_expr(const std::string& name,
250 const std::string& arg1,
251 const std::string& oper1,
252 const std::string& arg2,
253 const std::string& oper2,
254 const std::string& arg3) override;
256 std::string accumulate_binary_expr(const std::string& name,
257 const std::string& left,
258 const std::string& oper,
259 const std::string& right) override;
260 // Implementation of CodeContext::accumulate_ternary_expr()
261 std::string accumulate_ternary_expr(const std::string& name,
262 const std::string& arg1,
263 const std::string& oper1,
264 const std::string& arg2,
265 const std::string& oper2,
266 const std::string& arg3) override;
268 std::string stack_address(const DGVertex::Address& a) const override;
269
271 std::string macro_define(const std::string& name) const override;
273 std::string macro_define(const std::string& name,
274 const std::string& value) const override;
276 std::string macro_if(const std::string& name) const override;
278 std::string macro_ifdef(const std::string& name) const override;
280 std::string macro_endif() const override;
281
283 std::string comment(const std::string& statement) const override;
285 std::string open_block() const override;
287 std::string close_block() const override;
289 std::string end_of_stat() const override;
291 std::string value_to_pointer(const std::string& val) const override;
293 std::shared_ptr<ForLoop> for_loop(
294 std::string& varname, const std::shared_ptr<Entity>& less_than,
295 const std::shared_ptr<Entity>& start_at) const override;
296
298 std::string inteval_type_name(const std::string& task) const override;
300 std::string inteval_spec_type_name(const std::string& task) const override;
302 std::string inteval_gen_type_name() const override;
303
304 private:
305 bool vectorize_;
306
308 std::string unique_fp_name() const override;
310 std::string unique_int_name() const override;
312 std::string symbol_to_pointer(const std::string& symbol);
313
314 std::string void_type() const override;
315 std::string int_type() const override;
316 std::string size_type() const override;
317 std::string fp_type() const override;
318 std::string ptr_fp_type() const override;
319 std::string const_modifier() const override;
320 std::string mutable_modifier() const override;
321
322 std::string start_expr() const;
323 std::string end_expr() const;
324
326 std::string assign_(const std::string& name, const std::string& value,
327 bool accum);
329 std::string assign_binary_expr_(const std::string& name,
330 const std::string& left,
331 const std::string& oper,
332 const std::string& right, bool accum);
334 std::string assign_ternary_expr_(const std::string& name,
335 const std::string& arg1,
336 const std::string& oper1,
337 const std::string& arg2,
338 const std::string& oper2,
339 const std::string& arg3, bool accum);
340};
341
342}; // namespace libint2
343
344#endif
CTimeEntity is an Entity of type T that exists at compile-time of the generated code (hence has a val...
Definition entity.h:220
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'
void turn_comments(bool on)
turn comments on and off (the default is on)
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
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:101
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'...
const std::shared_ptr< CompilationParameters > & cparams() const
Returns the CompilationParameters used to create this context.
Definition context.cc:81
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:91
bool comments_on() const
returns true if to print comments
Definition context.cc:85
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:87
virtual std::shared_ptr< ForLoop > for_loop(std::string &varname, const std::shared_ptr< Entity > &less_than, const std::shared_ptr< Entity > &start_at=std::shared_ptr< Entity >(new CTimeEntity< int >(0))) const =0
returns a ForLoop object.
CodeContext(const std::shared_ptr< CompilationParameters > &cparams)
Lone constructor takes CompilationParams.
Definition context.cc:76
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:99
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:210
std::string end_of_stat() const override
Implementation of CodeContext::end_of_stat()
Definition context.cc:527
std::string inteval_type_name(const std::string &task) const override
Implementation of CodeContext::inteval_type_name()
Definition context.cc:577
std::shared_ptr< ForLoop > for_loop(std::string &varname, const std::shared_ptr< Entity > &less_than, const std::shared_ptr< Entity > &start_at) const override
Implementation of CodeContext::for_loop()
Definition context.cc:543
std::string code_prefix() const override
Implementation of CodeContext::code_prefix()
Definition context.cc:139
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:290
std::string copyright() const override
Implementation of CodeContext::copyright()
Definition context.cc:158
std::string accumulate(const std::string &name, const std::string &value) override
Implementation of CodeContext::accumulate()
Definition context.cc:252
std::string value_to_pointer(const std::string &val) const override
Implementation of CodeContext::value_to_pointer()
Definition context.cc:532
std::string inteval_spec_type_name(const std::string &task) const override
Implementation of CodeContext::inteval_spec_type_name()
Definition context.cc:584
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:297
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:227
std::string inteval_gen_type_name() const override
Implementation of CodeContext::inteval_spec_type_name()
Definition context.cc:591
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:237
std::string macro_endif() const override
Implementation of CodeContext::macro_endif()
Definition context.cc:510
std::string macro_ifdef(const std::string &name) const override
Implementation of CodeContext::macro_ifdef()
Definition context.cc:504
std::string close_block() const override
Implementation of CodeContext::close_block()
Definition context.cc:525
std::string comment(const std::string &statement) const override
Implementation of CodeContext::comment(statement)
Definition context.cc:516
std::string code_postfix() const override
Implementation of CodeContext::code_postfix()
Definition context.cc:150
std::string stack_address(const DGVertex::Address &a) const override
Implementation of CodeContext::stack_address()
Definition context.cc:476
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:351
std::string assign(const std::string &name, const std::string &value) override
Implementation of CodeContext::assign()
Definition context.cc:247
std::string macro_define(const std::string &name) const override
Implementation of CodeContext::macro_define()
Definition context.cc:485
std::string std_function_header() const override
Implementation of CodeContext::std_function_header()
Definition context.cc:201
std::string label_to_name(const std::string &label) const override
Implementation of CodeContext::label_to_name(label)
Definition context.cc:209
std::string macro_if(const std::string &name) const override
Implementation of CodeContext::macro_if()
Definition context.cc:498
std::string declare(const std::string &type, const std::string &name) const override
Implementation of CodeContext::declare()
Definition context.cc:218
std::string std_header() const override
Implementation of CodeContext::std_header()
Definition context.cc:196
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:434
std::string open_block() const override
Implementation of CodeContext::open_block()
Definition context.cc:523
MemoryManager::Address Address
The address on the stack during computation is described using this type.
Definition dgvertex.h:47
Defaults definitions for various parameters assumed by Libint.
Definition algebra.cc:24