adding better negative number inference

This commit is contained in:
Jakub Doka 2024-10-25 15:07:39 +02:00
parent 6988d8893f
commit c88daa4800
No known key found for this signature in database
GPG key ID: C6E9A89936B8C143
3 changed files with 15 additions and 8 deletions

3
Cargo.lock generated
View file

@ -594,6 +594,9 @@ name = "hashbrown"
version = "0.15.0" version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb" checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb"
dependencies = [
"allocator-api2",
]
[[package]] [[package]]
name = "hashlink" name = "hashlink"

View file

@ -8,7 +8,7 @@ name = "hbc"
path = "src/main.rs" path = "src/main.rs"
[dependencies] [dependencies]
hashbrown = { version = "0.15.0", default-features = false, features = ["raw-entry"] } hashbrown = { version = "0.15.0", default-features = false, features = ["raw-entry", "allocator-api2"] }
hbbytecode = { workspace = true, features = ["disasm"] } hbbytecode = { workspace = true, features = ["disasm"] }
hbvm = { workspace = true, features = ["nightly"] } hbvm = { workspace = true, features = ["nightly"] }
log = "0.4.22" log = "0.4.22"

View file

@ -1715,8 +1715,8 @@ struct Ctx {
} }
impl Ctx { impl Ctx {
pub fn with_ty(self, ty: impl Into<ty::Id>) -> Self { pub fn with_ty(self, ty: ty::Id) -> Self {
Self { ty: Some(ty.into()) } Self { ty: Some(ty) }
} }
} }
@ -2255,7 +2255,8 @@ impl<'a> Codegen<'a> {
Some(val) Some(val)
} }
Expr::UnOp { pos, op: op @ TokenKind::Sub, val } => { Expr::UnOp { pos, op: op @ TokenKind::Sub, val } => {
let val = self.expr_ctx(val, ctx)?; let val =
self.expr_ctx(val, Ctx::default().with_ty(ctx.ty.unwrap_or(ty::Id::INT)))?;
if !val.ty.is_integer() { if !val.ty.is_integer() {
self.report(pos, fa!("cant negate '{}'", self.ty_display(val.ty))); self.report(pos, fa!("cant negate '{}'", self.ty_display(val.ty)));
} }
@ -2360,7 +2361,8 @@ impl<'a> Codegen<'a> {
}; };
let elem = self.tys.ins.slices[s as usize].elem; let elem = self.tys.ins.slices[s as usize].elem;
let idx = self.expr_ctx(index, Ctx::default().with_ty(ty::Id::INT))?; let mut idx = self.expr_ctx(index, Ctx::default().with_ty(ty::Id::DEFAULT_INT))?;
self.assert_ty(index.pos(), &mut idx, ty::Id::DEFAULT_INT, "subscript");
let value = self.tys.size_of(elem) as i64; let value = self.tys.size_of(elem) as i64;
let size = self.ci.nodes.new_node_nop(ty::Id::INT, Kind::CInt { value }, [VOID]); let size = self.ci.nodes.new_node_nop(ty::Id::INT, Kind::CInt { value }, [VOID]);
let inps = [VOID, idx.id, size]; let inps = [VOID, idx.id, size];
@ -2888,7 +2890,8 @@ impl<'a> Codegen<'a> {
continue; continue;
} }
let value = self.expr_ctx(&field.value, Ctx::default().with_ty(ty))?; let mut value = self.expr_ctx(&field.value, Ctx::default().with_ty(ty))?;
self.assert_ty(field.pos, &mut value, ty, fa!("field {}", field.name));
let mem = self.offset(mem, offset); let mem = self.offset(mem, offset);
self.store_mem(mem, ty, value.id); self.store_mem(mem, ty, value.id);
} }
@ -3051,10 +3054,11 @@ impl<'a> Codegen<'a> {
Expr::Break { pos } => self.jump_to(pos, 1), Expr::Break { pos } => self.jump_to(pos, 1),
Expr::Continue { pos } => self.jump_to(pos, 0), Expr::Continue { pos } => self.jump_to(pos, 0),
Expr::If { cond, then, else_, .. } => { Expr::If { cond, then, else_, .. } => {
let cond = self.expr_ctx(cond, Ctx::default().with_ty(ty::BOOL))?; let mut cnd = self.expr_ctx(cond, Ctx::default().with_ty(ty::Id::BOOL))?;
self.assert_ty(cond.pos(), &mut cnd, ty::Id::BOOL, "condition");
let if_node = let if_node =
self.ci.nodes.new_node(ty::Id::VOID, Kind::If, [self.ci.ctrl, cond.id]); self.ci.nodes.new_node(ty::Id::VOID, Kind::If, [self.ci.ctrl, cnd.id]);
'b: { 'b: {
let branch = match self.tof(if_node).expand().inner() { let branch = match self.tof(if_node).expand().inner() {