From 84c0b01354014c0ff5d7095ff15622be4d0c6155 Mon Sep 17 00:00:00 2001 From: jespergravgaard Date: Mon, 15 May 2017 23:33:19 +0200 Subject: [PATCH] updated fib --- src/dk/camelot64/kickc/test/Main.java | 4 +- src/dk/camelot64/kickc/test/codegen-fib.txt | 123 ++++++++++++++++++++ src/dk/camelot64/kickc/test/fib.kc | 10 +- 3 files changed, 130 insertions(+), 7 deletions(-) create mode 100644 src/dk/camelot64/kickc/test/codegen-fib.txt diff --git a/src/dk/camelot64/kickc/test/Main.java b/src/dk/camelot64/kickc/test/Main.java index 02360e8c6..058be4b2c 100644 --- a/src/dk/camelot64/kickc/test/Main.java +++ b/src/dk/camelot64/kickc/test/Main.java @@ -51,8 +51,8 @@ public class Main { if (stepOptimized) { System.out.println("Succesful optimization "+optimization); optimized = true; - System.out.println("CONTROL FLOW GRAPH"); - System.out.println(controlFlowGraph.toString()); + //System.out.println("CONTROL FLOW GRAPH"); + //System.out.println(controlFlowGraph.toString()); } } } diff --git a/src/dk/camelot64/kickc/test/codegen-fib.txt b/src/dk/camelot64/kickc/test/codegen-fib.txt new file mode 100644 index 000000000..c42e2e2fe --- /dev/null +++ b/src/dk/camelot64/kickc/test/codegen-fib.txt @@ -0,0 +1,123 @@ +byte n1 = 0; +byte n2 = 1; +byte i = 0; +byte fib = 0; +while(i<=10) { + fib = n1 + n2; + n1 = n2; + n2 = fib; + i = i + 1; +} + +PROGRAM + (byte) n1 ← (byte) 0 + (byte) n2 ← (byte) 1 + (byte) i ← (byte) 0 + (byte) fib ← (byte) 0 +(label) @1: + (boolean*) $0 ← (byte) i <= (byte) 10 + if((boolean*) $0) goto @2 + goto @3 +(label) @2: + (byte*) $1 ← (byte) n1 + (byte) n2 + (byte) fib ← (byte*) $1 + (byte) n1 ← (byte) n2 + (byte) n2 ← (byte) fib + (byte*) $2 ← (byte) i + (byte) 1 + (byte) i ← (byte*) $2 + goto @1 +(label) @3: + +CONTROL FLOW GRAPH +@BEGIN: + to:@1 +@1: from @BEGIN @2 + (byte) n2#2 ← phi( @BEGIN/(byte) 1 @2/(byte) n2#1 ) + (byte) n1#2 ← phi( @BEGIN/(byte) 0 @2/(byte) n1#1 ) + (byte) i#3 ← phi( @BEGIN/(byte) 0 @2/(byte) i#1 ) + (boolean*) $0 ← (byte) i#3 <= (byte) 10 + if((boolean*) $0) goto @2 + to:@END +@2: from @1 + (byte) n2#1 ← (byte) n1#2 + (byte) n2#2 + (byte) n1#1 ← (byte) n2#2 + (byte) i#1 ← (byte) i#3 + (byte) 1 + to:@1 +@END: from @1 + +ASSEMBLER +BBEGIN: + jmp B1_from_BBEGIN +B1_from_BBEGIN: + // (byte) n2#2 = (byte) 1 // zpby1=coby1 + lda #1 + sta 7 + // (byte) n1#2 = (byte) 0 // zpby1=coby1 + lda #0 + sta 6 + // (byte) i#3 = (byte) 0 // zpby1=coby1 + lda #0 + sta 8 + jmp B1 +B1_from_B2: + // (byte) n2#2 = (byte) n2#1 // zpby1=zpby2 + lda 4 + sta 7 + // (byte) n1#2 = (byte) n1#1 // zpby1=zpby2 + lda 3 + sta 6 + // (byte) i#3 = (byte) i#1 // zpby1=zpby2 + lda 5 + sta 8 + jmp B1 +B1: + // (boolean*) $0 ← (byte) i#3 <= (byte) 10 // zpbo1=zpby1<=coby1 + lda 8 + cmp #10 + bcc !t+ + beq !t+ + !f: lda #0 + jmp !d+ + !t: lda #$ff + !d: sta 2 + // if((boolean*) $0) goto @2 // zpbo1?la1 + lda 2 + bne B2 + jmp BEND +B2: + // (byte) n2#1 ← (byte) n1#2 + (byte) n2#2 // zpby1=zpby2+zpby3 + lda 6 + clc + adc 7 + sta 4 + // (byte) n1#1 ← (byte) n2#2 // zpby1=zpby2 + lda 7 + sta 3 + // (byte) i#1 ← (byte) i#3 + (byte) 1 // zpby1=zpby2+coby1 + lda 8 + clc + adc #1 + sta 5 + jmp B1_from_B2 +BEND: + +OPTIMAL ASSEMBLER +BBEGIN: +B1_from_BBEGIN: + ldy #0 + sty $2 + iny + tya + ldx #12 + clc +B2: + adc $2 + sty $2 + tay + dex +B1: + bne B2 +BEND: + sta $3 + inc $d020 + jmp BEND \ No newline at end of file diff --git a/src/dk/camelot64/kickc/test/fib.kc b/src/dk/camelot64/kickc/test/fib.kc index b79d8d4a9..a3168f6d6 100644 --- a/src/dk/camelot64/kickc/test/fib.kc +++ b/src/dk/camelot64/kickc/test/fib.kc @@ -1,10 +1,10 @@ -byte num = 0; -byte num2 = 1; +byte n1 = 0; +byte n2 = 1; byte i = 0; byte fib = 0; while(i<=10) { - fib = num + num2; - num = num2; - num2 = fib; + fib = n1 + n2; + n1 = n2; + n2 = fib; i = i + 1; }