fix RPN comparison exprs

This commit is contained in:
Irmen de Jong 2023-03-19 13:10:41 +01:00
parent 374464a1f8
commit df2d7d4734
9 changed files with 89 additions and 33 deletions

View File

@ -6,7 +6,7 @@ import prog8.code.core.*
* Produces readable text from a [PtNode] (AST node, usually starting with PtProgram as root),
* passing it as a String to the specified receiver function.
*/
fun printAst(root: PtNode, output: (text: String) -> Unit) {
fun printAst(root: PtNode, skipLibraries: Boolean, output: (text: String) -> Unit) {
fun type(dt: DataType) = "!${dt.name.lowercase()}!"
fun txt(node: PtNode): String {
return when(node) {
@ -136,18 +136,24 @@ fun printAst(root: PtNode, output: (text: String) -> Unit) {
root.children.forEach {
walkAst(it) { node, depth ->
val txt = txt(node)
if(txt.isNotEmpty())
val library = if(node is PtBlock) node.library else node.definingBlock()?.library==true
if(!library || !skipLibraries) {
if (txt.isNotEmpty())
output(" ".repeat(depth) + txt(node))
}
}
}
println()
} else {
walkAst(root) { node, depth ->
val txt = txt(node)
if(txt.isNotEmpty())
val library = if(node is PtBlock) node.library else node.definingBlock()?.library==true
if(!library || !skipLibraries) {
if (txt.isNotEmpty())
output(" ".repeat(depth) + txt(node))
}
}
}
}
fun walkAst(root: PtNode, act: (node: PtNode, depth: Int) -> Unit) {

View File

@ -26,6 +26,8 @@ class AsmGen6502: ICodeGeneratorBackend {
errors.warn("EXPERIMENTAL RPN EXPRESSION NODES ARE USED. CODE SIZE+SPEED WILL SUFFER.", Position.DUMMY)
}
// printAst(program, true) { println(it) }
val asmgen = AsmGen6502Internal(program, symbolTable, options, errors)
return asmgen.compileToAssembly()
}

View File

@ -102,6 +102,28 @@ internal class AsmAssignTarget(val kind: TargetStorageKind,
RegisterOrPair.R15 -> AsmAssignTarget(TargetStorageKind.REGISTER, asmgen, if(signed) DataType.WORD else DataType.UWORD, scope, pos, register = registers)
}
}
fun isSameAs(left: PtExpression): Boolean =
when(kind) {
TargetStorageKind.VARIABLE -> {
val scopedName: String = if('.' in asmVarname)
asmVarname
else {
val scopeName = (scope as? PtNamedNode)?.scopedName
if (scopeName == null) asmVarname else "$scopeName.$asmVarname"
}
left is PtIdentifier && left.name==scopedName
}
TargetStorageKind.ARRAY -> {
left is PtArrayIndexer && left isSameAs array!!
}
TargetStorageKind.MEMORY -> {
left isSameAs memory!!
}
TargetStorageKind.REGISTER, TargetStorageKind.STACK -> {
false
}
}
}
internal class AsmAssignSource(val kind: SourceStorageKind,

View File

@ -366,7 +366,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
val value = assign.source.expression as PtRpn
val (left, oper, right) = value.finalOperation()
// TODO RPN the fallthrough if size>3 seems to generate not 100% correct code... (balls example fills the screen weird)
// TODO RPN the fallthrough if size>3 seems to generate very inefficient code...
if(value.children.size==3 && oper.operator in ComparisonOperators) {
assignRPNComparison(assign, value)
return true
@ -564,14 +564,39 @@ internal class AssignmentAsmGen(private val program: PtProgram,
val rightNum = right as? PtNumber
val jumpIfFalseLabel = asmgen.makeLabel("cmp")
when(assign.target.datatype) {
if(assign.target.isSameAs(left)) {
// In-place comparison Target = Target <compare> Right
// NOTE : this generates pretty inefficient code.... TODO RPN optimize?
val targetDt = assign.target.datatype
val tempVar = asmgen.getTempVarName(assign.target.datatype)
val tempTarget = AsmAssignTarget(TargetStorageKind.VARIABLE, asmgen, targetDt, comparison.definingISub(), comparison.position, variableAsmName = tempVar)
when (assign.target.datatype) {
in ByteDatatypes -> assignConstantByte(tempTarget, 0)
in WordDatatypes -> assignConstantWord(tempTarget, 0)
DataType.FLOAT -> assignConstantFloat(tempTarget, 0.0)
else -> throw AssemblyError("invalid dt")
}
asmgen.testNonzeroComparisonAndJump(left, oper.operator, right, jumpIfFalseLabel, leftNum, rightNum)
when (assign.target.datatype) {
in ByteDatatypes -> assignConstantByte(tempTarget, 1)
in WordDatatypes -> assignConstantWord(tempTarget, 1)
DataType.FLOAT -> assignConstantFloat(tempTarget, 1.0)
else -> throw AssemblyError("invalid dt")
}
asmgen.out(jumpIfFalseLabel)
val tempLeft = PtIdentifier(tempVar, targetDt, comparison.position)
tempLeft.parent=comparison
asmgen.assignExpressionTo(tempLeft, assign.target)
} else {
// Normal comparison Target = Left <compare> Right
when (assign.target.datatype) {
in ByteDatatypes -> assignConstantByte(assign.target, 0)
in WordDatatypes -> assignConstantWord(assign.target, 0)
DataType.FLOAT -> assignConstantFloat(assign.target, 0.0)
else -> throw AssemblyError("invalid dt")
}
asmgen.testNonzeroComparisonAndJump(left, oper.operator, right, jumpIfFalseLabel, leftNum, rightNum)
when(assign.target.datatype) {
when (assign.target.datatype) {
in ByteDatatypes -> assignConstantByte(assign.target, 1)
in WordDatatypes -> assignConstantWord(assign.target, 1)
DataType.FLOAT -> assignConstantFloat(assign.target, 1.0)
@ -579,6 +604,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
}
asmgen.out(jumpIfFalseLabel)
}
}
private fun attemptAssignOptimizedBinexpr(expr: PtBinaryExpression, assign: AsmAssignment): Boolean {
if(expr.operator in ComparisonOperators) {

View File

@ -120,7 +120,7 @@ fun compileProgram(args: CompilerArguments): CompilationResult? {
// println("*********** COMPILER AST RIGHT BEFORE ASM GENERATION *************")
// printProgram(program)
// println("*********** AST RIGHT BEFORE ASM GENERATION *************")
// printAst(intermediateAst, ::println)
// printAst(intermediateAst, true, ::println)
if(!createAssemblyAndAssemble(intermediateAst, args.errors, compilationOptions)) {
System.err.println("Error in codegeneration or assembler")

View File

@ -66,7 +66,7 @@ class TestIntermediateAst: FunSpec({
val fcall = (entry.children[4] as PtAssignment).value as PtFunctionCall
fcall.void shouldBe false
fcall.type shouldBe DataType.UBYTE
printAst(ast, ::println)
printAst(ast, false, ::println)
}
})

View File

@ -1,7 +1,6 @@
TODO
====
BRANCH: balls.p8 directions are wrong since the comparison operator fallthrough in attemptAssignOptimizedExprRPN
BRANCH: Fix the TODO RPN routines to be optimized assembly in RpnExpressionAsmGen.kt
BRANCH: check BinExprSplitter disablement any effect for RPN?
BRANCH: Implement RPN codegen for IR.

View File

@ -43,7 +43,7 @@ main {
DX[lp] = true
else
BX[lp]=BX[lp]-1
} else if DX[lp] {
} else {
if (BX[lp] == txt.DEFAULT_WIDTH-1) {
BX[lp] = txt.DEFAULT_WIDTH-2
DX[lp] = false
@ -56,7 +56,7 @@ main {
DY[lp] = true
else
BY[lp]=BY[lp]-1
} else if DY[lp] == 1 {
} else {
if (BY[lp] == txt.DEFAULT_HEIGHT-1) {
BY[lp] = txt.DEFAULT_HEIGHT-2
DY[lp] = false

View File

@ -1,4 +1,5 @@
%import math
%import textio
%zeropage basicsafe
; Note: this program is compatible with C64 and CX16.
@ -6,15 +7,15 @@
main {
sub start() {
; ubyte[255] BC
; bool[255] DX
if math.rnd() & 22 {
cx16.r0L++
bool x
ubyte y
repeat 20 {
x = math.rnd() & 1
y = ((math.rnd()&1)!=0)
txt.print_ub(x)
txt.spc()
txt.print_ub(y)
txt.nl()
}
; BC[2] = math.rnd() & 15
; BC[3] = math.rnd() & 15
; BC[4] = math.rnd() & 15
;DX[2] = math.rnd() & 1
}
}