commit aba521506ff6c227f02483bcca47bca1f8646b1a
parent d5a5d2e1f205aaa313f2d74bb43279be935e80f2
Author: Drew DeVault <sir@cmpwn.com>
Date: Mon, 23 Aug 2021 13:04:07 +0200
cmd/harec: basic gen_expr_const implementation
I'm not going to finish this until we simplify lex::value. The basic
idea is to also introduce:
type ast::value = lex::value;
type unit::value = ast::value;
Then I also want to merge bool, null, and void, into lex::value.
Signed-off-by: Drew DeVault <sir@cmpwn.com>
Diffstat:
2 files changed, 23 insertions(+), 3 deletions(-)
diff --git a/cmd/harec/gen.ha b/cmd/harec/gen.ha
@@ -1,6 +1,7 @@
use bufio;
use fmt;
use hare::ast;
+use hare::lex;
use hare::module;
use hare::types;
use hare::unit;
@@ -63,12 +64,30 @@ fn gen_func(ctx: *context, decl: *unit::decl) void = {
fmt::fprintln(ctx.out, mklabel(ctx, "body"))!;
gen_expr(ctx, fndecl.body: *unit::expr);
+ emit(ctx, void, qinstr::RET); // TODO
io::write(os::stdout, bufio::buffer(ctx.out))!;
fmt::println("}\n")!;
};
fn gen_expr(ctx: *context, expr: *unit::expr) value = {
- emit(ctx, void, qinstr::RET); // TODO
- return vvoid;
+ match (expr.expr) {
+ unit::constant_expr => return gen_expr_const(ctx, expr),
+ * => abort(), // TODO
+ };
+ abort(); // Unreachable
+};
+
+fn gen_expr_const(ctx: *context, expr: *unit::expr) value = {
+ const constexpr = expr.expr as unit::constant_expr;
+ const val: qval = match (constexpr) {
+ void => return vvoid,
+ b: bool => (if (b) 1u32 else 0u32): constant,
+ unit::_null => 0u64, // XXX: Arch
+ v: lex::value => abort(), // TODO
+ };
+ return value {
+ value = val,
+ _type = expr.result,
+ };
};
diff --git a/cmd/harec/qbe.ha b/cmd/harec/qbe.ha
@@ -4,9 +4,10 @@ type global = str;
type temporary = str;
type qvoid = void;
type constant = (u32 | u64 | f32 | f64 | str | qvoid);
+type qval = (global | temporary | constant);
type value = struct {
- value: (global | temporary | constant),
+ value: qval,
_type: const *types::_type,
};