|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package com.amazon.ion.bytecode.bin10 |
| 4 | + |
| 5 | +import com.amazon.ion.IonType |
| 6 | +import com.amazon.ion.bytecode.ir.OperationKind |
| 7 | +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings |
| 8 | + |
| 9 | +internal object TypeIdHelper { |
| 10 | + |
| 11 | + /** |
| 12 | + * Returns the IonType for a legal Ion 1.0 typeId. |
| 13 | + * |
| 14 | + * The returned value is undefined when attempting to use this function for illegal type IDs. |
| 15 | + */ |
| 16 | + @JvmStatic |
| 17 | + fun ionTypeForTypeId(typeId: Int): IonType? { |
| 18 | + return when (typeId shr 4) { |
| 19 | + 0x0 -> if (typeId == 0x0F) IonType.NULL else null |
| 20 | + 0x1 -> IonType.BOOL |
| 21 | + 0x2, 0x3 -> IonType.INT |
| 22 | + 0x4 -> IonType.FLOAT |
| 23 | + 0x5 -> IonType.DECIMAL |
| 24 | + 0x6 -> IonType.TIMESTAMP |
| 25 | + 0x7 -> IonType.SYMBOL |
| 26 | + 0x8 -> IonType.STRING |
| 27 | + 0x9 -> IonType.CLOB |
| 28 | + 0xA -> IonType.BLOB |
| 29 | + 0xB -> IonType.LIST |
| 30 | + 0xC -> IonType.SEXP |
| 31 | + 0xD -> IonType.STRUCT |
| 32 | + else -> null |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Returns the [OperationKind] for an Ion 1.0 typeId. |
| 38 | + */ |
| 39 | + @JvmStatic |
| 40 | + fun operationKindForTypeId(typeId: Int): Int = typeToOperationKindLookup[typeId] |
| 41 | + |
| 42 | + private val typeToOperationKindLookup = IntArray(256) { initOperationKindForType(it) } |
| 43 | + |
| 44 | + private fun initOperationKindForType(state: Int): Int { |
| 45 | + return when (state) { |
| 46 | + in 0x00..0x0E -> OperationKind.UNSET |
| 47 | + 0x0F -> OperationKind.NULL |
| 48 | + 0x10, 0x11, 0x1F -> OperationKind.BOOL |
| 49 | + in 0x20..0x2F -> OperationKind.INT |
| 50 | + in 0x31..0x3F -> OperationKind.INT |
| 51 | + 0x40, 0x44, 0x48, 0x4F -> OperationKind.FLOAT |
| 52 | + in 0x50..0x5F -> OperationKind.DECIMAL |
| 53 | + in 0x62..0x6F -> OperationKind.TIMESTAMP |
| 54 | + in 0x70..0x7F -> OperationKind.SYMBOL |
| 55 | + in 0x80..0x8F -> OperationKind.STRING |
| 56 | + in 0x90..0x9F -> OperationKind.CLOB |
| 57 | + in 0xA0..0xAF -> OperationKind.BLOB |
| 58 | + in 0xB0..0xBF -> OperationKind.LIST |
| 59 | + in 0xC0..0xCF -> OperationKind.SEXP |
| 60 | + 0xD0, in 0xD2..0xDF -> OperationKind.STRUCT |
| 61 | + 0xE0 -> OperationKind.IVM |
| 62 | + in 0xE3..0xEE -> OperationKind.ANNOTATIONS |
| 63 | + // Everything else: 12..1E, 30, D1, EF, F0..FF, illegal timestamp, float, and annotations sizes |
| 64 | + else -> OperationKind.UNSET |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * A lookup table containing the value length for Ion 1.0 type IDs. |
| 70 | + * A value of -1 indicates that the length follows as a `VarUInt`. |
| 71 | + * A value of -2 indicates that the typeId is not a valid Ion 1.0 typeId. |
| 72 | + * |
| 73 | + * TODO(perf): If there's not a noticeable amount of overhead, we should hide this behind a method instead of |
| 74 | + * exposing it directly and suppressing the spotbugs error. |
| 75 | + */ |
| 76 | + @JvmStatic |
| 77 | + @get:SuppressFBWarnings("MS_EXPOSE_REP", justification = "it is exposed for internal use only as a performance optimization") |
| 78 | + val TYPE_LENGTHS = IntArray(256) { initTypeLength(it) } |
| 79 | + |
| 80 | + @JvmStatic |
| 81 | + @OptIn(ExperimentalStdlibApi::class) |
| 82 | + private fun initTypeLength(typeId: Int): Int { |
| 83 | + return when (typeId) { |
| 84 | + // The length of all of these is determined entirely by the low-nibble of the typeId. |
| 85 | + 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, /* */ 0x70, 0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0 /* */ -> 0 |
| 86 | + 0x01, /* */ 0x21, 0x31, /* */ 0x51, /* */ 0x71, 0x81, 0x91, 0xA1, 0xB1, 0xC1 /* */ /* */ -> 1 |
| 87 | + 0x02, 0x12, 0x22, 0x32, /* */ 0x52, 0x62, 0x72, 0x82, 0x92, 0xA2, 0xB2, 0xC2, 0xD2 /* */ -> 2 |
| 88 | + 0x03, 0x13, 0x23, 0x33, /* */ 0x53, 0x63, 0x73, 0x83, 0x93, 0xA3, 0xB3, 0xC3, 0xD3, 0xE3 -> 3 |
| 89 | + 0x04, 0x14, 0x24, 0x34, 0x44, 0x54, 0x64, 0x74, 0x84, 0x94, 0xA4, 0xB4, 0xC4, 0xD4, 0xE4 -> 4 |
| 90 | + 0x05, 0x15, 0x25, 0x35, /* */ 0x55, 0x65, 0x75, 0x85, 0x95, 0xA5, 0xB5, 0xC5, 0xD5, 0xE5 -> 5 |
| 91 | + 0x06, 0x16, 0x26, 0x36, /* */ 0x56, 0x66, 0x76, 0x86, 0x96, 0xA6, 0xB6, 0xC6, 0xD6, 0xE6 -> 6 |
| 92 | + 0x07, 0x17, 0x27, 0x37, /* */ 0x57, 0x67, 0x77, 0x87, 0x97, 0xA7, 0xB7, 0xC7, 0xD7, 0xE7 -> 7 |
| 93 | + 0x08, 0x18, 0x28, 0x38, 0x48, 0x58, 0x68, 0x78, 0x88, 0x98, 0xA8, 0xB8, 0xC8, 0xD8, 0xE8 -> 8 |
| 94 | + 0x09, 0x19, 0x29, 0x39, /* */ 0x59, 0x69, 0x79, 0x89, 0x99, 0xA9, 0xB9, 0xC9, 0xD9, 0xE9 -> 9 |
| 95 | + 0x0A, 0x1A, 0x2A, 0x3A, /* */ 0x5A, 0x6A, 0x7A, 0x8A, 0x9A, 0xAA, 0xBA, 0xCA, 0xDA, 0xEA -> 10 |
| 96 | + 0x0B, 0x1B, 0x2B, 0x3B, /* */ 0x5B, 0x6B, 0x7B, 0x8B, 0x9B, 0xAB, 0xBB, 0xCB, 0xDB, 0xEB -> 11 |
| 97 | + 0x0C, 0x1C, 0x2C, 0x3C, /* */ 0x5C, 0x6C, 0x7C, 0x8C, 0x9C, 0xAC, 0xBC, 0xCC, 0xDC, 0xEC -> 12 |
| 98 | + 0x0D, 0x1D, 0x2D, 0x3D, /* */ 0x5D, 0x6D, 0x7D, 0x8D, 0x9D, 0xAD, 0xBD, 0xCD, 0xDD, 0xED -> 13 |
| 99 | + 0x0E, 0x1E, 0x2E, 0x3E, /* */ 0x5E, 0x6E, 0x7E, 0x8E, 0x9E, 0xAE, 0xBE, 0xCE, 0xDE, 0xEE -> -1 |
| 100 | + // Bool True |
| 101 | + 0x11 -> 0 |
| 102 | + // Nulls |
| 103 | + 0x0F, 0x1F, 0x2F, 0x3F, 0x4F, 0x5F, 0x6F, 0x7F, 0x8F, 0x9F, 0xAF, 0xBF, 0xCF, 0xDF -> 0 |
| 104 | + // IVM |
| 105 | + 0xE0 -> 3 // ...3 more than the typeId byte. |
| 106 | + |
| 107 | + // Reserved and/or illegal typeIds |
| 108 | + 0xEF, 0x60, 0x61, 0xD1, 0xE1, 0xE2, |
| 109 | + in 0x41..0x4E, // Illegal float lengths. 0x44 and 0x48 are trapped in the earlier condition. |
| 110 | + in 0xF0..0xFF -> -2 |
| 111 | + else -> TODO("This should be unreachable: ${typeId.toHexString()}") |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments