harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 56eac2ff6b4b6750b6eeef5ae7862497758c4f50
parent 6bef0ed6a0129220eed26f76647cc22fcd36a4b9
Author: Drew DeVault <sir@cmpwn.com>
Date:   Thu,  1 Jul 2021 09:37:18 -0400

gen: basic function riggings

Signed-off-by: Drew DeVault <sir@cmpwn.com>

Diffstat:
Msrc/gen.c | 37++++++++++++++++++++++++++++++++++++-
1 file changed, 36 insertions(+), 1 deletion(-)

diff --git a/src/gen.c b/src/gen.c @@ -1,15 +1,50 @@ #include <assert.h> +#include <string.h> #include "check.h" #include "expr.h" #include "gen.h" +#include "scope.h" +#include "util.h" static void -gen_function_decl(struct gen_context *ctx, const struct declaration *decl) +gen_expr(struct gen_context *ctx, const struct expression *expr) { assert(0); // TODO } static void +gen_function_decl(struct gen_context *ctx, const struct declaration *decl) +{ + const struct function_decl *func = &decl->func; + const struct type *fntype = func->type; + if (func->body == NULL) { + return; // Prototype + } + // TODO: Attributes + assert(!func->flags); + + struct qbe_def *qdef = xcalloc(1, sizeof(struct qbe_def)); + qdef->kind = Q_FUNC; + qdef->exported = decl->exported; + qdef->name = decl->symbol ? strdup(decl->symbol) + : ident_to_sym(&decl->ident); + + struct qbe_statement start_label = {0}; + genl(&start_label, &ctx->id, "start.%d"); + push(&qdef->func.prelude, &start_label); + + // TODO: Allocate parameters, return value + assert(fntype->func.result->storage == STORAGE_VOID); + assert(!func->scope->objects); + qdef->func.returns = &qbe_void; + + pushl(&qdef->func, &ctx->id, "body.%d"); + gen_expr(ctx, func->body); + + qbe_append_def(ctx->out, qdef); +} + +static void gen_decl(struct gen_context *ctx, const struct declaration *decl) { switch (decl->type) {