Update comments, remove commented out code.

Move assembler structure next to assembler code to make it easier to
move it to a separate file.
This commit is contained in:
Neal Norwitz 2006-10-29 18:30:10 +00:00
parent 7a4e804469
commit f733a013b2
1 changed files with 31 additions and 87 deletions

View File

@ -9,6 +9,7 @@
* 3. Generate code for basic blocks. See compiler_mod() in this file.
* 4. Assemble the basic blocks into final code. See assemble() in
* this file.
* 5. Optimize the byte code (peephole optimizations). See peephole.c
*
* Note that compiler_mod() suggests module, but the module ast type
* (mod_ty) has cases for expressions and interactive statements.
@ -16,7 +17,8 @@
* CAUTION: The VISIT_* macros abort the current function when they
* encounter a problem. So don't invoke them when there is memory
* which needs to be released. Code blocks are OK, as the compiler
* structure takes care of releasing those.
* structure takes care of releasing those. Use the arena to manage
* objects.
*/
#include "Python.h"
@ -32,16 +34,6 @@
int Py_OptimizeFlag = 0;
/*
ISSUES:
opcode_stack_effect() function should be reviewed since stack depth bugs
could be really hard to find later.
Dead code is being generated (i.e. after unconditional jumps).
XXX(nnorwitz): not sure this is still true
*/
#define DEFAULT_BLOCK_SIZE 16
#define DEFAULT_BLOCKS 8
#define DEFAULT_CODE_SIZE 128
@ -152,17 +144,6 @@ struct compiler {
PyArena *c_arena; /* pointer to memory allocation arena */
};
struct assembler {
PyObject *a_bytecode; /* string containing bytecode */
int a_offset; /* offset into bytecode */
int a_nblocks; /* number of reachable blocks */
basicblock **a_postorder; /* list of blocks in dfs postorder */
PyObject *a_lnotab; /* string containing lnotab */
int a_lnotab_off; /* offset into lnotab */
int a_lineno; /* last lineno of emitted instruction */
int a_lineno_off; /* bytecode offset of last lineno */
};
static int compiler_enter_scope(struct compiler *, identifier, void *, int);
static void compiler_free(struct compiler *);
static basicblock *compiler_new_block(struct compiler *);
@ -396,47 +377,6 @@ dictbytype(PyObject *src, int scope_type, int flag, int offset)
return dest;
}
/*
Leave this debugging code for just a little longer.
static void
compiler_display_symbols(PyObject *name, PyObject *symbols)
{
PyObject *key, *value;
int flags;
Py_ssize_t pos = 0;
fprintf(stderr, "block %s\n", PyString_AS_STRING(name));
while (PyDict_Next(symbols, &pos, &key, &value)) {
flags = PyInt_AsLong(value);
fprintf(stderr, "var %s:", PyString_AS_STRING(key));
if (flags & DEF_GLOBAL)
fprintf(stderr, " declared_global");
if (flags & DEF_LOCAL)
fprintf(stderr, " local");
if (flags & DEF_PARAM)
fprintf(stderr, " param");
if (flags & DEF_STAR)
fprintf(stderr, " stararg");
if (flags & DEF_DOUBLESTAR)
fprintf(stderr, " starstar");
if (flags & DEF_INTUPLE)
fprintf(stderr, " tuple");
if (flags & DEF_FREE)
fprintf(stderr, " free");
if (flags & DEF_FREE_GLOBAL)
fprintf(stderr, " global");
if (flags & DEF_FREE_CLASS)
fprintf(stderr, " free/class");
if (flags & DEF_IMPORT)
fprintf(stderr, " import");
fprintf(stderr, "\n");
}
fprintf(stderr, "\n");
}
*/
static void
compiler_unit_check(struct compiler_unit *u)
{
@ -1055,8 +995,8 @@ compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
from the current block to the new block.
*/
/* XXX The returns inside these macros make it impossible to decref
objects created in the local function.
/* The returns inside these macros make it impossible to decref objects
created in the local function. Local objects should use the arena.
*/
@ -2519,7 +2459,6 @@ compiler_compare(struct compiler *c, expr_ty e)
}
return 1;
}
#undef CMPCAST
static int
compiler_call(struct compiler *c, expr_ty e)
@ -2979,14 +2918,6 @@ compiler_visit_expr(struct compiler *c, expr_ty e)
case Yield_kind:
if (c->u->u_ste->ste_type != FunctionBlock)
return compiler_error(c, "'yield' outside function");
/*
for (i = 0; i < c->u->u_nfblocks; i++) {
if (c->u->u_fblock[i].fb_type == FINALLY_TRY)
return compiler_error(
c, "'yield' not allowed in a 'try' "
"block with a 'finally' clause");
}
*/
if (e->v.Yield.value) {
VISIT(c, expr, e->v.Yield.value);
}
@ -3317,7 +3248,6 @@ compiler_visit_nested_slice(struct compiler *c, slice_ty s,
return 1;
}
static int
compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
{
@ -3365,12 +3295,26 @@ compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
return compiler_handle_subscr(c, kindname, ctx);
}
/* End of the compiler section, beginning of the assembler section */
/* do depth-first search of basic block graph, starting with block.
post records the block indices in post-order.
XXX must handle implicit jumps from one block to next
*/
struct assembler {
PyObject *a_bytecode; /* string containing bytecode */
int a_offset; /* offset into bytecode */
int a_nblocks; /* number of reachable blocks */
basicblock **a_postorder; /* list of blocks in dfs postorder */
PyObject *a_lnotab; /* string containing lnotab */
int a_lnotab_off; /* offset into lnotab */
int a_lineno; /* last lineno of emitted instruction */
int a_lineno_off; /* bytecode offset of last lineno */
};
static void
dfs(struct compiler *c, basicblock *b, struct assembler *a)
{