48#define BUFFER_SIZE (4096)
49#define FATAL_ERROR_MESSAGE "\nFATAL AC ERROR: "
50#define KERNEL_PREFIX "__global__ void \n#if MAX_THREADS_PER_BLOCK\n__launch_bounds__(MAX_THREADS_PER_BLOCK)\n#endif\n"
52fatal(
const char* format, ...)
57 vfprintf(stderr,format,args);
108static __attribute__((unused))
const char*
109node_type_to_str(
const NodeType type)
135 if(type ==
NODE_IF)
return "if";
146typedef struct astnode_s {
171#define AST_ARENA_BLOCK_SIZE (1 << 16)
205 if(!node)
return NULL;
206 ASTNode* res = astnode_alloc();
210 res -> token = node->
token;
212 res -> no_auto = node->
no_auto;
219 res->
lhs= astnode_dup(node->
lhs,res);
221 res->
rhs= astnode_dup(node->
rhs,res);
232 if(node->
lhs) make_ids_unique(node->
lhs);
233 if(node->
rhs) make_ids_unique(node->
rhs);
241 ASTNode* node = astnode_alloc();
256 node->
lhs->parent = node;
259 node->
rhs->parent = node;
268 astnode_destroy(node->
lhs);
270 astnode_destroy(node->
rhs);
289astnode_set_buffer(
const char* buffer,
ASTNode* node)
291 node->
buffer = intern(buffer);
296astnode_set_prefix(
const char* prefix,
ASTNode* node)
298 node->
prefix= intern(prefix);
302astnode_set_infix(
const char* infix,
ASTNode* node)
304 node->
infix = intern(infix);
308astnode_set_postfix(
const char* postfix,
ASTNode* node)
310 node->
postfix = intern(postfix);
312static inline const char*
313sprintf_intern(
const char* format, ...)
315 static char buffer[10000];
318 vsprintf(buffer, format, args);
320 return intern(buffer);
324astnode_sprintf(
ASTNode* node,
const char* format, ...)
326 static char buffer[10000];
329 vsprintf(buffer, format, args);
331 astnode_set_buffer(buffer,node);
336astnode_sprintf_postfix(
ASTNode* node,
const char* format, ...)
338 static char buffer[100000];
341 vsprintf(buffer, format, args);
343 astnode_set_postfix(buffer,node);
347astnode_sprintf_infix(
ASTNode* node,
const char* format, ...)
349 static char buffer[10000];
352 vsprintf(buffer, format, args);
354 astnode_set_infix(buffer,node);
358astnode_sprintf_prefix(
ASTNode* node,
const char* format, ...)
360 static char buffer[10000];
363 vsprintf(buffer, format, args);
365 astnode_set_prefix(buffer,node);
368astnode_print(
const ASTNode* node)
370 printf(
"%u (%p)\n", node->
type, (
void*)node);
371 printf(
"\tid: %d\n", node->
id);
372 printf(
"\tparent: %p\n", (
void*)node->
parent);
373 printf(
"\tlhs: %p\n", (
void*)node->
lhs);
374 printf(
"\trhs: %p\n", (
void*)node->
rhs);
375 printf(
"\tbuffer: %s\n", node->
buffer);
376 printf(
"\ttoken: %d\n", node->
token);
377 printf(
"\tprefix: %p (\"%s\")\n", (
void*)node->
prefix, node->
prefix);
378 printf(
"\tinfix: %p (\"%s\")\n", (
void*)node->
infix, node->
infix);
379 printf(
"\tpostfix: %p (\"%s\")\n", (
void*)node->
postfix, node->
postfix);
383static inline void combine_buffers_recursive(
const ASTNode* node,
char* res){
387 combine_buffers_recursive(node->
lhs, res);
389 combine_buffers_recursive(node->
rhs, res);
391static inline void combine_buffers(
const ASTNode* node,
char* res){
393 combine_buffers_recursive(node,res);
395static inline const char* combine_buffers_new(
const ASTNode* node)
397 static char res[10000];
398 combine_buffers(node,res);
401static inline void combine_all_recursive(
const ASTNode* node,
char* res){
405 combine_all_recursive(node->
lhs, res);
409 strcat(res,node->
infix);
411 combine_all_recursive(node->
rhs, res);
416strip_whitespace(
char *str) {
421 while (isspace((
unsigned char)(*
src))) {
427 if (!isspace((
unsigned char)(*
src))) {
437strip_whitespace_new(
const char*
src) {
438 char* res = strdup(
src);
439 strip_whitespace(res);
443static inline void combine_all(
const ASTNode* node,
char* res){
445 combine_all_recursive(node,res);
446 strip_whitespace(res);
448static inline const char*combine_all_new(
const ASTNode* node){
449 static char res[10000];
451 combine_all_recursive(node,res);
452 strip_whitespace(res);
455static inline const char* combine_all_new_with_whitespace(
const ASTNode* node){
456 static char res[10000];
458 combine_all_recursive(node,res);
463get_node_by_token(
const int token,
const ASTNode* node)
467 if (node->
token == token)
471 ASTNode* lhs_res = get_node_by_token(token,node->
lhs);
472 if(lhs_res)
return lhs_res;
476 ASTNode* rhs_res = get_node_by_token(token,node->
rhs);
477 if(rhs_res)
return rhs_res;
482get_node_by_buffer_exclude_node(
const char* test,
const ASTNode* node,
const ASTNode* exclude)
486 if(node->
id == exclude->
id)
return res;
489 if (node->
lhs && !res)
490 res = get_node_by_buffer_exclude_node(test, node->
lhs,exclude);
491 if (node->
rhs && !res)
492 res = get_node_by_buffer_exclude_node(test, node->
rhs,exclude);
497get_node_by_buffer(
const char* test,
const ASTNode* node)
504 if (node->
lhs && !res)
505 res = get_node_by_buffer(test, node->
lhs);
506 if (node->
rhs && !res)
507 res = get_node_by_buffer(test, node->
rhs);
511get_node_by_buffer_and_type(
const char* test,
const NodeType type,
const ASTNode* node)
518 if (node->
lhs && !res)
519 res = get_node_by_buffer_and_type(test, type, node->
lhs);
520 if (node->
rhs && !res)
521 res = get_node_by_buffer_and_type(test, type, node->
rhs);
526get_node_by_buffer_and_token(
const char* test,
const int token,
const ASTNode* node)
533 if (node->
lhs && !res)
534 res = get_node_by_buffer_and_token(test, token, node->
lhs);
535 if (node->
rhs && !res)
536 res = get_node_by_buffer_and_token(test, token, node->
rhs);
540get_node_by_id(
const int id,
const ASTNode* node)
547 if (node->
lhs && !res)
548 res = get_node_by_id(
id, node->
lhs);
549 if (node->
rhs && !res)
550 res = get_node_by_id(
id, node->
rhs);
558 if (node->
parent->type & type)
560 return get_parent_node(type, node->
parent);
565 if(node->
type & type)
return node;
568 if (node->
parent->type & type)
570 return get_parent_node(type, node->
parent);
574get_parent_node_by_token(
const int token,
const ASTNode* node)
578 if (node->
parent->token == token)
580 return get_parent_node_by_token(token, node->
parent);
586 const ASTNode* parent = get_parent_node(type,node);
587 if(!parent)
return false;
588 return get_node_by_id(node->
id,parent->
lhs) != NULL;
592is_left_child_token(
const int token,
const ASTNode* node)
594 const ASTNode* parent = get_parent_node_by_token(token,node);
595 if(!parent)
return false;
596 return get_node_by_id(node->
id,parent->
lhs) != NULL;
602 const ASTNode* parent = get_parent_node(type,node);
603 if(!parent)
return false;
604 return get_node_by_id(node->
id,parent->
rhs) != NULL;
611 if (node->
parent->type == type)
613 return get_parent_node(type, node->
parent);
621static inline char* itoa(
const int x)
624 my_asprintf(&res,
"%d",x);
629set_buffers_empty(
ASTNode* node)
636 set_buffers_empty(node->
lhs);
638 set_buffers_empty(node->
rhs);
641add_node_type(
const NodeType type,
ASTNode* node,
const char* str_to_check)
644 add_node_type(type,node->
lhs,str_to_check);
646 add_node_type(type,node->
rhs,str_to_check);
647 if(!str_to_check || (node->
buffer && !strcmp(node->
buffer,str_to_check)))
651add_no_auto(
ASTNode* node,
const char* str_to_check)
655 add_no_auto(node->
lhs,str_to_check);
657 add_no_auto(node->
rhs,str_to_check);
658 if(!str_to_check || (node->
buffer && !strcmp(node->
buffer,str_to_check)))
661static inline void strprepend(
char* dst,
const char*
src)
663 memmove(dst + strlen(
src), dst, strlen(dst)+ 1);
664 memcpy(dst,
src, strlen(
src));
667strcatprintf(
char* dst,
const char* format, ...)
669 static char buffer[10000];
672 vsprintf(buffer, format, args);
679fprintf_filename(
const char* filename,
const char* format, ...)
681 FILE* fp = fopen(filename,
"a");
684 vfprintf(fp, format, args);
689fprintf_filename_w(
const char* filename,
const char* format, ...)
691 FILE* fp = fopen(filename,
"w");
694 vfprintf(fp, format, args);
700sprintf_new(
const char* format, ...)
702 static char buffer[10000];
705 vsprintf(buffer,format,args);
707 return strdup(buffer);
709static inline char* readFile(
const char *filename) {
710 FILE *
file = fopen(filename,
"rb");
713 perror(
"Error opening file");
718 fseek(
file, 0, SEEK_END);
719 long file_size = ftell(
file);
723 char *buffer = (
char *)malloc(file_size + 1);
724 if (buffer == NULL) {
725 perror(
"Memory allocation failed");
731 size_t bytes_read = fread(buffer, 1, file_size,
file);
732 if ((
long) bytes_read != file_size) {
733 perror(
"Error reading file");
740 buffer[file_size] =
'\0';
749file_append(
const char* filename,
const char* str_to_append)
751 FILE* fp = fopen(filename,
"a");
752 fprintf(fp,
"%s",str_to_append);
758is_number(
const char* str)
760 const size_t n = strlen(str);
762 for(
size_t i = 0; i < n; ++i)
763 res &= (isdigit(str[i]) > 0);
767is_number_expression(
const char* str)
769 const size_t n = strlen(str);
771 for(
size_t i = 0; i < n; ++i)
784is_real(
const char* str)
786 char* tmp = strdup(str);
787 remove_substring(tmp,
".");
788 remove_substring(tmp,
"(");
789 remove_substring(tmp,
")");
790 remove_substring(tmp,
"AcReal");
791 const bool res = is_number(tmp);
797count_num_of_nodes_in_list(
const ASTNode* list_head)
799 if(list_head == NULL)
return 0;
801 while(list_head->
rhs)
803 list_head = list_head->
lhs;
806 res += (list_head->
lhs != NULL);
812 bool last_elem = count_num_of_nodes_in_list(list_head) ==
index + 1;
815 list_head = list_head->
lhs;
817 return last_elem ? list_head->
lhs : list_head->
rhs;
819static __attribute__((unused))
void
831static inline bool has_qualifier(
const ASTNode* node,
const char* qualifier)
835 res |= has_qualifier(node->
lhs,qualifier);
837 res |= has_qualifier(node->
rhs,qualifier);
839 res |= node->
lhs->buffer == qualifier;
844format_source(
const char* file_in,
const char* file_out)
846 FILE* in = fopen(file_in,
"r");
849 FILE* out = fopen(file_out,
"w");
853 const int c = fgetc(in);
857 fprintf(out,
"%c", c);
858 if (c ==
';' || c ==
'{')
867file_exists(
const char* filename)
870 return (stat (filename, &buffer) == 0);
902 for(
size_t i = 0; i < dst->
size; ++i)
903 tmp[i] = dst->
data[i];
911build_list_node(
const node_vec nodes,
const char* separator)
913 if(nodes.
size == 0)
return NULL;
915 for(
size_t i = 1; i < nodes.
size; ++i)
917 list_head = astnode_create(
NODE_UNKNOWN,list_head, astnode_dup(nodes.
data[i],NULL));
918 list_head->
buffer = intern(separator);
929 fatal(
"WRONG; passed NULL to get_node\n");
932 if (node->
type & type)
936 const ASTNode* lhs_res = get_node(type,node->
lhs);
937 if(lhs_res)
return lhs_res;
941 const ASTNode* rhs_res = get_node(type,node->
rhs);
942 if(rhs_res)
return rhs_res;
947astnode_change_infixes(
ASTNode* node,
const char* old_infix,
const char* new_infix)
949 if(node == NULL)
return;
950 astnode_change_infixes(node->
lhs,old_infix,new_infix);
951 astnode_change_infixes(node->
rhs,old_infix,new_infix);
952 if(node->
infix != NULL && !strcmp(node->
infix,old_infix)) astnode_set_infix(new_infix,node);
957get_nodes_in_list(
const ASTNode* head)
960 if(head == NULL)
return res;
961 const int num_of_nodes = count_num_of_nodes_in_list(head);
965 push_node(&res,head->
lhs);
970 push_node(&res,head->
rhs);
980 get_array_access_nodes(node->
lhs,dst);
982 get_array_access_nodes(node->
rhs,dst);
984 push_node(dst,node->
rhs);
989 if(replacement == NULL)
991 fprintf(stderr,
"Tried to replace with a NULL node!");
994 if(original->
parent->lhs && original->
parent->lhs->id == original->
id)
995 original->
parent->lhs = replacement;
997 original->
parent->rhs = replacement;
1001node_vec_contains(
const node_vec vec,
const char* str)
1003 for(
size_t i = 0; i < vec.
size; ++i)
1004 if(!strcmp(combine_all_new(vec.
data[i]), str))
return true;
1009copy_file(
const char*
src,
const char* dst)
1012 sprintf(cmd,
"cp %s %s",
src,dst);
1013 if(system(cmd) != 0) fatal(
"Was not able to copy a file: %s!\n",cmd);
static const char const char * new_qualifier
Definition ast.h:821
#define FATAL_ERROR_MESSAGE
Definition ast.h:49
ast_arena * current_ast_arena
#define AST_ARENA_BLOCK_SIZE
Definition ast.h:171
NodeType
Definition ast.h:63
@ NODE_BOUNDCONDS_DEF
Definition ast.h:102
@ NODE_HOSTDEFINE
Definition ast.h:83
@ NODE_STENCIL
Definition ast.h:75
@ NODE_ANY
Definition ast.h:105
@ NODE_BINARY
Definition ast.h:70
@ NODE_ENUM_DEF
Definition ast.h:99
@ NODE_GLOBAL_DECLARATION
Definition ast.h:95
@ NODE_ASSIGNMENT
Definition ast.h:84
@ NODE_TSPEC
Definition ast.h:73
@ NODE_FUNCTION_CALL
Definition ast.h:92
@ NODE_ASSIGN_LIST
Definition ast.h:94
@ NODE_PRIMARY_EXPRESSION
Definition ast.h:65
@ NODE_DCONST
Definition ast.h:80
@ NODE_ARRAY_INITIALIZER
Definition ast.h:79
@ NODE_KFUNCTION
Definition ast.h:67
@ NODE_TERNARY
Definition ast.h:81
@ NODE_ARRAY_ACCESS
Definition ast.h:89
@ NODE_FUNCTION
Definition ast.h:98
@ NODE_DEF_BASE
Definition ast.h:86
@ NODE_TERNARY_EXPRESSION
Definition ast.h:104
@ NODE_DECLARATION
Definition ast.h:72
@ NODE_DFUNCTION_ID
Definition ast.h:93
@ NODE_INPUT
Definition ast.h:85
@ NODE_BINARY_EXPRESSION
Definition ast.h:103
@ NODE_DFUNCTION
Definition ast.h:66
@ NODE_STRUCT_DEF
Definition ast.h:100
@ NODE_EXPRESSION
Definition ast.h:76
@ NODE_STATEMENT_LIST_HEAD
Definition ast.h:90
@ NODE_FUNCTION_ID
Definition ast.h:68
@ NODE_TASKGRAPH_DEF
Definition ast.h:101
@ NODE_VARIABLE_ID
Definition ast.h:78
@ NODE_STRUCT_EXPRESSION
Definition ast.h:97
@ NODE_NO_OUT
Definition ast.h:96
@ NODE_IF
Definition ast.h:91
@ NODE_VARIABLE
Definition ast.h:77
@ NODE_GLOBAL
Definition ast.h:69
@ NODE_DEF
Definition ast.h:87
@ NODE_TQUAL
Definition ast.h:74
@ NODE_BEGIN_SCOPE
Definition ast.h:71
@ NODE_UNKNOWN
Definition ast.h:64
@ NODE_MEMBER_ID
Definition ast.h:82
@ NODE_STRUCT_INITIALIZER
Definition ast.h:88
static const char * old_qualifier
Definition ast.h:820
static const Stream const AcMeshOrder const AcReal * src
Definition astaroth_device.h:188
static int FILE * file
Definition string_vec.h:120
int counter
Definition string_vec.h:123
#define VEC_INITIALIZER
Definition string_vec.h:7
const char * prefix
Definition ast.h:151
bool is_constexpr
Definition ast.h:158
struct astnode_s * lhs
Definition ast.h:148
struct astnode_s * rhs
Definition ast.h:149
NodeType type
Definition ast.h:157
struct astnode_s * parent
Definition ast.h:147
int id
Definition ast.h:155
bool in_arena
Definition ast.h:160
int token
Definition ast.h:156
const char * postfix
Definition ast.h:153
const char * infix
Definition ast.h:152
bool no_auto
Definition ast.h:159
const char * buffer
Definition ast.h:150
const char * expr_type
Definition ast.h:154
size_t num_used_in_last_block
Definition ast.h:169
size_t num_blocks
Definition ast.h:168
ASTNode ** blocks
Definition ast.h:167
int capacity
Definition ast.h:877
size_t size
Definition ast.h:876
const ASTNode ** data
Definition ast.h:875
Definition string_vec.h:9
static __device__ const int const int index
Definition user_kernel.h:128
#define size(arr)
Definition user_kernel.h:73