1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-09-08 17:54:40 +00:00

Added a smaller sieve.

This commit is contained in:
jespergravgaard 2019-08-07 11:35:15 +02:00
parent dacd25ac5f
commit b2804ef335
6 changed files with 2850 additions and 0 deletions

View File

@ -71,6 +71,11 @@ public class TestPrograms {
compileAndCompare("sieve");
}
@Test
public void testSieveMin() throws IOException, URISyntaxException {
compileAndCompare("sieve-min");
}
@Test
public void testCoalesceAssignment() throws IOException, URISyntaxException {
compileAndCompare("coalesce-assignment");

39
src/test/kc/sieve-min.kc Normal file
View File

@ -0,0 +1,39 @@
import "string"
import "time"
import "stdint"
import "division"
import "c64"
import "print"
const char* SCREEN = 0x0400;
const uint16_t COUNT = 16384; /* Up to what number? */
const uint8_t SQRT_COUNT = 128; /* Sqrt of COUNT */
uint8_t* sieve = 0x1000;
void main (void) {
// Fill sieve with zeros
memset(sieve, 0, COUNT);
unsigned int i = 2;
char* sieve_i = &sieve[i];
while (i < SQRT_COUNT) {
if (!*sieve_i) {
/* Prime number - mark all multiples */
unsigned int j = i*2;
unsigned char* s = &sieve[j];
while (j < COUNT) {
*s = 1;
s += i;
j += i;
}
}
i++;
sieve_i++;
}
for (i = 2; i < 0x04c7; ++i)
if (!sieve[i]) {
print_word(i);
print_char(' ');
}
while(true) { (*(SCREEN+999))++; }
}

203
src/test/ref/sieve-min.asm Normal file
View File

@ -0,0 +1,203 @@
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
.label SCREEN = $400
.const COUNT = $4000
/* Up to what number? */
.const SQRT_COUNT = $80
/* Sqrt of COUNT */
.label sieve = $1000
.label print_char_cursor = 8
main: {
.label i = 8
.label sieve_i = $a
.label j = 4
.label s = 6
.label i_3 = 2
.label i_10 = 2
.label _18 = $c
jsr memset
lda #<sieve+2
sta sieve_i
lda #>sieve+2
sta sieve_i+1
lda #<2
sta i
lda #>2
sta i+1
b1:
lda i+1
cmp #>SQRT_COUNT
bcc b2
bne !+
lda i
cmp #<SQRT_COUNT
bcc b2
!:
lda #<$400
sta print_char_cursor
lda #>$400
sta print_char_cursor+1
lda #<2
sta i_10
lda #>2
sta i_10+1
b7:
lda i_10+1
cmp #>$4c7
bcc b8
bne !+
lda i_10
cmp #<$4c7
bcc b8
!:
b11:
inc SCREEN+$3e7
jmp b11
b8:
lda i_10
clc
adc #<sieve
sta _18
lda i_10+1
adc #>sieve
sta _18+1
ldy #0
lda (_18),y
cmp #0
bne b9
jsr print_word
lda #' '
jsr print_char
b9:
inc i_3
bne !+
inc i_3+1
!:
jmp b7
b2:
ldy #0
lda (sieve_i),y
cmp #0
bne b3
lda i
asl
sta j
lda i+1
rol
sta j+1
lda j
clc
adc #<sieve
sta s
lda j+1
adc #>sieve
sta s+1
b4:
lda j+1
cmp #>COUNT
bcc b5
bne !+
lda j
cmp #<COUNT
bcc b5
!:
b3:
inc i
bne !+
inc i+1
!:
inc sieve_i
bne !+
inc sieve_i+1
!:
jmp b1
b5:
lda #1
ldy #0
sta (s),y
lda s
clc
adc i
sta s
lda s+1
adc i+1
sta s+1
lda j
clc
adc i
sta j
lda j+1
adc i+1
sta j+1
jmp b4
}
// Print a single char
// print_char(byte register(A) ch)
print_char: {
ldy #0
sta (print_char_cursor),y
inc print_char_cursor
bne !+
inc print_char_cursor+1
!:
rts
}
// Print a word as HEX
// print_word(word zeropage(2) w)
print_word: {
.label w = 2
lda w+1
tax
jsr print_byte
lda w
tax
jsr print_byte
rts
}
// Print a byte as HEX
// print_byte(byte register(X) b)
print_byte: {
txa
lsr
lsr
lsr
lsr
tay
lda print_hextab,y
jsr print_char
lda #$f
axs #0
lda print_hextab,x
jsr print_char
rts
}
// Copies the character c (an unsigned char) to the first num characters of the object pointed to by the argument str.
memset: {
.const c = 0
.label str = sieve
.label end = str+COUNT
.label dst = $a
lda #<str
sta dst
lda #>str
sta dst+1
b1:
lda dst+1
cmp #>end
bne b2
lda dst
cmp #<end
bne b2
rts
b2:
lda #c
ldy #0
sta (dst),y
inc dst
bne !+
inc dst+1
!:
jmp b1
}
print_hextab: .text "0123456789abcdef"

112
src/test/ref/sieve-min.cfg Normal file
View File

@ -0,0 +1,112 @@
@begin: scope:[] from
[0] phi()
to:@1
@1: scope:[] from @begin
[1] phi()
[2] call main
to:@end
@end: scope:[] from @1
[3] phi()
main: scope:[main] from @1
[4] phi()
[5] call memset
to:main::@1
main::@1: scope:[main] from main main::@3
[6] (byte*) main::sieve_i#2 ← phi( main/(const byte*) sieve#0+(byte) 2 main::@3/(byte*) main::sieve_i#1 )
[6] (word) main::i#12 ← phi( main/(byte) 2 main::@3/(word) main::i#2 )
[7] if((word) main::i#12<(const byte) SQRT_COUNT#0) goto main::@2
to:main::@7
main::@7: scope:[main] from main::@1 main::@9
[8] (byte*) print_char_cursor#27 ← phi( main::@9/(byte*) print_char_cursor#35 main::@1/(byte*) 1024 )
[8] (word) main::i#10 ← phi( main::@9/(word) main::i#3 main::@1/(byte) 2 )
[9] if((word) main::i#10<(word) $4c7) goto main::@8
to:main::@11
main::@11: scope:[main] from main::@11 main::@7
[10] *((const byte*) SCREEN#0+(word) $3e7) ← ++ *((const byte*) SCREEN#0+(word) $3e7)
to:main::@11
main::@8: scope:[main] from main::@7
[11] (byte*~) main::$18 ← (const byte*) sieve#0 + (word) main::i#10
[12] if((byte) 0!=*((byte*~) main::$18)) goto main::@9
to:main::@10
main::@10: scope:[main] from main::@8
[13] (word) print_word::w#0 ← (word) main::i#10
[14] call print_word
to:main::@12
main::@12: scope:[main] from main::@10
[15] phi()
[16] call print_char
to:main::@9
main::@9: scope:[main] from main::@12 main::@8
[17] (byte*) print_char_cursor#35 ← phi( main::@8/(byte*) print_char_cursor#27 main::@12/(byte*) print_char_cursor#20 )
[18] (word) main::i#3 ← ++ (word) main::i#10
to:main::@7
main::@2: scope:[main] from main::@1
[19] if((byte) 0!=*((byte*) main::sieve_i#2)) goto main::@3
to:main::@6
main::@6: scope:[main] from main::@2
[20] (word) main::j#0 ← (word) main::i#12 << (byte) 1
[21] (byte*) main::s#0 ← (const byte*) sieve#0 + (word) main::j#0
to:main::@4
main::@4: scope:[main] from main::@5 main::@6
[22] (byte*) main::s#2 ← phi( main::@6/(byte*) main::s#0 main::@5/(byte*) main::s#1 )
[22] (word) main::j#2 ← phi( main::@6/(word) main::j#0 main::@5/(word) main::j#1 )
[23] if((word) main::j#2<(const word) COUNT#0) goto main::@5
to:main::@3
main::@3: scope:[main] from main::@2 main::@4
[24] (word) main::i#2 ← ++ (word) main::i#12
[25] (byte*) main::sieve_i#1 ← ++ (byte*) main::sieve_i#2
to:main::@1
main::@5: scope:[main] from main::@4
[26] *((byte*) main::s#2) ← (byte) 1
[27] (byte*) main::s#1 ← (byte*) main::s#2 + (word) main::i#12
[28] (word) main::j#1 ← (word) main::j#2 + (word) main::i#12
to:main::@4
print_char: scope:[print_char] from main::@12 print_byte print_byte::@1
[29] (byte*) print_char_cursor#19 ← phi( main::@12/(byte*) print_char_cursor#20 print_byte/(byte*) print_char_cursor#26 print_byte::@1/(byte*) print_char_cursor#20 )
[29] (byte) print_char::ch#3 ← phi( main::@12/(byte) ' ' print_byte/(byte) print_char::ch#0 print_byte::@1/(byte) print_char::ch#1 )
[30] *((byte*) print_char_cursor#19) ← (byte) print_char::ch#3
[31] (byte*) print_char_cursor#20 ← ++ (byte*) print_char_cursor#19
to:print_char::@return
print_char::@return: scope:[print_char] from print_char
[32] return
to:@return
print_word: scope:[print_word] from main::@10
[33] (byte) print_byte::b#0 ← > (word) print_word::w#0
[34] call print_byte
to:print_word::@1
print_word::@1: scope:[print_word] from print_word
[35] (byte) print_byte::b#1 ← < (word) print_word::w#0
[36] call print_byte
to:print_word::@return
print_word::@return: scope:[print_word] from print_word::@1
[37] return
to:@return
print_byte: scope:[print_byte] from print_word print_word::@1
[38] (byte*) print_char_cursor#26 ← phi( print_word/(byte*) print_char_cursor#27 print_word::@1/(byte*) print_char_cursor#20 )
[38] (byte) print_byte::b#2 ← phi( print_word/(byte) print_byte::b#0 print_word::@1/(byte) print_byte::b#1 )
[39] (byte~) print_byte::$0 ← (byte) print_byte::b#2 >> (byte) 4
[40] (byte) print_char::ch#0 ← *((const byte[]) print_hextab#0 + (byte~) print_byte::$0)
[41] call print_char
to:print_byte::@1
print_byte::@1: scope:[print_byte] from print_byte
[42] (byte~) print_byte::$2 ← (byte) print_byte::b#2 & (byte) $f
[43] (byte) print_char::ch#1 ← *((const byte[]) print_hextab#0 + (byte~) print_byte::$2)
[44] call print_char
to:print_byte::@return
print_byte::@return: scope:[print_byte] from print_byte::@1
[45] return
to:@return
memset: scope:[memset] from main
[46] phi()
to:memset::@1
memset::@1: scope:[memset] from memset memset::@2
[47] (byte*) memset::dst#2 ← phi( memset/(byte*)(const void*) memset::str#0 memset::@2/(byte*) memset::dst#1 )
[48] if((byte*) memset::dst#2!=(const byte*) memset::end#0) goto memset::@2
to:memset::@return
memset::@return: scope:[memset] from memset::@1
[49] return
to:@return
memset::@2: scope:[memset] from memset::@1
[50] *((byte*) memset::dst#2) ← (const byte) memset::c#0
[51] (byte*) memset::dst#1 ← ++ (byte*) memset::dst#2
to:memset::@1

2390
src/test/ref/sieve-min.log Normal file

File diff suppressed because it is too large Load Diff

101
src/test/ref/sieve-min.sym Normal file
View File

@ -0,0 +1,101 @@
(label) @1
(label) @begin
(label) @end
(word) COUNT
(const word) COUNT#0 COUNT = (word) $4000
(const byte) RADIX::BINARY BINARY = (number) 2
(const byte) RADIX::DECIMAL DECIMAL = (number) $a
(const byte) RADIX::HEXADECIMAL HEXADECIMAL = (number) $10
(const byte) RADIX::OCTAL OCTAL = (number) 8
(byte*) SCREEN
(const byte*) SCREEN#0 SCREEN = (byte*) 1024
(byte) SQRT_COUNT
(const byte) SQRT_COUNT#0 SQRT_COUNT = (byte) $80
(void()) main()
(byte*~) main::$18 $18 zp ZP_WORD:12 22.0
(label) main::@1
(label) main::@10
(label) main::@11
(label) main::@12
(label) main::@2
(label) main::@3
(label) main::@4
(label) main::@5
(label) main::@6
(label) main::@7
(label) main::@8
(label) main::@9
(word) main::i
(word) main::i#10 i#10 zp ZP_WORD:2 6.111111111111112
(word) main::i#12 i zp ZP_WORD:8 24.6
(word) main::i#2 i zp ZP_WORD:8 11.0
(word) main::i#3 i#3 zp ZP_WORD:2 22.0
(word) main::j
(word) main::j#0 j zp ZP_WORD:4 16.5
(word) main::j#1 j zp ZP_WORD:4 202.0
(word) main::j#2 j zp ZP_WORD:4 78.5
(byte*) main::s
(byte*) main::s#0 s zp ZP_WORD:6 22.0
(byte*) main::s#1 s zp ZP_WORD:6 101.0
(byte*) main::s#2 s zp ZP_WORD:6 104.66666666666666
(byte*) main::sieve_i
(byte*) main::sieve_i#1 sieve_i zp ZP_WORD:10 22.0
(byte*) main::sieve_i#2 sieve_i zp ZP_WORD:10 3.0
(void*()) memset((void*) memset::str , (byte) memset::c , (word) memset::num)
(label) memset::@1
(label) memset::@2
(label) memset::@return
(byte) memset::c
(const byte) memset::c#0 c = (byte) 0
(byte*) memset::dst
(byte*) memset::dst#1 dst zp ZP_WORD:10 22.0
(byte*) memset::dst#2 dst zp ZP_WORD:10 14.666666666666666
(byte*) memset::end
(const byte*) memset::end#0 end = (byte*)(const void*) memset::str#0+(const word) COUNT#0
(word) memset::num
(void*) memset::return
(void*) memset::str
(const void*) memset::str#0 str = (void*)(const byte*) sieve#0
(void()) print_byte((byte) print_byte::b)
(byte~) print_byte::$0 reg byte a 4.0
(byte~) print_byte::$2 reg byte x 4.0
(label) print_byte::@1
(label) print_byte::@return
(byte) print_byte::b
(byte) print_byte::b#0 reg byte x 4.0
(byte) print_byte::b#1 reg byte x 4.0
(byte) print_byte::b#2 reg byte x 2.0
(void()) print_char((byte) print_char::ch)
(label) print_char::@return
(byte) print_char::ch
(byte) print_char::ch#0 reg byte a 4.0
(byte) print_char::ch#1 reg byte a 4.0
(byte) print_char::ch#3 reg byte a 6.0
(byte*) print_char_cursor
(byte*) print_char_cursor#19 print_char_cursor zp ZP_WORD:8 9.5
(byte*) print_char_cursor#20 print_char_cursor zp ZP_WORD:8 1.9999999999999998
(byte*) print_char_cursor#26 print_char_cursor zp ZP_WORD:8 2.0
(byte*) print_char_cursor#27 print_char_cursor zp ZP_WORD:8 4.0
(byte*) print_char_cursor#35 print_char_cursor zp ZP_WORD:8 16.5
(byte[]) print_hextab
(const byte[]) print_hextab#0 print_hextab = (string) "0123456789abcdef"
(byte*) print_line_cursor
(byte*) print_screen
(void()) print_word((word) print_word::w)
(label) print_word::@1
(label) print_word::@return
(word) print_word::w
(word) print_word::w#0 w zp ZP_WORD:2 5.0
(byte*) sieve
(const byte*) sieve#0 sieve = (byte*) 4096
zp ZP_WORD:2 [ main::i#10 main::i#3 print_word::w#0 ]
zp ZP_WORD:4 [ main::j#2 main::j#0 main::j#1 ]
zp ZP_WORD:6 [ main::s#2 main::s#0 main::s#1 ]
reg byte a [ print_char::ch#3 print_char::ch#0 print_char::ch#1 ]
zp ZP_WORD:8 [ print_char_cursor#19 print_char_cursor#27 print_char_cursor#35 print_char_cursor#20 print_char_cursor#26 main::i#12 main::i#2 ]
reg byte x [ print_byte::b#2 print_byte::b#0 print_byte::b#1 ]
zp ZP_WORD:10 [ memset::dst#2 memset::dst#1 main::sieve_i#2 main::sieve_i#1 ]
zp ZP_WORD:12 [ main::$18 ]
reg byte a [ print_byte::$0 ]
reg byte x [ print_byte::$2 ]