Inspired by Banshaku's recent threads, I did some analyzing and I found out that the cc65 compiler is not very efficient when it comes to pointer access, even though this has nothing to do with the architecture and could easily be avoided, if I'm not mistaken.
When I have this simple code snippet:
then this is what the compiler turns it into:
My own pointer is clearly declared as being located in the zeropage:
And yet, the compiler feels the need to always copy the pointer values to its own pointer instead of simply doing this:
Why is this the case at all? Is there any technical reason for it or is it simply an oversight by the programmer who created the parser?
Is there any way to get the compiler to change this behavior without adding inline Assembly manually?
I compiled with
cc65 -O Test.c
and the situation is the same in the old cc65 from cc65.org as well as the newer version from github.
By the way, if you do more than one variable access, like this:
Guess what:
When I have this simple code snippet:
Code:
extern unsigned char *pNumber;
#pragma zpsym("pNumber")
void __fastcall__ Test(void)
{
*pNumber = 5;
}
#pragma zpsym("pNumber")
void __fastcall__ Test(void)
{
*pNumber = 5;
}
then this is what the compiler turns it into:
Code:
lda _pNumber+1
sta ptr1+1
lda _pNumber
sta ptr1
lda #$05
ldy #$00
sta (ptr1),y
rts
sta ptr1+1
lda _pNumber
sta ptr1
lda #$05
ldy #$00
sta (ptr1),y
rts
My own pointer is clearly declared as being located in the zeropage:
Code:
#pragma zpsym("pNumber")
--> .importzp _pNumber
--> .importzp _pNumber
And yet, the compiler feels the need to always copy the pointer values to its own pointer instead of simply doing this:
Code:
lda #$05
ldy #$00
sta (_pNumber),y
rts
ldy #$00
sta (_pNumber),y
rts
Why is this the case at all? Is there any technical reason for it or is it simply an oversight by the programmer who created the parser?
Is there any way to get the compiler to change this behavior without adding inline Assembly manually?
I compiled with
cc65 -O Test.c
and the situation is the same in the old cc65 from cc65.org as well as the newer version from github.
By the way, if you do more than one variable access, like this:
Code:
*pNumber = 5;
*pNumber = 6;
*pNumber = 6;
Guess what:
Code:
lda _pNumber+1
sta ptr1+1
lda _pNumber
sta ptr1
lda #$05
ldy #$00
sta (ptr1),y
lda _pNumber+1
sta ptr1+1
lda _pNumber
sta ptr1
lda #$06
sta (ptr1),y
sta ptr1+1
lda _pNumber
sta ptr1
lda #$05
ldy #$00
sta (ptr1),y
lda _pNumber+1
sta ptr1+1
lda _pNumber
sta ptr1
lda #$06
sta (ptr1),y