[Python-checkins] python/dist/src/Python newcompile.c,1.1.2.3,1.1.2.4
jhylton@users.sourceforge.net
jhylton@users.sourceforge.net
Fri, 30 Aug 2002 13:25:51 -0700
Update of /cvsroot/python/python/dist/src/Python
In directory usw-pr-cvs1:/tmp/cvs-serv14029/Python
Modified Files:
Tag: ast-branch
newcompile.c
Log Message:
Add arg_is_pyobject(opcode) -- returns true of the opcode has an
argument that is represented internally as a PyObject *.
Add init() function to initialize the compiler structure.
Stick some code in PyAST_Compile() that calls marshal_write_mod() just
to test this code.
Use PyObject_Malloc() / PyObject_Free() for compiler memory.
Fix compiler_new_block() to return -1 on error, since 0 is a perfectly
valid block index. Fix all calls to test for failure using result < 0
instead of result == 0.
Fix addop() functions to use opcode argument.
Add placeholder assemble() function that prints out the contents of
the basic blocks.
Index: newcompile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/Attic/newcompile.c,v
retrieving revision 1.1.2.3
retrieving revision 1.1.2.4
diff -C2 -d -r1.1.2.3 -r1.1.2.4
*** newcompile.c 23 Aug 2002 18:35:41 -0000 1.1.2.3
--- newcompile.c 30 Aug 2002 20:25:49 -0000 1.1.2.4
***************
*** 46,50 ****
static int compiler_enter_scope(struct compiler *, identifier, void *);
- static int compiler_exit_scope(struct compiler *, identifier, void *);
static void compiler_free(struct compiler *);
static PyCodeObject *compiler_get_code(struct compiler *);
--- 46,49 ----
***************
*** 67,70 ****
--- 66,72 ----
static void compiler_pop_fblock(struct compiler *, enum fblocktype, int);
+ static PyCodeObject *assemble(struct compiler *);
+ static int arg_is_pyobject(int);
+
int
_Py_Mangle(char *p, char *name, char *buffer, size_t maxlen)
***************
*** 95,98 ****
--- 97,108 ----
}
+ static void
+ init(struct compiler *c)
+ {
+ c->c_blocks = NULL;
+ c->c_nblocks = 0;
+ c->c_interactive = 0;
+ }
+
PyCodeObject *
PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags)
***************
*** 100,103 ****
--- 110,114 ----
struct compiler c;
+ init(&c);
c.c_filename = filename;
c.c_future = PyFuture_FromAST(mod, filename);
***************
*** 109,122 ****
flags->cf_flags = merged;
}
!
c.c_st = PySymtable_Build(mod, filename, c.c_future);
if (c.c_st == NULL)
goto error;
- return NULL;
! compiler_mod(&c, mod);
error:
compiler_free(&c);
return NULL;
}
--- 120,147 ----
flags->cf_flags = merged;
}
!
! /* Trivial test of marshal code for now. */
! {
! PyObject *buf = PyString_FromStringAndSize(NULL, 1024);
! int offset = 0;
! assert(marshal_write_mod(&buf, &offset, mod));
! if (!_PyString_Resize(&buf, offset) < 0) {
! fprintf(stderr, "resize failed!\n");
! goto error;
! }
! }
!
c.c_st = PySymtable_Build(mod, filename, c.c_future);
if (c.c_st == NULL)
goto error;
! if (!compiler_mod(&c, mod))
! goto error;
+ return assemble(&c);
error:
compiler_free(&c);
+ if (!PyErr_Occurred())
+ PyErr_SetString(PyExc_SystemError, "PyAST_Compile() failed");
return NULL;
}
***************
*** 130,138 ****
PySymtable_Free(c->c_st);
if (c->c_future)
! PyMem_Free((void *)c->c_future);
for (i = 0; i < c->c_nblocks; i++)
! free((void *)c->c_blocks[i]);
if (c->c_blocks)
! free((void *)c->c_blocks);
}
--- 155,163 ----
PySymtable_Free(c->c_st);
if (c->c_future)
! PyObject_Free((void *)c->c_future);
for (i = 0; i < c->c_nblocks; i++)
! PyObject_Free((void *)c->c_blocks[i]);
if (c->c_blocks)
! PyObject_Free((void *)c->c_blocks);
}
***************
*** 141,171 ****
compiler_enter_scope(struct compiler *c, identifier name, void *key)
{
! /* XXX need stack of info */
! PyObject *k, *v;
!
! k = PyLong_FromVoidPtr(key);
! if (k == NULL)
! return 0;
! v = PyDict_GetItem(c->c_st->st_symbols, k);
! if (!v) {
! /* XXX */
! PyErr_SetObject(PyExc_KeyError, name);
return 0;
! }
! assert(PySTEntry_Check(v));
! c->c_ste = (PySTEntryObject *)v;
!
c->c_nblocks = 0;
! c->c_blocks = (struct basicblock **)malloc(sizeof(struct basicblock *)
! * DEFAULT_BLOCKS);
if (!c->c_blocks)
return 0;
! return 1;
! }
!
! static int
! compiler_exit_scope(struct compiler *c, identifier name, void *key)
! {
! /* pop current scope off stack & reinit */
return 1;
}
--- 166,181 ----
compiler_enter_scope(struct compiler *c, identifier name, void *key)
{
! c->c_ste = PySymtable_Lookup(c->c_st, key);
! if (!c->c_ste) {
return 0;
! }
c->c_nblocks = 0;
! c->c_blocks = (struct basicblock **)PyObject_Malloc(
! sizeof(struct basicblock *) * DEFAULT_BLOCKS);
if (!c->c_blocks)
return 0;
! memset(c->c_blocks, 0, sizeof(struct basicblock *) * DEFAULT_BLOCKS);
! if (compiler_use_new_block(c) < 0)
! return 0;
return 1;
}
***************
*** 177,185 ****
XXX may want to return a thunk instead to allow later passes
*/
return NULL;
}
/* Allocate a new block and return its index in c_blocks.
! Returns 0 on error.
*/
--- 187,196 ----
XXX may want to return a thunk instead to allow later passes
*/
+ PyErr_SetString(PyExc_SystemError, "compiler doesn't work");
return NULL;
}
/* Allocate a new block and return its index in c_blocks.
! Returns -1 on error.
*/
***************
*** 193,203 ****
/* XXX should double */
int newsize = c->c_nblocks + DEFAULT_BLOCKS;
! c->c_blocks = (struct basicblock **)realloc(c->c_blocks,
! newsize);
if (c->c_blocks == NULL)
return 0;
}
i = c->c_nblocks++;
! b = (struct basicblock *)malloc(sizeof(struct basicblock));
if (b == NULL)
return 0;
--- 204,214 ----
/* XXX should double */
int newsize = c->c_nblocks + DEFAULT_BLOCKS;
! c->c_blocks = (struct basicblock **)PyObject_Realloc(c->c_blocks,
! newsize);
if (c->c_blocks == NULL)
return 0;
}
i = c->c_nblocks++;
! b = (struct basicblock *)PyObject_Malloc(sizeof(struct basicblock));
if (b == NULL)
return 0;
***************
*** 219,223 ****
{
int block = compiler_new_block(c);
! if (!block)
return 0;
c->c_curblock = block;
--- 230,234 ----
{
int block = compiler_new_block(c);
! if (block < 0)
return 0;
c->c_curblock = block;
***************
*** 240,245 ****
int newsize;
b->b_ialloc *= 2;
! /* XXX newsize is wrong */
! ptr = realloc((void *)b, newsize);
if (ptr == NULL)
return -1;
--- 251,258 ----
int newsize;
b->b_ialloc *= 2;
! newsize = sizeof(struct basicblock)
! + ((b->b_ialloc - DEFAULT_BLOCK_SIZE)
! * sizeof(struct instr));
! ptr = PyObject_Realloc((void *)b, newsize);
if (ptr == NULL)
return -1;
***************
*** 274,282 ****
struct instr *i;
int off;
off = compiler_next_instr(c, c->c_curblock);
if (off < 0)
return 0;
i = &c->c_blocks[c->c_curblock]->b_instr[off];
! i->i_opcode = i->i_opcode;
i->i_arg = o;
return 1;
--- 287,296 ----
struct instr *i;
int off;
+ assert(arg_is_pyobject(opcode));
off = compiler_next_instr(c, c->c_curblock);
if (off < 0)
return 0;
i = &c->c_blocks[c->c_curblock]->b_instr[off];
! i->i_opcode = opcode;
i->i_arg = o;
return 1;
***************
*** 296,300 ****
return 0;
i = &c->c_blocks[c->c_curblock]->b_instr[off];
! i->i_opcode = i->i_opcode;
i->i_oparg = oparg;
return 1;
--- 310,314 ----
return 0;
i = &c->c_blocks[c->c_curblock]->b_instr[off];
! i->i_opcode = opcode;
i->i_oparg = oparg;
return 1;
***************
*** 302,306 ****
#define NEW_BLOCK(C) { \
! if (!compiler_use_new_block((C))) \
return 0; \
}
--- 316,320 ----
#define NEW_BLOCK(C) { \
! if (compiler_use_new_block((C)) < 0) \
return 0; \
}
***************
*** 334,338 ****
asdl_seq *seq = (SEQ); /* avoid variable capture */ \
for (i = 0; i < asdl_seq_LEN(seq); i++) { \
! TYPE ## _ty elt = asdl_seq_get(seq, i); \
if (!compiler_visit_ ## TYPE((C), elt)) \
return 0; \
--- 348,352 ----
asdl_seq *seq = (SEQ); /* avoid variable capture */ \
for (i = 0; i < asdl_seq_LEN(seq); i++) { \
! TYPE ## _ty elt = asdl_seq_GET(seq, i); \
if (!compiler_visit_ ## TYPE((C), elt)) \
return 0; \
***************
*** 343,346 ****
--- 357,362 ----
compiler_mod(struct compiler *c, mod_ty mod)
{
+ if (!compiler_enter_scope(c, NULL, mod))
+ return 0;
switch (mod->kind) {
case Module_kind:
***************
*** 348,351 ****
--- 364,368 ----
break;
case Interactive_kind:
+ c->c_interactive = 1;
VISIT(c, stmt, mod->v.Interactive.body);
break;
***************
*** 397,401 ****
ADDOP(c, DUP_TOP);
VISIT(c, expr,
! (expr_ty)asdl_seq_get(s->v.Print.values, i));
ADDOP(c, ROT_TWO);
ADDOP(c, PRINT_ITEM_TO);
--- 414,418 ----
ADDOP(c, DUP_TOP);
VISIT(c, expr,
! (expr_ty)asdl_seq_GET(s->v.Print.values, i));
ADDOP(c, ROT_TWO);
ADDOP(c, PRINT_ITEM_TO);
***************
*** 403,407 ****
else {
VISIT(c, expr,
! (expr_ty)asdl_seq_get(s->v.Print.values, i));
ADDOP(c, PRINT_ITEM);
}
--- 420,424 ----
else {
VISIT(c, expr,
! (expr_ty)asdl_seq_GET(s->v.Print.values, i));
ADDOP(c, PRINT_ITEM);
}
***************
*** 425,433 ****
assert(s->kind == If_kind);
end = compiler_new_block(c);
! if (!end)
return 0;
while (elif) {
next = compiler_new_block(c);
! if (!next)
return 0;
VISIT(c, expr, s->v.If.test);
--- 442,450 ----
assert(s->kind == If_kind);
end = compiler_new_block(c);
! if (end < 0)
return 0;
while (elif) {
next = compiler_new_block(c);
! if (next < 0)
return 0;
VISIT(c, expr, s->v.If.test);
***************
*** 440,444 ****
ADDOP(c, POP_TOP);
if (s->v.If.orelse) {
! stmt_ty t = asdl_seq_get(s->v.If.orelse, 0);
if (t->kind == If_kind) {
elif = 1;
--- 457,461 ----
ADDOP(c, POP_TOP);
if (s->v.If.orelse) {
! stmt_ty t = asdl_seq_GET(s->v.If.orelse, 0);
if (t->kind == If_kind) {
elif = 1;
***************
*** 462,466 ****
cleanup = compiler_new_block(c);
end = compiler_new_block(c);
! if (!(start && end && cleanup))
return 0;
ADDOP_I(c, SETUP_LOOP, end);
--- 479,483 ----
cleanup = compiler_new_block(c);
end = compiler_new_block(c);
! if (start < 0 || end < 0 || cleanup < 0)
return 0;
ADDOP_I(c, SETUP_LOOP, end);
***************
*** 488,496 ****
loop = compiler_new_block(c);
end = compiler_new_block(c);
! if (!(loop && end))
return 0;
if (s->v.While.orelse) {
orelse = compiler_new_block(c);
! if (!orelse)
return 0;
}
--- 505,513 ----
loop = compiler_new_block(c);
end = compiler_new_block(c);
! if (loop < 0 || end < 0)
return 0;
if (s->v.While.orelse) {
orelse = compiler_new_block(c);
! if (orelse < 0)
return 0;
}
***************
*** 562,565 ****
--- 579,583 ----
int i, n;
+ fprintf(stderr, "compile stmt %d at %d\n", s->kind, s->lineno);
c->c_lineno = s->lineno; /* XXX this isn't right */
switch (s->kind) {
***************
*** 590,594 ****
ADDOP(c, DUP_TOP);
VISIT(c, expr,
! (expr_ty)asdl_seq_get(s->v.Assign.targets, i));
}
break;
--- 608,612 ----
ADDOP(c, DUP_TOP);
VISIT(c, expr,
! (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
}
break;
***************
*** 803,817 ****
jumpi = JUMP_IF_TRUE;
end = compiler_new_block(c);
! if (!end)
return 0;
s = e->v.BoolOp.values;
n = asdl_seq_LEN(s) - 1;
for (i = 0; i < n; ++i) {
! VISIT(c, expr, asdl_seq_get(s, i));
ADDOP_I(c, jumpi, end);
NEW_BLOCK(c);
ADDOP(c, POP_TOP)
}
! VISIT(c, expr, asdl_seq_get(s, n));
compiler_use_block(c, end);
return 1;
--- 821,835 ----
jumpi = JUMP_IF_TRUE;
end = compiler_new_block(c);
! if (end < 0)
return 0;
s = e->v.BoolOp.values;
n = asdl_seq_LEN(s) - 1;
for (i = 0; i < n; ++i) {
! VISIT(c, expr, asdl_seq_GET(s, i));
ADDOP_I(c, jumpi, end);
NEW_BLOCK(c);
ADDOP(c, POP_TOP)
}
! VISIT(c, expr, asdl_seq_GET(s, n));
compiler_use_block(c, end);
return 1;
***************
*** 846,852 ****
for (i = 0; i < n; i++) {
ADDOP(c, DUP_TOP);
! VISIT(c, expr, asdl_seq_get(e->v.Dict.values, i));
ADDOP(c, ROT_TWO);
! VISIT(c, expr, asdl_seq_get(e->v.Dict.keys, i));
ADDOP(c, STORE_SUBSCR);
}
--- 864,870 ----
for (i = 0; i < n; i++) {
ADDOP(c, DUP_TOP);
! VISIT(c, expr, asdl_seq_GET(e->v.Dict.values, i));
ADDOP(c, ROT_TWO);
! VISIT(c, expr, asdl_seq_GET(e->v.Dict.keys, i));
ADDOP(c, STORE_SUBSCR);
}
***************
*** 862,866 ****
ADDOP(c, UNARY_CONVERT);
break;
! case Num_kind:
break;
case Str_kind:
--- 880,885 ----
ADDOP(c, UNARY_CONVERT);
break;
! case Num_kind:
! ADDOP_O(c, LOAD_CONST, e->v.Num.n);
break;
case Str_kind:
***************
*** 962,964 ****
--- 981,1020 ----
{
return 1;
+ }
+
+ static PyCodeObject *
+ assemble(struct compiler *c)
+ {
+ int i, j;
+
+ fprintf(stderr, "nblocks=%d curblock=%d\n",
+ c->c_nblocks, c->c_curblock);
+ for (i = 0; i < c->c_nblocks; i++) {
+ struct basicblock *b = c->c_blocks[i];
+ fprintf(stderr, "block %d: used=%d alloc=%d\n",
+ i, b->b_iused, b->b_ialloc);
+ for (j = 0; j < b->b_iused; j++) {
+ fprintf(stderr, "instr %d: %d\n",
+ j, b->b_instr[j].i_opcode);
+ }
+ }
+
+ PyErr_SetString(PyExc_SystemError, "no assembler exists\n");
+ return NULL;
+ }
+
+ static int
+ arg_is_pyobject(int opcode)
+ {
+ switch (opcode) {
+ case LOAD_CONST:
+ case LOAD_NAME: case LOAD_GLOBAL: case LOAD_FAST: case LOAD_DEREF:
+ case STORE_NAME: case STORE_GLOBAL: case STORE_FAST: case STORE_DEREF:
+ case DELETE_NAME: case DELETE_GLOBAL: case DELETE_FAST:
+ return 1;
+ default:
+ return 0;
+ }
+ assert(0); /* Can't get here */
+ return 0;
}