Astaroth  3.1
Scalable Multi-GPU Stencil Computations
Loading...
Searching...
No Matches
ast.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2021, Johannes Pekkila.
3
4 This file is part of Astaroth.
5
6 Astaroth 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 Astaroth is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with Astaroth. If not, see <http://www.gnu.org/licenses/>.
17*/
18/*
19 * Abstract Syntax Tree
20 */
21#pragma once
22#include <assert.h>
23#include <ctype.h>
24#include <stdbool.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/stat.h>
29
30#include "hash.h"
31#include "vecs.h"
32
47
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"
51static const char*
52fatal(const char* format, ...)
53{
54 va_list args;
55 va_start(args,format);
56 fprintf(stderr,FATAL_ERROR_MESSAGE);
57 vfprintf(stderr,format,args);
58 va_end(args);
59 abort();
60 exit(EXIT_FAILURE);
61}
62
63typedef enum {
66 NODE_DFUNCTION = (1 << 1),
67 NODE_KFUNCTION = (1 << 2),
68 NODE_FUNCTION_ID = (1 << 3),
69 NODE_GLOBAL = (1 << 4),
70 NODE_BINARY = (1 << 5),
71 NODE_BEGIN_SCOPE = (1 << 6),
72 NODE_DECLARATION = (1 << 7),
73 NODE_TSPEC = (1 << 8),
74 NODE_TQUAL = (1 << 9),
75 NODE_STENCIL = (1 << 10),
76 NODE_EXPRESSION = (1 << 11),
77 NODE_VARIABLE = (1 << 12),
78 NODE_VARIABLE_ID = (1 << 13),
80 NODE_DCONST = (1 << 15),
81 NODE_TERNARY = (1 << 16),
82 NODE_MEMBER_ID = (1 << 17),
83 NODE_HOSTDEFINE = (1 << 18),
84 NODE_ASSIGNMENT = (1 << 19),
85 NODE_INPUT = (1 << 20),
86 NODE_DEF_BASE = (1 << 21),
89 NODE_ARRAY_ACCESS = (1 << 23),
91 NODE_IF = (1 << 25),
92 NODE_FUNCTION_CALL = (1 << 26),
93 NODE_DFUNCTION_ID = (1 << 27),
96 NODE_NO_OUT = (1 << 29),
106} NodeType;
107
108static __attribute__((unused)) const char*
109node_type_to_str(const NodeType type)
110{
111 if(type == NODE_UNKNOWN) return "unknown";
112 if(type == NODE_PRIMARY_EXPRESSION) return "primary_expression";
113 if(type == NODE_DFUNCTION) return "dfunction";
114 if(type == NODE_KFUNCTION) return "global";
115 if(type == NODE_BINARY) return "binary";
116 if(type == NODE_BEGIN_SCOPE) return "begin_scope";
117 if(type == NODE_DECLARATION) return "declaration";
118 if(type == NODE_TSPEC) return "tspec";
119 if(type == NODE_TQUAL) return "tqual";
120 if(type == NODE_STENCIL) return "stencil";
121 if(type == NODE_EXPRESSION) return "expression";
122 if(type == NODE_VARIABLE) return "variable";
123 if(type == NODE_VARIABLE_ID) return "variable_id";
124 if(type == NODE_ARRAY_INITIALIZER) return "array_initializer";
125 if(type == NODE_DCONST) return "dconst";
126 if(type == NODE_TERNARY) return "ternary";
127 if(type == NODE_MEMBER_ID) return "member_id";
128 if(type == NODE_HOSTDEFINE) return "hostdefine";
129 if(type == NODE_ASSIGNMENT) return "assignment";
130 if(type == NODE_INPUT) return "input";
131 if(type == NODE_DEF) return "def";
132 if(type == NODE_STRUCT_INITIALIZER) return "struct_initializer";
133 if(type == NODE_ARRAY_ACCESS) return "array_access";
134 if(type == NODE_STATEMENT_LIST_HEAD) return "statement_list_head";
135 if(type == NODE_IF) return "if";
136 if(type == NODE_FUNCTION_CALL) return "function_call";
137 if(type == NODE_DFUNCTION_ID) return "dfunction_id";
138 if(type == NODE_ASSIGN_LIST) return "assign_list";
139 if(type == NODE_NO_OUT) return "no_out";
140 if(type == NODE_STRUCT_EXPRESSION) return "struct_expression";
141 if(type == NODE_ANY) return "any";
142 return "unknown";
143}
144
145//Ordering is to make the smallest structs
146typedef struct astnode_s {
147 struct astnode_s* parent;
148 struct astnode_s* lhs;
149 struct astnode_s* rhs;
150 const char* buffer; // Indentifiers and other strings (empty by default)
151 const char* prefix; // Strings. Also makes the grammar since we don't have
152 const char* infix; // to divide it into max two-child rules
153 const char* postfix; // (which makes it much harder to read)
154 const char* expr_type; //The type of the expr the node represent
155 int id;
156 int token; // Type of a terminal (that is not a simple char)
157 NodeType type; // Type of the AST node
158 bool is_constexpr; //Whether the node represents information known at compile time
160 bool in_arena; //Allocated from an ast_arena, freed only together with the arena
161} ASTNode;
162
163//TP: a pass working on a copy of the tree (generate) allocates all of its nodes from an arena and frees them at once at the end of the pass.
164// This frees also the nodes the transformations detach from the tree without freeing them and avoids the overhead of allocating each node separately
165typedef struct
166{
170} ast_arena;
171#define AST_ARENA_BLOCK_SIZE (1 << 16)
172//Declared in ac.y
174
175static inline ASTNode*
176astnode_alloc(void)
177{
179 if(!arena) return (ASTNode*)calloc(1,sizeof(ASTNode));
180 if(arena->num_blocks == 0 || arena->num_used_in_last_block == AST_ARENA_BLOCK_SIZE)
181 {
182 arena->blocks = (ASTNode**)realloc(arena->blocks,sizeof(ASTNode*)*(arena->num_blocks+1));
183 arena->blocks[arena->num_blocks++] = (ASTNode*)calloc(AST_ARENA_BLOCK_SIZE,sizeof(ASTNode));
184 arena->num_used_in_last_block = 0;
185 }
186 ASTNode* res = &arena->blocks[arena->num_blocks-1][arena->num_used_in_last_block++];
187 res->in_arena = true;
188 return res;
189}
190static inline void
191ast_arena_free(ast_arena* arena)
192{
193 for(size_t i = 0; i < arena->num_blocks; ++i)
194 free(arena->blocks[i]);
195 free(arena->blocks);
196 arena->blocks = NULL;
197 arena->num_blocks = 0;
198 arena->num_used_in_last_block = 0;
199}
200
201
202static inline ASTNode*
203astnode_dup(const ASTNode* node, ASTNode* parent)
204{
205 if(!node) return NULL;
206 ASTNode* res = astnode_alloc();
207 res->id = node->id;
208 res->type = node->type;
209 res->parent = parent;
210 res -> token = node->token;
211 res -> is_constexpr = node->is_constexpr;
212 res -> no_auto = node->no_auto;
213 res -> expr_type = node->expr_type;
214 res->buffer = node->buffer;
215 res->prefix= node->prefix;
216 res->infix = node->infix;
217 res->postfix= node->postfix;
218 if(node->lhs)
219 res->lhs= astnode_dup(node->lhs,res);
220 if(node->rhs)
221 res->rhs= astnode_dup(node->rhs,res);
222 return res;
223}
224//Declared in ac.y
225extern int id_counter;
226
227
228static inline void
229make_ids_unique(ASTNode* node)
230{
231 node->id = id_counter++;
232 if(node->lhs) make_ids_unique(node->lhs);
233 if(node->rhs) make_ids_unique(node->rhs);
234}
235
236
237
238static inline ASTNode*
239astnode_create(const NodeType type, ASTNode* lhs, ASTNode* rhs)
240{
241 ASTNode* node = astnode_alloc();
242
243 node->id = id_counter++;
244 node->type = type;
245 node->lhs = lhs;
246 node->rhs = rhs;
247 node->buffer = NULL;
248 node->is_constexpr = false;
249 node->no_auto = false;
250 node->expr_type = NULL;
251
252 node->token = 0;
253 node->prefix = node->infix = node->postfix = NULL;
254
255 if (lhs)
256 node->lhs->parent = node;
257
258 if (rhs)
259 node->rhs->parent = node;
260
261 return node;
262}
263
264static inline void
265astnode_destroy(ASTNode* node)
266{
267 if (node->lhs)
268 astnode_destroy(node->lhs);
269 if (node->rhs)
270 astnode_destroy(node->rhs);
271 node->buffer = NULL;
272 node->prefix = NULL;
273 node->infix = NULL;
274 node->postfix = NULL;
275 if(!node->in_arena) free(node);
276}
277static inline void
278astnode_free(ASTNode* node)
279{
280 node->lhs = NULL;
281 node->rhs = NULL;
282 node->buffer = NULL;
283 node->prefix= NULL;
284 node->infix= NULL;
285 node->postfix= NULL;
286}
287
288static inline void
289astnode_set_buffer(const char* buffer, ASTNode* node)
290{
291 node->buffer = intern(buffer);
292}
293
294
295static inline void
296astnode_set_prefix(const char* prefix, ASTNode* node)
297{
298 node->prefix= intern(prefix);
299}
300
301static inline void
302astnode_set_infix(const char* infix, ASTNode* node)
303{
304 node->infix = intern(infix);
305}
306
307static inline void
308astnode_set_postfix(const char* postfix, ASTNode* node)
309{
310 node->postfix = intern(postfix);
311}
312static inline const char*
313sprintf_intern(const char* format, ...)
314{
315 static char buffer[10000];
316 va_list args;
317 va_start(args,format);
318 vsprintf(buffer, format, args);
319 va_end(args);
320 return intern(buffer);
321}
322
323static inline void
324astnode_sprintf(ASTNode* node, const char* format, ...)
325{
326 static char buffer[10000];
327 va_list args;
328 va_start(args,format);
329 vsprintf(buffer, format, args);
330 va_end(args);
331 astnode_set_buffer(buffer,node);
332}
333
334
335static inline void
336astnode_sprintf_postfix(ASTNode* node, const char* format, ...)
337{
338 static char buffer[100000];
339 va_list args;
340 va_start(args,format);
341 vsprintf(buffer, format, args);
342 va_end(args);
343 astnode_set_postfix(buffer,node);
344}
345
346static inline void
347astnode_sprintf_infix(ASTNode* node, const char* format, ...)
348{
349 static char buffer[10000];
350 va_list args;
351 va_start(args,format);
352 vsprintf(buffer, format, args);
353 va_end(args);
354 astnode_set_infix(buffer,node);
355}
356
357static inline void
358astnode_sprintf_prefix(ASTNode* node, const char* format, ...)
359{
360 static char buffer[10000];
361 va_list args;
362 va_start(args,format);
363 vsprintf(buffer, format, args);
364 va_end(args);
365 astnode_set_prefix(buffer,node);
366}
367static inline void
368astnode_print(const ASTNode* node)
369{
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);
380}
381
382
383static inline void combine_buffers_recursive(const ASTNode* node, char* res){
384 if(node->buffer)
385 strcat(res,node->buffer);
386 if(node->lhs)
387 combine_buffers_recursive(node->lhs, res);
388 if(node->rhs)
389 combine_buffers_recursive(node->rhs, res);
390}
391static inline void combine_buffers(const ASTNode* node, char* res){
392 res[0] = '\0';
393 combine_buffers_recursive(node,res);
394}
395static inline const char* combine_buffers_new(const ASTNode* node)
396{
397 static char res[10000];
398 combine_buffers(node,res);
399 return res;
400}
401static inline void combine_all_recursive(const ASTNode* node, char* res){
402 if(node->prefix)
403 strcat(res,node->prefix);
404 if(node->lhs)
405 combine_all_recursive(node->lhs, res);
406 if(node->buffer)
407 strcat(res,node->buffer);
408 if(node->infix)
409 strcat(res,node->infix);
410 if(node->rhs)
411 combine_all_recursive(node->rhs, res);
412 if(node->postfix)
413 strcat(res,node->postfix);
414}
415static inline void
416strip_whitespace(char *str) {
417 char *dest = str; // Destination pointer to overwrite the original string
418 char *src = str; // Source pointer to traverse the original string
419
420 // Skip leading whitespace
421 while (isspace((unsigned char)(*src))) {
422 src++;
423 }
424
425 // Copy non-whitespace characters to the destination
426 while (*src) {
427 if (!isspace((unsigned char)(*src))) {
428 *dest++ = *src;
429 }
430 src++;
431 }
432
433 // Null-terminate the destination string
434 *dest = '\0';
435}
436static inline char*
437strip_whitespace_new(const char* src) {
438 char* res = strdup(src);
439 strip_whitespace(res);
440 return res;
441}
442
443static inline void combine_all(const ASTNode* node, char* res){
444 res[0] = '\0';
445 combine_all_recursive(node,res);
446 strip_whitespace(res);
447}
448static inline const char*combine_all_new(const ASTNode* node){
449 static char res[10000];
450 res[0] = '\0';
451 combine_all_recursive(node,res);
452 strip_whitespace(res);
453 return res;
454}
455static inline const char* combine_all_new_with_whitespace(const ASTNode* node){
456 static char res[10000];
457 res[0] = '\0';
458 combine_all_recursive(node,res);
459 return res;
460}
461
462static inline ASTNode*
463get_node_by_token(const int token, const ASTNode* node)
464{
465 assert(node);
466
467 if (node->token == token)
468 return (ASTNode*) node;
469 if(node->lhs)
470 {
471 ASTNode* lhs_res = get_node_by_token(token,node->lhs);
472 if(lhs_res) return lhs_res;
473 }
474 if(node->rhs)
475 {
476 ASTNode* rhs_res = get_node_by_token(token,node->rhs);
477 if(rhs_res) return rhs_res;
478 }
479 return NULL;
480}
481static inline ASTNode*
482get_node_by_buffer_exclude_node(const char* test, const ASTNode* node, const ASTNode* exclude)
483{
484 assert(node);
485 ASTNode* res = NULL;
486 if(node->id == exclude->id) return res;
487 if (node->buffer && test == node->buffer)
488 res = (ASTNode*) node;
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);
493 return res;
494}
495
496static inline ASTNode*
497get_node_by_buffer(const char* test, const ASTNode* node)
498{
499 assert(node);
500
501 ASTNode* res = NULL;
502 if (node->buffer && test == node->buffer)
503 res = (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);
508 return res;
509}
510static inline ASTNode*
511get_node_by_buffer_and_type(const char* test, const NodeType type, const ASTNode* node)
512{
513 assert(node);
514
515 ASTNode* res = NULL;
516 if (node->buffer && node->type & type && test == node->buffer)
517 res = (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);
522 return res;
523}
524
525static inline ASTNode*
526get_node_by_buffer_and_token(const char* test, const int token, const ASTNode* node)
527{
528 assert(node);
529
530 ASTNode* res = NULL;
531 if (node->buffer && test == node->buffer && node->token == token)
532 res = (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);
537 return res;
538}
539static inline ASTNode*
540get_node_by_id(const int id, const ASTNode* node)
541{
542 assert(node);
543
544 ASTNode* res = NULL;
545 if (node->id == id)
546 res = (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);
551 return res;
552}
553static inline const ASTNode*
554get_parent_node(const NodeType type, const ASTNode* node)
555{
556 if(!node->parent)
557 return NULL;
558 if (node->parent->type & type)
559 return node->parent;
560 return get_parent_node(type, node->parent);
561}
562static inline const ASTNode*
563get_parent_node_inclusive(const NodeType type, const ASTNode* node)
564{
565 if(node->type & type) return node;
566 if(!node->parent)
567 return NULL;
568 if (node->parent->type & type)
569 return node->parent;
570 return get_parent_node(type, node->parent);
571}
572
573static inline const ASTNode*
574get_parent_node_by_token(const int token, const ASTNode* node)
575{
576 if(!node->parent)
577 return NULL;
578 if (node->parent->token == token)
579 return node->parent;
580 return get_parent_node_by_token(token, node->parent);
581}
582
583static inline bool
584is_left_child(const NodeType type, const ASTNode* node)
585{
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;
589}
590
591static inline bool
592is_left_child_token(const int token, const ASTNode* node)
593{
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;
597}
598
599static inline bool
600is_right_child(const NodeType type, const ASTNode* node)
601{
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;
605}
606static inline const ASTNode*
607get_parent_node_exclusive(const NodeType type, const ASTNode* node)
608{
609 if(!node->parent)
610 return NULL;
611 if (node->parent->type == type)
612 return node->parent;
613 return get_parent_node(type, node->parent);
614}
620
621static inline char* itoa(const int x)
622{
623 char* res = NULL;
624 my_asprintf(&res,"%d",x);
625 return res;
626}
627
628static inline void
629set_buffers_empty(ASTNode* node)
630{
631 node->buffer = NULL;
632 node->prefix = NULL;
633 node->infix = NULL;
634 node->postfix = NULL;
635 if(node->lhs)
636 set_buffers_empty(node->lhs);
637 if(node->rhs)
638 set_buffers_empty(node->rhs);
639}
640static inline void
641add_node_type(const NodeType type, ASTNode* node, const char* str_to_check)
642{
643 if(node->lhs)
644 add_node_type(type,node->lhs,str_to_check);
645 if(node->rhs)
646 add_node_type(type,node->rhs,str_to_check);
647 if(!str_to_check || (node->buffer && !strcmp(node->buffer,str_to_check)))
648 node->type |= type;
649}
650static inline void
651add_no_auto(ASTNode* node, const char* str_to_check)
652{
653 if(!node) return;
654 if(node->lhs)
655 add_no_auto(node->lhs,str_to_check);
656 if(node->rhs)
657 add_no_auto(node->rhs,str_to_check);
658 if(!str_to_check || (node->buffer && !strcmp(node->buffer,str_to_check)))
659 node->no_auto = true;
660}
661static inline void strprepend(char* dst, const char* src)
662{
663 memmove(dst + strlen(src), dst, strlen(dst)+ 1); // Move existing data including null terminator
664 memcpy(dst, src, strlen(src)); // Copy src to the beginning of dst
665}
666static inline void
667strcatprintf(char* dst, const char* format, ...)
668{
669 static char buffer[10000];
670 va_list args;
671 va_start(args,format);
672 vsprintf(buffer, format, args);
673 va_end(args);
674 strcat(dst,buffer);
675
676}
677
678static inline void
679fprintf_filename(const char* filename, const char* format, ...)
680{
681 FILE* fp = fopen(filename,"a");
682 va_list args;
683 va_start(args,format);
684 vfprintf(fp, format, args);
685 va_end(args);
686 fclose(fp);
687}
688static inline void
689fprintf_filename_w(const char* filename, const char* format, ...)
690{
691 FILE* fp = fopen(filename,"w");
692 va_list args;
693 va_start(args,format);
694 vfprintf(fp, format, args);
695 va_end(args);
696 fclose(fp);
697}
698
699static inline char*
700sprintf_new(const char* format, ...)
701{
702 static char buffer[10000];
703 va_list args;
704 va_start(args,format);
705 vsprintf(buffer,format,args);
706 va_end(args);
707 return strdup(buffer);
708}
709static inline char* readFile(const char *filename) {
710 FILE *file = fopen(filename, "rb"); // Open the file in binary mode
711
712 if (file == NULL) {
713 perror("Error opening file");
714 return NULL;
715 }
716
717 // Determine the size of the file
718 fseek(file, 0, SEEK_END);
719 long file_size = ftell(file);
720 rewind(file);
721
722 // Allocate memory for the string
723 char *buffer = (char *)malloc(file_size + 1); // Plus one for the null terminator
724 if (buffer == NULL) {
725 perror("Memory allocation failed");
726 fclose(file);
727 return NULL;
728 }
729
730 // Read the entire contents of the file into the allocated memory
731 size_t bytes_read = fread(buffer, 1, file_size, file);
732 if ((long) bytes_read != file_size) {
733 perror("Error reading file");
734 free(buffer);
735 fclose(file);
736 return NULL;
737 }
738
739 // Null-terminate the string
740 buffer[file_size] = '\0';
741
742 // Close the file
743 fclose(file);
744
745 return buffer;
746}
747
748static inline void
749file_append(const char* filename, const char* str_to_append)
750{
751 FILE* fp = fopen(filename,"a");
752 fprintf(fp,"%s",str_to_append);
753 fclose(fp);
754}
755
756
757static inline bool
758is_number(const char* str)
759{
760 const size_t n = strlen(str);
761 bool res = true;
762 for(size_t i = 0; i < n; ++i)
763 res &= (isdigit(str[i]) > 0);
764 return res;
765}
766static inline bool
767is_number_expression(const char* str)
768{
769 const size_t n = strlen(str);
770 bool res = true;
771 for(size_t i = 0; i < n; ++i)
772 res &= (
773 isdigit(str[i]) > 0
774 || str[i] == ')'
775 || str[i] == '('
776 || str[i] == '+'
777 || str[i] == '-'
778 || str[i] == '/'
779 || str[i] == '*'
780 );
781 return res;
782}
783static inline bool
784is_real(const char* str)
785{
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);
792 free(tmp);
793 return res;
794}
795
796static inline int
797count_num_of_nodes_in_list(const ASTNode* list_head)
798{
799 if(list_head == NULL) return 0;
800 int res = 0;
801 while(list_head->rhs)
802 {
803 list_head = list_head->lhs;
804 ++res;
805 }
806 res += (list_head->lhs != NULL);
807 return res;
808}
809static inline ASTNode*
810get_node_in_list(const ASTNode* list_head, int index)
811{
812 bool last_elem = count_num_of_nodes_in_list(list_head) == index + 1;
813 while(--index)
814 {
815 list_head = list_head->lhs;
816 }
817 return last_elem ? list_head->lhs : list_head->rhs;
818}
819static __attribute__((unused)) void
820change_qualifier(const ASTNode* node, const char* old_qualifier, const char* new_qualifier)
821{
822 if(node->lhs)
823 change_qualifier(node->lhs,old_qualifier,new_qualifier);
824 if(node->rhs)
825 change_qualifier(node->rhs,old_qualifier,new_qualifier);
826 if(node->type & NODE_TQUAL && node->lhs->buffer == old_qualifier)
827 {
828 node->lhs->buffer = new_qualifier;
829 }
830}
831static inline bool has_qualifier(const ASTNode* node, const char* qualifier)
832{
833 bool res = false;
834 if(node->lhs)
835 res |= has_qualifier(node->lhs,qualifier);
836 if(node->rhs)
837 res |= has_qualifier(node->rhs,qualifier);
838 if(node->type & NODE_TQUAL)
839 res |= node->lhs->buffer == qualifier;
840 return res;
841}
842
843static void
844format_source(const char* file_in, const char* file_out)
845{
846 FILE* in = fopen(file_in, "r");
847 assert(in);
848
849 FILE* out = fopen(file_out, "w");
850 assert(out);
851
852 while (!feof(in)) {
853 const int c = fgetc(in);
854 if (c == EOF)
855 break;
856
857 fprintf(out, "%c", c);
858 if (c == ';' || c == '{')
859 fprintf(out, "\n");
860 }
861
862 fclose(in);
863 fclose(out);
864}
865
866static bool
867file_exists(const char* filename)
868{
869 struct stat buffer;
870 return (stat (filename, &buffer) == 0);
871}
872
873typedef struct node_vec
874{
875 const ASTNode** data;
876 size_t size;
878
879} node_vec;
880static inline void
881free_node_vec(node_vec* vec)
882{
883 free(vec->data);
884 vec -> size = 0;
885 vec -> capacity = 0;
886 vec -> data = NULL;
887}
888static inline int
889push_node(node_vec* dst, const ASTNode* src)
890{
891 if(dst->capacity == 0)
892 {
893 dst->capacity = 4;
894 dst->data = malloc(sizeof(ASTNode*)*dst->capacity);
895 }
896 dst->data[dst->size] = src;
897 ++(dst->size);
898 if(dst->size == (size_t)dst->capacity)
899 {
900 dst->capacity = dst->capacity*2;
901 const ASTNode** tmp = malloc(sizeof(ASTNode*)*dst->capacity);
902 for(size_t i = 0; i < dst->size; ++i)
903 tmp[i] = dst->data[i];
904 free(dst->data);
905 dst->data = tmp;
906 }
907 return dst->size-1;
908}
909
910static inline ASTNode*
911build_list_node(const node_vec nodes, const char* separator)
912{
913 if(nodes.size == 0) return NULL;
914 ASTNode* list_head = astnode_create(NODE_UNKNOWN, astnode_dup(nodes.data[0],NULL),NULL);
915 for(size_t i = 1; i < nodes.size; ++i)
916 {
917 list_head = astnode_create(NODE_UNKNOWN,list_head, astnode_dup(nodes.data[i],NULL));
918 list_head->buffer = intern(separator);
919 }
920 return list_head;
921}
922
923
924static inline const ASTNode*
925get_node(const NodeType type, const ASTNode* node)
926{
927 if(!node)
928 {
929 fatal("WRONG; passed NULL to get_node\n");
930 }
931
932 if (node->type & type)
933 return node;
934 if(node->lhs)
935 {
936 const ASTNode* lhs_res = get_node(type,node->lhs);
937 if(lhs_res) return lhs_res;
938 }
939 if(node->rhs)
940 {
941 const ASTNode* rhs_res = get_node(type,node->rhs);
942 if(rhs_res) return rhs_res;
943 }
944 return NULL;
945}
946static inline void
947astnode_change_infixes(ASTNode* node, const char* old_infix, const char* new_infix)
948{
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);
953}
954
955
956static inline node_vec
957get_nodes_in_list(const ASTNode* head)
958{
960 if(head == NULL) return res;
961 const int num_of_nodes = count_num_of_nodes_in_list(head);
962 int counter = num_of_nodes;
963 while(--counter)
964 head = head -> lhs;
965 push_node(&res,head->lhs);
966 counter = num_of_nodes;
967 while(--counter)
968 {
969 head = head->parent;
970 push_node(&res,head->rhs);
971 }
972 return res;
973}
974
975
976static inline void
977get_array_access_nodes(const ASTNode* node, node_vec* dst)
978{
979 if(node->lhs)
980 get_array_access_nodes(node->lhs,dst);
981 if(node->rhs)
982 get_array_access_nodes(node->rhs,dst);
983 if(node->type == NODE_ARRAY_ACCESS)
984 push_node(dst,node->rhs);
985}
986static inline void
987replace_node(ASTNode* original, ASTNode* replacement)
988{
989 if(replacement == NULL)
990 {
991 fprintf(stderr,"Tried to replace with a NULL node!");
992 exit(EXIT_FAILURE);
993 }
994 if(original->parent->lhs && original->parent->lhs->id == original->id)
995 original->parent->lhs = replacement;
996 else
997 original->parent->rhs = replacement;
998 replacement->parent = original->parent;
999}
1000static inline bool
1001node_vec_contains(const node_vec vec, const char* str)
1002{
1003 for(size_t i = 0; i < vec.size; ++i)
1004 if(!strcmp(combine_all_new(vec.data[i]), str)) return true;
1005 return false;
1006}
1007
1008static inline void
1009copy_file(const char* src, const char* dst)
1010{
1011 char cmd[10000];
1012 sprintf(cmd,"cp %s %s",src,dst);
1013 if(system(cmd) != 0) fatal("Was not able to copy a file: %s!\n",cmd);
1014}
static const char const char * new_qualifier
Definition ast.h:821
#define FATAL_ERROR_MESSAGE
Definition ast.h:49
int id_counter
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
Definition ast.h:146
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
Definition ast.h:616
string_vec const_ints
Definition ast.h:617
string_vec const_int_values
Definition ast.h:618
Definition ast.h:166
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
Definition ast.h:874
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
va_start(ap, fmt)
va_end(ap)