From bef7663023309344585ecfd335d2efa19dcd786c Mon Sep 17 00:00:00 2001
From: Jesper Gravgaard <jg@ramboll.com>
Date: Tue, 16 Jul 2019 22:32:41 +0200
Subject: [PATCH] Optimized & minimized spline sample program. Added test
 demonstrating struct array initializer problem #223

---
 .../fragment/vwuz1=pwuz1_derefidx_vbuyy.asm   |  7 ++++++
 .../dk/camelot64/kickc/test/TestPrograms.java |  8 +++++++
 src/test/kc/complex/splines/simple-splines.kc |  4 +---
 src/test/kc/problem-array-struct-init.kc      | 21 ++++++++++++++++++
 .../ref/complex/splines/simple-splines.asm    |  2 +-
 .../ref/complex/splines/simple-splines.log    | 22 +++++++++----------
 .../ref/complex/splines/simple-splines.sym    |  6 ++---
 7 files changed, 52 insertions(+), 18 deletions(-)
 create mode 100644 src/main/fragment/vwuz1=pwuz1_derefidx_vbuyy.asm
 create mode 100644 src/test/kc/problem-array-struct-init.kc

diff --git a/src/main/fragment/vwuz1=pwuz1_derefidx_vbuyy.asm b/src/main/fragment/vwuz1=pwuz1_derefidx_vbuyy.asm
new file mode 100644
index 000000000..117f5eede
--- /dev/null
+++ b/src/main/fragment/vwuz1=pwuz1_derefidx_vbuyy.asm
@@ -0,0 +1,7 @@
+lda ({z1}),y
+pha
+iny
+lda ({z1}),y
+sta {z1}+1
+pla
+sta {z1}
diff --git a/src/test/java/dk/camelot64/kickc/test/TestPrograms.java b/src/test/java/dk/camelot64/kickc/test/TestPrograms.java
index 216dcde66..2e12ca797 100644
--- a/src/test/java/dk/camelot64/kickc/test/TestPrograms.java
+++ b/src/test/java/dk/camelot64/kickc/test/TestPrograms.java
@@ -35,6 +35,13 @@ public class TestPrograms {
    public TestPrograms() {
    }
 
+   /*
+   @Test
+   public void testProblemArrayStructInit() throws IOException, URISyntaxException {
+      compileAndCompare("problem-array-struct-init");
+   }
+   */
+
    /*
    @Test
    public void testProblemInlineStructReturn() throws IOException, URISyntaxException {
@@ -42,6 +49,7 @@ public class TestPrograms {
    }
    */
 
+
    @Test
    public void testSplines() throws IOException, URISyntaxException {
       compileAndCompare("complex/splines/simple-splines");
diff --git a/src/test/kc/complex/splines/simple-splines.kc b/src/test/kc/complex/splines/simple-splines.kc
index e80961d47..f25693a0d 100644
--- a/src/test/kc/complex/splines/simple-splines.kc
+++ b/src/test/kc/complex/splines/simple-splines.kc
@@ -10,10 +10,8 @@ const char* PRINT_SCREEN = 0x0400;
 const char* BITMAP_SCREEN = 0x5c00;
 const char* BITMAP_GRAPHICS = 0x6000;
 
-enum SegmentType { MOVE_TO, SPLINE_TO, LINE_TO};
-
 struct Segment {
-    enum SegmentType type;
+    enum SegmentType { MOVE_TO, SPLINE_TO, LINE_TO} type;
     struct SplineVector16 to;
     struct SplineVector16 via;
 };
diff --git a/src/test/kc/problem-array-struct-init.kc b/src/test/kc/problem-array-struct-init.kc
new file mode 100644
index 000000000..d9f906f7c
--- /dev/null
+++ b/src/test/kc/problem-array-struct-init.kc
@@ -0,0 +1,21 @@
+// Demonstrates problem with initializing array of structs
+
+struct Point {
+    char x;
+    char y;
+};
+
+struct Point[] points = {
+    { 1, 2 },
+    { 3, 4 },
+    { 5, 6 }
+};
+
+void main() {
+    const char* SCREEN = 0x0400;
+    char idx = 0;
+    for ( char i: 0..2) {
+        SCREEN[idx++] = points[i].x;
+        SCREEN[idx++] = points[i].y;
+    }
+}
\ No newline at end of file
diff --git a/src/test/ref/complex/splines/simple-splines.asm b/src/test/ref/complex/splines/simple-splines.asm
index 25ee218e7..eb63b56da 100644
--- a/src/test/ref/complex/splines/simple-splines.asm
+++ b/src/test/ref/complex/splines/simple-splines.asm
@@ -1225,5 +1225,5 @@ bitmap_init: {
   bitmap_plot_ylo: .fill $100, 0
   bitmap_plot_yhi: .fill $100, 0
   bitmap_plot_bit: .fill $100, 0
-  // True type letter a
+  // True type letter c
   letter_c: .fill 9*$15, 0
diff --git a/src/test/ref/complex/splines/simple-splines.log b/src/test/ref/complex/splines/simple-splines.log
index e2b36f436..9a5c81640 100644
--- a/src/test/ref/complex/splines/simple-splines.log
+++ b/src/test/ref/complex/splines/simple-splines.log
@@ -1689,12 +1689,12 @@ SYMBOL TABLE SSA
 (struct SplineVector16[9]) SPLINE_8SEG
 (struct SplineVector16[9]) SPLINE_8SEG#0
 (const byte) SPLINE_TO = (byte) 1
+(const byte) Segment::SegmentType::LINE_TO = (byte) 2
+(const byte) Segment::SegmentType::MOVE_TO = (byte) 0
+(const byte) Segment::SegmentType::SPLINE_TO = (byte) 1
 (struct SplineVector16) Segment::to
 (byte) Segment::type
 (struct SplineVector16) Segment::via
-(const byte) SegmentType::LINE_TO = (byte) 2
-(const byte) SegmentType::MOVE_TO = (byte) 0
-(const byte) SegmentType::SPLINE_TO = (byte) 1
 (signed word) SplineVector16::x
 (signed word) SplineVector16::y
 (signed dword) SplineVector32::x
@@ -8112,7 +8112,7 @@ bitmap_init: {
   bitmap_plot_ylo: .fill $100, 0
   bitmap_plot_yhi: .fill $100, 0
   bitmap_plot_bit: .fill $100, 0
-  // True type letter a
+  // True type letter c
   letter_c: .fill 9*$15, 0
 
 REGISTER UPLIFT POTENTIAL REGISTERS
@@ -8691,8 +8691,8 @@ Uplift Scope [sgn_u16] 14: zp ZP_WORD:27 [ sgn_u16::w#2 sgn_u16::w#0 sgn_u16::w#
 Uplift Scope [SplineVector16] 
 Uplift Scope [SplineVector32] 
 Uplift Scope [bitmap_clear] 
-Uplift Scope [SegmentType] 
 Uplift Scope [Segment] 
+Uplift Scope [Segment::SegmentType] 
 Uplift Scope [] 
 
 Uplifting [bitmap_line] best 81770 combination zp ZP_WORD:18 [ bitmap_line::y#15 bitmap_line::y#7 bitmap_line::y#13 bitmap_line::y#4 bitmap_line::y#0 bitmap_line::y1#1 bitmap_line::y1#0 bitmap_line::y#1 bitmap_line::y#2 ] zp ZP_WORD:20 [ bitmap_line::x#7 bitmap_line::x#6 bitmap_line::x#15 bitmap_line::x#13 bitmap_line::x#0 bitmap_line::x1#1 bitmap_line::x1#0 bitmap_line::x#12 bitmap_line::x#1 ] zp ZP_WORD:16 [ bitmap_line::e#3 bitmap_line::e#0 bitmap_line::e#6 bitmap_line::e#1 bitmap_line::e#2 ] zp ZP_WORD:22 [ bitmap_line::e1#3 bitmap_line::e1#6 bitmap_line::e1#0 bitmap_line::e1#2 bitmap_line::e1#1 ] zp ZP_WORD:14 [ bitmap_line::y2#11 bitmap_line::y2#13 bitmap_line::y2#0 ] zp ZP_WORD:12 [ bitmap_line::x2#10 bitmap_line::x2#13 bitmap_line::x2#0 ] zp ZP_WORD:85 [ bitmap_line::dy#0 ] zp ZP_WORD:93 [ bitmap_line::sy#0 ] zp ZP_WORD:81 [ bitmap_line::dx#0 ] zp ZP_WORD:89 [ bitmap_line::sx#0 ] 
@@ -8708,8 +8708,8 @@ Uplifting [sgn_u16] best 77601 combination zp ZP_WORD:27 [ sgn_u16::w#2 sgn_u16:
 Uplifting [SplineVector16] best 77601 combination 
 Uplifting [SplineVector32] best 77601 combination 
 Uplifting [bitmap_clear] best 77601 combination 
-Uplifting [SegmentType] best 77601 combination 
 Uplifting [Segment] best 77601 combination 
+Uplifting [Segment::SegmentType] best 77601 combination 
 Uplifting [] best 77601 combination 
 Attempting to uplift remaining variables inzp ZP_BYTE:11 [ bitmap_plot_spline_8seg::n#2 bitmap_plot_spline_8seg::n#1 ]
 Uplifting [bitmap_plot_spline_8seg] best 77601 combination zp ZP_BYTE:11 [ bitmap_plot_spline_8seg::n#2 bitmap_plot_spline_8seg::n#1 ] 
@@ -10649,7 +10649,7 @@ bitmap_init: {
   bitmap_plot_ylo: .fill $100, 0
   bitmap_plot_yhi: .fill $100, 0
   bitmap_plot_bit: .fill $100, 0
-  // True type letter a
+  // True type letter c
   letter_c: .fill 9*$15, 0
 
 ASSEMBLER OPTIMIZATIONS
@@ -10880,12 +10880,12 @@ FINAL SYMBOL TABLE
 (struct SplineVector16[9]) SPLINE_8SEG
 (const struct SplineVector16[9]) SPLINE_8SEG#0 SPLINE_8SEG = { fill( 9, 0) }
 (const byte) SPLINE_TO SPLINE_TO = (byte) 1
+(const byte) Segment::SegmentType::LINE_TO LINE_TO = (byte) 2
+(const byte) Segment::SegmentType::MOVE_TO MOVE_TO = (byte) 0
+(const byte) Segment::SegmentType::SPLINE_TO SPLINE_TO = (byte) 1
 (struct SplineVector16) Segment::to
 (byte) Segment::type
 (struct SplineVector16) Segment::via
-(const byte) SegmentType::LINE_TO LINE_TO = (byte) 2
-(const byte) SegmentType::MOVE_TO MOVE_TO = (byte) 0
-(const byte) SegmentType::SPLINE_TO SPLINE_TO = (byte) 1
 (signed word) SplineVector16::x
 (signed word) SplineVector16::y
 (signed dword) SplineVector32::x
@@ -13117,6 +13117,6 @@ bitmap_init: {
   bitmap_plot_ylo: .fill $100, 0
   bitmap_plot_yhi: .fill $100, 0
   bitmap_plot_bit: .fill $100, 0
-  // True type letter a
+  // True type letter c
   letter_c: .fill 9*$15, 0
 
diff --git a/src/test/ref/complex/splines/simple-splines.sym b/src/test/ref/complex/splines/simple-splines.sym
index ad79b94a8..e5c4d6177 100644
--- a/src/test/ref/complex/splines/simple-splines.sym
+++ b/src/test/ref/complex/splines/simple-splines.sym
@@ -26,12 +26,12 @@
 (struct SplineVector16[9]) SPLINE_8SEG
 (const struct SplineVector16[9]) SPLINE_8SEG#0 SPLINE_8SEG = { fill( 9, 0) }
 (const byte) SPLINE_TO SPLINE_TO = (byte) 1
+(const byte) Segment::SegmentType::LINE_TO LINE_TO = (byte) 2
+(const byte) Segment::SegmentType::MOVE_TO MOVE_TO = (byte) 0
+(const byte) Segment::SegmentType::SPLINE_TO SPLINE_TO = (byte) 1
 (struct SplineVector16) Segment::to
 (byte) Segment::type
 (struct SplineVector16) Segment::via
-(const byte) SegmentType::LINE_TO LINE_TO = (byte) 2
-(const byte) SegmentType::MOVE_TO MOVE_TO = (byte) 0
-(const byte) SegmentType::SPLINE_TO SPLINE_TO = (byte) 1
 (signed word) SplineVector16::x
 (signed word) SplineVector16::y
 (signed dword) SplineVector32::x