Re: [cc65] Optimizations of structures

From: Ullrich von Bassewitz <uz1musoftware.de>
Date: 2010-09-10 15:04:09
Hi!

On Fri, Sep 10, 2010 at 01:58:46PM +0200, Jakub wrote:
> Is it possible to add a compiler switch to reorganize structures
> fields to be lineary placed in the memory?

Actually this is already the case. There is code in the compiler that will
optimze array accesses under some conditions using Y indexed addressing. But
this will fail for structures or if any of the preconditions aren't met (for
example the array not being a global one).

Something that will work quite reasonably is using a pointer stored in a
register variable (aka zeropage).

The following code:

------------------------------------------------------------------------
#define OFFSET 10
#define XSPACE 20
#define YSPACE 20

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

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

int main(void)
{
    register struct pos* q = p;

    for (line = 0; line < 8; ++line) {
      ++x;
      for (row = 0; row < 8; ++row) {
          q->x = x;
          q->y = y;
          ++q;
          x += XSPACE;
      }
      y += YSPACE;
    }
    return 0;
}
------------------------------------------------------------------------

will generate:

------------------------------------------------------------------------
;
; q->x = x;
;
	.dbg	line, "test.c", 20
	lda     _x
	ldy     #$00
	sta     (regbank+4),y
;
; q->y = y;
;
	.dbg	line, "test.c", 21
	lda     _y
	iny
	sta     (regbank+4),y
;
; ++q;
;
	.dbg	line, "test.c", 22
	lda     #$02
	clc
	adc     regbank+4
	sta     regbank+4
	bcc     L001A
	inc     regbank+4+1
------------------------------------------------------------------------

I can also have a look at code generation for structure accesses, but as said
above there are problems and it won't work in many cases, while using pointers
(as above) will always work.

I admit that it is unfortunate to write code in a special way to help a
compiler, but this will also make a difference with compilers for machines
where code generation is easier than for the 6502.

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz@musoftware.de
----------------------------------------------------------------------
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 15:04:18 2010

This archive was generated by hypermail 2.1.8 : 2010-09-10 15:04:22 CEST