简体   繁体   English

Java编译器:Java语言应该如何映射到JVM指令?

[英]Java Compiler: how should Java language be mapped to JVM instructions?

The semantics of constructs in Java language are defined by the Java language specification, but they're not in terms of JVM instructions. Java语言中的构造语义由Java语言规范定义,但它们不是JVM指令。 So, is there possiblity that the specification is not strictly defined to prevent mis-implementation of Java compilers? 那么,是否有可能严格定义规范以防止错误实现Java编译器? Or the mapping between Java language and JVM instructions is pretty straightforward, there's no need to worry about this? 或者Java语言和JVM指令之间的映射非常简单,没有必要担心这个问题?

It would defeat much of the point of separating the language from the bytecode for there to be a clear mapping of Java code to JVM instructions. 它会破坏将语言与字节码分离的大部分内容,以便将Java代码清晰地映射到JVM指令。 Instead the compiler has the freedom to make determinations of which instructions best represent the provided source, in order to optimize the efficiency of what's actually run. 相反,编译器可以自由地确定哪个指令最能代表所提供的源,以便优化实际运行的效率。

This does mean it's possible for a compiler to do the wrong thing, however generally speaking you can trust major compilers like Oracle's are doing the right thing; 这确实意味着编译器可能会做错事,但一般来说,你可以相信像Oracle这样的主要编译器正在做正确的事情; they get tested a lot . 他们经受了很多考验。

The semantics of constructs in Java language are defined by the Java language specification, but they're not in terms of JVM instructions. Java语言中的构造语义由Java语言规范定义,但它们不是JVM指令。 So, is there possiblity that the specification is not strictly defined to prevent mis-implementation of Java compilers? 那么,是否有可能严格定义规范以防止错误实现Java编译器?

Yes, definitely. 当然是。 Later editions of the Java Language Specification use more formal notation than previous versions. Java语言规范的后续版本使用比以前版本更正式的表示法。 This should help to reduce the possibility of ambiguity and misinterpretation, but any language specification primarily written in English is prone to misinterpretation. 这应该有助于减少模糊和误解的可能性,但任何主要用英语编写的语言规范都容易被误解。

Or the mapping between Java language and JVM instructions is pretty straightforward, there's no need to worry about this? 或者Java语言和JVM指令之间的映射非常简单,没有必要担心这个问题?

I think 'straightforward' is a subjective measure. 我认为“直截了当”是一种主观的衡量标准。 :o) It's more straightforward than I first expected, certainly. :o)当然,它比我的预期更直截了当。

The compiler ( javac ) itself doesn't appear to do much in the way of optimisation, see this question for example. 编译器( javac )本身在优化方面似乎没有做太多,例如,请参阅此问题 Most optimisations are actually done by the JVM at runtime (by HotSpot). 大多数优化实际上是由JVM在运行时完成的(通过HotSpot)。

Other compiler implementations might do more optimisations. 其他编译器实现可能会进行更多优化。 Also, there are some bytecode processors (like ProGuard) that perform optimisations on the bytecode. 此外,还有一些字节码处理器(如ProGuard)对字节码执行优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM