]> git.xonotic.org Git - xonotic/gmqcc.git/blob - ast.c
Adding debug dump functions
[xonotic/gmqcc.git] / ast.c
1 /*
2  * Copyright (C) 2012 
3  *     Wolfgang Bumiller
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "gmqcc.h"
28 #include "ast.h"
29
30 #define ast_setfunc(me, fn, what) ( *(void**)&((me)->fn) = what )
31
32 #define ast_instantiate(T, ctx, destroyfn)                    \
33     T *self = (T*)mem_a(sizeof(T));                           \
34     ast_node_init((ast_node*)self, ctx);                      \
35     ast_setfunc(&((ast_node*)self)->node, destroy, destroyfn)
36
37 /* It must not be possible to get here. */
38 static void _ast_node_destroy(ast_node *self)
39 {
40     fprintf(stderr, "ast node missing destroy()\n");
41     abort();
42 }
43
44 /* Initialize main ast node aprts */
45 static void ast_node_init(ast_node *self, lex_ctx ctx)
46 {
47     self->node.context = ctx;
48     self->node.destroy = &_ast_node_destroy;
49     self->node.keep    = false;
50 }
51
52 /* General expression initialization */
53 static void ast_expression_init(ast_expression *self,
54                                 ast_expression_codegen *codegen)
55 {
56     ast_setfunc(&self->expression, codegen, codegen);
57 }
58
59 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t, bool keep)
60 {
61     ast_instantiate(ast_value, ctx, ast_value_delete);
62     ast_expression_init((ast_expression*)self,
63                         (ast_expression_codegen*)&ast_value_codegen);
64     self->expression.node.keep = keep;
65
66     self->name = name ? util_strdup(name) : NULL;
67     self->vtype = t;
68     self->next = NULL;
69     MEM_VECTOR_INIT(self, params);
70     self->isconst = false;
71     memset(&self->constval, 0, sizeof(self->constval));
72
73     self->ir_v = NULL;
74
75     return self;
76 }
77 MEM_VEC_FUNCTIONS(ast_value, ast_value*, params)
78
79 void ast_value_delete(ast_value* self)
80 {
81     size_t i;
82     if (self->name)
83         mem_d((void*)self->name);
84     for (i = 0; i < self->params_count; ++i)
85         ast_unref(self->params[i]);
86     MEM_VECTOR_CLEAR(self, params);
87     if (self->next) /* delete, not unref, types are always copied */
88         ast_delete(self->next);
89     if (self->isconst) {
90         switch (self->vtype)
91         {
92         case TYPE_STRING:
93             mem_d((void*)self->constval.vstring);
94             break;
95         /* NOTE: delete function? currently collected in
96          * the parser structure
97          */
98         default:
99             break;
100         }
101     }
102     mem_d(self);
103 }
104
105 bool ast_value_set_name(ast_value *self, const char *name)
106 {
107     if (self->name)
108         mem_d((void*)self->name);
109     self->name = util_strdup(name);
110     return !!self->name;
111 }
112
113 ast_binary* ast_binary_new(lex_ctx ctx, int op,
114                            ast_value* left, ast_value* right)
115 {
116     ast_instantiate(ast_binary, ctx, ast_binary_delete);
117     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
118
119     self->op = op;
120     self->left = left;
121     self->right = right;
122
123     return self;
124 }
125
126 void ast_binary_delete(ast_binary *self)
127 {
128     ast_unref(self->left);
129     ast_unref(self->right);
130     mem_d(self);
131 }
132
133 ast_store* ast_store_new(lex_ctx ctx, int op,
134                          ast_value *dest, ast_value *source)
135 {
136     ast_instantiate(ast_store, ctx, ast_store_delete);
137     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
138
139     self->op = op;
140     self->dest = dest;
141     self->source = source;
142
143     return self;
144 }
145
146 void ast_store_delete(ast_store *self)
147 {
148     ast_unref(self->dest);
149     ast_unref(self->source);
150     mem_d(self);
151 }
152
153 ast_block* ast_block_new(lex_ctx ctx)
154 {
155     ast_instantiate(ast_block, ctx, ast_block_delete);
156     ast_expression_init((ast_expression*)self,
157                         (ast_expression_codegen*)&ast_block_codegen);
158
159     MEM_VECTOR_INIT(self, locals);
160     MEM_VECTOR_INIT(self, exprs);
161
162     return self;
163 }
164 MEM_VEC_FUNCTIONS(ast_block, ast_value*, locals)
165 MEM_VEC_FUNCTIONS(ast_block, ast_expression*, exprs)
166
167 void ast_block_delete(ast_block *self)
168 {
169     size_t i;
170     for (i = 0; i < self->locals_count; ++i)
171         ast_delete(self->locals[i]);
172     MEM_VECTOR_CLEAR(self, locals);
173     for (i = 0; i < self->exprs_count; ++i)
174         ast_unref(self->exprs[i]);
175     MEM_VECTOR_CLEAR(self, exprs);
176     mem_d(self);
177 }
178
179 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
180 {
181     ast_instantiate(ast_function, ctx, ast_function_delete);
182
183     self->vtype = vtype;
184     self->name = name ? util_strdup(name) : NULL;
185     MEM_VECTOR_INIT(self, blocks);
186
187     return self;
188 }
189
190 MEM_VEC_FUNCTIONS(ast_function, ast_block*, blocks)
191
192 void ast_function_delete(ast_function *self)
193 {
194     size_t i;
195     if (self->name)
196         mem_d((void*)self->name);
197     if (self->vtype)
198         ast_value_delete(self->vtype);
199     for (i = 0; i < self->blocks_count; ++i)
200         ast_delete(self->blocks[i]);
201     MEM_VECTOR_CLEAR(self, blocks);
202     mem_d(self);
203 }
204
205 /*********************************************************************/
206 /* AST codegen aprt
207  */
208
209 /* Some dummies so it compiles... */
210 bool ast_value_codegen(ast_value *self, ast_function *func, ir_value **out)
211 {
212     return false;
213 }
214
215 bool ast_block_codegen(ast_block *self, ast_function *func, ir_value **out)
216 {
217     return false;
218 }
219
220 bool ast_store_codegen(ast_store *self, ast_function *func, ir_value **out)
221 {
222     return false;
223 }
224
225 bool ast_binary_codegen(ast_binary *self, ast_function *func, ir_value **out)
226 {
227     return false;
228 }