generated from rnentjes/kotlin-server-web-undertow
73 lines
2.0 KiB
Kotlin
73 lines
2.0 KiB
Kotlin
package mtmc.asm.instructions
|
|
|
|
import mtmc.asm.ASMElement
|
|
import mtmc.asm.Assembler
|
|
import mtmc.asm.instructions.ALUInstruction.Companion.disassemble
|
|
import mtmc.emulator.MonTanaMiniComputer.Companion.isDoubleWordInstruction
|
|
import mtmc.tokenizer.MTMCToken
|
|
|
|
abstract class Instruction(
|
|
@JvmField val type: InstructionType,
|
|
labels: List<MTMCToken>,
|
|
@JvmField val instructionToken: MTMCToken
|
|
) : ASMElement(labels, instructionToken.line) {
|
|
open fun validateLabel(assembler: Assembler) {
|
|
// default does nothing
|
|
}
|
|
|
|
override fun addError(error: String) {
|
|
addError(instructionToken, error)
|
|
}
|
|
|
|
abstract fun genCode(output: ByteArray, assembler: Assembler)
|
|
|
|
override var sizeInBytes: Int = type.sizeInBytes
|
|
get() = type.sizeInBytes
|
|
|
|
companion object {
|
|
@JvmStatic
|
|
fun isInstruction(cmd: String): Boolean {
|
|
return InstructionType.fromString(cmd) != null
|
|
}
|
|
|
|
fun disassemble(instruction: Short, previousInstruction: Short): String {
|
|
if (isDoubleWordInstruction(previousInstruction)) {
|
|
return instruction.toInt().toString()
|
|
}
|
|
val misc = MiscInstruction.disassemble(instruction)
|
|
if (misc != null) {
|
|
return misc
|
|
}
|
|
val aluOp = disassemble(instruction)
|
|
if (aluOp != null) {
|
|
return aluOp
|
|
}
|
|
val stack = StackInstruction.disassemble(instruction)
|
|
if (stack != null) {
|
|
return stack
|
|
}
|
|
val test = TestInstruction.disassemble(instruction)
|
|
if (test != null) {
|
|
return test
|
|
}
|
|
val lsr = LoadStoreRegisterInstruction.disassemble(instruction)
|
|
if (lsr != null) {
|
|
return lsr
|
|
}
|
|
val ls = LoadStoreInstruction.disassemble(instruction)
|
|
if (ls != null) {
|
|
return ls
|
|
}
|
|
val jumpReg = JumpInstruction.disassemble(instruction)
|
|
if (jumpReg != null) {
|
|
return jumpReg
|
|
}
|
|
val jump = JumpInstruction.disassemble(instruction)
|
|
if (jump != null) {
|
|
return jump
|
|
}
|
|
return "<unknown>"
|
|
}
|
|
}
|
|
}
|