[Python-checkins] python/dist/src/Python newcompile.c, 1.1.2.110, 1.1.2.111

jhylton@users.sourceforge.net jhylton at users.sourceforge.net
Fri Oct 7 20:42:54 CEST 2005


Update of /cvsroot/python/python/dist/src/Python
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6237

Modified Files:
      Tag: ast-branch
	newcompile.c 
Log Message:
Fix a small bug in test_dis and add some high-level comments.

One failure in test_dis was shallow.  test_dis() failed because the
compiler generated a redundant LOAD_CONST None / RETURN_VALUE block.
The assembler() now checks the b_return flag of the final block before
emitting a new RETURN_VALUE opcode.



Index: newcompile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/Attic/newcompile.c,v
retrieving revision 1.1.2.110
retrieving revision 1.1.2.111
diff -u -d -r1.1.2.110 -r1.1.2.111
--- newcompile.c	28 Jul 2005 05:50:01 -0000	1.1.2.110
+++ newcompile.c	7 Oct 2005 18:42:50 -0000	1.1.2.111
@@ -1,3 +1,19 @@
+/*
+ * This file compiles an abstract syntax tree (AST) into Python bytecode.
+ *
+ * The primary entry point is PyAST_Compile(), which returns a
+ * PyCodeObject.  The compiler makes several passes to build the code
+ * object:
+ *   1. Checks for future statements.  See future.c
+ *   2. Builds a symbol table.  See symtable.c.
+ *   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.  
+ *
+ * Note that compiler_mod() suggests module, but the module ast type
+ * (mod_ty) has cases for expressions and interactive statements.
+ */
+
 #include "Python.h"
 
 #include "Python-ast.h"
@@ -134,6 +150,13 @@
 			      has been generated with current lineno */
 };
 
+/* This struct captures the global state of a compilation.  
+
+   The u pointer points to the current compilation unit, while units
+   for enclosing blocks are stored in c_stack.  The u and c_stack are
+   managed by compiler_enter_scope() and compiler_exit_scope().
+*/
+
 struct compiler {
 	const char *c_filename;
 	struct symtable *c_st;
@@ -143,9 +166,9 @@
 	int c_interactive;
         int c_nestlevel;
 
-	struct compiler_unit *u;
-	PyObject *c_stack;
-	char *c_encoding;	/* source encoding (a borrowed reference) */
+        struct compiler_unit *u; /* compiler state for current block */
+	PyObject *c_stack;       /* Python list holding compiler_unit ptrs */
+	char *c_encoding;	 /* source encoding (a borrowed reference) */
 };
 
 struct assembler {
@@ -3559,10 +3582,12 @@
 	   XXX NEXT_BLOCK() isn't quite right, because if the last
 	   block ends with a jump or return b_next shouldn't set.
 	 */
-	NEXT_BLOCK(c);
-        if (addNone)
-            ADDOP_O(c, LOAD_CONST, Py_None, consts);
-	ADDOP(c, RETURN_VALUE);
+	if (!c->u->u_curblock->b_return) {
+		NEXT_BLOCK(c);
+		if (addNone)
+			ADDOP_O(c, LOAD_CONST, Py_None, consts);
+		ADDOP(c, RETURN_VALUE);
+	}
 
 	nblocks = 0;
 	entryblock = NULL;



More information about the Python-checkins mailing list