type checking !?!?

This commit is contained in:
2025-03-22 14:40:32 -04:00
parent 606cb30c6b
commit 7f809d797c
44 changed files with 664 additions and 314 deletions

View File

@@ -10,7 +10,13 @@ pub enum LinkerInstruction {
Slli { dest: Reg, src: Reg, imm: i32 },
Srli { dest: Reg, src: Reg, imm: i32 },
Sd { src: Reg, offset: i32, base: Reg },
Sw { src: Reg, offset: i32, base: Reg },
Sh { src: Reg, offset: i32, base: Reg },
Sb { src: Reg, offset: i32, base: Reg },
Ld { dest: Reg, offset: i32, base: Reg },
Lw { dest: Reg, offset: i32, base: Reg },
Lh { dest: Reg, offset: i32, base: Reg },
Lb { dest: Reg, offset: i32, base: Reg },
Mv { dest: Reg, src: Reg },
La { dest: Reg, src: Symbol },
Jal { dest: Reg, offset: i32 },
@@ -36,7 +42,13 @@ impl Instr for LinkerInstruction {
Self::Slli { dest, src, imm } => slli(*dest, *src, BitsI32::new(*imm)),
Self::Srli { dest, src, imm } => srli(*dest, *src, BitsI32::new(*imm)),
Self::Sd { src, offset, base } => sd(*src, BitsI32::new(*offset), *base),
Self::Sw { src, offset, base } => sw(*src, BitsI32::new(*offset), *base),
Self::Sh { src, offset, base } => sh(*src, BitsI32::new(*offset), *base),
Self::Sb { src, offset, base } => sb(*src, BitsI32::new(*offset), *base),
Self::Ld { dest, offset, base } => ld(*dest, BitsI32::new(*offset), *base),
Self::Lw { dest, offset, base } => lw(*dest, BitsI32::new(*offset), *base),
Self::Lh { dest, offset, base } => lh(*dest, BitsI32::new(*offset), *base),
Self::Lb { dest, offset, base } => lb(*dest, BitsI32::new(*offset), *base),
Self::Mv { dest, src } => addi(*dest, *src, BitsI32::new(0)),
Self::La { dest, src } => {
if let Some(addr) = sym_map.get(*src) {

View File

@@ -4,7 +4,7 @@ use crate::{
compiler::{arch::riscv64::Reg, create_program, Addr},
ir::{
arch::riscv64::{RV64Instruction as AI, RegRef},
IRLInstruction as IRI, IRLProgram, Len, Size, Symbol, VarID,
IRLInstruction as IRI, IRLProgram, Len, Size,
},
};
@@ -40,6 +40,54 @@ fn mov_mem(
len -= 8;
off += 8;
}
while len >= 4 {
v.extend([
LI::Lw {
dest: temp,
offset: src_offset + off,
base: src,
},
LI::Sw {
src: temp,
offset: dest_offset + off,
base: dest,
},
]);
len -= 4;
off += 4;
}
while len >= 2 {
v.extend([
LI::Lh {
dest: temp,
offset: src_offset + off,
base: src,
},
LI::Sh {
src: temp,
offset: dest_offset + off,
base: dest,
},
]);
len -= 2;
off += 2;
}
while len >= 1 {
v.extend([
LI::Lb {
dest: temp,
offset: src_offset + off,
base: src,
},
LI::Sb {
src: temp,
offset: dest_offset + off,
base: dest,
},
]);
len -= 1;
off += 1;
}
}
pub fn compile(program: IRLProgram) -> (Vec<u8>, Option<Addr>) {

View File

@@ -13,24 +13,33 @@ pub const fn ebreak() -> I {
pub const fn auipc(dest: Reg, imm: BitsI32<31, 12>) -> I {
u_type(imm.to_u(), dest, AUIPC)
}
pub const fn ld(dest: Reg, offset: BitsI32<11, 0>, base: Reg) -> I {
i_type(offset.to_u(), base, width::D, dest, LOAD)
}
pub const fn lw(dest: Reg, offset: BitsI32<11, 0>, base: Reg) -> I {
i_type(offset.to_u(), base, width::W, dest, LOAD)
}
pub const fn lh(dest: Reg, offset: BitsI32<11, 0>, base: Reg) -> I {
i_type(offset.to_u(), base, width::H, dest, LOAD)
}
pub const fn lb(dest: Reg, offset: BitsI32<11, 0>, base: Reg) -> I {
i_type(offset.to_u(), base, width::B, dest, LOAD)
}
pub const fn sd(src: Reg, offset: BitsI32<11, 0>, base: Reg) -> I {
s_type(src, base, width::D, offset.to_u(), STORE)
}
pub const fn sb(src: Reg, offset: BitsI32<11, 0>, base: Reg) -> I {
s_type(src, base, width::B, offset.to_u(), STORE)
}
pub const fn sh(src: Reg, offset: BitsI32<11, 0>, base: Reg) -> I {
s_type(src, base, width::H, offset.to_u(), STORE)
}
pub const fn sw(src: Reg, offset: BitsI32<11, 0>, base: Reg) -> I {
s_type(src, base, width::W, offset.to_u(), STORE)
}
pub const fn sd(src: Reg, offset: BitsI32<11, 0>, base: Reg) -> I {
s_type(src, base, width::D, offset.to_u(), STORE)
}
pub const fn add(dest: Reg, src1: Reg, src2: Reg) -> I {
r_type(Bits32::new(0), src2, src1, ADD, dest, OP)
}