From 007985111a4cbfa10ffbef63816ae187fa8e7898 Mon Sep 17 00:00:00 2001 From: jespergravgaard Date: Wed, 29 May 2019 08:05:34 +0200 Subject: [PATCH] Temporary implementation for arrays of structs --- .../pssc1_derefidx_vbuxx_mbr_0=vbuaa.asm | 2 + .../pssc1_derefidx_vbuxx_mbr_1=vbuaa.asm | 2 + .../pssc1_derefidx_vbuyy_mbr_0=vbuaa.asm | 2 + .../pssc1_derefidx_vbuyy_mbr_1=vbuaa.asm | 2 + .../fragment/vbuaa=_deref_pssc1_mbr_0.asm | 1 + .../fragment/vbuaa=_deref_pssc1_mbr_1.asm | 1 + .../vbuaa=pssc1_derefidx_vbuxx_mbr_0.asm | 1 + .../vbuaa=pssc1_derefidx_vbuxx_mbr_1.asm | 1 + .../vbuaa=pssc1_derefidx_vbuyy_mbr_0.asm | 1 + .../vbuaa=pssc1_derefidx_vbuyy_mbr_1.asm | 1 + .../AsmFragmentInstanceSpecFactory.java | 2 + .../kickc/passes/Pass4CodeGeneration.java | 10 +++-- .../dk/camelot64/kickc/test/TestPrograms.java | 5 +++ src/test/kc/struct-3.kc | 20 ++++++++++ src/test/ref/struct-3.asm | 39 +++++++++++++++++++ 15 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 src/main/fragment/pssc1_derefidx_vbuxx_mbr_0=vbuaa.asm create mode 100644 src/main/fragment/pssc1_derefidx_vbuxx_mbr_1=vbuaa.asm create mode 100644 src/main/fragment/pssc1_derefidx_vbuyy_mbr_0=vbuaa.asm create mode 100644 src/main/fragment/pssc1_derefidx_vbuyy_mbr_1=vbuaa.asm create mode 100644 src/main/fragment/vbuaa=_deref_pssc1_mbr_0.asm create mode 100644 src/main/fragment/vbuaa=_deref_pssc1_mbr_1.asm create mode 100644 src/main/fragment/vbuaa=pssc1_derefidx_vbuxx_mbr_0.asm create mode 100644 src/main/fragment/vbuaa=pssc1_derefidx_vbuxx_mbr_1.asm create mode 100644 src/main/fragment/vbuaa=pssc1_derefidx_vbuyy_mbr_0.asm create mode 100644 src/main/fragment/vbuaa=pssc1_derefidx_vbuyy_mbr_1.asm create mode 100644 src/test/kc/struct-3.kc create mode 100644 src/test/ref/struct-3.asm diff --git a/src/main/fragment/pssc1_derefidx_vbuxx_mbr_0=vbuaa.asm b/src/main/fragment/pssc1_derefidx_vbuxx_mbr_0=vbuaa.asm new file mode 100644 index 000000000..916e765e0 --- /dev/null +++ b/src/main/fragment/pssc1_derefidx_vbuxx_mbr_0=vbuaa.asm @@ -0,0 +1,2 @@ +sta {c1},x + diff --git a/src/main/fragment/pssc1_derefidx_vbuxx_mbr_1=vbuaa.asm b/src/main/fragment/pssc1_derefidx_vbuxx_mbr_1=vbuaa.asm new file mode 100644 index 000000000..778da5aff --- /dev/null +++ b/src/main/fragment/pssc1_derefidx_vbuxx_mbr_1=vbuaa.asm @@ -0,0 +1,2 @@ +sta {c1}+1,x + diff --git a/src/main/fragment/pssc1_derefidx_vbuyy_mbr_0=vbuaa.asm b/src/main/fragment/pssc1_derefidx_vbuyy_mbr_0=vbuaa.asm new file mode 100644 index 000000000..64fd3eaf2 --- /dev/null +++ b/src/main/fragment/pssc1_derefidx_vbuyy_mbr_0=vbuaa.asm @@ -0,0 +1,2 @@ +sta {c1},y + diff --git a/src/main/fragment/pssc1_derefidx_vbuyy_mbr_1=vbuaa.asm b/src/main/fragment/pssc1_derefidx_vbuyy_mbr_1=vbuaa.asm new file mode 100644 index 000000000..91a797b27 --- /dev/null +++ b/src/main/fragment/pssc1_derefidx_vbuyy_mbr_1=vbuaa.asm @@ -0,0 +1,2 @@ +sta {c1}+1,y + diff --git a/src/main/fragment/vbuaa=_deref_pssc1_mbr_0.asm b/src/main/fragment/vbuaa=_deref_pssc1_mbr_0.asm new file mode 100644 index 000000000..bee08251a --- /dev/null +++ b/src/main/fragment/vbuaa=_deref_pssc1_mbr_0.asm @@ -0,0 +1 @@ +lda {c1} \ No newline at end of file diff --git a/src/main/fragment/vbuaa=_deref_pssc1_mbr_1.asm b/src/main/fragment/vbuaa=_deref_pssc1_mbr_1.asm new file mode 100644 index 000000000..a836deba0 --- /dev/null +++ b/src/main/fragment/vbuaa=_deref_pssc1_mbr_1.asm @@ -0,0 +1 @@ +lda {c1}+1 \ No newline at end of file diff --git a/src/main/fragment/vbuaa=pssc1_derefidx_vbuxx_mbr_0.asm b/src/main/fragment/vbuaa=pssc1_derefidx_vbuxx_mbr_0.asm new file mode 100644 index 000000000..7454fa763 --- /dev/null +++ b/src/main/fragment/vbuaa=pssc1_derefidx_vbuxx_mbr_0.asm @@ -0,0 +1 @@ +lda {c1},x \ No newline at end of file diff --git a/src/main/fragment/vbuaa=pssc1_derefidx_vbuxx_mbr_1.asm b/src/main/fragment/vbuaa=pssc1_derefidx_vbuxx_mbr_1.asm new file mode 100644 index 000000000..66ef35236 --- /dev/null +++ b/src/main/fragment/vbuaa=pssc1_derefidx_vbuxx_mbr_1.asm @@ -0,0 +1 @@ +lda {c1}+1,x \ No newline at end of file diff --git a/src/main/fragment/vbuaa=pssc1_derefidx_vbuyy_mbr_0.asm b/src/main/fragment/vbuaa=pssc1_derefidx_vbuyy_mbr_0.asm new file mode 100644 index 000000000..b2b867b05 --- /dev/null +++ b/src/main/fragment/vbuaa=pssc1_derefidx_vbuyy_mbr_0.asm @@ -0,0 +1 @@ +lda {c1},y \ No newline at end of file diff --git a/src/main/fragment/vbuaa=pssc1_derefidx_vbuyy_mbr_1.asm b/src/main/fragment/vbuaa=pssc1_derefidx_vbuyy_mbr_1.asm new file mode 100644 index 000000000..5c24499d4 --- /dev/null +++ b/src/main/fragment/vbuaa=pssc1_derefidx_vbuyy_mbr_1.asm @@ -0,0 +1 @@ +lda {c1}+1,y \ No newline at end of file diff --git a/src/main/java/dk/camelot64/kickc/fragment/AsmFragmentInstanceSpecFactory.java b/src/main/java/dk/camelot64/kickc/fragment/AsmFragmentInstanceSpecFactory.java index d32b64004..c50725d1b 100644 --- a/src/main/java/dk/camelot64/kickc/fragment/AsmFragmentInstanceSpecFactory.java +++ b/src/main/java/dk/camelot64/kickc/fragment/AsmFragmentInstanceSpecFactory.java @@ -383,6 +383,8 @@ public class AsmFragmentInstanceSpecFactory { return "ppr"; } else if(elementType instanceof SymbolTypePointer) { return "ppt"; + } else if(elementType instanceof SymbolTypeStruct) { + return "pss"; } else { throw new RuntimeException("Not implemented " + type); } diff --git a/src/main/java/dk/camelot64/kickc/passes/Pass4CodeGeneration.java b/src/main/java/dk/camelot64/kickc/passes/Pass4CodeGeneration.java index e758eb361..8a3920906 100644 --- a/src/main/java/dk/camelot64/kickc/passes/Pass4CodeGeneration.java +++ b/src/main/java/dk/camelot64/kickc/passes/Pass4CodeGeneration.java @@ -6,10 +6,7 @@ import dk.camelot64.kickc.model.*; import dk.camelot64.kickc.model.operators.Operators; import dk.camelot64.kickc.model.statements.*; import dk.camelot64.kickc.model.symbols.*; -import dk.camelot64.kickc.model.types.SymbolType; -import dk.camelot64.kickc.model.types.SymbolTypeArray; -import dk.camelot64.kickc.model.types.SymbolTypePointer; -import dk.camelot64.kickc.model.types.SymbolTypeProcedure; +import dk.camelot64.kickc.model.types.*; import dk.camelot64.kickc.model.values.*; import java.util.*; @@ -438,6 +435,11 @@ public class Pass4CodeGeneration { String asmSize = AsmFormat.getAsmConstant(program, new ConstantBinary(new ConstantInteger(2L), Operators.MULTIPLY, arraySize), 99, scopeRef); asm.addDataFilled(asmName.replace("#", "_").replace("$", "_"), AsmDataNumeric.Type.WORD, asmSize, size, "0"); added.add(asmName); + } else if(constantArrayFilled.getElementType() instanceof SymbolTypeStruct) { + SymbolTypeStruct structElementType = (SymbolTypeStruct) constantArrayFilled.getElementType(); + String asmSize = AsmFormat.getAsmConstant(program, new ConstantBinary(new ConstantInteger((long) structElementType.getSizeBytes()), Operators.MULTIPLY, arraySize), 99, scopeRef); + asm.addDataFilled(asmName.replace("#", "_").replace("$", "_"), AsmDataNumeric.Type.WORD, asmSize, size, "0"); + added.add(asmName); } else { throw new RuntimeException("Unhandled constant array element type " + constantArrayFilled.toString(program)); } diff --git a/src/test/java/dk/camelot64/kickc/test/TestPrograms.java b/src/test/java/dk/camelot64/kickc/test/TestPrograms.java index 4df4055da..96badfff4 100644 --- a/src/test/java/dk/camelot64/kickc/test/TestPrograms.java +++ b/src/test/java/dk/camelot64/kickc/test/TestPrograms.java @@ -32,6 +32,11 @@ public class TestPrograms { public TestPrograms() { } + @Test + public void testStruct3() throws IOException, URISyntaxException { + compileAndCompare("struct-3", log().verboseParse().verboseCreateSsa().verboseStatementSequence().verboseFragmentLog()); + } + @Test public void testStruct2() throws IOException, URISyntaxException { compileAndCompare("struct-2"); diff --git a/src/test/kc/struct-3.kc b/src/test/kc/struct-3.kc new file mode 100644 index 000000000..6257aa033 --- /dev/null +++ b/src/test/kc/struct-3.kc @@ -0,0 +1,20 @@ +// Minimal struct - different instances and copying + +struct Point { + byte x; + byte y; +}; + +struct Point[4] points; + +void main() { + for( byte i: 0..4) { + points[i].x = i; + points[i].y = i+1; + } + const byte* SCREEN = 0x0400; + for( byte i: 0..4) { + SCREEN[i] = points[i].x; + (SCREEN+40)[i] = points[i].y; + } +} \ No newline at end of file diff --git a/src/test/ref/struct-3.asm b/src/test/ref/struct-3.asm new file mode 100644 index 000000000..650be77b8 --- /dev/null +++ b/src/test/ref/struct-3.asm @@ -0,0 +1,39 @@ +// Minimal struct - different instances and copying +.pc = $801 "Basic" +:BasicUpstart(main) +.pc = $80d "Program" +main: { + .label SCREEN = $400 + .label _5 = 2 + ldx #0 + b1: + txa + asl + sta _5 + tay + txa + sta points,y + txa + tay + iny + tya + ldy _5 + sta points+1,y + inx + cpx #5 + bne b1 + ldy #0 + b2: + tya + asl + tax + lda points,x + sta SCREEN,y + lda points+1,x + sta SCREEN+$28,y + iny + cpy #5 + bne b2 + rts +} + points: .fill 2*4, 0