Block-Structured AMR Software Framework
 
Loading...
Searching...
No Matches
AMReX_IParser_Y.H
Go to the documentation of this file.
1#ifndef AMREX_IPARSER_Y_H_
2#define AMREX_IPARSER_Y_H_
3#include <AMReX_Config.H>
4
6#include <AMReX_GpuPrint.H>
7#include <AMReX_Math.H>
8#include <AMReX_Print.H>
9
10#include <cstddef>
11#include <cstdio>
12#include <cstdlib>
13#include <cstring>
14#include <set>
15#include <string>
16#include <type_traits>
17
18void amrex_iparsererror (char const *s, ...);
19
20namespace amrex {
21
22enum iparser_f1_t { // Built-in functions with one argument
23 IPARSER_ABS = 1
24};
25
41
42enum iparser_f3_t { // functions with three arguments
44};
45
70
71/* In C, the address of the first member of a struct is the same as
72 * the address of the struct itself. Because of this, all struct iparser_*
73 * pointers can be passed around as struct iparser_node pointer and enum
74 * iparser_node_t type can be safely checked to determine their real type.
75 */
76
78 struct iparser_node* n;
79 long long v;
80 int ip;
81};
82
85 struct iparser_node* l;
86 struct iparser_node* r;
87 union iparser_nvp lvp; // After optimization, this may store left value/pointer offset.
88 int rip; // this may store right pointer offset.
89};
90
93 long long value;
94};
95
98 char* name;
99 int ip;
100};
101
102struct iparser_f1 { /* Builtin functions with one argument */
106};
107
108struct iparser_f2 { /* Builtin functions with two arguments */
113};
114
115struct iparser_f3 { /* Builtin functions with three arguments */
121};
122
128
129static_assert(sizeof(iparser_f3) <= sizeof(iparser_node), "amrex iparser: sizeof iparser_node too small");
130
131/*******************************************************************/
132
133/* These functions are used in bison rules to generate the original AST. */
134void iparser_defexpr (struct iparser_node* body);
135struct iparser_symbol* iparser_makesymbol (char* name);
136struct iparser_node* iparser_newnode (enum iparser_node_t type, struct iparser_node* l,
137 struct iparser_node* r);
138struct iparser_node* iparser_newnumber (long long d);
139struct iparser_node* iparser_newsymbol (struct iparser_symbol* sym);
140struct iparser_node* iparser_newf1 (enum iparser_f1_t ftype, struct iparser_node* l);
141struct iparser_node* iparser_newf2 (enum iparser_f2_t ftype, struct iparser_node* l,
142 struct iparser_node* r);
143struct iparser_node* iparser_newf3 (enum iparser_f3_t ftype, struct iparser_node* n1,
144 struct iparser_node* n2, struct iparser_node* n3);
145struct iparser_node* iparser_newassign (struct iparser_symbol* s, struct iparser_node* v);
146struct iparser_node* iparser_newlist (struct iparser_node* nl, struct iparser_node* nr);
147struct iparser_node* iparser_newcmpchain (struct iparser_node* nl, enum iparser_f2_t cmp,
148 struct iparser_node* nr);
149
150/*******************************************************************/
151
152/* This is our struct for storing AST in a more packed way. The whole
153 * tree is stored in a contiguous chunk of memory starting from void*
154 * p_root with a size of sz_mempool.
155 */
157 void* p_root;
158 void* p_free;
160 std::size_t sz_mempool;
161};
162
164void amrex_iparser_delete (struct amrex_iparser* iparser);
166
167struct iparser_node* iparser_ast_dup (struct amrex_iparser* iparser, struct iparser_node* node);
168
169void iparser_regvar (struct amrex_iparser* iparser, char const* name, int i);
170void iparser_setconst (struct amrex_iparser* iparser, char const* name, long long c);
171void iparser_print (struct amrex_iparser* iparser);
172std::set<std::string> iparser_get_symbols (struct amrex_iparser* iparser);
173int iparser_depth (struct amrex_iparser* iparser);
174
175/* We need to walk the tree in these functions */
176void iparser_ast_optimize (struct iparser_node* node);
177std::size_t iparser_ast_size (struct iparser_node* node);
178void iparser_ast_print (struct iparser_node* node, std::string const& space, AllPrint& printer);
179void iparser_ast_regvar (struct iparser_node* node, char const* name, int i);
180void iparser_ast_setconst (struct iparser_node* node, char const* name, long long c);
181void iparser_ast_get_symbols (struct iparser_node* node, std::set<std::string>& symbols,
182 std::set<std::string>& local_symbols);
183int iparser_ast_depth (struct iparser_node* node);
184
185/*******************************************************************/
186
188iparser_call_f1 (enum iparser_f1_t /*type*/, long long a)
189{
191 return std::abs(a);
192}
193
195iparser_call_f2 (enum iparser_f2_t type, long long a, long long b)
196{
197 switch (type) {
198 case IPARSER_FLRDIV:
199 {
200 long long r = a/b;
201 if (r*b == a || (a < 0 && b < 0) || (a > 0 && b > 0)) {
202 return r;
203 } else {
204 return r-1;
205 }
206 }
207 case IPARSER_POW:
208 {
209 if (b < 0) {
210 return 0;
211 } else {
212 long long r = 1;
213 while (b != 0) {
214 if (b & 1) {
215 r *= a;
216 }
217 b >>= 1;
218 if (b > 0) { a *= a; } // to avoid overflow
219 }
220 return r;
221 }
222 }
223 case IPARSER_GT:
224 return (a > b) ? 1 : 0;
225 case IPARSER_LT:
226 return (a < b) ? 1 : 0;
227 case IPARSER_GEQ:
228 return (a >= b) ? 1 : 0;
229 case IPARSER_LEQ:
230 return (a <= b) ? 1 : 0;
231 case IPARSER_EQ:
232 return (a == b) ? 1 : 0;
233 case IPARSER_NEQ:
234 return (a != b) ? 1 : 0;
236 case IPARSER_AND:
237 return ((a != 0) && (b != 0)) ? 1 : 0;
238 case IPARSER_OR:
239 return ((a != 0) || (b != 0)) ? 1 : 0;
240 case IPARSER_MIN:
241 return (a < b) ? a : b;
242 case IPARSER_MAX:
243 return (a > b) ? a : b;
244 default:
245 amrex::Abort("iparser_call_f2: Unknown function");
246 return 0;
247 }
248}
249
251iparser_call_f3 (enum iparser_f3_t /*type*/, long long a, long long b, long long c)
252{
253 // There is only one type currently
254 return (a != 0) ? b : c;
255}
256
257long long iparser_atoll (const char* str);
258
259}
260
261#endif
#define AMREX_FORCE_INLINE
Definition AMReX_Extension.H:119
#define AMREX_GPU_HOST_DEVICE
Definition AMReX_GpuQualifiers.H:20
void amrex_iparsererror(char const *s,...)
Definition AMReX_IParser_Y.cpp:10
Print on all processors of the default communicator.
Definition AMReX_Print.H:117
Definition AMReX_Amr.cpp:49
__host__ __device__ long long iparser_call_f2(enum iparser_f2_t type, long long a, long long b)
Definition AMReX_IParser_Y.H:195
struct iparser_node * iparser_newf3(enum iparser_f3_t ftype, struct iparser_node *n1, struct iparser_node *n2, struct iparser_node *n3)
Definition AMReX_IParser_Y.cpp:97
struct iparser_node * iparser_newnumber(long long d)
Definition AMReX_IParser_Y.cpp:58
struct iparser_node * iparser_newcmpchain(struct iparser_node *nl, enum iparser_f2_t cmp, struct iparser_node *nr)
Definition AMReX_IParser_Y.cpp:166
struct iparser_node * iparser_newf2(enum iparser_f2_t ftype, struct iparser_node *l, struct iparser_node *r)
Definition AMReX_IParser_Y.cpp:85
struct iparser_node * iparser_newf1(enum iparser_f1_t ftype, struct iparser_node *l)
Definition AMReX_IParser_Y.cpp:74
void iparser_print(struct amrex_iparser *iparser)
Definition AMReX_IParser_Y.cpp:1452
void iparser_ast_print(struct iparser_node *node, std::string const &space, AllPrint &printer)
Definition AMReX_IParser_Y.cpp:1110
std::set< std::string > iparser_get_symbols(struct amrex_iparser *iparser)
Definition AMReX_IParser_Y.cpp:1459
long long iparser_atoll(const char *str)
Definition AMReX_IParser_Y.cpp:1477
struct amrex_iparser * amrex_iparser_new()
Definition AMReX_IParser_Y.cpp:183
void iparser_ast_setconst(struct iparser_node *node, char const *name, long long c)
Definition AMReX_IParser_Y.cpp:1329
int iparser_ast_depth(struct iparser_node *node)
Definition AMReX_IParser_Y.cpp:1208
__host__ __device__ long long iparser_call_f3(enum iparser_f3_t, long long a, long long b, long long c)
Definition AMReX_IParser_Y.H:251
iparser_f1_t
Definition AMReX_IParser_Y.H:22
@ IPARSER_ABS
Definition AMReX_IParser_Y.H:23
struct iparser_node * iparser_newnode(enum iparser_node_t type, struct iparser_node *l, struct iparser_node *r)
Definition AMReX_IParser_Y.cpp:47
void amrex_iparser_delete(struct amrex_iparser *iparser)
Definition AMReX_IParser_Y.cpp:205
void iparser_ast_optimize(struct iparser_node *node)
Definition AMReX_IParser_Y.cpp:426
struct iparser_node * iparser_newlist(struct iparser_node *nl, struct iparser_node *nr)
Definition AMReX_IParser_Y.cpp:122
__host__ __device__ long long iparser_call_f1(enum iparser_f1_t, long long a)
Definition AMReX_IParser_Y.H:188
std::size_t iparser_ast_size(struct iparser_node *node)
Definition AMReX_IParser_Y.cpp:242
void iparser_ast_get_symbols(struct iparser_node *node, std::set< std::string > &symbols, std::set< std::string > &local_symbols)
Definition AMReX_IParser_Y.cpp:1384
iparser_f2_t
Definition AMReX_IParser_Y.H:26
@ IPARSER_LT
Definition AMReX_IParser_Y.H:30
@ IPARSER_GT
Definition AMReX_IParser_Y.H:29
@ IPARSER_POW
Definition AMReX_IParser_Y.H:28
@ IPARSER_FLRDIV
Definition AMReX_IParser_Y.H:27
@ IPARSER_AND
Definition AMReX_IParser_Y.H:36
@ IPARSER_OR
Definition AMReX_IParser_Y.H:37
@ IPARSER_MAX
Definition AMReX_IParser_Y.H:39
@ IPARSER_EQ
Definition AMReX_IParser_Y.H:33
@ IPARSER_MIN
Definition AMReX_IParser_Y.H:38
@ IPARSER_GEQ
Definition AMReX_IParser_Y.H:31
@ IPARSER_LEQ
Definition AMReX_IParser_Y.H:32
@ IPARSER_CMP_CHAIN
Definition AMReX_IParser_Y.H:35
@ IPARSER_NEQ
Definition AMReX_IParser_Y.H:34
struct iparser_node * iparser_newassign(struct iparser_symbol *sym, struct iparser_node *v)
Definition AMReX_IParser_Y.cpp:111
void iparser_regvar(struct amrex_iparser *iparser, char const *name, int i)
Definition AMReX_IParser_Y.cpp:1439
void iparser_setconst(struct amrex_iparser *iparser, char const *name, long long c)
Definition AMReX_IParser_Y.cpp:1445
struct iparser_symbol * iparser_makesymbol(char *name)
Definition AMReX_IParser_Y.cpp:35
void iparser_defexpr(struct iparser_node *body)
Definition AMReX_IParser_Y.cpp:29
void amrex_iparser_delete_ptrs()
Definition AMReX_IParser_Y.cpp:212
void iparser_ast_regvar(struct iparser_node *node, char const *name, int i)
Definition AMReX_IParser_Y.cpp:1265
int iparser_depth(struct amrex_iparser *iparser)
Definition AMReX_IParser_Y.cpp:1471
void Abort(const std::string &msg)
Print out message to cerr and exit via abort().
Definition AMReX.cpp:230
iparser_node_t
Definition AMReX_IParser_Y.H:46
@ IPARSER_ADD_PP
Definition AMReX_IParser_Y.H:60
@ IPARSER_MUL_PP
Definition AMReX_IParser_Y.H:64
@ IPARSER_NEG_P
Definition AMReX_IParser_Y.H:68
@ IPARSER_ASSIGN
Definition AMReX_IParser_Y.H:57
@ IPARSER_SUB
Definition AMReX_IParser_Y.H:50
@ IPARSER_DIV_VP
Definition AMReX_IParser_Y.H:65
@ IPARSER_DIV_PV
Definition AMReX_IParser_Y.H:66
@ IPARSER_F1
Definition AMReX_IParser_Y.H:54
@ IPARSER_NUMBER
Definition AMReX_IParser_Y.H:47
@ IPARSER_ADD_VP
Definition AMReX_IParser_Y.H:59
@ IPARSER_SUB_PP
Definition AMReX_IParser_Y.H:62
@ IPARSER_F3
Definition AMReX_IParser_Y.H:56
@ IPARSER_SUB_VP
Definition AMReX_IParser_Y.H:61
@ IPARSER_LIST
Definition AMReX_IParser_Y.H:58
@ IPARSER_NEG
Definition AMReX_IParser_Y.H:53
@ IPARSER_ADD
Definition AMReX_IParser_Y.H:49
@ IPARSER_DIV_PP
Definition AMReX_IParser_Y.H:67
@ IPARSER_F2
Definition AMReX_IParser_Y.H:55
@ IPARSER_MUL_VP
Definition AMReX_IParser_Y.H:63
@ IPARSER_DIV
Definition AMReX_IParser_Y.H:52
@ IPARSER_MUL
Definition AMReX_IParser_Y.H:51
@ IPARSER_SYMBOL
Definition AMReX_IParser_Y.H:48
struct iparser_node * iparser_ast_dup(struct amrex_iparser *my_iparser, struct iparser_node *node)
Definition AMReX_IParser_Y.cpp:311
struct iparser_node * iparser_newsymbol(struct iparser_symbol *symbol)
Definition AMReX_IParser_Y.cpp:68
iparser_f3_t
Definition AMReX_IParser_Y.H:42
@ IPARSER_IF
Definition AMReX_IParser_Y.H:43
Definition AMReX_IParser_Y.H:156
struct iparser_node * ast
Definition AMReX_IParser_Y.H:159
std::size_t sz_mempool
Definition AMReX_IParser_Y.H:160
void * p_root
Definition AMReX_IParser_Y.H:157
void * p_free
Definition AMReX_IParser_Y.H:158
Definition AMReX_IParser_Y.H:123
struct iparser_node * v
Definition AMReX_IParser_Y.H:126
enum iparser_node_t type
Definition AMReX_IParser_Y.H:124
struct iparser_symbol * s
Definition AMReX_IParser_Y.H:125
Definition AMReX_IParser_Y.H:102
struct iparser_node * l
Definition AMReX_IParser_Y.H:104
enum iparser_f1_t ftype
Definition AMReX_IParser_Y.H:105
enum iparser_node_t type
Definition AMReX_IParser_Y.H:103
Definition AMReX_IParser_Y.H:108
struct iparser_node * r
Definition AMReX_IParser_Y.H:111
struct iparser_node * l
Definition AMReX_IParser_Y.H:110
enum iparser_node_t type
Definition AMReX_IParser_Y.H:109
enum iparser_f2_t ftype
Definition AMReX_IParser_Y.H:112
Definition AMReX_IParser_Y.H:115
enum iparser_node_t type
Definition AMReX_IParser_Y.H:116
struct iparser_node * n3
Definition AMReX_IParser_Y.H:119
enum iparser_f3_t ftype
Definition AMReX_IParser_Y.H:120
struct iparser_node * n1
Definition AMReX_IParser_Y.H:117
struct iparser_node * n2
Definition AMReX_IParser_Y.H:118
Definition AMReX_IParser_Y.H:83
struct iparser_node * l
Definition AMReX_IParser_Y.H:85
struct iparser_node * r
Definition AMReX_IParser_Y.H:86
union iparser_nvp lvp
Definition AMReX_IParser_Y.H:87
int rip
Definition AMReX_IParser_Y.H:88
enum iparser_node_t type
Definition AMReX_IParser_Y.H:84
Definition AMReX_IParser_Y.H:91
long long value
Definition AMReX_IParser_Y.H:93
enum iparser_node_t type
Definition AMReX_IParser_Y.H:92
Definition AMReX_IParser_Y.H:96
int ip
Definition AMReX_IParser_Y.H:99
char * name
Definition AMReX_IParser_Y.H:98
enum iparser_node_t type
Definition AMReX_IParser_Y.H:97
Definition AMReX_IParser_Y.H:77
long long v
Definition AMReX_IParser_Y.H:79
struct iparser_node * n
Definition AMReX_IParser_Y.H:78
int ip
Definition AMReX_IParser_Y.H:80