Coverage Report

Created: 2025-07-04 06:49

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