tinycodinglm / lang /lang-0033.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
3.7 kB
opencode trace
System You are an expert Java bytecode engineer specializing in ASM, bytecode manipulation, and class transformation.

You are an expert Java bytecode engineer specializing in ASM, bytecode manipulation, and class transformation.

User

How does javac compile switch statements? Show me TABLESWITCH vs LOOKUPSWITCH in ASM bytecode.

Assistant

Switch statements compile to TABLESWITCH (dense) or LOOKUPSWITCH (sparse) opcodes:

import org.objectweb.asm.*;

public class SwitchExample {
public static byte[] generateDenseSwitch() {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
cw.visit(Opcodes.V11, Opcodes.ACC_PUBLIC, "SwitchExample", null, "java/lang/Object", null);

markdown
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "denseSwitch", "(I)I", null, null);
    mv.visitCode();

    Label dflt = new Label();
    Label case1 = new Label();
    Label case2 = new Label();
    Label case3 = new Label();

    mv.visitVarInsn(Opcodes.ILOAD, 1);
    mv.visitTableSwitchInsn(1, 3, dflt, case1, case2, case3);

    mv.visitLabel(case1);
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitInsn(Opcodes.IRETURN);

    mv.visitLabel(case2);
    mv.visitInsn(Opcodes.ICONST_2);
    mv.visitInsn(Opcodes.IRETURN);

    mv.visitLabel(case3);
    mv.visitInsn(Opcodes.ICONST_3);
    mv.visitInsn(Opcodes.IRETURN);

    mv.visitLabel(dflt);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitInsn(Opcodes.IRETURN);

    mv.visitMaxs(1, 2);
    mv.visitEnd();
    cw.visitEnd();
    return cw.toByteArray();
}

public static byte[] generateSparseSwitch() {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    cw.visit(Opcodes.V11, Opcodes.ACC_PUBLIC, "SwitchExample", null, "java/lang/Object", null);

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "sparseSwitch", "(I)I", null, null);
    mv.visitCode();

    Label dflt = new Label();
    Label case100 = new Label();
    Label case5000 = new Label();
    Label case99999 = new Label();

    mv.visitVarInsn(Opcodes.ILOAD, 1);
    mv.visitLookupSwitchInsn(dflt,
        new int[]{100, 5000, 99999},
        new Label[]{case100, case5000, case99999});

    mv.visitLabel(case100);
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitInsn(Opcodes.IRETURN);

    mv.visitLabel(case5000);
    mv.visitInsn(Opcodes.ICONST_2);
    mv.visitInsn(Opcodes.IRETURN);

    mv.visitLabel(case99999);
    mv.visitInsn(Opcodes.ICONST_3);
    mv.visitInsn(Opcodes.IRETURN);

    mv.visitLabel(dflt);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitInsn(Opcodes.IRETURN);

    mv.visitMaxs(1, 2);
    mv.visitEnd();
    cw.visitEnd();
    return cw.toByteArray();
}

}

When javac compiles switch:

  • Dense cases (1,2,3) use TABLESWITCH - an array-based jump table, O(1) lookup
  • Sparse cases (100,5000,99999) use LOOKUPSWITCH - a sorted key-value array, O(log n) lookup
  • javac decides based on density: if case values are contiguous or near-contiguous, TABLESWITCH is used
  • TABLESWITCH: visitTableSwitchInsn(min, max, dflt, labels)
  • LOOKUPSWITCH: visitLookupSwitchInsn(dflt, keys, labels)
  • The default label is always required
  • String switches compile to hashCode/equals chains with tableswitch for hash values