Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TIR: Intrinsic overhaul, remove hash-intrinsics and make everything more declarative #969

Merged
merged 11 commits into from
Sep 13, 2023
Merged
22 changes: 2 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
members = [
"compiler/*",
"compiler/hash*",
kontheocharis marked this conversation as resolved.
Show resolved Hide resolved
"tests",
"tests/testing-internal",
"tests/testing-macros",
Expand Down
20 changes: 6 additions & 14 deletions compiler/hash-ast-expand/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ use hash_storage::store::{
use hash_target::HasTarget;
use hash_tir::{
args::Arg,
intrinsics::definitions::Primitive,
lits::{CharLit, FloatLit, IntLit, Lit, StrLit},
node::{Node, NodeOrigin},
params::ParamIndex,
primitives::primitives,
terms::{Term, Ty, TyId},
utils::params::ParamUtils,
};
Expand Down Expand Up @@ -107,19 +107,11 @@ impl AstExpander<'_> {
};

match *param_ty.value() {
Ty::DataTy(data) => match data.data_def {
d if d == primitives().i32() => {
maybe_emit_err(matches!(value, AttrValueKind::Int(_)))
}
d if d == primitives().f64() => {
maybe_emit_err(matches!(value, AttrValueKind::Float(_)))
}
d if d == primitives().char() => {
maybe_emit_err(matches!(value, AttrValueKind::Char(_)))
}
d if d == primitives().str() => {
maybe_emit_err(matches!(value, AttrValueKind::Str(_)))
}
Ty::DataTy(data) => match Primitive::try_from_def(data.data_def) {
Some(Primitive::I32) => maybe_emit_err(matches!(value, AttrValueKind::Int(_))),
Some(Primitive::F64) => maybe_emit_err(matches!(value, AttrValueKind::Float(_))),
Some(Primitive::Char) => maybe_emit_err(matches!(value, AttrValueKind::Char(_))),
Some(Primitive::Str) => maybe_emit_err(matches!(value, AttrValueKind::Str(_))),
_ => panic!("unexpected attribute parameter type"),
},
_ => panic!("unexpected attribute parameter type"),
Expand Down
11 changes: 7 additions & 4 deletions compiler/hash-attrs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ use std::sync::LazyLock;

use hash_ast_utils::attr::AttrTarget;
use hash_source::identifier::Identifier;
use hash_tir::{building::gen, primitives::primitives, terms::Ty};
use hash_tir::building::gen;
use paste::paste;

use crate::ty::{AttrId, AttrTy, AttrTyMap};

// @@Future: add more complex rules which allow to specify more exotic types,
// i.e. a list of values.
macro_rules! make_ty {
($kind: ident) => {
($kind: ident) => {{
// ##GeneratedOrigin: these attributes are generated, so they do not
// have an origin.
Ty::data_ty(primitives().$kind(), hash_tir::node::NodeOrigin::Generated)
};
use hash_tir::intrinsics::definitions::*;
paste! {
[<$kind _gen_ty>]()
}
}};
}

macro_rules! define_attr {
Expand Down
1 change: 0 additions & 1 deletion compiler/hash-exhaustiveness/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ doctest = false

[dependencies]
hash-ast = { path = "../hash-ast" }
hash-intrinsics = { path = "../hash-intrinsics" }
hash-reporting = { path = "../hash-reporting" }
hash-source = { path = "../hash-source" }
hash-storage = { path = "../hash-storage" }
Expand Down
4 changes: 2 additions & 2 deletions compiler/hash-exhaustiveness/src/deconstruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use std::{
fmt::{self, Debug},
};

use hash_intrinsics::utils::PrimitiveUtils;
use hash_storage::store::{statics::StoreId, Store};
use hash_tir::{
data::{CtorDefId, DataTy},
intrinsics::utils::try_use_ty_as_array_ty,
node::NodesId,
pats::PatId,
terms::{Ty, TyId},
Expand Down Expand Up @@ -140,7 +140,7 @@ impl<'tc> ExhaustivenessChecker<'tc> {
match this_list.kind {
ArrayKind::Fixed(_) => panic!("{this_list:?} cannot cover {other_list:?}"),
ArrayKind::Var(prefix, suffix) => {
let array_ty = self.try_use_ty_as_array_ty(ctx.ty).unwrap();
let array_ty = try_use_ty_as_array_ty(ctx.ty).unwrap();

let prefix = pat.fields.fields[..prefix].to_vec();
let suffix = pat.fields.fields[this_list.arity() - suffix..].to_vec();
Expand Down
4 changes: 2 additions & 2 deletions compiler/hash-exhaustiveness/src/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
//! [Fields] with the typechecker context available for reading and creating
//! [DeconstructedPat](super::deconstruct::DeconstructedPat)s.

use hash_intrinsics::utils::PrimitiveUtils;
use hash_storage::store::{statics::StoreId, Store};
use hash_tir::{
data::{CtorDefId, DataDefCtors, DataTy},
intrinsics::utils::try_use_ty_as_array_ty,
node::NodesId,
terms::{Ty, TyId},
tuples::TupleTy,
Expand Down Expand Up @@ -112,7 +112,7 @@ impl<'tc> ExhaustivenessChecker<'tc> {
}
DeconstructedCtor::Array(list) => {
let arity = list.arity();
let array_ty = self.try_use_ty_as_array_ty(ctx.ty).unwrap();
let array_ty = try_use_ty_as_array_ty(ctx.ty).unwrap();
self.wildcards_from_tys((0..arity).map(|_| array_ty.element_ty))
}
DeconstructedCtor::Str(..)
Expand Down
18 changes: 10 additions & 8 deletions compiler/hash-exhaustiveness/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use std::mem::size_of;

use hash_ast::ast::RangeEnd;
use hash_intrinsics::utils::{LitTy, PrimitiveUtils};
use hash_source::constant::InternedInt;
use hash_storage::store::{
statics::{SequenceStoreValue, StoreId},
Expand All @@ -15,6 +14,10 @@ use hash_tir::{
control::{IfPat, OrPat},
data::{ArrayCtorInfo, CtorDefId, CtorPat, DataTy},
environment::env::AccessToEnv,
intrinsics::utils::{
numeric_max_val_of_lit, numeric_min_val_of_lit, try_use_ty_as_array_ty,
try_use_ty_as_int_ty, try_use_ty_as_lit_ty, LitTy,
},
lits::{CharLit, IntLit, Lit, LitPat, StrLit},
node::{Node, NodeOrigin, NodesId},
params::{ParamId, ParamsId},
Expand Down Expand Up @@ -167,8 +170,7 @@ impl<'tc> ExhaustivenessChecker<'tc> {
let mut prefix = vec![];
let mut suffix = vec![];

let Some(ArrayCtorInfo { element_ty, .. }) = self.try_use_ty_as_array_ty(ty_id)
else {
let Some(ArrayCtorInfo { element_ty, .. }) = try_use_ty_as_array_ty(ty_id) else {
panic!("Expected array type")
};

Expand Down Expand Up @@ -351,8 +353,8 @@ impl<'tc> ExhaustivenessChecker<'tc> {
Lit::Int(int) => Constant::from_int(int.interned_value(), ty).data(),
_ => unreachable!(),
},
None if at_end => self.numeric_max_val_of_lit(ty).unwrap(),
None => self.numeric_min_val_of_lit(ty).unwrap(),
None if at_end => numeric_max_val_of_lit(self.target(), ty).unwrap(),
None => numeric_min_val_of_lit(self.target(), ty).unwrap(),
}
};

Expand All @@ -369,7 +371,7 @@ impl<'tc> ExhaustivenessChecker<'tc> {
// `ubig` and `ibig` won't appear here since the range will never become
// a singleton, and in fact the range will never be constructed from a
// `ubig` or `ibig` type.
match self.try_use_ty_as_lit_ty(ty).unwrap() {
match try_use_ty_as_lit_ty(self.target(), ty).unwrap() {
ty if ty.is_int() => {
let (lo, _) = range.boundaries();
let bias = range.bias;
Expand Down Expand Up @@ -402,7 +404,7 @@ impl<'tc> ExhaustivenessChecker<'tc> {
let bias = range.bias;
let (lo, hi) = (lo ^ bias, hi ^ bias);

let (lo, hi) = match self.try_use_ty_as_lit_ty(ty).unwrap() {
let (lo, hi) = match try_use_ty_as_lit_ty(self.target(), ty).unwrap() {
LitTy::I8
| LitTy::U8
| LitTy::I16
Expand All @@ -413,7 +415,7 @@ impl<'tc> ExhaustivenessChecker<'tc> {
| LitTy::U64
| LitTy::U128
| LitTy::I128 => {
let kind = self.try_use_ty_as_int_ty(ty).unwrap();
let kind = try_use_ty_as_int_ty(ty).unwrap();
let ptr_width = self.target().ptr_size();

let lo_val = InternedInt::from_u128(lo, kind, ptr_width);
Expand Down
4 changes: 2 additions & 2 deletions compiler/hash-exhaustiveness/src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ use std::{
};

use hash_ast::ast::RangeEnd;
use hash_intrinsics::utils::PrimitiveUtils;
use hash_reporting::diagnostic::Diagnostics;
use hash_storage::store::Store;
use hash_tir::{
environment::env::AccessToEnv,
intrinsics::utils::try_use_ty_as_int_ty,
pats::{PatId, RangePat},
terms::TyId,
};
Expand Down Expand Up @@ -275,7 +275,7 @@ impl<'tc> ExhaustivenessChecker<'tc> {
/// of the integer size, in other words at the position where the
/// last byte is that identifies the sign.
pub(crate) fn signed_bias(&self, ty: TyId) -> u128 {
if let Some(ty) = self.try_use_ty_as_int_ty(ty) {
if let Some(ty) = try_use_ty_as_int_ty(ty) {
if ty.is_signed() && !ty.is_big() {
let size = ty.size(self.target().ptr_size());
let bits = size.bits() as u128;
Expand Down
4 changes: 2 additions & 2 deletions compiler/hash-exhaustiveness/src/wildcard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
//! the whole range of all possible values by the associated type
//! to the constructor.
use hash_ast::ast::RangeEnd;
use hash_intrinsics::utils::PrimitiveUtils;
use hash_storage::store::{statics::StoreId, SequenceStoreKey, Store, TrivialSequenceStoreKey};
use hash_target::size::Size;
use hash_tir::{
data::{DataDefCtors, DataTy, NumericCtorBits, PrimitiveCtorInfo},
intrinsics::utils::try_use_ty_as_int_ty,
terms::Ty,
};
use hash_utils::smallvec::{smallvec, SmallVec};
Expand Down Expand Up @@ -221,7 +221,7 @@ impl<'tc> ExhaustivenessChecker<'tc> {
// we sometimes prefer reporting the list of constructors instead of
// just `_`.
let ctor = if !wildcard.matrix_ctors.is_empty()
|| (ctx.is_top_level && self.try_use_ty_as_int_ty(ctx.ty).is_none())
|| (ctx.is_top_level && try_use_ty_as_int_ty(ctx.ty).is_none())
{
DeconstructedCtor::Missing
} else {
Expand Down
20 changes: 0 additions & 20 deletions compiler/hash-intrinsics/Cargo.toml

This file was deleted.

Loading
Loading