fix crash when optimizing pipe expression too aggressively

This commit is contained in:
Irmen de Jong 2022-05-07 17:06:40 +02:00
parent 348b3036ff
commit 942c5cc04b
4 changed files with 72 additions and 27 deletions

View File

@ -18,7 +18,7 @@ class StatementOptimizer(private val program: Program,
override fun before(functionCallExpr: FunctionCallExpression, parent: Node): Iterable<IAstModification> { override fun before(functionCallExpr: FunctionCallExpression, parent: Node): Iterable<IAstModification> {
// if the first instruction in the called subroutine is a return statement with a simple value (NOT being a parameter), // if the first instruction in the called subroutine is a return statement with a simple value (NOT being a parameter),
// remove the jump altogeter and inline the returnvalue directly. // remove the jump altogeter and inline the returnvalue directly. (only if not part of a pipe expression)
fun scopePrefix(variable: IdentifierReference): IdentifierReference { fun scopePrefix(variable: IdentifierReference): IdentifierReference {
val target = variable.targetStatement(program) as INamedStatement val target = variable.targetStatement(program) as INamedStatement
@ -28,7 +28,7 @@ class StatementOptimizer(private val program: Program,
val subroutine = functionCallExpr.target.targetSubroutine(program) val subroutine = functionCallExpr.target.targetSubroutine(program)
if(subroutine!=null) { if(subroutine!=null) {
val first = subroutine.statements.asSequence().filterNot { it is VarDecl || it is Directive }.firstOrNull() val first = subroutine.statements.asSequence().filterNot { it is VarDecl || it is Directive }.firstOrNull()
if(first is Return && first.value?.isSimple==true) { if(first is Return && first.value?.isSimple==true && parent !is IPipe) {
val copy = when(val orig = first.value!!) { val copy = when(val orig = first.value!!) {
is AddressOf -> { is AddressOf -> {
val scoped = scopePrefix(orig.identifier) val scoped = scopePrefix(orig.identifier)

View File

@ -3,5 +3,68 @@
; Written by Irmen de Jong (irmen@razorvine.net) - license: GNU GPL 3.0 ; Written by Irmen de Jong (irmen@razorvine.net) - license: GNU GPL 3.0
math { math {
; required by the compiler, but the vm implements all math operations as instructions.
sub sin8u(ubyte angle) -> ubyte {
return 42 ; TODO
}
sub cos8u(ubyte angle) -> ubyte {
return 42 ; TODO
}
sub sin8(ubyte angle) -> byte {
return 42 ; TODO
}
sub cos8(ubyte angle) -> byte {
return 42 ; TODO
}
sub sin16(ubyte angle) -> word {
return 4242 ; TODO
}
sub cos16(ubyte angle) -> word {
return 4242 ; TODO
}
sub sin16u(ubyte angle) -> uword {
return 4242 ; TODO
}
sub cos16u(ubyte angle) -> uword {
return 4242 ; TODO
}
sub sinr8u(ubyte radians) -> ubyte {
return 42 ; TODO
}
sub cosr8u(ubyte radians) -> ubyte {
return 42 ; TODO
}
sub sinr8(ubyte radians) -> byte {
return 42 ; TODO
}
sub cosr8(ubyte radians) -> byte {
return 42 ; TODO
}
sub sinr16(ubyte radians) -> word {
return 4242 ; TODO
}
sub cosr16(ubyte radians) -> word {
return 4242 ; TODO
}
sub sinr16u(ubyte radians) -> uword {
return 4242 ; TODO
}
sub cosr16u(ubyte radians) -> uword {
return 4242 ; TODO
}
} }

View File

@ -472,6 +472,7 @@ class AstToSourceTextConverter(val output: (text: String) -> Unit, val program:
private fun printPipe(source: Expression, segments: Iterable<Expression>) { private fun printPipe(source: Expression, segments: Iterable<Expression>) {
source.accept(this) source.accept(this)
output(" |> ")
segments.first().accept(this) segments.first().accept(this)
outputln("") outputln("")
scopelevel++ scopelevel++

View File

@ -1,4 +1,5 @@
%import textio %import textio
%import math
%zeropage dontuse %zeropage dontuse
@ -7,31 +8,11 @@
main { main {
sub start() { sub start() {
const ubyte times=3 ubyte source=99
ubyte value= source |> math.sin8u() |> math.cos8u()
txt.print_ub(value)
; expected output: aaabbb aaa bbb ; expected output: aaabbb aaa bbb
txt.print("aaa"+"bbb"+"ccc")
txt.nl()
; txt.print("aaa")
; txt.nl()
; txt.print("bbb")
; txt.nl()
; expected output: xxx xxxxxx xxxxxxxxx xxx
; txt.print("xxx"*(times-2))
; txt.nl()
; txt.print("xxx"*(times-1))
; txt.nl()
txt.print("xxx"*times)
txt.nl()
; txt.print("xxx")
; txt.nl()
sys.exit(42)
; floats.print_f(-42.42)
; float f1 = 1.2345
; float f2 = -9.99
; float f3
; f3 = floats.sin(f3)
; floats.print_f(f3)
; txt.nl()
; float f1 = 1.555 ; float f1 = 1.555
; floats.print_f(floats.sin(f1)) ; floats.print_f(floats.sin(f1))