2009-08-14 17:09:24 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* coptptrload.h */
|
|
|
|
/* */
|
|
|
|
/* Optimize loads through pointers */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* (C) 2001-2009 Ullrich von Bassewitz */
|
|
|
|
/* Roemerstrasse 52 */
|
|
|
|
/* D-70794 Filderstadt */
|
|
|
|
/* EMail: uz@cc65.org */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* This software is provided 'as-is', without any expressed or implied */
|
|
|
|
/* warranty. In no event will the authors be held liable for any damages */
|
|
|
|
/* arising from the use of this software. */
|
|
|
|
/* */
|
|
|
|
/* Permission is granted to anyone to use this software for any purpose, */
|
|
|
|
/* including commercial applications, and to alter it and redistribute it */
|
|
|
|
/* freely, subject to the following restrictions: */
|
|
|
|
/* */
|
|
|
|
/* 1. The origin of this software must not be misrepresented; you must not */
|
|
|
|
/* claim that you wrote the original software. If you use this software */
|
|
|
|
/* in a product, an acknowledgment in the product documentation would be */
|
|
|
|
/* appreciated but is not required. */
|
|
|
|
/* 2. Altered source versions must be plainly marked as such, and must not */
|
|
|
|
/* be misrepresented as being the original software. */
|
|
|
|
/* 3. This notice may not be removed or altered from any source */
|
|
|
|
/* distribution. */
|
|
|
|
/* */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef COPTPTRLOAD_H
|
|
|
|
#define COPTPTRLOAD_H
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* cc65 */
|
|
|
|
#include "codeseg.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Code */
|
2009-08-14 17:09:24 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned OptPtrLoad1 (CodeSeg* S);
|
|
|
|
/* Search for the sequence:
|
2014-06-30 09:10:35 +00:00
|
|
|
**
|
|
|
|
** clc
|
|
|
|
** adc xxx
|
|
|
|
** tay
|
|
|
|
** txa
|
|
|
|
** adc yyy
|
|
|
|
** tax
|
|
|
|
** tya
|
|
|
|
** ldy
|
|
|
|
** jsr ldauidx
|
|
|
|
**
|
|
|
|
** and replace it by:
|
|
|
|
**
|
|
|
|
** clc
|
|
|
|
** adc xxx
|
|
|
|
** sta ptr1
|
|
|
|
** txa
|
|
|
|
** adc yyy
|
|
|
|
** sta ptr1+1
|
|
|
|
** ldy
|
|
|
|
** ldx #$00
|
|
|
|
** lda (ptr1),y
|
|
|
|
*/
|
2009-08-14 17:09:24 +00:00
|
|
|
|
|
|
|
unsigned OptPtrLoad2 (CodeSeg* S);
|
|
|
|
/* Search for the sequence:
|
2014-06-30 09:10:35 +00:00
|
|
|
**
|
|
|
|
** adc xxx
|
|
|
|
** pha
|
|
|
|
** txa
|
|
|
|
** iny
|
|
|
|
** adc yyy
|
|
|
|
** tax
|
|
|
|
** pla
|
|
|
|
** ldy
|
|
|
|
** jsr ldauidx
|
|
|
|
**
|
|
|
|
** and replace it by:
|
|
|
|
**
|
|
|
|
** adc xxx
|
|
|
|
** sta ptr1
|
|
|
|
** txa
|
|
|
|
** iny
|
|
|
|
** adc yyy
|
|
|
|
** sta ptr1+1
|
|
|
|
** ldy
|
|
|
|
** ldx #$00
|
|
|
|
** lda (ptr1),y
|
|
|
|
*/
|
2009-08-14 17:09:24 +00:00
|
|
|
|
|
|
|
unsigned OptPtrLoad3 (CodeSeg* S);
|
|
|
|
/* Search for the sequence:
|
2014-06-30 09:10:35 +00:00
|
|
|
**
|
|
|
|
** lda #<(label+0)
|
|
|
|
** ldx #>(label+0)
|
|
|
|
** clc
|
|
|
|
** adc xxx
|
|
|
|
** bcc L
|
|
|
|
** inx
|
|
|
|
** L: ldy #$00
|
|
|
|
** jsr ldauidx
|
|
|
|
**
|
|
|
|
** and replace it by:
|
|
|
|
**
|
|
|
|
** ldy xxx
|
|
|
|
** ldx #$00
|
|
|
|
** lda label,y
|
|
|
|
*/
|
2009-08-14 17:09:24 +00:00
|
|
|
|
|
|
|
unsigned OptPtrLoad4 (CodeSeg* S);
|
|
|
|
/* Search for the sequence:
|
2014-06-30 09:10:35 +00:00
|
|
|
**
|
|
|
|
** lda #<(label+0)
|
|
|
|
** ldx #>(label+0)
|
|
|
|
** ldy #$xx
|
|
|
|
** clc
|
|
|
|
** adc (sp),y
|
|
|
|
** bcc L
|
|
|
|
** inx
|
|
|
|
** L: ldy #$00
|
|
|
|
** jsr ldauidx
|
|
|
|
**
|
|
|
|
** and replace it by:
|
|
|
|
**
|
|
|
|
** ldy #$xx
|
|
|
|
** lda (sp),y
|
|
|
|
** tay
|
|
|
|
** ldx #$00
|
|
|
|
** lda label,y
|
|
|
|
*/
|
2009-08-14 17:09:24 +00:00
|
|
|
|
|
|
|
unsigned OptPtrLoad5 (CodeSeg* S);
|
2009-08-16 20:56:33 +00:00
|
|
|
/* Search for the sequence:
|
2014-06-30 09:10:35 +00:00
|
|
|
**
|
|
|
|
** jsr pushax
|
|
|
|
** ldx #$00
|
|
|
|
** lda yyy
|
|
|
|
** jsr tosaddax
|
|
|
|
** ldy #$00
|
|
|
|
** jsr ldauidx
|
|
|
|
**
|
|
|
|
** and replace it by:
|
|
|
|
**
|
|
|
|
** sta ptr1
|
|
|
|
** stx ptr1+1
|
|
|
|
** ldy yyy
|
|
|
|
** lda (ptr1),y
|
|
|
|
*/
|
2009-08-16 20:56:33 +00:00
|
|
|
|
|
|
|
unsigned OptPtrLoad6 (CodeSeg* S);
|
|
|
|
/* Search for the sequence:
|
2014-06-30 09:10:35 +00:00
|
|
|
**
|
|
|
|
** jsr pushax
|
|
|
|
** ldy xxx
|
|
|
|
** ldx #$00
|
|
|
|
** lda (sp),y
|
|
|
|
** jsr tosaddax
|
|
|
|
** ldy #$00
|
|
|
|
** jsr ldauidx
|
|
|
|
**
|
|
|
|
** and replace it by:
|
|
|
|
**
|
|
|
|
** sta ptr1
|
|
|
|
** stx ptr1+1
|
|
|
|
** ldy xxx
|
|
|
|
** lda (sp),y
|
|
|
|
** tay
|
|
|
|
** lda (ptr1),y
|
|
|
|
*/
|
2009-08-16 20:56:33 +00:00
|
|
|
|
|
|
|
unsigned OptPtrLoad7 (CodeSeg* S);
|
|
|
|
/* Search for the sequence:
|
2014-06-30 09:10:35 +00:00
|
|
|
**
|
|
|
|
** jsr aslax1/shlax1
|
|
|
|
** clc
|
|
|
|
** adc xxx
|
|
|
|
** tay
|
|
|
|
** txa
|
|
|
|
** adc yyy
|
|
|
|
** tax
|
|
|
|
** tya
|
|
|
|
** ldy zzz
|
|
|
|
** jsr ldaxidx
|
|
|
|
**
|
|
|
|
** and replace it by:
|
|
|
|
**
|
|
|
|
** stx tmp1
|
|
|
|
** asl a
|
|
|
|
** rol tmp1
|
|
|
|
** clc
|
|
|
|
** adc xxx
|
|
|
|
** sta ptr1
|
|
|
|
** lda tmp1
|
|
|
|
** adc yyy
|
|
|
|
** sta ptr1+1
|
|
|
|
** ldy zzz
|
|
|
|
** lda (ptr1),y
|
|
|
|
** tax
|
|
|
|
** dey
|
|
|
|
** lda (ptr1),y
|
|
|
|
*/
|
2009-08-16 20:56:33 +00:00
|
|
|
|
|
|
|
unsigned OptPtrLoad11 (CodeSeg* S);
|
2009-08-14 17:09:24 +00:00
|
|
|
/* Search for the sequence:
|
2014-06-30 09:10:35 +00:00
|
|
|
**
|
|
|
|
** clc
|
|
|
|
** adc xxx
|
|
|
|
** bcc L
|
|
|
|
** inx
|
|
|
|
** L: ldy #$00
|
|
|
|
** jsr ldauidx
|
|
|
|
**
|
|
|
|
** and replace it by:
|
|
|
|
**
|
|
|
|
** ldy xxx
|
|
|
|
** sta ptr1
|
|
|
|
** stx ptr1+1
|
|
|
|
** ldx #$00
|
|
|
|
** lda (ptr1),y
|
|
|
|
*/
|
2009-08-14 17:09:24 +00:00
|
|
|
|
2009-08-16 20:56:33 +00:00
|
|
|
unsigned OptPtrLoad12 (CodeSeg* S);
|
2021-02-15 10:37:01 +00:00
|
|
|
/* Search for the sequence:
|
2014-06-30 09:10:35 +00:00
|
|
|
**
|
|
|
|
** lda regbank+n
|
|
|
|
** ldx regbank+n+1
|
|
|
|
** sta regsave
|
|
|
|
** stx regsave+1
|
|
|
|
** clc
|
|
|
|
** adc #$01
|
|
|
|
** bcc L0005
|
|
|
|
** inx
|
|
|
|
** L: sta regbank+n
|
|
|
|
** stx regbank+n+1
|
|
|
|
** lda regsave
|
|
|
|
** ldx regsave+1
|
|
|
|
** ldy #$00
|
|
|
|
** jsr ldauidx
|
|
|
|
**
|
|
|
|
** and replace it by:
|
|
|
|
**
|
|
|
|
** ldy #$00
|
|
|
|
** ldx #$00
|
|
|
|
** lda (regbank+n),y
|
|
|
|
** inc regbank+n
|
|
|
|
** bne L1
|
|
|
|
** inc regbank+n+1
|
|
|
|
** L1: tay <- only if flags are used
|
|
|
|
**
|
|
|
|
** This function must execute before OptPtrLoad7!
|
|
|
|
**
|
|
|
|
*/
|
2009-08-14 17:09:24 +00:00
|
|
|
|
2009-08-16 20:56:33 +00:00
|
|
|
unsigned OptPtrLoad13 (CodeSeg* S);
|
2009-08-14 17:09:24 +00:00
|
|
|
/* Search for the sequence:
|
2014-06-30 09:10:35 +00:00
|
|
|
**
|
|
|
|
** lda zp
|
|
|
|
** ldx zp+1
|
|
|
|
** ldy xx
|
|
|
|
** jsr ldauidx
|
|
|
|
**
|
|
|
|
** and replace it by:
|
|
|
|
**
|
|
|
|
** ldy xx
|
|
|
|
** ldx #$00
|
|
|
|
** lda (zp),y
|
|
|
|
*/
|
2009-08-14 17:09:24 +00:00
|
|
|
|
2009-08-16 20:56:33 +00:00
|
|
|
unsigned OptPtrLoad14 (CodeSeg* S);
|
2009-08-14 17:09:24 +00:00
|
|
|
/* Search for the sequence:
|
2014-06-30 09:10:35 +00:00
|
|
|
**
|
|
|
|
** lda zp
|
|
|
|
** ldx zp+1
|
|
|
|
** (anything that doesn't change a/x)
|
|
|
|
** ldy xx
|
|
|
|
** jsr ldauidx
|
|
|
|
**
|
|
|
|
** and replace it by:
|
|
|
|
**
|
|
|
|
** lda zp
|
|
|
|
** ldx zp+1
|
|
|
|
** (anything that doesn't change a/x)
|
|
|
|
** ldy xx
|
|
|
|
** ldx #$00
|
|
|
|
** lda (zp),y
|
|
|
|
**
|
|
|
|
** Must execute before OptPtrLoad10!
|
|
|
|
*/
|
2009-08-14 17:09:24 +00:00
|
|
|
|
2009-08-16 20:56:33 +00:00
|
|
|
unsigned OptPtrLoad15 (CodeSeg* S);
|
2009-08-14 17:09:24 +00:00
|
|
|
/* Search for the sequence:
|
2014-06-30 09:10:35 +00:00
|
|
|
**
|
|
|
|
** lda zp
|
|
|
|
** ldx zp+1
|
|
|
|
** ldy xx
|
|
|
|
** jsr ldaxidx
|
|
|
|
**
|
|
|
|
** and replace it by:
|
|
|
|
**
|
|
|
|
** ldy xx
|
|
|
|
** lda (zp),y
|
|
|
|
** tax
|
|
|
|
** dey
|
|
|
|
** lda (zp),y
|
|
|
|
*/
|
2009-08-14 17:09:24 +00:00
|
|
|
|
2009-08-16 20:56:33 +00:00
|
|
|
unsigned OptPtrLoad16 (CodeSeg* S);
|
2009-08-14 17:09:24 +00:00
|
|
|
/* Search for the sequence
|
2014-06-30 09:10:35 +00:00
|
|
|
**
|
|
|
|
** ldy ...
|
|
|
|
** jsr ldauidx
|
|
|
|
**
|
|
|
|
** and replace it by:
|
|
|
|
**
|
|
|
|
** ldy ...
|
|
|
|
** stx ptr1+1
|
|
|
|
** sta ptr1
|
|
|
|
** ldx #$00
|
|
|
|
** lda (ptr1),y
|
|
|
|
**
|
|
|
|
** This step must be executed *after* OptPtrLoad1!
|
|
|
|
*/
|
2009-08-14 17:09:24 +00:00
|
|
|
|
2009-08-16 20:56:33 +00:00
|
|
|
unsigned OptPtrLoad17 (CodeSeg* S);
|
2009-08-15 14:20:26 +00:00
|
|
|
/* Search for the sequence
|
2014-06-30 09:10:35 +00:00
|
|
|
**
|
|
|
|
** ldy ...
|
|
|
|
** jsr ldaxidx
|
|
|
|
**
|
|
|
|
** and replace it by:
|
|
|
|
**
|
|
|
|
** ldy ...
|
|
|
|
** sta ptr1
|
|
|
|
** stx ptr1+1
|
|
|
|
** lda (ptr1),y
|
|
|
|
** tax
|
|
|
|
** dey
|
|
|
|
** lda (ptr1),y
|
|
|
|
**
|
|
|
|
** This step must be executed *after* OptPtrLoad9! While code size increases
|
|
|
|
** by more than 200%, inlining will greatly improve visibility for the
|
|
|
|
** optimizer, so often part of the code gets improved later. So we will mark
|
|
|
|
** the step with less than 200% so it gets executed when -Oi is in effect.
|
|
|
|
*/
|
2009-08-15 14:20:26 +00:00
|
|
|
|
2019-03-14 11:20:16 +00:00
|
|
|
unsigned OptPtrLoad18 (CodeSeg* S);
|
|
|
|
/* Search for the sequence:
|
|
|
|
**
|
|
|
|
** ldx #$xx
|
|
|
|
** lda #$yy
|
|
|
|
** clc
|
|
|
|
** adc xxx
|
|
|
|
** bcc L
|
|
|
|
** inx
|
|
|
|
** L: ldy #$00
|
|
|
|
** jsr ldauidx
|
|
|
|
**
|
|
|
|
** and replace it by:
|
|
|
|
**
|
|
|
|
** ldy xxx
|
|
|
|
** ldx #$00
|
|
|
|
** lda $xxyy,y
|
|
|
|
**
|
|
|
|
** This is similar to OptPtrLoad3 but works on a constant address
|
|
|
|
** instead of a label. Also, the initial X and A loads are reversed.
|
|
|
|
*/
|
2009-08-14 17:09:24 +00:00
|
|
|
|
|
|
|
|
2019-03-14 13:41:50 +00:00
|
|
|
unsigned OptPtrLoad19 (CodeSeg* S);
|
|
|
|
/* Search for the sequence:
|
|
|
|
**
|
2019-03-15 19:10:58 +00:00
|
|
|
** ldx #0
|
2019-03-16 21:35:22 +00:00
|
|
|
** and #mask (any value < 0x80)
|
2019-03-14 13:41:50 +00:00
|
|
|
** jsr aslax1/shlax1
|
|
|
|
** clc
|
|
|
|
** adc #<(label+0)
|
|
|
|
** tay
|
|
|
|
** txa
|
|
|
|
** adc #>(label+0)
|
|
|
|
** tax
|
|
|
|
** tya
|
|
|
|
** ldy #$01
|
|
|
|
** jsr ldaxidx
|
|
|
|
**
|
|
|
|
** and replace it by:
|
|
|
|
**
|
2019-03-16 21:35:22 +00:00
|
|
|
** and #mask (remove if == 0x7F)
|
|
|
|
** asl
|
|
|
|
** tay
|
|
|
|
** lda label,y
|
|
|
|
** ldx label+1,y
|
2019-03-14 13:41:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2009-08-14 17:09:24 +00:00
|
|
|
/* End of coptptrload.h */
|
|
|
|
|
2014-03-04 00:11:19 +00:00
|
|
|
#endif
|