1b1994897Sopenharmony_ci# Compiled Code Metainfo
2b1994897Sopenharmony_ci
3b1994897Sopenharmony_ci## Overview
4b1994897Sopenharmony_ci
5b1994897Sopenharmony_ciMetainfo is an information that aims to provide reg-to-stack mapping for virtual registers. It is needed for stack
6b1994897Sopenharmony_ciunwinding process to restore CFrame at specific PC.
7b1994897Sopenharmony_ci
8b1994897Sopenharmony_ciWhen native code calls runtime or another code that can call runtime, we must provide approach to restore virtual
9b1994897Sopenharmony_ciregisters which are live during this call instruction. Since all virtual regisetrs should be saved on the stack before
10b1994897Sopenharmony_ciwe call the runtime, we can save information in which stack slot specific vreg is live.
11b1994897Sopenharmony_ci
12b1994897Sopenharmony_ciMetainfo is placed together with compiled code:
13b1994897Sopenharmony_ci```
14b1994897Sopenharmony_ci +-------------+
15b1994897Sopenharmony_ci | CodePrefix  |
16b1994897Sopenharmony_ci |             +-------------------+
17b1994897Sopenharmony_ci |             |  magic            |
18b1994897Sopenharmony_ci |             |  code_info_offset |
19b1994897Sopenharmony_ci |             |  code_info_size   |
20b1994897Sopenharmony_ci +-------------+-------------------+
21b1994897Sopenharmony_ci |             | <-- Method::CompiledCodeEntrypoint
22b1994897Sopenharmony_ci | Code        |
23b1994897Sopenharmony_ci |             |
24b1994897Sopenharmony_ci +-------------+-----------------+
25b1994897Sopenharmony_ci | CodeInfo    | CodeInfoHeader  |
26b1994897Sopenharmony_ci |             |-----------------+----------------------+
27b1994897Sopenharmony_ci |             |                 |  StackMap            |
28b1994897Sopenharmony_ci |             |                 |  InlineInfo          |
29b1994897Sopenharmony_ci |             |                 |  Roots Reg Mask      |
30b1994897Sopenharmony_ci |             |                 |  Roots Stack Mask    |
31b1994897Sopenharmony_ci |             |   Bit Tables    |  Method indexes      |
32b1994897Sopenharmony_ci |             |                 |  VRegs mask          |
33b1994897Sopenharmony_ci |             |                 |  VRegs map           |
34b1994897Sopenharmony_ci |             |                 |  VRegs catalogue     |
35b1994897Sopenharmony_ci |             |                 |  Implicit Nullchecks |
36b1994897Sopenharmony_ci |             |                 |  Constants           |
37b1994897Sopenharmony_ci |-------------+-----------------+----------------------+
38b1994897Sopenharmony_ci```
39b1994897Sopenharmony_ci
40b1994897Sopenharmony_ci## Bit table
41b1994897Sopenharmony_ci
42b1994897Sopenharmony_ciColumns width is in a bits.
43b1994897Sopenharmony_ci
44b1994897Sopenharmony_ciFirst row is a BitTable's header, that describe rows count and columns width.
45b1994897Sopenharmony_ci
46b1994897Sopenharmony_ci```
47b1994897Sopenharmony_ci+------------+----------------+----------------+----------------+
48b1994897Sopenharmony_ci| Rows count | Column 0 width |      . . .     | Column N width |
49b1994897Sopenharmony_ci+------------+----------------+----------------+----------------+
50b1994897Sopenharmony_ci```
51b1994897Sopenharmony_ciHeader is followed by data, which is a rows with fixed length. Row length is equal to sum of columns width.
52b1994897Sopenharmony_ci
53b1994897Sopenharmony_ciColumn width can't be greater than 32 bits, because `BitTableBuilder` class, that aims to build bit tables,
54b1994897Sopenharmony_ciuse `uint32_t` for a single table element.
55b1994897Sopenharmony_ci
56b1994897Sopenharmony_ciExample:
57b1994897Sopenharmony_ci```
58b1994897Sopenharmony_ci Rows                           
59b1994897Sopenharmony_ci count         Columns
60b1994897Sopenharmony_ci+-----+-----+-----+-------+-----+
61b1994897Sopenharmony_ci|  5  |  2  |  0  |  15   |  8  |  ; Header
62b1994897Sopenharmony_ci+-----+-----+-----+-------+-----+
63b1994897Sopenharmony_ci      |  2  |  -  | 31547 |  23 |  ; 0 row
64b1994897Sopenharmony_ci      |  1  |  -  |    12 | 241 |  ; 1 row
65b1994897Sopenharmony_ci      |  1  |  -  |   128 |   1 |  ; 2 row
66b1994897Sopenharmony_ci      |  2  |  -  |     0 |  24 |  ; 3 row
67b1994897Sopenharmony_ci      |  0  |  -  |  4587 |   0 |  ; 4 row
68b1994897Sopenharmony_ci      +-----+-----+-------+-----+
69b1994897Sopenharmony_ci```
70b1994897Sopenharmony_ciHere, we have 4 columns and 5 rows. Number of rows is defeined in the first digit in a header,
71b1994897Sopenharmony_cinumber of columns is determined in compile time.
72b1994897Sopenharmony_ci
73b1994897Sopenharmony_ciRow size is 25 bits, that is sum of columns width: 2 + 0 + 15 + 8.
74b1994897Sopenharmony_ciColumn width is determined by maximum value in a table, e.g. for 2th column it is zero row, that has value 31547. This value fits in 15 bits.
75b1994897Sopenharmony_ci
76b1994897Sopenharmony_ciSo, the size of this table's data is 25 * 5 = 125 bits = 15.625 bytes.
77b1994897Sopenharmony_ci
78b1994897Sopenharmony_ci## Bitmap table
79b1994897Sopenharmony_ci
80b1994897Sopenharmony_ciBitmap table is a Bit table with one column, that doesn't have 32-bits limitation for the width.
81b1994897Sopenharmony_ci
82b1994897Sopenharmony_ci## Numbers packing
83b1994897Sopenharmony_ci
84b1994897Sopenharmony_ciFor number compressisng following approach is used.
85b1994897Sopenharmony_ci
86b1994897Sopenharmony_ciThe first four bits determine the variable length of the encoded number:
87b1994897Sopenharmony_ci- Values 0..11 represent the result as-is, with no further following bits.
88b1994897Sopenharmony_ci- Values 12..15 mean the result is in the next 8/16/24/32-bits respectively.
89b1994897Sopenharmony_ci
90b1994897Sopenharmony_ciExample for numbers (2, 0, 15, 254874):
91b1994897Sopenharmony_ci```
92b1994897Sopenharmony_ci+---------+---------+---------+---------+---------+---------+
93b1994897Sopenharmony_ci| 0 byte  | 1 byte  | 2 byte  | 3 byte  | 4 byte  | 5 byte  |
94b1994897Sopenharmony_ci+---------+---------+---------+---------+---------+---------+
95b1994897Sopenharmony_ci|  2 |  0 | 12 | 14 |    15   |            254874           |
96b1994897Sopenharmony_ci+---------+---------+---------+---------+---------+---------+
97b1994897Sopenharmony_ci```
98b1994897Sopenharmony_ci
99b1994897Sopenharmony_ci## Code header
100b1994897Sopenharmony_ci
101b1994897Sopenharmony_ciCode headers describes structure of the compiled code and metainfo.
102b1994897Sopenharmony_ci
103b1994897Sopenharmony_ci| Field | Description |
104b1994897Sopenharmony_ci|-----|----|
105b1994897Sopenharmony_ci| PROPERTIES | Properties of the code info (e.g. frame size) |
106b1994897Sopenharmony_ci| CALLEE_REG_MASK | Specifies registers mask that is saved in the method |
107b1994897Sopenharmony_ci| CALLEE_FP_REG_MASK | Specifies fp registers mask that is saved in the method |
108b1994897Sopenharmony_ci| TABLE_MASK | Bit mask of existing bit tables |
109b1994897Sopenharmony_ci| VREGS_COUNT | Number of virtual registers in reg map |
110b1994897Sopenharmony_ci
111b1994897Sopenharmony_ci## Tables
112b1994897Sopenharmony_ci
113b1994897Sopenharmony_ci### 1. StackMap
114b1994897Sopenharmony_ci
115b1994897Sopenharmony_ci| Field | Description |
116b1994897Sopenharmony_ci|-----|----|
117b1994897Sopenharmony_ci| PROPERTIES | Define properties of the stackmap, currently, it contains only one flag: is_osr |
118b1994897Sopenharmony_ci| NATIVE_PC | Native address to which this stackmap corresponds |
119b1994897Sopenharmony_ci| BYTECODE_PC | Bytecode address to which this stackmap corresponds |
120b1994897Sopenharmony_ci| ROOTS_REG_MASK_INDEX | Mask of the CPU registers that hold managed objects |
121b1994897Sopenharmony_ci| ROOTS_STACK_MASK_INDEX | Mask of the stack slots that hold managed objects |
122b1994897Sopenharmony_ci| INLINE_INFO_INDEX | Inline information for the stackmap |
123b1994897Sopenharmony_ci| VREG_MASK_INDEX | Mask of the virtual registers, that are modified from the last stackmap to the current one |
124b1994897Sopenharmony_ci| VREG_MAP_INDEX | Map of the virtual registers, that are modified from the last stackmap to the current one |
125b1994897Sopenharmony_ci
126b1994897Sopenharmony_ci> NOTE: fields with `_INDEX` in the end of the name contain only index of the record in the corresponding table.
127b1994897Sopenharmony_ci
128b1994897Sopenharmony_ci### 2. InlineInfo
129b1994897Sopenharmony_ci
130b1994897Sopenharmony_ci| Field | Description |
131b1994897Sopenharmony_ci|-----|----|
132b1994897Sopenharmony_ci| IS_LAST | Whether this inlined method is a last, i.e. is it a leaf |
133b1994897Sopenharmony_ci| BYTECODE_PC | Bytecode address to which this inline info corresponds |
134b1994897Sopenharmony_ci| METHOD_ID_INDEX | Index of the method id in the Methods table |
135b1994897Sopenharmony_ci| METHOD_HI | Hi 32-bit part of the method pointer (actual only for jit) |
136b1994897Sopenharmony_ci| METHOD_LOW | Low 32-bit part of the method pointer (actual only for jit)  |
137b1994897Sopenharmony_ci| VREGS_COUNT | Number of virtual registers, that belongs to this inlined method |
138b1994897Sopenharmony_ci
139b1994897Sopenharmony_ciStackmap and all its inlined infos use same Virtual registers map, but separate it via `VREGS_COUNT` field of inline infoes.
140b1994897Sopenharmony_ci
141b1994897Sopenharmony_ciExample for inline chain `method 0` -> `inlines 1` -> `inlines 3`:
142b1994897Sopenharmony_ci```
143b1994897Sopenharmony_ci-------------
144b1994897Sopenharmony_civreg 0 
145b1994897Sopenharmony_civreg 1  ; method 0: CodeInfoHeader::VREGS_COUNT=3
146b1994897Sopenharmony_civreg 2
147b1994897Sopenharmony_ci---------------
148b1994897Sopenharmony_civreg 3  ; method 1: InlineInfo::VREGS_COUNT=1
149b1994897Sopenharmony_ci---------------
150b1994897Sopenharmony_civreg 4
151b1994897Sopenharmony_civreg 5  ; method 3: InlineInfo::VREGS_COUNT=2
152b1994897Sopenharmony_ci```
153b1994897Sopenharmony_ci
154b1994897Sopenharmony_ci### 3. Roots Reg Mask
155b1994897Sopenharmony_ci
156b1994897Sopenharmony_ciThis is a Bitmap table, where column is a bit mask, that determines which CPU register holds a managed object.
157b1994897Sopenharmony_ci
158b1994897Sopenharmony_ci### 4. Roots Stack Mask
159b1994897Sopenharmony_ci
160b1994897Sopenharmony_ciThis is a Bitmap table, where column is a bit mask, that determines which stack slot holds a managed object.
161b1994897Sopenharmony_ci
162b1994897Sopenharmony_ci### 5. Method indexes
163b1994897Sopenharmony_ci
164b1994897Sopenharmony_ciHolds single column - method index within a pandafile. It was moved to the separate table due to better deduplication.
165b1994897Sopenharmony_ci
166b1994897Sopenharmony_ci### 6. VRegs mask
167b1994897Sopenharmony_ci
168b1994897Sopenharmony_ciThis is a Bitmap table, where column is a bit mask, that determines which virtual register is modified in the StackMap.
169b1994897Sopenharmony_ci
170b1994897Sopenharmony_ci### 7. VRegs map
171b1994897Sopenharmony_ci
172b1994897Sopenharmony_ciHolds single column - index of the VReg description in the `VRegs catalogue` table.
173b1994897Sopenharmony_ci
174b1994897Sopenharmony_ci### 8. VRegs catalogue
175b1994897Sopenharmony_ci
176b1994897Sopenharmony_ci| Field | Description |
177b1994897Sopenharmony_ci|-----|----|
178b1994897Sopenharmony_ci| INFO | Virtual register description |
179b1994897Sopenharmony_ci| VALUE | Virtual register value |
180b1994897Sopenharmony_ci
181b1994897Sopenharmony_ciVirtual register description has the following fields:
182b1994897Sopenharmony_ci- **Location** - where vreg is stored: stack slot, CPU register or constant.
183b1994897Sopenharmony_ci- **Type** - type of the value: OBJECT, INT32, INT64, FLOAT32, FLOAT64, BOOL.
184b1994897Sopenharmony_ci- **IsAccumulator** - whethre vreg is accumulator.
185b1994897Sopenharmony_ci- **Index** - index of the virtual register.
186b1994897Sopenharmony_ci
187b1994897Sopenharmony_ciValue of the `VALUE` field depends on the value of `Location` field:
188b1994897Sopenharmony_ci- CPU register: number of CPU register
189b1994897Sopenharmony_ci- stack slot: number of the slot within a frame
190b1994897Sopenharmony_ci- constant: index of a row within `Constants` table
191b1994897Sopenharmony_ci
192b1994897Sopenharmony_ci### 9. Implicit nullchecks
193b1994897Sopenharmony_ci
194b1994897Sopenharmony_ciThis is a table with offsets: removed NullCheck instruction position and corresponding SlowPath position.
195b1994897Sopenharmony_ciThe table helps the signal handler to find the SlowPath address to continue execution after a segmentation fault.
196b1994897Sopenharmony_ci
197b1994897Sopenharmony_ci| Field | Description |
198b1994897Sopenharmony_ci|-----|----|
199b1994897Sopenharmony_ci| INST_NATIVE_PC | NullCheck instruction position. |
200b1994897Sopenharmony_ci| SLOW_PATH_NATIVE_PC | NullCheck SlowPath position. |
201b1994897Sopenharmony_ci
202b1994897Sopenharmony_ci### 10. Constants
203b1994897Sopenharmony_ci
204b1994897Sopenharmony_ciThis table contains only one column - constant value.