Skip to content

Miscompilation when passing and returning structs. #21

Closed
@Dragon-Hatcher

Description

@Dragon-Hatcher

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingmiscompileIncorrect code was generated

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions