itszor / gcc-6502-bits

Build tools, tiny C library, etc. for gcc-6502 port.
61 stars 17 forks source link

static variables placed in the wrong segment #11

Closed sgadrat closed 3 years ago

sgadrat commented 3 years ago

Steps to reproduce

static int a[100];

int f(int i, int v) {
    a[i] = v;
    return *a;
}

Compile with 6502-gcc sample.c -O0 -S -o sample.s

Expected behavior

Memory for the a variable is reserved in BSS segment.

Actual behavior

Memory for the a variable is reserved in CODE segment.

    .feature at_in_identifiers
    .feature dollar_in_identifiers
    .autoimport +
    .p02
    .importzp _sp0, _sp1, _fp0, _fp1
    .importzp _r0, _r1, _r2, _r3, _r4, _r5, _r6, _r7
    .importzp _s0, _s1, _s2, _s3, _s4, _s5, _s6, _s7
    .importzp _e0, _e1, _e2, _e3, _e4, _e5, _e6, _e7
    .importzp _e8, _e9, _e10, _e11, _e12, _e13, _e14, _e15
    .importzp _e16, _e17, _e18, _e19, _e20, _e21, _e22, _e23
    .importzp _e24, _e25, _e26, _e27, _e28, _e29, _e30, _e31
    .importzp _tmp0, _tmp1
    .importzp _sa, _sx, _sy
    .segment "CODE"
a$:
    .res 200
    .export f$
f$:
; frame size 4, pretend size 0, outgoing size 0
    lda _sp1
    pha
    lda _sp0
    pha
    [...]

Additional information

Commit 67a23d5fb65f7df35558e51694dad1ab723bd824 is applied. Without it the same behavior was also observed on non-static variables.

The faulty behavior is also observable if a is declared in the function:

int f(int i, int v) {
    static int a[100];
    a[i] = v;
    return *a;
}
sgadrat commented 3 years ago

Oops, just saw it is a duplicate of https://github.com/itszor/gcc-6502/issues/8 and definitely an issue of gcc, not the bits. I close this issue.