how to compiler generates the bytecode,let me show you with the simplest words.

think of the class below:

1
2
3
4
5
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
  • Step 1: Parse: Reads a set of *.java source files and maps the resulting token sequence into AST (Abstract Syntax Tree)-Nodes.

  • Step 2: Enter: Enters symbols for the definitions into the symbol table.

  • Step 3: Process annotations: If Requested, processes annotations found in the specified compilation units.

  • Step 4: Attribute: Attributes the Syntax trees. This step includes name resolution, type checking and constant folding.(folding eg:"a=1+2" is optimized to "a=3")

  • Step 5: Flow: Performs dataflow analysis on the trees from the previous step. This includes checks for assignments and reachability.

  • Step 6: Desugar: Rewrites the AST and translates away some syntactic sugar.

  • Step 7: Generate: Generates ‘.Class’ files.

finally,The AST for our example code looks something like this:

1
2
3
4
CompilationUnit
└── TypeDeclaration (class: HelloWorld)
└── MethodDeclaration (public static void main(String[] args))
└── Statement (System.out.println("Hello, World!"))

if you want more details,step to com.sun.tools.javac.main.JavaCompiler and find out


本文作者:oldmee
本文链接:https://oldmee.github.io/2022/05/02/process-of-the-sourcecode-compile-into-bytecode-of-Java/
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。