package p.studio.compiler.backend.bytecode;

import java.util.Map;

final class BytecodeOpcodeLayout {
    private static final Map<Integer, Integer> SIZE_BY_OPCODE = Map.ofEntries(
            Map.entry(0x01, 2),
            Map.entry(0x02, 6),
            Map.entry(0x03, 6),
            Map.entry(0x04, 6),
            Map.entry(0x10, 6),
            Map.entry(0x11, 2),
            Map.entry(0x14, 10),
            Map.entry(0x15, 10),
            Map.entry(0x16, 3),
            Map.entry(0x17, 6),
            Map.entry(0x18, 6),
            Map.entry(0x20, 2),
            Map.entry(0x21, 2),
            Map.entry(0x22, 2),
            Map.entry(0x23, 2),
            Map.entry(0x24, 2),
            Map.entry(0x30, 2),
            Map.entry(0x31, 2),
            Map.entry(0x32, 2),
            Map.entry(0x33, 2),
            Map.entry(0x34, 2),
            Map.entry(0x35, 2),
            Map.entry(0x36, 2),
            Map.entry(0x3C, 2),
            Map.entry(0x3D, 2),
            Map.entry(0x3E, 2),
            Map.entry(0x40, 6),
            Map.entry(0x41, 6),
            Map.entry(0x42, 6),
            Map.entry(0x43, 6),
            Map.entry(0x50, 6),
            Map.entry(0x51, 2),
            Map.entry(0x52, 10),
            Map.entry(0x54, 10),
            Map.entry(0x56, 6),
            Map.entry(0x70, 6),
            Map.entry(0x71, 6),
            Map.entry(0x72, 6));

    private BytecodeOpcodeLayout() {
    }

    static int sizeForOpcode(final int opcode) {
        final var size = SIZE_BY_OPCODE.get(opcode);
        if (size == null) {
            throw new BytecodeMarshalingException(
                    BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_UNKNOWN_OPCODE,
                    "unknown opcode 0x%04X".formatted(opcode));
        }
        return size;
    }
}
