Coverage Report

Created: 2026-05-16 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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.38k
#define DEFAULT_CODE_SIZE 128
12
5.38k
#define DEFAULT_LNOTAB_SIZE 16
13
5.38k
#define DEFAULT_CNOTAB_SIZE 32
14
15
#undef SUCCESS
16
#undef ERROR
17
391k
#define SUCCESS 0
18
5.38k
#define ERROR -1
19
20
#define RETURN_IF_ERROR(X)  \
21
435k
    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
389k
{
32
389k
    return a.lineno == b.lineno &&
33
144k
           a.end_lineno == b.end_lineno &&
34
141k
           a.col_offset == b.col_offset &&
35
88.3k
           a.end_col_offset == b.end_col_offset;
36
389k
}
37
38
static int
39
instr_size(instruction *instr)
40
1.02M
{
41
1.02M
    int opcode = instr->i_opcode;
42
1.02M
    int oparg = instr->i_oparg;
43
1.02M
    assert(!IS_PSEUDO_INSTR(opcode));
44
1.02M
    assert(OPCODE_HAS_ARG(opcode) || oparg == 0);
45
1.02M
    int extended_args = (0xFFFFFF < oparg) + (0xFFFF < oparg) + (0xFF < oparg);
46
1.02M
    int caches = _PyOpcode_Caches[opcode];
47
1.02M
    return extended_args + 1 + caches;
48
1.02M
}
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.38k
{
64
5.38k
    memset(a, 0, sizeof(struct assembler));
65
5.38k
    a->a_lineno = firstlineno;
66
5.38k
    a->a_linetable = NULL;
67
5.38k
    a->a_location_off = 0;
68
5.38k
    a->a_except_table = NULL;
69
5.38k
    a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
70
5.38k
    if (a->a_bytecode == NULL) {
71
0
        goto error;
72
0
    }
73
5.38k
    a->a_linetable = PyBytes_FromStringAndSize(NULL, DEFAULT_CNOTAB_SIZE);
74
5.38k
    if (a->a_linetable == NULL) {
75
0
        goto error;
76
0
    }
77
5.38k
    a->a_except_table = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
78
5.38k
    if (a->a_except_table == NULL) {
79
0
        goto error;
80
0
    }
81
5.38k
    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.38k
}
88
89
static void
90
assemble_free(struct assembler *a)
91
5.38k
{
92
5.38k
    Py_XDECREF(a->a_bytecode);
93
5.38k
    Py_XDECREF(a->a_linetable);
94
5.38k
    Py_XDECREF(a->a_except_table);
95
5.38k
}
96
97
static inline void
98
23.2k
write_except_byte(struct assembler *a, int byte) {
99
23.2k
    unsigned char *p = (unsigned char *) PyBytes_AS_STRING(a->a_except_table);
100
23.2k
    p[a->a_except_table_off++] = byte;
101
23.2k
}
102
103
6.93k
#define CONTINUATION_BIT 64
104
105
static void
106
assemble_emit_exception_table_item(struct assembler *a, int value, int msb)
107
16.3k
{
108
16.3k
    assert ((msb | 128) == 128);
109
16.3k
    assert(value >= 0 && value < (1 << 30));
110
16.3k
    if (value >= 1 << 24) {
111
0
        write_except_byte(a, (value >> 24) | CONTINUATION_BIT | msb);
112
0
        msb = 0;
113
0
    }
114
16.3k
    if (value >= 1 << 18) {
115
0
        write_except_byte(a, ((value >> 18)&0x3f) | CONTINUATION_BIT | msb);
116
0
        msb = 0;
117
0
    }
118
16.3k
    if (value >= 1 << 12) {
119
0
        write_except_byte(a, ((value >> 12)&0x3f) | CONTINUATION_BIT | msb);
120
0
        msb = 0;
121
0
    }
122
16.3k
    if (value >= 1 << 6) {
123
6.93k
        write_except_byte(a, ((value >> 6)&0x3f) | CONTINUATION_BIT | msb);
124
6.93k
        msb = 0;
125
6.93k
    }
126
16.3k
    write_except_byte(a, (value&0x3f) | msb);
127
16.3k
}
128
129
/* See InternalDocs/exception_handling.md for details of layout */
130
4.09k
#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.09k
{
137
4.09k
    Py_ssize_t len = PyBytes_GET_SIZE(a->a_except_table);
138
4.09k
    if (a->a_except_table_off + MAX_SIZE_OF_ENTRY >= len) {
139
1.15k
        RETURN_IF_ERROR(_PyBytes_Resize(&a->a_except_table, len * 2));
140
1.15k
    }
141
4.09k
    int size = end-start;
142
4.09k
    assert(end > start);
143
4.09k
    int target = handler_offset;
144
4.09k
    int depth = handler->h_startdepth - 1;
145
4.09k
    if (handler->h_preserve_lasti > 0) {
146
2.99k
        depth -= 1;
147
2.99k
    }
148
4.09k
    assert(depth >= 0);
149
4.09k
    int depth_lasti = (depth<<1) | handler->h_preserve_lasti;
150
4.09k
    assemble_emit_exception_table_item(a, start, (1<<7));
151
4.09k
    assemble_emit_exception_table_item(a, size, 0);
152
4.09k
    assemble_emit_exception_table_item(a, target, 0);
153
4.09k
    assemble_emit_exception_table_item(a, depth_lasti, 0);
154
4.09k
    return SUCCESS;
155
4.09k
}
156
157
static int
158
assemble_exception_table(struct assembler *a, instr_sequence *instrs)
159
5.38k
{
160
5.38k
    int ioffset = 0;
161
5.38k
    _PyExceptHandlerInfo handler;
162
5.38k
    handler.h_label = -1;
163
5.38k
    handler.h_startdepth = -1;
164
5.38k
    handler.h_preserve_lasti = -1;
165
5.38k
    int start = -1;
166
200k
    for (int i = 0; i < instrs->s_used; i++) {
167
194k
        instruction *instr = &instrs->s_instrs[i];
168
194k
        if (instr->i_except_handler_info.h_label != handler.h_label) {
169
7.05k
            if (handler.h_label >= 0) {
170
4.09k
                int handler_offset = instrs->s_instrs[handler.h_label].i_offset;
171
4.09k
                RETURN_IF_ERROR(
172
4.09k
                    assemble_emit_exception_table_entry(a, start, ioffset,
173
4.09k
                                                        handler_offset,
174
4.09k
                                                        &handler));
175
4.09k
            }
176
7.05k
            start = ioffset;
177
7.05k
            handler = instr->i_except_handler_info;
178
7.05k
        }
179
194k
        ioffset += instr_size(instr);
180
194k
    }
181
5.38k
    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.38k
    return SUCCESS;
188
5.38k
}
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
158k
{
198
158k
    PyBytes_AS_STRING(a->a_linetable)[a->a_location_off] = val&255;
199
158k
    a->a_location_off++;
200
158k
}
201
202
203
static uint8_t *
204
location_pointer(struct assembler* a)
205
284k
{
206
284k
    return (uint8_t *)PyBytes_AS_STRING(a->a_linetable) +
207
284k
        a->a_location_off;
208
284k
}
209
210
static void
211
write_location_first_byte(struct assembler* a, int code, int length)
212
145k
{
213
145k
    a->a_location_off += write_location_entry_start(
214
145k
        location_pointer(a), code, length);
215
145k
}
216
217
static void
218
write_location_varint(struct assembler* a, unsigned int val)
219
104k
{
220
104k
    uint8_t *ptr = location_pointer(a);
221
104k
    a->a_location_off += write_varint(ptr, val);
222
104k
}
223
224
225
static void
226
write_location_signed_varint(struct assembler* a, int val)
227
35.0k
{
228
35.0k
    uint8_t *ptr = location_pointer(a);
229
35.0k
    a->a_location_off += write_signed_varint(ptr, val);
230
35.0k
}
231
232
static void
233
write_location_info_short_form(struct assembler* a, int length, int column, int end_column)
234
56.0k
{
235
56.0k
    assert(length > 0 &&  length <= 8);
236
56.0k
    int column_low_bits = column & 7;
237
56.0k
    int column_group = column >> 3;
238
56.0k
    assert(column < 80);
239
56.0k
    assert(end_column >= column);
240
56.0k
    assert(end_column - column < 16);
241
56.0k
    write_location_first_byte(a, PY_CODE_LOCATION_INFO_SHORT0 + column_group, length);
242
56.0k
    write_location_byte(a, (column_low_bits << 4) | (end_column - column));
243
56.0k
}
244
245
static void
246
write_location_info_oneline_form(struct assembler* a, int length, int line_delta, int column, int end_column)
247
51.3k
{
248
51.3k
    assert(length > 0 &&  length <= 8);
249
51.3k
    assert(line_delta >= 0 && line_delta < 3);
250
51.3k
    assert(column < 128);
251
51.3k
    assert(end_column < 128);
252
51.3k
    write_location_first_byte(a, PY_CODE_LOCATION_INFO_ONE_LINE0 + line_delta, length);
253
51.3k
    write_location_byte(a, column);
254
51.3k
    write_location_byte(a, end_column);
255
51.3k
}
256
257
static void
258
write_location_info_long_form(struct assembler* a, location loc, int length)
259
34.8k
{
260
34.8k
    assert(length > 0 &&  length <= 8);
261
34.8k
    write_location_first_byte(a, PY_CODE_LOCATION_INFO_LONG, length);
262
34.8k
    write_location_signed_varint(a, loc.lineno - a->a_lineno);
263
34.8k
    assert(loc.end_lineno >= loc.lineno);
264
34.8k
    write_location_varint(a, loc.end_lineno - loc.lineno);
265
34.8k
    write_location_varint(a, loc.col_offset + 1);
266
34.8k
    write_location_varint(a, loc.end_col_offset + 1);
267
34.8k
}
268
269
static void
270
write_location_info_none(struct assembler* a, int length)
271
2.69k
{
272
2.69k
    write_location_first_byte(a, PY_CODE_LOCATION_INFO_NONE, length);
273
2.69k
}
274
275
static void
276
write_location_info_no_column(struct assembler* a, int length, int line_delta)
277
252
{
278
252
    write_location_first_byte(a, PY_CODE_LOCATION_INFO_NO_COLUMNS, length);
279
252
    write_location_signed_varint(a, line_delta);
280
252
}
281
282
145k
#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
145k
{
288
145k
    Py_ssize_t len = PyBytes_GET_SIZE(a->a_linetable);
289
145k
    if (a->a_location_off + THEORETICAL_MAX_ENTRY_SIZE >= len) {
290
8.27k
        assert(len > THEORETICAL_MAX_ENTRY_SIZE);
291
8.27k
        RETURN_IF_ERROR(_PyBytes_Resize(&a->a_linetable, len*2));
292
8.27k
    }
293
145k
    if (loc.lineno == NO_LOCATION.lineno) {
294
2.69k
        write_location_info_none(a, isize);
295
2.69k
        return SUCCESS;
296
2.69k
    }
297
142k
    int line_delta = loc.lineno - a->a_lineno;
298
142k
    int column = loc.col_offset;
299
142k
    int end_column = loc.end_col_offset;
300
142k
    if (column < 0 || end_column < 0) {
301
252
        if (loc.end_lineno == loc.lineno || loc.end_lineno < 0) {
302
252
            write_location_info_no_column(a, isize, line_delta);
303
252
            a->a_lineno = loc.lineno;
304
252
            return SUCCESS;
305
252
        }
306
252
    }
307
142k
    else if (loc.end_lineno == loc.lineno) {
308
124k
        if (line_delta == 0 && column < 80 && end_column - column < 16 && end_column >= column) {
309
56.0k
            write_location_info_short_form(a, isize, column, end_column);
310
56.0k
            return SUCCESS;
311
56.0k
        }
312
68.6k
        if (line_delta >= 0 && line_delta < 3 && column < 128 && end_column < 128) {
313
51.3k
            write_location_info_oneline_form(a, isize, line_delta, column, end_column);
314
51.3k
            a->a_lineno = loc.lineno;
315
51.3k
            return SUCCESS;
316
51.3k
        }
317
68.6k
    }
318
34.8k
    write_location_info_long_form(a, loc, isize);
319
34.8k
    a->a_lineno = loc.lineno;
320
34.8k
    return SUCCESS;
321
142k
}
322
323
static int
324
assemble_emit_location(struct assembler* a, location loc, int isize)
325
130k
{
326
130k
    if (isize == 0) {
327
3.93k
        return SUCCESS;
328
3.93k
    }
329
145k
    while (isize > 8) {
330
18.3k
        RETURN_IF_ERROR(write_location_info_entry(a, loc, 8));
331
18.3k
        isize -= 8;
332
18.3k
    }
333
126k
    return write_location_info_entry(a, loc, isize);
334
126k
}
335
336
static int
337
assemble_location_info(struct assembler *a, instr_sequence *instrs,
338
                       int firstlineno)
339
5.38k
{
340
5.38k
    a->a_lineno = firstlineno;
341
5.38k
    location loc = NO_LOCATION;
342
200k
    for (int i = instrs->s_used-1; i >= 0; i--) {
343
194k
        instruction *instr = &instrs->s_instrs[i];
344
194k
        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
194k
    }
354
5.38k
    int size = 0;
355
200k
    for (int i = 0; i < instrs->s_used; i++) {
356
194k
        instruction *instr = &instrs->s_instrs[i];
357
194k
        if (!same_location(loc, instr->i_loc)) {
358
125k
                RETURN_IF_ERROR(assemble_emit_location(a, loc, size));
359
125k
                loc = instr->i_loc;
360
125k
                size = 0;
361
125k
        }
362
194k
        size += instr_size(instr);
363
194k
    }
364
5.38k
    RETURN_IF_ERROR(assemble_emit_location(a, loc, size));
365
5.38k
    return SUCCESS;
366
5.38k
}
367
368
static void
369
write_instr(_Py_CODEUNIT *codestr, instruction *instr, int ilen)
370
194k
{
371
194k
    int opcode = instr->i_opcode;
372
194k
    assert(!IS_PSEUDO_INSTR(opcode));
373
194k
    int oparg = instr->i_oparg;
374
194k
    assert(OPCODE_HAS_ARG(opcode) || oparg == 0);
375
194k
    int caches = _PyOpcode_Caches[opcode];
376
194k
    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
302
        case 2:
388
302
            codestr->op.code = EXTENDED_ARG;
389
302
            codestr->op.arg = (oparg >> 8) & 0xFF;
390
302
            codestr++;
391
302
            _Py_FALLTHROUGH;
392
194k
        case 1:
393
194k
            codestr->op.code = opcode;
394
194k
            codestr->op.arg = oparg & 0xFF;
395
194k
            codestr++;
396
194k
            break;
397
0
        default:
398
0
            Py_UNREACHABLE();
399
194k
    }
400
427k
    while (caches--) {
401
232k
        codestr->op.code = CACHE;
402
232k
        codestr->op.arg = 0;
403
232k
        codestr++;
404
232k
    }
405
194k
}
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
194k
{
415
194k
    Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
416
194k
    _Py_CODEUNIT *code;
417
418
194k
    int size = instr_size(instr);
419
194k
    if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
420
3.03k
        if (len > PY_SSIZE_T_MAX / 2) {
421
0
            PyErr_NoMemory();
422
0
            return ERROR;
423
0
        }
424
3.03k
        RETURN_IF_ERROR(_PyBytes_Resize(&a->a_bytecode, len * 2));
425
3.03k
    }
426
194k
    code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
427
194k
    a->a_offset += size;
428
194k
    write_instr(code, instr, size);
429
194k
    return SUCCESS;
430
194k
}
431
432
static int
433
assemble_emit(struct assembler *a, instr_sequence *instrs,
434
              int first_lineno, PyObject *const_cache)
435
5.38k
{
436
5.38k
    RETURN_IF_ERROR(assemble_init(a, first_lineno));
437
438
200k
    for (int i = 0; i < instrs->s_used; i++) {
439
194k
        instruction *instr = &instrs->s_instrs[i];
440
194k
        RETURN_IF_ERROR(assemble_emit_instr(a, instr));
441
194k
    }
442
443
5.38k
    RETURN_IF_ERROR(assemble_location_info(a, instrs, a->a_lineno));
444
445
5.38k
    RETURN_IF_ERROR(assemble_exception_table(a, instrs));
446
447
5.38k
    RETURN_IF_ERROR(_PyBytes_Resize(&a->a_except_table, a->a_except_table_off));
448
5.38k
    RETURN_IF_ERROR(_PyCompile_ConstCacheMergeOne(const_cache, &a->a_except_table));
449
450
5.38k
    RETURN_IF_ERROR(_PyBytes_Resize(&a->a_linetable, a->a_location_off));
451
5.38k
    RETURN_IF_ERROR(_PyCompile_ConstCacheMergeOne(const_cache, &a->a_linetable));
452
453
5.38k
    RETURN_IF_ERROR(_PyBytes_Resize(&a->a_bytecode, a->a_offset * sizeof(_Py_CODEUNIT)));
454
5.38k
    RETURN_IF_ERROR(_PyCompile_ConstCacheMergeOne(const_cache, &a->a_bytecode));
455
5.38k
    return SUCCESS;
456
5.38k
}
457
458
static PyObject *
459
dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
460
5.38k
{
461
5.38k
    PyObject *tuple, *k, *v;
462
5.38k
    Py_ssize_t pos = 0, size = PyDict_GET_SIZE(dict);
463
464
5.38k
    tuple = PyTuple_New(size);
465
5.38k
    if (tuple == NULL)
466
0
        return NULL;
467
33.6k
    while (PyDict_Next(dict, &pos, &k, &v)) {
468
28.2k
        Py_ssize_t i = PyLong_AsSsize_t(v);
469
28.2k
        if (i == -1 && PyErr_Occurred()) {
470
0
            Py_DECREF(tuple);
471
0
            return NULL;
472
0
        }
473
28.2k
        assert((i - offset) < size);
474
28.2k
        assert((i - offset) >= 0);
475
28.2k
        PyTuple_SET_ITEM(tuple, i - offset, Py_NewRef(k));
476
28.2k
    }
477
5.38k
    return tuple;
478
5.38k
}
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.38k
{
488
5.38k
    PyObject *k, *v;
489
5.38k
    Py_ssize_t pos = 0;
490
491
    // Set the locals kinds.  Arg vars fill the first portion of the list.
492
5.38k
    struct {
493
5.38k
        int count;
494
5.38k
        _PyLocals_Kind kind;
495
5.38k
    }  argvarkinds[6] = {
496
5.38k
        {(int)umd->u_posonlyargcount, CO_FAST_ARG_POS},
497
5.38k
        {(int)umd->u_argcount, CO_FAST_ARG_POS | CO_FAST_ARG_KW},
498
5.38k
        {(int)umd->u_kwonlyargcount, CO_FAST_ARG_KW},
499
5.38k
        {!!(flags & CO_VARARGS), CO_FAST_ARG_VAR | CO_FAST_ARG_POS},
500
5.38k
        {!!(flags & CO_VARKEYWORDS), CO_FAST_ARG_VAR | CO_FAST_ARG_KW},
501
5.38k
        {-1, 0},  // the remaining local vars
502
5.38k
    };
503
5.38k
    int max = 0;
504
37.6k
    for (int i = 0; i < 6; i++) {
505
32.2k
        max = argvarkinds[i].count < 0
506
32.2k
            ? INT_MAX
507
32.2k
            : max + argvarkinds[i].count;
508
45.0k
        while (pos < max && PyDict_Next(umd->u_varnames, &pos, &k, &v)) {
509
12.7k
            int offset = PyLong_AsInt(v);
510
12.7k
            if (offset == -1 && PyErr_Occurred()) {
511
0
                return ERROR;
512
0
            }
513
12.7k
            assert(offset >= 0);
514
12.7k
            assert(offset < nlocalsplus);
515
516
12.7k
            _PyLocals_Kind kind = CO_FAST_LOCAL | argvarkinds[i].kind;
517
518
12.7k
            int has_key = PyDict_Contains(umd->u_fasthidden, k);
519
12.7k
            RETURN_IF_ERROR(has_key);
520
12.7k
            if (has_key) {
521
3
                kind |= CO_FAST_HIDDEN;
522
3
            }
523
524
12.7k
            has_key = PyDict_Contains(umd->u_cellvars, k);
525
12.7k
            RETURN_IF_ERROR(has_key);
526
12.7k
            if (has_key) {
527
516
                kind |= CO_FAST_CELL;
528
516
            }
529
530
12.7k
            _Py_set_localsplus_info(offset, k, kind, names, kinds);
531
12.7k
        }
532
32.2k
    }
533
5.38k
    int nlocals = (int)PyDict_GET_SIZE(umd->u_varnames);
534
535
    // This counter mirrors the fix done in fix_cell_offsets().
536
5.38k
    int numdropped = 0, cellvar_offset = -1;
537
5.38k
    pos = 0;
538
6.45k
    while (PyDict_Next(umd->u_cellvars, &pos, &k, &v)) {
539
1.07k
        int has_name = PyDict_Contains(umd->u_varnames, k);
540
1.07k
        RETURN_IF_ERROR(has_name);
541
1.07k
        if (has_name) {
542
            // Skip cells that are already covered by locals.
543
516
            numdropped += 1;
544
516
            continue;
545
516
        }
546
547
556
        cellvar_offset = PyLong_AsInt(v);
548
556
        if (cellvar_offset == -1 && PyErr_Occurred()) {
549
0
            return ERROR;
550
0
        }
551
556
        assert(cellvar_offset >= 0);
552
556
        cellvar_offset += nlocals - numdropped;
553
556
        assert(cellvar_offset < nlocalsplus);
554
556
        _Py_set_localsplus_info(cellvar_offset, k, CO_FAST_CELL, names, kinds);
555
556
    }
556
557
5.38k
    pos = 0;
558
6.29k
    while (PyDict_Next(umd->u_freevars, &pos, &k, &v)) {
559
915
        int offset = PyLong_AsInt(v);
560
915
        if (offset == -1 && PyErr_Occurred()) {
561
0
            return ERROR;
562
0
        }
563
915
        assert(offset >= 0);
564
915
        offset += nlocals - numdropped;
565
915
        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
915
        assert(offset > cellvar_offset);
570
915
        _Py_set_localsplus_info(offset, k, CO_FAST_FREE, names, kinds);
571
915
    }
572
5.38k
    return SUCCESS;
573
5.38k
}
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.38k
{
580
5.38k
    PyCodeObject *co = NULL;
581
5.38k
    PyObject *names = NULL;
582
5.38k
    PyObject *consts = NULL;
583
5.38k
    PyObject *localsplusnames = NULL;
584
5.38k
    PyObject *localspluskinds = NULL;
585
5.38k
    names = dict_keys_inorder(umd->u_names, 0);
586
5.38k
    if (!names) {
587
0
        goto error;
588
0
    }
589
5.38k
    if (_PyCompile_ConstCacheMergeOne(const_cache, &names) < 0) {
590
0
        goto error;
591
0
    }
592
593
5.38k
    consts = PyList_AsTuple(constslist); /* PyCode_New requires a tuple */
594
5.38k
    if (consts == NULL) {
595
0
        goto error;
596
0
    }
597
5.38k
    if (_PyCompile_ConstCacheMergeOne(const_cache, &consts) < 0) {
598
0
        goto error;
599
0
    }
600
601
5.38k
    assert(umd->u_posonlyargcount < INT_MAX);
602
5.38k
    assert(umd->u_argcount < INT_MAX);
603
5.38k
    assert(umd->u_kwonlyargcount < INT_MAX);
604
5.38k
    int posonlyargcount = (int)umd->u_posonlyargcount;
605
5.38k
    int posorkwargcount = (int)umd->u_argcount;
606
5.38k
    assert(INT_MAX - posonlyargcount - posorkwargcount > 0);
607
5.38k
    int kwonlyargcount = (int)umd->u_kwonlyargcount;
608
609
5.38k
    localsplusnames = PyTuple_New(nlocalsplus);
610
5.38k
    if (localsplusnames == NULL) {
611
0
        goto error;
612
0
    }
613
5.38k
    localspluskinds = PyBytes_FromStringAndSize(NULL, nlocalsplus);
614
5.38k
    if (localspluskinds == NULL) {
615
0
        goto error;
616
0
    }
617
5.38k
    if (compute_localsplus_info(
618
5.38k
            umd, nlocalsplus, code_flags,
619
5.38k
            localsplusnames, localspluskinds) == ERROR)
620
0
    {
621
0
        goto error;
622
0
    }
623
624
5.38k
    struct _PyCodeConstructor con = {
625
5.38k
        .filename = filename,
626
5.38k
        .name = umd->u_name,
627
5.38k
        .qualname = umd->u_qualname ? umd->u_qualname : umd->u_name,
628
5.38k
        .flags = code_flags,
629
630
5.38k
        .code = a->a_bytecode,
631
5.38k
        .firstlineno = umd->u_firstlineno,
632
5.38k
        .linetable = a->a_linetable,
633
634
5.38k
        .consts = consts,
635
5.38k
        .names = names,
636
637
5.38k
        .localsplusnames = localsplusnames,
638
5.38k
        .localspluskinds = localspluskinds,
639
640
5.38k
        .argcount = posonlyargcount + posorkwargcount,
641
5.38k
        .posonlyargcount = posonlyargcount,
642
5.38k
        .kwonlyargcount = kwonlyargcount,
643
644
5.38k
        .stacksize = maxdepth,
645
646
5.38k
        .exceptiontable = a->a_except_table,
647
5.38k
    };
648
649
5.38k
   if (_PyCode_Validate(&con) < 0) {
650
0
        goto error;
651
0
    }
652
653
5.38k
    if (_PyCompile_ConstCacheMergeOne(const_cache, &localsplusnames) < 0) {
654
0
        goto error;
655
0
    }
656
5.38k
    con.localsplusnames = localsplusnames;
657
658
5.38k
    co = _PyCode_New(&con);
659
5.38k
    if (co == NULL) {
660
0
        goto error;
661
0
    }
662
663
5.38k
error:
664
5.38k
    Py_XDECREF(names);
665
5.38k
    Py_XDECREF(consts);
666
5.38k
    Py_XDECREF(localsplusnames);
667
5.38k
    Py_XDECREF(localspluskinds);
668
5.38k
    return co;
669
5.38k
}
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.38k
{
678
    /* Compute the size of each instruction and fixup jump args.
679
     * Replace instruction index with position in bytecode.
680
     */
681
682
200k
    for (int i = 0; i < instrs->s_used; i++) {
683
194k
        instruction *instr = &instrs->s_instrs[i];
684
194k
        if (OPCODE_HAS_JUMP(instr->i_opcode)) {
685
7.69k
            instr->i_target = instr->i_oparg;
686
7.69k
        }
687
194k
    }
688
689
5.38k
    int extended_arg_recompile;
690
691
5.46k
    do {
692
5.46k
        int totsize = 0;
693
223k
        for (int i = 0; i < instrs->s_used; i++) {
694
217k
            instruction *instr = &instrs->s_instrs[i];
695
217k
            instr->i_offset = totsize;
696
217k
            int isize = instr_size(instr);
697
217k
            totsize += isize;
698
217k
        }
699
5.46k
        extended_arg_recompile = 0;
700
701
5.46k
        int offset = 0;
702
223k
        for (int i = 0; i < instrs->s_used; i++) {
703
217k
            instruction *instr = &instrs->s_instrs[i];
704
217k
            int isize = instr_size(instr);
705
            /* jump offsets are computed relative to
706
             * the instruction pointer after fetching
707
             * the jump instruction.
708
             */
709
217k
            offset += isize;
710
217k
            if (OPCODE_HAS_JUMP(instr->i_opcode)) {
711
9.60k
                instruction *target = &instrs->s_instrs[instr->i_target];
712
9.60k
                instr->i_oparg = target->i_offset;
713
9.60k
                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
9.60k
                else if (instr->i_oparg < offset) {
719
1.59k
                    assert(IS_BACKWARDS_JUMP_OPCODE(instr->i_opcode));
720
1.59k
                    instr->i_oparg = offset - instr->i_oparg;
721
1.59k
                }
722
8.01k
                else {
723
8.01k
                    assert(!IS_BACKWARDS_JUMP_OPCODE(instr->i_opcode));
724
8.01k
                    instr->i_oparg = instr->i_oparg - offset;
725
8.01k
                }
726
9.60k
                if (instr_size(instr) != isize) {
727
534
                    extended_arg_recompile = 1;
728
534
                }
729
9.60k
            }
730
217k
        }
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.46k
    } while (extended_arg_recompile);
746
5.38k
    return SUCCESS;
747
5.38k
}
748
749
static int
750
resolve_unconditional_jumps(instr_sequence *instrs)
751
5.38k
{
752
    /* Resolve directions of unconditional jumps */
753
754
200k
    for (int i = 0; i < instrs->s_used; i++) {
755
194k
        instruction *instr = &instrs->s_instrs[i];
756
194k
        bool is_forward = (instr->i_oparg > i);
757
194k
        switch(instr->i_opcode) {
758
866
            case JUMP:
759
866
                assert(is_pseudo_target(JUMP, JUMP_FORWARD));
760
866
                assert(is_pseudo_target(JUMP, JUMP_BACKWARD));
761
866
                instr->i_opcode = is_forward ? JUMP_FORWARD : JUMP_BACKWARD;
762
866
                break;
763
962
            case JUMP_NO_INTERRUPT:
764
962
                assert(is_pseudo_target(JUMP_NO_INTERRUPT, JUMP_FORWARD));
765
962
                assert(is_pseudo_target(JUMP_NO_INTERRUPT, JUMP_BACKWARD_NO_INTERRUPT));
766
962
                instr->i_opcode = is_forward ?
767
516
                    JUMP_FORWARD : JUMP_BACKWARD_NO_INTERRUPT;
768
962
                break;
769
192k
            default:
770
192k
                if (OPCODE_HAS_JUMP(instr->i_opcode) &&
771
5.87k
                    IS_PSEUDO_INSTR(instr->i_opcode)) {
772
0
                    Py_UNREACHABLE();
773
0
                }
774
194k
        }
775
194k
    }
776
5.38k
    return SUCCESS;
777
5.38k
}
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.38k
{
784
5.38k
    if (_PyInstructionSequence_ApplyLabelMap(instrs) < 0) {
785
0
        return NULL;
786
0
    }
787
5.38k
    if (resolve_unconditional_jumps(instrs) < 0) {
788
0
        return NULL;
789
0
    }
790
5.38k
    if (resolve_jump_offsets(instrs) < 0) {
791
0
        return NULL;
792
0
    }
793
5.38k
    PyCodeObject *co = NULL;
794
795
5.38k
    struct assembler a;
796
5.38k
    int res = assemble_emit(&a, instrs, umd->u_firstlineno, const_cache);
797
5.38k
    if (res == SUCCESS) {
798
5.38k
        co = makecode(umd, &a, const_cache, consts, maxdepth, nlocalsplus,
799
5.38k
                      code_flags, filename);
800
5.38k
    }
801
5.38k
    assemble_free(&a);
802
5.38k
    return co;
803
5.38k
}