BestCoderEver
BestCoderEver

Reputation: 567

Understanding Java Hotspot Function Prologue

I'm having trouble understanding the function prologue that Java hotpot generates. Consider this dummy example:

public static int getLen(String s) {
    return s.length();
}

public static void main(String[] args) {
    for(int i = 0; i < 1_000_000; i++) {
        getLen("abcd");
    }
}

The generated code for getLen, by C2, is:

[Entry Point]
[Verified Entry Point]
[Constants]
  # {method} {0x00007fbed3fc2318} 'getLen' '(Ljava/lang/String;)I' in 'examples/Main'
  # parm0:    rsi:rsi   = 'java/lang/String'
  #           [sp+0x20]  (sp of caller)
  0x00007fbefd11a960: mov    %eax,-0x14000(%rsp)
  0x00007fbefd11a967: push   %rbp
  0x00007fbefd11a968: sub    $0x10,%rsp         ;*synchronization entry
                                                ; - examples.Main::getLen@-1 (line 6)

  0x00007fbefd11a96c: mov    0xc(%rsi),%r11d    ;*getfield value
                                                ; - java.lang.String::length@1 (line 623)
                                                ; - examples.Main::getLen@1 (line 6)
                                                ; implicit exception: dispatches to 0x00007fbefd11a981
  0x00007fbefd11a970: mov    0xc(%r12,%r11,8),%eax  ;*arraylength
                                                ; - java.lang.String::length@4 (line 623)
                                                ; - examples.Main::getLen@1 (line 6)
                                                ; implicit exception: dispatches to 0x00007fbefd11a991

Question:

  1. What is the purpose of mov %eax,-0x14000(%rsp)?
  2. The sample code has no synchronization; so what is sub $0x10,%rsp ;*synchronization entry?

Upvotes: 4

Views: 109

Answers (1)

tevemadar
tevemadar

Reputation: 13195

The 14000 is some magic number, searching for it reveals related question: Why do hotspot generated compiled methods stash eax high up the stack before execution? - also has some answer with a non-working link, which you may attempt finding on archive.org (I have not tried). The push bp+decrement sp thing looks like a part of some standard stack frame setup to me (https://en.m.wikibooks.org/wiki/X86_Disassembly/Functions_and_Stack_Frames ), though the store-sp-into-bp part is missing.

Upvotes: 1

Related Questions