/src/cpython/Python/assemble.c
Line | Count | Source |
1 | | #include "Python.h" |
2 | | #include "pycore_code.h" // write_location_entry_start() |
3 | | #include "pycore_compile.h" |
4 | | #include "pycore_instruction_sequence.h" |
5 | | #include "pycore_opcode_utils.h" // IS_BACKWARDS_JUMP_OPCODE |
6 | | #include "pycore_opcode_metadata.h" // is_pseudo_target, _PyOpcode_Caches |
7 | | #include "pycore_symtable.h" // _Py_SourceLocation |
8 | | |
9 | | #include <stdbool.h> |
10 | | |
11 | 4.26k | #define DEFAULT_CODE_SIZE 128 |
12 | 4.26k | #define DEFAULT_LNOTAB_SIZE 16 |
13 | 4.26k | #define DEFAULT_CNOTAB_SIZE 32 |
14 | | |
15 | | #undef SUCCESS |
16 | | #undef ERROR |
17 | 308k | #define SUCCESS 0 |
18 | 4.26k | #define ERROR -1 |
19 | | |
20 | | #define RETURN_IF_ERROR(X) \ |
21 | 319k | if ((X) < 0) { \ |
22 | 0 | return ERROR; \ |
23 | 0 | } |
24 | | |
25 | | typedef _Py_SourceLocation location; |
26 | | typedef _PyInstruction instruction; |
27 | | typedef _PyInstructionSequence instr_sequence; |
28 | | |
29 | | static inline bool |
30 | | same_location(location a, location b) |
31 | 307k | { |
32 | 307k | return a.lineno == b.lineno && |
33 | 111k | a.end_lineno == b.end_lineno && |
34 | 108k | a.col_offset == b.col_offset && |
35 | 65.2k | a.end_col_offset == b.end_col_offset; |
36 | 307k | } |
37 | | |
38 | | static int |
39 | | instr_size(instruction *instr) |
40 | 813k | { |
41 | 813k | int opcode = instr->i_opcode; |
42 | 813k | int oparg = instr->i_oparg; |
43 | 813k | assert(!IS_PSEUDO_INSTR(opcode)); |
44 | 813k | assert(OPCODE_HAS_ARG(opcode) || oparg == 0); |
45 | 813k | int extended_args = (0xFFFFFF < oparg) + (0xFFFF < oparg) + (0xFF < oparg); |
46 | 813k | int caches = _PyOpcode_Caches[opcode]; |
47 | 813k | return extended_args + 1 + caches; |
48 | 813k | } |
49 | | |
50 | | struct assembler { |
51 | | PyBytesWriter *a_bytecode_writer; /* writer containing bytecode */ |
52 | | PyObject *a_bytecode; /* bytes containing bytecode */ |
53 | | int a_offset; /* offset into bytecode */ |
54 | | PyBytesWriter *a_except_table_writer; /* writer containing exception table */ |
55 | | PyObject *a_except_table; /* bytes containing exception table */ |
56 | | int a_except_table_off; /* offset into exception table */ |
57 | | /* Location Info */ |
58 | | int a_lineno; /* lineno of last emitted instruction */ |
59 | | PyBytesWriter *a_linetable_writer; /* writer containing location info */ |
60 | | PyObject *a_linetable; /* bytes object containing location info */ |
61 | | int a_location_off; /* offset of last written location info frame */ |
62 | | }; |
63 | | |
64 | | static int |
65 | | assemble_init(struct assembler *a, int firstlineno) |
66 | 4.26k | { |
67 | 4.26k | memset(a, 0, sizeof(struct assembler)); |
68 | 4.26k | a->a_lineno = firstlineno; |
69 | 4.26k | a->a_bytecode_writer = PyBytesWriter_Create(DEFAULT_CODE_SIZE); |
70 | 4.26k | if (a->a_bytecode_writer == NULL) { |
71 | 0 | goto error; |
72 | 0 | } |
73 | 4.26k | a->a_linetable_writer = PyBytesWriter_Create(DEFAULT_CNOTAB_SIZE); |
74 | 4.26k | if (a->a_linetable_writer == NULL) { |
75 | 0 | goto error; |
76 | 0 | } |
77 | 4.26k | a->a_except_table_writer = PyBytesWriter_Create(DEFAULT_LNOTAB_SIZE); |
78 | 4.26k | if (a->a_except_table_writer == NULL) { |
79 | 0 | goto error; |
80 | 0 | } |
81 | 4.26k | return SUCCESS; |
82 | 0 | error: |
83 | 0 | PyBytesWriter_Discard(a->a_bytecode_writer); |
84 | 0 | a->a_bytecode_writer = NULL; |
85 | 0 | PyBytesWriter_Discard(a->a_linetable_writer); |
86 | 0 | a->a_linetable_writer = NULL; |
87 | 0 | PyBytesWriter_Discard(a->a_except_table_writer); |
88 | 0 | a->a_except_table_writer = NULL; |
89 | 0 | return ERROR; |
90 | 4.26k | } |
91 | | |
92 | | static void |
93 | | assemble_free(struct assembler *a) |
94 | 4.26k | { |
95 | 4.26k | PyBytesWriter_Discard(a->a_bytecode_writer); |
96 | 4.26k | PyBytesWriter_Discard(a->a_linetable_writer); |
97 | 4.26k | PyBytesWriter_Discard(a->a_except_table_writer); |
98 | 4.26k | Py_XDECREF(a->a_bytecode); |
99 | 4.26k | Py_XDECREF(a->a_linetable); |
100 | 4.26k | Py_XDECREF(a->a_except_table); |
101 | 4.26k | } |
102 | | |
103 | | static inline void |
104 | 10.5k | write_except_byte(struct assembler *a, int byte) { |
105 | 10.5k | unsigned char *p = PyBytesWriter_GetData(a->a_except_table_writer); |
106 | 10.5k | p[a->a_except_table_off++] = byte; |
107 | 10.5k | } |
108 | | |
109 | 3.09k | #define CONTINUATION_BIT 64 |
110 | | |
111 | | static void |
112 | | assemble_emit_exception_table_item(struct assembler *a, int value, int msb) |
113 | 7.44k | { |
114 | 7.44k | assert ((msb | 128) == 128); |
115 | 7.44k | assert(value >= 0 && value < (1 << 30)); |
116 | 7.44k | if (value >= 1 << 24) { |
117 | 0 | write_except_byte(a, (value >> 24) | CONTINUATION_BIT | msb); |
118 | 0 | msb = 0; |
119 | 0 | } |
120 | 7.44k | if (value >= 1 << 18) { |
121 | 0 | write_except_byte(a, ((value >> 18)&0x3f) | CONTINUATION_BIT | msb); |
122 | 0 | msb = 0; |
123 | 0 | } |
124 | 7.44k | if (value >= 1 << 12) { |
125 | 0 | write_except_byte(a, ((value >> 12)&0x3f) | CONTINUATION_BIT | msb); |
126 | 0 | msb = 0; |
127 | 0 | } |
128 | 7.44k | if (value >= 1 << 6) { |
129 | 3.09k | write_except_byte(a, ((value >> 6)&0x3f) | CONTINUATION_BIT | msb); |
130 | 3.09k | msb = 0; |
131 | 3.09k | } |
132 | 7.44k | write_except_byte(a, (value&0x3f) | msb); |
133 | 7.44k | } |
134 | | |
135 | | /* See InternalDocs/exception_handling.md for details of layout */ |
136 | 1.86k | #define MAX_SIZE_OF_ENTRY 20 |
137 | | |
138 | | static int |
139 | | assemble_emit_exception_table_entry(struct assembler *a, int start, int end, |
140 | | int handler_offset, |
141 | | _PyExceptHandlerInfo *handler) |
142 | 1.86k | { |
143 | 1.86k | Py_ssize_t len = PyBytesWriter_GetSize(a->a_except_table_writer); |
144 | 1.86k | if (a->a_except_table_off + MAX_SIZE_OF_ENTRY >= len) { |
145 | 589 | RETURN_IF_ERROR(PyBytesWriter_Resize(a->a_except_table_writer, len * 2)); |
146 | 589 | } |
147 | 1.86k | int size = end-start; |
148 | 1.86k | assert(end > start); |
149 | 1.86k | int target = handler_offset; |
150 | 1.86k | int depth = handler->h_startdepth - 1; |
151 | 1.86k | if (handler->h_preserve_lasti > 0) { |
152 | 1.37k | depth -= 1; |
153 | 1.37k | } |
154 | 1.86k | assert(depth >= 0); |
155 | 1.86k | int depth_lasti = (depth<<1) | handler->h_preserve_lasti; |
156 | 1.86k | assemble_emit_exception_table_item(a, start, (1<<7)); |
157 | 1.86k | assemble_emit_exception_table_item(a, size, 0); |
158 | 1.86k | assemble_emit_exception_table_item(a, target, 0); |
159 | 1.86k | assemble_emit_exception_table_item(a, depth_lasti, 0); |
160 | 1.86k | return SUCCESS; |
161 | 1.86k | } |
162 | | |
163 | | static int |
164 | | assemble_exception_table(struct assembler *a, instr_sequence *instrs) |
165 | 4.26k | { |
166 | 4.26k | int ioffset = 0; |
167 | 4.26k | _PyExceptHandlerInfo handler; |
168 | 4.26k | handler.h_label = -1; |
169 | 4.26k | handler.h_startdepth = -1; |
170 | 4.26k | handler.h_preserve_lasti = -1; |
171 | 4.26k | int start = -1; |
172 | 158k | for (int i = 0; i < instrs->s_used; i++) { |
173 | 153k | instruction *instr = &instrs->s_instrs[i]; |
174 | 153k | if (instr->i_except_handler_info.h_label != handler.h_label) { |
175 | 3.34k | if (handler.h_label >= 0) { |
176 | 1.86k | int handler_offset = instrs->s_instrs[handler.h_label].i_offset; |
177 | 1.86k | RETURN_IF_ERROR( |
178 | 1.86k | assemble_emit_exception_table_entry(a, start, ioffset, |
179 | 1.86k | handler_offset, |
180 | 1.86k | &handler)); |
181 | 1.86k | } |
182 | 3.34k | start = ioffset; |
183 | 3.34k | handler = instr->i_except_handler_info; |
184 | 3.34k | } |
185 | 153k | ioffset += instr_size(instr); |
186 | 153k | } |
187 | 4.26k | if (handler.h_label >= 0) { |
188 | 0 | int handler_offset = instrs->s_instrs[handler.h_label].i_offset; |
189 | 0 | RETURN_IF_ERROR(assemble_emit_exception_table_entry(a, start, ioffset, |
190 | 0 | handler_offset, |
191 | 0 | &handler)); |
192 | 0 | } |
193 | 4.26k | return SUCCESS; |
194 | 4.26k | } |
195 | | |
196 | | |
197 | | /* Code location emitting code. See locations.md for a description of the format. */ |
198 | | |
199 | | #define MSB 0x80 |
200 | | |
201 | | static void |
202 | | write_location_byte(struct assembler* a, int val) |
203 | 117k | { |
204 | 117k | uint8_t *linetable = PyBytesWriter_GetData(a->a_linetable_writer); |
205 | 117k | linetable[a->a_location_off] = val & 255; |
206 | 117k | a->a_location_off++; |
207 | 117k | } |
208 | | |
209 | | |
210 | | static uint8_t * |
211 | | location_pointer(struct assembler* a) |
212 | 247k | { |
213 | 247k | uint8_t *linetable = PyBytesWriter_GetData(a->a_linetable_writer); |
214 | 247k | return linetable + a->a_location_off; |
215 | 247k | } |
216 | | |
217 | | static void |
218 | | write_location_first_byte(struct assembler* a, int code, int length) |
219 | 115k | { |
220 | 115k | a->a_location_off += write_location_entry_start( |
221 | 115k | location_pointer(a), code, length); |
222 | 115k | } |
223 | | |
224 | | static void |
225 | | write_location_varint(struct assembler* a, unsigned int val) |
226 | 99.2k | { |
227 | 99.2k | uint8_t *ptr = location_pointer(a); |
228 | 99.2k | a->a_location_off += write_varint(ptr, val); |
229 | 99.2k | } |
230 | | |
231 | | |
232 | | static void |
233 | | write_location_signed_varint(struct assembler* a, int val) |
234 | 33.1k | { |
235 | 33.1k | uint8_t *ptr = location_pointer(a); |
236 | 33.1k | a->a_location_off += write_signed_varint(ptr, val); |
237 | 33.1k | } |
238 | | |
239 | | static void |
240 | | write_location_info_short_form(struct assembler* a, int length, int column, int end_column) |
241 | 43.3k | { |
242 | 43.3k | assert(length > 0 && length <= 8); |
243 | 43.3k | int column_low_bits = column & 7; |
244 | 43.3k | int column_group = column >> 3; |
245 | 43.3k | assert(column < 80); |
246 | 43.3k | assert(end_column >= column); |
247 | 43.3k | assert(end_column - column < 16); |
248 | 43.3k | write_location_first_byte(a, PY_CODE_LOCATION_INFO_SHORT0 + column_group, length); |
249 | 43.3k | write_location_byte(a, (column_low_bits << 4) | (end_column - column)); |
250 | 43.3k | } |
251 | | |
252 | | static void |
253 | | write_location_info_oneline_form(struct assembler* a, int length, int line_delta, int column, int end_column) |
254 | 37.0k | { |
255 | 37.0k | assert(length > 0 && length <= 8); |
256 | 37.0k | assert(line_delta >= 0 && line_delta < 3); |
257 | 37.0k | assert(column < 128); |
258 | 37.0k | assert(end_column < 128); |
259 | 37.0k | write_location_first_byte(a, PY_CODE_LOCATION_INFO_ONE_LINE0 + line_delta, length); |
260 | 37.0k | write_location_byte(a, column); |
261 | 37.0k | write_location_byte(a, end_column); |
262 | 37.0k | } |
263 | | |
264 | | static void |
265 | | write_location_info_long_form(struct assembler* a, location loc, int length) |
266 | 33.0k | { |
267 | 33.0k | assert(length > 0 && length <= 8); |
268 | 33.0k | write_location_first_byte(a, PY_CODE_LOCATION_INFO_LONG, length); |
269 | 33.0k | write_location_signed_varint(a, loc.lineno - a->a_lineno); |
270 | 33.0k | assert(loc.end_lineno >= loc.lineno); |
271 | 33.0k | write_location_varint(a, loc.end_lineno - loc.lineno); |
272 | 33.0k | write_location_varint(a, loc.col_offset + 1); |
273 | 33.0k | write_location_varint(a, loc.end_col_offset + 1); |
274 | 33.0k | } |
275 | | |
276 | | static void |
277 | | write_location_info_none(struct assembler* a, int length) |
278 | 1.87k | { |
279 | 1.87k | write_location_first_byte(a, PY_CODE_LOCATION_INFO_NONE, length); |
280 | 1.87k | } |
281 | | |
282 | | static void |
283 | | write_location_info_no_column(struct assembler* a, int length, int line_delta) |
284 | 94 | { |
285 | 94 | write_location_first_byte(a, PY_CODE_LOCATION_INFO_NO_COLUMNS, length); |
286 | 94 | write_location_signed_varint(a, line_delta); |
287 | 94 | } |
288 | | |
289 | 115k | #define THEORETICAL_MAX_ENTRY_SIZE 25 /* 1 + 6 + 6 + 6 + 6 */ |
290 | | |
291 | | |
292 | | static int |
293 | | write_location_info_entry(struct assembler* a, location loc, int isize) |
294 | 115k | { |
295 | 115k | Py_ssize_t len = PyBytesWriter_GetSize(a->a_linetable_writer); |
296 | 115k | if (a->a_location_off + THEORETICAL_MAX_ENTRY_SIZE >= len) { |
297 | 6.42k | assert(len > THEORETICAL_MAX_ENTRY_SIZE); |
298 | 6.42k | RETURN_IF_ERROR(PyBytesWriter_Resize(a->a_linetable_writer, len * 2)); |
299 | 6.42k | } |
300 | 115k | if (loc.lineno == NO_LOCATION.lineno) { |
301 | 1.87k | write_location_info_none(a, isize); |
302 | 1.87k | return SUCCESS; |
303 | 1.87k | } |
304 | 113k | int line_delta = loc.lineno - a->a_lineno; |
305 | 113k | int column = loc.col_offset; |
306 | 113k | int end_column = loc.end_col_offset; |
307 | 113k | if (column < 0 || end_column < 0) { |
308 | 94 | if (loc.end_lineno == loc.lineno || loc.end_lineno < 0) { |
309 | 94 | write_location_info_no_column(a, isize, line_delta); |
310 | 94 | a->a_lineno = loc.lineno; |
311 | 94 | return SUCCESS; |
312 | 94 | } |
313 | 94 | } |
314 | 113k | else if (loc.end_lineno == loc.lineno) { |
315 | 97.2k | if (line_delta == 0 && column < 80 && end_column - column < 16 && end_column >= column) { |
316 | 43.3k | write_location_info_short_form(a, isize, column, end_column); |
317 | 43.3k | return SUCCESS; |
318 | 43.3k | } |
319 | 53.8k | if (line_delta >= 0 && line_delta < 3 && column < 128 && end_column < 128) { |
320 | 37.0k | write_location_info_oneline_form(a, isize, line_delta, column, end_column); |
321 | 37.0k | a->a_lineno = loc.lineno; |
322 | 37.0k | return SUCCESS; |
323 | 37.0k | } |
324 | 53.8k | } |
325 | 33.0k | write_location_info_long_form(a, loc, isize); |
326 | 33.0k | a->a_lineno = loc.lineno; |
327 | 33.0k | return SUCCESS; |
328 | 113k | } |
329 | | |
330 | | static int |
331 | | assemble_emit_location(struct assembler* a, location loc, int isize) |
332 | 105k | { |
333 | 105k | if (isize == 0) { |
334 | 3.03k | return SUCCESS; |
335 | 3.03k | } |
336 | 115k | while (isize > 8) { |
337 | 12.8k | RETURN_IF_ERROR(write_location_info_entry(a, loc, 8)); |
338 | 12.8k | isize -= 8; |
339 | 12.8k | } |
340 | 102k | return write_location_info_entry(a, loc, isize); |
341 | 102k | } |
342 | | |
343 | | static int |
344 | | assemble_location_info(struct assembler *a, instr_sequence *instrs, |
345 | | int firstlineno) |
346 | 4.26k | { |
347 | 4.26k | a->a_lineno = firstlineno; |
348 | 4.26k | location loc = NO_LOCATION; |
349 | 158k | for (int i = instrs->s_used-1; i >= 0; i--) { |
350 | 153k | instruction *instr = &instrs->s_instrs[i]; |
351 | 153k | if (same_location(instr->i_loc, NEXT_LOCATION)) { |
352 | 0 | if (IS_TERMINATOR_OPCODE(instr->i_opcode)) { |
353 | 0 | instr->i_loc = NO_LOCATION; |
354 | 0 | } |
355 | 0 | else { |
356 | 0 | assert(i < instrs->s_used-1); |
357 | 0 | instr->i_loc = instr[1].i_loc; |
358 | 0 | } |
359 | 0 | } |
360 | 153k | } |
361 | 4.26k | int size = 0; |
362 | 158k | for (int i = 0; i < instrs->s_used; i++) { |
363 | 153k | instruction *instr = &instrs->s_instrs[i]; |
364 | 153k | if (!same_location(loc, instr->i_loc)) { |
365 | 101k | RETURN_IF_ERROR(assemble_emit_location(a, loc, size)); |
366 | 101k | loc = instr->i_loc; |
367 | 101k | size = 0; |
368 | 101k | } |
369 | 153k | size += instr_size(instr); |
370 | 153k | } |
371 | 4.26k | RETURN_IF_ERROR(assemble_emit_location(a, loc, size)); |
372 | 4.26k | return SUCCESS; |
373 | 4.26k | } |
374 | | |
375 | | static void |
376 | | write_instr(_Py_CODEUNIT *codestr, instruction *instr, int ilen) |
377 | 153k | { |
378 | 153k | int opcode = instr->i_opcode; |
379 | 153k | assert(!IS_PSEUDO_INSTR(opcode)); |
380 | 153k | int oparg = instr->i_oparg; |
381 | 153k | assert(OPCODE_HAS_ARG(opcode) || oparg == 0); |
382 | 153k | int caches = _PyOpcode_Caches[opcode]; |
383 | 153k | switch (ilen - caches) { |
384 | 0 | case 4: |
385 | 0 | codestr->op.code = EXTENDED_ARG; |
386 | 0 | codestr->op.arg = (oparg >> 24) & 0xFF; |
387 | 0 | codestr++; |
388 | 0 | _Py_FALLTHROUGH; |
389 | 0 | case 3: |
390 | 0 | codestr->op.code = EXTENDED_ARG; |
391 | 0 | codestr->op.arg = (oparg >> 16) & 0xFF; |
392 | 0 | codestr++; |
393 | 0 | _Py_FALLTHROUGH; |
394 | 275 | case 2: |
395 | 275 | codestr->op.code = EXTENDED_ARG; |
396 | 275 | codestr->op.arg = (oparg >> 8) & 0xFF; |
397 | 275 | codestr++; |
398 | 275 | _Py_FALLTHROUGH; |
399 | 153k | case 1: |
400 | 153k | codestr->op.code = opcode; |
401 | 153k | codestr->op.arg = oparg & 0xFF; |
402 | 153k | codestr++; |
403 | 153k | break; |
404 | 0 | default: |
405 | 0 | Py_UNREACHABLE(); |
406 | 153k | } |
407 | 320k | while (caches--) { |
408 | 167k | codestr->op.code = CACHE; |
409 | 167k | codestr->op.arg = 0; |
410 | 167k | codestr++; |
411 | 167k | } |
412 | 153k | } |
413 | | |
414 | | /* assemble_emit_instr() |
415 | | Extend the bytecode with a new instruction. |
416 | | Update lnotab if necessary. |
417 | | */ |
418 | | |
419 | | static int |
420 | | assemble_emit_instr(struct assembler *a, instruction *instr) |
421 | 153k | { |
422 | 153k | Py_ssize_t len = PyBytesWriter_GetSize(a->a_bytecode_writer); |
423 | 153k | _Py_CODEUNIT *code; |
424 | | |
425 | 153k | int size = instr_size(instr); |
426 | 153k | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
427 | 2.17k | if (len > PY_SSIZE_T_MAX / 2) { |
428 | 0 | PyErr_NoMemory(); |
429 | 0 | return ERROR; |
430 | 0 | } |
431 | 2.17k | RETURN_IF_ERROR(PyBytesWriter_Resize(a->a_bytecode_writer, len * 2)); |
432 | 2.17k | } |
433 | 153k | code = (_Py_CODEUNIT *)PyBytesWriter_GetData(a->a_bytecode_writer) + a->a_offset; |
434 | 153k | a->a_offset += size; |
435 | 153k | write_instr(code, instr, size); |
436 | 153k | return SUCCESS; |
437 | 153k | } |
438 | | |
439 | | static int |
440 | | assemble_emit(struct assembler *a, instr_sequence *instrs, |
441 | | int first_lineno, PyObject *const_cache) |
442 | 4.26k | { |
443 | 4.26k | RETURN_IF_ERROR(assemble_init(a, first_lineno)); |
444 | | |
445 | 158k | for (int i = 0; i < instrs->s_used; i++) { |
446 | 153k | instruction *instr = &instrs->s_instrs[i]; |
447 | 153k | RETURN_IF_ERROR(assemble_emit_instr(a, instr)); |
448 | 153k | } |
449 | | |
450 | 4.26k | RETURN_IF_ERROR(assemble_location_info(a, instrs, a->a_lineno)); |
451 | | |
452 | 4.26k | RETURN_IF_ERROR(assemble_exception_table(a, instrs)); |
453 | | |
454 | 4.26k | a->a_except_table = PyBytesWriter_FinishWithSize(a->a_except_table_writer, |
455 | 4.26k | a->a_except_table_off); |
456 | 4.26k | a->a_except_table_writer = NULL; |
457 | 4.26k | if (a->a_except_table == NULL) { |
458 | 0 | return ERROR; |
459 | 0 | } |
460 | 4.26k | RETURN_IF_ERROR(_PyCompile_ConstCacheMergeOne(const_cache, &a->a_except_table)); |
461 | | |
462 | 4.26k | a->a_linetable = PyBytesWriter_FinishWithSize(a->a_linetable_writer, |
463 | 4.26k | a->a_location_off); |
464 | 4.26k | a->a_linetable_writer = NULL; |
465 | 4.26k | if (a->a_linetable == NULL) { |
466 | 0 | return ERROR; |
467 | 0 | } |
468 | 4.26k | RETURN_IF_ERROR(_PyCompile_ConstCacheMergeOne(const_cache, &a->a_linetable)); |
469 | | |
470 | 4.26k | a->a_bytecode = PyBytesWriter_FinishWithSize(a->a_bytecode_writer, |
471 | 4.26k | a->a_offset * sizeof(_Py_CODEUNIT)); |
472 | 4.26k | a->a_bytecode_writer = NULL; |
473 | 4.26k | if (a->a_bytecode == NULL) { |
474 | 0 | return ERROR; |
475 | 0 | } |
476 | 4.26k | RETURN_IF_ERROR(_PyCompile_ConstCacheMergeOne(const_cache, &a->a_bytecode)); |
477 | 4.26k | return SUCCESS; |
478 | 4.26k | } |
479 | | |
480 | | static PyObject * |
481 | | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
482 | 4.26k | { |
483 | 4.26k | PyObject *tuple, *k, *v; |
484 | 4.26k | Py_ssize_t pos = 0, size = PyDict_GET_SIZE(dict); |
485 | | |
486 | 4.26k | tuple = PyTuple_New(size); |
487 | 4.26k | if (tuple == NULL) |
488 | 0 | return NULL; |
489 | 25.6k | while (PyDict_Next(dict, &pos, &k, &v)) { |
490 | 21.3k | Py_ssize_t i = PyLong_AsSsize_t(v); |
491 | 21.3k | if (i == -1 && PyErr_Occurred()) { |
492 | 0 | Py_DECREF(tuple); |
493 | 0 | return NULL; |
494 | 0 | } |
495 | 21.3k | assert((i - offset) < size); |
496 | 21.3k | assert((i - offset) >= 0); |
497 | 21.3k | PyTuple_SET_ITEM(tuple, i - offset, Py_NewRef(k)); |
498 | 21.3k | } |
499 | 4.26k | return tuple; |
500 | 4.26k | } |
501 | | |
502 | | // This is in codeobject.c. |
503 | | extern void _Py_set_localsplus_info(int, PyObject *, unsigned char, |
504 | | PyObject *, PyObject *); |
505 | | |
506 | | static int |
507 | | compute_localsplus_info(_PyCompile_CodeUnitMetadata *umd, int nlocalsplus, |
508 | | int flags, PyObject *names, PyObject *kinds) |
509 | 4.26k | { |
510 | 4.26k | PyObject *k, *v; |
511 | 4.26k | Py_ssize_t pos = 0; |
512 | | |
513 | | // Set the locals kinds. Arg vars fill the first portion of the list. |
514 | 4.26k | struct { |
515 | 4.26k | int count; |
516 | 4.26k | _PyLocals_Kind kind; |
517 | 4.26k | } argvarkinds[6] = { |
518 | 4.26k | {(int)umd->u_posonlyargcount, CO_FAST_ARG_POS}, |
519 | 4.26k | {(int)umd->u_argcount, CO_FAST_ARG_POS | CO_FAST_ARG_KW}, |
520 | 4.26k | {(int)umd->u_kwonlyargcount, CO_FAST_ARG_KW}, |
521 | 4.26k | {!!(flags & CO_VARARGS), CO_FAST_ARG_VAR | CO_FAST_ARG_POS}, |
522 | 4.26k | {!!(flags & CO_VARKEYWORDS), CO_FAST_ARG_VAR | CO_FAST_ARG_KW}, |
523 | 4.26k | {-1, 0}, // the remaining local vars |
524 | 4.26k | }; |
525 | 4.26k | int max = 0; |
526 | 29.8k | for (int i = 0; i < 6; i++) { |
527 | 25.5k | max = argvarkinds[i].count < 0 |
528 | 25.5k | ? INT_MAX |
529 | 25.5k | : max + argvarkinds[i].count; |
530 | 35.5k | while (pos < max && PyDict_Next(umd->u_varnames, &pos, &k, &v)) { |
531 | 10.0k | int offset = PyLong_AsInt(v); |
532 | 10.0k | if (offset == -1 && PyErr_Occurred()) { |
533 | 0 | return ERROR; |
534 | 0 | } |
535 | 10.0k | assert(offset >= 0); |
536 | 10.0k | assert(offset < nlocalsplus); |
537 | | |
538 | 10.0k | _PyLocals_Kind kind = CO_FAST_LOCAL | argvarkinds[i].kind; |
539 | | |
540 | 10.0k | if (umd->u_fasthidden != NULL) { |
541 | 3 | int hidden = PySet_Contains(umd->u_fasthidden, k); |
542 | 3 | RETURN_IF_ERROR(hidden); |
543 | 3 | if (hidden) { |
544 | 3 | kind |= CO_FAST_HIDDEN; |
545 | 3 | } |
546 | 3 | } |
547 | | |
548 | 10.0k | int has_key = PyDict_Contains(umd->u_cellvars, k); |
549 | 10.0k | RETURN_IF_ERROR(has_key); |
550 | 10.0k | if (has_key) { |
551 | 476 | kind |= CO_FAST_CELL; |
552 | 476 | } |
553 | | |
554 | 10.0k | _Py_set_localsplus_info(offset, k, kind, names, kinds); |
555 | 10.0k | } |
556 | 25.5k | } |
557 | 4.26k | int nlocals = (int)PyDict_GET_SIZE(umd->u_varnames); |
558 | | |
559 | | // This counter mirrors the fix done in fix_cell_offsets(). |
560 | 4.26k | int numdropped = 0, cellvar_offset = -1; |
561 | 4.26k | pos = 0; |
562 | 5.16k | while (PyDict_Next(umd->u_cellvars, &pos, &k, &v)) { |
563 | 902 | int has_name = PyDict_Contains(umd->u_varnames, k); |
564 | 902 | RETURN_IF_ERROR(has_name); |
565 | 902 | if (has_name) { |
566 | | // Skip cells that are already covered by locals. |
567 | 476 | numdropped += 1; |
568 | 476 | continue; |
569 | 476 | } |
570 | | |
571 | 426 | cellvar_offset = PyLong_AsInt(v); |
572 | 426 | if (cellvar_offset == -1 && PyErr_Occurred()) { |
573 | 0 | return ERROR; |
574 | 0 | } |
575 | 426 | assert(cellvar_offset >= 0); |
576 | 426 | cellvar_offset += nlocals - numdropped; |
577 | 426 | assert(cellvar_offset < nlocalsplus); |
578 | 426 | _Py_set_localsplus_info(cellvar_offset, k, CO_FAST_CELL, names, kinds); |
579 | 426 | } |
580 | | |
581 | 4.26k | pos = 0; |
582 | 5.02k | while (PyDict_Next(umd->u_freevars, &pos, &k, &v)) { |
583 | 767 | int offset = PyLong_AsInt(v); |
584 | 767 | if (offset == -1 && PyErr_Occurred()) { |
585 | 0 | return ERROR; |
586 | 0 | } |
587 | 767 | assert(offset >= 0); |
588 | 767 | offset += nlocals - numdropped; |
589 | 767 | assert(offset < nlocalsplus); |
590 | | /* XXX If the assertion below fails it is most likely because a freevar |
591 | | was added to u_freevars with the wrong index due to not taking into |
592 | | account cellvars already present, see gh-128632. */ |
593 | 767 | assert(offset > cellvar_offset); |
594 | 767 | _Py_set_localsplus_info(offset, k, CO_FAST_FREE, names, kinds); |
595 | 767 | } |
596 | 4.26k | return SUCCESS; |
597 | 4.26k | } |
598 | | |
599 | | static PyCodeObject * |
600 | | makecode(_PyCompile_CodeUnitMetadata *umd, struct assembler *a, PyObject *const_cache, |
601 | | PyObject *constslist, int maxdepth, int nlocalsplus, int code_flags, |
602 | | PyObject *filename) |
603 | 4.26k | { |
604 | 4.26k | PyCodeObject *co = NULL; |
605 | 4.26k | PyObject *names = NULL; |
606 | 4.26k | PyObject *consts = NULL; |
607 | 4.26k | PyObject *localsplusnames = NULL; |
608 | 4.26k | PyObject *localspluskinds = NULL; |
609 | 4.26k | names = dict_keys_inorder(umd->u_names, 0); |
610 | 4.26k | if (!names) { |
611 | 0 | goto error; |
612 | 0 | } |
613 | 4.26k | if (_PyCompile_ConstCacheMergeOne(const_cache, &names) < 0) { |
614 | 0 | goto error; |
615 | 0 | } |
616 | | |
617 | 4.26k | consts = PyList_AsTuple(constslist); /* PyCode_New requires a tuple */ |
618 | 4.26k | if (consts == NULL) { |
619 | 0 | goto error; |
620 | 0 | } |
621 | 4.26k | if (_PyCompile_ConstCacheMergeOne(const_cache, &consts) < 0) { |
622 | 0 | goto error; |
623 | 0 | } |
624 | | |
625 | 4.26k | assert(umd->u_posonlyargcount < INT_MAX); |
626 | 4.26k | assert(umd->u_argcount < INT_MAX); |
627 | 4.26k | assert(umd->u_kwonlyargcount < INT_MAX); |
628 | 4.26k | int posonlyargcount = (int)umd->u_posonlyargcount; |
629 | 4.26k | int posorkwargcount = (int)umd->u_argcount; |
630 | 4.26k | assert(INT_MAX - posonlyargcount - posorkwargcount > 0); |
631 | 4.26k | int kwonlyargcount = (int)umd->u_kwonlyargcount; |
632 | | |
633 | 4.26k | localsplusnames = PyTuple_New(nlocalsplus); |
634 | 4.26k | if (localsplusnames == NULL) { |
635 | 0 | goto error; |
636 | 0 | } |
637 | 4.26k | localspluskinds = PyBytes_FromStringAndSize(NULL, nlocalsplus); |
638 | 4.26k | if (localspluskinds == NULL) { |
639 | 0 | goto error; |
640 | 0 | } |
641 | 4.26k | if (compute_localsplus_info( |
642 | 4.26k | umd, nlocalsplus, code_flags, |
643 | 4.26k | localsplusnames, localspluskinds) == ERROR) |
644 | 0 | { |
645 | 0 | goto error; |
646 | 0 | } |
647 | | |
648 | 4.26k | struct _PyCodeConstructor con = { |
649 | 4.26k | .filename = filename, |
650 | 4.26k | .name = umd->u_name, |
651 | 4.26k | .qualname = umd->u_qualname ? umd->u_qualname : umd->u_name, |
652 | 4.26k | .flags = code_flags, |
653 | | |
654 | 4.26k | .code = a->a_bytecode, |
655 | 4.26k | .firstlineno = umd->u_firstlineno, |
656 | 4.26k | .linetable = a->a_linetable, |
657 | | |
658 | 4.26k | .consts = consts, |
659 | 4.26k | .names = names, |
660 | | |
661 | 4.26k | .localsplusnames = localsplusnames, |
662 | 4.26k | .localspluskinds = localspluskinds, |
663 | | |
664 | 4.26k | .argcount = posonlyargcount + posorkwargcount, |
665 | 4.26k | .posonlyargcount = posonlyargcount, |
666 | 4.26k | .kwonlyargcount = kwonlyargcount, |
667 | | |
668 | 4.26k | .stacksize = maxdepth, |
669 | | |
670 | 4.26k | .exceptiontable = a->a_except_table, |
671 | 4.26k | }; |
672 | | |
673 | 4.26k | if (_PyCode_Validate(&con) < 0) { |
674 | 0 | goto error; |
675 | 0 | } |
676 | | |
677 | 4.26k | if (_PyCompile_ConstCacheMergeOne(const_cache, &localsplusnames) < 0) { |
678 | 0 | goto error; |
679 | 0 | } |
680 | 4.26k | con.localsplusnames = localsplusnames; |
681 | | |
682 | 4.26k | co = _PyCode_New(&con); |
683 | 4.26k | if (co == NULL) { |
684 | 0 | goto error; |
685 | 0 | } |
686 | | |
687 | 4.26k | error: |
688 | 4.26k | Py_XDECREF(names); |
689 | 4.26k | Py_XDECREF(consts); |
690 | 4.26k | Py_XDECREF(localsplusnames); |
691 | 4.26k | Py_XDECREF(localspluskinds); |
692 | 4.26k | return co; |
693 | 4.26k | } |
694 | | |
695 | | |
696 | | // The offset (in code units) of the END_SEND from the SEND in the `yield from` sequence. |
697 | 0 | #define END_SEND_OFFSET 6 |
698 | | |
699 | | static int |
700 | | resolve_jump_offsets(instr_sequence *instrs) |
701 | 4.26k | { |
702 | | /* Compute the size of each instruction and fixup jump args. |
703 | | * Replace instruction index with position in bytecode. |
704 | | */ |
705 | | |
706 | 158k | for (int i = 0; i < instrs->s_used; i++) { |
707 | 153k | instruction *instr = &instrs->s_instrs[i]; |
708 | 153k | if (OPCODE_HAS_JUMP(instr->i_opcode)) { |
709 | 5.01k | instr->i_target = instr->i_oparg; |
710 | 5.01k | } |
711 | 153k | } |
712 | | |
713 | 4.26k | int extended_arg_recompile; |
714 | | |
715 | 4.33k | do { |
716 | 4.33k | int totsize = 0; |
717 | 177k | for (int i = 0; i < instrs->s_used; i++) { |
718 | 172k | instruction *instr = &instrs->s_instrs[i]; |
719 | 172k | instr->i_offset = totsize; |
720 | 172k | int isize = instr_size(instr); |
721 | 172k | totsize += isize; |
722 | 172k | } |
723 | 4.33k | extended_arg_recompile = 0; |
724 | | |
725 | 4.33k | int offset = 0; |
726 | 177k | for (int i = 0; i < instrs->s_used; i++) { |
727 | 172k | instruction *instr = &instrs->s_instrs[i]; |
728 | 172k | int isize = instr_size(instr); |
729 | | /* jump offsets are computed relative to |
730 | | * the instruction pointer after fetching |
731 | | * the jump instruction. |
732 | | */ |
733 | 172k | offset += isize; |
734 | 172k | if (OPCODE_HAS_JUMP(instr->i_opcode)) { |
735 | 6.50k | instruction *target = &instrs->s_instrs[instr->i_target]; |
736 | 6.50k | instr->i_oparg = target->i_offset; |
737 | 6.50k | if (instr->i_opcode == END_ASYNC_FOR) { |
738 | | // sys.monitoring needs to be able to find the matching END_SEND |
739 | | // but the target is the SEND, so we adjust it here. |
740 | 0 | instr->i_oparg = offset - instr->i_oparg - END_SEND_OFFSET; |
741 | 0 | } |
742 | 6.50k | else if (instr->i_oparg < offset) { |
743 | 970 | assert(IS_BACKWARDS_JUMP_OPCODE(instr->i_opcode)); |
744 | 970 | instr->i_oparg = offset - instr->i_oparg; |
745 | 970 | } |
746 | 5.53k | else { |
747 | 5.53k | assert(!IS_BACKWARDS_JUMP_OPCODE(instr->i_opcode)); |
748 | 5.53k | instr->i_oparg = instr->i_oparg - offset; |
749 | 5.53k | } |
750 | 6.50k | if (instr_size(instr) != isize) { |
751 | 442 | extended_arg_recompile = 1; |
752 | 442 | } |
753 | 6.50k | } |
754 | 172k | } |
755 | | /* XXX: This is an awful hack that could hurt performance, but |
756 | | on the bright side it should work until we come up |
757 | | with a better solution. |
758 | | |
759 | | The issue is that in the first loop instr_size() is |
760 | | called, and it requires i_oparg be set appropriately. |
761 | | There is a bootstrap problem because i_oparg is |
762 | | calculated in the second loop above. |
763 | | |
764 | | So we loop until we stop seeing new EXTENDED_ARGs. |
765 | | The only EXTENDED_ARGs that could be popping up are |
766 | | ones in jump instructions. So this should converge |
767 | | fairly quickly. |
768 | | */ |
769 | 4.33k | } while (extended_arg_recompile); |
770 | 4.26k | return SUCCESS; |
771 | 4.26k | } |
772 | | |
773 | | static int |
774 | | resolve_unconditional_jumps(instr_sequence *instrs) |
775 | 4.26k | { |
776 | | /* Resolve directions of unconditional jumps */ |
777 | | |
778 | 158k | for (int i = 0; i < instrs->s_used; i++) { |
779 | 153k | instruction *instr = &instrs->s_instrs[i]; |
780 | 153k | bool is_forward = (instr->i_oparg > i); |
781 | 153k | switch(instr->i_opcode) { |
782 | 645 | case JUMP: |
783 | 645 | assert(is_pseudo_target(JUMP, JUMP_FORWARD)); |
784 | 645 | assert(is_pseudo_target(JUMP, JUMP_BACKWARD)); |
785 | 645 | instr->i_opcode = is_forward ? JUMP_FORWARD : JUMP_BACKWARD; |
786 | 645 | break; |
787 | 479 | case JUMP_NO_INTERRUPT: |
788 | 479 | assert(is_pseudo_target(JUMP_NO_INTERRUPT, JUMP_FORWARD)); |
789 | 479 | assert(is_pseudo_target(JUMP_NO_INTERRUPT, JUMP_BACKWARD_NO_INTERRUPT)); |
790 | 479 | instr->i_opcode = is_forward ? |
791 | 345 | JUMP_FORWARD : JUMP_BACKWARD_NO_INTERRUPT; |
792 | 479 | break; |
793 | 152k | default: |
794 | 152k | if (OPCODE_HAS_JUMP(instr->i_opcode) && |
795 | 3.88k | IS_PSEUDO_INSTR(instr->i_opcode)) { |
796 | 0 | Py_UNREACHABLE(); |
797 | 0 | } |
798 | 153k | } |
799 | 153k | } |
800 | 4.26k | return SUCCESS; |
801 | 4.26k | } |
802 | | |
803 | | PyCodeObject * |
804 | | _PyAssemble_MakeCodeObject(_PyCompile_CodeUnitMetadata *umd, PyObject *const_cache, |
805 | | PyObject *consts, int maxdepth, instr_sequence *instrs, |
806 | | int nlocalsplus, int code_flags, PyObject *filename) |
807 | 4.26k | { |
808 | 4.26k | if (_PyInstructionSequence_ApplyLabelMap(instrs) < 0) { |
809 | 0 | return NULL; |
810 | 0 | } |
811 | 4.26k | if (resolve_unconditional_jumps(instrs) < 0) { |
812 | 0 | return NULL; |
813 | 0 | } |
814 | 4.26k | if (resolve_jump_offsets(instrs) < 0) { |
815 | 0 | return NULL; |
816 | 0 | } |
817 | 4.26k | PyCodeObject *co = NULL; |
818 | | |
819 | 4.26k | struct assembler a; |
820 | 4.26k | int res = assemble_emit(&a, instrs, umd->u_firstlineno, const_cache); |
821 | 4.26k | if (res == SUCCESS) { |
822 | 4.26k | co = makecode(umd, &a, const_cache, consts, maxdepth, nlocalsplus, |
823 | 4.26k | code_flags, filename); |
824 | 4.26k | } |
825 | 4.26k | assemble_free(&a); |
826 | 4.26k | return co; |
827 | 4.26k | } |