From 5c907162f35fd79948b9d5f8661c6357ec68ed73 Mon Sep 17 00:00:00 2001 From: jespergravgaard Date: Sun, 28 May 2017 17:58:02 +0200 Subject: [PATCH] Added more general pointer support --- .../kickc/asm/fragment/zpby1=coby1_plus_xby.asm | 4 ++++ .../kickc/asm/fragment/zpptrby1=zpptrby2_plus_1.asm | 7 +++++++ .../icl/Pass1GenerateSingleStaticAssignmentForm.java | 9 +++++++++ .../kickc/icl/Pass1GenerateStatementSequence.java | 10 ++++++++-- .../camelot64/kickc/icl/Pass3RegisterAllocation.java | 3 +++ src/dk/camelot64/kickc/test/Main.java | 2 +- src/dk/camelot64/kickc/test/memptr.kc | 8 ++++++++ 7 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 src/dk/camelot64/kickc/asm/fragment/zpby1=coby1_plus_xby.asm create mode 100644 src/dk/camelot64/kickc/asm/fragment/zpptrby1=zpptrby2_plus_1.asm create mode 100644 src/dk/camelot64/kickc/test/memptr.kc diff --git a/src/dk/camelot64/kickc/asm/fragment/zpby1=coby1_plus_xby.asm b/src/dk/camelot64/kickc/asm/fragment/zpby1=coby1_plus_xby.asm new file mode 100644 index 000000000..cd2f9882e --- /dev/null +++ b/src/dk/camelot64/kickc/asm/fragment/zpby1=coby1_plus_xby.asm @@ -0,0 +1,4 @@ +txa +clc +adc #{coby1} +sta {zpby1} \ No newline at end of file diff --git a/src/dk/camelot64/kickc/asm/fragment/zpptrby1=zpptrby2_plus_1.asm b/src/dk/camelot64/kickc/asm/fragment/zpptrby1=zpptrby2_plus_1.asm new file mode 100644 index 000000000..5a3da4fe9 --- /dev/null +++ b/src/dk/camelot64/kickc/asm/fragment/zpptrby1=zpptrby2_plus_1.asm @@ -0,0 +1,7 @@ +lda {zpptrby2} +clc +adc #1 +sta {zpptrby1} +lda {zpptrby2}+1 +adc #0 +sta {zpptrby1}+1 \ No newline at end of file diff --git a/src/dk/camelot64/kickc/icl/Pass1GenerateSingleStaticAssignmentForm.java b/src/dk/camelot64/kickc/icl/Pass1GenerateSingleStaticAssignmentForm.java index d3b48000b..de2c454ca 100644 --- a/src/dk/camelot64/kickc/icl/Pass1GenerateSingleStaticAssignmentForm.java +++ b/src/dk/camelot64/kickc/icl/Pass1GenerateSingleStaticAssignmentForm.java @@ -40,6 +40,15 @@ 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); + } } } } diff --git a/src/dk/camelot64/kickc/icl/Pass1GenerateStatementSequence.java b/src/dk/camelot64/kickc/icl/Pass1GenerateStatementSequence.java index d1329c304..9d03ebe40 100644 --- a/src/dk/camelot64/kickc/icl/Pass1GenerateStatementSequence.java +++ b/src/dk/camelot64/kickc/icl/Pass1GenerateStatementSequence.java @@ -154,7 +154,12 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor { @Override public LValue visitLvaluePtr(KickCParser.LvaluePtrContext ctx) { - throw new RuntimeException("Not implemented"); + LValue lval = (LValue) visit(ctx.lvalue()); + if(lval instanceof Variable) { + return new PointerDereferenceVariable((Variable) lval); + } else { + throw new RuntimeException("Not implemented"); + } } @Override @@ -185,7 +190,8 @@ public class Pass1GenerateStatementSequence extends KickCBaseVisitor { @Override public SymbolType visitTypePtr(KickCParser.TypePtrContext ctx) { - throw new RuntimeException("Not implemented"); + SymbolType elementType = (SymbolType) visit(ctx.typeDecl()); + return new SymbolTypePointer(elementType); } @Override diff --git a/src/dk/camelot64/kickc/icl/Pass3RegisterAllocation.java b/src/dk/camelot64/kickc/icl/Pass3RegisterAllocation.java index 72a0b0fdb..33ab14be7 100644 --- a/src/dk/camelot64/kickc/icl/Pass3RegisterAllocation.java +++ b/src/dk/camelot64/kickc/icl/Pass3RegisterAllocation.java @@ -31,6 +31,9 @@ public class Pass3RegisterAllocation { allocation.allocate(symbols.getVariable("i#3"), RegisterAllocation.getRegisterX()); allocation.allocate(symbols.getVariable("n1#1"), RegisterAllocation.getRegisterY()); allocation.allocate(symbols.getVariable("n1#2"), RegisterAllocation.getRegisterY()); + 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)); symbols.setAllocation(allocation); } diff --git a/src/dk/camelot64/kickc/test/Main.java b/src/dk/camelot64/kickc/test/Main.java index c3c865a99..ac5c99261 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/fibmem.kc"; + final String fileName = "src/dk/camelot64/kickc/test/memptr.kc"; final CharStream input = CharStreams.fromFileName(fileName); System.out.println(input.toString()); KickCLexer lexer = new KickCLexer(input); diff --git a/src/dk/camelot64/kickc/test/memptr.kc b/src/dk/camelot64/kickc/test/memptr.kc new file mode 100644 index 000000000..5b85ffc40 --- /dev/null +++ b/src/dk/camelot64/kickc/test/memptr.kc @@ -0,0 +1,8 @@ +byte[256] tab = $1100; +byte* ptr = tab; +byte i=0; +do { + *ptr = i; + ptr = ptr+1; + i = i+1; +} while (i!=0)