1# Interaction of compiled code and the runtime 2 3## Introduction 4 5During execution compiled code and Panda runtime should interact with each other. This document describes the following aspects interation: 6* Runtime structures 7* Calling convention 8* The structure of compiled code stack frames and stack traversing 9* Transition from the interpeter to compiled code and vise versa 10* Calling the runtime 11* Deoptimization 12* Stack unwinding during exception handling 13 14Documentation of meta information generated by the compiler is located in compiled_method_info.md document. 15 16## Panda runtime (the runtime) 17Panda runtime as a set of functions aimed to execute managed code. The runtime consists of several modules. 18The document refers to the interpreter and the compiler modules. 19 20The interpreter is a part of the runtime aimed to execute bytecode of managed functions. The interpreter is responsible to manage 21hotness counter (see [Structure of `panda::Method`](#structure-of-pandamethod)) of managed functions. 22 23The compiler is aimed to translate managed function's bytecode to native code. The compiler has an interface function 24`panda::CompilerInterface::CompileMethodSync` which starts compilation. When the function gets compiled the compiler 25changes its entrypoint to newly generated code. Next time when the function gets called native code will be executed. 26 27## Calling convention 28Panda runtime and managed code must call functions according to the target calling convention. 29Compiled code of a managed function must accept one extra argumnent: the pointer to `panda::Method` which describes this function. 30This argument must be the first argument. 31 32Example: 33Consider a function int max(int a, int b). 34When the compiler generates native code for this function for ARM target it must consider that 35the function accepts 3 arguments: 36- a pointer to `panda::Method` in the register R0. 37- `a` in the register R1 38- `b` in the register R2 39 40The function must return the result in the register R0. 41 42## Structure of `panda::ManagedThread` 43`panda::ManagedThread` has the following fields that compiled code may use: 44 45| Field | Type | Description | 46| --- | ---- | ----------- | 47| sp_flag_ | bool* | Safepoint flag. See *Safepoints* in memory_management.md | 48| pending_exception_ | panda::ObjectHeader* | A pointer to a thrown exception or 0 if there is no exception thrown. | 49| runtime_entrypoints_ | void*[] | A table of runtime entrypoints (See [Runtime entrypoints](#runtime_entrypoints)). | 50| stack_frame_kind_ | StackFrameKind | A kind of the current stack frame (compiled code or interpreter stack frame). | 51 52## Access to `panda::ManagedThread` from compiled code 53There is an allocated register for each target architecture to store a pointer to `panda::ManagedThread`. This register is called `thread register` and 54must contains a valid pointer to `panda::ManagedThread` on entry to each compiled function. 55 56## Runtime entrypoints 57Runtime serves compiled code via runtime entrypoints. A runtime entrypoint is a function which conforms to the target calling convention. 58A table of the entrypoints is located in `panda::ManagedThread::runtime_entrypoints_` which could be accessed via `thread register`. 59 60## Structure of `panda::Method` 61`panda::Method` describes a managed function in the runtime. 62This document refers to the following fields of `panda::Method`: 63 64| Field | Description | 65| ----- | ----------- | 66| hotness_counter_ | A hotness counter of the managed function. | 67| compiled_entry_point_ | Function entrypoint. | 68 69### Hotness counter 70The field `hotness_counter_` reflects hotness of a managed function. The interpreter increments it each time the function gets called, 71backward branch is taken and call instruction is handled. 72When the hotness counter gets saturated (reaches the threshold) the interpreter triggers compilation of the function. 73Panda runtime provides a command line option to tune the hotness counter threshold: `--compiler-hotness-threshold`. 74 75### Entrypoint 76Entrypoint is a pointer to native code which can execute the function. This code must conform to the target calling convention and must accept 77one extra argument: a pointer to `panda::Method` ( See [Calling convention](#calling_convention)). 78The managed function could have compiled code or it could be executed by the interpreter. 79In the case the function has compiled code the `compiled_entry_point_` must point to compiled code. 80In the case the function is executed by the interpreter the `compiled_entry_point_` must point to a runtime function `CompiledCodeToInterpreterBridge` which calls the interpreter. 81 82## Stack frame 83A stack frame contains data necessary to execute the function the frame belongs to. 84The runtime can create several kinds of stack frames. But all the frames of managed code must have the structure described in [Compiled code stack frame](#compiled-code-stack-frame). 85 86### Interpreter stack frame 87Interpreter stack frame is decribed by `panda::Frame` class. The class has fields to store virtual registers and a pointer to the previous stack frame. 88All the consecutive interpreter stack frames are organized into a linked list. The field `panda::Frame::prev_` contains a pointer to the previous interpreter (or compiled bridge) frame. 89 90### Compiled code stack frame 91Each compiled function is responsible to reserve stack frame for its purpose and then release it when the function doesn't need it. 92Generaly compiled function builds the stack frame in prolog and releases it in epilog. If a compiled function doesn't require 93the stack frame it can omit its creation. 94When compiled code is executing the `stack pointer` register must point to a valid stack frame (newly created stack frame of stack frame of caller) and 95`frame pointer` register must point to correct place in the frame before the following operations: 96* Managed objects access 97* Safepoint flag access 98* Call of managed functions or runtime entrypoints 99 100Release of the stack frame could be done by restoring values of `stack pointer` and `frame pointer` registers to the value they have at the moment of function entry. 101 102Compiled code stack frames of caller and callee must be continuous in the stack i.e. the callee's stack frame must immediately follow the caller's stack frame. 103 104A compiled code stack frame must have the following structure: 105 106``` 107(Stack grows in increasing order: higher slot has lower address) 108-----+----------------------+ <- Stack pointer 109 | Callee parameters | 110 +----------------------+ 111 | Spills | 112 +----------------------+ 113 | Caller saved fp regs | 114 D +----------------------+ 115 A | Caller saved regs | 116 T +----------------------+ 117 A | Callee saved fp regs | 118 +----------------------+ 119 | Callee saved regs | 120 +----------------------+ 121 | Locals | 122-----+----------------------+ 123 H | Properties | 124 E +----------------------+ 125 A | panda::Method* | 126 D +----------------------+ <- Frame pointer 127 E | Frame pointer | 128 R +----------------------+ 129 | Return address | 130-----+----------------------+ 131``` 132Stack frame elements: 133- data - arbitraty data necessary for function execution. May be omited. 134- properties - define properties of the frame, f.e. whether it is OSR frame or not. 135- `panda::Method*` - a pointer to `panda::Method` which describes the called function. 136- frame pointer - pointer to the previous frame. The value of `frame pointer` register at the moment of function entry. 137- return address - address to which control will be transfered after the function gets returned. 138 139There are two special registers: `stack pointer` and `frame pointer`. 140`stack pointer` register contains a pointer to the last stack element. 141`frame pointer` register contains a pointer to the place in the stack where the return address is stored. 142 143Panda contains special class for getting cframe layout: `class CFrameLayout`. Everything related to the cframe layout 144should be processed via this class, 145 146## Calling a function from compiled code 147To call a managed function compiled code must resolve it (i.e. retreive a pointer to callee's `panda::Method`), 148prepare arguments in the registers and the stack (if necessary) and jump to callee's entrypoint. 149Resolving of a function could be done by calling the corresponding runtime entrypoint. 150 151Example: 152Calling `int max(int a, int b)` function from compiled code on ARM architecture with arguments `2` and `3` 153could be described by the following pseudocode: 154``` 155// tr - thread register 156// r0 contains a pointer to the current `panda::Method` 157// 1st step: resolve `int max(int, int)` 158mov r1, MAX_INT_INT_ID // MAX_INT_INT_ID - identifier of int max(int, int) function 159ldr lr, [tr, #RESOLVE_RUNTIME_ENTRYPOINT_OFFSET] 160blx lr // call resolve(currentMethod, MAX_INT_INT_ID) 161// r0 contains a pointer to `panada::Method` which describes `int max(int, int)` function. 162// 2nd step: prepare arguments and entrypoint to call `int max(int, int)` 163mov r1, #2 164mov r2, #3 165lr = ldr [r0, #entrypoint_offset] 166// 3rd step: call the function 167blx lr // call max('max_method', 2, 3) 168// r0 contains the function result 169``` 170 171## Calling a function from compiled code: Bridge function 172The Compiler have an entrypoints table. Each entrypoint contains a link to the Bridge Function. 173The Bridge Functions are auto-generated for each runtime function to be called using the macro assembly. 174The Bridge Function sets up the Boundary Frame and performs the call to the actual runtime function. 175 176To do a runtime call from compiled code the Compiler generates: 177* putting callee saved (if need) and param holding (if any) register values to the stack 178* (callee saved regisers goes to Bridge Function stack frame, caller saved registers goes to the current stack frame) 179* parameter holding registers values setup 180* Bridge Function address load and branch intruction 181* register values restore 182 183The bridge function does: 184* setup the Bridge Function stack frame 185* push the caller saved registers (except of registers holding function parameters) to the caller's stack frame 186* adjust Stack Pointer, and pass execution to the runtime function 187* restore the Stack Pointer and caller saved registers 188 189Bridge Function stack frame: 190``` 191--------+------------------------------------------+ 192 | Return address | 193 +------------------------------------------+ 194 HEADER | Frame pointer | 195 +------------------------------------------+ 196 | COMPILED_CODE_TO_INTERPRETER_BRIDGE flag | 197 +------------------------------------------+ 198 | - unused - | 199--------+------------------------------------------+ 200 | | 201 | Callee saved regs | 202 | | 203 DATA +------------------------------------------+ 204 | | 205 | Callee saved fp regs | 206 | | 207--------+------------------------------------------+ 208 + 16-byte alignment pad to the next frame + 209``` 210 211## Transition from the interpreter to compiled code 212When the interpreter handles a call instruction first it should resolve the callee method. 213Depending on the callee's entrypoint there may be different cases. 214If the entrypoint points to `CompiledCodeToInterpreterBridge` then the callee should be executed by the interpreter. In this case the interpreter calls itself directly. 215In other cases the interpreter calls the function `InterpreterToCompiledCodeBridge` passing to it the resolved callee function, 216the call instruction, the interpreter's frame and the pointer to `panda::ManagedThread`. 217 218`InterpreterToCompiledCodeBridge` function does the following: 219* Build a boundary stack frame. 220* Set the pointer to `panda::ManagedThread` to the thread register. 221* Change stack frame kind in `panda::ManagedThread::stack_frame_kind_` to compiled code stack frame. 222* Prepare the arguments according to the target calling convention. The function uses the bytecode instruction (which must be a variant of `call` instruction) 223 and interpreter's frame to retreive the function's arguments. 224* Jump to the callee's entrypoint. 225* After the return save the result to the interpreter stack frame. 226* Change stack frame kind in `panda::ManagedThread::stack_frame_kind_` back to interpreter stack frame. 227* Drop the boundary stack frame. 228 229`InterpreterToCompiledCodeBridge`'s boundary stack frame is necessary to link the interpreter's frame with the compiled code's frame. 230Its structure is depicted below: 231``` 232---- +----------------+ <- stack pointer 233b s | INTERPRETER_ | 234o t | TO_COMPILED_ | 235u a | CODE_BRIDGE | 236n c +----------------+ <- frame pointer 237d k | pointer to the | 238a | interpreter | 239r f | frame | 240y r | | 241 a +----------------+ 242 m | return address | 243 e | | 244---- +----------------+ 245``` 246 247The structure of boundary frame is the same as a stack frame of compiled code. 248Instead of pointer to `panda::Method` the frame contains constant `INTERPRETER_TO_COMPILED_CODE_BRIDGE`. 249Frame pointer points to the previous interpreter frame. 250 251## Transition from compiled code to the interpreter 252If a function should be executed by the interpreter it must have `CompiledCodeToInterpreterBridge` as an entrypoint. 253`CompiledCodeToInterpreterBridge` does the following: 254* Change stack frame kind in `panda::ManagedThread::stack_frame_kind_` to interpreter stack frame. 255* Creates a boundary stack frame which contains room for interpreter frame. 256* Fill in the interpreter frame by the arguments passed to `CompiledCodeToInterpreterBridge` in the registers or via the stack. 257* Call the interpreter. 258* Store the result in registers or in the stack according to the target calling convention. 259* Drop the boundary stack frame. 260* Change stack frame kind in `panda::ManagedThread::stack_frame_kind_` back to compiled code stack frame. 261 262`CompiledCodeToInterpreterBridge`'s boundary stack frame is necessary to link the compiled code's frame with the interpreter's frame. 263Its structure is depicted below: 264``` 265---- +----------------+ <-+ stack pointer 266 s | interpreter's | -+ `panda::Frame::prev_` 267b t | frame | | 268o a +----------------+ <+ frame pointer 269u c | frame pointer | 270n k +----------------+ 271d | COMPILED_CODE_ | 272a f | TO_ | 273r r | INTERPRETER_ | 274y a | BRIDGE | 275 m +----------------+ 276 e | return address | 277---- +----------------+ 278 | ... | 279``` 280 281The structure of boundary frame is the same as a stack frame of compiled code. 282Instead of a pointer to `panda::Method` the frame contains constant `COMPILED_CODE_TO_INTERPRETER_BRIDGE`. 283Frame pointer points to the previous frame in compiled code stack frame. 284The field `panda::Frame::prev_` must point to the boundary frame pointer. 285 286## Stack traversing 287Stack traversing is performed by the runtime. When the runtime examinates a managed thread's stack the thread mustn't execute any managed code. 288Stack unwinding always starts from the top frame. Its kind could be determined from `panda::ManagedThread::stak_frame_kind_` field. A pointer 289to the top frame could be determined depends on the kind of the top stack frame: 290* The top stack frame is an interpreter stack frame. Address of the interpreter's frame could be retrieved from `panda::ManagedThread::GetCurrentFrame()`. 291* The top stack frame is a compiled code stack frame. `frame pointer` register contains the address of the top stack frame. 292 293Having a pointer to the top stack frame, its kind and structure the runtime can move to the next frame. 294Moving to the next frame is done according to the table below: 295 296| Kind of the current stack frame | How to get a pointer to the next stack frame | Kind of the previous stack frame | 297| ------------------------------- | -------------------------------------------- | -------------------------------- | 298| Interpreter stack frame | Read `panda::Frame::prev_` field | Interpreter stack frame or COMPILED_CODE_TO_INTERPRETER boundary frame | 299| INTERPRETER_TO_COMPILED_CODE_BRIDGE boundary stack frame | Read `pointer to the interpreter frame` from the stack | Interpreter stack frame | 300| COMPILED_CODE_TO_INTERPRETER_BRIDGE boundary stack frame | Read `frame pointer` from the stack | Compiled code stack frame | 301| Compiled code stack frame | Read `frame pointer` | Compiled code stack frame or INTERPRETER_TO_COMPILED_CODE_BRIDGE boundary frame| 302 303Thus the runtime can traverse all the managed stack frames moving from one frame to the previous frame and changing frame type 304crossing the boundary frames. 305 306Unwinding of stack frames has specifics. 307* Compiled code could be combined from several managed functions (inlined functions). If the runtime needs to get information about inlined functions 308during handling a compiled code stack frame it uses meta information generated by the compiler (See compiled_method_info.md). 309* Compiled code may save any callee-saved registers on the stack. Before moving to the next stack frame the runtime must restore values of these registers. 310To do that the runtime uses information about callee-saved registers stored on the stack. This information is generated by the compiler (See compiled_method_info.md). 311* Values of virtual registers could be changed during stack unwinding. For example, when GC moves an object, it must update all the references to the object. 312The runtime should provide an internal API for changing values of virtual registers. 313 314Example: 315Consider the following call sequence: 316``` 317 calls calls 318 foo --------> bar ------> baz 319(interpreted) (compiled) (interpreted) 320``` 321Functions `foo` and `baz` are executed by the interpreter and the function `bar` has compiled code. 322In this situation the stack might look as follow: 323``` 324---- +----------------+ <- stack pointer 325E | native frame | 326x u | of | 327e t | interpreter | 328c e | | 329---- +----------------+ <--- `panda::ManagedThread::GetCurrentFrame()` 330b | baz's | -+ 331o s | interperer | | 332u t | stack frame | | 333n a +----------------+<-+ 334d c | frame pointer | -+ 335a k +----------------+ | 336r | COMPILED_CODE_ | | 337y f | TO_ | | 338 r | INTERPRETER_ | | 339 a | BRIDGE | | 340 m +----------------+ | 341 e | return address | | 342---- +----------------+ | 343 | data | | 344 +----------------+ | 345 b | panda::Method* | | 346 a +----------------+ <+ 347 r | frame pointer | -+ 348 +----------------+ | 349 | return address | | 350---- +----------------+ | 351b s | INTERPRETER_ | | 352o t | TO_COMPILED_ | | 353u a | CODE_BRIDGE | | 354n c +----------------+ <+ 355d k | pointer to the | -+ 356a | interpreter | | 357r f | frame | | 358y r | | | 359 a +----------------+ | 360 m | return address | | 361 e | | | 362---- +----------------+ | 363E | native frame | | 364x u | of | | 365e t | interpreter | | 366c e | | | 367---- +----------------+ | 368 | ... | | 369 +----------------+ <+ 370 | foo's | 371 | interpreter | 372 | frame | 373 +----------------+ 374 | ... | 375``` 376 377The runtime determines kind of the top stack frame by reading `panda::ManagedThread::stack_frame_kind_` (the top stack frame kind must be interpreter stack frame). 378`panda::ManagedThread::GetCurrentFrame()` method must return the pointer to `baz`'s interpreter stack frame. 379To go to the previous frame the runtime reads the field `panda::Frame::prev_` which must point to `COMPILED_CODE_TO_INTERPRETER_BRIDGE` boundary stack frame. 380It means that to get `bar`'s stack frame the runtime must read `frame pointer` and the kind of the next frame will be compiled code's frame. 381At this step the runtime has a pointer to `bar`'s compiled code stack frame. To go to the next frame runtime reads `frame pointer` again and gets 382`INTERPRETER_TO_COMPILED_CODE_BRIDGE` boundary stack frame. To reach `foo`'s interpreter stack frame the runtime reads `pointer to the interpreter's frame` field. 383 384## Deoptimization 385There is may be a situation when compiled code cannot continue execution for some reason. 386For such cases compiled code must call `void Deoptimize()` runtime entrypoint to continue execution of the method in the interpreter from the point where compiled code gets stopped. 387The function reconstructs the interpreter stack frame and calls the interpreter. 388When compiled code is combined from several managed functions (inlined functions) `Deoptimize` reconstructs interpreter stack frame and calls the interpreter for each inlined function too. 389 390Details in [deoptimization documentation](deoptimization.md) 391## Throwing an exception 392Throwing an exeption from compiled code is performed by calling a runtime entrypoint `void ThrowException(panda::ObjectHeader* exception)`. 393The function `ThrowException` does the following: 394* Saves all the callee-saved registers to the stack 395* Stores the pointer to the exception object to `panda::ManagedThread::pending_exception_` 396* Unwind compiled code stack frames to find the corresponding exception handler by going from one stack frame to the previous and making checks. 397 398If the corresponding catch handler is found in the current stack frame the runtime jumps to the handler. 399 400If a INTERPRETER_TO_COMPILED_CODE_BRIDGE boundary stack frame is reached the runtime returns to the interpreter letting it to handle the exception. 401Returning to the interpreter is performed as follow: 4021. Determine the return address to the boundary frame. The return address is stored in the following compiled code stack frame. 4032. Set the pointer to the boundary frame into stack pointer, assign the return address determined at the previous step to program counter. 404 405If there is no catch handler in the current frame then the runtime restores values of callee-saved registers and moves to the previous stack frame. 406 407Details of stack travesing are described in [Stack traversing](#stack_traversing) 408 409Finding a catch handler in a compiled code stack frame is performed according meta information generated by the compiler (See compiled_method_info.md). 410 411The interpreter must ignore the returned value if `panda::ManagedThread::pending_exception_` is not 0. 412 413