hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 8db8f5319ee0408303e2082effca13f0f8683c46
parent d50c9aa0b362b20ff49647bd0adfe7f7b0a780f8
Author: Drew DeVault <sir@cmpwn.com>
Date:   Thu,  2 Sep 2021 17:50:45 +0200

cmd/harec: basic gen_store

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

Diffstat:
Mcmd/harec/gen.ha | 18+++++++++++++++++-
Mcmd/harec/qbe.ha | 27+++++++++++++++++++++++++++
2 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/cmd/harec/gen.ha b/cmd/harec/gen.ha @@ -4,6 +4,7 @@ use hare::ast; use hare::lex; use hare::module; use hare::types; +use hare::types::{builtin}; use hare::unit; use io; use os; @@ -79,6 +80,21 @@ fn gen_func(ctx: *context, decl: *unit::decl) void = { fmt::println("}\n")!; }; +fn gen_store(ctx: *context, object: value, value: value) void = { + match (types::dealias(object._type).repr) { + bi: types::builtin => switch (bi) { + builtin::STR => abort(), // TODO + * => void, + }, + types::_struct => abort(), // TODO + (types::array | types::slice | types::tagged | types::tuple) => + abort(), // TODO + * => void, + }; + const instr = qinstr_store(ctx, object._type); + emit(ctx.out, void, instr, value, object); +}; + fn gen_expr(ctx: *context, expr: *unit::expr) value = { return match (expr.expr) { unit::binding_expr => gen_binding(ctx, expr), @@ -94,7 +110,7 @@ fn gen_expr_at(ctx: *context, expr: *unit::expr, at: value) void = { * => void, }; const value = gen_expr(ctx, expr); - // TODO: Store + gen_store(ctx, at, value); }; fn gen_binding(ctx: *context, expr: *unit::expr) value = { diff --git a/cmd/harec/qbe.ha b/cmd/harec/qbe.ha @@ -1,6 +1,7 @@ use fmt; use io; use hare::types; +use hare::types::{builtin}; type global = str; type temporary = str; @@ -158,6 +159,32 @@ fn qinstr_alloc(ty: *types::_type) qinstr = switch (ty.align) { * => abort(), }; +fn qinstr_store( + ctx: *context, + ty: *types::_type, +) qinstr = match (ty.repr) { + al: types::alias => qinstr_store(ctx, al.secondary: *types::_type), + bi: types::builtin => switch (bi) { + builtin::STR, builtin::VOID => abort(), + builtin::F32 => qinstr::STORES, + builtin::F64 => qinstr::STORED, + * => switch (ty.sz) { + 1 => qinstr::STOREB, + 2 => qinstr::STOREH, + 4 => qinstr::STOREW, + 8 => qinstr::STOREL, + * => abort(), + }, + }, + types::pointer => if (ctx.arch.ptr == &qlong) { + yield qinstr::STOREL; + } else if (ctx.arch.ptr == &qword) { + yield qinstr::STOREW; + } else abort(), + en: types::_enum => abort(), // TODO + * => abort(), +}; + type qinstr = enum { ADD, ALLOC16,