repeat countervars again in zeropage if possible, fix pointer arithmetic error

This commit is contained in:
Irmen de Jong
2025-05-17 11:51:34 +02:00
parent 65e34d4989
commit 50650b966b
5 changed files with 37 additions and 24 deletions

View File

@@ -838,7 +838,7 @@ class AsmGen6502Internal (
private fun repeatWordCount(iterations: Int, stmt: PtRepeatLoop) { private fun repeatWordCount(iterations: Int, stmt: PtRepeatLoop) {
require(iterations in 257..65536) { "invalid repeat count ${stmt.position}" } require(iterations in 257..65536) { "invalid repeat count ${stmt.position}" }
val repeatLabel = makeLabel("repeat") val repeatLabel = makeLabel("repeat")
val counterVar = createTempVarReused(BaseDataType.UWORD, isTargetCpu(CpuType.CPU65C02), stmt) val counterVar = createTempVarReused(BaseDataType.UWORD, true, stmt)
val loopcount = if(iterations==65536) 0 else if(iterations and 0x00ff == 0) iterations else iterations + 0x0100 // so that the loop can simply use a double-dec val loopcount = if(iterations==65536) 0 else if(iterations and 0x00ff == 0) iterations else iterations + 0x0100 // so that the loop can simply use a double-dec
out(""" out("""
ldy #>$loopcount ldy #>$loopcount
@@ -858,7 +858,7 @@ $repeatLabel""")
// note: A/Y must have been loaded with the number of iterations! // note: A/Y must have been loaded with the number of iterations!
// the iny + double dec is microoptimization of the 16 bit loop // the iny + double dec is microoptimization of the 16 bit loop
val repeatLabel = makeLabel("repeat") val repeatLabel = makeLabel("repeat")
val counterVar = createTempVarReused(BaseDataType.UWORD, false, stmt) val counterVar = createTempVarReused(BaseDataType.UWORD, true, stmt)
out(""" out("""
cmp #0 cmp #0
beq + beq +

View File

@@ -154,7 +154,7 @@ fun compileProgram(args: CompilerArguments): CompilationResult? {
println("*********** COMPILER AST END *************\n") println("*********** COMPILER AST END *************\n")
} }
val intermediateAst = SimplifiedAstMaker(program, options.compTarget, args.errors).transform() val intermediateAst = SimplifiedAstMaker(program, args.errors).transform()
val stMaker = SymbolTableMaker(intermediateAst, compilationOptions) val stMaker = SymbolTableMaker(intermediateAst, compilationOptions)
val symbolTable = stMaker.make() val symbolTable = stMaker.make()

View File

@@ -22,7 +22,7 @@ import kotlin.io.path.isRegularFile
/** /**
* Convert 'old' compiler-AST into the 'new' simplified AST with baked types. * Convert 'old' compiler-AST into the 'new' simplified AST with baked types.
*/ */
class SimplifiedAstMaker(private val program: Program, private val target: ICompilationTarget, private val errors: IErrorReporter) { class SimplifiedAstMaker(private val program: Program, private val errors: IErrorReporter) {
fun transform(): PtProgram { fun transform(): PtProgram {
val ptProgram = PtProgram( val ptProgram = PtProgram(
program.name, program.name,
@@ -789,9 +789,16 @@ class SimplifiedAstMaker(private val program: Program, private val target: IComp
plusorminus.add(PtNumber(BaseDataType.UWORD, total, expr.position)) plusorminus.add(PtNumber(BaseDataType.UWORD, total, expr.position))
return plusorminus return plusorminus
} }
} else {
if(structSize==1) {
// ptr + right, just keep it as it is
val plus = PtBinaryExpression("+", DataType.UWORD, expr.position)
plus.add(transformExpression(expr.left))
plus.add(transformExpression(expr.right))
return plus
} else { } else {
// ptr + right * structSize // ptr + right * structSize
val total = PtBinaryExpression("*", DataType.UWORD, expr.position).also { it.add(transformExpression(expr.right)) } val total = PtBinaryExpression("*", DataType.UWORD, expr.position)
total.add(transformExpression(expr.right)) total.add(transformExpression(expr.right))
total.add(PtNumber(BaseDataType.UWORD, structSize.toDouble(), expr.position)) total.add(PtNumber(BaseDataType.UWORD, structSize.toDouble(), expr.position))
val plusorminus = PtBinaryExpression(operator, resultDt, expr.position) val plusorminus = PtBinaryExpression(operator, resultDt, expr.position)
@@ -799,6 +806,7 @@ class SimplifiedAstMaker(private val program: Program, private val target: IComp
plusorminus.add(total) plusorminus.add(total)
return plusorminus return plusorminus
} }
}
} else if(!leftDt.isPointer && rightDt.isPointer) { } else if(!leftDt.isPointer && rightDt.isPointer) {
val resultDt = rightDt val resultDt = rightDt
val structSize = rightDt.size(program.memsizer) val structSize = rightDt.size(program.memsizer)
@@ -814,6 +822,13 @@ class SimplifiedAstMaker(private val program: Program, private val target: IComp
plusorminus.add(PtNumber(BaseDataType.UWORD, total, expr.position)) plusorminus.add(PtNumber(BaseDataType.UWORD, total, expr.position))
return plusorminus return plusorminus
} }
} else {
if(structSize==1) {
// ptr + left, just keep it as it is
val plus = PtBinaryExpression("+", DataType.UWORD, expr.position)
plus.add(transformExpression(expr.left))
plus.add(transformExpression(expr.right))
return plus
} else { } else {
// ptr + left * structSize // ptr + left * structSize
val total = PtBinaryExpression("*", DataType.UWORD, expr.position) val total = PtBinaryExpression("*", DataType.UWORD, expr.position)
@@ -824,6 +839,7 @@ class SimplifiedAstMaker(private val program: Program, private val target: IComp
plusorminus.add(total) plusorminus.add(total)
return plusorminus return plusorminus
} }
}
} else { } else {
throw FatalAstException("weird pointer arithmetic ${expr.position}") throw FatalAstException("weird pointer arithmetic ${expr.position}")
} }

View File

@@ -92,7 +92,7 @@ class TestAsmGenSymbols: StringSpec({
val astchecker = AstChecker(program, errors, options) val astchecker = AstChecker(program, errors, options)
astchecker.visit(program) astchecker.visit(program)
errors.report() errors.report()
val ptProgram = SimplifiedAstMaker(program, options.compTarget, errors).transform() val ptProgram = SimplifiedAstMaker(program, errors).transform()
val st = SymbolTableMaker(ptProgram, options).make() val st = SymbolTableMaker(ptProgram, options).make()
return AsmGen6502Internal(ptProgram, st, options, errors, 0) return AsmGen6502Internal(ptProgram, st, options, errors, 0)
} }

View File

@@ -1,9 +1,6 @@
TODO TODO
==== ====
Fix this: Assembler suddenly is a lot larger than before. (rockrunner seems fine?)
also: cx16 examples: bobs, cobra, fileseek, showbmx, tehtriz
STRUCTS and TYPED POINTERS STRUCTS and TYPED POINTERS
-------------------------- --------------------------