commit de6020676a978c9ca1f101408d70e62bcfa09aa6
parent 4e8cc2de3ce1764671fab1c29d64cf1bb686ab34
Author: Drew DeVault <sir@cmpwn.com>
Date: Sun, 20 Dec 2020 11:57:18 -0500
gen: implement label generation
Diffstat:
4 files changed, 24 insertions(+), 16 deletions(-)
diff --git a/include/qbe.h b/include/qbe.h
@@ -191,14 +191,10 @@ struct qbe_program {
void qbe_append_def(struct qbe_program *prog, struct qbe_def *def);
-// The "i" family of functions take a list of qbe_values as the parameters to
-// provide to the given instruction. The "l" family takes a printf-compatible
-// set of values to match with fmt to produce a label (plus the ID postfix,
-// which is added for you).
void geni(struct qbe_statement *stmt, enum qbe_instr instr, const struct qbe_value *out, ...);
-void genl(struct qbe_statement *stmt, uint64_t *id, const char *fmt, ...);
+const char *genl(struct qbe_statement *stmt, uint64_t *id, const char *fmt);
void pushi(struct qbe_func *func, enum qbe_instr instr, const struct qbe_value *out, ...);
-void pushl(struct qbe_func *func, uint64_t *id, const char *fmt, ...);
+const char *pushl(struct qbe_func *func, uint64_t *id, const char *fmt);
void constl(struct qbe_value *val, uint64_t l);
diff --git a/src/emit.c b/src/emit.c
@@ -85,7 +85,8 @@ emit_stmt(struct qbe_statement *stmt, FILE *out)
fprintf(out, "\n");
break;
case Q_LABEL:
- assert(0); // TODO
+ fprintf(out, "@%s\n", stmt->label);
+ break;
}
}
diff --git a/src/gen.c b/src/gen.c
@@ -49,8 +49,7 @@ static void
alloc_temp(struct gen_context *ctx, struct qbe_value *val,
const struct type *type, char *fmt)
{
- const struct qbe_type *qtype = qtype_for_type(ctx, type, false);
- gen_temp(ctx, val, qtype, fmt);
+ gen_temp(ctx, val, &qbe_long, fmt); // XXX: Architecture dependent
struct qbe_value size;
constl(&size, type->size);
@@ -64,7 +63,7 @@ gen_expression(struct gen_context *ctx,
const struct expression *expr,
struct qbe_value *out)
{
- //assert(0); // TODO
+ assert(0); // TODO
}
static void
@@ -85,6 +84,8 @@ gen_function_decl(struct gen_context *ctx, const struct declaration *decl)
assert(fntype->func.params == NULL); // TODO
+ pushl(&qdef->func, &ctx->id, "start.%d");
+
// TODO: Update for void type
struct qbe_value rval;
alloc_temp(ctx, &rval, fntype->func.result, "return.%d");
diff --git a/src/qbe.c b/src/qbe.c
@@ -1,5 +1,6 @@
#include <assert.h>
#include <stdarg.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "qbe.h"
@@ -186,10 +187,16 @@ geni(struct qbe_statement *stmt, enum qbe_instr instr,
va_end(ap);
}
-void
-genl(struct qbe_statement *stmt, uint64_t *id, const char *fmt, ...)
+const char *
+genl(struct qbe_statement *stmt, uint64_t *id, const char *fmt)
{
- assert(0); // TODO
+ stmt->type = Q_LABEL;
+ int n = snprintf(NULL, 0, fmt, *id);
+ char *l = calloc(1, n + 1);
+ snprintf(l, n + 1, fmt, *id);
+ stmt->label = l;
+ *id = *id + 1;
+ return l;
}
static void
@@ -222,10 +229,13 @@ pushi(struct qbe_func *func, enum qbe_instr instr,
push(func, &stmt);
}
-void
-pushl(struct qbe_func *func, uint64_t *id, const char *fmt, ...)
+const char *
+pushl(struct qbe_func *func, uint64_t *id, const char *fmt)
{
- assert(0); // TODO
+ struct qbe_statement stmt = {0};
+ const char *l = genl(&stmt, id, fmt);
+ push(func, &stmt);
+ return l;
}
void