|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package com.amazon.ion.bytecode.bin11.bytearray |
| 4 | + |
| 5 | +import com.amazon.ion.bytecode.BytecodeEmitter |
| 6 | +import com.amazon.ion.bytecode.bin11.OpCode |
| 7 | +import com.amazon.ion.bytecode.util.AppendableConstantPoolView |
| 8 | +import com.amazon.ion.bytecode.util.BytecodeBuffer |
| 9 | +import com.amazon.ion.bytecode.util.unsignedToInt |
| 10 | + |
| 11 | +// TODO: much of the logic here is shared between lists and sexps. It might be worthwhile to do something like |
| 12 | +// "SequenceOpcodeHandlers" and pass the start instruction (Instructions.I_LIST_START vs .I_SEXP_START) to a helper |
| 13 | +// BytecodeEmitter.emitSequence() or similar so this logic is not duplicated in a set of `*SexpOpcodeHandler`s. |
| 14 | + |
| 15 | +/** |
| 16 | + * Writes a length prefixed list to the bytecode buffer. Handles opcode `0xB0`-`0xBF`. |
| 17 | + */ |
| 18 | +internal object ShortLengthPrefixedListOpcodeHandler : OpcodeToBytecodeHandler { |
| 19 | + @OptIn(ExperimentalStdlibApi::class) |
| 20 | + override fun convertOpcodeToBytecode( |
| 21 | + opcode: Int, |
| 22 | + source: ByteArray, |
| 23 | + position: Int, |
| 24 | + destination: BytecodeBuffer, |
| 25 | + constantPool: AppendableConstantPoolView, |
| 26 | + macroSrc: IntArray, |
| 27 | + macroIndices: IntArray, |
| 28 | + symbolTable: Array<String?> |
| 29 | + ): Int { |
| 30 | + val length = opcode and 0xF |
| 31 | + BytecodeEmitter.emitList(destination) { |
| 32 | + var p = position |
| 33 | + val end = p + length |
| 34 | + while (p < end) { |
| 35 | + val opcode = source[p++].unsignedToInt() |
| 36 | + p += OpcodeHandlerTable.handler(opcode).convertOpcodeToBytecode( |
| 37 | + opcode, |
| 38 | + source, |
| 39 | + p, |
| 40 | + destination, |
| 41 | + constantPool, |
| 42 | + macroSrc, |
| 43 | + macroIndices, |
| 44 | + symbolTable, |
| 45 | + ) |
| 46 | + } |
| 47 | + } |
| 48 | + return length |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Writes a length prefixed list to the bytecode buffer. Handles opcode `0xFA`. |
| 54 | + */ |
| 55 | +internal object LongLengthPrefixedListOpcodeHandler : OpcodeToBytecodeHandler { |
| 56 | + @OptIn(ExperimentalStdlibApi::class) |
| 57 | + override fun convertOpcodeToBytecode( |
| 58 | + opcode: Int, |
| 59 | + source: ByteArray, |
| 60 | + position: Int, |
| 61 | + destination: BytecodeBuffer, |
| 62 | + constantPool: AppendableConstantPoolView, |
| 63 | + macroSrc: IntArray, |
| 64 | + macroIndices: IntArray, |
| 65 | + symbolTable: Array<String?> |
| 66 | + ): Int { |
| 67 | + val containerSizeUIntValueAndLength = PrimitiveDecoder.readFlexUIntValueAndLength(source, position) |
| 68 | + val containerLength = containerSizeUIntValueAndLength.toInt() |
| 69 | + val prefixLength = containerSizeUIntValueAndLength.shr(Int.SIZE_BITS).toInt() |
| 70 | + BytecodeEmitter.emitList(destination) { |
| 71 | + var p = position + prefixLength |
| 72 | + val end = p + containerLength |
| 73 | + while (p < end) { |
| 74 | + val opcode = source[p++].unsignedToInt() |
| 75 | + p += OpcodeHandlerTable.handler(opcode).convertOpcodeToBytecode( |
| 76 | + opcode, |
| 77 | + source, |
| 78 | + p, |
| 79 | + destination, |
| 80 | + constantPool, |
| 81 | + macroSrc, |
| 82 | + macroIndices, |
| 83 | + symbolTable, |
| 84 | + ) |
| 85 | + } |
| 86 | + } |
| 87 | + return containerLength + prefixLength |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Writes a delimited list to the bytecode buffer. Handles opcode `0xF0`. |
| 93 | + */ |
| 94 | +internal object DelimitedListOpcodeHandler : OpcodeToBytecodeHandler { |
| 95 | + @OptIn(ExperimentalStdlibApi::class) |
| 96 | + override fun convertOpcodeToBytecode( |
| 97 | + opcode: Int, |
| 98 | + source: ByteArray, |
| 99 | + position: Int, |
| 100 | + destination: BytecodeBuffer, |
| 101 | + constantPool: AppendableConstantPoolView, |
| 102 | + macroSrc: IntArray, |
| 103 | + macroIndices: IntArray, |
| 104 | + symbolTable: Array<String?> |
| 105 | + ): Int { |
| 106 | + var p = position |
| 107 | + BytecodeEmitter.emitList(destination) { |
| 108 | + while (true) { |
| 109 | + val opcode = source[p++].unsignedToInt() |
| 110 | + if (opcode == OpCode.DELIMITED_CONTAINER_END) { |
| 111 | + break |
| 112 | + } |
| 113 | + p += OpcodeHandlerTable.handler(opcode).convertOpcodeToBytecode( |
| 114 | + opcode, |
| 115 | + source, |
| 116 | + p, |
| 117 | + destination, |
| 118 | + constantPool, |
| 119 | + macroSrc, |
| 120 | + macroIndices, |
| 121 | + symbolTable, |
| 122 | + ) |
| 123 | + } |
| 124 | + } |
| 125 | + val bytesRead = p - position |
| 126 | + return bytesRead |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * Writes a tagless-element list to the bytecode buffer. Handles opcode `0x5B`. |
| 132 | + */ |
| 133 | +internal object TaglessElementListOpcodeHandler : OpcodeToBytecodeHandler { |
| 134 | + @OptIn(ExperimentalStdlibApi::class) |
| 135 | + override fun convertOpcodeToBytecode( |
| 136 | + opcode: Int, |
| 137 | + source: ByteArray, |
| 138 | + position: Int, |
| 139 | + destination: BytecodeBuffer, |
| 140 | + constantPool: AppendableConstantPoolView, |
| 141 | + macroSrc: IntArray, |
| 142 | + macroIndices: IntArray, |
| 143 | + symbolTable: Array<String?> |
| 144 | + ): Int { |
| 145 | + var p = position |
| 146 | + val childOpcode = source[p++].unsignedToInt() |
| 147 | + val macroAddress = when (childOpcode) { |
| 148 | + in 0x00..0x47 -> childOpcode |
| 149 | + in 0x48..0x4f -> { |
| 150 | + val flexUIntValueAndLength = PrimitiveDecoder.readFlexUIntValueAndLength(source, p) |
| 151 | + val addressLength = flexUIntValueAndLength.shr(Int.SIZE_BITS).toInt() |
| 152 | + p += addressLength |
| 153 | + val lsb = childOpcode - 0x48 |
| 154 | + val msb = flexUIntValueAndLength.toInt() * 8 |
| 155 | + msb + lsb + 72 |
| 156 | + } |
| 157 | + 0xf4 -> { |
| 158 | + val addressValueAndLength = PrimitiveDecoder.readFlexUIntValueAndLength(source, p) |
| 159 | + val addressValue = addressValueAndLength.toInt() |
| 160 | + val addressLength = addressValueAndLength.shr(Int.SIZE_BITS).toInt() |
| 161 | + p += addressLength |
| 162 | + addressValue |
| 163 | + } |
| 164 | + else -> -1 |
| 165 | + } |
| 166 | + |
| 167 | + val childCountValueAndLength = PrimitiveDecoder.readFlexUIntValueAndLength(source, p) |
| 168 | + val childCount = childCountValueAndLength.toInt() |
| 169 | + val prefixSize = childCountValueAndLength.shr(Int.SIZE_BITS).toInt() |
| 170 | + p += prefixSize |
| 171 | + |
| 172 | + // If macroAddress > -1, then it is the address of the macro-shaped values, |
| 173 | + // and childOpcode should be ignored. |
| 174 | + // If macroAddress is -1, then childOpcode is the opcode of the values. |
| 175 | + if (macroAddress < 0) { |
| 176 | + val handler = TaglessOpcodeHandlerTable.handler(childOpcode) |
| 177 | + BytecodeEmitter.emitList(destination) { |
| 178 | + for (i in 0 until childCount) { |
| 179 | + p += handler.convertOpcodeToBytecode( |
| 180 | + childOpcode, |
| 181 | + source, |
| 182 | + p, |
| 183 | + destination, |
| 184 | + constantPool, |
| 185 | + macroSrc, |
| 186 | + macroIndices, |
| 187 | + symbolTable, |
| 188 | + ) |
| 189 | + } |
| 190 | + } |
| 191 | + } else { |
| 192 | + TODO("Macro evaluation not yet implemented") |
| 193 | + } |
| 194 | + |
| 195 | + return p - position |
| 196 | + } |
| 197 | +} |
0 commit comments