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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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
30 #define ast_instantiate(T, ctx, destroyfn) \
31 T* self = (T*)mem_a(sizeof(T)); \
35 ast_node_init((ast_node*)self, ctx); \
36 ( (ast_node*)self )->node.destroy = (ast_node_delete*)destroyfn
38 /* It must not be possible to get here. */
39 static void _ast_node_destroy(ast_node *self)
41 fprintf(stderr, "ast node missing destroy()\n");
45 /* Initialize main ast node aprts */
46 static void ast_node_init(ast_node *self, lex_ctx ctx)
48 self->node.context = ctx;
49 self->node.destroy = &_ast_node_destroy;
50 self->node.keep = false;
53 /* General expression initialization */
54 static void ast_expression_init(ast_expression *self,
55 ast_expression_codegen *codegen)
57 self->expression.codegen = codegen;
60 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
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 */
67 self->name = name ? util_strdup(name) : NULL;
70 MEM_VECTOR_INIT(self, params);
71 self->isconst = false;
72 memset(&self->constval, 0, sizeof(self->constval));
78 MEM_VEC_FUNCTIONS(ast_value, ast_value*, params)
80 void ast_value_delete(ast_value* self)
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);
94 mem_d((void*)self->constval.vstring);
97 /* unlink us from the function node */
98 self->constval.vfunc->vtype = NULL;
100 /* NOTE: delete function? currently collected in
101 * the parser structure
110 bool ast_value_set_name(ast_value *self, const char *name)
113 mem_d((void*)self->name);
114 self->name = util_strdup(name);
118 ast_binary* ast_binary_new(lex_ctx ctx, int op,
119 ast_expression* left, ast_expression* right)
121 ast_instantiate(ast_binary, ctx, ast_binary_delete);
122 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
131 void ast_binary_delete(ast_binary *self)
133 ast_unref(self->left);
134 ast_unref(self->right);
138 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
140 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
141 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
143 self->entity = entity;
149 void ast_entfield_delete(ast_entfield *self)
151 ast_unref(self->entity);
152 ast_unref(self->field);
156 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
158 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
159 if (!ontrue && !onfalse) {
160 /* because it is invalid */
164 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
167 self->on_true = ontrue;
168 self->on_false = onfalse;
173 void ast_ifthen_delete(ast_ifthen *self)
175 ast_unref(self->cond);
176 ast_unref(self->on_true);
177 ast_unref(self->on_false);
181 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
183 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
184 /* This time NEITHER must be NULL */
185 if (!ontrue || !onfalse) {
189 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
192 self->on_true = ontrue;
193 self->on_false = onfalse;
194 self->phi_out = NULL;
199 void ast_ternary_delete(ast_ternary *self)
201 ast_unref(self->cond);
202 ast_unref(self->on_true);
203 ast_unref(self->on_false);
207 ast_store* ast_store_new(lex_ctx ctx, int op,
208 ast_value *dest, ast_expression *source)
210 ast_instantiate(ast_store, ctx, ast_store_delete);
211 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
215 self->source = source;
220 void ast_store_delete(ast_store *self)
222 ast_unref(self->dest);
223 ast_unref(self->source);
227 ast_block* ast_block_new(lex_ctx ctx)
229 ast_instantiate(ast_block, ctx, ast_block_delete);
230 ast_expression_init((ast_expression*)self,
231 (ast_expression_codegen*)&ast_block_codegen);
233 MEM_VECTOR_INIT(self, locals);
234 MEM_VECTOR_INIT(self, exprs);
238 MEM_VEC_FUNCTIONS(ast_block, ast_value*, locals)
239 MEM_VEC_FUNCTIONS(ast_block, ast_expression*, exprs)
241 void ast_block_delete(ast_block *self)
244 for (i = 0; i < self->exprs_count; ++i)
245 ast_unref(self->exprs[i]);
246 MEM_VECTOR_CLEAR(self, exprs);
247 for (i = 0; i < self->locals_count; ++i)
248 ast_delete(self->locals[i]);
249 MEM_VECTOR_CLEAR(self, locals);
253 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
255 ast_instantiate(ast_function, ctx, ast_function_delete);
259 vtype->vtype != TYPE_FUNCTION)
266 self->name = name ? util_strdup(name) : NULL;
267 MEM_VECTOR_INIT(self, blocks);
269 self->ir_func = NULL;
270 self->curblock = NULL;
272 vtype->isconst = true;
273 vtype->constval.vfunc = self;
278 MEM_VEC_FUNCTIONS(ast_function, ast_block*, blocks)
280 void ast_function_delete(ast_function *self)
284 mem_d((void*)self->name);
286 /* ast_value_delete(self->vtype); */
287 self->vtype->isconst = false;
288 self->vtype->constval.vfunc = NULL;
289 /* We use unref - if it was stored in a global table it is supposed
290 * to be deleted from *there*
292 ast_unref(self->vtype);
294 for (i = 0; i < self->blocks_count; ++i)
295 ast_delete(self->blocks[i]);
296 MEM_VECTOR_CLEAR(self, blocks);
300 /*********************************************************************/
302 * by convention you must never pass NULL to the 'ir_value **out'
303 * parameter. If you really don't care about the output, pass a dummy.
304 * But I can't imagine a pituation where the output is truly unnecessary.
307 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
309 /* NOTE: This is the codegen for a variable used in an expression.
310 * It is not the codegen to generate the value. For this purpose,
311 * ast_local_codegen and ast_global_codegen are to be used before this
312 * is executed. ast_function_codegen should take care of its locals,
313 * and the ast-user should take care of ast_global_codegen to be used
314 * on all the globals.
322 bool ast_global_codegen(ast_value *self, ir_builder *ir)
325 if (self->isconst && self->vtype == TYPE_FUNCTION)
327 ir_function *func = ir_builder_create_function(ir, self->name);
331 self->constval.vfunc->ir_func = func;
332 /* The function is filled later on ast_function_codegen... */
336 v = ir_builder_create_global(ir, self->name, self->vtype);
344 if (!ir_value_set_float(v, self->constval.vfloat))
348 if (!ir_value_set_vector(v, self->constval.vvec))
352 if (!ir_value_set_string(v, self->constval.vstring))
356 /* Cannot generate an IR value for a function,
357 * need a pointer pointing to a function rather.
361 printf("TODO: global constant type %i\n", self->vtype);
366 /* link us to the ir_value */
370 error: /* clean up */
375 bool ast_local_codegen(ast_value *self, ir_function *func)
378 if (self->isconst && self->vtype == TYPE_FUNCTION)
380 /* Do we allow local functions? I think not...
381 * this is NOT a function pointer atm.
386 v = ir_function_create_local(func, self->name, self->vtype);
390 /* A constant local... hmmm...
391 * I suppose the IR will have to deal with this
397 if (!ir_value_set_float(v, self->constval.vfloat))
401 if (!ir_value_set_vector(v, self->constval.vvec))
405 if (!ir_value_set_string(v, self->constval.vstring))
409 printf("TODO: global constant type %i\n", self->vtype);
414 /* link us to the ir_value */
418 error: /* clean up */
423 bool ast_function_codegen(ast_function *self, ir_builder *ir)
431 printf("ast_function's related ast_value was not generated yet\n");
435 self->curblock = ir_function_create_block(irf, "entry");
439 for (i = 0; i < self->blocks_count; ++i) {
440 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
441 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
447 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
452 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
454 /* NOTE: remember: destination codegen needs to have lvalue=true */
458 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
463 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
468 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
470 if (out) *out = NULL;
474 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
476 /* In theory it shouldn't be possible to pass through a node twice, but
477 * in case we add any kind of optimization pass for the AST itself, it
478 * may still happen, thus we remember a created ir_value and simply return one
479 * if it already exists.
482 *out = self->phi_out;