cleanup: 1

This commit is contained in:
Jakub Doka 2024-10-19 19:37:02 +02:00
parent 6ad0b41759
commit 959bfd7f76
No known key found for this signature in database
GPG key ID: C6E9A89936B8C143
8 changed files with 640 additions and 469 deletions

View file

@ -342,7 +342,7 @@ fib_iter := fn(n: int): int {
```hb
main := fn(): int {
addr := @as(u16, 0x1FF)
msg := [u8].(0, 0, @as(u8, addr & 0xFF), @as(u8, addr >> 8 & 0xFF))
msg := [u8].(0, 0, @trunc(addr), @trunc(addr >> 8))
_force_stack := &msg
arr := [int].(1, 2, 4)

View file

@ -874,7 +874,7 @@ impl Codegen {
match base_val.ty.expand() {
ty::Kind::Slice(arr) => {
let ty = self.tys.ins.arrays[arr as usize].ty;
let ty = self.tys.ins.slices[arr as usize].elem;
let item_size = self.tys.size_of(ty);
let Loc::Rt { derefed: true, ref mut reg, ref stack, offset } =
@ -1209,12 +1209,12 @@ impl Codegen {
}
}
ty::Kind::Slice(arr) => {
let arr = self.tys.ins.arrays[arr as usize];
let item_size = self.tys.size_of(arr.ty);
let arr = self.tys.ins.slices[arr as usize];
let item_size = self.tys.size_of(arr.elem);
for (i, value) in fields.iter().enumerate() {
let loc = loc.as_ref().offset(i as u32 * item_size);
let value =
self.expr_ctx(value, Ctx::default().with_loc(loc).with_ty(arr.ty))?;
let value = self
.expr_ctx(value, Ctx::default().with_loc(loc).with_ty(arr.elem))?;
self.ci.free_loc(value.loc);
}
}
@ -2014,9 +2014,9 @@ impl Codegen {
}
}
ty::Kind::Slice(arr) => {
let arr = &self.tys.ins.arrays[arr as usize];
let arr = &self.tys.ins.slices[arr as usize];
if arr.len == ArrayLen::MAX {
ty = self.tys.make_array(arr.ty, field_len as _);
ty = self.tys.make_array(arr.elem, field_len as _);
} else if arr.len != field_len as u32 {
self.report(
pos,

View file

@ -280,6 +280,7 @@ impl TokenKind {
Self::Eq => (a == b) as i64,
Self::Ne => (a != b) as i64,
Self::Band => a & b,
Self::Shr => a >> b,
s => todo!("{s}"),
}
}

View file

@ -318,7 +318,7 @@ mod ty {
let gb = &ctx.globals[g as usize];
crate::SymKey::Decl(gb.file, gb.name)
}
Kind::Slice(s) => crate::SymKey::Array(&ctx.arrays[s as usize]),
Kind::Slice(s) => crate::SymKey::Array(&ctx.slices[s as usize]),
Kind::Module(_) | Kind::Builtin(_) => crate::SymKey::Decl(u32::MAX, u32::MAX),
}
}
@ -351,7 +351,7 @@ mod ty {
}
pub fn is_pointer(self) -> bool {
matches!(Kind::from_ty(self), Kind::Ptr(_))
matches!(self.expand(), Kind::Ptr(_))
}
pub fn try_upcast(self, ob: Self, kind: TyCheck) -> Option<Self> {
@ -587,9 +587,9 @@ mod ty {
idx.fmt(f)
}
TK::Slice(idx) => {
let array = self.tys.ins.arrays[idx as usize];
let array = self.tys.ins.slices[idx as usize];
f.write_str("[")?;
self.rety(array.ty).fmt(f)?;
self.rety(array.elem).fmt(f)?;
if array.len != ArrayLen::MAX {
f.write_str("; ")?;
array.len.fmt(f)?;
@ -738,9 +738,14 @@ pub struct Ptr {
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Array {
ty: ty::Id,
elem: ty::Id,
len: ArrayLen,
}
impl Array {
fn len(&self) -> Option<usize> {
(self.len != ArrayLen::MAX).then_some(self.len as usize)
}
}
struct ParamAlloc(Range<u8>);
@ -822,7 +827,7 @@ pub struct TypeIns {
structs: Vec<Struct>,
fields: Vec<Field>,
ptrs: Vec<Ptr>,
arrays: Vec<Array>,
slices: Vec<Array>,
}
#[derive(Default)]
@ -1118,9 +1123,9 @@ impl Types {
fn make_array_low(&mut self, ty: ty::Id, len: ArrayLen) -> ty::Slice {
self.syms
.get_or_insert(SymKey::Array(&Array { ty, len }), &mut self.ins, |ins| {
ins.arrays.push(Array { ty, len });
ty::Kind::Slice(ins.arrays.len() as u32 - 1).compress()
.get_or_insert(SymKey::Array(&Array { elem: ty, len }), &mut self.ins, |ins| {
ins.slices.push(Array { elem: ty, len });
ty::Kind::Slice(ins.slices.len() as u32 - 1).compress()
})
.expand()
.inner()
@ -1156,11 +1161,11 @@ impl Types {
ty::Kind::Builtin(ty::I16 | ty::U16) => 2,
ty::Kind::Builtin(ty::I8 | ty::U8 | ty::BOOL) => 1,
ty::Kind::Slice(arr) => {
let arr = &self.ins.arrays[arr as usize];
let arr = &self.ins.slices[arr as usize];
match arr.len {
0 => 0,
ArrayLen::MAX => 16,
len => self.size_of(arr.ty) * len,
len => self.size_of(arr.elem) * len,
}
}
ty::Kind::Struct(stru) => {
@ -1197,10 +1202,10 @@ impl Types {
align
}
ty::Kind::Slice(arr) => {
let arr = &self.ins.arrays[arr as usize];
let arr = &self.ins.slices[arr as usize];
match arr.len {
ArrayLen::MAX => 8,
_ => self.align_of(arr.ty),
_ => self.align_of(arr.elem),
}
}
_ => self.size_of(ty).max(1),

View file

@ -622,6 +622,7 @@ fn find_ident(idents: &mut [ScopeIdent], id: Ident) -> &mut ScopeIdent {
}
pub fn find_symbol(symbols: &[Symbol], id: Ident) -> &Symbol {
// TODO: we can turn this to direct index
symbols.binary_search_by_key(&id, |s| s.name).map(|i| &symbols[i]).unwrap()
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,36 @@
main:
ADDI64 r254, r254, -100d
ST r31, r254, 28a, 72h
LI64 r32, 4d
LI64 r33, 1d
LI64 r34, 2d
LI64 r35, 3d
LI64 r36, 1d
LI64 r37, 0d
ADDI64 r2, r254, 0d
ADDI64 r38, r254, 24d
ST r37, r254, 24a, 1h
ST r37, r254, 25a, 1h
ST r35, r254, 26a, 1h
ST r33, r254, 27a, 1h
ST r36, r254, 0a, 8h
ST r34, r254, 8a, 8h
ST r32, r254, 16a, 8h
JAL r31, r0, :pass
LD r39, r254, 27a, 1h
ADD64 r1, r39, r1
LD r31, r254, 28a, 72h
ADDI64 r254, r254, 100d
JALA r0, r31, 0a
pass:
LD r3, r2, 8a, 8h
MULI64 r8, r3, 8d
ADD64 r10, r8, r2
LD r8, r10, 0a, 8h
LD r9, r2, 0a, 8h
ADD64 r11, r3, r9
ADD64 r1, r8, r11
JALA r0, r31, 0a
code size: 337
ret: 8
status: Ok(())