mirror of
				https://github.com/irmen/prog8.git
				synced 2025-11-03 19:16:13 +00:00 
			
		
		
		
	repeat countervars again in zeropage if possible, fix pointer arithmetic error
This commit is contained in:
		@@ -838,7 +838,7 @@ class AsmGen6502Internal (
 | 
			
		||||
    private fun repeatWordCount(iterations: Int, stmt: PtRepeatLoop) {
 | 
			
		||||
        require(iterations in 257..65536) { "invalid repeat count ${stmt.position}" }
 | 
			
		||||
        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
 | 
			
		||||
        out("""
 | 
			
		||||
            ldy  #>$loopcount
 | 
			
		||||
@@ -858,7 +858,7 @@ $repeatLabel""")
 | 
			
		||||
        // note: A/Y must have been loaded with the number of iterations!
 | 
			
		||||
        // the iny + double dec is microoptimization of the 16 bit loop
 | 
			
		||||
        val repeatLabel = makeLabel("repeat")
 | 
			
		||||
        val counterVar = createTempVarReused(BaseDataType.UWORD, false, stmt)
 | 
			
		||||
        val counterVar = createTempVarReused(BaseDataType.UWORD, true, stmt)
 | 
			
		||||
        out("""
 | 
			
		||||
            cmp  #0
 | 
			
		||||
            beq  +
 | 
			
		||||
 
 | 
			
		||||
@@ -154,7 +154,7 @@ fun compileProgram(args: CompilerArguments): CompilationResult? {
 | 
			
		||||
                    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 symbolTable = stMaker.make()
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -22,7 +22,7 @@ import kotlin.io.path.isRegularFile
 | 
			
		||||
/**
 | 
			
		||||
 *  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 {
 | 
			
		||||
        val ptProgram = PtProgram(
 | 
			
		||||
            program.name,
 | 
			
		||||
@@ -790,14 +790,22 @@ class SimplifiedAstMaker(private val program: Program, private val target: IComp
 | 
			
		||||
                    return plusorminus
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                // ptr + right * structSize
 | 
			
		||||
                val total = PtBinaryExpression("*", DataType.UWORD, expr.position).also { it.add(transformExpression(expr.right)) }
 | 
			
		||||
                total.add(transformExpression(expr.right))
 | 
			
		||||
                total.add(PtNumber(BaseDataType.UWORD, structSize.toDouble(), expr.position))
 | 
			
		||||
                val plusorminus = PtBinaryExpression(operator, resultDt, expr.position)
 | 
			
		||||
                plusorminus.add(transformExpression(expr.left))
 | 
			
		||||
                plusorminus.add(total)
 | 
			
		||||
                return plusorminus
 | 
			
		||||
                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 {
 | 
			
		||||
                    // ptr + right * structSize
 | 
			
		||||
                    val total = PtBinaryExpression("*", DataType.UWORD, expr.position)
 | 
			
		||||
                    total.add(transformExpression(expr.right))
 | 
			
		||||
                    total.add(PtNumber(BaseDataType.UWORD, structSize.toDouble(), expr.position))
 | 
			
		||||
                    val plusorminus = PtBinaryExpression(operator, resultDt, expr.position)
 | 
			
		||||
                    plusorminus.add(transformExpression(expr.left))
 | 
			
		||||
                    plusorminus.add(total)
 | 
			
		||||
                    return plusorminus
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        } else if(!leftDt.isPointer && rightDt.isPointer) {
 | 
			
		||||
            val resultDt = rightDt
 | 
			
		||||
@@ -815,14 +823,22 @@ class SimplifiedAstMaker(private val program: Program, private val target: IComp
 | 
			
		||||
                    return plusorminus
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                // ptr + left  * structSize
 | 
			
		||||
                val total = PtBinaryExpression("*", DataType.UWORD, expr.position)
 | 
			
		||||
                total.add(transformExpression(expr.left))
 | 
			
		||||
                total.add(PtNumber(BaseDataType.UWORD, structSize.toDouble(), expr.position))
 | 
			
		||||
                val plusorminus = PtBinaryExpression(operator, resultDt, expr.position)
 | 
			
		||||
                plusorminus.add(transformExpression(expr.right))
 | 
			
		||||
                plusorminus.add(total)
 | 
			
		||||
                return plusorminus
 | 
			
		||||
                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 {
 | 
			
		||||
                    // ptr + left  * structSize
 | 
			
		||||
                    val total = PtBinaryExpression("*", DataType.UWORD, expr.position)
 | 
			
		||||
                    total.add(transformExpression(expr.left))
 | 
			
		||||
                    total.add(PtNumber(BaseDataType.UWORD, structSize.toDouble(), expr.position))
 | 
			
		||||
                    val plusorminus = PtBinaryExpression(operator, resultDt, expr.position)
 | 
			
		||||
                    plusorminus.add(transformExpression(expr.right))
 | 
			
		||||
                    plusorminus.add(total)
 | 
			
		||||
                    return plusorminus
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            throw FatalAstException("weird pointer arithmetic ${expr.position}")
 | 
			
		||||
 
 | 
			
		||||
@@ -92,7 +92,7 @@ class TestAsmGenSymbols: StringSpec({
 | 
			
		||||
        val astchecker = AstChecker(program, errors, options)
 | 
			
		||||
        astchecker.visit(program)
 | 
			
		||||
        errors.report()
 | 
			
		||||
        val ptProgram = SimplifiedAstMaker(program, options.compTarget, errors).transform()
 | 
			
		||||
        val ptProgram = SimplifiedAstMaker(program, errors).transform()
 | 
			
		||||
        val st = SymbolTableMaker(ptProgram, options).make()
 | 
			
		||||
        return AsmGen6502Internal(ptProgram, st, options, errors, 0)
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,9 +1,6 @@
 | 
			
		||||
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
 | 
			
		||||
--------------------------
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user