File indexing completed on 2025-10-31 09:11:12
0001 #ifndef Py_INTERNAL_CFG_H
0002 #define Py_INTERNAL_CFG_H
0003 #ifdef __cplusplus
0004 extern "C" {
0005 #endif
0006 
0007 #ifndef Py_BUILD_CORE
0008 #  error "this header requires Py_BUILD_CORE define"
0009 #endif
0010 
0011 #include "pycore_opcode_utils.h"
0012 #include "pycore_compile.h"
0013 
0014 
0015 typedef struct {
0016     int i_opcode;
0017     int i_oparg;
0018     _PyCompilerSrcLocation i_loc;
0019     struct _PyCfgBasicblock_ *i_target; 
0020     struct _PyCfgBasicblock_ *i_except; 
0021 } _PyCfgInstruction;
0022 
0023 typedef struct {
0024     int id;
0025 } _PyCfgJumpTargetLabel;
0026 
0027 
0028 typedef struct {
0029     struct _PyCfgBasicblock_ *handlers[CO_MAXBLOCKS+2];
0030     int depth;
0031 } _PyCfgExceptStack;
0032 
0033 typedef struct _PyCfgBasicblock_ {
0034     
0035 
0036 
0037 
0038     struct _PyCfgBasicblock_ *b_list;
0039     
0040     _PyCfgJumpTargetLabel b_label;
0041     
0042     _PyCfgExceptStack *b_exceptstack;
0043     
0044     _PyCfgInstruction *b_instr;
0045     
0046 
0047     struct _PyCfgBasicblock_ *b_next;
0048     
0049     int b_iused;
0050     
0051     int b_ialloc;
0052     
0053     uint64_t b_unsafe_locals_mask;
0054     
0055     int b_predecessors;
0056     
0057     int b_startdepth;
0058     
0059     int b_offset;
0060     
0061     unsigned b_preserve_lasti : 1;
0062     
0063     unsigned b_visited : 1;
0064     
0065     unsigned b_except_handler : 1;
0066     
0067     unsigned b_cold : 1;
0068     
0069     unsigned b_warm : 1;
0070 } _PyCfgBasicblock;
0071 
0072 int _PyBasicblock_InsertInstruction(_PyCfgBasicblock *block, int pos, _PyCfgInstruction *instr);
0073 
0074 typedef struct cfg_builder_ {
0075     
0076 
0077     _PyCfgBasicblock *g_entryblock;
0078     
0079 
0080     _PyCfgBasicblock *g_block_list;
0081     
0082     _PyCfgBasicblock *g_curblock;
0083     
0084     _PyCfgJumpTargetLabel g_current_label;
0085 } _PyCfgBuilder;
0086 
0087 int _PyCfgBuilder_UseLabel(_PyCfgBuilder *g, _PyCfgJumpTargetLabel lbl);
0088 int _PyCfgBuilder_Addop(_PyCfgBuilder *g, int opcode, int oparg, _PyCompilerSrcLocation loc);
0089 
0090 int _PyCfgBuilder_Init(_PyCfgBuilder *g);
0091 void _PyCfgBuilder_Fini(_PyCfgBuilder *g);
0092 
0093 _PyCfgInstruction* _PyCfg_BasicblockLastInstr(const _PyCfgBasicblock *b);
0094 int _PyCfg_OptimizeCodeUnit(_PyCfgBuilder *g, PyObject *consts, PyObject *const_cache,
0095                             int code_flags, int nlocals, int nparams, int firstlineno);
0096 int _PyCfg_Stackdepth(_PyCfgBasicblock *entryblock, int code_flags);
0097 void _PyCfg_ConvertPseudoOps(_PyCfgBasicblock *entryblock);
0098 int _PyCfg_ResolveJumps(_PyCfgBuilder *g);
0099 
0100 
0101 static inline int
0102 basicblock_nofallthrough(const _PyCfgBasicblock *b) {
0103     _PyCfgInstruction *last = _PyCfg_BasicblockLastInstr(b);
0104     return (last &&
0105             (IS_SCOPE_EXIT_OPCODE(last->i_opcode) ||
0106              IS_UNCONDITIONAL_JUMP_OPCODE(last->i_opcode)));
0107 }
0108 
0109 #define BB_NO_FALLTHROUGH(B) (basicblock_nofallthrough(B))
0110 #define BB_HAS_FALLTHROUGH(B) (!basicblock_nofallthrough(B))
0111 
0112 PyCodeObject *
0113 _PyAssemble_MakeCodeObject(_PyCompile_CodeUnitMetadata *u, PyObject *const_cache,
0114                            PyObject *consts, int maxdepth, _PyCompile_InstructionSequence *instrs,
0115                            int nlocalsplus, int code_flags, PyObject *filename);
0116 
0117 #ifdef __cplusplus
0118 }
0119 #endif
0120 #endif