Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Java compile to assembly twice? [duplicate]

Tags:

java

assembly

I compiled a simple Java file to assembly using Java 8 on Mac OS X. This is Test.java:

public class Test {

    static volatile int a = 1;

    public static void main(String[] args) {
        a++;
    }
}

I output the assembly code using:

java -server -Xcomp -XX:+UnlockDiagnosticVMOptions -XX:-Inline -XX:CompileCommand=print,*Test.main Test>Test.asm

This is the Test.asm output:

CompilerOracle: print *Test.main
Compiled method (c1)    1733 1750       3       Test::main (9 bytes)
 total in heap  [0x0000000111848650,0x0000000111848970] = 800
 relocation     [0x0000000111848778,0x00000001118487a0] = 40
 main code      [0x00000001118487a0,0x0000000111848860] = 192
 stub code      [0x0000000111848860,0x00000001118488f0] = 144
 oops           [0x00000001118488f0,0x00000001118488f8] = 8
 metadata       [0x00000001118488f8,0x0000000111848900] = 8
 scopes data    [0x0000000111848900,0x0000000111848918] = 24
 scopes pcs     [0x0000000111848918,0x0000000111848968] = 80
 dependencies   [0x0000000111848968,0x0000000111848970] = 8
Loaded disassembler from /Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/hsdis-amd64.dylib
Decoding compiled method 0x0000000111848650:
Code:
[Disassembling for mach='i386:x86-64']
[Entry Point]
[Verified Entry Point]
[Constants]
  # {method} {0x000000010e40e240} 'main' '([Ljava/lang/String;)V' in 'Test'
  # parm0:    rsi:rsi   = '[Ljava/lang/String;'
  #           [sp+0x40]  (sp of caller)
  0x00000001118487a0: mov    %eax,-0x14000(%rsp)
  0x00000001118487a7: push   %rbp
  0x00000001118487a8: sub    $0x30,%rsp
  0x00000001118487ac: movabs $0x10e40e4d0,%rdi  ;   {metadata(method data for {method} {0x000000010e40e240} 'main' '([Ljava/lang/String;)V' in 'Test')}
  0x00000001118487b6: mov    0xdc(%rdi),%ebx
  0x00000001118487bc: add    $0x8,%ebx
  0x00000001118487bf: mov    %ebx,0xdc(%rdi)
  0x00000001118487c5: movabs $0x10e40e240,%rdi  ;   {metadata({method} {0x000000010e40e240} 'main' '([Ljava/lang/String;)V' in 'Test')}
  0x00000001118487cf: and    $0x0,%ebx
  0x00000001118487d2: cmp    $0x0,%ebx
  0x00000001118487d5: je     0x00000001118487fe
  0x00000001118487db: movabs $0x7955fd1e8,%rsi  ;   {oop(a 'java/lang/Class' = 'Test')}
  0x00000001118487e5: mov    0x68(%rsi),%edi    ;*getstatic a
                                                ; - Test::main@0 (line 6)

  0x00000001118487e8: inc    %edi
  0x00000001118487ea: mov    %edi,0x68(%rsi)
  0x00000001118487ed: lock addl $0x0,(%rsp)     ;*putstatic a
                                                ; - Test::main@5 (line 6)

  0x00000001118487f2: add    $0x30,%rsp
  0x00000001118487f6: pop    %rbp
  0x00000001118487f7: test   %eax,-0x5f7f6fd(%rip)        # 0x000000010b8c9100
                                                ;   {poll_return}
  0x00000001118487fd: retq   
  0x00000001118487fe: mov    %rdi,0x8(%rsp)
  0x0000000111848803: movq   $0xffffffffffffffff,(%rsp)
  0x000000011184880b: callq  0x0000000111543f60  ; OopMap{rsi=Oop off=112}
                                                ;*synchronization entry
                                                ; - Test::main@-1 (line 6)
                                                ;   {runtime_call}
  0x0000000111848810: jmp    0x00000001118487db
  0x0000000111848812: nop
  0x0000000111848813: nop
  0x0000000111848814: mov    0x2a8(%r15),%rax
  0x000000011184881b: movabs $0x0,%r10
  0x0000000111848825: mov    %r10,0x2a8(%r15)
  0x000000011184882c: movabs $0x0,%r10
  0x0000000111848836: mov    %r10,0x2b0(%r15)
  0x000000011184883d: add    $0x30,%rsp
  0x0000000111848841: pop    %rbp
  0x0000000111848842: jmpq   0x00000001114b24a0  ;   {runtime_call}
  0x0000000111848847: hlt    
  0x0000000111848848: hlt    
  0x0000000111848849: hlt    
  0x000000011184884a: hlt    
  0x000000011184884b: hlt    
  0x000000011184884c: hlt    
  0x000000011184884d: hlt    
  0x000000011184884e: hlt    
  0x000000011184884f: hlt    
  0x0000000111848850: hlt    
  0x0000000111848851: hlt    
  0x0000000111848852: hlt    
  0x0000000111848853: hlt    
  0x0000000111848854: hlt    
  0x0000000111848855: hlt    
  0x0000000111848856: hlt    
  0x0000000111848857: hlt    
  0x0000000111848858: hlt    
  0x0000000111848859: hlt    
  0x000000011184885a: hlt    
  0x000000011184885b: hlt    
  0x000000011184885c: hlt    
  0x000000011184885d: hlt    
  0x000000011184885e: hlt    
  0x000000011184885f: hlt    
[Exception Handler]
[Stub Code]
  0x0000000111848860: callq  0x00000001115418e0  ;   {no_reloc}
  0x0000000111848865: mov    %rsp,-0x28(%rsp)
  0x000000011184886a: sub    $0x80,%rsp
  0x0000000111848871: mov    %rax,0x78(%rsp)
  0x0000000111848876: mov    %rcx,0x70(%rsp)
  0x000000011184887b: mov    %rdx,0x68(%rsp)
  0x0000000111848880: mov    %rbx,0x60(%rsp)
  0x0000000111848885: mov    %rbp,0x50(%rsp)
  0x000000011184888a: mov    %rsi,0x48(%rsp)
  0x000000011184888f: mov    %rdi,0x40(%rsp)
  0x0000000111848894: mov    %r8,0x38(%rsp)
  0x0000000111848899: mov    %r9,0x30(%rsp)
  0x000000011184889e: mov    %r10,0x28(%rsp)
  0x00000001118488a3: mov    %r11,0x20(%rsp)
  0x00000001118488a8: mov    %r12,0x18(%rsp)
  0x00000001118488ad: mov    %r13,0x10(%rsp)
  0x00000001118488b2: mov    %r14,0x8(%rsp)
  0x00000001118488b7: mov    %r15,(%rsp)
  0x00000001118488bb: movabs $0x10c7d8060,%rdi  ;   {external_word}
  0x00000001118488c5: movabs $0x111848865,%rsi  ;   {internal_word}
  0x00000001118488cf: mov    %rsp,%rdx
  0x00000001118488d2: and    $0xfffffffffffffff0,%rsp
  0x00000001118488d6: callq  0x000000010c60361a  ;   {runtime_call}
  0x00000001118488db: hlt    
[Deopt Handler Code]
  0x00000001118488dc: movabs $0x1118488dc,%r10  ;   {section_word}
  0x00000001118488e6: push   %r10
  0x00000001118488e8: jmpq   0x000000011148d3c0  ;   {runtime_call}
  0x00000001118488ed: hlt    
  0x00000001118488ee: hlt    
  0x00000001118488ef: hlt    
OopMapSet contains 1 OopMaps

#0 
OopMap{rsi=Oop off=112}
Compiled method (c1)    1747 1751       1       Test::main (9 bytes)
 total in heap  [0x0000000111847250,0x0000000111847518] = 712
 relocation     [0x0000000111847378,0x00000001118473a0] = 40
 main code      [0x00000001118473a0,0x0000000111847420] = 128
 stub code      [0x0000000111847420,0x00000001118474b0] = 144
 oops           [0x00000001118474b0,0x00000001118474b8] = 8
 metadata       [0x00000001118474b8,0x00000001118474c0] = 8
 scopes data    [0x00000001118474c0,0x00000001118474d0] = 16
 scopes pcs     [0x00000001118474d0,0x0000000111847510] = 64
 dependencies   [0x0000000111847510,0x0000000111847518] = 8
Decoding compiled method 0x0000000111847250:
Code:
[Entry Point]
[Verified Entry Point]
[Constants]
  # {method} {0x000000010e40e240} 'main' '([Ljava/lang/String;)V' in 'Test'
  # parm0:    rsi:rsi   = '[Ljava/lang/String;'
  #           [sp+0x40]  (sp of caller)
  0x00000001118473a0: mov    %eax,-0x14000(%rsp)
  0x00000001118473a7: push   %rbp
  0x00000001118473a8: sub    $0x30,%rsp
  0x00000001118473ac: movabs $0x7955fd1e8,%rsi  ;   {oop(a 'java/lang/Class' = 'Test')}
  0x00000001118473b6: mov    0x68(%rsi),%edi    ;*getstatic a
                                                ; - Test::main@0 (line 6)

  0x00000001118473b9: inc    %edi
  0x00000001118473bb: mov    %edi,0x68(%rsi)
  0x00000001118473be: lock addl $0x0,(%rsp)     ;*putstatic a
                                                ; - Test::main@5 (line 6)

  0x00000001118473c3: add    $0x30,%rsp
  0x00000001118473c7: pop    %rbp
  0x00000001118473c8: test   %eax,-0x5f7e2ce(%rip)        # 0x000000010b8c9100
                                                ;   {poll_return}
  0x00000001118473ce: retq   
  0x00000001118473cf: nop
  0x00000001118473d0: nop
  0x00000001118473d1: mov    0x2a8(%r15),%rax
  0x00000001118473d8: movabs $0x0,%r10
  0x00000001118473e2: mov    %r10,0x2a8(%r15)
  0x00000001118473e9: movabs $0x0,%r10
  0x00000001118473f3: mov    %r10,0x2b0(%r15)
  0x00000001118473fa: add    $0x30,%rsp
  0x00000001118473fe: pop    %rbp
  0x00000001118473ff: jmpq   0x00000001114b24a0  ;   {runtime_call}
  0x0000000111847404: hlt    
  0x0000000111847405: hlt    
  0x0000000111847406: hlt    
  0x0000000111847407: hlt    
  0x0000000111847408: hlt    
  0x0000000111847409: hlt    
  0x000000011184740a: hlt    
  0x000000011184740b: hlt    
  0x000000011184740c: hlt    
  0x000000011184740d: hlt    
  0x000000011184740e: hlt    
  0x000000011184740f: hlt    
  0x0000000111847410: hlt    
  0x0000000111847411: hlt    
  0x0000000111847412: hlt    
  0x0000000111847413: hlt    
  0x0000000111847414: hlt    
  0x0000000111847415: hlt    
  0x0000000111847416: hlt    
  0x0000000111847417: hlt    
  0x0000000111847418: hlt    
  0x0000000111847419: hlt    
  0x000000011184741a: hlt    
  0x000000011184741b: hlt    
  0x000000011184741c: hlt    
  0x000000011184741d: hlt    
  0x000000011184741e: hlt    
  0x000000011184741f: hlt    
[Exception Handler]
[Stub Code]
  0x0000000111847420: callq  0x00000001115418e0  ;   {no_reloc}
  0x0000000111847425: mov    %rsp,-0x28(%rsp)
  0x000000011184742a: sub    $0x80,%rsp
  0x0000000111847431: mov    %rax,0x78(%rsp)
  0x0000000111847436: mov    %rcx,0x70(%rsp)
  0x000000011184743b: mov    %rdx,0x68(%rsp)
  0x0000000111847440: mov    %rbx,0x60(%rsp)
  0x0000000111847445: mov    %rbp,0x50(%rsp)
  0x000000011184744a: mov    %rsi,0x48(%rsp)
  0x000000011184744f: mov    %rdi,0x40(%rsp)
  0x0000000111847454: mov    %r8,0x38(%rsp)
  0x0000000111847459: mov    %r9,0x30(%rsp)
  0x000000011184745e: mov    %r10,0x28(%rsp)
  0x0000000111847463: mov    %r11,0x20(%rsp)
  0x0000000111847468: mov    %r12,0x18(%rsp)
  0x000000011184746d: mov    %r13,0x10(%rsp)
  0x0000000111847472: mov    %r14,0x8(%rsp)
  0x0000000111847477: mov    %r15,(%rsp)
  0x000000011184747b: movabs $0x10c7d8060,%rdi  ;   {external_word}
  0x0000000111847485: movabs $0x111847425,%rsi  ;   {internal_word}
  0x000000011184748f: mov    %rsp,%rdx
  0x0000000111847492: and    $0xfffffffffffffff0,%rsp
  0x0000000111847496: callq  0x000000010c60361a  ;   {runtime_call}
  0x000000011184749b: hlt    
[Deopt Handler Code]
  0x000000011184749c: movabs $0x11184749c,%r10  ;   {section_word}
  0x00000001118474a6: push   %r10
  0x00000001118474a8: jmpq   0x000000011148d3c0  ;   {runtime_call}
  0x00000001118474ad: hlt    
  0x00000001118474ae: hlt    
  0x00000001118474af: hlt    
OopMapSet contains 0 OopMaps

The question is: why does the generated assembly code have two main methods and how do I make it have only one?

like image 705
Dolphin Avatar asked Jun 01 '19 05:06

Dolphin


1 Answers

Why does the generated assembly code have two main methods?

The Assembly contains the result of the work of the C1 compiler at different compilation levels:

C1 / Level 3

C1 / Level 1

The appropriate information is present in the Test.asm output:

Compiled method (c1)    1733 1750       3       Test::main (9 bytes)

Compiled method (c1)    1747 1751       1       Test::main (9 bytes)

In order to remove an extra compilation overhead, you can set -XX:TieredStopAtLevel=1 - check this answer for more details.

like image 193
Oleksandr Pyrohov Avatar answered Oct 25 '22 17:10

Oleksandr Pyrohov