1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-11-20 02:32:36 +00:00

Added support for special types signed/unsigned (that are implicitly int). Closes #158

This commit is contained in:
jespergravgaard 2019-04-22 09:20:14 +02:00
parent e9ece1a8f0
commit 94bd8cef0a
9 changed files with 2732 additions and 435 deletions

View File

@ -108,7 +108,7 @@ forClassicInit
typeDecl
: '(' typeDecl ')' #typePar
| SIMPLETYPE #typeSimple
| ('signed'|'unsigned') SIMPLETYPE #typeSignedSimple
| ('signed'|'unsigned') SIMPLETYPE? #typeSignedSimple
| typeDecl '*' #typePtr
| typeDecl '[' (expr)? ']' #typeArray
| typeDecl '(' ')' #typeProcedure

File diff suppressed because it is too large Load Diff

View File

@ -1117,7 +1117,14 @@ public class Pass0GenerateStatementSequence extends KickCBaseVisitor<Object> {
@Override
public SymbolType visitTypeSignedSimple(KickCParser.TypeSignedSimpleContext ctx) {
String signedness = ctx.getChild(0).getText();
return SymbolType.get(signedness + " " + ctx.SIMPLETYPE().getText());
String simpleTypeName;
if(ctx.SIMPLETYPE()!=null) {
simpleTypeName = ctx.SIMPLETYPE().getText();
} else {
simpleTypeName = "int";
}
return SymbolType.get(signedness + " " + simpleTypeName);
}
@Override

View File

@ -37,6 +37,11 @@ public class TestPrograms {
// compileAndCompare("pointer-cast-3");
//}
@Test
public void testTypeSigned() throws IOException, URISyntaxException {
compileAndCompare("type-signed");
}
@Test
public void testPointerPlus0() throws IOException, URISyntaxException {
compileAndCompare("pointer-plus-0");

View File

@ -0,0 +1,17 @@
// Tests the special "signed" / "unsigned" without a simple type name
import "print"
void main() {
signed a = -1023;
unsigned b = 4132;
for( byte i : 0..5 ) {
a += -7;
b += 321;
print_sword(a);
print_char(' ');
print_word(b);
print_ln();
}
}

View File

@ -0,0 +1,156 @@
// Tests the special "signed" / "unsigned" without a simple type name
.pc = $801 "Basic"
:BasicUpstart(main)
.pc = $80d "Program"
.label print_line_cursor = 6
.label print_char_cursor = $b
main: {
.label a = 2
.label b = 4
ldx #0
lda #<$400
sta print_line_cursor
lda #>$400
sta print_line_cursor+1
lda #<$400
sta print_char_cursor
lda #>$400
sta print_char_cursor+1
lda #<$1024
sta b
lda #>$1024
sta b+1
lda #<-$3ff
sta a
lda #>-$3ff
sta a+1
b1:
lda #-7
sta $fe
ora #$7f
bmi !+
lda #0
!:
sta $ff
clc
lda a
adc $fe
sta a
lda a+1
adc $ff
sta a+1
clc
lda b
adc #<$141
sta b
lda b+1
adc #>$141
sta b+1
lda a
sta print_sword.w
lda a+1
sta print_sword.w+1
jsr print_sword
lda #' '
jsr print_char
lda b
sta print_word.w
lda b+1
sta print_word.w+1
jsr print_word
jsr print_ln
inx
cpx #6
bne b6
rts
b6:
lda print_line_cursor
sta print_char_cursor
lda print_line_cursor+1
sta print_char_cursor+1
jmp b1
}
// Print a newline
print_ln: {
b1:
lda #$28
clc
adc print_line_cursor
sta print_line_cursor
bcc !+
inc print_line_cursor+1
!:
lda print_line_cursor+1
cmp print_char_cursor+1
bcc b1
bne !+
lda print_line_cursor
cmp print_char_cursor
bcc b1
!:
rts
}
// Print a word as HEX
// print_word(word zeropage(8) w)
print_word: {
.label w = 8
lda w+1
sta print_byte.b
jsr print_byte
lda w
sta print_byte.b
jsr print_byte
rts
}
// Print a byte as HEX
// print_byte(byte zeropage($a) b)
print_byte: {
.label b = $a
lda b
lsr
lsr
lsr
lsr
tay
lda print_hextab,y
jsr print_char
lda #$f
and b
tay
lda print_hextab,y
jsr print_char
rts
}
// 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 signed word as HEX
// print_sword(signed word zeropage(8) w)
print_sword: {
.label w = 8
lda w+1
bpl b1
lda #'-'
jsr print_char
sec
lda w
eor #$ff
adc #0
sta w
lda w+1
eor #$ff
adc #0
sta w+1
b1:
jsr print_word
rts
}
print_hextab: .text "0123456789abcdef"

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()
to:main::@1
main::@1: scope:[main] from main main::@6
[5] (byte) main::i#2 ← phi( main/(byte/signed byte/word/signed word/dword/signed dword) 0 main::@6/(byte) main::i#1 )
[5] (byte*) print_line_cursor#13 ← phi( main/((byte*))(word/signed word/dword/signed dword) $400 main::@6/(byte*) print_line_cursor#1 )
[5] (byte*) print_char_cursor#44 ← phi( main/((byte*))(word/signed word/dword/signed dword) $400 main::@6/(byte*~) print_char_cursor#49 )
[5] (word) main::b#2 ← phi( main/(word/signed word/dword/signed dword) $1024 main::@6/(word) main::b#1 )
[5] (signed word) main::a#2 ← phi( main/-(word/signed word/dword/signed dword) $3ff main::@6/(signed word) main::a#1 )
[6] (signed word) main::a#1 ← (signed word) main::a#2 + -(byte/signed byte/word/signed word/dword/signed dword) 7
[7] (word) main::b#1 ← (word) main::b#2 + (word/signed word/dword/signed dword) $141
[8] (signed word) print_sword::w#1 ← (signed word) main::a#1
[9] call print_sword
to:main::@2
main::@2: scope:[main] from main::@1
[10] phi()
[11] call print_char
to:main::@3
main::@3: scope:[main] from main::@2
[12] (word) print_word::w#1 ← (word) main::b#1
[13] call print_word
to:main::@4
main::@4: scope:[main] from main::@3
[14] phi()
[15] call print_ln
to:main::@5
main::@5: scope:[main] from main::@4
[16] (byte) main::i#1 ← ++ (byte) main::i#2
[17] if((byte) main::i#1!=(byte/signed byte/word/signed word/dword/signed dword) 6) goto main::@6
to:main::@return
main::@return: scope:[main] from main::@5
[18] return
to:@return
main::@6: scope:[main] from main::@5
[19] (byte*~) print_char_cursor#49 ← (byte*) print_line_cursor#1
to:main::@1
print_ln: scope:[print_ln] from main::@4
[20] phi()
to:print_ln::@1
print_ln::@1: scope:[print_ln] from print_ln print_ln::@1
[21] (byte*) print_line_cursor#6 ← phi( print_ln/(byte*) print_line_cursor#13 print_ln::@1/(byte*) print_line_cursor#1 )
[22] (byte*) print_line_cursor#1 ← (byte*) print_line_cursor#6 + (byte/signed byte/word/signed word/dword/signed dword) $28
[23] if((byte*) print_line_cursor#1<(byte*) print_char_cursor#12) goto print_ln::@1
to:print_ln::@return
print_ln::@return: scope:[print_ln] from print_ln::@1
[24] return
to:@return
print_word: scope:[print_word] from main::@3 print_sword::@1
[25] (byte*) print_char_cursor#42 ← phi( main::@3/(byte*) print_char_cursor#12 print_sword::@1/(byte*) print_char_cursor#40 )
[25] (word) print_word::w#2 ← phi( main::@3/(word) print_word::w#1 print_sword::@1/(word~) print_word::w#5 )
[26] (byte) print_byte::b#0 ← > (word) print_word::w#2
[27] call print_byte
to:print_word::@1
print_word::@1: scope:[print_word] from print_word
[28] (byte) print_byte::b#1 ← < (word) print_word::w#2
[29] call print_byte
to:print_word::@return
print_word::@return: scope:[print_word] from print_word::@1
[30] return
to:@return
print_byte: scope:[print_byte] from print_word print_word::@1
[31] (byte*) print_char_cursor#43 ← phi( print_word/(byte*) print_char_cursor#42 print_word::@1/(byte*) print_char_cursor#12 )
[31] (byte) print_byte::b#2 ← phi( print_word/(byte) print_byte::b#0 print_word::@1/(byte) print_byte::b#1 )
[32] (byte~) print_byte::$0 ← (byte) print_byte::b#2 >> (byte/signed byte/word/signed word/dword/signed dword) 4
[33] (byte) print_char::ch#1 ← *((const byte[]) print_hextab#0 + (byte~) print_byte::$0)
[34] call print_char
to:print_byte::@1
print_byte::@1: scope:[print_byte] from print_byte
[35] (byte~) print_byte::$2 ← (byte) print_byte::b#2 & (byte/signed byte/word/signed word/dword/signed dword) $f
[36] (byte) print_char::ch#2 ← *((const byte[]) print_hextab#0 + (byte~) print_byte::$2)
[37] call print_char
to:print_byte::@return
print_byte::@return: scope:[print_byte] from print_byte::@1
[38] return
to:@return
print_char: scope:[print_char] from main::@2 print_byte print_byte::@1 print_sword::@2
[39] (byte*) print_char_cursor#31 ← phi( main::@2/(byte*) print_char_cursor#12 print_byte/(byte*) print_char_cursor#43 print_byte::@1/(byte*) print_char_cursor#12 print_sword::@2/(byte*) print_char_cursor#44 )
[39] (byte) print_char::ch#4 ← phi( main::@2/(byte) ' ' print_byte/(byte) print_char::ch#1 print_byte::@1/(byte) print_char::ch#2 print_sword::@2/(byte) '-' )
[40] *((byte*) print_char_cursor#31) ← (byte) print_char::ch#4
[41] (byte*) print_char_cursor#12 ← ++ (byte*) print_char_cursor#31
to:print_char::@return
print_char::@return: scope:[print_char] from print_char
[42] return
to:@return
print_sword: scope:[print_sword] from main::@1
[43] if((signed word) print_sword::w#1>=(byte/signed byte/word/signed word/dword/signed dword) 0) goto print_sword::@1
to:print_sword::@2
print_sword::@2: scope:[print_sword] from print_sword
[44] phi()
[45] call print_char
to:print_sword::@3
print_sword::@3: scope:[print_sword] from print_sword::@2
[46] (signed word) print_sword::w#0 ← - (signed word) print_sword::w#1
to:print_sword::@1
print_sword::@1: scope:[print_sword] from print_sword print_sword::@3
[47] (byte*) print_char_cursor#40 ← phi( print_sword/(byte*) print_char_cursor#44 print_sword::@3/(byte*) print_char_cursor#12 )
[47] (signed word) print_sword::w#3 ← phi( print_sword/(signed word) print_sword::w#1 print_sword::@3/(signed word) print_sword::w#0 )
[48] (word~) print_word::w#5 ← (word)(signed word) print_sword::w#3
[49] call print_word
to:print_sword::@return
print_sword::@return: scope:[print_sword] from print_sword::@1
[50] return
to:@return

1911
src/test/ref/type-signed.log Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,80 @@
(label) @1
(label) @begin
(label) @end
(void()) main()
(label) main::@1
(label) main::@2
(label) main::@3
(label) main::@4
(label) main::@5
(label) main::@6
(label) main::@return
(signed word) main::a
(signed word) main::a#1 a zp ZP_WORD:2 2.5384615384615383
(signed word) main::a#2 a zp ZP_WORD:2 22.0
(word) main::b
(word) main::b#1 b zp ZP_WORD:4 2.75
(word) main::b#2 b zp ZP_WORD:4 11.0
(byte) main::i
(byte) main::i#1 reg byte x 11.0
(byte) main::i#2 reg byte x 2.0
(void()) print_byte((byte) print_byte::b)
(byte~) print_byte::$0 reg byte a 4.0
(byte~) print_byte::$2 reg byte a 4.0
(label) print_byte::@1
(label) print_byte::@return
(byte) print_byte::b
(byte) print_byte::b#0 b zp ZP_BYTE:10 4.0
(byte) print_byte::b#1 b zp ZP_BYTE:10 4.0
(byte) print_byte::b#2 b zp ZP_BYTE:10 2.0
(void()) print_char((byte) print_char::ch)
(label) print_char::@return
(byte) print_char::ch
(byte) print_char::ch#1 reg byte a 4.0
(byte) print_char::ch#2 reg byte a 4.0
(byte) print_char::ch#4 reg byte a 6.0
(byte*) print_char_cursor
(byte*) print_char_cursor#12 print_char_cursor zp ZP_WORD:11 5.240000000000001
(byte*) print_char_cursor#31 print_char_cursor zp ZP_WORD:11 10.5
(byte*) print_char_cursor#40 print_char_cursor zp ZP_WORD:11 3.0
(byte*) print_char_cursor#42 print_char_cursor zp ZP_WORD:11 7.5
(byte*) print_char_cursor#43 print_char_cursor zp ZP_WORD:11 2.0
(byte*) print_char_cursor#44 print_char_cursor zp ZP_WORD:11 2.5
(byte*~) print_char_cursor#49 print_char_cursor zp ZP_WORD:11 22.0
(byte[]) print_hextab
(const byte[]) print_hextab#0 print_hextab = (string) "0123456789abcdef"
(byte*) print_line_cursor
(byte*) print_line_cursor#1 print_line_cursor zp ZP_WORD:6 46.42857142857143
(byte*) print_line_cursor#13 print_line_cursor zp ZP_WORD:6 1.1818181818181819
(byte*) print_line_cursor#6 print_line_cursor zp ZP_WORD:6 204.0
(void()) print_ln()
(label) print_ln::@1
(label) print_ln::@return
(byte*) print_screen
(void()) print_sword((signed word) print_sword::w)
(label) print_sword::@1
(label) print_sword::@2
(label) print_sword::@3
(label) print_sword::@return
(signed word) print_sword::w
(signed word) print_sword::w#0 w zp ZP_WORD:8 4.0
(signed word) print_sword::w#1 w zp ZP_WORD:8 4.25
(signed word) print_sword::w#3 w zp ZP_WORD:8 4.0
(void()) print_word((word) print_word::w)
(label) print_word::@1
(label) print_word::@return
(word) print_word::w
(word) print_word::w#1 w zp ZP_WORD:8 22.0
(word) print_word::w#2 w zp ZP_WORD:8 5.666666666666667
(word~) print_word::w#5 w zp ZP_WORD:8 4.0
zp ZP_WORD:2 [ main::a#2 main::a#1 ]
zp ZP_WORD:4 [ main::b#2 main::b#1 ]
reg byte x [ main::i#2 main::i#1 ]
zp ZP_WORD:6 [ print_line_cursor#6 print_line_cursor#13 print_line_cursor#1 ]
zp ZP_WORD:8 [ print_word::w#2 print_word::w#1 print_word::w#5 print_sword::w#3 print_sword::w#1 print_sword::w#0 ]
zp ZP_BYTE:10 [ print_byte::b#2 print_byte::b#0 print_byte::b#1 ]
reg byte a [ print_char::ch#4 print_char::ch#1 print_char::ch#2 ]
zp ZP_WORD:11 [ print_char_cursor#31 print_char_cursor#43 print_char_cursor#42 print_char_cursor#12 print_char_cursor#40 print_char_cursor#44 print_char_cursor#49 ]
reg byte a [ print_byte::$0 ]
reg byte a [ print_byte::$2 ]