Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Include/internal/pycore_flowgraph.h
12 views
1
#ifndef Py_INTERNAL_CFG_H
2
#define Py_INTERNAL_CFG_H
3
#ifdef __cplusplus
4
extern "C" {
5
#endif
6
7
#ifndef Py_BUILD_CORE
8
# error "this header requires Py_BUILD_CORE define"
9
#endif
10
11
#include "pycore_opcode_utils.h"
12
#include "pycore_compile.h"
13
14
15
typedef struct {
16
int i_opcode;
17
int i_oparg;
18
_PyCompilerSrcLocation i_loc;
19
struct _PyCfgBasicblock_ *i_target; /* target block (if jump instruction) */
20
struct _PyCfgBasicblock_ *i_except; /* target block when exception is raised */
21
} _PyCfgInstruction;
22
23
typedef struct {
24
int id;
25
} _PyCfgJumpTargetLabel;
26
27
28
typedef struct {
29
struct _PyCfgBasicblock_ *handlers[CO_MAXBLOCKS+1];
30
int depth;
31
} _PyCfgExceptStack;
32
33
typedef struct _PyCfgBasicblock_ {
34
/* Each basicblock in a compilation unit is linked via b_list in the
35
reverse order that the block are allocated. b_list points to the next
36
block in this list, not to be confused with b_next, which is next by
37
control flow. */
38
struct _PyCfgBasicblock_ *b_list;
39
/* The label of this block if it is a jump target, -1 otherwise */
40
_PyCfgJumpTargetLabel b_label;
41
/* Exception stack at start of block, used by assembler to create the exception handling table */
42
_PyCfgExceptStack *b_exceptstack;
43
/* pointer to an array of instructions, initially NULL */
44
_PyCfgInstruction *b_instr;
45
/* If b_next is non-NULL, it is a pointer to the next
46
block reached by normal control flow. */
47
struct _PyCfgBasicblock_ *b_next;
48
/* number of instructions used */
49
int b_iused;
50
/* length of instruction array (b_instr) */
51
int b_ialloc;
52
/* Used by add_checks_for_loads_of_unknown_variables */
53
uint64_t b_unsafe_locals_mask;
54
/* Number of predecessors that a block has. */
55
int b_predecessors;
56
/* depth of stack upon entry of block, computed by stackdepth() */
57
int b_startdepth;
58
/* Basic block is an exception handler that preserves lasti */
59
unsigned b_preserve_lasti : 1;
60
/* Used by compiler passes to mark whether they have visited a basic block. */
61
unsigned b_visited : 1;
62
/* b_except_handler is used by the cold-detection algorithm to mark exception targets */
63
unsigned b_except_handler : 1;
64
/* b_cold is true if this block is not perf critical (like an exception handler) */
65
unsigned b_cold : 1;
66
/* b_warm is used by the cold-detection algorithm to mark blocks which are definitely not cold */
67
unsigned b_warm : 1;
68
} _PyCfgBasicblock;
69
70
int _PyBasicblock_InsertInstruction(_PyCfgBasicblock *block, int pos, _PyCfgInstruction *instr);
71
72
typedef struct cfg_builder_ {
73
/* The entryblock, at which control flow begins. All blocks of the
74
CFG are reachable through the b_next links */
75
_PyCfgBasicblock *g_entryblock;
76
/* Pointer to the most recently allocated block. By following
77
b_list links, you can reach all allocated blocks. */
78
_PyCfgBasicblock *g_block_list;
79
/* pointer to the block currently being constructed */
80
_PyCfgBasicblock *g_curblock;
81
/* label for the next instruction to be placed */
82
_PyCfgJumpTargetLabel g_current_label;
83
} _PyCfgBuilder;
84
85
int _PyCfgBuilder_UseLabel(_PyCfgBuilder *g, _PyCfgJumpTargetLabel lbl);
86
int _PyCfgBuilder_Addop(_PyCfgBuilder *g, int opcode, int oparg, _PyCompilerSrcLocation loc);
87
88
int _PyCfgBuilder_Init(_PyCfgBuilder *g);
89
void _PyCfgBuilder_Fini(_PyCfgBuilder *g);
90
91
_PyCfgInstruction* _PyCfg_BasicblockLastInstr(const _PyCfgBasicblock *b);
92
int _PyCfg_OptimizeCodeUnit(_PyCfgBuilder *g, PyObject *consts, PyObject *const_cache,
93
int code_flags, int nlocals, int nparams, int firstlineno);
94
int _PyCfg_Stackdepth(_PyCfgBasicblock *entryblock, int code_flags);
95
void _PyCfg_ConvertPseudoOps(_PyCfgBasicblock *entryblock);
96
int _PyCfg_ResolveJumps(_PyCfgBuilder *g);
97
98
99
static inline int
100
basicblock_nofallthrough(const _PyCfgBasicblock *b) {
101
_PyCfgInstruction *last = _PyCfg_BasicblockLastInstr(b);
102
return (last &&
103
(IS_SCOPE_EXIT_OPCODE(last->i_opcode) ||
104
IS_UNCONDITIONAL_JUMP_OPCODE(last->i_opcode)));
105
}
106
107
#define BB_NO_FALLTHROUGH(B) (basicblock_nofallthrough(B))
108
#define BB_HAS_FALLTHROUGH(B) (!basicblock_nofallthrough(B))
109
110
PyCodeObject *
111
_PyAssemble_MakeCodeObject(_PyCompile_CodeUnitMetadata *u, PyObject *const_cache,
112
PyObject *consts, int maxdepth, _PyCompile_InstructionSequence *instrs,
113
int nlocalsplus, int code_flags, PyObject *filename);
114
115
#ifdef __cplusplus
116
}
117
#endif
118
#endif /* !Py_INTERNAL_CFG_H */
119
120