now the generic types work too

This commit is contained in:
Jakub Doka 2024-10-22 10:17:16 +02:00
parent b0a85f44c9
commit 5aa6150c70
No known key found for this signature in database
GPG key ID: C6E9A89936B8C143
4 changed files with 144 additions and 7 deletions

View file

@ -815,7 +815,7 @@ impl Codegen {
explicit_alignment: Option<u8>, explicit_alignment: Option<u8>,
fields: &[CommentOr<StructField>], fields: &[CommentOr<StructField>],
) -> ty::Struct { ) -> ty::Struct {
let sym = pos.map(|pos| SymKey::Struct(file, pos)); let sym = pos.map(|pos| SymKey::Struct(file, pos, Default::default()));
if let Some(sym) = sym if let Some(sym) = sym
&& let Some(&ty) = self.tys.syms.get(sym, &self.tys.ins) && let Some(&ty) = self.tys.syms.get(sym, &self.tys.ins)
{ {

View file

@ -267,7 +267,7 @@ mod ty {
pub const ECA: Func = Func::MAX; pub const ECA: Func = Func::MAX;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
pub struct Tuple(pub u32); pub struct Tuple(pub u32);
impl Tuple { impl Tuple {
@ -309,7 +309,7 @@ mod ty {
Kind::Struct(s) => { Kind::Struct(s) => {
let st = &ctx.structs[s as usize]; let st = &ctx.structs[s as usize];
debug_assert_ne!(st.pos, Pos::MAX); debug_assert_ne!(st.pos, Pos::MAX);
crate::SymKey::Struct(st.file, st.pos) crate::SymKey::Struct(st.file, st.pos, st.captures)
} }
Kind::Ptr(p) => crate::SymKey::Pointer(&ctx.ptrs[p as usize]), Kind::Ptr(p) => crate::SymKey::Pointer(&ctx.ptrs[p as usize]),
Kind::Func(f) => { Kind::Func(f) => {
@ -665,7 +665,7 @@ fn emit(out: &mut Vec<u8>, (len, instr): EncodedInstr) {
#[derive(PartialEq, Eq, Hash, Clone, Copy)] #[derive(PartialEq, Eq, Hash, Clone, Copy)]
pub enum SymKey<'a> { pub enum SymKey<'a> {
Pointer(&'a Ptr), Pointer(&'a Ptr),
Struct(FileId, Pos), Struct(FileId, Pos, ty::Tuple),
FuncInst(ty::Func, ty::Tuple), FuncInst(ty::Func, ty::Tuple),
Decl(FileId, Ident), Decl(FileId, Ident),
Array(&'a Array), Array(&'a Array),
@ -772,6 +772,7 @@ struct Struct {
file: FileId, file: FileId,
size: Cell<Size>, size: Cell<Size>,
align: Cell<u8>, align: Cell<u8>,
captures: ty::Tuple,
explicit_alignment: Option<u8>, explicit_alignment: Option<u8>,
field_start: u32, field_start: u32,
} }
@ -1014,8 +1015,14 @@ trait TypeParser {
self.tys().make_array(ty, len) self.tys().make_array(ty, len)
} }
Expr::Struct { pos, fields, packed, captured, .. } => { Expr::Struct { pos, fields, packed, captured, .. } => {
assert!(captured.is_empty()); let captures_start = self.tys().tmp.args.len();
let sym = SymKey::Struct(file, pos); for &cp in captured {
let ty = self.find_local_ty(cp).expect("TODO");
self.tys().tmp.args.push(ty);
}
let captured = self.tys().pack_args(captures_start).expect("TODO");
let sym = SymKey::Struct(file, pos, captured);
let tys = self.tys(); let tys = self.tys();
if let Some(&ty) = tys.syms.get(sym, &tys.ins) { if let Some(&ty) = tys.syms.get(sym, &tys.ins) {
return ty; return ty;

View file

@ -3964,7 +3964,7 @@ mod tests {
// Incomplete Examples; // Incomplete Examples;
//comptime_pointers; //comptime_pointers;
//generic_types; generic_types;
fb_driver; fb_driver;
// Purely Testing Examples; // Purely Testing Examples;

View file

@ -0,0 +1,130 @@
deinit:
ADDI64 r254, r254, -64d
ST r31, r254, 24a, 40h
LI64 r4, 8d
LD r32, r2, 16a, 8h
CP r33, r2
MUL64 r3, r32, r4
CP r34, r33
LD r2, r34, 0a, 8h
JAL r31, r0, :free
ADDI64 r35, r254, 0d
CP r1, r35
JAL r31, r0, :new
CP r2, r33
BMC r35, r2, 24h
LD r31, r254, 24a, 40h
ADDI64 r254, r254, 64d
JALA r0, r31, 0a
free:
CP r10, r2
LRA r7, r0, :FREE_SYS_CALL
LD r2, r7, 0a, 8h
CP r5, r4
CP r4, r3
CP r3, r10
ECA
JALA r0, r31, 0a
main:
ADDI64 r254, r254, -88d
ST r31, r254, 48a, 40h
ADDI64 r32, r254, 24d
CP r1, r32
JAL r31, r0, :new
LI64 r3, 69d
ADDI64 r33, r254, 0d
BMC r32, r33, 24h
CP r2, r33
JAL r31, r0, :push
LD r34, r254, 0a, 8h
LD r35, r34, 0a, 8h
CP r2, r33
JAL r31, r0, :deinit
CP r1, r35
LD r31, r254, 48a, 40h
ADDI64 r254, r254, 88d
JALA r0, r31, 0a
malloc:
CP r9, r2
LRA r5, r0, :MALLOC_SYS_CALL
LD r2, r5, 0a, 8h
CP r4, r3
CP r3, r9
ECA
JALA r0, r31, 0a
new:
ADDI64 r254, r254, -24d
LI64 r2, 0d
ADDI64 r3, r254, 0d
ST r2, r254, 0a, 8h
ST r2, r254, 8a, 8h
ST r2, r254, 16a, 8h
BMC r3, r1, 24h
ADDI64 r254, r254, 24d
JALA r0, r31, 0a
push:
ADDI64 r254, r254, -184d
ST r31, r254, 0a, 184h
CP r32, r3
LI64 r33, 1d
LI64 r34, 8d
LD r35, r2, 16a, 8h
LD r36, r2, 8a, 8h
CP r37, r2
JNE r35, r36, :0
LI64 r38, 0d
JNE r35, r38, :1
CP r39, r33
JMP :2
1: MULI64 r39, r35, 2d
2: MUL64 r2, r39, r34
ST r39, r37, 16a, 8h
CP r3, r34
JAL r31, r0, :malloc
JNE r1, r38, :3
CP r1, r38
JMP :4
3: CP r40, r37
CP r41, r1
CP r1, r38
LD r42, r40, 8a, 8h
MUL64 r43, r42, r34
LD r44, r40, 0a, 8h
ADD64 r45, r44, r43
CP r46, r41
9: JNE r45, r44, :5
LD r46, r40, 8a, 8h
JEQ r46, r1, :6
LD r2, r40, 0a, 8h
MUL64 r3, r46, r34
CP r4, r34
JAL r31, r0, :free
CP r38, r41
JMP :7
6: CP r38, r41
7: ST r38, r40, 0a, 8h
JMP :8
5: CP r38, r41
ADDI64 r47, r44, 8d
LD r48, r44, 0a, 8h
ADDI64 r49, r46, 8d
ST r48, r46, 0a, 8h
CP r44, r47
CP r46, r49
JMP :9
0: CP r40, r37
8: LD r50, r40, 8a, 8h
MUL64 r51, r50, r34
LD r38, r40, 0a, 8h
ADD64 r1, r38, r51
CP r3, r32
ST r3, r1, 0a, 8h
LD r52, r40, 8a, 8h
ADD64 r53, r52, r33
ST r53, r40, 8a, 8h
4: LD r31, r254, 0a, 184h
ADDI64 r254, r254, 184d
JALA r0, r31, 0a
code size: 963
ret: 69
status: Ok(())