Skip to content

Commit

Permalink
tiny cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
irmen committed Oct 20, 2019
1 parent f6d4c90 commit b5d1e86
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 47 deletions.
10 changes: 10 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 28 additions & 28 deletions compiler/res/prog8lib/prog8lib.asm
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1669,7 +1669,7 @@ _loop_hi sty c64.SCRATCH_ZPREG
dey
bne _loop_hi

rts
rts
.pend

ror2_mem_ub .proc
Expand All @@ -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()}"
Expand All @@ -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
2 changes: 1 addition & 1 deletion compiler/src/prog8/ast/AstToplevel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/prog8/compiler/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/prog8/compiler/target/c64/AssemblyProgram.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 4 additions & 4 deletions compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/prog8/optimizer/SimplifyExpressions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/prog8/optimizer/StatementOptimizer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
*/


Expand Down
2 changes: 1 addition & 1 deletion compiler/src/prog8/parser/ModuleParsing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions compiler/src/prog8/vm/astvm/AstVm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/prog8/vm/astvm/Memory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand All @@ -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
Expand Down

0 comments on commit b5d1e86

Please sign in to comment.