jacobly0 / llvm-project

This fork of the canonical git mirror of the LLVM subversion repository adds (e)Z80 targets. Please refer to the wiki for important build instructions.
https://github.com/jacobly0/llvm-project/wiki
123 stars 15 forks source link

Miscompilation when passing and returning structs. #21

Closed Dragon-Hatcher closed 2 years ago

Dragon-Hatcher commented 3 years ago

Consider the following code:

#include <stdio.h>
#include <tice.h>

typedef struct {
    int a;
} Inner;

typedef struct {
    int i1;
    int i2;
} TestStruct;

void use(TestStruct ts) {
    printf("%d\n", ts.i2);
}

Inner test(TestStruct ts) {
    printf("%d\n", ts.i2);
    use(ts);
    Inner i = {0};
    return i;
}

void waitForKey() {
    uint16_t key;
    do {
        key = os_GetKey();
    } while (key != sk_Enter);
}

int main(void) {

    TestStruct ts = {1,2};
    printf("%d\n", ts.i2);
    test(ts);

    waitForKey();

    return 0;
}

It produces the output

2
1
1

It should instead produce the output

2
2
2

The first element of ts.i1 is printed instead of ts.i2 in test and use.

Here is full project that reproduces this issue: TestWeirdThing.zip