implement jump registry

This commit is contained in:
Mark Canlas 2020-08-21 03:11:27 -04:00
parent 3c618410bb
commit 2db25f5982
7 changed files with 31 additions and 9 deletions

View File

@ -16,7 +16,6 @@
## TODO
- postfix operations to accumulator values
- implement jump registry
- maybe implement define registry
- register locking (i.e. disallow X writes during indexed traversal that uses X)
- compiler optimization

View File

@ -1,5 +1,6 @@
package com.htmlism.mos6502.dsl
import scala.collection.immutable.ListSet
import scala.collection.mutable.ListBuffer
case class AsmDocument(xs: List[TopLevelAsmDocumentFragment]) {
@ -13,14 +14,21 @@ class AsmDocumentContext {
private val xs: ListBuffer[TopLevelAsmDocumentFragment] =
ListBuffer()
private var jumps: ListSet[Subroutine] =
ListSet()
def push(x: TopLevelAsmDocumentFragment): Unit =
xs.append(x)
def attach(sub: Subroutine): Unit =
xs.append(sub)
def addJumpRegistry(ys: ListSet[Subroutine]): Unit =
jumps = jumps ++ ys
def toDoc: AsmDocument =
AsmDocument(xs.toList)
def toDoc: AsmDocument = {
val asmFragmentsAndSubroutines =
xs.toList ::: jumps.toList
AsmDocument(asmFragmentsAndSubroutines)
}
}
sealed trait TopLevelAsmDocumentFragment {

View File

@ -1,5 +1,6 @@
package com.htmlism.mos6502.dsl
import scala.collection.immutable.ListSet
import scala.collection.mutable.ListBuffer
import cats.implicits._
@ -133,6 +134,9 @@ class AssemblyContext {
private val xs: ListBuffer[Statement] =
ListBuffer()
private var jumps: ListSet[Subroutine] =
ListSet()
def push(instruction: Instruction): Unit =
xs.append(UnaryInstruction(instruction, None))
@ -151,6 +155,9 @@ class AssemblyContext {
def branch(instruction: Instruction, label: String): Unit =
xs.append(BranchingInstruction(instruction, label))
def addJump(subroutine: Subroutine): Unit =
jumps = jumps + subroutine
def printOut(): Unit = {
xs.map(_.toAsm)
.foreach(println)
@ -161,4 +168,7 @@ class AssemblyContext {
def toFragment: AsmFragment =
AsmFragment(xs.toList)
def getJumps: ListSet[Subroutine] =
jumps
}

View File

@ -68,4 +68,4 @@ case class BranchingInstruction(instruction: Instruction, label: String) extends
def toTriplet: (String, Option[String], Option[String]) =
(instruction.toString, label.some, None)
}
}

View File

@ -8,6 +8,9 @@ trait AsmDocSyntax {
f(asmCtx)
ctx
.addJumpRegistry(asmCtx.getJumps)
ctx
.push(asmCtx.toFragment)
}

View File

@ -17,7 +17,11 @@ trait AsmSyntax {
Subroutine(s, ctx.toFragment)
}
def jump(s: Subroutine)(implicit ctx: AssemblyContext): Unit =
def jump(s: Subroutine)(implicit ctx: AssemblyContext): Unit = {
ctx
.addJump(s)
ctx
.branch(JSR, s.name)
}
}

View File

@ -77,8 +77,6 @@ class Easy6502Spec extends AnyFlatSpec with should.Matchers {
asm { implicit a =>
jump(init)
}
ctx.attach(init)
}
println(