File size: 3,702 Bytes
b27bfb3 | 1 2 3 4 5 | {"harness": "opencode", "name": "Switch Statements TABLESWITCH vs LOOKUPSWITCH", "id": "lang-0033", "type": "session"}
{"message": {"content": "You are an expert Java bytecode engineer specializing in ASM, bytecode manipulation, and class transformation.", "role": "system"}, "type": "message"}
{"message": {"content": "How does javac compile switch statements? Show me TABLESWITCH vs LOOKUPSWITCH in ASM bytecode.", "role": "user"}, "type": "message"}
{"message": {"content": "Switch statements compile to TABLESWITCH (dense) or LOOKUPSWITCH (sparse) opcodes:\n\nimport org.objectweb.asm.*;\n\npublic class SwitchExample {\n public static byte[] generateDenseSwitch() {\n ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);\n cw.visit(Opcodes.V11, Opcodes.ACC_PUBLIC, \"SwitchExample\", null, \"java/lang/Object\", null);\n\n MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, \"denseSwitch\", \"(I)I\", null, null);\n mv.visitCode();\n\n Label dflt = new Label();\n Label case1 = new Label();\n Label case2 = new Label();\n Label case3 = new Label();\n\n mv.visitVarInsn(Opcodes.ILOAD, 1);\n mv.visitTableSwitchInsn(1, 3, dflt, case1, case2, case3);\n\n mv.visitLabel(case1);\n mv.visitInsn(Opcodes.ICONST_1);\n mv.visitInsn(Opcodes.IRETURN);\n\n mv.visitLabel(case2);\n mv.visitInsn(Opcodes.ICONST_2);\n mv.visitInsn(Opcodes.IRETURN);\n\n mv.visitLabel(case3);\n mv.visitInsn(Opcodes.ICONST_3);\n mv.visitInsn(Opcodes.IRETURN);\n\n mv.visitLabel(dflt);\n mv.visitInsn(Opcodes.ICONST_0);\n mv.visitInsn(Opcodes.IRETURN);\n\n mv.visitMaxs(1, 2);\n mv.visitEnd();\n cw.visitEnd();\n return cw.toByteArray();\n }\n\n public static byte[] generateSparseSwitch() {\n ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);\n cw.visit(Opcodes.V11, Opcodes.ACC_PUBLIC, \"SwitchExample\", null, \"java/lang/Object\", null);\n\n MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, \"sparseSwitch\", \"(I)I\", null, null);\n mv.visitCode();\n\n Label dflt = new Label();\n Label case100 = new Label();\n Label case5000 = new Label();\n Label case99999 = new Label();\n\n mv.visitVarInsn(Opcodes.ILOAD, 1);\n mv.visitLookupSwitchInsn(dflt,\n new int[]{100, 5000, 99999},\n new Label[]{case100, case5000, case99999});\n\n mv.visitLabel(case100);\n mv.visitInsn(Opcodes.ICONST_1);\n mv.visitInsn(Opcodes.IRETURN);\n\n mv.visitLabel(case5000);\n mv.visitInsn(Opcodes.ICONST_2);\n mv.visitInsn(Opcodes.IRETURN);\n\n mv.visitLabel(case99999);\n mv.visitInsn(Opcodes.ICONST_3);\n mv.visitInsn(Opcodes.IRETURN);\n\n mv.visitLabel(dflt);\n mv.visitInsn(Opcodes.ICONST_0);\n mv.visitInsn(Opcodes.IRETURN);\n\n mv.visitMaxs(1, 2);\n mv.visitEnd();\n cw.visitEnd();\n return cw.toByteArray();\n }\n}\n\nWhen javac compiles switch:\n- Dense cases (1,2,3) use TABLESWITCH - an array-based jump table, O(1) lookup\n- Sparse cases (100,5000,99999) use LOOKUPSWITCH - a sorted key-value array, O(log n) lookup\n- javac decides based on density: if case values are contiguous or near-contiguous, TABLESWITCH is used\n- TABLESWITCH: visitTableSwitchInsn(min, max, dflt, labels)\n- LOOKUPSWITCH: visitLookupSwitchInsn(dflt, keys, labels)\n- The default label is always required\n- String switches compile to hashCode/equals chains with tableswitch for hash values", "role": "assistant"}, "type": "message"}
|