Skip to content

Commit

Permalink
restructure c64 machinedefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
irmen committed Jul 13, 2019
1 parent 1f54200 commit 87c28cf
Show file tree
Hide file tree
Showing 14 changed files with 421 additions and 399 deletions.
4 changes: 2 additions & 2 deletions compiler/src/prog8/ast/processing/AstChecker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import prog8.ast.expressions.*
import prog8.ast.statements.*
import prog8.compiler.CompilationOptions
import prog8.compiler.HeapValues
import prog8.compiler.target.c64.FLOAT_MAX_NEGATIVE
import prog8.compiler.target.c64.FLOAT_MAX_POSITIVE
import prog8.compiler.target.c64.MachineDefinition.FLOAT_MAX_NEGATIVE
import prog8.compiler.target.c64.MachineDefinition.FLOAT_MAX_POSITIVE
import prog8.functions.BuiltinFunctions
import java.io.File

Expand Down
4 changes: 2 additions & 2 deletions compiler/src/prog8/ast/statements/AstStatements.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import prog8.ast.expressions.*
import prog8.ast.processing.IAstModifyingVisitor
import prog8.ast.processing.IAstVisitor
import prog8.compiler.HeapValues
import prog8.compiler.target.c64.Mflpt5
import prog8.compiler.target.c64.MachineDefinition


class BuiltinFunctionStatementPlaceholder(val name: String, override val position: Position) : IStatement {
Expand Down Expand Up @@ -757,7 +757,7 @@ class StructDecl(override val name: String,
when {
decl.datatype in ByteDatatypes -> 8
decl.datatype in WordDatatypes -> 16
decl.datatype==DataType.FLOAT -> Mflpt5.MemorySize
decl.datatype==DataType.FLOAT -> MachineDefinition.Mflpt5.MemorySize
decl.datatype in StringDatatypes -> TODO("stringvalue size")
decl.datatype in ArrayDatatypes -> decl.arraysize!!.size()!!
decl.datatype==DataType.STRUCT -> decl.struct!!.memorySize
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/prog8/compiler/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import prog8.ast.base.checkValid
import prog8.ast.base.reorderStatements
import prog8.ast.statements.Directive
import prog8.compiler.target.c64.AsmGen
import prog8.compiler.target.c64.C64Zeropage
import prog8.compiler.target.c64.MachineDefinition
import prog8.optimizer.constantFold
import prog8.optimizer.optimizeStatements
import prog8.optimizer.simplifyExpressions
Expand Down Expand Up @@ -108,7 +108,7 @@ fun compileProgram(filepath: Path,
}

if (writeAssembly) {
val zeropage = C64Zeropage(compilerOptions)
val zeropage = MachineDefinition.C64Zeropage(compilerOptions)
intermediate.allocateZeropage(zeropage)
val assembly = AsmGen(compilerOptions, intermediate, programAst.heap, zeropage).compileToAssembly(optimize)
assembly.assemble(compilerOptions)
Expand Down
21 changes: 11 additions & 10 deletions compiler/src/prog8/compiler/target/c64/AsmGen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class AsmGen(private val options: CompilationOptions, private val program: Inter
return name.replace("-", "")
}

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

if(program.loadAddress==0) // fix load address
program.loadAddress = if(options.launcher==LauncherType.BASIC) BASIC_LOAD_ADDRESS else RAW_LOAD_ADDRESS
program.loadAddress = if(options.launcher==LauncherType.BASIC)
MachineDefinition.BASIC_LOAD_ADDRESS else MachineDefinition.RAW_LOAD_ADDRESS

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

// the global list of all floating point constants for the whole program
for(flt in globalFloatConsts) {
val floatFill = makeFloatFill(Mflpt5.fromNumber(flt.key))
val floatFill = makeFloatFill(MachineDefinition.Mflpt5.fromNumber(flt.key))
out("${flt.value}\t.byte $floatFill ; float ${flt.key}")
}
}
Expand Down Expand Up @@ -371,7 +372,7 @@ class AsmGen(private val options: CompilationOptions, private val program: Inter
DataType.ARRAY_F -> {
// float arraysize
val array = heap.get(value.heapId!!).doubleArray!!
val floatFills = array.map { makeFloatFill(Mflpt5.fromNumber(it)) }
val floatFills = array.map { makeFloatFill(MachineDefinition.Mflpt5.fromNumber(it)) }
out(varname)
for(f in array.zip(floatFills))
out(" .byte ${f.second} ; float ${f.first}")
Expand Down Expand Up @@ -574,14 +575,14 @@ class AsmGen(private val options: CompilationOptions, private val program: Inter
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}")
Opcode.INC_INDEXED_VAR_FLOAT -> AsmFragment(
"""
lda #<($variable+${index*Mflpt5.MemorySize})
ldy #>($variable+${index*Mflpt5.MemorySize})
lda #<($variable+${index* MachineDefinition.Mflpt5.MemorySize})
ldy #>($variable+${index* MachineDefinition.Mflpt5.MemorySize})
jsr c64flt.inc_var_f
""")
Opcode.DEC_INDEXED_VAR_FLOAT -> AsmFragment(
"""
lda #<($variable+${index*Mflpt5.MemorySize})
ldy #>($variable+${index*Mflpt5.MemorySize})
lda #<($variable+${index* MachineDefinition.Mflpt5.MemorySize})
ldy #>($variable+${index* MachineDefinition.Mflpt5.MemorySize})
jsr c64flt.dec_var_f
""")

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

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

Expand Down
11 changes: 6 additions & 5 deletions compiler/src/prog8/compiler/target/c64/AsmOptimizer.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package prog8.compiler.target.c64

import prog8.compiler.toHex
import prog8.compiler.target.c64.MachineDefinition.ESTACK_LO_HEX
import prog8.compiler.target.c64.MachineDefinition.ESTACK_LO_PLUS1_HEX


// note: see https://wiki.nesdev.com/w/index.php/6502_assembly_optimisations
Expand Down Expand Up @@ -69,10 +70,10 @@ fun optimizeCmpSequence(linesByFour: List<List<IndexedValue<String>>>): List<Int
// the repeated lda can be removed
val removeLines = mutableListOf<Int>()
for(lines in linesByFour) {
if(lines[0].value.trim()=="lda ${(ESTACK_LO+1).toHex()},x" &&
if(lines[0].value.trim()=="lda $ESTACK_LO_PLUS1_HEX,x" &&
lines[1].value.trim().startsWith("cmp ") &&
lines[2].value.trim().startsWith("beq ") &&
lines[3].value.trim()=="lda ${(ESTACK_LO+1).toHex()},x") {
lines[3].value.trim()=="lda $ESTACK_LO_PLUS1_HEX,x") {
removeLines.add(lines[3].index) // remove the second lda
}
}
Expand All @@ -84,10 +85,10 @@ fun optimizeUselessStackByteWrites(linesByFour: List<List<IndexedValue<String>>>
// this is a lot harder for word values because the instruction sequence varies.
val removeLines = mutableListOf<Int>()
for(lines in linesByFour) {
if(lines[0].value.trim()=="sta ${ESTACK_LO.toHex()},x" &&
if(lines[0].value.trim()=="sta $ESTACK_LO_HEX,x" &&
lines[1].value.trim()=="dex" &&
lines[2].value.trim()=="inx" &&
lines[3].value.trim()=="lda ${ESTACK_LO.toHex()},x") {
lines[3].value.trim()=="lda $ESTACK_LO_HEX,x") {
removeLines.add(lines[0].index)
removeLines.add(lines[1].index)
removeLines.add(lines[2].index)
Expand Down
Loading

0 comments on commit 87c28cf

Please sign in to comment.