mirror of
https://gitlab.com/camelot/kickc.git
synced 2024-12-22 21:29:50 +00:00
updated fib
This commit is contained in:
parent
7e043de594
commit
84c0b01354
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
123
src/dk/camelot64/kickc/test/codegen-fib.txt
Normal file
123
src/dk/camelot64/kickc/test/codegen-fib.txt
Normal file
@ -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
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user