* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
-#include <stdio.h>
#include <stdlib.h>
#include <string.h>
ast_node_init((ast_node*)self, ctx, TYPE_##T); \
( (ast_node*)self )->destroy = (ast_node_delete*)destroyfn
+/*
+ * forward declarations, these need not be in ast.h for obvious
+ * static reasons.
+ */
+static bool ast_member_codegen(ast_member*, ast_function*, bool lvalue, ir_value**);
+static void ast_array_index_delete(ast_array_index*);
+static bool ast_array_index_codegen(ast_array_index*, ast_function*, bool lvalue, ir_value**);
+static void ast_store_delete(ast_store*);
+static bool ast_store_codegen(ast_store*, ast_function*, bool lvalue, ir_value**);
+static void ast_ifthen_delete(ast_ifthen*);
+static bool ast_ifthen_codegen(ast_ifthen*, ast_function*, bool lvalue, ir_value**);
+static void ast_ternary_delete(ast_ternary*);
+static bool ast_ternary_codegen(ast_ternary*, ast_function*, bool lvalue, ir_value**);
+static void ast_loop_delete(ast_loop*);
+static bool ast_loop_codegen(ast_loop*, ast_function*, bool lvalue, ir_value**);
+static void ast_breakcont_delete(ast_breakcont*);
+static bool ast_breakcont_codegen(ast_breakcont*, ast_function*, bool lvalue, ir_value**);
+static void ast_switch_delete(ast_switch*);
+static bool ast_switch_codegen(ast_switch*, ast_function*, bool lvalue, ir_value**);
+static void ast_label_delete(ast_label*);
+static void ast_label_register_goto(ast_label*, ast_goto*);
+static bool ast_label_codegen(ast_label*, ast_function*, bool lvalue, ir_value**);
+static bool ast_goto_codegen(ast_goto*, ast_function*, bool lvalue, ir_value**);
+static void ast_goto_delete(ast_goto*);
+static void ast_call_delete(ast_call*);
+static bool ast_call_codegen(ast_call*, ast_function*, bool lvalue, ir_value**);
+static bool ast_block_codegen(ast_block*, ast_function*, bool lvalue, ir_value**);
+static void ast_unary_delete(ast_unary*);
+static bool ast_unary_codegen(ast_unary*, ast_function*, bool lvalue, ir_value**);
+static void ast_entfield_delete(ast_entfield*);
+static bool ast_entfield_codegen(ast_entfield*, ast_function*, bool lvalue, ir_value**);
+static void ast_return_delete(ast_return*);
+static bool ast_return_codegen(ast_return*, ast_function*, bool lvalue, ir_value**);
+static void ast_binstore_delete(ast_binstore*);
+static bool ast_binstore_codegen(ast_binstore*, ast_function*, bool lvalue, ir_value**);
+static void ast_binary_delete(ast_binary*);
+static bool ast_binary_codegen(ast_binary*, ast_function*, bool lvalue, ir_value**);
/* It must not be possible to get here. */
static GMQCC_NORETURN void _ast_node_destroy(ast_node *self)
buf[pos] = 0;
}
+static bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out);
ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
{
ast_instantiate(ast_value, ctx, ast_value_delete);
mem_d(self);
}
-void ast_label_register_goto(ast_label *self, ast_goto *g)
+static void ast_label_register_goto(ast_label *self, ast_goto *g)
{
vec_push(self->gotos, g);
}
vtype->hasvalue = true;
vtype->constval.vfunc = self;
- self->varargs = NULL;
- self->argc = NULL;
- self->fixedparams = NULL;
+ self->varargs = NULL;
+ self->argc = NULL;
+ self->fixedparams = NULL;
+ self->return_value = NULL;
return self;
}
ast_delete(self->argc);
if (self->fixedparams)
ast_unref(self->fixedparams);
+ if (self->return_value)
+ ast_unref(self->return_value);
mem_d(self);
}
-const char* ast_function_label(ast_function *self, const char *prefix)
+static const char* ast_function_label(ast_function *self, const char *prefix)
{
size_t id;
size_t len;
* But I can't imagine a pituation where the output is truly unnecessary.
*/
-void _ast_codegen_output_type(ast_expression *self, ir_value *out)
+static void _ast_codegen_output_type(ast_expression *self, ir_value *out)
{
if (out->vtype == TYPE_FIELD)
out->fieldtype = self->next->vtype;
return false;
}
-bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
+static bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
{
ir_value *v = NULL;
return true;
}
+ /* have a local return value variable? */
+ if (self->return_value) {
+ if (!ast_local_codegen(self->return_value, self->ir_func, false))
+ return false;
+ }
+
if (!vec_size(self->blocks)) {
compile_error(ast_ctx(self), "function `%s` has no body", self->name);
return false;
}
else if (vec_size(self->curblock->entries) || self->curblock == irf->first)
{
- /* error("missing return"); */
- if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
+ if (self->return_value) {
+ cgen = self->return_value->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->return_value), self, false, &dummy))
+ return false;
+ return ir_block_create_return(self->curblock, ast_ctx(self), dummy);
+ }
+ else if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
"control reaches end of non-void function (`%s`) via %s",
self->name, self->curblock->label))
{