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