[cc65] Optimizations of structures

From: Jakub <debski.jakub1wp.pl>
Date: 2010-09-10 13:58:46
Hi,

1. Would it be possible to optimize placement of structures' data in the 
memory by special compiler switch?
For example:

// cc65 -Cl -T -Osir --cpu 6502 -t atari main.c
#define OFFSET 10
#define XSPACE 20
#define YSPACE 20

struct pos {
  unsigned char x;
  unsigned char y;
};

struct pos p[64];
unsigned char idx,line,row,x,y;

void main(void)
{
  for (line=0;line<8;++line)
  {
    ++x;
    for (row=0;row<8;++row)
    {
      p[idx].y=y;
      p[idx].x=x;
      ++idx;
      x+=XSPACE;
    }
    y+=YSPACE;
  }
}


This code generates very slow access to the structure fields:

;
; p[idx].y=y;
;
 ldx     #$00
 lda     _idx
 asl     a
 bcc     L0022
 inx
 clc
L0022: adc     #<(_p)
 sta     ptr1
 txa
 adc     #>(_p)
 sta     ptr1+1
 lda     _y
 ldy     #$01
 sta     (ptr1),y
;
; p[idx].x=x;
;
 ldx     #$00
 lda     _idx
 asl     a
 bcc     L0023
 inx
 clc
L0023: adc     #<(_p)
 sta     ptr1
 txa
 adc     #>(_p)
 sta     ptr1+1
 lda     _x
 dey
 sta     (ptr1),y

With a simple replace of structures with arrays we get much faster and 
compact code:

// cc65 -Cl -T -Osir --cpu 6502 -t atari main.c
#define OFFSET 10
#define XSPACE 20
#define YSPACE 20

unsigned char px[64];
unsigned char py[64];

void main(void)
{
  unsigned char idx,line,row,x,y;
  for (line=0;line<8;++line)
  {
    x=0;
    if (line%2!=0)
      x=OFFSET;
    for (row=0;row<8;++row)
    {
      py[idx]=y;
      px[idx]=x;
      ++idx;
      x+=XSPACE;
    }
    y+=YSPACE;
  }
}

Which gives:

;
; py[idx]=y;
;
 ldy     L0005
 lda     L0009
 sta     _py,y
;
; px[idx]=x;
;
 ldy     L0005
 lda     L0008
 sta     _px,y

(it still could be optimized: y register is loaded twice)

Is it possible to add a compiler switch to reorganize structures fields to 
be lineary placed in the memory?
I know it won't work well for all the cases (f.e. nested structures).

regards,
Jakub 

----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Fri Sep 10 13:58:53 2010

This archive was generated by hypermail 2.1.8 : 2010-09-10 13:58:56 CEST