some optimizations

This commit is contained in:
Irmen de Jong 2019-01-01 21:53:59 +01:00
parent 39a8b76534
commit 72d58d5856
7 changed files with 30 additions and 12 deletions

View File

@ -31,6 +31,7 @@
float[len(zcoor)] rotatedz
sub start() {
set_irqvec()
while true {
if irq.time_changed {
irq.time_changed = 0

View File

@ -420,8 +420,8 @@ interface INameScope {
namespace.allLabelsAndVariables().forEach {
println(" ".repeat(4 * (1 + indent)) + "${it.key} -> ${it.value::class.simpleName} at ${it.value.position}")
}
namespace.statements.filter { it is INameScope }.forEach {
printNames(indent+1, it as INameScope)
namespace.statements.filterIsInstance<INameScope>().forEach {
printNames(indent+1, it)
}
}
printNames(0, this)

View File

@ -61,7 +61,7 @@ class AstChecker(private val namespace: INameScope,
override fun process(module: Module) {
super.process(module)
val directives = module.statements.asSequence().filter { it is Directive }.groupBy { (it as Directive).directive }
val directives = module.statements.filterIsInstance<Directive>().groupBy { it.directive }
directives.filter { it.value.size > 1 }.forEach{ entry ->
when(entry.key) {
"%output", "%launcher", "%zeropage", "%address" ->

View File

@ -28,7 +28,7 @@ class StatementReorderer(private val namespace: INameScope, private val heap: He
module.statements.remove(mainBlock)
module.statements.add(0, mainBlock)
}
val varDecls = module.statements.filter { it is VarDecl }
val varDecls = module.statements.filterIsInstance<VarDecl>()
module.statements.removeAll(varDecls)
module.statements.addAll(0, varDecls)
val directives = module.statements.filter {it is Directive && it.directive in directivesToMove}
@ -44,7 +44,7 @@ class StatementReorderer(private val namespace: INameScope, private val heap: He
}
override fun process(block: Block): IStatement {
val subroutines = block.statements.asSequence().filter { it is Subroutine }.map { it as Subroutine }.toList()
val subroutines = block.statements.filterIsInstance<Subroutine>()
var numSubroutinesAtEnd = 0
// move all subroutines to the end of the block
for (subroutine in subroutines) {
@ -91,7 +91,7 @@ class StatementReorderer(private val namespace: INameScope, private val heap: He
override fun process(subroutine: Subroutine): IStatement {
super.process(subroutine)
val varDecls = subroutine.statements.filter { it is VarDecl }
val varDecls = subroutine.statements.filterIsInstance<VarDecl>()
subroutine.statements.removeAll(varDecls)
subroutine.statements.addAll(0, varDecls)
val directives = subroutine.statements.filter {it is Directive && it.directive in directivesToMove}

View File

@ -160,7 +160,7 @@ private class StatementTranslator(private val prog: IntermediateProgram,
}
private fun processVariables(scope: INameScope) {
for(variable in scope.statements.asSequence().filter {it is VarDecl }.map { it as VarDecl })
for(variable in scope.statements.filterIsInstance<VarDecl>())
prog.variable(variable.scopedname, variable)
for(subscope in scope.subScopes())
processVariables(subscope.value)

View File

@ -94,4 +94,9 @@ class Memory {
fun clear() {
for(i in 0..65535) mem[i]=0
}
fun copy(from: Int, to: Int, numbytes: Int) {
for(i in 0 until numbytes)
mem[to+i] = mem[from+i]
}
}

View File

@ -126,6 +126,7 @@ class StackVm(private var traceOutputFile: String?) {
var callstack = MyStack<Instruction>()
private set
private var program = listOf<Instruction>()
private var labels = emptyMap<String, Instruction>()
private var heap = HeapValues()
private var traceOutput = if(traceOutputFile!=null) PrintStream(File(traceOutputFile), "utf-8") else null
private var canvas: BitmapScreenPanel? = null
@ -136,8 +137,10 @@ class StackVm(private var traceOutputFile: String?) {
var sourceLine: String = ""
private set
fun load(program: Program, canvas: BitmapScreenPanel?) {
this.program = program.program
this.labels = program.labels
this.heap = program.heap
this.canvas = canvas
canvas?.requestFocusInWindow()
@ -158,7 +161,7 @@ class StackVm(private var traceOutputFile: String?) {
P_irqd = false
sourceLine = ""
currentIns = this.program[0]
irqStartInstruction = program.labels["irq.irq"]
irqStartInstruction = null
}
fun step(instructionCount: Int = 5000) {
@ -1613,9 +1616,18 @@ class StackVm(private var traceOutputFile: String?) {
val value = heap.get(iterable.heapId)
evalstack.push(Value(DataType.UBYTE, if (value.array!!.all { v -> v != 0 }) 1 else 0))
}
Syscall.FUNC_SET_IRQVEC, Syscall.FUNC_SET_IRQVEC_EXCL -> TODO()
Syscall.FUNC_RESTORE_IRQVEC -> TODO()
Syscall.FUNC_MEMCOPY -> TODO()
Syscall.FUNC_SET_IRQVEC, Syscall.FUNC_SET_IRQVEC_EXCL -> {
irqStartInstruction = labels["irq.irq"]
}
Syscall.FUNC_RESTORE_IRQVEC -> {
irqStartInstruction = null
}
Syscall.FUNC_MEMCOPY -> {
val numbytes = evalstack.pop().integerValue()
val to = evalstack.pop().integerValue()
val from = evalstack.pop().integerValue()
mem.copy(from, to, numbytes)
}
}
}