diff --git a/src/Makefile b/src/Makefile
index 034a2230f..4b993c881 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -66,7 +66,7 @@ ifndef BUILD_ID
endif
$(info BUILD_ID: $(BUILD_ID))
-CFLAGS += -MMD -MP -O3 -I common \
+CFLAGS += -MMD -MP -O0 -g -I common \
-Wall -Wextra -Wno-char-subscripts $(USER_CFLAGS) \
-DCA65_INC="\"$(CA65_INC)\"" -DCC65_INC="\"$(CC65_INC)\"" -DCL65_TGT="\"$(CL65_TGT)\"" \
-DLD65_LIB="\"$(LD65_LIB)\"" -DLD65_OBJ="\"$(LD65_OBJ)\"" -DLD65_CFG="\"$(LD65_CFG)\"" \
diff --git a/src/cc65.vcxproj b/src/cc65.vcxproj
index 6f3f8e4e4..9c1719538 100644
--- a/src/cc65.vcxproj
+++ b/src/cc65.vcxproj
@@ -69,6 +69,7 @@
+
@@ -150,6 +151,7 @@
+
@@ -214,4 +216,4 @@
-
\ No newline at end of file
+
diff --git a/src/cc65/codegen.c b/src/cc65/codegen.c
index 2721f8e96..db487ca40 100644
--- a/src/cc65/codegen.c
+++ b/src/cc65/codegen.c
@@ -705,7 +705,6 @@ void g_getimmed (unsigned Flags, uintptr_t Val, long Offs)
/* Load a constant into the primary register */
{
unsigned char B1, B2, B3, B4;
- unsigned Done;
if ((Flags & CF_CONST) != 0) {
@@ -731,40 +730,15 @@ void g_getimmed (unsigned Flags, uintptr_t Val, long Offs)
B3 = (unsigned char) (Val >> 16);
B4 = (unsigned char) (Val >> 24);
- /* Remember which bytes are done */
- Done = 0;
-
- /* Load the value */
- AddCodeLine ("ldx #$%02X", B2);
- Done |= 0x02;
- if (B2 == B3) {
- AddCodeLine ("stx sreg");
- Done |= 0x04;
- }
- if (B2 == B4) {
- AddCodeLine ("stx sreg+1");
- Done |= 0x08;
- }
- if ((Done & 0x04) == 0 && B1 != B3) {
- AddCodeLine ("lda #$%02X", B3);
- AddCodeLine ("sta sreg");
- Done |= 0x04;
- }
- if ((Done & 0x08) == 0 && B1 != B4) {
- AddCodeLine ("lda #$%02X", B4);
- AddCodeLine ("sta sreg+1");
- Done |= 0x08;
- }
+ /* Load the value. Don't be too smart here and let
+ * the optimizer do its job.
+ */
+ AddCodeLine ("lda #$%02X", B4);
+ AddCodeLine ("sta sreg+1");
+ AddCodeLine ("lda #$%02X", B3);
+ AddCodeLine ("sta sreg");
AddCodeLine ("lda #$%02X", B1);
- Done |= 0x01;
- if ((Done & 0x04) == 0) {
- CHECK (B1 == B3);
- AddCodeLine ("sta sreg");
- }
- if ((Done & 0x08) == 0) {
- CHECK (B1 == B4);
- AddCodeLine ("sta sreg+1");
- }
+ AddCodeLine ("ldx #$%02X", B2);
break;
default:
diff --git a/src/cc65/codeopt.c b/src/cc65/codeopt.c
index 3a2c2a35d..c9c1592bc 100644
--- a/src/cc65/codeopt.c
+++ b/src/cc65/codeopt.c
@@ -57,6 +57,7 @@
#include "coptcmp.h"
#include "coptind.h"
#include "coptjmp.h"
+#include "coptlong.h"
#include "coptmisc.h"
#include "coptptrload.h"
#include "coptptrstore.h"
@@ -150,6 +151,8 @@ static OptFunc DOptJumpTarget3 = { OptJumpTarget3, "OptJumpTarget3", 100, 0,
static OptFunc DOptLoad1 = { OptLoad1, "OptLoad1", 100, 0, 0, 0, 0, 0 };
static OptFunc DOptLoad2 = { OptLoad2, "OptLoad2", 200, 0, 0, 0, 0, 0 };
static OptFunc DOptLoad3 = { OptLoad3, "OptLoad3", 0, 0, 0, 0, 0, 0 };
+static OptFunc DOptLongAssign = { OptLongAssign, "OptLongAssign", 100, 0, 0, 0, 0, 0 };
+static OptFunc DOptLongCopy = { OptLongCopy, "OptLongCopy", 100, 0, 0, 0, 0, 0 };
static OptFunc DOptNegAX1 = { OptNegAX1, "OptNegAX1", 165, 0, 0, 0, 0, 0 };
static OptFunc DOptNegAX2 = { OptNegAX2, "OptNegAX2", 200, 0, 0, 0, 0, 0 };
static OptFunc DOptPrecalc = { OptPrecalc, "OptPrecalc", 100, 0, 0, 0, 0, 0 };
@@ -262,6 +265,8 @@ static OptFunc* OptFuncs[] = {
&DOptLoad1,
&DOptLoad2,
&DOptLoad3,
+ &DOptLongAssign,
+ &DOptLongCopy,
&DOptNegAX1,
&DOptNegAX2,
&DOptPrecalc,
@@ -632,6 +637,7 @@ static unsigned RunOptGroup1 (CodeSeg* S)
Changes += RunOptFunc (S, &DOptAdd6, 1);
Changes += RunOptFunc (S, &DOptSub1, 1);
Changes += RunOptFunc (S, &DOptSub3, 1);
+ Changes += RunOptFunc (S, &DOptLongAssign, 1);
Changes += RunOptFunc (S, &DOptStore4, 1);
Changes += RunOptFunc (S, &DOptStore5, 1);
Changes += RunOptFunc (S, &DOptShift1, 1);
@@ -641,6 +647,7 @@ static unsigned RunOptGroup1 (CodeSeg* S)
Changes += RunOptFunc (S, &DOptStore1, 1);
Changes += RunOptFunc (S, &DOptStore2, 5);
Changes += RunOptFunc (S, &DOptStore3, 5);
+ Changes += RunOptFunc (S, &DOptLongCopy, 1);
/* Return the number of changes */
return Changes;
diff --git a/src/cc65/coptlong.c b/src/cc65/coptlong.c
new file mode 100644
index 000000000..16f089e49
--- /dev/null
+++ b/src/cc65/coptlong.c
@@ -0,0 +1,210 @@
+/*****************************************************************************/
+/* */
+/* coptlong.c */
+/* */
+/* Long integers optimizations */
+/* */
+/* */
+/* */
+/* (C) 2001-2009, Ullrich von Bassewitz */
+/* Roemerstrasse 52 */
+/* D-70794 Filderstadt */
+/* EMail: uz@cc65.org */
+/* (C) 2023, Colin Leroy-Mira OPC == OP65_LDA &&
+ L[1]->OPC == OP65_STA &&
+ !strcmp (L[1]->Arg, "sreg+1") &&
+ L[2]->OPC == OP65_LDA &&
+ L[3]->OPC == OP65_STA &&
+ !strcmp (L[3]->Arg, "sreg") &&
+ L[4]->OPC == OP65_LDA &&
+ L[5]->OPC == OP65_LDX &&
+ L[6]->OPC == OP65_STA &&
+ L[7]->OPC == OP65_STX &&
+ !strncmp(L[7]->Arg, L[6]->Arg, strlen(L[6]->Arg)) &&
+ !strcmp(L[7]->Arg + strlen(L[6]->Arg), "+1") &&
+ L[8]->OPC == OP65_LDY &&
+ !strcmp (L[8]->Arg, "sreg") &&
+ L[9]->OPC == OP65_STY &&
+ !strncmp(L[9]->Arg, L[6]->Arg, strlen(L[6]->Arg)) &&
+ !strcmp(L[9]->Arg + strlen(L[6]->Arg), "+2") &&
+ L[10]->OPC == OP65_LDY &&
+ !strcmp (L[10]->Arg, "sreg+1") &&
+ L[11]->OPC == OP65_STY &&
+ !strncmp(L[11]->Arg, L[6]->Arg, strlen(L[6]->Arg)) &&
+ !strcmp(L[11]->Arg + strlen(L[6]->Arg), "+3") &&
+ !RegXUsed (S, I+11)) {
+
+ L[1]->AM = L[11]->AM;
+ CE_SetArg(L[1], L[11]->Arg);
+
+ L[3]->AM = L[9]->AM;
+ CE_SetArg(L[3], L[9]->Arg);
+
+ CS_DelEntries (S, I+8, 4);
+
+ /* Remember, we had changes */
+ ++Changes;
+ }
+ }
+
+ /* Next entry */
+ ++I;
+
+ }
+
+ /* Return the number of changes made */
+ return Changes;
+}
+
+unsigned OptLongCopy (CodeSeg* S)
+/* Simplify long copies.
+** Recognize
+** lda XXX+3 0
+** sta sreg+1 1
+** lda XXX+2 2
+** sta sreg 3
+** ldx XXX+1 4
+** lda XXX 5
+** sta YYY 6
+** stx YYY+1 7
+** ldy sreg 8
+** sty YYY+2 9
+** ldy sreg+1 10
+** sty YYY+3 11
+** and simplify if not used right after.
+*/
+{
+ unsigned Changes = 0;
+
+ /* Walk over the entries */
+ unsigned I = 0;
+ while (I < CS_GetEntryCount (S)) {
+
+ CodeEntry* L[12];
+
+ /* Get next entry */
+ L[0] = CS_GetEntry (S, I);
+
+ if (CS_GetEntries (S, L+1, I+1, 11)) {
+ if (L[0]->OPC == OP65_LDA &&
+ !strncmp(L[0]->Arg, L[5]->Arg, strlen(L[5]->Arg)) &&
+ !strcmp(L[0]->Arg + strlen(L[5]->Arg), "+3") &&
+ L[1]->OPC == OP65_STA &&
+ !strcmp (L[1]->Arg, "sreg+1") &&
+ L[2]->OPC == OP65_LDA &&
+ !strncmp(L[2]->Arg, L[5]->Arg, strlen(L[5]->Arg)) &&
+ !strcmp(L[2]->Arg + strlen(L[5]->Arg), "+2") &&
+ L[3]->OPC == OP65_STA &&
+ !strcmp (L[3]->Arg, "sreg") &&
+ L[4]->OPC == OP65_LDX &&
+ !strncmp(L[4]->Arg, L[5]->Arg, strlen(L[5]->Arg)) &&
+ !strcmp(L[4]->Arg + strlen(L[5]->Arg), "+1") &&
+ L[5]->OPC == OP65_LDA &&
+ L[6]->OPC == OP65_STA &&
+ L[7]->OPC == OP65_STX &&
+ !strncmp(L[7]->Arg, L[6]->Arg, strlen(L[6]->Arg)) &&
+ !strcmp(L[7]->Arg + strlen(L[6]->Arg), "+1") &&
+ L[8]->OPC == OP65_LDY &&
+ !strcmp (L[8]->Arg, "sreg") &&
+ L[9]->OPC == OP65_STY &&
+ !strncmp(L[9]->Arg, L[6]->Arg, strlen(L[6]->Arg)) &&
+ !strcmp(L[9]->Arg + strlen(L[6]->Arg), "+2") &&
+ L[10]->OPC == OP65_LDY &&
+ !strcmp (L[10]->Arg, "sreg+1") &&
+ L[11]->OPC == OP65_STY &&
+ !strncmp(L[11]->Arg, L[6]->Arg, strlen(L[6]->Arg)) &&
+ !strcmp(L[11]->Arg + strlen(L[6]->Arg), "+3") &&
+ !RegXUsed (S, I+11)) {
+
+ L[1]->AM = L[11]->AM;
+ CE_SetArg(L[1], L[11]->Arg);
+
+ L[3]->AM = L[9]->AM;
+ CE_SetArg(L[3], L[9]->Arg);
+
+ CS_DelEntries (S, I+8, 4);
+
+ /* Remember, we had changes */
+ ++Changes;
+ }
+ }
+
+ /* Next entry */
+ ++I;
+
+ }
+
+ /* Return the number of changes made */
+ return Changes;
+}
diff --git a/src/cc65/coptlong.h b/src/cc65/coptlong.h
new file mode 100644
index 000000000..c0233c299
--- /dev/null
+++ b/src/cc65/coptlong.h
@@ -0,0 +1,63 @@
+/*****************************************************************************/
+/* */
+/* coptlong.h */
+/* */
+/* Long integers optimizations */
+/* */
+/* */
+/* */
+/* (C) 2001-2009, Ullrich von Bassewitz */
+/* Roemerstrasse 52 */
+/* D-70794 Filderstadt */
+/* EMail: uz@cc65.org */
+/* (C) 2023, Colin Leroy-Mira
+#include
+
+int res = 0;
+
+int main(void)
+{
+ long a, b;
+
+ a = 0x12345678L;
+
+ /* Test assignment */
+ b = a;
+ if (b != a) {
+ res++;
+ }
+
+ /* Test increment */
+ b++;
+ if (b != 0x12345679L) {
+ res++;
+ }
+
+ /* Test decrement */
+ b--;
+ if (b != 0x12345678L) {
+ res++;
+ }
+
+ /* Test pre-decrement with test */
+ if (--b != 0x12345677L) {
+ res++;
+ }
+
+ a = --b;
+ if (a != 0x12345676L) {
+ res++;
+ }
+
+ return res;
+}
diff --git a/test/val/static-long.c b/test/val/static-long.c
new file mode 100644
index 000000000..a2e4e53a3
--- /dev/null
+++ b/test/val/static-long.c
@@ -0,0 +1,41 @@
+#include
+#include
+
+int res = 0;
+
+int main(void)
+{
+ static long a, b;
+
+ a = 0x12345678L;
+
+ /* Test assignment */
+ b = a;
+ if (b != a) {
+ res++;
+ }
+
+ /* Test increment */
+ b++;
+ if (b != 0x12345679L) {
+ res++;
+ }
+
+ /* Test decrement */
+ b--;
+ if (b != 0x12345678L) {
+ res++;
+ }
+
+ /* Test pre-decrement with test */
+ if (--b != 0x12345677L) {
+ res++;
+ }
+
+ a = --b;
+ if (a != 0x12345676L) {
+ res++;
+ }
+
+ return res;
+}