为什么没有ICMP指令? [英] Why is there no ICMP instruction?

查看:118
本文介绍了为什么没有ICMP指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可能已经知道,我们有大量的操作码用于比较不同类型的原始值:

As some of you might know, we have a ton of opcodes for comparing different types of primitive values:

LCMP
FCMPL
FCMPG
DCMPL
DCMPG
IFEQ
IFNE
IFLT
IFGE
IFGT
IFLE
IF_ICMPEQ
IF_ICMPNE
IF_ICMPLT
IF_ICMPGE
IF_ICMPGT
IF_ICMPLE
IF_ACMPEQ
IF_ACMPNE
...

出于明显的原因,指令集的创建者没有费心去添加所有IF_LCMPEQIF_FCMPLT,...指令,但是我想知道为什么没有ICMP指令,因为它会非常尤其适用于布尔值或Integer.compare(int, int).

For obvious reasons the creators of the instruction set did not bother to add all IF_LCMPEQ, IF_FCMPLT, ... instructions, but I am wondering why there is no ICMP instruction, seeing that it would be very useful especially for booleans or Integer.compare(int, int).

推荐答案

已经有两个主要基于意见"的接近投票.确实,没有人可以在这里给出确切的答案,当试图争论一群工程师25年前做出的决定时,可能涉及一些麻烦.但我会尝试一下...

首先,我认为这个问题是有道理的:int类型是Java语言中最突出"的类型(最后但并非最不重要的原因是因为它作为数组索引).这与它在Java虚拟机中的特殊角色并驾齐驱,在Java虚拟机中,该语言中存在的所有(较小)整数类型(如byteshort)都可以有效地转换为int进行所有计算.或者,如 Java所述虚拟机规范,第2.11.7节:

First of all, I think that the question is justified: The int type is the most "prominent" type in the Java Language (last but not least because of its role as an array index). This goes hand in hand with its special role in the Java Virtual Machine, where all (smaller) integral types that are present in the language, like byte or short, are effectively converted into int for all computations. Or, as mentioned in the Java Virtual Machine Specification, Section 2.11.7:

由于着重于int比较,Java虚拟机为int类型提供了丰富的条件分支指令补充.

Because of its emphasis on int comparisons, the Java Virtual Machine provides a rich complement of conditional branch instructions for type int.

现在可以合理地问为什么这个丰富的补语"似乎排除了对所有其他类型都同样存在的指令.

Now it is reasonable to ask why this "rich complement" seems to exclude an instruction that is equivalently present for all other types.

没有icmp指令的主要原因可能是它既没有必要,也没有好处.

The main reason why there is no icmp instruction may be that it is neither necessary, nor beneficial.

建议将其用于Integer#compare(int, int)的应用案例几乎不能算作参数:不会实现这种方法(即使存在icmp)

The suggested application case of using it for Integer#compare(int, int) can hardly count as an argument: The implementation of such a method (even if an icmp existed) would not be

return icmp, arg0, arg1;

将方法转换为字节码可能相当复杂,并且鉴于 Java语言本身的可能性,无论如何,这种方法都必须等效地实现

The translation of a method into bytecode can be rather complex, and given the possibilities of the Java Language itself, such a method anyhow has to be implemented equivalently as

if (x > y) return 1;
if (x < y) return -1;
return 0;

显然可以将

转换为现有if_icmp<?>指令的序列.

which obviously is can be translated into a sequence of the existing if_icmp<?> instructions.

在这里,应该记住,这些比较指令的主要目的是分支:它们导致 jump 跳转到另一个位置.它们并非旨在将栈中的值压入然后可以用作方法的返回值"的值.在这里,谈论语言和谈论虚拟机是完全不同的两件事.

Here, one should keep in mind that the main purpose of these comparison instructions is branching: They cause a jump to a different location. They are not intended for pushing a value on the stack that could then be "used as a return value of a method". Talking about the language and talking about the virtual machine are two completely different things here.

也可能会问一个问题,并问:为什么中有lcmpfcmp_dcmp_指令可用于longfloatdouble,分别?

One might also turn the question around, and ask: Why are there lcmp, fcmp_ and dcmp_ instructions available for long, float and double, respectively?

在这里,确定的答案要容易得多:为longfloat提供整套eqneltlegtge比较指令集.和double表示另外18条指令(甚至对于浮点类型使用NaN处理,甚至更多).考虑到一个字节可能有256条指令的硬性限制,这很多.

Here, a definite answer is far easier: Offering the whole set of eq, ne, lt, le, gt and ge comparision instructions for long, float and double would imply 18 additional instructions (or even more, with the NaN treatment for the floating point types). That's a lot, considering that there is a hard limit of 256 instructions that are possible with one byte.

通过为这些类型提供lcmpfcmp_dcmp_指令,可将int可用的其余指令用于模拟所有其他可能的比较情况.但是同样,这些主要用于分支,因此完全不需要icmp指令,因为对于int,所有必需的分支指令(跳转条件")已经可用.

By offering the lcmp, fcmp_ and dcmp_ instructions for these types, the remaining instructions that are available for int can be used to emulate all the other possible comparison situations. But again, these are mainly intended for branching, so there simply is no need for an icmp instruction, because for int, all necessary branching instructions ("jump conditions") are already available.

这篇关于为什么没有ICMP指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆