antimony-lang / antimony

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

QBE generator #42

Closed YerinAlexey closed 3 years ago

YerinAlexey commented 3 years ago

Description

This adds a generator for QBE. This time it should go a bit better.

Notes

Demo time

struct Person {
    beans: int
}

fn main() {
    let me = new Person {
        beans: 0
    }

    printf("before: %d beans\n", me.beans)

    me.beans += 100

    printf("after: %d beans\n", me.beans)
}

Produces:

$ ./target/debug/sb -t qbe build -o - test.sb | qbe | cc -x assembler -o test -
$ ./test
before: 0 beans
after: 100 beans

Known issues

ToDo

garritfra commented 3 years ago

Do we want to build 100% static binaries without any dependencies?

I think for now we can rely on the c stdlib as a dependency. Once the language is a bit more sophisticated, we can add our own wrappers.

garritfra commented 3 years ago

Regarding assignment operators (+=): to keep it simple, we could maybe transform that during build-time to x = x + 1. Or how easy would that be to implement?

YerinAlexey commented 3 years ago

Regarding assignment operators (+=): to keep it simple, we could maybe transform that during build-time to x = x + 1. Or how easy would that be to implement?

This should be quite easy to implement. I had the idea to have a resolve(Expression) -> QbeValue function that retrieves the value from variable, field access or array access and then use it in *Assign binops.

It should be basically this (pseudocode):

let (ty, tmp) = resolve(lhs);
generate_assign(
    lhs,
    ty,
    QbeInstr::Add(tmp, rhs),
);

Feel free to work on this in the meantime.

YerinAlexey commented 3 years ago

I rebased this on master, should still work

YerinAlexey commented 3 years ago

Any comments so far?

garritfra commented 3 years ago

Also, would you mind opening some tickets for the missing parts? That way we have them documented to be worked on later

YerinAlexey commented 3 years ago

Also, would you mind opening some tickets for the missing parts? That way we have them documented to be worked on later

Ok, sure. I'll add arrays and += now, and leave match as a ticket. This PR is already massive