stricter type checking in assignments (less implicit typecasts)

This commit is contained in:
Irmen de Jong 2020-06-02 21:27:38 +02:00
parent 3723c22054
commit 3280993e2a
6 changed files with 202 additions and 79 deletions

View File

@ -42,13 +42,11 @@ class TypecastsAdder(val program: Program, val errors: ErrorReporter) : AstWalke
if(targetItype.isKnown && valueItype.isKnown) { if(targetItype.isKnown && valueItype.isKnown) {
val targettype = targetItype.typeOrElse(DataType.STRUCT) val targettype = targetItype.typeOrElse(DataType.STRUCT)
val valuetype = valueItype.typeOrElse(DataType.STRUCT) val valuetype = valueItype.typeOrElse(DataType.STRUCT)
if (valuetype != targettype) { if (valuetype != targettype && valuetype isAssignableTo targettype) {
if(valuetype.isAssignableTo(targettype)) {
return listOf(IAstModification.ReplaceNode( return listOf(IAstModification.ReplaceNode(
assignment.value, assignment.value,
TypecastExpression(assignment.value, targettype, true, assignment.value.position), TypecastExpression(assignment.value, targettype, true, assignment.value.position),
assignment)) assignment))
}
} }
} }
return emptyList() return emptyList()

View File

@ -19,6 +19,8 @@ import kotlin.math.pow
x + x + x -> ???? x*3 ??? words/bytes? x + x + x -> ???? x*3 ??? words/bytes?
x - x -> 0 x - x -> 0
x < 0 (for word, byte as well?): just test the most significant bit for 1
x >= 0 (for word, byte as well?): just test the most significant bit for 0
Investigate what optimizations binaryen has, also see https://egorbo.com/peephole-optimizations.html Investigate what optimizations binaryen has, also see https://egorbo.com/peephole-optimizations.html

View File

@ -17,79 +17,134 @@ graphics {
c64scr.clear_screen($10, 0) ; pixel color $1 (white) backround $0 (black) c64scr.clear_screen($10, 0) ; pixel color $1 (white) backround $0 (black)
} }
sub line(uword x1, ubyte y1, uword x2, ubyte y2) { sub line(uword x1, ubyte y1, uword x2, ubyte y2) {
; Bresenham algorithm ; Bresenham algorithm
word dx ; This code is a bit long because each of the 8 different octants has a dedicated loop.
word dy ; This minimizes the number of actual math operations, and allows usins simple ++ and -- operations.
byte ix = 1 ; TODO sort X/Y coordinates to eliminate some of the special cases
byte iy = 1
if x2>x1 {
dx = x2-x1
} else {
ix = -1
dx = x1-x2
}
if y2>y1 {
dy = y2-y1
} else {
iy = -1
dy = y1-y2
}
word dx2 = 2 * dx
word dy2 = 2 * dy
word d = 0 word d = 0
ubyte positive_ix = true
ubyte positive_iy = true
word dx = x2 - x1 as word
word dy = y2 as word - y1 as word
if dx < 0 {
dx = -dx
positive_ix = false
}
if dy < 0 {
dy = -dy
positive_iy = false
}
dx *= 2
dy *= 2
plotx = x1 plotx = x1
if dx >= dy { if dx >= dy {
if ix<0 { if positive_ix {
forever { if positive_iy {
graphics.plot(y1) forever {
if plotx==x2 graphics.plot(y1)
return if plotx==x2
plotx-- return
d += dy2 plotx++
if d > dx { d += dy
y1 += iy if d > dx {
d -= dx2 y1++
d -= dx
}
}
} else {
forever {
graphics.plot(y1)
if plotx==x2
return
plotx++
d += dy
if d > dx {
y1--
d -= dx
}
} }
} }
} else { } else {
forever { if positive_iy {
graphics.plot(y1) forever {
if plotx==x2 graphics.plot(y1)
return if plotx==x2
plotx++ return
d += dy2 plotx--
if d > dx { d += dy
y1 += iy if d > dx {
d -= dx2 y1++
d -= dx
}
}
} else {
forever {
graphics.plot(y1)
if plotx==x2
return
plotx--
d += dy
if d > dx {
y1--
d -= dx
}
} }
} }
} }
} else { }
if iy<0 { else {
forever { if positive_iy {
plot(y1) if positive_ix {
if y1 == y2 forever {
return plot(y1)
y1-- if y1 == y2
d += dx2 return
if d > dy { y1++
plotx += ix as word d += dx
d -= dy2 if d > dy {
plotx++
d -= dy
}
}
} else {
forever {
plot(y1)
if y1 == y2
return
y1++
d += dx
if d > dy {
plotx--
d -= dy
}
} }
} }
} else { } else {
forever { if positive_ix {
plot(y1) forever {
if y1 == y2 plot(y1)
return if y1 == y2
y1++ return
d += dx2 y1--
if d > dy { d += dx
plotx += ix as word if d > dy {
d -= dy2 plotx++
d -= dy
}
}
} else {
forever {
plot(y1)
if y1 == y2
return
y1--
d += dx
if d > dy {
plotx--
d -= dy
}
} }
} }
} }
@ -101,7 +156,7 @@ graphics {
ubyte ploty ubyte ploty
ubyte xx = radius ubyte xx = radius
ubyte yy = 0 ubyte yy = 0
byte decisionOver2 = 1-xx byte decisionOver2 = 1-xx as byte
while xx>=yy { while xx>=yy {
plotx = xcenter + xx plotx = xcenter + xx
@ -138,7 +193,7 @@ graphics {
; Midpoint algorithm, filled ; Midpoint algorithm, filled
ubyte xx = radius ubyte xx = radius
ubyte yy = 0 ubyte yy = 0
byte decisionOver2 = 1-xx byte decisionOver2 = 1-xx as byte
while xx>=yy { while xx>=yy {
ubyte cy_plus_yy = cy + yy ubyte cy_plus_yy = cy + yy
@ -180,7 +235,7 @@ graphics {
; @(addr) |= ormask[lsb(px) & 7] ; @(addr) |= ormask[lsb(px) & 7]
; } ; }
uword plotx ; 0..319 uword plotx ; 0..319 ; separate 'parameter' for plot()
asmsub plot(ubyte ploty @A) { ; plotx is 16 bits 0 to 319... doesn't fit in a register asmsub plot(ubyte ploty @A) { ; plotx is 16 bits 0 to 319... doesn't fit in a register
%asm {{ %asm {{

View File

@ -71,8 +71,8 @@ main {
ubyte dy = abs(y2 - y1) ubyte dy = abs(y2 - y1)
ubyte dx2 = 2 * dx ubyte dx2 = 2 * dx
ubyte dy2 = 2 * dy ubyte dy2 = 2 * dy
byte ix = sgn(x2 as byte - x1 as byte) ubyte ix = sgn(x2 as byte - x1 as byte) as ubyte
byte iy = sgn(y2 as byte - y1 as byte) ubyte iy = sgn(y2 as byte - y1 as byte) as ubyte
ubyte x = x1 ubyte x = x1
ubyte y = y1 ubyte y = y1
@ -107,7 +107,7 @@ main {
; Midpoint algorithm ; Midpoint algorithm
ubyte x = radius ubyte x = radius
ubyte y = 0 ubyte y = 0
byte decisionOver2 = 1-x byte decisionOver2 = 1-x as byte
while x>=y { while x>=y {
c64scr.setcc(xcenter + x, ycenter + y as ubyte, 81, 1) c64scr.setcc(xcenter + x, ycenter + y as ubyte, 81, 1)
@ -132,7 +132,7 @@ main {
; Midpoint algorithm, filled ; Midpoint algorithm, filled
ubyte x = radius ubyte x = radius
ubyte y = 0 ubyte y = 0
byte decisionOver2 = 1-x byte decisionOver2 = 1-x as byte
ubyte xx ubyte xx
while x>=y { while x>=y {

36
examples/strings.p8 Normal file
View File

@ -0,0 +1,36 @@
%import c64lib
%zeropage basicsafe
main {
sub start() {
str s1 = "apple"
str s2 = "banana"
byte[] a1 = [66,77,88,0]
ubyte i1 = 101
uword w1 = 000
c64.STROUT(s1)
c64.CHROUT('\n')
c64.STROUT(a1)
c64.CHROUT('\n')
c64scr.print("will play the music from boulderdash,\nmade in 1984 by peter liepa.\npress enter to start: ") ; XXX TODO FIX PRINT OUTPUT
; c64scr.print_uwhex(s1, true)
; w1 = &s1
; c64scr.print_uwhex(w1, true)
;
; c64scr.print_uwhex(a1, true)
; w1 = &a1
; c64scr.print_uwhex(w1, true)
;
; s1 = s1
; s1 = s2
; s2 = "zzz"
}
}

View File

@ -6,19 +6,51 @@
main { main {
sub start() { sub start() {
ubyte ubb = 44 line(1111,22,3333,22)
byte bbb=44 line(1111,22,1111,22)
uword uww = 4444 line(1111,22,666,22)
word www = 4444
float flt = 4.4
A = ubb line(1111,22,1111,33)
A = ubb as byte line(1111,22,1111,22)
A = bbb line(1111,22,1111,11)
A = bbb as ubyte
str foo = "foo"
} }
sub line(uword x1, ubyte y1, uword x2, ubyte y2) {
word dx
word dy
byte ix = 1
byte iy = 1
if x2>x1 {
dx = x2-x1 as word
} else {
ix = -1
dx = x1-x2 as word
}
if y2>y1 {
dy = y2-y1
} else {
iy = -1
dy = y1-y2
}
word dx2 = 2 * dx
word dy2 = 2 * dy
word d = 0
plotx = x1
c64scr.print("dx=")
c64scr.print_w(dx)
c64scr.print(" ix=")
c64scr.print_b(ix)
c64.CHROUT('\n')
c64scr.print("dy=")
c64scr.print_w(dy)
c64scr.print(" iy=")
c64scr.print_b(iy)
c64.CHROUT('\n')
}
uword plotx
} }