[jit] Factor out duplicate code from the castclass/unbox.any opcode implementation.