Skip to content

Commit 87c28cf

Browse files
committed
restructure c64 machinedefinition
1 parent 1f54200 commit 87c28cf

File tree

14 files changed

+421
-399
lines changed

14 files changed

+421
-399
lines changed

compiler/src/prog8/ast/processing/AstChecker.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import prog8.ast.expressions.*
77
import prog8.ast.statements.*
88
import prog8.compiler.CompilationOptions
99
import prog8.compiler.HeapValues
10-
import prog8.compiler.target.c64.FLOAT_MAX_NEGATIVE
11-
import prog8.compiler.target.c64.FLOAT_MAX_POSITIVE
10+
import prog8.compiler.target.c64.MachineDefinition.FLOAT_MAX_NEGATIVE
11+
import prog8.compiler.target.c64.MachineDefinition.FLOAT_MAX_POSITIVE
1212
import prog8.functions.BuiltinFunctions
1313
import java.io.File
1414

compiler/src/prog8/ast/statements/AstStatements.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import prog8.ast.expressions.*
66
import prog8.ast.processing.IAstModifyingVisitor
77
import prog8.ast.processing.IAstVisitor
88
import prog8.compiler.HeapValues
9-
import prog8.compiler.target.c64.Mflpt5
9+
import prog8.compiler.target.c64.MachineDefinition
1010

1111

1212
class BuiltinFunctionStatementPlaceholder(val name: String, override val position: Position) : IStatement {
@@ -757,7 +757,7 @@ class StructDecl(override val name: String,
757757
when {
758758
decl.datatype in ByteDatatypes -> 8
759759
decl.datatype in WordDatatypes -> 16
760-
decl.datatype==DataType.FLOAT -> Mflpt5.MemorySize
760+
decl.datatype==DataType.FLOAT -> MachineDefinition.Mflpt5.MemorySize
761761
decl.datatype in StringDatatypes -> TODO("stringvalue size")
762762
decl.datatype in ArrayDatatypes -> decl.arraysize!!.size()!!
763763
decl.datatype==DataType.STRUCT -> decl.struct!!.memorySize

compiler/src/prog8/compiler/Main.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import prog8.ast.base.checkValid
77
import prog8.ast.base.reorderStatements
88
import prog8.ast.statements.Directive
99
import prog8.compiler.target.c64.AsmGen
10-
import prog8.compiler.target.c64.C64Zeropage
10+
import prog8.compiler.target.c64.MachineDefinition
1111
import prog8.optimizer.constantFold
1212
import prog8.optimizer.optimizeStatements
1313
import prog8.optimizer.simplifyExpressions
@@ -108,7 +108,7 @@ fun compileProgram(filepath: Path,
108108
}
109109

110110
if (writeAssembly) {
111-
val zeropage = C64Zeropage(compilerOptions)
111+
val zeropage = MachineDefinition.C64Zeropage(compilerOptions)
112112
intermediate.allocateZeropage(zeropage)
113113
val assembly = AsmGen(compilerOptions, intermediate, programAst.heap, zeropage).compileToAssembly(optimize)
114114
assembly.assemble(compilerOptions)

compiler/src/prog8/compiler/target/c64/AsmGen.kt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class AsmGen(private val options: CompilationOptions, private val program: Inter
147147
return name.replace("-", "")
148148
}
149149

150-
private fun makeFloatFill(flt: Mflpt5): String {
150+
private fun makeFloatFill(flt: MachineDefinition.Mflpt5): String {
151151
val b0 = "$"+flt.b0.toString(16).padStart(2, '0')
152152
val b1 = "$"+flt.b1.toString(16).padStart(2, '0')
153153
val b2 = "$"+flt.b2.toString(16).padStart(2, '0')
@@ -165,7 +165,8 @@ class AsmGen(private val options: CompilationOptions, private val program: Inter
165165
out("\n.cpu '6502'\n.enc 'none'\n")
166166

167167
if(program.loadAddress==0) // fix load address
168-
program.loadAddress = if(options.launcher==LauncherType.BASIC) BASIC_LOAD_ADDRESS else RAW_LOAD_ADDRESS
168+
program.loadAddress = if(options.launcher==LauncherType.BASIC)
169+
MachineDefinition.BASIC_LOAD_ADDRESS else MachineDefinition.RAW_LOAD_ADDRESS
169170

170171
when {
171172
options.launcher == LauncherType.BASIC -> {
@@ -221,7 +222,7 @@ class AsmGen(private val options: CompilationOptions, private val program: Inter
221222

222223
// the global list of all floating point constants for the whole program
223224
for(flt in globalFloatConsts) {
224-
val floatFill = makeFloatFill(Mflpt5.fromNumber(flt.key))
225+
val floatFill = makeFloatFill(MachineDefinition.Mflpt5.fromNumber(flt.key))
225226
out("${flt.value}\t.byte $floatFill ; float ${flt.key}")
226227
}
227228
}
@@ -371,7 +372,7 @@ class AsmGen(private val options: CompilationOptions, private val program: Inter
371372
DataType.ARRAY_F -> {
372373
// float arraysize
373374
val array = heap.get(value.heapId!!).doubleArray!!
374-
val floatFills = array.map { makeFloatFill(Mflpt5.fromNumber(it)) }
375+
val floatFills = array.map { makeFloatFill(MachineDefinition.Mflpt5.fromNumber(it)) }
375376
out(varname)
376377
for(f in array.zip(floatFills))
377378
out(" .byte ${f.second} ; float ${f.first}")
@@ -574,14 +575,14 @@ class AsmGen(private val options: CompilationOptions, private val program: Inter
574575
Opcode.DEC_INDEXED_VAR_W, Opcode.DEC_INDEXED_VAR_UW -> AsmFragment(" lda $variable+${index*2} | bne + | dec $variable+${index*2+1} |+ | dec $variable+${index*2}")
575576
Opcode.INC_INDEXED_VAR_FLOAT -> AsmFragment(
576577
"""
577-
lda #<($variable+${index*Mflpt5.MemorySize})
578-
ldy #>($variable+${index*Mflpt5.MemorySize})
578+
lda #<($variable+${index* MachineDefinition.Mflpt5.MemorySize})
579+
ldy #>($variable+${index* MachineDefinition.Mflpt5.MemorySize})
579580
jsr c64flt.inc_var_f
580581
""")
581582
Opcode.DEC_INDEXED_VAR_FLOAT -> AsmFragment(
582583
"""
583-
lda #<($variable+${index*Mflpt5.MemorySize})
584-
ldy #>($variable+${index*Mflpt5.MemorySize})
584+
lda #<($variable+${index* MachineDefinition.Mflpt5.MemorySize})
585+
ldy #>($variable+${index* MachineDefinition.Mflpt5.MemorySize})
585586
jsr c64flt.dec_var_f
586587
""")
587588

@@ -591,8 +592,8 @@ class AsmGen(private val options: CompilationOptions, private val program: Inter
591592

592593
private fun sameIndexedVarOperation(variable: String, indexVar: String, ins: Instruction): AsmFragment? {
593594
// an in place operation that consists of a push-value / op / push-index-var / pop-into-indexed-var
594-
val saveX = " stx ${C64Zeropage.SCRATCH_B1} |"
595-
val restoreX = " | ldx ${C64Zeropage.SCRATCH_B1}"
595+
val saveX = " stx ${MachineDefinition.C64Zeropage.SCRATCH_B1} |"
596+
val restoreX = " | ldx ${MachineDefinition.C64Zeropage.SCRATCH_B1}"
596597
val loadXWord: String
597598
val loadX: String
598599

compiler/src/prog8/compiler/target/c64/AsmOptimizer.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package prog8.compiler.target.c64
22

3-
import prog8.compiler.toHex
3+
import prog8.compiler.target.c64.MachineDefinition.ESTACK_LO_HEX
4+
import prog8.compiler.target.c64.MachineDefinition.ESTACK_LO_PLUS1_HEX
45

56

67
// note: see https://wiki.nesdev.com/w/index.php/6502_assembly_optimisations
@@ -69,10 +70,10 @@ fun optimizeCmpSequence(linesByFour: List<List<IndexedValue<String>>>): List<Int
6970
// the repeated lda can be removed
7071
val removeLines = mutableListOf<Int>()
7172
for(lines in linesByFour) {
72-
if(lines[0].value.trim()=="lda ${(ESTACK_LO+1).toHex()},x" &&
73+
if(lines[0].value.trim()=="lda $ESTACK_LO_PLUS1_HEX,x" &&
7374
lines[1].value.trim().startsWith("cmp ") &&
7475
lines[2].value.trim().startsWith("beq ") &&
75-
lines[3].value.trim()=="lda ${(ESTACK_LO+1).toHex()},x") {
76+
lines[3].value.trim()=="lda $ESTACK_LO_PLUS1_HEX,x") {
7677
removeLines.add(lines[3].index) // remove the second lda
7778
}
7879
}
@@ -84,10 +85,10 @@ fun optimizeUselessStackByteWrites(linesByFour: List<List<IndexedValue<String>>>
8485
// this is a lot harder for word values because the instruction sequence varies.
8586
val removeLines = mutableListOf<Int>()
8687
for(lines in linesByFour) {
87-
if(lines[0].value.trim()=="sta ${ESTACK_LO.toHex()},x" &&
88+
if(lines[0].value.trim()=="sta $ESTACK_LO_HEX,x" &&
8889
lines[1].value.trim()=="dex" &&
8990
lines[2].value.trim()=="inx" &&
90-
lines[3].value.trim()=="lda ${ESTACK_LO.toHex()},x") {
91+
lines[3].value.trim()=="lda $ESTACK_LO_HEX,x") {
9192
removeLines.add(lines[0].index)
9293
removeLines.add(lines[1].index)
9394
removeLines.add(lines[2].index)

0 commit comments

Comments
 (0)