tiny cleanups

This commit is contained in:
Irmen de Jong 2019-10-13 07:35:51 +02:00
parent f6d4c90dea
commit b5d1e8653d
11 changed files with 57 additions and 47 deletions

View File

@ -1,6 +1,16 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<Languages>
<language minSize="100" isEnabled="false" name="JavaScript" />
<language isEnabled="false" name="Groovy" />
<language isEnabled="false" name="Style Sheets" />
<language minSize="70" name="Kotlin" />
<language isEnabled="false" name="TypeScript" />
<language isEnabled="false" name="ActionScript" />
</Languages>
</inspection_tool>
<inspection_tool class="SpellCheckingInspection" enabled="true" level="TYPO" enabled_by_default="true">
<option name="processCode" value="false" />
<option name="processLiterals" value="true" />

View File

@ -1417,7 +1417,7 @@ _l3 ldy c64.SCRATCH_ZPB1 ;where the largest value shall be put
rts
.pend
sort_b .proc
; 8bit signed sort
; sorting subroutine coded by mats rosengren (mats.rosengren@esa.int)
@ -1471,7 +1471,7 @@ _sort_loop ldy c64.SCRATCH_ZPB1 ;start of subroutine sort
sta _work3+1
dey
jmp _l2
_l1 dey
_l1 dey
dey
beq _l3
iny
@ -1506,12 +1506,12 @@ _l3 ldy c64.SCRATCH_ZPB1 ;where the largest value shall be put
dec c64.SCRATCH_ZPB1 ;end of the shorter sequence still left
dec c64.SCRATCH_ZPB1
bne _sort_loop ;start working with the shorter sequence
rts
rts
_work1 .byte 0
_work3 .word 0
.pend
sort_w .proc
; 16bit signed sort
; sorting subroutine coded by mats rosengren (mats.rosengren@esa.int)
@ -1532,7 +1532,7 @@ _sort_loop ldy c64.SCRATCH_ZPB1 ;start of subroutine sort
sta _work3+1
dey
jmp _l2
_l1 dey
_l1 dey
dey
beq _l3
lda (c64.SCRATCH_ZPWORD1),y
@ -1543,7 +1543,7 @@ _l1 dey
sbc c64.SCRATCH_ZPWORD2+1
bvc +
eor #$80
+ bmi _l1
+ bmi _l1
_l2 sty _work1 ;index of potentially largest value
lda (c64.SCRATCH_ZPWORD1),y
sta c64.SCRATCH_ZPWORD2 ;potentially largest value
@ -1568,12 +1568,12 @@ _l3 ldy c64.SCRATCH_ZPB1 ;where the largest value shall be put
dec c64.SCRATCH_ZPB1 ;end of the shorter sequence still left
dec c64.SCRATCH_ZPB1
bne _sort_loop ;start working with the shorter sequence
rts
rts
_work1 .byte 0
_work3 .word 0
.pend
.pend
reverse_b .proc
; --- reverse an array of bytes (in-place)
; inputs: pointer to array in c64.SCRATCH_ZPWORD1, length in A
@ -1607,7 +1607,7 @@ _loop sty c64.SCRATCH_ZPREG
rts
.pend
reverse_w .proc
; --- reverse an array of words (in-place)
; inputs: pointer to array in c64.SCRATCH_ZPWORD1, length in A
@ -1669,7 +1669,7 @@ _loop_hi sty c64.SCRATCH_ZPREG
dey
bne _loop_hi
rts
rts
.pend
ror2_mem_ub .proc
@ -1687,7 +1687,7 @@ ror2_mem_ub .proc
+ sta (c64.SCRATCH_ZPWORD1),y
rts
.pend
rol2_mem_ub .proc
; -- in-place 8-bit rol of byte at memory location on stack
;" lda ${number.toHex()} | cmp #\$80 | rol a | sta ${number.toHex()}"
@ -1703,59 +1703,59 @@ rol2_mem_ub .proc
sta (c64.SCRATCH_ZPWORD1),y
rts
.pend
lsl_array_b .proc
.warn "lsl_array_b" ; TODO
.pend
lsl_array_w .proc
.warn "lsl_array_w" ; TODO
.pend
lsr_array_ub .proc
.warn "lsr_array_ub" ; TODO
.pend
lsr_array_b .proc
.warn "lsr_array_b" ; TODO
.pend
lsr_array_uw .proc
.warn "lsl_array_uw" ; TODO
.warn "lsr_array_uw" ; TODO
.pend
lsr_array_w .proc
.warn "lsr_array_w" ; TODO
.pend
rol_array_ub .proc
.warn "rol_array_ub" ; TODO
.pend
rol_array_uw .proc
.warn "rol_array_uw" ; TODO
.pend
rol2_array_ub .proc
.warn "rol2_array_ub" ; TODO
.pend
rol2_array_uw .proc
.warn "rol2_array_uw" ; TODO
.pend
ror_array_ub .proc
.warn "ror_array_ub" ; TODO
.pend
ror_array_uw .proc
.warn "ror_array_uw" ; TODO
.pend
ror2_array_ub .proc
.warn "ror2_array_ub" ; TODO
.pend
ror2_array_uw .proc
.warn "ror2_array_uw" ; TODO
.pend

View File

@ -176,7 +176,7 @@ class Program(val name: String, val modules: MutableList<Module>) {
var actualLoadAddress: Int = 0
fun entrypoint(): Subroutine? {
val mainBlocks = modules.flatMap { it.statements }.filter { b -> b is Block && b.name=="main" }.map { it as Block }
val mainBlocks = allBlocks().filter { it.name=="main" }
if(mainBlocks.size > 1)
throw FatalAstException("more than one 'main' block")
return if(mainBlocks.isEmpty()) {

View File

@ -103,7 +103,7 @@ fun compileProgram(filepath: Path,
// asm generation directly from the Ast, no need for intermediate code
val zeropage = MachineDefinition.C64Zeropage(compilerOptions)
programAst.anonscopeVarsCleanup()
val assembly = AsmGen(programAst, compilerOptions, zeropage, outputDir).compileToAssembly(optimize)
val assembly = AsmGen(programAst, zeropage, compilerOptions, outputDir).compileToAssembly(optimize)
assembly.assemble(compilerOptions)
programName = assembly.name
}

View File

@ -5,7 +5,7 @@ import prog8.compiler.OutputType
import java.nio.file.Path
import kotlin.system.exitProcess
class AssemblyProgram(val name: String, val outputDir: Path) {
class AssemblyProgram(val name: String, outputDir: Path) {
private val assemblyFile = outputDir.resolve("$name.asm")
private val prgFile = outputDir.resolve("$name.prg")
private val binFile = outputDir.resolve("$name.bin")

View File

@ -25,10 +25,10 @@ import kotlin.math.absoluteValue
internal class AssemblyError(msg: String) : RuntimeException(msg)
internal class AsmGen(val program: Program,
val options: CompilationOptions,
val zeropage: Zeropage,
val outputDir: Path) {
internal class AsmGen(private val program: Program,
private val zeropage: Zeropage,
private val options: CompilationOptions,
private val outputDir: Path) {
private val assemblyLines = mutableListOf<String>()
private val globalFloatConsts = mutableMapOf<Double, String>() // all float values in the entire program (value -> varname)

View File

@ -11,7 +11,7 @@ import kotlin.math.log2
import kotlin.math.pow
/*
todo advanced expression optimization: common (sub) expression elimination (turn common expressions into single subroutine call + introduce variable to hold it)
todo add more expression optimizations
Also see https://egorbo.com/peephole-optimizations.html

View File

@ -16,8 +16,8 @@ import kotlin.math.floor
/*
TODO: analyse for unreachable code and remove that (f.i. code after goto or return that has no label so can never be jumped to) + print warning about this
TODO: proper inlining of small subroutines (correctly renaming/relocating all variables in them and refs to those as well, or restrict to subs without variables?)
TODO: remove unreachable code?
TODO: proper inlining of tiny subroutines (correctly renaming/relocating all variables in them and refs to those as well, or restrict to subs without variables?)
*/

View File

@ -121,7 +121,7 @@ private fun executeImportDirective(program: Program, import: Directive, source:
if(existing!=null)
return null
val resource = tryGetEmbeddedResource(moduleName+".p8")
val resource = tryGetEmbeddedResource("$moduleName.p8")
val importedModule =
if(resource!=null) {
// load the module from the embedded resource

View File

@ -173,9 +173,9 @@ class AstVm(val program: Program) {
fun memwrite(address: Int, value: Short): Short {
if(address==0xa0 || address==0xa1 || address==0xa2) {
// a write to the jiffy clock, update the clock offset for the irq
val timeHi = if(address==0xa0) value else mem.getUByte_DMA(0xa0)
val timeMid = if(address==0xa1) value else mem.getUByte_DMA(0xa1)
val timeLo = if(address==0xa2) value else mem.getUByte_DMA(0xa2)
val timeHi = if(address==0xa0) value else mem.getUByteDirectly(0xa0)
val timeMid = if(address==0xa1) value else mem.getUByteDirectly(0xa1)
val timeLo = if(address==0xa2) value else mem.getUByteDirectly(0xa2)
val jiffies = (timeHi.toInt() shl 16) + (timeMid.toInt() shl 8) + timeLo
rtcOffset = bootTime - (jiffies*1000/60)
}
@ -260,9 +260,9 @@ class AstVm(val program: Program) {
rtcOffset = timeStamp
}
// update the C-64 60hz jiffy clock in the ZP addresses:
mem.setUByte_DMA(0x00a0, (jiffies ushr 16).toShort())
mem.setUByte_DMA(0x00a1, (jiffies ushr 8 and 255).toShort())
mem.setUByte_DMA(0x00a2, (jiffies and 255).toShort())
mem.setUByteDirectly(0x00a0, (jiffies ushr 16).toShort())
mem.setUByteDirectly(0x00a1, (jiffies ushr 8 and 255).toShort())
mem.setUByteDirectly(0x00a2, (jiffies and 255).toShort())
}
private val runtimeVariables = RuntimeVariables()

View File

@ -21,7 +21,7 @@ class Memory(private val readObserver: (address: Int, value: Short) -> Short,
else mem[address]
}
fun getUByte_DMA(address: Int): Short {
fun getUByteDirectly(address: Int): Short {
return mem[address]
}
@ -39,7 +39,7 @@ class Memory(private val readObserver: (address: Int, value: Short) -> Short,
else value
}
fun setUByte_DMA(address: Int, value: Short) {
fun setUByteDirectly(address: Int, value: Short) {
if(value !in 0..255)
throw VmExecutionException("ubyte value out of range $value")
mem[address] = value