2022-09-26 17:03:54 +00:00
|
|
|
package prog8.vm
|
|
|
|
|
2023-02-20 22:29:16 +00:00
|
|
|
import prog8.code.core.ArrayDatatypes
|
|
|
|
import prog8.code.core.AssemblyError
|
|
|
|
import prog8.code.core.DataType
|
2023-05-26 20:56:12 +00:00
|
|
|
import prog8.code.core.SplitWordArrayTypes
|
2022-09-26 17:03:54 +00:00
|
|
|
import prog8.intermediate.*
|
|
|
|
|
|
|
|
class VmProgramLoader {
|
2022-10-16 16:30:14 +00:00
|
|
|
private val placeholders = mutableMapOf<Pair<IRCodeChunk, Int>, String>() // program chunk+index to symbolname
|
2023-04-13 21:45:02 +00:00
|
|
|
private val subroutines = mutableMapOf<String, IRSubroutine>() // label to subroutine node
|
2022-09-26 17:03:54 +00:00
|
|
|
|
2022-10-16 16:30:14 +00:00
|
|
|
fun load(irProgram: IRProgram, memory: Memory): List<IRCodeChunk> {
|
|
|
|
irProgram.validate()
|
2023-04-13 21:45:02 +00:00
|
|
|
placeholders.clear()
|
|
|
|
subroutines.clear()
|
2022-09-26 17:03:54 +00:00
|
|
|
val allocations = VmVariableAllocator(irProgram.st, irProgram.encoding, irProgram.options.compTarget)
|
2022-10-23 15:29:03 +00:00
|
|
|
val variableAddresses = allocations.allocations.toMutableMap()
|
2022-10-16 16:30:14 +00:00
|
|
|
val programChunks = mutableListOf<IRCodeChunk>()
|
2022-09-26 17:03:54 +00:00
|
|
|
|
2022-10-23 15:29:03 +00:00
|
|
|
varsToMemory(irProgram, allocations, variableAddresses, memory)
|
2022-09-27 00:42:01 +00:00
|
|
|
|
2023-02-20 22:29:16 +00:00
|
|
|
if(irProgram.globalInits.isNotEmpty())
|
|
|
|
programChunks += irProgram.globalInits
|
2022-09-26 17:03:54 +00:00
|
|
|
|
|
|
|
// make sure that if there is a "main.start" entrypoint, we jump to it
|
|
|
|
irProgram.blocks.firstOrNull()?.let {
|
2022-11-22 01:04:24 +00:00
|
|
|
if(it.children.any { sub -> sub is IRSubroutine && sub.label=="main.start" }) {
|
2023-03-12 15:09:55 +00:00
|
|
|
val previous = programChunks.lastOrNull()
|
|
|
|
val chunk = IRCodeChunk(null, previous)
|
2022-10-16 16:30:14 +00:00
|
|
|
placeholders[Pair(chunk, 0)] = "main.start"
|
|
|
|
chunk += IRInstruction(Opcode.JUMP, labelSymbol = "main.start")
|
2023-03-12 15:09:55 +00:00
|
|
|
previous?.let { p -> p.next = chunk }
|
2022-10-16 16:30:14 +00:00
|
|
|
programChunks += chunk
|
2022-09-26 17:03:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-23 15:29:03 +00:00
|
|
|
// load rest of the program into the list
|
2022-10-23 23:57:37 +00:00
|
|
|
val chunkReplacements = mutableListOf<Pair<IRCodeChunkBase, IRCodeChunk>>()
|
2022-09-26 17:03:54 +00:00
|
|
|
irProgram.blocks.forEach { block ->
|
2023-12-26 21:01:49 +00:00
|
|
|
if(block.options.address!=null)
|
2023-04-09 21:09:30 +00:00
|
|
|
throw IRParseException("blocks cannot have a load address for vm: ${block.label}")
|
2022-09-26 17:03:54 +00:00
|
|
|
|
2022-11-22 01:04:24 +00:00
|
|
|
block.children.forEach { child ->
|
|
|
|
when(child) {
|
2023-12-26 13:47:31 +00:00
|
|
|
is IRAsmSubroutine -> throw IRParseException("vm does not support non-inlined asmsubs (use normal sub): ${child.label}")
|
2022-11-22 01:04:24 +00:00
|
|
|
is IRCodeChunk -> programChunks += child
|
2023-05-09 19:04:31 +00:00
|
|
|
is IRInlineAsmChunk -> throw IRParseException("encountered unconverted inline assembly chunk")
|
|
|
|
is IRInlineBinaryChunk -> throw IRParseException("inline binary data not yet supported in the VM")
|
2022-11-22 01:04:24 +00:00
|
|
|
is IRSubroutine -> {
|
2023-04-13 21:45:02 +00:00
|
|
|
subroutines[child.label] = child
|
2022-11-22 01:04:24 +00:00
|
|
|
child.chunks.forEach { chunk ->
|
|
|
|
when (chunk) {
|
2023-05-09 19:04:31 +00:00
|
|
|
is IRInlineAsmChunk -> throw IRParseException("encountered unconverted inline assembly chunk")
|
|
|
|
is IRInlineBinaryChunk -> throw IRParseException("inline binary data not yet supported in the VM")
|
2022-11-22 01:04:24 +00:00
|
|
|
is IRCodeChunk -> programChunks += chunk
|
|
|
|
else -> throw AssemblyError("weird chunk type")
|
|
|
|
}
|
2023-04-13 21:45:02 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-26 23:50:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-23 23:57:37 +00:00
|
|
|
pass2translateSyscalls(programChunks)
|
2023-04-13 21:45:02 +00:00
|
|
|
pass2replaceLabelsByProgIndex(programChunks, variableAddresses, subroutines)
|
2022-10-23 23:57:37 +00:00
|
|
|
phase2relinkReplacedChunks(chunkReplacements, programChunks)
|
|
|
|
|
|
|
|
programChunks.forEach {
|
|
|
|
it.instructions.forEach { ins ->
|
2023-05-14 14:48:48 +00:00
|
|
|
if (ins.labelSymbol != null && ins.opcode !in OpcodesThatBranch)
|
2023-04-09 13:06:40 +00:00
|
|
|
require(ins.address != null) { "instruction with labelSymbol for a var should have value set to the memory address" }
|
2022-10-23 23:57:37 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-26 23:50:00 +00:00
|
|
|
|
2022-10-16 16:30:14 +00:00
|
|
|
return programChunks
|
|
|
|
}
|
|
|
|
|
2022-10-23 23:57:37 +00:00
|
|
|
private fun phase2relinkReplacedChunks(
|
|
|
|
replacements: MutableList<Pair<IRCodeChunkBase, IRCodeChunk>>,
|
|
|
|
programChunks: MutableList<IRCodeChunk>
|
|
|
|
) {
|
|
|
|
replacements.forEach { (old, new) ->
|
|
|
|
programChunks.forEach { chunk ->
|
|
|
|
if(chunk.next === old) {
|
|
|
|
chunk.next = new
|
|
|
|
}
|
|
|
|
chunk.instructions.forEach { ins ->
|
|
|
|
if(ins.branchTarget === old) {
|
|
|
|
ins.branchTarget = new
|
|
|
|
} else if(ins.branchTarget==null && ins.labelSymbol==new.label) {
|
|
|
|
ins.branchTarget = new
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun pass2translateSyscalls(chunks: MutableList<IRCodeChunk>) {
|
|
|
|
chunks.forEach { chunk ->
|
|
|
|
chunk.instructions.withIndex().forEach { (index, ins) ->
|
2023-04-10 14:16:35 +00:00
|
|
|
if(ins.opcode == Opcode.SYSCALL) {
|
2022-10-23 23:57:37 +00:00
|
|
|
// convert IR Syscall to VM Syscall
|
2023-04-09 13:06:40 +00:00
|
|
|
val vmSyscall = when(ins.immediate!!) {
|
2022-10-23 23:57:37 +00:00
|
|
|
IMSyscall.SORT_UBYTE.number -> Syscall.SORT_UBYTE
|
|
|
|
IMSyscall.SORT_BYTE.number -> Syscall.SORT_BYTE
|
|
|
|
IMSyscall.SORT_UWORD.number -> Syscall.SORT_UWORD
|
|
|
|
IMSyscall.SORT_WORD.number -> Syscall.SORT_WORD
|
|
|
|
IMSyscall.ANY_BYTE.number -> Syscall.ANY_BYTE
|
|
|
|
IMSyscall.ANY_WORD.number -> Syscall.ANY_WORD
|
|
|
|
IMSyscall.ANY_FLOAT.number -> Syscall.ANY_FLOAT
|
|
|
|
IMSyscall.ALL_BYTE.number -> Syscall.ALL_BYTE
|
|
|
|
IMSyscall.ALL_WORD.number -> Syscall.ALL_WORD
|
|
|
|
IMSyscall.ALL_FLOAT.number -> Syscall.ALL_FLOAT
|
|
|
|
IMSyscall.REVERSE_BYTES.number -> Syscall.REVERSE_BYTES
|
|
|
|
IMSyscall.REVERSE_WORDS.number -> Syscall.REVERSE_WORDS
|
|
|
|
IMSyscall.REVERSE_FLOATS.number -> Syscall.REVERSE_FLOATS
|
2022-11-04 21:37:42 +00:00
|
|
|
IMSyscall.COMPARE_STRINGS.number -> Syscall.COMPARE_STRINGS
|
|
|
|
IMSyscall.STRING_CONTAINS.number -> Syscall.STRING_CONTAINS
|
|
|
|
IMSyscall.BYTEARRAY_CONTAINS.number -> Syscall.BYTEARRAY_CONTAINS
|
|
|
|
IMSyscall.WORDARRAY_CONTAINS.number -> Syscall.WORDARRAY_CONTAINS
|
2024-01-14 12:20:12 +00:00
|
|
|
IMSyscall.FLOATARRAY_CONTAINS.number -> Syscall.FLOATARRAY_CONTAINS
|
2023-05-17 21:03:59 +00:00
|
|
|
IMSyscall.CLAMP_BYTE.number -> Syscall.CLAMP_BYTE
|
|
|
|
IMSyscall.CLAMP_UBYTE.number -> Syscall.CLAMP_UBYTE
|
|
|
|
IMSyscall.CLAMP_WORD.number -> Syscall.CLAMP_WORD
|
|
|
|
IMSyscall.CLAMP_UWORD.number -> Syscall.CLAMP_UWORD
|
|
|
|
IMSyscall.CLAMP_FLOAT.number -> Syscall.CLAMP_FLOAT
|
2023-07-06 22:05:07 +00:00
|
|
|
IMSyscall.CALLFAR.number -> throw IRParseException("vm doesn't support the callfar() syscall")
|
2024-02-09 23:54:15 +00:00
|
|
|
IMSyscall.MEMCOPY.number -> Syscall.MEMCOPY
|
2022-10-23 23:57:37 +00:00
|
|
|
else -> null
|
|
|
|
}
|
|
|
|
|
|
|
|
if(vmSyscall!=null)
|
2023-04-09 13:06:40 +00:00
|
|
|
chunk.instructions[index] = ins.copy(immediate = vmSyscall.ordinal)
|
2022-10-23 23:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val label = ins.labelSymbol
|
|
|
|
if (label != null && ins.opcode !in OpcodesThatBranch) {
|
|
|
|
placeholders[Pair(chunk, index)] = label
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-16 16:30:14 +00:00
|
|
|
private fun pass2replaceLabelsByProgIndex(
|
|
|
|
chunks: MutableList<IRCodeChunk>,
|
2023-04-13 21:45:02 +00:00
|
|
|
variableAddresses: MutableMap<String, Int>,
|
|
|
|
subroutines: MutableMap<String, IRSubroutine>
|
2022-10-16 16:30:14 +00:00
|
|
|
) {
|
|
|
|
for((ref, label) in placeholders) {
|
|
|
|
val (chunk, line) = ref
|
2022-10-23 15:29:03 +00:00
|
|
|
val replacement = variableAddresses[label]
|
2024-01-13 12:55:16 +00:00
|
|
|
val instr = chunk.instructions[line]
|
|
|
|
val offset = instr.labelSymbolOffset ?: 0
|
2022-10-16 16:30:14 +00:00
|
|
|
if(replacement==null) {
|
|
|
|
// it could be an address + index: symbol+42
|
2024-01-13 12:55:16 +00:00
|
|
|
if(offset>0) {
|
|
|
|
val address = variableAddresses.getValue(label) + offset
|
|
|
|
chunk.instructions[line] = instr.copy(address = address)
|
2022-10-16 16:30:14 +00:00
|
|
|
} else {
|
2022-10-23 15:29:03 +00:00
|
|
|
// placeholder is not a variable, so it must be a label of a code chunk instead
|
|
|
|
val target: IRCodeChunk? = chunks.firstOrNull { it.label==label }
|
2023-05-14 14:48:48 +00:00
|
|
|
if(target==null)
|
|
|
|
throw IRParseException("placeholder not found in variables nor labels: $label")
|
2024-01-13 12:55:16 +00:00
|
|
|
else if(instr.opcode in OpcodesThatBranch)
|
|
|
|
chunk.instructions[line] = instr.copy(branchTarget = target, address = null)
|
2023-05-14 14:48:48 +00:00
|
|
|
else
|
2024-01-13 12:55:16 +00:00
|
|
|
throw IRParseException("vm cannot yet load a label address as a value: ${instr}")
|
2022-10-16 16:30:14 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-01-13 12:55:16 +00:00
|
|
|
chunk.instructions[line] = instr.copy(address = replacement + offset)
|
2022-10-16 16:30:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-13 21:45:02 +00:00
|
|
|
|
2023-05-12 21:26:36 +00:00
|
|
|
subroutines.forEach {
|
|
|
|
it.value.chunks.forEach { chunk ->
|
2023-07-22 21:44:26 +00:00
|
|
|
chunk.instructions.withIndex().forEach { (_, ins) ->
|
2023-05-12 21:26:36 +00:00
|
|
|
if(ins.opcode==Opcode.CALL) {
|
|
|
|
val fcallspec = ins.fcallArgs!!
|
|
|
|
val argsWithAddresses = fcallspec.arguments.map { arg ->
|
|
|
|
if(arg.address!=null)
|
|
|
|
arg
|
|
|
|
else {
|
2023-07-02 21:41:15 +00:00
|
|
|
val address = ins.address ?: variableAddresses.getValue(ins.labelSymbol + "." + arg.name)
|
2023-05-12 21:26:36 +00:00
|
|
|
FunctionCallArgs.ArgumentSpec(arg.name, address, arg.reg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fcallspec.arguments = argsWithAddresses
|
|
|
|
}
|
2023-04-13 21:45:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 00:42:01 +00:00
|
|
|
private fun varsToMemory(
|
|
|
|
program: IRProgram,
|
|
|
|
allocations: VmVariableAllocator,
|
|
|
|
symbolAddresses: MutableMap<String, Int>,
|
|
|
|
memory: Memory
|
|
|
|
) {
|
|
|
|
program.st.allVariables().forEach { variable ->
|
|
|
|
var addr = allocations.allocations.getValue(variable.name)
|
2022-12-03 16:46:06 +00:00
|
|
|
|
2023-06-17 13:36:53 +00:00
|
|
|
// zero out uninitialized ('bss') variables.
|
2023-02-19 14:23:57 +00:00
|
|
|
if(variable.uninitialized) {
|
2022-12-04 15:02:58 +00:00
|
|
|
if(variable.dt in ArrayDatatypes) {
|
|
|
|
repeat(variable.length!!) {
|
|
|
|
when(variable.dt) {
|
|
|
|
DataType.STR, DataType.ARRAY_UB, DataType.ARRAY_B, DataType.ARRAY_BOOL -> {
|
|
|
|
memory.setUB(addr, 0u)
|
|
|
|
addr++
|
|
|
|
}
|
|
|
|
DataType.ARRAY_UW, DataType.ARRAY_W -> {
|
|
|
|
memory.setUW(addr, 0u)
|
|
|
|
addr += 2
|
|
|
|
}
|
|
|
|
DataType.ARRAY_F -> {
|
2023-11-20 22:19:08 +00:00
|
|
|
memory.setFloat(addr, 0.0)
|
2022-12-04 15:02:58 +00:00
|
|
|
addr += program.options.compTarget.machine.FLOAT_MEM_SIZE
|
|
|
|
}
|
2023-05-26 20:56:12 +00:00
|
|
|
in SplitWordArrayTypes -> {
|
2023-05-22 18:31:28 +00:00
|
|
|
// lo bytes come after the hi bytes
|
|
|
|
memory.setUB(addr, 0u)
|
|
|
|
memory.setUB(addr+variable.length!!, 0u)
|
|
|
|
addr++
|
|
|
|
}
|
2022-12-04 15:02:58 +00:00
|
|
|
else -> throw IRParseException("invalid dt")
|
2022-12-03 16:46:06 +00:00
|
|
|
}
|
2022-12-04 15:02:58 +00:00
|
|
|
}
|
2022-12-03 16:46:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 00:42:01 +00:00
|
|
|
variable.onetimeInitializationNumericValue?.let {
|
|
|
|
when(variable.dt) {
|
|
|
|
DataType.UBYTE -> memory.setUB(addr, it.toInt().toUByte())
|
|
|
|
DataType.BYTE -> memory.setSB(addr, it.toInt().toByte())
|
|
|
|
DataType.UWORD -> memory.setUW(addr, it.toInt().toUShort())
|
|
|
|
DataType.WORD -> memory.setSW(addr, it.toInt().toShort())
|
2023-11-20 22:19:08 +00:00
|
|
|
DataType.FLOAT -> memory.setFloat(addr, it)
|
2022-09-27 01:32:39 +00:00
|
|
|
else -> throw IRParseException("invalid dt")
|
2022-09-27 00:42:01 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-19 21:13:44 +00:00
|
|
|
variable.onetimeInitializationArrayValue?.let { iElts ->
|
|
|
|
require(variable.length==iElts.size || iElts.size==1 || iElts.size==0)
|
|
|
|
if(iElts.isEmpty() || iElts.size==1) {
|
|
|
|
val iElt = if(iElts.isEmpty()) {
|
2023-02-19 14:23:57 +00:00
|
|
|
require(variable.uninitialized)
|
2023-12-11 21:51:33 +00:00
|
|
|
IRStArrayElement(0.0, null)
|
2022-09-28 13:58:58 +00:00
|
|
|
} else {
|
2023-02-19 14:23:57 +00:00
|
|
|
require(!variable.uninitialized)
|
2023-09-19 21:13:44 +00:00
|
|
|
iElts[0]
|
2022-09-27 00:42:01 +00:00
|
|
|
}
|
2023-09-19 21:13:44 +00:00
|
|
|
initializeWithOneValue(variable, iElt, addr, symbolAddresses, memory, program)
|
2022-09-27 00:42:01 +00:00
|
|
|
} else {
|
2023-09-19 21:13:44 +00:00
|
|
|
initializeWithValues(variable, iElts, addr, symbolAddresses, memory, program)
|
2022-09-27 00:42:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
require(variable.onetimeInitializationStringValue==null) { "in vm/ir, strings should have been converted into bytearrays." }
|
|
|
|
}
|
|
|
|
}
|
2023-09-19 21:13:44 +00:00
|
|
|
|
|
|
|
private fun initializeWithValues(
|
2023-12-11 21:51:33 +00:00
|
|
|
variable: IRStStaticVariable,
|
|
|
|
iElts: IRStArray,
|
2023-09-19 21:13:44 +00:00
|
|
|
startAddress: Int,
|
|
|
|
symbolAddresses: MutableMap<String, Int>,
|
|
|
|
memory: Memory,
|
|
|
|
program: IRProgram
|
|
|
|
) {
|
|
|
|
var address = startAddress
|
|
|
|
when (variable.dt) {
|
|
|
|
DataType.STR, DataType.ARRAY_UB -> {
|
|
|
|
for (elt in iElts) {
|
|
|
|
val value = getInitializerValue(variable.dt, elt, symbolAddresses).toInt().toUByte()
|
|
|
|
memory.setUB(address, value)
|
|
|
|
address++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DataType.ARRAY_B -> {
|
|
|
|
for (elt in iElts) {
|
|
|
|
val value = getInitializerValue(variable.dt, elt, symbolAddresses).toInt().toByte()
|
|
|
|
memory.setSB(address, value)
|
|
|
|
address++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DataType.ARRAY_UW -> {
|
|
|
|
for (elt in iElts) {
|
|
|
|
val value = getInitializerValue(variable.dt, elt, symbolAddresses).toInt().toUShort()
|
|
|
|
memory.setUW(address, value)
|
|
|
|
address += 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DataType.ARRAY_W -> {
|
|
|
|
for (elt in iElts) {
|
|
|
|
val value = getInitializerValue(variable.dt, elt, symbolAddresses).toInt().toShort()
|
|
|
|
memory.setSW(address, value)
|
|
|
|
address += 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
in SplitWordArrayTypes -> {
|
|
|
|
for (elt in iElts) {
|
|
|
|
val value = getInitializerValue(variable.dt, elt, symbolAddresses).toUInt()
|
|
|
|
memory.setUB(address, (value and 255u).toUByte())
|
|
|
|
memory.setUB(address + variable.length!!, (value shr 8).toUByte())
|
|
|
|
address++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DataType.ARRAY_F -> {
|
|
|
|
for (elt in iElts) {
|
2023-11-20 22:19:08 +00:00
|
|
|
val value = getInitializerValue(variable.dt, elt, symbolAddresses).toDouble()
|
2023-09-19 21:13:44 +00:00
|
|
|
memory.setFloat(address, value)
|
|
|
|
address += program.options.compTarget.machine.FLOAT_MEM_SIZE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else -> throw IRParseException("invalid dt")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun initializeWithOneValue(
|
2023-12-11 21:51:33 +00:00
|
|
|
variable: IRStStaticVariable,
|
|
|
|
iElt: IRStArrayElement,
|
2023-09-19 21:13:44 +00:00
|
|
|
startAddress: Int,
|
|
|
|
symbolAddresses: MutableMap<String, Int>,
|
|
|
|
memory: Memory,
|
|
|
|
program: IRProgram
|
|
|
|
) {
|
|
|
|
var address = startAddress
|
|
|
|
when (variable.dt) {
|
|
|
|
DataType.STR, DataType.ARRAY_UB -> {
|
|
|
|
val value = getInitializerValue(variable.dt, iElt, symbolAddresses).toInt().toUByte()
|
|
|
|
repeat(variable.length!!) {
|
|
|
|
memory.setUB(address, value)
|
|
|
|
address++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DataType.ARRAY_B -> {
|
|
|
|
val value = getInitializerValue(variable.dt, iElt, symbolAddresses).toInt().toByte()
|
|
|
|
repeat(variable.length!!) {
|
|
|
|
memory.setSB(address, value)
|
|
|
|
address++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DataType.ARRAY_UW -> {
|
|
|
|
val value = getInitializerValue(variable.dt, iElt, symbolAddresses).toInt().toUShort()
|
|
|
|
repeat(variable.length!!) {
|
|
|
|
memory.setUW(address, value)
|
|
|
|
address += 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DataType.ARRAY_W -> {
|
|
|
|
val value = getInitializerValue(variable.dt, iElt, symbolAddresses).toInt().toShort()
|
|
|
|
repeat(variable.length!!) {
|
|
|
|
memory.setSW(address, value)
|
|
|
|
address += 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
in SplitWordArrayTypes -> {
|
|
|
|
val value = getInitializerValue(variable.dt, iElt, symbolAddresses).toUInt()
|
|
|
|
val lsb = (value and 255u).toUByte()
|
|
|
|
val msb = (value shr 8).toUByte()
|
|
|
|
repeat(variable.length!!) {
|
|
|
|
memory.setUB(address, lsb)
|
|
|
|
memory.setUB(address + variable.length!!, msb)
|
|
|
|
address++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DataType.ARRAY_F -> {
|
2023-11-20 22:19:08 +00:00
|
|
|
val value = getInitializerValue(variable.dt, iElt, symbolAddresses).toDouble()
|
2023-09-19 21:13:44 +00:00
|
|
|
repeat(variable.length!!) {
|
|
|
|
memory.setFloat(address, value)
|
|
|
|
address += program.options.compTarget.machine.FLOAT_MEM_SIZE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else -> throw IRParseException("invalid dt")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-11 21:51:33 +00:00
|
|
|
private fun getInitializerValue(arrayDt: DataType, elt: IRStArrayElement, symbolAddresses: MutableMap<String, Int>): Double {
|
2023-09-19 21:13:44 +00:00
|
|
|
if(elt.addressOfSymbol!=null) {
|
|
|
|
when(arrayDt) {
|
|
|
|
DataType.ARRAY_UB, DataType.STR, DataType.ARRAY_B, DataType.ARRAY_BOOL -> {
|
|
|
|
val name = elt.addressOfSymbol!!
|
|
|
|
val symbolAddress = if(name.startsWith('<')) {
|
|
|
|
symbolAddresses[name.drop(1)]?.and(255)
|
|
|
|
?: throw IRParseException("vm cannot yet load a label address as a value: $name")
|
|
|
|
} else if(name.startsWith('>')) {
|
|
|
|
symbolAddresses[name.drop(1)]?.shr(8)
|
|
|
|
?: throw IRParseException("vm cannot yet load a label address as a value: $name")
|
|
|
|
} else
|
|
|
|
throw IRParseException("for byte-array address-of, expected < or > (lsb/msb)")
|
|
|
|
return symbolAddress.toDouble()
|
|
|
|
}
|
|
|
|
else -> {
|
|
|
|
val name = elt.addressOfSymbol!!
|
|
|
|
val symbolAddress = symbolAddresses[name]
|
|
|
|
?: throw IRParseException("vm cannot yet load a label address as a value: $name")
|
|
|
|
return symbolAddress.toDouble()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return elt.number!!
|
|
|
|
}
|
|
|
|
}
|
2022-09-26 17:03:54 +00:00
|
|
|
}
|