commit 9e4de86912bda3eee5d83f894f9dd8b9e4282133
parent 552dcd235b500e6c7b33176e5d027ec9989f3bfe
Author: Drew DeVault <sir@cmpwn.com>
Date: Thu, 2 Sep 2021 11:57:37 +0200
cmd/harec: implement emit with args
Signed-off-by: Drew DeVault <sir@cmpwn.com>
Diffstat:
2 files changed, 33 insertions(+), 4 deletions(-)
diff --git a/cmd/harec/gen.ha b/cmd/harec/gen.ha
@@ -64,8 +64,14 @@ fn gen_func(ctx: *context, decl: *unit::decl) void = {
bufio::reset(ctx.out);
fmt::fprintln(ctx.out, mklabel(ctx, "body"))!;
- gen_expr(ctx, fndecl.body: *unit::expr);
- emit(ctx, void, qinstr::RET); // TODO
+
+ // TODO: We needn't write our own return if the user returned themselves
+ const rval = gen_expr(ctx, fndecl.body: *unit::expr);
+ if (has_rval) {
+ emit(ctx, void, qinstr::RET, rval);
+ } else {
+ emit(ctx, void, qinstr::RET);
+ };
io::write(os::stdout, bufio::buffer(ctx.out))!;
fmt::println("}\n")!;
@@ -84,7 +90,11 @@ fn gen_expr_const(ctx: *context, expr: *unit::expr) value = {
void => return vvoid,
b: bool => (if (b) 1u32 else 0u32): constant,
ast::_null => 0u64, // XXX: Arch
- v: ast::value => abort(), // TODO
+ r: rune => r: u32,
+ v: (u64 | f64) => v,
+ i: i64 => i: u64,
+ s: str => abort(), // TODO
+ // TODO: Aggregate types
};
return value {
value = val,
diff --git a/cmd/harec/qbe.ha b/cmd/harec/qbe.ha
@@ -118,7 +118,26 @@ fn emit(
qinstr::XOR => "xor",
})!;
for (let i = 0z; i < len(args); i += 1) {
- abort(); // TODO
+ const arg = match (args[i]) {
+ v: value => v.value,
+ * => args[i],
+ };
+ match (arg) {
+ v: value => abort(), // Invariant
+ qv: qval => match (qv) {
+ g: global => fmt::fprintf(ctx.out, " ${}", g)!,
+ t: temporary => fmt::fprintf(ctx.out, " %{}", t)!,
+ c: constant => match (c) {
+ qvoid => abort(),
+ v: (u32 | u64 | f32 | f64 | str) =>
+ fmt::fprintf(ctx.out, " {}", v)!,
+ },
+ },
+ qt: qtype => abort(), // TODO
+ };
+ if (i + 1 < len(args)) {
+ fmt::fprint(ctx.out, ",")!;
+ };
};
fmt::fprintln(ctx.out)!;
};