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