fixing miscompilation of generic struct functions

Signed-off-by: Jakub Doka <jakub.doka2@gmail.com>
This commit is contained in:
Jakub Doka 2024-11-30 16:30:58 +01:00
parent e7cd2c0129
commit 8ea6c5cfcc
No known key found for this signature in database
GPG key ID: C6E9A89936B8C143
5 changed files with 166 additions and 46 deletions

View file

@ -615,9 +615,12 @@ Vec := fn($Elem: type): type return struct {
main := fn(): uint { main := fn(): uint {
vec := Vec(uint).new() vec := Vec(uint).new()
_ = vec.push(69) _ = vec.push(35)
defer vec.deinit() defer vec.deinit()
return *vec.data vec2 := Vec(u8).new()
_ = vec2.push(34)
defer vec2.deinit()
return *vec.data + *vec2.data
} }
``` ```

View file

@ -246,11 +246,6 @@ pub struct CantLoadFile {
source: io::Error, source: io::Error,
} }
pub fn parse_from_fs(
extra_threads: usize,
root: &str,
resolve: PathResolver,
) -> io::Result<Loaded> {
impl core::fmt::Display for CantLoadFile { impl core::fmt::Display for CantLoadFile {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "can't load file: {}", display_rel_path(&self.path),) write!(f, "can't load file: {}", display_rel_path(&self.path),)
@ -269,6 +264,11 @@ pub fn parse_from_fs(
} }
} }
pub fn parse_from_fs(
extra_threads: usize,
root: &str,
resolve: PathResolver,
) -> io::Result<Loaded> {
type Task = (usize, PathBuf); type Task = (usize, PathBuf);
let seen_modules = Mutex::new(HashMap::<PathBuf, usize>::default()); let seen_modules = Mutex::new(HashMap::<PathBuf, usize>::default());
@ -331,10 +331,10 @@ pub fn parse_from_fs(
) )
})?; })?;
let mut embeds = embeds.lock().unwrap(); let mut embeds = embeds.lock().unwrap();
if id as usize >= embeds.len() { if id >= embeds.len() {
embeds.resize(id as usize + 1, Default::default()); embeds.resize(id + 1, Default::default());
} }
embeds[id as usize] = content; embeds[id] = content;
Ok(id) Ok(id)
} }
} }

View file

@ -371,16 +371,16 @@ pub mod ty {
// TODO: merge base and sig // TODO: merge base and sig
crate::SymKey::FuncInst(base, fc.sig.unwrap().args) crate::SymKey::FuncInst(base, fc.sig.unwrap().args)
} else { } else {
crate::SymKey::Decl(fc.file, fc.name) crate::SymKey::Decl(fc.parent, fc.name)
} }
} }
Kind::Global(g) => { Kind::Global(g) => {
let gb = &ctx.globals[g]; let gb = &ctx.globals[g];
crate::SymKey::Decl(gb.file, gb.name) crate::SymKey::Decl(gb.file.into(), gb.name)
} }
Kind::Slice(s) => crate::SymKey::Array(&ctx.slices[s]), Kind::Slice(s) => crate::SymKey::Array(&ctx.slices[s]),
Kind::Module(_) | Kind::Builtin(_) => { Kind::Module(_) | Kind::Builtin(_) => {
crate::SymKey::Decl(Module::default(), Ident::INVALID) crate::SymKey::Decl(Module::default().into(), Ident::INVALID)
} }
Kind::Const(c) => crate::SymKey::Constant(&ctx.consts[c]), Kind::Const(c) => crate::SymKey::Constant(&ctx.consts[c]),
} }
@ -809,7 +809,7 @@ pub enum SymKey<'a> {
Struct(Module, Pos, ty::Tuple), Struct(Module, Pos, ty::Tuple),
Enum(Module, Pos), Enum(Module, Pos),
FuncInst(ty::Func, ty::Tuple), FuncInst(ty::Func, ty::Tuple),
Decl(Module, Ident), Decl(ty::Id, Ident),
Array(&'a Array), Array(&'a Array),
Constant(&'a Const), Constant(&'a Const),
} }

View file

@ -379,7 +379,6 @@ impl Nodes {
antideps[cursor as usize] = load; antideps[cursor as usize] = load;
if self[cursor].clobbers.get(aclass as _) { if self[cursor].clobbers.get(aclass as _) {
min = self[cursor].inputs[0]; min = self[cursor].inputs[0];
break;
} }
cursor = self.idom(cursor, Some(scheds)); cursor = self.idom(cursor, Some(scheds));
} }
@ -5427,7 +5426,7 @@ impl<'a> Codegen<'a> {
{ {
ty ty
} else if let Ok(id) = id } else if let Ok(id) = id
&& let Some(&ty) = self.tys.syms.get(SymKey::Decl(file, id), &self.tys.ins) && let Some(&ty) = self.tys.syms.get(SymKey::Decl(parent, id), &self.tys.ins)
{ {
self.on_reuse(ty); self.on_reuse(ty);
ty ty
@ -5452,7 +5451,7 @@ impl<'a> Codegen<'a> {
piter = match self.tys.parent_of(piter) { piter = match self.tys.parent_of(piter) {
Some(p) => p, Some(p) => p,
None => { None => {
if let ty::Kind::Struct(_) = parent.expand() { if let ty::Kind::Struct(_) = piter.expand() {
panic!(); panic!();
} }
break None; break None;
@ -5482,7 +5481,8 @@ impl<'a> Codegen<'a> {
}; };
}; };
let ty = if let Some(&ty) = self.tys.syms.get(SymKey::Decl(file, name), &self.tys.ins) { let ty = if let Some(&ty) = self.tys.syms.get(SymKey::Decl(parent, name), &self.tys.ins)
{
ty ty
} else { } else {
let (is_ct, ty) = left let (is_ct, ty) = left
@ -5509,7 +5509,7 @@ impl<'a> Codegen<'a> {
{ {
self.tys.ins.funcs[f].is_inline = true; self.tys.ins.funcs[f].is_inline = true;
} }
self.tys.syms.insert(SymKey::Decl(file, name), ty, &self.tys.ins); self.tys.syms.insert(SymKey::Decl(parent, name), ty, &self.tys.ins);
ty ty
}; };

View file

@ -15,6 +15,34 @@ deinit:
LD r31, r254, 0a, 40h LD r31, r254, 0a, 40h
ADDI64 r254, r254, 40d ADDI64 r254, r254, 40d
JALA r0, r31, 0a JALA r0, r31, 0a
deinit:
ADDI64 r254, r254, -40d
ST r31, r254, 0a, 40h
CP r32, r2
LI64 r33, 1d
LD r34, r32, 16a, 8h
LD r35, r32, 0a, 8h
CP r2, r35
CP r3, r34
CP r4, r33
JAL r31, r0, :free
CP r1, r32
JAL r31, r0, :new
LD r31, r254, 0a, 40h
ADDI64 r254, r254, 40d
JALA r0, r31, 0a
free:
CP r13, r2
CP r14, r3
CP r15, r4
LRA r16, r0, :free_sys_call
LD r16, r16, 0a, 8h
CP r2, r16
CP r3, r13
CP r4, r14
CP r5, r15
ECA
JALA r0, r31, 0a
free: free:
CP r13, r2 CP r13, r2
CP r14, r3 CP r14, r3
@ -28,23 +56,48 @@ free:
ECA ECA
JALA r0, r31, 0a JALA r0, r31, 0a
main: main:
ADDI64 r254, r254, -56d ADDI64 r254, r254, -96d
ST r31, r254, 24a, 32h ST r31, r254, 48a, 48h
ADDI64 r32, r254, 0d ADDI64 r32, r254, 24d
CP r1, r32 CP r1, r32
JAL r31, r0, :new JAL r31, r0, :new
LI64 r33, 69d LI64 r33, 35d
CP r2, r32 CP r2, r32
CP r3, r33 CP r3, r33
JAL r31, r0, :push JAL r31, r0, :push
CP r33, r1 ADDI64 r33, r254, 0d
LD r34, r254, 0a, 8h CP r1, r33
LD r33, r34, 0a, 8h JAL r31, r0, :new
LI8 r34, 34b
CP r2, r33
CP r3, r34
JAL r31, r0, :push
CP r34, r1
LD r35, r254, 0a, 8h
LD r35, r35, 0a, 1h
LD r36, r254, 24a, 8h
LD r34, r36, 0a, 8h
CP r2, r33
JAL r31, r0, :deinit
CP r2, r32 CP r2, r32
JAL r31, r0, :deinit JAL r31, r0, :deinit
CP r1, r33 ANDI r32, r35, 255d
LD r31, r254, 24a, 32h ADD64 r32, r34, r32
ADDI64 r254, r254, 56d CP r1, r32
LD r31, r254, 48a, 48h
ADDI64 r254, r254, 96d
JALA r0, r31, 0a
malloc:
CP r13, r2
CP r14, r3
LRA r15, r0, :malloc_sys_call
LD r15, r15, 0a, 8h
CP r2, r15
CP r3, r13
CP r4, r14
ECA
CP r13, r1
CP r1, r13
JALA r0, r31, 0a JALA r0, r31, 0a
malloc: malloc:
CP r13, r2 CP r13, r2
@ -60,14 +113,78 @@ malloc:
JALA r0, r31, 0a JALA r0, r31, 0a
new: new:
ADDI64 r254, r254, -24d ADDI64 r254, r254, -24d
CP r14, r1 CP r15, r1
LI64 r14, 8d
ADDI64 r13, r254, 0d ADDI64 r13, r254, 0d
ST r0, r254, 0a, 8h ST r14, r254, 0a, 8h
ST r0, r254, 8a, 8h ST r0, r254, 8a, 8h
ST r0, r254, 16a, 8h ST r0, r254, 16a, 8h
BMC r13, r14, 24h BMC r13, r15, 24h
ADDI64 r254, r254, 24d ADDI64 r254, r254, 24d
JALA r0, r31, 0a JALA r0, r31, 0a
new:
ADDI64 r254, r254, -24d
CP r15, r1
LI64 r14, 1d
ADDI64 r13, r254, 0d
ST r14, r254, 0a, 8h
ST r0, r254, 8a, 8h
ST r0, r254, 16a, 8h
BMC r13, r15, 24h
ADDI64 r254, r254, 24d
JALA r0, r31, 0a
push:
ADDI64 r254, r254, -80d
ST r31, r254, 0a, 80h
CP r38, r2
CP r39, r3
LI64 r37, 1d
LD r33, r38, 8a, 8h
LD r32, r38, 16a, 8h
JNE r32, r33, :0
JNE r32, r0, :1
CP r32, r37
JMP :2
1: MULI64 r32, r32, 2d
2: CP r2, r32
CP r3, r37
JAL r31, r0, :malloc
CP r35, r1
ST r32, r38, 16a, 8h
JNE r35, r0, :3
CP r1, r0
JMP :4
3: LD r32, r38, 0a, 8h
ADD64 r40, r33, r32
CP r34, r35
7: LD r33, r38, 0a, 8h
LD r36, r38, 8a, 8h
JNE r40, r32, :5
JEQ r36, r0, :6
CP r2, r33
CP r3, r36
CP r4, r37
JAL r31, r0, :free
JMP :6
6: ST r35, r38, 0a, 8h
JMP :0
5: ADDI64 r36, r34, 1d
ADDI64 r33, r32, 1d
LD r32, r32, 0a, 1h
ST r32, r34, 0a, 1h
CP r34, r36
CP r32, r33
JMP :7
0: LD r32, r38, 0a, 8h
LD r33, r38, 8a, 8h
ADD64 r32, r33, r32
ST r39, r32, 0a, 1h
ADD64 r33, r33, r37
ST r33, r38, 8a, 8h
CP r1, r32
4: LD r31, r254, 0a, 80h
ADDI64 r254, r254, 80d
JALA r0, r31, 0a
push: push:
ADDI64 r254, r254, -88d ADDI64 r254, r254, -88d
ST r31, r254, 0a, 88h ST r31, r254, 0a, 88h
@ -125,6 +242,6 @@ push:
4: LD r31, r254, 0a, 88h 4: LD r31, r254, 0a, 88h
ADDI64 r254, r254, 88d ADDI64 r254, r254, 88d
JALA r0, r31, 0a JALA r0, r31, 0a
code size: 923 code size: 1757
ret: 69 ret: 69
status: Ok(()) status: Ok(())