address InterpreterGenerator::generate_normal_entry(bool synchronized) { inc_counter = UseCompiler || CountCompiledCalls; // ebx: methodOop // r13: sender sp address entry_point = __ pc(); const Address size_of_parameters(rbx, methodOopDesc::size_of_parameters_offset()); const Address size_of_locals(rbx, methodOopDesc::size_of_locals_offset()); const Address invocation_counter(rbx, methodOopDesc::invocation_counter_offset() + InvocationCounter::counter_offset()); const Address access_flags(rbx, methodOopDesc::access_flags_offset()); // get parameter size (always needed) __ load_unsigned_short(rcx, size_of_parameters);
其中methodOop指针被保存在rbx中,调用Java方法的sender sp被保存在r13中,参数大小保存在rcx中
(2).获取局部变量区的大小,保存在rdx中,并减去参数数量,将除参数以外的局部变量数量保存在rdx中(虽然参数作为局部变量是方法的一部分,但参数由调用者提供,这些参数应有调用者栈帧而非被调用者栈帧维护,即被调用者栈帧只需要维护局部变量中除了参数的部分即可)
// rbx: methodOop // rcx: size of parameters // r13: sender_sp (could differ from sp+wordSize if we were called via c2i ) __ load_unsigned_short(rdx, size_of_locals); // get size of locals in words __ subl(rdx, rcx); // rdx = no. of additional locals
(3).对栈空间大小进行检查,判断是否会发生栈溢出
// see if we've got enough room on the stack for locals plus overhead. generate_stack_overflow_check();
(4).获取返回地址,保存在rax中(注意此时栈顶为调用函数call指令后下一条指令的地址)
// get return address __ pop(rax);
(5).由于参数在栈中由低地址向高地址是以相反的顺序存放的,所以第一个参数的地址应该是 rsp+rcx*8-8(第一个参数地址范围为 rsp+rcx*8-8 ~ rsp+rcx*8),将其保存在r14中
// compute beginning of parameters (r14) __ lea(r14, Address(rsp, rcx, Address::times_8, -wordSize))
(6).为除参数以外的局部变量分配栈空间,若这些局部变量数量为0,那么就跳过这一部分处理,否则,将压入 maxlocals - param_size个0,以初始化这些局部变量
//该部分为一个loop循环 // rdx - # of additional locals // allocate space for locals // explicitly initialize locals { Label exit, loop; __ testl(rdx, rdx); __ jcc(Assembler::lessEqual, exit); // do nothing if rdx <= 0 __ bind(loop); __ push((int) NULL_WORD); // initialize local variables __ decrementl(rdx); // until everything initialized __ jcc(Assembler::greater, loop); __ bind(exit); }
这时栈的层次如下:
(7).将方法的调用次数保存在rcx/ecx中
(inc_counter) { __ movl(rcx, invocation_counter); }
(8).初始化当前方法的栈帧
// initialize fixed part of activation frame generate_fixed_frame(false);
generate_fixed_frame()的实现如下:
__ push(rax); // save return address __ enter(); // save old & set new rbp __ push(r13); // set sender sp __ push((int)NULL_WORD); // leave last_sp as null __ movptr(r13, Address(rbx, methodOopDesc::const_offset())); // get constMethodOop __ lea(r13, Address(r13, constMethodOopDesc::codes_offset())); // get codebase __ push(rbx);
保存返回地址,为被调用的Java方法准备栈帧,并将sender sp指针、last_sp(设置为0)压入栈,根据methodOop的constMethodOop成员将字节码指针保存到r13寄存器中,并将methodOop压入栈
} else { __ push(0); //methodData } __ movptr(rdx, Address(rbx, methodOopDesc::constants_offset())); __ movptr(rdx, Address(rdx, constantPoolOopDesc::cache_offset_in_bytes())); __ push(rdx); // set constant pool cache __ push(r14); (native_call) { __ push(0); // no bcp } else { __ push(r13); // set bcp } __ push(0); // reserve word for pointer to expression stack bottom __ movptr(Address(rsp, 0), rsp); // set expression stack bottom }