hare

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

commit a70840843b277c91e27f11d1ea0bad7dd50cbc9b
parent 79352cc1dd744ff4464427eecc93ddbf018e2c72
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sun, 22 Aug 2021 19:39:59 +0200

hare::types: add singletons for builtins

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

Diffstat:
Mcmd/harec/gen.ha | 13++-----------
Mcmd/harec/qbe.ha | 5+++++
Ahare/types/builtins.ha | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mhare/types/hash.ha | 1+
Mhare/types/store.ha | 20++++++++++++++++++++
Mscripts/gen-stdlib | 1+
Mstdlib.mk | 2++
7 files changed, 105 insertions(+), 11 deletions(-)

diff --git a/cmd/harec/gen.ha b/cmd/harec/gen.ha @@ -69,15 +69,6 @@ fn gen_func(ctx: *context, decl: *unit::decl) void = { }; fn gen_expr(ctx: *context, expr: *unit::expr) value = { - // TODO - emit(ctx, void, qinstr::RET); - - // TODO: - // - Make this a constant called valvoid - // - Implement tagged unions (and their casts) in eval - // - Add builtin type globals in hare::types like &types::builtin_void - return value { - value = qvoid, - _type = types::lookup_builtin(ctx.store, ast::builtin_type::VOID), - }; + emit(ctx, void, qinstr::RET); // TODO + return vvoid; }; diff --git a/cmd/harec/qbe.ha b/cmd/harec/qbe.ha @@ -10,6 +10,11 @@ type value = struct { _type: const *types::_type, }; +const vvoid: value = value { + value = qvoid, + _type = &types::builtin_void, +}; + type qinstr = enum { ADD, ALLOC16, diff --git a/hare/types/builtins.ha b/hare/types/builtins.ha @@ -0,0 +1,74 @@ +export let builtin_char: _type = _type { + repr = builtin::CHAR, + sz = 1, align = 1, + ... +}, builtin_f32: _type = _type { + repr = builtin::F32, + sz = 4, align = 4, + ... +}, builtin_f64: _type = _type { + repr = builtin::F64, + sz = 8, align = 8, + ... +}, builtin_i8: _type = _type { + repr = builtin::I8, + sz = 1, align = 1, + ... +}, builtin_i16: _type = _type { + repr = builtin::I16, + sz = 2, align = 2, + ... +}, builtin_i32: _type = _type { + repr = builtin::I32, + sz = 4, align = 4, + ... +}, builtin_i64: _type = _type { + repr = builtin::I64, + sz = 8, align = 8, + ... +}, builtin_rune: _type = _type { + repr = builtin::RUNE, + sz = 4, align = 4, + ... +}, builtin_u8: _type = _type { + repr = builtin::U8, + sz = 1, align = 1, + ... +}, builtin_u16: _type = _type { + repr = builtin::U16, + sz = 2, align = 2, + ... +}, builtin_u32: _type = _type { + repr = builtin::U32, + sz = 4, align = 4, + ... +}, builtin_u64: _type = _type { + repr = builtin::U64, + sz = 8, align = 8, + ... +}, builtin_void: _type = _type { + repr = builtin::VOID, + sz = 0, align = 0, + ... +}; + +@init fn init() void = { + const builtins = [ + &builtin_char, + &builtin_f32, + &builtin_f64, + &builtin_i8, + &builtin_i16, + &builtin_i32, + &builtin_i64, + &builtin_rune, + &builtin_u8, + &builtin_u16, + &builtin_u32, + &builtin_u64, + &builtin_void, + ]; + for (let i = 0z; i < len(builtins); i += 1) { + builtins[i].id = hash(builtins[i]); + }; +}; diff --git a/hare/types/hash.ha b/hare/types/hash.ha @@ -2,6 +2,7 @@ use endian; use hash::fnv; use hash; use strings; +use fmt; // Keep ordered with respect to bootstrap harec:include/types.h type storage = enum u8 { diff --git a/hare/types/store.ha b/hare/types/store.ha @@ -68,6 +68,26 @@ export fn lookup( ty: *ast::_type, ) (const *_type | deferred | error) = { const ty = fromast(store, ty)?; + if (ty.flags == 0) match (ty.repr) { + b: builtin => switch (b) { + builtin::CHAR => return &builtin_char, + builtin::F32 => return &builtin_f32, + builtin::F64 => return &builtin_f64, + builtin::I8 => return &builtin_i8, + builtin::I16 => return &builtin_i16, + builtin::I32 => return &builtin_i32, + builtin::I64 => return &builtin_i64, + builtin::RUNE => return &builtin_rune, + builtin::U8 => return &builtin_u8, + builtin::U16 => return &builtin_u16, + builtin::U32 => return &builtin_u32, + builtin::U64 => return &builtin_u64, + builtin::VOID => return &builtin_void, + * => void, + }, + * => void, + }; + const id = hash(&ty); let bucket = &store.map[id % BUCKETS]; for (let i = 0z; i < len(bucket); i += 1) { diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib @@ -361,6 +361,7 @@ hare_ast() { gensrcs_hare_types() { gen_srcs hare::types \ arch.ha \ + builtins.ha \ hash.ha \ lookup.ha \ store.ha \ diff --git a/stdlib.mk b/stdlib.mk @@ -682,6 +682,7 @@ $(HARECACHE)/hare/parse/hare_parse.ssa: $(stdlib_hare_parse_srcs) $(stdlib_rt) $ # hare::types stdlib_hare_types_srcs= \ $(STDLIB)/hare/types/arch.ha \ + $(STDLIB)/hare/types/builtins.ha \ $(STDLIB)/hare/types/hash.ha \ $(STDLIB)/hare/types/lookup.ha \ $(STDLIB)/hare/types/store.ha \ @@ -1904,6 +1905,7 @@ $(TESTCACHE)/hare/parse/hare_parse.ssa: $(testlib_hare_parse_srcs) $(testlib_rt) # hare::types testlib_hare_types_srcs= \ $(STDLIB)/hare/types/arch.ha \ + $(STDLIB)/hare/types/builtins.ha \ $(STDLIB)/hare/types/hash.ha \ $(STDLIB)/hare/types/lookup.ha \ $(STDLIB)/hare/types/store.ha \