3
3
#include "pycore_interp.h"
4
4
#include "pycore_opcode.h"
5
5
#include "opcode_metadata.h"
6
+ #include "pycore_opcode_utils.h"
6
7
#include "pycore_pystate.h" // _PyInterpreterState_GET()
7
8
#include "pycore_uops.h"
8
9
#include "cpython/optimizer.h"
9
10
#include <stdbool.h>
10
11
#include <stdint.h>
11
12
#include <stddef.h>
12
13
14
+ #define MAX_EXECUTORS_SIZE 256
15
+
13
16
static bool
14
17
has_space_for_executor (PyCodeObject * code , _Py_CODEUNIT * instr )
15
18
{
@@ -19,7 +22,7 @@ has_space_for_executor(PyCodeObject *code, _Py_CODEUNIT *instr)
19
22
if (code -> co_executors == NULL ) {
20
23
return true;
21
24
}
22
- return code -> co_executors -> size < 256 ;
25
+ return code -> co_executors -> size < MAX_EXECUTORS_SIZE ;
23
26
}
24
27
25
28
static int32_t
@@ -34,7 +37,7 @@ get_index_for_executor(PyCodeObject *code, _Py_CODEUNIT *instr)
34
37
if (old != NULL ) {
35
38
size = old -> size ;
36
39
capacity = old -> capacity ;
37
- assert (size < 256 );
40
+ assert (size < MAX_EXECUTORS_SIZE );
38
41
}
39
42
assert (size <= capacity );
40
43
if (size == capacity ) {
@@ -74,7 +77,7 @@ insert_executor(PyCodeObject *code, _Py_CODEUNIT *instr, int index, _PyExecutorO
74
77
executor -> vm_data .opcode = instr -> op .code ;
75
78
executor -> vm_data .oparg = instr -> op .arg ;
76
79
code -> co_executors -> executors [index ] = executor ;
77
- assert (index < 256 );
80
+ assert (index < MAX_EXECUTORS_SIZE );
78
81
instr -> op .code = ENTER_EXECUTOR ;
79
82
instr -> op .arg = index ;
80
83
code -> co_executors -> size ++ ;
@@ -307,7 +310,7 @@ uop_dealloc(_PyUOpExecutorObject *self) {
307
310
308
311
static const char *
309
312
uop_name (int index ) {
310
- if (index < 256 ) {
313
+ if (index <= MAX_REAL_OPCODE ) {
311
314
return _PyOpcode_OpName [index ];
312
315
}
313
316
return _PyOpcode_uop_name [index ];
@@ -391,17 +394,20 @@ translate_bytecode_to_trace(
391
394
#define ADD_TO_TRACE (OPCODE , OPERAND ) \
392
395
DPRINTF(2, \
393
396
" ADD_TO_TRACE(%s, %" PRIu64 ")\n", \
394
- (OPCODE) < 256 ? _PyOpcode_OpName[(OPCODE)] : _PyOpcode_uop_name[(OPCODE)] , \
397
+ uop_name (OPCODE), \
395
398
(uint64_t)(OPERAND)); \
396
399
assert(trace_length < max_length); \
397
400
trace[trace_length].opcode = (OPCODE); \
398
401
trace[trace_length].operand = (OPERAND); \
399
402
trace_length++;
400
403
404
+ #define INSTR_IP (INSTR , CODE ) \
405
+ ((long)((INSTR) - ((_Py_CODEUNIT *)(CODE)->co_code_adaptive)))
406
+
401
407
#define ADD_TO_STUB (INDEX , OPCODE , OPERAND ) \
402
408
DPRINTF(2, " ADD_TO_STUB(%d, %s, %" PRIu64 ")\n", \
403
409
(INDEX), \
404
- (OPCODE) < 256 ? _PyOpcode_OpName[(OPCODE)] : _PyOpcode_uop_name[(OPCODE)] , \
410
+ uop_name (OPCODE), \
405
411
(uint64_t)(OPERAND)); \
406
412
trace[(INDEX)].opcode = (OPCODE); \
407
413
trace[(INDEX)].operand = (OPERAND);
@@ -411,10 +417,10 @@ translate_bytecode_to_trace(
411
417
PyUnicode_AsUTF8 (code -> co_qualname ),
412
418
PyUnicode_AsUTF8 (code -> co_filename ),
413
419
code -> co_firstlineno ,
414
- 2 * ( long )( initial_instr - ( _Py_CODEUNIT * ) code -> co_code_adaptive ));
420
+ 2 * INSTR_IP ( initial_instr , code ));
415
421
416
422
for (;;) {
417
- ADD_TO_TRACE (SAVE_IP , instr - ( _Py_CODEUNIT * ) code -> co_code_adaptive );
423
+ ADD_TO_TRACE (SAVE_IP , INSTR_IP ( instr , code ) );
418
424
int opcode = instr -> op .code ;
419
425
int oparg = instr -> op .arg ;
420
426
int extras = 0 ;
@@ -448,8 +454,7 @@ translate_bytecode_to_trace(
448
454
int uopcode = opcode == POP_JUMP_IF_TRUE ?
449
455
_POP_JUMP_IF_TRUE : _POP_JUMP_IF_FALSE ;
450
456
ADD_TO_TRACE (uopcode , max_length );
451
- ADD_TO_STUB (max_length , SAVE_IP ,
452
- target_instr - (_Py_CODEUNIT * )code -> co_code_adaptive );
457
+ ADD_TO_STUB (max_length , SAVE_IP , INSTR_IP (target_instr , code ));
453
458
ADD_TO_STUB (max_length + 1 , EXIT_TRACE , 0 );
454
459
break ;
455
460
}
@@ -474,9 +479,7 @@ translate_bytecode_to_trace(
474
479
// Reserve space for nuops (+ SAVE_IP + EXIT_TRACE)
475
480
int nuops = expansion -> nuops ;
476
481
if (trace_length + nuops + 2 > max_length ) {
477
- DPRINTF (1 ,
478
- "Ran out of space for %s\n" ,
479
- opcode < 256 ? _PyOpcode_OpName [opcode ] : _PyOpcode_uop_name [opcode ]);
482
+ DPRINTF (1 , "Ran out of space for %s\n" , uop_name (opcode ));
480
483
goto done ;
481
484
}
482
485
for (int i = 0 ; i < nuops ; i ++ ) {
@@ -522,9 +525,7 @@ translate_bytecode_to_trace(
522
525
}
523
526
break ;
524
527
}
525
- DPRINTF (2 ,
526
- "Unsupported opcode %s\n" ,
527
- opcode < 256 ? _PyOpcode_OpName [opcode ] : _PyOpcode_uop_name [opcode ]);
528
+ DPRINTF (2 , "Unsupported opcode %s\n" , uop_name (opcode ));
528
529
goto done ; // Break out of loop
529
530
}
530
531
}
@@ -542,7 +543,7 @@ translate_bytecode_to_trace(
542
543
PyUnicode_AsUTF8 (code -> co_qualname ),
543
544
PyUnicode_AsUTF8 (code -> co_filename ),
544
545
code -> co_firstlineno ,
545
- 2 * ( long )( initial_instr - ( _Py_CODEUNIT * ) code -> co_code_adaptive ),
546
+ 2 * INSTR_IP ( initial_instr , code ),
546
547
trace_length );
547
548
if (max_length < buffer_size && trace_length < max_length ) {
548
549
// Move the stubs back to be immediately after the main trace
@@ -576,7 +577,7 @@ translate_bytecode_to_trace(
576
577
PyUnicode_AsUTF8 (code -> co_qualname ),
577
578
PyUnicode_AsUTF8 (code -> co_filename ),
578
579
code -> co_firstlineno ,
579
- 2 * ( long )( initial_instr - ( _Py_CODEUNIT * ) code -> co_code_adaptive ));
580
+ 2 * INSTR_IP ( initial_instr , code ));
580
581
}
581
582
return 0 ;
582
583
0 commit comments