/* AST Optimizer */
#include "Python.h"
#include "pycore_ast.h" // _PyAST_GetDocString()
-#include "pycore_compile.h" // _PyASTOptimizeState
#include "pycore_long.h" // _PyLong
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_format.h" // F_LJUST
+typedef struct {
+ int optimize;
+ int ff_features;
+
+ int recursion_depth; /* current recursion depth */
+ int recursion_limit; /* recursion limit */
+} _PyASTOptimizeState;
+
+
static int
make_const(expr_ty node, PyObject *val, PyArena *arena)
{
#define COMPILER_STACK_FRAME_SCALE 3
int
-_PyAST_Optimize(mod_ty mod, PyArena *arena, _PyASTOptimizeState *state)
+_PyAST_Optimize(mod_ty mod, PyArena *arena, int optimize, int ff_features)
{
PyThreadState *tstate;
int starting_recursion_depth;
+ _PyASTOptimizeState state;
+ state.optimize = optimize;
+ state.ff_features = ff_features;
+
/* Setup recursion depth check counters */
tstate = _PyThreadState_GET();
if (!tstate) {
/* Be careful here to prevent overflow. */
int recursion_depth = C_RECURSION_LIMIT - tstate->c_recursion_remaining;
starting_recursion_depth = recursion_depth * COMPILER_STACK_FRAME_SCALE;
- state->recursion_depth = starting_recursion_depth;
- state->recursion_limit = C_RECURSION_LIMIT * COMPILER_STACK_FRAME_SCALE;
+ state.recursion_depth = starting_recursion_depth;
+ state.recursion_limit = C_RECURSION_LIMIT * COMPILER_STACK_FRAME_SCALE;
- int ret = astfold_mod(mod, arena, state);
+ int ret = astfold_mod(mod, arena, &state);
assert(ret || PyErr_Occurred());
/* Check that the recursion depth counting balanced correctly */
- if (ret && state->recursion_depth != starting_recursion_depth) {
+ if (ret && state.recursion_depth != starting_recursion_depth) {
PyErr_Format(PyExc_SystemError,
"AST optimizer recursion depth mismatch (before=%d, after=%d)",
- starting_recursion_depth, state->recursion_depth);
+ starting_recursion_depth, state.recursion_depth);
return 0;
}
c->c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
c->c_nestlevel = 0;
- _PyASTOptimizeState state;
- state.optimize = c->c_optimize;
- state.ff_features = merged;
-
- if (!_PyAST_Optimize(mod, arena, &state)) {
+ if (!_PyAST_Optimize(mod, arena, c->c_optimize, merged)) {
return ERROR;
}
c->c_st = _PySymtable_Build(mod, filename, &c->c_future);
PyCompilerFlags flags = _PyCompilerFlags_INIT;
- _PyASTOptimizeState state;
- state.optimize = _Py_GetConfig()->optimization_level;
- state.ff_features = 0;
-
mod_ty module = _PyParser_ASTFromString(segment_str, filename, Py_file_input,
&flags, arena);
if (!module) {
goto done;
}
- if (!_PyAST_Optimize(module, arena, &state)) {
+ if (!_PyAST_Optimize(module, arena, _Py_GetConfig()->optimization_level, 0)) {
goto done;
}