]> git.xonotic.org Git - xonotic/gmqcc.git/blob - ast.c
ast_entfield node
[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_instantiate(T, ctx, destroyfn)                          \
31     T* self = (T*)mem_a(sizeof(T));                                 \
32     ast_node_init((ast_node*)self, ctx);                            \
33     ( (ast_node*)self )->node.destroy = (ast_node_delete*)destroyfn
34
35 /* It must not be possible to get here. */
36 static void _ast_node_destroy(ast_node *self)
37 {
38     fprintf(stderr, "ast node missing destroy()\n");
39     abort();
40 }
41
42 /* Initialize main ast node aprts */
43 static void ast_node_init(ast_node *self, lex_ctx ctx)
44 {
45     self->node.context = ctx;
46     self->node.destroy = &_ast_node_destroy;
47     self->node.keep    = false;
48 }
49
50 /* General expression initialization */
51 static void ast_expression_init(ast_expression *self,
52                                 ast_expression_codegen *codegen)
53 {
54     self->expression.codegen = codegen;
55 }
56
57 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
58 {
59     ast_instantiate(ast_value, ctx, ast_value_delete);
60     ast_expression_init((ast_expression*)self,
61                         (ast_expression_codegen*)&ast_value_codegen);
62     self->expression.node.keep = true; /* keep */
63
64     self->name = name ? util_strdup(name) : NULL;
65     self->vtype = t;
66     self->next = NULL;
67     MEM_VECTOR_INIT(self, params);
68     self->isconst = false;
69     memset(&self->constval, 0, sizeof(self->constval));
70
71     self->ir_v    = NULL;
72
73     return self;
74 }
75 MEM_VEC_FUNCTIONS(ast_value, ast_value*, params)
76
77 void ast_value_delete(ast_value* self)
78 {
79     size_t i;
80     if (self->name)
81         mem_d((void*)self->name);
82     for (i = 0; i < self->params_count; ++i)
83         ast_value_delete(self->params[i]); /* delete, the ast_function is expected to die first */
84     MEM_VECTOR_CLEAR(self, params);
85     if (self->next) /* delete, not unref, types are always copied */
86         ast_delete(self->next);
87     if (self->isconst) {
88         switch (self->vtype)
89         {
90         case TYPE_STRING:
91             mem_d((void*)self->constval.vstring);
92             break;
93         case TYPE_FUNCTION:
94             /* unlink us from the function node */
95             self->constval.vfunc->vtype = NULL;
96             break;
97         /* NOTE: delete function? currently collected in
98          * the parser structure
99          */
100         default:
101             break;
102         }
103     }
104     mem_d(self);
105 }
106
107 bool ast_value_set_name(ast_value *self, const char *name)
108 {
109     if (self->name)
110         mem_d((void*)self->name);
111     self->name = util_strdup(name);
112     return !!self->name;
113 }
114
115 ast_binary* ast_binary_new(lex_ctx ctx, int op,
116                            ast_expression* left, ast_expression* right)
117 {
118     ast_instantiate(ast_binary, ctx, ast_binary_delete);
119     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
120
121     self->op = op;
122     self->left = left;
123     self->right = right;
124
125     return self;
126 }
127
128 void ast_binary_delete(ast_binary *self)
129 {
130     ast_unref(self->left);
131     ast_unref(self->right);
132     mem_d(self);
133 }
134
135 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
136 {
137     ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
138     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
139
140     self->entity = entity;
141     self->field  = field;
142
143     return self;
144 }
145
146 void ast_entfield_delete(ast_entfield *self)
147 {
148     ast_unref(self->entity);
149     ast_unref(self->field);
150     mem_d(self);
151 }
152
153 ast_store* ast_store_new(lex_ctx ctx, int op,
154                          ast_value *dest, ast_expression *source)
155 {
156     ast_instantiate(ast_store, ctx, ast_store_delete);
157     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
158
159     self->op = op;
160     self->dest = dest;
161     self->source = source;
162
163     return self;
164 }
165
166 void ast_store_delete(ast_store *self)
167 {
168     ast_unref(self->dest);
169     ast_unref(self->source);
170     mem_d(self);
171 }
172
173 ast_block* ast_block_new(lex_ctx ctx)
174 {
175     ast_instantiate(ast_block, ctx, ast_block_delete);
176     ast_expression_init((ast_expression*)self,
177                         (ast_expression_codegen*)&ast_block_codegen);
178
179     MEM_VECTOR_INIT(self, locals);
180     MEM_VECTOR_INIT(self, exprs);
181
182     return self;
183 }
184 MEM_VEC_FUNCTIONS(ast_block, ast_value*, locals)
185 MEM_VEC_FUNCTIONS(ast_block, ast_expression*, exprs)
186
187 void ast_block_delete(ast_block *self)
188 {
189     size_t i;
190     for (i = 0; i < self->exprs_count; ++i)
191         ast_unref(self->exprs[i]);
192     MEM_VECTOR_CLEAR(self, exprs);
193     for (i = 0; i < self->locals_count; ++i)
194         ast_delete(self->locals[i]);
195     MEM_VECTOR_CLEAR(self, locals);
196     mem_d(self);
197 }
198
199 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
200 {
201     ast_instantiate(ast_function, ctx, ast_function_delete);
202
203     if (!vtype ||
204         vtype->isconst ||
205         vtype->vtype != TYPE_FUNCTION)
206     {
207         mem_d(self);
208         return NULL;
209     }
210
211     self->vtype = vtype;
212     self->name = name ? util_strdup(name) : NULL;
213     MEM_VECTOR_INIT(self, blocks);
214
215     self->ir_func = NULL;
216
217     vtype->isconst = true;
218     vtype->constval.vfunc = self;
219
220     return self;
221 }
222
223 MEM_VEC_FUNCTIONS(ast_function, ast_block*, blocks)
224
225 void ast_function_delete(ast_function *self)
226 {
227     size_t i;
228     if (self->name)
229         mem_d((void*)self->name);
230     if (self->vtype) {
231         /* ast_value_delete(self->vtype); */
232         self->vtype->isconst = false;
233         self->vtype->constval.vfunc = NULL;
234         /* We use unref - if it was stored in a global table it is supposed
235          * to be deleted from *there*
236          */
237         ast_unref(self->vtype);
238     }
239     for (i = 0; i < self->blocks_count; ++i)
240         ast_delete(self->blocks[i]);
241     MEM_VECTOR_CLEAR(self, blocks);
242     mem_d(self);
243 }
244
245 /*********************************************************************/
246 /* AST codegen aprt
247  */
248
249 bool ast_value_codegen(ast_value *self, ast_function *func, ir_value **out)
250 {
251     /* NOTE: This is the codegen for a variable used in an expression.
252      * It is not the codegen to generate the value. For this purpose,
253      * ast_local_codegen and ast_global_codegen are to be used before this
254      * is executed. ast_function_codegen should take care of its locals,
255      * and the ast-user should take care of ast_global_codegen to be used
256      * on all the globals.
257      */
258     return false;
259 }
260
261 bool ast_global_codegen(ast_value *self, ir_builder *ir)
262 {
263     ir_value *v = NULL;
264     if (self->isconst && self->vtype == TYPE_FUNCTION)
265     {
266         ir_function *func = ir_builder_create_function(ir, self->name);
267         if (!func)
268             return false;
269
270         self->constval.vfunc->ir_func = func;
271         /* The function is filled later on ast_function_codegen... */
272         return true;
273     }
274
275     v = ir_builder_create_global(ir, self->name, self->vtype);
276     if (!v)
277         return false;
278
279     if (self->isconst) {
280         switch (self->vtype)
281         {
282             case TYPE_FLOAT:
283                 if (!ir_value_set_float(v, self->constval.vfloat))
284                     goto error;
285                 break;
286             case TYPE_VECTOR:
287                 if (!ir_value_set_vector(v, self->constval.vvec))
288                     goto error;
289                 break;
290             case TYPE_STRING:
291                 if (!ir_value_set_string(v, self->constval.vstring))
292                     goto error;
293                 break;
294             case TYPE_FUNCTION:
295                 /* Cannot generate an IR value for a function,
296                  * need a pointer pointing to a function rather.
297                  */
298                 goto error;
299             default:
300                 printf("TODO: global constant type %i\n", self->vtype);
301                 break;
302         }
303     }
304
305     /* link us to the ir_value */
306     self->ir_v = v;
307     return true;
308
309 error: /* clean up */
310     ir_value_delete(v);
311     return false;
312 }
313
314 bool ast_function_codegen(ast_function *self, ir_builder *ir)
315 {
316     if (!self->ir_func) {
317         printf("ast_function's related ast_value was not generated yet\n");
318         return false;
319     }
320     return false;
321 }
322
323 bool ast_block_codegen(ast_block *self, ast_function *func, ir_value **out)
324 {
325     return false;
326 }
327
328 bool ast_store_codegen(ast_store *self, ast_function *func, ir_value **out)
329 {
330     return false;
331 }
332
333 bool ast_binary_codegen(ast_binary *self, ast_function *func, ir_value **out)
334 {
335     return false;
336 }
337
338 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, ir_value **out)
339 {
340     return false;
341 }