From 38408825cd4ea12ebdf327e235cbab9f90fabb06 Mon Sep 17 00:00:00 2001 From: jespergravgaard Date: Sun, 28 May 2017 21:47:50 +0200 Subject: [PATCH] Implemented simple bresenhm --- .../asm/fragment/coby1_le_zpby1_then_la1.asm | 4 ++++ .../asm/fragment/coby1_lt_zpby1_then_la1.asm | 3 +++ .../asm/fragment/zpbo1=zpby1_le_coby1.asm | 2 +- .../asm/fragment/zpby1=zpby1_minus_coby1.asm | 4 ++++ .../kickc/asm/fragment/zpby1=zpby1_plus_1.asm | 1 + .../asm/fragment/zpby1=zpby1_plus_coby1.asm | 4 ++++ .../kickc/asm/fragment/zpby1=zpby2_plus_1.asm | 4 ++-- .../fragment/zpptrby1=zpptrby1_plus_coby1.asm | 7 ++++++ ...ss1GenerateSingleStaticAssignmentForm.java | 16 ++++++------- .../kickc/icl/Pass2ConstantPropagation.java | 7 ++++++ .../kickc/icl/Pass3RegisterAllocation.java | 13 +++++++++++ src/dk/camelot64/kickc/test/Main.java | 3 +-- src/dk/camelot64/kickc/test/bresenham.kc | 23 +++++++++++++++++++ 13 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 src/dk/camelot64/kickc/asm/fragment/coby1_le_zpby1_then_la1.asm create mode 100644 src/dk/camelot64/kickc/asm/fragment/coby1_lt_zpby1_then_la1.asm create mode 100644 src/dk/camelot64/kickc/asm/fragment/zpby1=zpby1_minus_coby1.asm create mode 100644 src/dk/camelot64/kickc/asm/fragment/zpby1=zpby1_plus_1.asm create mode 100644 src/dk/camelot64/kickc/asm/fragment/zpby1=zpby1_plus_coby1.asm create mode 100644 src/dk/camelot64/kickc/asm/fragment/zpptrby1=zpptrby1_plus_coby1.asm create mode 100644 src/dk/camelot64/kickc/test/bresenham.kc diff --git a/src/dk/camelot64/kickc/asm/fragment/coby1_le_zpby1_then_la1.asm b/src/dk/camelot64/kickc/asm/fragment/coby1_le_zpby1_then_la1.asm new file mode 100644 index 000000000..f06ac6d0d --- /dev/null +++ b/src/dk/camelot64/kickc/asm/fragment/coby1_le_zpby1_then_la1.asm @@ -0,0 +1,4 @@ +lda #{coby1} +cmp {zpby1} +bcc {la1} +beq {la1} \ No newline at end of file diff --git a/src/dk/camelot64/kickc/asm/fragment/coby1_lt_zpby1_then_la1.asm b/src/dk/camelot64/kickc/asm/fragment/coby1_lt_zpby1_then_la1.asm new file mode 100644 index 000000000..e11d36391 --- /dev/null +++ b/src/dk/camelot64/kickc/asm/fragment/coby1_lt_zpby1_then_la1.asm @@ -0,0 +1,3 @@ +lda #{coby1} +cmp {zpby1} +bcc {la1} \ No newline at end of file diff --git a/src/dk/camelot64/kickc/asm/fragment/zpbo1=zpby1_le_coby1.asm b/src/dk/camelot64/kickc/asm/fragment/zpbo1=zpby1_le_coby1.asm index 2b8b0f1c7..d710663d5 100644 --- a/src/dk/camelot64/kickc/asm/fragment/zpbo1=zpby1_le_coby1.asm +++ b/src/dk/camelot64/kickc/asm/fragment/zpbo1=zpby1_le_coby1.asm @@ -5,4 +5,4 @@ beq !t+ !f: lda #0 jmp !d+ !t: lda #$ff -!d: sta {zpbo1 \ No newline at end of file +!d: sta {zpbo1} \ No newline at end of file diff --git a/src/dk/camelot64/kickc/asm/fragment/zpby1=zpby1_minus_coby1.asm b/src/dk/camelot64/kickc/asm/fragment/zpby1=zpby1_minus_coby1.asm new file mode 100644 index 000000000..753630696 --- /dev/null +++ b/src/dk/camelot64/kickc/asm/fragment/zpby1=zpby1_minus_coby1.asm @@ -0,0 +1,4 @@ +lda {zpby1} +sec +sbc #{coby1} +sta {zpby1} \ No newline at end of file diff --git a/src/dk/camelot64/kickc/asm/fragment/zpby1=zpby1_plus_1.asm b/src/dk/camelot64/kickc/asm/fragment/zpby1=zpby1_plus_1.asm new file mode 100644 index 000000000..d28af0f96 --- /dev/null +++ b/src/dk/camelot64/kickc/asm/fragment/zpby1=zpby1_plus_1.asm @@ -0,0 +1 @@ +inc {zpby1} \ No newline at end of file diff --git a/src/dk/camelot64/kickc/asm/fragment/zpby1=zpby1_plus_coby1.asm b/src/dk/camelot64/kickc/asm/fragment/zpby1=zpby1_plus_coby1.asm new file mode 100644 index 000000000..40a42de73 --- /dev/null +++ b/src/dk/camelot64/kickc/asm/fragment/zpby1=zpby1_plus_coby1.asm @@ -0,0 +1,4 @@ +lda {zpby1} +clc +adc #{coby1} +sta {zpby1} \ No newline at end of file diff --git a/src/dk/camelot64/kickc/asm/fragment/zpby1=zpby2_plus_1.asm b/src/dk/camelot64/kickc/asm/fragment/zpby1=zpby2_plus_1.asm index 580648132..919c475e6 100644 --- a/src/dk/camelot64/kickc/asm/fragment/zpby1=zpby2_plus_1.asm +++ b/src/dk/camelot64/kickc/asm/fragment/zpby1=zpby2_plus_1.asm @@ -1,4 +1,4 @@ -lda {zpby1} +lda {zpby2} clc adc #1 -sta {zpby2} \ No newline at end of file +sta {zpby1} \ No newline at end of file diff --git a/src/dk/camelot64/kickc/asm/fragment/zpptrby1=zpptrby1_plus_coby1.asm b/src/dk/camelot64/kickc/asm/fragment/zpptrby1=zpptrby1_plus_coby1.asm new file mode 100644 index 000000000..78b0c05df --- /dev/null +++ b/src/dk/camelot64/kickc/asm/fragment/zpptrby1=zpptrby1_plus_coby1.asm @@ -0,0 +1,7 @@ +lda {zpptrby1} +clc +adc #{coby1} +sta {zpptrby1} +lda {zpptrby1}+1 +adc #0 +sta {zpptrby1}+1 diff --git a/src/dk/camelot64/kickc/icl/Pass1GenerateSingleStaticAssignmentForm.java b/src/dk/camelot64/kickc/icl/Pass1GenerateSingleStaticAssignmentForm.java index de2c454ca..b20ecfdbb 100644 --- a/src/dk/camelot64/kickc/icl/Pass1GenerateSingleStaticAssignmentForm.java +++ b/src/dk/camelot64/kickc/icl/Pass1GenerateSingleStaticAssignmentForm.java @@ -40,15 +40,6 @@ public class Pass1GenerateSingleStaticAssignmentForm { VariableUnversioned assignedSymbol = (VariableUnversioned) lValue; VariableVersion version = symbols.createVersion(assignedSymbol); assignment.setLValue(version); - } else if(lValue instanceof PointerDereferenceVariable) { - PointerDereferenceVariable deref = (PointerDereferenceVariable) lValue; - Variable pointer = deref.getPointer(); - if(pointer instanceof VariableUnversioned) { - // Assignment to a non-versioned non-intermediary variable - VariableUnversioned assignedSymbol = (VariableUnversioned) pointer; - VariableVersion version = symbols.createVersion(assignedSymbol); - deref.setPointerVariable(version); - } } } } @@ -82,6 +73,13 @@ public class Pass1GenerateSingleStaticAssignmentForm { if (lValue instanceof VariableVersion) { VariableVersion versioned = (VariableVersion) lValue; blockVersions.put(versioned.getVersionOf(), versioned); + } else if(lValue instanceof PointerDereferenceVariable) { + PointerDereferenceVariable deref = (PointerDereferenceVariable) lValue; + Variable pointer = deref.getPointer(); + VariableVersion version = findOrCreateVersion(pointer, blockVersions, blockNewPhis); + if (version != null) { + deref.setPointerVariable(version); + } } } } diff --git a/src/dk/camelot64/kickc/icl/Pass2ConstantPropagation.java b/src/dk/camelot64/kickc/icl/Pass2ConstantPropagation.java index d577afc26..f19b15fdc 100644 --- a/src/dk/camelot64/kickc/icl/Pass2ConstantPropagation.java +++ b/src/dk/camelot64/kickc/icl/Pass2ConstantPropagation.java @@ -104,6 +104,13 @@ public class Pass2ConstantPropagation extends Pass2SsaOptimization { return new ConstantDouble(getDouble(c1) * getDouble(c2)); } } + case "/": { + if (c1 instanceof ConstantInteger && c2 instanceof ConstantInteger) { + return new ConstantInteger(getInteger(c1) / getInteger(c2)); + } else { + return new ConstantDouble(getDouble(c1) / getDouble(c2)); + } + } default: throw new RuntimeException("Unhandled Binary Operator " + operator.getOperator()); } diff --git a/src/dk/camelot64/kickc/icl/Pass3RegisterAllocation.java b/src/dk/camelot64/kickc/icl/Pass3RegisterAllocation.java index 33ab14be7..64433c7e3 100644 --- a/src/dk/camelot64/kickc/icl/Pass3RegisterAllocation.java +++ b/src/dk/camelot64/kickc/icl/Pass3RegisterAllocation.java @@ -34,6 +34,19 @@ public class Pass3RegisterAllocation { allocation.allocate(symbols.getVariable("ptr#1"), new RegisterAllocation.RegisterZpPointerByte(2)); allocation.allocate(symbols.getVariable("ptr#2"), new RegisterAllocation.RegisterZpPointerByte(2)); allocation.allocate(symbols.getVariable("ptr#3"), new RegisterAllocation.RegisterZpPointerByte(2)); + allocation.allocate(symbols.getVariable("y#1"), new RegisterAllocation.RegisterZpByte(4)); + allocation.allocate(symbols.getVariable("y#2"), new RegisterAllocation.RegisterZpByte(4)); + allocation.allocate(symbols.getVariable("y#5"), new RegisterAllocation.RegisterZpByte(4)); + allocation.allocate(symbols.getVariable("x#2"), new RegisterAllocation.RegisterZpByte(8)); + allocation.allocate(symbols.getVariable("x#5"), new RegisterAllocation.RegisterZpByte(8)); + allocation.allocate(symbols.getVariable("cursor#2"), new RegisterAllocation.RegisterZpPointerByte(5)); + allocation.allocate(symbols.getVariable("cursor#3"), new RegisterAllocation.RegisterZpPointerByte(5)); + allocation.allocate(symbols.getVariable("cursor#4"), new RegisterAllocation.RegisterZpPointerByte(5)); + allocation.allocate(symbols.getVariable("cursor#5"), new RegisterAllocation.RegisterZpPointerByte(5)); + allocation.allocate(symbols.getVariable("e#2"), new RegisterAllocation.RegisterZpByte(7)); + allocation.allocate(symbols.getVariable("e#3"), new RegisterAllocation.RegisterZpByte(7)); + allocation.allocate(symbols.getVariable("e#4"), new RegisterAllocation.RegisterZpByte(7)); + allocation.allocate(symbols.getVariable("e#5"), new RegisterAllocation.RegisterZpByte(7)); symbols.setAllocation(allocation); } diff --git a/src/dk/camelot64/kickc/test/Main.java b/src/dk/camelot64/kickc/test/Main.java index 9e248ac30..9623d3a72 100644 --- a/src/dk/camelot64/kickc/test/Main.java +++ b/src/dk/camelot64/kickc/test/Main.java @@ -13,7 +13,7 @@ import java.util.List; /** Test my KickC Grammar */ public class Main { public static void main(String[] args) throws IOException { - final String fileName = "src/dk/camelot64/kickc/test/memptr.kc"; + final String fileName = "src/dk/camelot64/kickc/test/bresenham.kc"; final CharStream input = CharStreams.fromFileName(fileName); System.out.println(input.toString()); KickCLexer lexer = new KickCLexer(input); @@ -78,7 +78,6 @@ public class Main { asmOptimized = pass5NextJumpElimination.optimize(); } - System.out.println("SYMBOLS"); System.out.println(symbolTable.toString()); System.out.println("CONTROL FLOW GRAPH"); diff --git a/src/dk/camelot64/kickc/test/bresenham.kc b/src/dk/camelot64/kickc/test/bresenham.kc new file mode 100644 index 000000000..f840da623 --- /dev/null +++ b/src/dk/camelot64/kickc/test/bresenham.kc @@ -0,0 +1,23 @@ +byte STAR = 81; +byte[40*25] SCREEN = $0400; +byte x0 = 0; +byte y0 = 0; +byte x1 = 39; +byte y1 = 24; +byte xd = x1-x0; +byte yd = y1-y0; +byte x = x0; +byte y = y0; +byte e = yd/2; +byte *cursor = SCREEN+y*40+x; +do { + *cursor = STAR; + x = x + 1; + cursor = cursor + 1; + e = e+yd; + if(xd