Astaroth  3.1
Scalable Multi-GPU Stencil Computations
Loading...
Searching...
No Matches
expr.h
Go to the documentation of this file.
1#pragma once
2
3#include "tinyexpr.h"
4
5static void replace_const_ints(ASTNode* node, const string_vec values, const string_vec names)
6{
7 //TP: do not replace const int on lhs of assignment to avoid replacing the initial assignment
8 //if there is an array assignment on the lhs then cannot be the initial assignment
9 if(node->lhs &&
10 (
12 || get_node(NODE_ARRAY_ACCESS,node->lhs)
13 )
14 )
15 {
16 replace_const_ints(node->lhs,values,names);
17 }
18 if(node->rhs)
19 replace_const_ints(node->rhs,values,names);
20 if(node->token != IDENTIFIER || !node->buffer) return;
21 const int index = str_vec_get_index(names,node->buffer);
22 if(index == -1) return;
23 astnode_set_buffer(values.data[index],node);
24 node->type = NODE_UNKNOWN;
25 node->token = NUMBER;
26}
27
28static void eval_ternaries(ASTNode* node, const string_vec values,
29 const string_vec names, const string_vec run_values,
30 const string_vec run_names, const bool failure_fatal,
31 int* err);
32//TP: elements of global const int arrays, stored as "name[i]" --> value, such that e.g. field_order(arr[2]-1) can be evaluated
36
37static inline int
38eval_int(ASTNode* node, const string_vec values, const string_vec names,
39 const string_vec run_values, const string_vec run_names,
40 const bool failure_fatal, int* error_code);
41
42static void
43replace_const_int_array_accesses(ASTNode* node, const string_vec values, const string_vec names,
44 const string_vec run_values, const string_vec run_names,
45 const bool failure_fatal)
46{
47 if(node->lhs)
48 replace_const_int_array_accesses(node->lhs, values, names, run_values, run_names, failure_fatal);
49 if(node->rhs)
50 replace_const_int_array_accesses(node->rhs, values, names, run_values, run_names, failure_fatal);
51 //TP: members of const int3s, stored as "name.x" etc.
52 if((node->type & NODE_STRUCT_EXPRESSION) && node->lhs && node->rhs)
53 {
54 if(!(node->lhs->type & NODE_PRIMARY_EXPRESSION)) return;
55 const ASTNode* id = get_node_by_token(IDENTIFIER, node->lhs);
56 const ASTNode* member = get_node_by_token(IDENTIFIER, node->rhs);
57 if(!id || !id->buffer || !member || !member->buffer) return;
58 if(str_vec_get_index(const_int_arrays, id->buffer) == -1) return;
59 const int elem = str_vec_get_index(const_int_array_elems, sprintf_intern("%s.%s", id->buffer, member->buffer));
60 if(elem == -1) return;
61 set_buffers_empty(node);
62 node->lhs = NULL;
63 node->rhs = NULL;
64 astnode_set_buffer(const_int_array_elem_values.data[elem], node);
65 node->type = NODE_UNKNOWN;
66 node->token = NUMBER;
67 return;
68 }
69
70 if(!(node->type & NODE_ARRAY_ACCESS) || !node->lhs || !node->rhs) return;
71 if(!(node->lhs->type & NODE_PRIMARY_EXPRESSION)) return;
72 const ASTNode* id = get_node_by_token(IDENTIFIER, node->lhs);
73 if(!id || !id->buffer || str_vec_get_index(const_int_arrays, id->buffer) == -1) return;
74 int err = 0;
75 const int index = eval_int(node->rhs, values, names, run_values, run_names, failure_fatal, &err);
76 if(err) return;
77 const int elem = str_vec_get_index(const_int_array_elems, sprintf_intern("%s[%d]", id->buffer, index));
78 if(elem == -1)
79 {
80 if(failure_fatal)
81 {
82 fprintf(stderr,"Index %d out of bounds for const int array %s\n", index, id->buffer);
83 exit(EXIT_FAILURE);
84 }
85 return;
86 }
87 set_buffers_empty(node);
88 node->lhs = NULL;
89 node->rhs = NULL;
90 astnode_set_buffer(const_int_array_elem_values.data[elem], node);
91 node->type = NODE_UNKNOWN;
92 node->token = NUMBER;
93}
94
95
96static inline int
97eval_int(ASTNode* node, const string_vec values, const string_vec names,
98 const string_vec run_values, const string_vec run_names,
99 const bool failure_fatal, int* error_code)
100{
101 replace_const_int_array_accesses(node, values, names, run_values, run_names, failure_fatal);
102 replace_const_ints(node, values, names);
103 replace_const_ints(node, run_values, run_names);
104 eval_ternaries(node, values, names, run_values, run_names,
105 failure_fatal, error_code);
106 const char* copy = combine_all_new(node);
107 if(!strcmp(copy,"INT_MAX"))
108 return INT_MAX;
109 int err;
110 te_expr* expr = te_compile(copy, NULL, 0, &err);
111 if(!expr)
112 {
113 if(failure_fatal)
114 {
115 fprintf(stderr,"Parse error at tinyexpr\n");
116 fprintf(stderr,"Was not able to parse: %s\n",copy);
117 exit(EXIT_FAILURE);
118 }
119 *error_code = 1;
120 te_free(expr);
121 return -1;
122 }
123 int res = (int) round(te_eval(expr));
124 te_free(expr);
125 return res;
126}
127
128static void
129eval_ternaries(ASTNode* node, const string_vec values, const string_vec names,
130 const string_vec run_values, const string_vec run_names,
131 const bool failure_fatal, int* err)
132{
133 if(node->lhs)
134 {
135 eval_ternaries(node->lhs, values, names, run_values, run_names,
136 failure_fatal, err);
137 }
138 if(node->rhs)
139 {
140 eval_ternaries(node->rhs, values, names, run_values, run_names,
141 failure_fatal, err);
142 }
143 if(node->type != NODE_TERNARY_EXPRESSION) return;
144 //TP: for now consider only that expr is a < b
145 //TP: where a and b are const int expressions
146 //printf("ORIG: %s\n",combine_all_new(node));
147 const ASTNode* cond = node->lhs->lhs;
148 if(!cond) return;
149 const char* op = cond->rhs->lhs->buffer;
150 if(!op) return;
151 const bool less = !strcmp(op,"<");
152 const bool more = !strcmp(op,">");
153 const bool eq = !strcmp(op,"==");
154 const bool neq = !strcmp(op,"!=");
155 if(!less && !more && !eq && !neq) return;
156 const int lhs = eval_int(cond->lhs, values, names, run_values, run_names, failure_fatal,err);
157 if(!failure_fatal && *err) return;
158 const int rhs = eval_int(cond->rhs->rhs, values, names, run_values, run_names, failure_fatal, err);
159 if(!failure_fatal && *err) return;
160 const bool pick_left = (less && lhs < rhs) || (more && lhs > rhs) || (eq && lhs == rhs) || (neq && lhs != rhs);
161 ASTNode* correct_node = pick_left ? node->rhs->lhs : node->rhs->rhs;
162 if(!correct_node) return;
163 *node = *correct_node;
164 node->prefix = NULL;
165}
166
@ NODE_ASSIGNMENT
Definition ast.h:84
@ NODE_PRIMARY_EXPRESSION
Definition ast.h:65
@ NODE_ARRAY_ACCESS
Definition ast.h:89
@ NODE_TERNARY_EXPRESSION
Definition ast.h:104
@ NODE_DECLARATION
Definition ast.h:72
@ NODE_STRUCT_EXPRESSION
Definition ast.h:97
@ NODE_UNKNOWN
Definition ast.h:64
string_vec const_int_arrays
string_vec const_int_array_elem_values
string_vec const_int_array_elems
Definition ast.h:146
const char * prefix
Definition ast.h:151
struct astnode_s * lhs
Definition ast.h:148
struct astnode_s * rhs
Definition ast.h:149
NodeType type
Definition ast.h:157
int token
Definition ast.h:156
const char * buffer
Definition ast.h:150
Definition string_vec.h:9
const char ** data
Definition string_vec.h:10
Definition tinyexpr.h:33
void te_free(te_expr *n)
double te_eval(const te_expr *n)
te_expr * te_compile(const char *expression, const te_variable *variables, int var_count, int *error)
static __device__ const int const int index
Definition user_kernel.h:128