[ Web Proxy ]
URL:
Viewing:
https://github-redirect.dependabot.com/python/cpython/blob/main/Python/instruction_sequence.c
[Back]
[Original]
cpython/Python/instruction_sequence.c at main python/cpython GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
python
cpython
Repository navigation
Code
Issues
5k+
(5k+)
Pull requests
2.6k
(2.6k)
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
cpython
/
Python
/
instruction_sequence.c
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
490 lines (420 loc) 13.7 KB
main
Breadcrumbs
cpython
/
Python
/
instruction_sequence.c
Copy path
Top
File metadata and controls
Code
Blame
490 lines (420 loc) 13.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
* This file implements a data structure representing a sequence of
* instructions, which is used by different parts of the compilation
* pipeline.
*/
#include
"Python.h"
#include
"pycore_c_array.h"
// _Py_CArray_EnsureCapacity
#include
"pycore_compile.h"
// _PyInstruction
#include
"pycore_opcode_utils.h"
#include
"pycore_opcode_metadata.h"
// OPCODE_HAS_ARG, etc
typedef
_PyInstruction
instruction
;
typedef
_PyInstructionSequence
instr_sequence
;
typedef
_Py_SourceLocation
location
;
#define
INITIAL_INSTR_SEQUENCE_SIZE
100
#define
INITIAL_INSTR_SEQUENCE_LABELS_MAP_SIZE
10
#include
"clinic/instruction_sequence.c.h"
#include
<stdbool.h>
#undef
SUCCESS
#undef
ERROR
#define
SUCCESS
0
#define
ERROR
-1
#define
RETURN_IF_ERROR
(
X
) \
if ((X) == -1) { \
return ERROR; \
}
static
int
instr_sequence_next_inst
(
instr_sequence
*
seq
) {
assert
(
seq
->
s_instrs
!=
NULL
||
seq
->
s_used
==
0
);
_Py_c_array_t
array
=
{
.
array
=
(
void
*
)
seq
->
s_instrs
,
.
allocated_entries
=
seq
->
s_allocated
,
.
item_size
=
sizeof
(
instruction
),
.
initial_num_entries
=
INITIAL_INSTR_SEQUENCE_SIZE
,
};
RETURN_IF_ERROR
(
_Py_CArray_EnsureCapacity
(
&
array
,
seq
->
s_used
+
1
));
seq
->
s_instrs
=
array
.
array
;
seq
->
s_allocated
=
array
.
allocated_entries
;
assert
(
seq
->
s_allocated
>=
0
);
assert
(
seq
->
s_used
<
seq
->
s_allocated
);
return
seq
->
s_used
++
;
}
_PyJumpTargetLabel
_PyInstructionSequence_NewLabel
(
instr_sequence
*
seq
)
{
_PyJumpTargetLabel
lbl
=
{
++
seq
->
s_next_free_label
};
return
lbl
;
}
int
_PyInstructionSequence_UseLabel
(
instr_sequence
*
seq
,
int
lbl
)
{
int
old_size
=
seq
->
s_labelmap_size
;
_Py_c_array_t
array
=
{
.
array
=
(
void
*
)
seq
->
s_labelmap
,
.
allocated_entries
=
seq
->
s_labelmap_size
,
.
item_size
=
sizeof
(
int
),
.
initial_num_entries
=
INITIAL_INSTR_SEQUENCE_LABELS_MAP_SIZE
,
};
RETURN_IF_ERROR
(
_Py_CArray_EnsureCapacity
(
&
array
,
lbl
));
seq
->
s_labelmap
=
array
.
array
;
seq
->
s_labelmap_size
=
array
.
allocated_entries
;
for
(
int
i
=
old_size
;
i
<
seq
->
s_labelmap_size
;
i
++
) {
seq
->
s_labelmap
[
i
]
=
-111
;
/* something weird, for debugging */
}
seq
->
s_labelmap
[
lbl
]
=
seq
->
s_used
;
/* label refers to the next instruction */
return
SUCCESS
;
}
int
_PyInstructionSequence_ApplyLabelMap
(
instr_sequence
*
instrs
)
{
if
(
instrs
->
s_labelmap
==
NULL
) {
/* Already applied - nothing to do */
return
SUCCESS
;
}
/* Replace labels by offsets in the code */
for
(
int
i
=
0
;
i
<
instrs
->
s_used
;
i
++
) {
instruction
*
instr
=
&
instrs
->
s_instrs
[
i
];
if
(
HAS_TARGET
(
instr
->
i_opcode
)) {
assert
(
instr
->
i_oparg
<
instrs
->
s_labelmap_size
);
instr
->
i_oparg
=
instrs
->
s_labelmap
[
instr
->
i_oparg
];
}
_PyExceptHandlerInfo
*
hi
=
&
instr
->
i_except_handler_info
;
if
(
hi
->
h_label
>=
0
) {
assert
(
hi
->
h_label
<
instrs
->
s_labelmap_size
);
hi
->
h_label
=
instrs
->
s_labelmap
[
hi
->
h_label
];
}
}
/* Clear label map so it's never used again */
PyMem_Free
(
instrs
->
s_labelmap
);
instrs
->
s_labelmap
=
NULL
;
instrs
->
s_labelmap_size
=
0
;
return
SUCCESS
;
}
#define
MAX_OPCODE
511
int
_PyInstructionSequence_Addop
(
instr_sequence
*
seq
,
int
opcode
,
int
oparg
,
location
loc
)
{
assert
(
0
<=
opcode
&&
opcode
<=
MAX_OPCODE
);
assert
(
IS_WITHIN_OPCODE_RANGE
(
opcode
));
assert
(
OPCODE_HAS_ARG
(
opcode
)
||
HAS_TARGET
(
opcode
)
||
oparg
==
0
);
assert
(
0
<=
oparg
&&
oparg
<
(
1
<<
30
));
int
idx
=
instr_sequence_next_inst
(
seq
);
RETURN_IF_ERROR
(
idx
);
instruction
*
ci
=
&
seq
->
s_instrs
[
idx
];
ci
->
i_opcode
=
opcode
;
ci
->
i_oparg
=
oparg
;
ci
->
i_loc
=
loc
;
return
SUCCESS
;
}
int
_PyInstructionSequence_InsertInstruction
(
instr_sequence
*
seq
,
int
pos
,
int
opcode
,
int
oparg
,
location
loc
)
{
assert
(
pos
>=
0
&&
pos
<=
seq
->
s_used
);
int
last_idx
=
instr_sequence_next_inst
(
seq
);
RETURN_IF_ERROR
(
last_idx
);
for
(
int
i
=
last_idx
-
1
;
i
>=
pos
;
i
--
) {
seq
->
s_instrs
[
i
+
1
]
=
seq
->
s_instrs
[
i
];
}
instruction
*
ci
=
&
seq
->
s_instrs
[
pos
];
ci
->
i_opcode
=
opcode
;
ci
->
i_oparg
=
oparg
;
ci
->
i_loc
=
loc
;
/* fix the labels map */
for
(
int
lbl
=
0
;
lbl
<
seq
->
s_labelmap_size
;
lbl
++
) {
if
(
seq
->
s_labelmap
[
lbl
] >=
pos
) {
seq
->
s_labelmap
[
lbl
]
++
;
}
}
return
SUCCESS
;
}
_PyInstruction
_PyInstructionSequence_GetInstruction
(
instr_sequence
*
seq
,
int
pos
)
{
assert
(
pos
>=
0
&&
pos
<
seq
->
s_used
);
return
seq
->
s_instrs
[
pos
];
}
int
_PyInstructionSequence_SetAnnotationsCode
(
instr_sequence
*
seq
,
instr_sequence
*
annotations
)
{
assert
(
seq
->
s_annotations_code
==
NULL
);
seq
->
s_annotations_code
=
annotations
;
return
SUCCESS
;
}
int
_PyInstructionSequence_AddNested
(
instr_sequence
*
seq
,
instr_sequence
*
nested
)
{
if
(
seq
->
s_nested
==
NULL
) {
seq
->
s_nested
=
PyList_New
(
0
);
if
(
seq
->
s_nested
==
NULL
) {
return
ERROR
;
}
}
if
(
PyList_Append
(
seq
->
s_nested
, (
PyObject
*
)
nested
)
<
0
) {
return
ERROR
;
}
return
SUCCESS
;
}
void
PyInstructionSequence_Fini
(
instr_sequence
*
seq
) {
Py_XDECREF
(
seq
->
s_nested
);
PyMem_Free
(
seq
->
s_labelmap
);
seq
->
s_labelmap
=
NULL
;
PyMem_Free
(
seq
->
s_instrs
);
seq
->
s_instrs
=
NULL
;
if
(
seq
->
s_annotations_code
!=
NULL
) {
PyInstructionSequence_Fini
(
seq
->
s_annotations_code
);
Py_CLEAR
(
seq
->
s_annotations_code
);
}
}
/*[clinic input]
class InstructionSequenceType "_PyInstructionSequence *" "&_PyInstructionSequence_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=589963e07480390f]*/
static
_PyInstructionSequence
*
inst_seq_create
(
void
)
{
_PyInstructionSequence
*
seq
;
seq
=
PyObject_GC_New
(
_PyInstructionSequence
,
&
_PyInstructionSequence_Type
);
if
(
seq
==
NULL
) {
return
NULL
;
}
seq
->
s_instrs
=
NULL
;
seq
->
s_allocated
=
0
;
seq
->
s_used
=
0
;
seq
->
s_next_free_label
=
0
;
seq
->
s_labelmap
=
NULL
;
seq
->
s_labelmap_size
=
0
;
seq
->
s_nested
=
NULL
;
seq
->
s_annotations_code
=
NULL
;
PyObject_GC_Track
(
seq
);
return
seq
;
}
PyObject
*
_PyInstructionSequence_New
(
void
)
{
_PyInstructionSequence
*
seq
=
inst_seq_create
();
if
(
seq
==
NULL
) {
return
NULL
;
}
return
(
PyObject
*
)
seq
;
}
/*[clinic input]
@classmethod
InstructionSequenceType.__new__ as inst_seq_new
Create a new InstructionSequence object.
[clinic start generated code]*/
static
PyObject
*
inst_seq_new_impl
(
PyTypeObject
*
type
)
/*[clinic end generated code: output=98881de92c8876f6 input=b393150146849c74]*/
{
return
(
PyObject
*
)
inst_seq_create
();
}
/*[clinic input]
InstructionSequenceType.use_label
label: int
Place label at current location.
[clinic start generated code]*/
static
PyObject
*
InstructionSequenceType_use_label_impl
(
_PyInstructionSequence
*
self
,
int
label
)
/*[clinic end generated code: output=4c06bbacb2854755 input=da55f49bb91841f3]*/
{
if
(
_PyInstructionSequence_UseLabel
(
self
,
label
)
<
0
) {
return
NULL
;
}
Py_RETURN_NONE
;
}
/*[clinic input]
InstructionSequenceType.addop
opcode: int
oparg: int
lineno: int
col_offset: int
end_lineno: int
end_col_offset: int
Append an instruction.
[clinic start generated code]*/
static
PyObject
*
InstructionSequenceType_addop_impl
(
_PyInstructionSequence
*
self
,
int
opcode
,
int
oparg
,
int
lineno
,
int
col_offset
,
int
end_lineno
,
int
end_col_offset
)
/*[clinic end generated code: output=af0cc22c048dfbf3 input=012762ac88198713]*/
{
_Py_SourceLocation
loc
=
{
lineno
,
col_offset
,
end_lineno
,
end_col_offset
};
if
(
_PyInstructionSequence_Addop
(
self
,
opcode
,
oparg
,
loc
)
<
0
) {
return
NULL
;
}
Py_RETURN_NONE
;
}
/*[clinic input]
InstructionSequenceType.new_label -> int
Return a new label.
[clinic start generated code]*/
static
int
InstructionSequenceType_new_label_impl
(
_PyInstructionSequence
*
self
)
/*[clinic end generated code: output=dcb0589e4f5bf4bd input=c66040b9897bc327]*/
{
_PyJumpTargetLabel
lbl
=
_PyInstructionSequence_NewLabel
(
self
);
return
lbl
.
id
;
}
/*[clinic input]
InstructionSequenceType.add_nested
nested: object
Add a nested sequence.
[clinic start generated code]*/
static
PyObject
*
InstructionSequenceType_add_nested_impl
(
_PyInstructionSequence
*
self
,
PyObject
*
nested
)
/*[clinic end generated code: output=14540fad459f7971 input=f2c482568b3b3c0f]*/
{
if
(!
_PyInstructionSequence_Check
(
nested
)) {
PyErr_Format
(
PyExc_TypeError
,
"expected an instruction sequence, not %T"
,
Py_TYPE
(
nested
));
return
NULL
;
}
if
(
_PyInstructionSequence_AddNested
(
self
, (
_PyInstructionSequence
*
)
nested
)
<
0
) {
return
NULL
;
}
Py_RETURN_NONE
;
}
/*[clinic input]
InstructionSequenceType.get_nested
Add a nested sequence.
[clinic start generated code]*/
static
PyObject
*
InstructionSequenceType_get_nested_impl
(
_PyInstructionSequence
*
self
)
/*[clinic end generated code: output=f415112c292630cb input=e429e474c57b95b4]*/
{
if
(
self
->
s_nested
==
NULL
) {
return
PyList_New
(
0
);
}
return
Py_NewRef
(
self
->
s_nested
);
}
/*[clinic input]
InstructionSequenceType.get_instructions
Return the instructions as a list of tuples or labels.
[clinic start generated code]*/
static
PyObject
*
InstructionSequenceType_get_instructions_impl
(
_PyInstructionSequence
*
self
)
/*[clinic end generated code: output=23f4f3f894c301b3 input=fbadb5dadb611291]*/
{
if
(
_PyInstructionSequence_ApplyLabelMap
(
self
)
<
0
) {
return
NULL
;
}
PyObject
*
instructions
=
PyList_New
(
0
);
if
(
instructions
==
NULL
) {
return
NULL
;
}
for
(
int
i
=
0
;
i
<
self
->
s_used
;
i
++
) {
instruction
*
instr
=
&
self
->
s_instrs
[
i
];
location
loc
=
instr
->
i_loc
;
PyObject
*
inst_tuple
;
if
(
OPCODE_HAS_ARG
(
instr
->
i_opcode
)) {
inst_tuple
=
Py_BuildValue
(
"(iiiiii)"
,
instr
->
i_opcode
,
instr
->
i_oparg
,
loc
.
lineno
,
loc
.
end_lineno
,
loc
.
col_offset
,
loc
.
end_col_offset
);
}
else
{
inst_tuple
=
Py_BuildValue
(
"(iOiiii)"
,
instr
->
i_opcode
,
Py_None
,
loc
.
lineno
,
loc
.
end_lineno
,
loc
.
col_offset
,
loc
.
end_col_offset
);
}
if
(
inst_tuple
==
NULL
) {
goto
error
;
}
int
res
=
PyList_Append
(
instructions
,
inst_tuple
);
Py_DECREF
(
inst_tuple
);
if
(
res
!=
0
) {
goto
error
;
}
}
return
instructions
;
error
:
Py_XDECREF
(
instructions
);
return
NULL
;
}
static
PyMethodDef
inst_seq_methods
[]
=
{
INSTRUCTIONSEQUENCETYPE_ADDOP_METHODDEF
INSTRUCTIONSEQUENCETYPE_NEW_LABEL_METHODDEF
INSTRUCTIONSEQUENCETYPE_USE_LABEL_METHODDEF
INSTRUCTIONSEQUENCETYPE_ADD_NESTED_METHODDEF
INSTRUCTIONSEQUENCETYPE_GET_NESTED_METHODDEF
INSTRUCTIONSEQUENCETYPE_GET_INSTRUCTIONS_METHODDEF
{
NULL
,
NULL
,
0
,
NULL
},
};
static
PyMemberDef
inst_seq_memberlist
[]
=
{
{
NULL
}
/* Sentinel */
};
static
PyGetSetDef
inst_seq_getsetters
[]
=
{
{
NULL
}
/* Sentinel */
};
static
void
inst_seq_dealloc
(
PyObject
*
op
)
{
_PyInstructionSequence
*
seq
=
(
_PyInstructionSequence
*
)
op
;
PyObject_GC_UnTrack
(
seq
);
PyInstructionSequence_Fini
(
seq
);
PyObject_GC_Del
(
seq
);
}
static
int
inst_seq_traverse
(
PyObject
*
op
,
visitproc
visit
,
void
*
arg
)
{
_PyInstructionSequence
*
seq
=
(
_PyInstructionSequence
*
)
op
;
Py_VISIT
(
seq
->
s_nested
);
Py_VISIT
((
PyObject
*
)
seq
->
s_annotations_code
);
return
0
;
}
static
int
inst_seq_clear
(
PyObject
*
op
)
{
_PyInstructionSequence
*
seq
=
(
_PyInstructionSequence
*
)
op
;
Py_CLEAR
(
seq
->
s_nested
);
Py_CLEAR
(
seq
->
s_annotations_code
);
return
0
;
}
PyTypeObject
_PyInstructionSequence_Type
=
{
PyVarObject_HEAD_INIT
(
&
PyType_Type
,
0
)
"InstructionSequence"
,
sizeof
(
_PyInstructionSequence
),
0
,
inst_seq_dealloc
,
/*tp_dealloc*/
0
,
/*tp_vectorcall_offset*/
0
,
/*tp_getattr*/
0
,
/*tp_setattr*/
0
,
/*tp_as_async*/
0
,
/*tp_repr*/
0
,
/*tp_as_number*/
0
,
/*tp_as_sequence*/
0
,
/*tp_as_mapping*/
0
,
/* tp_hash */
0
,
/* tp_call */
0
,
/* tp_str */
PyObject_GenericGetAttr
,
/* tp_getattro */
0
,
/* tp_setattro */
0
,
/* tp_as_buffer */
Py_TPFLAGS_DEFAULT
|
Py_TPFLAGS_HAVE_GC
,
/* tp_flags */
inst_seq_new__doc__
,
/* tp_doc */
inst_seq_traverse
,
/* tp_traverse */
inst_seq_clear
,
/* tp_clear */
0
,
/* tp_richcompare */
0
,
/* tp_weaklistoffset */
0
,
/* tp_iter */
0
,
/* tp_iternext */
inst_seq_methods
,
/* tp_methods */
inst_seq_memberlist
,
/* tp_members */
inst_seq_getsetters
,
/* tp_getset */
0
,
/* tp_base */
0
,
/* tp_dict */
0
,
/* tp_descr_get */
0
,
/* tp_descr_set */
0
,
/* tp_dictoffset */
0
,
/* tp_init */
0
,
/* tp_alloc */
inst_seq_new
,
/* tp_new */
};
Footer
© 2026 GitHub, Inc.
Footer navigation
Terms
Privacy
Security
Status
Community
Docs
Contact
Manage cookies
Do not share my personal information
You cant perform that action at this time.
Web Proxy Viewer |
New URL
|
Original Page