generated from rnentjes/kotlin-server-web-undertow
Refactor: Restructure project package hierarchy and add initial implementation for assembler instructions, shell commands, and exception handling.
This commit is contained in:
72
src/jvmMain/kotlin/mtmc/asm/instructions/Instruction.kt
Normal file
72
src/jvmMain/kotlin/mtmc/asm/instructions/Instruction.kt
Normal file
@@ -0,0 +1,72 @@
|
||||
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>"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user