( (ast_node*)self )->node.destroy = (ast_node_delete*)destroyfn
/* It must not be possible to get here. */
-static void _ast_node_destroy(ast_node *self)
+static GMQCC_NORETURN void _ast_node_destroy(ast_node *self)
{
fprintf(stderr, "ast node missing destroy()\n");
abort();
mem_d(self);
}
+ast_call* ast_call_new(lex_ctx ctx,
+ ast_expression *funcexpr)
+{
+ ast_instantiate(ast_call, ctx, ast_call_delete);
+ ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
+
+ MEM_VECTOR_INIT(self, params);
+
+ return self;
+}
+
+void ast_call_delete(ast_call *self)
+{
+ size_t i;
+ for (i = 0; i < self->params_count; ++i)
+ ast_unref(self->params[i]);
+ MEM_VECTOR_CLEAR(self, params);
+
+ if (self->func)
+ ast_unref(self->func);
+
+ ast_expression_delete((ast_expression*)self);
+ mem_d(self);
+}
+
ast_store* ast_store_new(lex_ctx ctx, int op,
ast_value *dest, ast_expression *source)
{
ir_value *v = NULL;
if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
{
- ir_function *func = ir_builder_create_function(ir, self->name);
+ ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
if (!func)
return false;
return false;
} else
ontrue = NULL;
-
+
/* on-false path */
if (self->on_false) {
/* create on-false block */
/* Merge block were they all merge in to */
merge = ir_function_create_block(func->ir_func, ast_function_label(func, "endif"));
if (!merge)
- return NULL;
+ return false;
/* add jumps ot the merge block */
if (ontrue && !ir_block_create_jump(ontrue, merge))
return false;
/* In the following, contraty to ast_ifthen, we assume both paths exist. */
-
+
/* generate the condition */
func->curblock = cond;
cgen = self->cond->expression.codegen;
if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
return false;
}
-
+
/* create on-false block */
onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_F"));
if (!onfalse)
/* create merge block */
merge = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_out"));
if (!merge)
- return NULL;
+ return false;
/* jump to merge block */
if (!ir_block_create_jump(ontrue, merge))
return false;
{
ast_expression_codegen *cgen;
- ir_value *dummy;
- ir_value *precond;
- ir_value *postcond;
+ ir_value *dummy = NULL;
+ ir_value *precond = NULL;
+ ir_value *postcond = NULL;
/* Since we insert some jumps "late" so we have blocks
* ordered "nicely", we need to keep track of the actual end-blocks
* of expressions to add the jumps to.
*/
- ir_block *bbody, *end_bbody;
- ir_block *bprecond, *end_bprecond;
- ir_block *bpostcond, *end_bpostcond;
- ir_block *bincrement, *end_bincrement;
- ir_block *bout, *bin;
+ ir_block *bbody = NULL, *end_bbody = NULL;
+ ir_block *bprecond = NULL, *end_bprecond = NULL;
+ ir_block *bpostcond = NULL, *end_bpostcond = NULL;
+ ir_block *bincrement = NULL, *end_bincrement = NULL;
+ ir_block *bout = NULL, *bin = NULL;
+
+ /* let's at least move the outgoing block to the end */
+ size_t bout_id;
/* 'break' and 'continue' need to be able to find the right blocks */
- ir_block *bcontinue = NULL;
- ir_block *bbreak = NULL;
+ ir_block *bcontinue = NULL;
+ ir_block *bbreak = NULL;
- ir_block *old_bcontinue;
- ir_block *old_bbreak;
+ ir_block *old_bcontinue = NULL;
+ ir_block *old_bbreak = NULL;
+
+ ir_block *tmpblock = NULL;
(void)lvalue;
(void)out;
bpostcond = end_bpostcond = NULL;
}
+ bout_id = func->ir_func->blocks_count;
bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_loop"));
if (!bout)
return false;
end_bincrement = func->curblock;
}
+ /* In any case now, we continue from the outgoing block */
+ func->curblock = bout;
+
/* Now all blocks are in place */
/* From 'bin' we jump to whatever comes first */
- if (bprecond && !ir_block_create_jump(bin, bprecond))
- return false;
- else if (bbody && !ir_block_create_jump(bin, bbody))
- return false;
- else if (bpostcond && !ir_block_create_jump(bin, bpostcond))
- return false;
- else if ( !ir_block_create_jump(bin, bout))
+ if (bprecond) tmpblock = bprecond;
+ else if (bbody) tmpblock = bbody;
+ else if (bpostcond) tmpblock = bpostcond;
+ else tmpblock = bout;
+ if (!ir_block_create_jump(bin, tmpblock))
return false;
/* From precond */
/* from body */
if (bbody)
{
- if (bincrement && !ir_block_create_jump(end_bbody, bincrement))
- return false;
- else if (bpostcond && !ir_block_create_jump(end_bbody, bpostcond))
- return false;
- else if (bprecond && !ir_block_create_jump(end_bbody, bprecond))
- return false;
- else if (!ir_block_create_jump(end_bbody, bout))
+ if (bincrement) tmpblock = bincrement;
+ else if (bpostcond) tmpblock = bpostcond;
+ else if (bprecond) tmpblock = bprecond;
+ else tmpblock = bout;
+ if (!ir_block_create_jump(end_bbody, tmpblock))
return false;
}
/* from increment */
if (bincrement)
{
- if (bpostcond && !ir_block_create_jump(end_bincrement, bpostcond))
- return false;
- else if (bprecond && !ir_block_create_jump(end_bincrement, bprecond))
- return false;
- else if (bbody && !ir_block_create_jump(end_bincrement, bbody))
- return false;
- else if (!ir_block_create_jump(end_bincrement, bout))
+ if (bpostcond) tmpblock = bpostcond;
+ else if (bprecond) tmpblock = bprecond;
+ else if (bbody) tmpblock = bbody;
+ else tmpblock = bout;
+ if (!ir_block_create_jump(end_bincrement, tmpblock))
return false;
}
return false;
}
+ /* Move 'bout' to the end */
+ if (!ir_function_blocks_remove(func->ir_func, bout_id) ||
+ !ir_function_blocks_add(func->ir_func, bout))
+ {
+ ir_block_delete(bout);
+ return false;
+ }
+
+ return true;
+}
+
+bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
+{
+ /* TODO: call ir codegen */
+ ast_expression_codegen *cgen;
+ ir_value_vector params;
+ ir_instr *callinstr;
+ size_t i;
+
+ ir_value *funval = NULL;
+
+ /* return values are never rvalues */
+ (void)lvalue;
+
+ cgen = self->func->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
+ return false;
+ if (!funval)
+ return false;
+
+ MEM_VECTOR_INIT(¶ms, v);
+
+ /* parameters */
+ for (i = 0; i < self->params_count; ++i)
+ {
+ ir_value *param;
+ ast_expression *expr = self->params[i];
+
+ cgen = expr->expression.codegen;
+ if (!(*cgen)(expr, func, false, ¶m))
+ goto error;
+ if (!param)
+ goto error;
+ if (!ir_value_vector_v_add(¶ms, param))
+ goto error;
+ }
+
+ callinstr = ir_block_create_call(func->curblock, ast_function_label(func, "call"), funval);
+ if (!callinstr)
+ goto error;
+
+ for (i = 0; i < params.v_count; ++i) {
+ if (!ir_call_param(callinstr, params.v[i]))
+ goto error;
+ }
+
+ *out = ir_call_value(callinstr);
+
return true;
+error:
+ MEM_VECTOR_CLEAR(¶ms, v);
+ return false;
}