Java 解释器或任何解释器究竟是如何工作的? [英] How exactly does the Java interpreter or any interpreter work?

查看:17
本文介绍了Java 解释器或任何解释器究竟是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在弄清楚口译员的确切工作方式,在谷歌上搜索并得出了一些结论,只是希望能够让我更好地了解口译员工作的人来纠正它.

I have been figuring out the exact working of an interpreter, have googled around and have come up with some conclusion, just wanted it to be rectified by someone who can give me a better understanding of the working of interpreter.

所以我的理解是:

  1. 解释器是一种将高级代码转换为语言转机器格式.
  2. 具体来说java解释器,它获取二进制格式的代码(之前被 java 编译器从源代码翻译成字节码).
  3. 现在 Java 解释器的平台是运行它的 JVM,所以基本上它会生成可以被 JVM 运行的代码.
  4. 所以它需要字节码产生中间码和目标机器代码并将其提供给 JVM.
  5. JVM 依次在 JVM 所在的操作系统平台上执行该代码已实施或正在运行.

现在我仍然不清楚中间发生的子过程,即

Now i am still not clear with the sub process that happens in between i.e.

  1. 解释器产生中间代码.
  2. 然后优化解释的代码.
  3. 然后生成目标代码
  4. 最终被执行.

还有一些问题:

  • 那么解释器是否单独负责生成目标代码?和执行它?
  • 执行是否意味着它在 JVM 或底层操作系统中执行?

推荐答案

解释器是将代码从高级语言转换为机器格式的软件程序.

An interpreter is a software program that converts code from high level language to machine format.

没有.那是一个编译器.interpreter 是一个执行所写指令的计算机程序直接用一种语言.这与将高级语言转换为低级语言的编译器不同.C 编译器从 C 语言转换为汇编代码,而汇编器(另一种类型的编译器)将汇编语言转换为机器代码——现代 C 编译器执行从 C 语言转换为机器代码的两个步骤.

No. That's a compiler. An interpreter is a computer program that executes the instructions written in a language directly. This is different from a compiler that converts a higher level language into a lower language. The C compiler goes from C to assembly code with the assembler (another type of compiler) translates from assembly to machine code -- modern C compilers do both steps to go from C to machine code.

在 Java 中,java 编译器进行代码验证并将 Java 源代码转换为字节码类文件.它还执行一些小的处理任务,例如常量的预计算(如果可能)、字符串的缓存等.

In Java, the java compiler does code verification and converts from Java source to byte-code class files. It also does a number of small processing tasks such as pre-calculation of constants (if possible), caching of strings, etc..

现在java解释器的平台是JVM,它在其中运行,所以基本上它会产生可以被JVM运行的代码.

now platform for a java interpreter is the JVM, in which it runs, so basically it is going to produce code which can be run by JVM.

JVM 直接对字节码进行操作.Java 解释器与 JVM 集成得如此紧密,以至于它们不应该被认为是单独的实体.同样发生的是大量的优化,其中字节码基本上是动态优化的.这使得仅将其称为解释器是不够的.见下文.

The JVM operates on the bytecode directly. The java interpreter is integrated so closely with the JVM that they shouldn't really be thought of as separate entities. What also is happening is a crap-ton of optimization where bytecode is basically optimized on the fly. This makes calling it just an interpreter inadequate. See below.

所以它需要字节码产生中间码和目标机器码并把它交给JVM.

so it takes the bytecode produces intermediate code and the target machine code and gives it to JVM.

JVM 正在执行这些转换.

The JVM is doing these translations.

JVM 依次在实现或运行 JVM 的操作系统平台上执行该代码.

JVM in turns executes that code on the OS platform in which JVM is implemented or being run.

我宁愿说 JVM 使用字节码、优化的用户代码、包含 java 和本机代码的 java 库以及 OS 调用来执行 java 应用程序.

I'd rather say that the JVM uses the bytecode, optimized user code, the java libraries which include java and native code, in conjunction with OS calls to execute java applications.

现在我仍然不清楚中间发生的子过程,即 1. 解释器产生中间代码.2. 然后优化解释代码.3.然后生成目标代码 4.最后执行.

now i am still not clear with the sub process that happens in between i.e. 1. interpreter produces intermediate code. 2. interpreted code is then optimized. 3. then target code is generated 4. and finally executed.

Java 编译器生成字节码.当 JVM 执行代码时,步骤 2-4 在 JVM 内部运行时发生.它与 C (例如)非常不同,C 中的这些单独的步骤由不同的实用程序运行.不要将其视为子进程",将其视为 JVM 内部的模块.

The Java compiler generates bytecode. When the JVM executes the code, steps 2-4 happen at runtime inside of the JVM. It is very different than C (for example) which has these separate steps being run by different utilities. Don't think about this as "subprocesses", think about it as modules inside of the JVM.

那么解释器是否单独负责生成目标代码?并执行它?

so is the interpreter alone responsible for generating target code ? and executing it ?

有点.根据定义,JVM 的解释器读取字节码并直接执行它.但是,在现代 JVM 中,解释器与即时编译器 (JIT) 协同工作以 即时生成本机代码,以便 JVM 可以让您的代码更有效地执行.

Sort of. The JVM's interpreter by definition reads the bytecode and executes it directly. However, in modern JVMs, the interpreter works in tandem with the Just-In-Time compiler (JIT) to generate native code on the fly so that the JVM can have your code execute more efficiently.

此外,还有后处理编译"阶段,可在运行时分析生成的代码,以便通过内联常用代码块和其他机制来优化本机代码.这就是 JVM 负载在启动时如此高的原因.它不仅加载到 jars 和类文件中,而且实际上是在运行中执行 cc -O3.

In addition, there are post-processing "compilation" stages which analyze the generated code at runtime so that native code can be optimized by inlining often-used code blocks and through other mechanisms. This is the reason why the JVM load spikes so high on startup. Not only is it loading in the jars and class files, but it is in effect doing a cc -O3 on the fly.

执行是否意味着它在 JVM 或底层操作系统中执行?

and does executing means it gets executed in JVM or in the underlying OS ?

虽然我们谈论的是 JVM 执行代码,但这在技术上并不正确.一旦字节码被翻译成本机代码,JVM 和您的 Java 应用程序的执行就由 CPU 和硬件架构的其余部分完成.

Although we talk about the JVM executing the code, this is not technically correct. As soon as the byte-code is translated into native code, the execution of the JVM and your java application is done by the CPU and the rest of the hardware architecture.

操作系统是执行所有进程和资源管理的基础,因此程序可以有效地共享硬件并高效执行.该操作系统还为应用程序提供 API,以便轻松访问磁盘、网络、内存和其他硬件和资源.

The Operating System is the substrate that that does all of the process and resource management so the programs can efficiently share the hardware and execute efficiently. The OS also provides the APIs for applications to easily access the disk, network, memory, and other hardware and resources.

这篇关于Java 解释器或任何解释器究竟是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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