antimony-lang / antimony

The Antimony programming language
https://antimony-lang.github.io/antimony/
Apache License 2.0
138 stars 6 forks source link

qbe: Improvements to structs #61

Open YerinAlexey opened 2 years ago

YerinAlexey commented 2 years ago

Description

Implements a few cool things for structs in the QBE backend. Now it should be much more usable. Unfortunately, right now it uses memcpy which means now all programs using structs implicitly depend on libc.

Aside: a proper type system would have helped here significantly.

Example (Antimony + C):

struct Bar {
    b: int
}
struct Foo {
    a: int
    bar: Bar
}

fn main() {
    let foo = new Foo {
        a: 2
        bar: new Bar {
            b: 12
        }
    }
    check(foo)
    foo.bar.b += 1
    check2(foo)
}
#include <assert.h>

struct Bar {
    int b;
};
struct Foo {
    int a;
    struct Bar bar;
};

void check(struct Foo foo)
{
    assert(foo.a == 2);
    assert(foo.bar.b == 12);
}
void check2(struct Foo foo)
{
    assert(foo.a == 2);
    assert(foo.bar.b == 13);
}
$ sb -t qbe build -o temp.ssa structs.sb
$ qbe -o temp.s temp.ssa
$ cc -o structs temp.s structs.c

ToDo

garritfra commented 2 years ago

Whoops, this PR totally went past me!!

Looks like a solid change to me. Do you mind documenting this in the changelog? Maybe add the code as a test case?

YerinAlexey commented 2 years ago

Looks like a solid change to me. Do you mind documenting this in the changelog? Maybe add the code as a test case?

I still have to figure out alignment, right now it's likely to miscompile when struct has members of different sizes