1991-02-19 08:39:46 -04:00
|
|
|
|
1995-07-18 11:21:06 -03:00
|
|
|
/* Definitions for bytecode */
|
1990-12-20 11:06:42 -04:00
|
|
|
|
2000-07-08 21:20:36 -03:00
|
|
|
#ifndef Py_COMPILE_H
|
|
|
|
#define Py_COMPILE_H
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
1995-07-18 11:21:06 -03:00
|
|
|
/* Bytecode object */
|
1990-12-20 11:06:42 -04:00
|
|
|
typedef struct {
|
2000-07-08 21:20:36 -03:00
|
|
|
PyObject_HEAD
|
|
|
|
int co_argcount; /* #arguments, except *args */
|
|
|
|
int co_nlocals; /* #local variables */
|
|
|
|
int co_stacksize; /* #entries needed for evaluation stack */
|
|
|
|
int co_flags; /* CO_..., see below */
|
|
|
|
PyObject *co_code; /* instruction opcodes */
|
|
|
|
PyObject *co_consts; /* list (constants used) */
|
|
|
|
PyObject *co_names; /* list of strings (names used) */
|
|
|
|
PyObject *co_varnames; /* tuple of strings (local variable names) */
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
PyObject *co_freevars; /* tuple of strings (free variable names) */
|
|
|
|
PyObject *co_cellvars; /* tuple of strings (cell variable names) */
|
2000-07-08 21:20:36 -03:00
|
|
|
/* The rest doesn't count for hash/cmp */
|
|
|
|
PyObject *co_filename; /* string (where it was loaded from) */
|
|
|
|
PyObject *co_name; /* string (name, for reference) */
|
|
|
|
int co_firstlineno; /* first source line number */
|
|
|
|
PyObject *co_lnotab; /* string (encoding addr<->lineno mapping) */
|
1995-01-12 07:45:45 -04:00
|
|
|
} PyCodeObject;
|
1990-12-20 11:06:42 -04:00
|
|
|
|
1995-07-18 11:21:06 -03:00
|
|
|
/* Masks for co_flags above */
|
|
|
|
#define CO_OPTIMIZED 0x0001
|
|
|
|
#define CO_NEWLOCALS 0x0002
|
|
|
|
#define CO_VARARGS 0x0004
|
|
|
|
#define CO_VARKEYWORDS 0x0008
|
2001-03-21 22:32:48 -04:00
|
|
|
#define CO_NESTED 0x0010
|
2001-06-18 19:08:13 -03:00
|
|
|
#define CO_GENERATOR 0x0020
|
2001-07-15 23:29:45 -03:00
|
|
|
/* XXX Temporary hack. Until generators are a permanent part of the
|
|
|
|
language, we need a way for a code object to record that generators
|
|
|
|
were *possible* when it was compiled. This is so code dynamically
|
|
|
|
compiled *by* a code object knows whether to allow yield stmts. In
|
|
|
|
effect, this passes on the "from __future__ import generators" state
|
|
|
|
in effect when the code block was compiled. */
|
2002-04-11 22:20:10 -03:00
|
|
|
#define CO_GENERATOR_ALLOWED 0x1000 /* no longer used in an essential way */
|
2001-08-08 02:00:18 -03:00
|
|
|
#define CO_FUTURE_DIVISION 0x2000
|
1995-07-18 11:21:06 -03:00
|
|
|
|
1995-02-27 06:17:52 -04:00
|
|
|
extern DL_IMPORT(PyTypeObject) PyCode_Type;
|
1990-12-20 11:06:42 -04:00
|
|
|
|
1995-01-12 07:45:45 -04:00
|
|
|
#define PyCode_Check(op) ((op)->ob_type == &PyCode_Type)
|
2001-12-13 15:47:02 -04:00
|
|
|
#define PyCode_GetNumFree(op) (PyTuple_GET_SIZE((op)->co_freevars))
|
1990-12-20 11:06:42 -04:00
|
|
|
|
1997-01-17 16:59:26 -04:00
|
|
|
#define CO_MAXBLOCKS 20 /* Max static block nesting within a function */
|
1990-12-20 11:06:42 -04:00
|
|
|
|
|
|
|
/* Public interface */
|
1991-04-03 15:00:55 -04:00
|
|
|
struct _node; /* Declare the existence of this type */
|
2000-07-08 21:20:36 -03:00
|
|
|
DL_IMPORT(PyCodeObject *) PyNode_Compile(struct _node *, char *);
|
|
|
|
DL_IMPORT(PyCodeObject *) PyCode_New(
|
1997-01-17 16:59:26 -04:00
|
|
|
int, int, int, int, PyObject *, PyObject *, PyObject *, PyObject *,
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
PyObject *, PyObject *, PyObject *, PyObject *, int, PyObject *);
|
|
|
|
/* same as struct above */
|
2000-07-08 21:20:36 -03:00
|
|
|
DL_IMPORT(int) PyCode_Addr2Line(PyCodeObject *, int);
|
1993-07-28 06:05:47 -03:00
|
|
|
|
2001-02-27 15:07:02 -04:00
|
|
|
/* Future feature support */
|
|
|
|
|
|
|
|
typedef struct {
|
2001-02-27 21:58:08 -04:00
|
|
|
int ff_found_docstring;
|
2001-02-27 15:07:02 -04:00
|
|
|
int ff_last_lineno;
|
2001-08-10 18:38:04 -03:00
|
|
|
int ff_features;
|
2001-02-27 15:07:02 -04:00
|
|
|
} PyFutureFeatures;
|
|
|
|
|
|
|
|
DL_IMPORT(PyFutureFeatures *) PyNode_Future(struct _node *, char *);
|
2001-03-01 18:59:14 -04:00
|
|
|
DL_IMPORT(PyCodeObject *) PyNode_CompileFlags(struct _node *, char *,
|
|
|
|
PyCompilerFlags *);
|
2001-02-27 15:07:02 -04:00
|
|
|
|
|
|
|
#define FUTURE_NESTED_SCOPES "nested_scopes"
|
2001-07-15 18:08:29 -03:00
|
|
|
#define FUTURE_GENERATORS "generators"
|
2001-08-08 02:00:18 -03:00
|
|
|
#define FUTURE_DIVISION "division"
|
|
|
|
|
1998-10-07 16:42:25 -03:00
|
|
|
/* for internal use only */
|
|
|
|
#define _PyCode_GETCODEPTR(co, pp) \
|
|
|
|
((*(co)->co_code->ob_type->tp_as_buffer->bf_getreadbuffer) \
|
|
|
|
((co)->co_code, 0, (void **)(pp)))
|
|
|
|
|
1993-07-28 06:05:47 -03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* !Py_COMPILE_H */
|