-
Notifications
You must be signed in to change notification settings - Fork 130
Add list, symbol/annotation SID, single-char symbol opcode handlers #1140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
austnwil
merged 8 commits into
amazon-ion:ion11
from
austnwil:austnwil/list-and-misc-handlers
Nov 6, 2025
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
070ce67
Implement list, Symbol/AnnotationSID, SingleCharSymbol opcode handlers
austnwil 513467a
Delete unused object
austnwil b462486
Apply suggestion from @popematt
austnwil 15ac400
Apply suggestion from @popematt
austnwil ea3381a
Apply suggestion from @popematt
austnwil ab2a111
Apply suggestion from @popematt
austnwil 11bed3f
Apply suggestion from @popematt
austnwil 85ea140
Implement PR suggestions
austnwil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/com/amazon/ion/bytecode/bin11/bytearray/AnnotationSIDOpcodeHandler.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package com.amazon.ion.bytecode.bin11.bytearray | ||
|
|
||
| import com.amazon.ion.bytecode.ir.Instructions | ||
| import com.amazon.ion.bytecode.ir.Instructions.packInstructionData | ||
| import com.amazon.ion.bytecode.util.AppendableConstantPoolView | ||
| import com.amazon.ion.bytecode.util.BytecodeBuffer | ||
|
|
||
| /** | ||
| * Writes an annotation with symbol address to the bytecode buffer. Handles opcode `0x58`. | ||
| */ | ||
| internal object AnnotationSIDOpcodeHandler : OpcodeToBytecodeHandler { | ||
| @OptIn(ExperimentalStdlibApi::class) | ||
| override fun convertOpcodeToBytecode( | ||
| opcode: Int, | ||
| source: ByteArray, | ||
| position: Int, | ||
| destination: BytecodeBuffer, | ||
| constantPool: AppendableConstantPoolView, | ||
| macroSrc: IntArray, | ||
| macroIndices: IntArray, | ||
| symbolTable: Array<String?> | ||
| ): Int { | ||
| assert(opcode == 0x58) { "Handler cannot compile opcode ${opcode.toHexString()}" } | ||
|
austnwil marked this conversation as resolved.
Outdated
|
||
|
|
||
| val sidValueAndLength = PrimitiveDecoder.readFlexUIntValueAndLength(source, position) | ||
| val sid = sidValueAndLength.toInt() | ||
| val length = sidValueAndLength.shr(Int.SIZE_BITS).toInt() | ||
| destination.add(Instructions.I_ANNOTATION_SID.packInstructionData(sid)) | ||
| return length | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
src/main/java/com/amazon/ion/bytecode/bin11/bytearray/ListOpcodeHandlers.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package com.amazon.ion.bytecode.bin11.bytearray | ||
|
|
||
| import com.amazon.ion.bytecode.BytecodeEmitter | ||
| import com.amazon.ion.bytecode.bin11.OpCode | ||
| import com.amazon.ion.bytecode.util.AppendableConstantPoolView | ||
| import com.amazon.ion.bytecode.util.BytecodeBuffer | ||
| import com.amazon.ion.bytecode.util.unsignedToInt | ||
|
|
||
| // TODO: much of the logic here is shared between lists and sexps. It might be worthwhile to do something like | ||
| // "SequenceOpcodeHandlers" and pass the start instruction (Instructions.I_LIST_START vs .I_SEXP_START) to a helper | ||
| // BytecodeEmitter.emitSequence() or similar so this logic is not duplicated in a set of `*SexpOpcodeHandler`s. | ||
|
|
||
| /** | ||
| * Writes a length prefixed list to the bytecode buffer. Handles opcode `0xB0`-`0xBF`. | ||
| */ | ||
| internal object ShortLengthPrefixedListOpcodeHandler : OpcodeToBytecodeHandler { | ||
| @OptIn(ExperimentalStdlibApi::class) | ||
| override fun convertOpcodeToBytecode( | ||
| opcode: Int, | ||
| source: ByteArray, | ||
| position: Int, | ||
| destination: BytecodeBuffer, | ||
| constantPool: AppendableConstantPoolView, | ||
| macroSrc: IntArray, | ||
| macroIndices: IntArray, | ||
| symbolTable: Array<String?> | ||
| ): Int { | ||
| assert(opcode in 0xb0..0xbf) { "Handler cannot compile opcode ${opcode.toHexString()}" } | ||
|
|
||
| val length = opcode and 0xF | ||
| BytecodeEmitter.emitList(destination) { | ||
| var p = position | ||
| val end = p + length | ||
| while (p < end) { | ||
| val opcode = source[p++].unsignedToInt() | ||
| p += OpcodeHandlerTable.handler(opcode).convertOpcodeToBytecode( | ||
| opcode, | ||
| source, | ||
| p, | ||
| destination, | ||
| constantPool, | ||
| macroSrc, | ||
| macroIndices, | ||
| symbolTable, | ||
| ) | ||
| } | ||
| } | ||
| return length | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Writes a length prefixed list to the bytecode buffer. Handles opcode `0xFA`. | ||
| */ | ||
| internal object LongLengthPrefixedListOpcodeHandler : OpcodeToBytecodeHandler { | ||
| @OptIn(ExperimentalStdlibApi::class) | ||
| override fun convertOpcodeToBytecode( | ||
| opcode: Int, | ||
| source: ByteArray, | ||
| position: Int, | ||
| destination: BytecodeBuffer, | ||
| constantPool: AppendableConstantPoolView, | ||
| macroSrc: IntArray, | ||
| macroIndices: IntArray, | ||
| symbolTable: Array<String?> | ||
| ): Int { | ||
| assert(opcode == 0xfa) { "Handler cannot compile opcode ${opcode.toHexString()}" } | ||
|
|
||
| val containerSizeUIntValueAndLength = PrimitiveDecoder.readFlexUIntValueAndLength(source, position) | ||
| val containerLength = containerSizeUIntValueAndLength.toInt() | ||
| val prefixLength = containerSizeUIntValueAndLength.shr(Int.SIZE_BITS).toInt() | ||
| BytecodeEmitter.emitList(destination) { | ||
| var p = position + prefixLength | ||
| val end = p + containerLength | ||
| while (p < end) { | ||
| val opcode = source[p++].unsignedToInt() | ||
| p += OpcodeHandlerTable.handler(opcode).convertOpcodeToBytecode( | ||
| opcode, | ||
| source, | ||
| p, | ||
| destination, | ||
| constantPool, | ||
| macroSrc, | ||
| macroIndices, | ||
| symbolTable, | ||
| ) | ||
| } | ||
| } | ||
| return containerLength + prefixLength | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Writes a delimited list to the bytecode buffer. Handles opcode `0xF0`. | ||
| */ | ||
| internal object DelimitedListOpcodeHandler : OpcodeToBytecodeHandler { | ||
| @OptIn(ExperimentalStdlibApi::class) | ||
| override fun convertOpcodeToBytecode( | ||
| opcode: Int, | ||
| source: ByteArray, | ||
| position: Int, | ||
| destination: BytecodeBuffer, | ||
| constantPool: AppendableConstantPoolView, | ||
| macroSrc: IntArray, | ||
| macroIndices: IntArray, | ||
| symbolTable: Array<String?> | ||
| ): Int { | ||
| assert(opcode == 0xf0) { "Handler cannot compile opcode ${opcode.toHexString()}" } | ||
|
|
||
| var p = position | ||
| BytecodeEmitter.emitList(destination) { | ||
| while (true) { | ||
| val opcode = source[p++].unsignedToInt() | ||
| if (opcode == OpCode.DELIMITED_CONTAINER_END) { | ||
| break | ||
| } | ||
| p += OpcodeHandlerTable.handler(opcode).convertOpcodeToBytecode( | ||
| opcode, | ||
| source, | ||
| p, | ||
| destination, | ||
| constantPool, | ||
| macroSrc, | ||
| macroIndices, | ||
| symbolTable, | ||
| ) | ||
| } | ||
| } | ||
| val bytesRead = p - position | ||
| return bytesRead | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Writes a tagless-element list to the bytecode buffer. Handles opcode `0x5B`. | ||
| */ | ||
| internal object TaglessElementListOpcodeHandler : OpcodeToBytecodeHandler { | ||
| @OptIn(ExperimentalStdlibApi::class) | ||
| override fun convertOpcodeToBytecode( | ||
| opcode: Int, | ||
| source: ByteArray, | ||
| position: Int, | ||
| destination: BytecodeBuffer, | ||
| constantPool: AppendableConstantPoolView, | ||
| macroSrc: IntArray, | ||
| macroIndices: IntArray, | ||
| symbolTable: Array<String?> | ||
| ): Int { | ||
| assert(opcode == 0x5b) { "Handler cannot compile opcode ${opcode.toHexString()}" } | ||
|
|
||
| var p = position | ||
| val childOpcode = source[p++].unsignedToInt() | ||
| val macroAddress = when (childOpcode) { | ||
| in 0x00..0x47 -> childOpcode | ||
| in 0x48..0x4f, 0xf4 -> { | ||
|
austnwil marked this conversation as resolved.
Outdated
|
||
| val addressValueAndLength = PrimitiveDecoder.readFlexUIntValueAndLength(source, p) | ||
| val addressValue = addressValueAndLength.toInt() | ||
| val addressLength = addressValueAndLength.shr(Int.SIZE_BITS).toInt() | ||
| p += addressLength | ||
| addressValue | ||
| } | ||
| else -> -1 | ||
| } | ||
|
|
||
| val containerSizeValueAndLength = PrimitiveDecoder.readFlexUIntValueAndLength(source, p) | ||
| val containerLength = containerSizeValueAndLength.toInt() | ||
| val prefixLength = containerSizeValueAndLength.shr(Int.SIZE_BITS).toInt() | ||
| p += prefixLength | ||
|
austnwil marked this conversation as resolved.
Outdated
|
||
|
|
||
| // If macroAddress > -1, then it is the address of the macro-shaped values, | ||
| // and childOpcode should be ignored. | ||
| // If macroAddress is -1, then childOpcode is the opcode of the values. | ||
| if (macroAddress < 0) { | ||
| val handler = TaglessOpcodeHandlerTable.handler(childOpcode) | ||
| BytecodeEmitter.emitList(destination) { | ||
| for (i in 0 until containerLength) { | ||
| p += handler.convertOpcodeToBytecode( | ||
| childOpcode, | ||
| source, | ||
| p, | ||
| destination, | ||
| constantPool, | ||
| macroSrc, | ||
| macroIndices, | ||
| symbolTable, | ||
| ) | ||
| } | ||
| } | ||
| } else { | ||
| TODO("Macro evaluation not yet implemented") | ||
| } | ||
|
|
||
| return p - position | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.