Index: src/CodeGen/CodeGenerator.cc
===================================================================
--- src/CodeGen/CodeGenerator.cc	(revision a61fea9ae36d50374d924b984ee54318ba2d7f61)
+++ src/CodeGen/CodeGenerator.cc	(revision 9a8930f5bb61faf0b3acd02c38d9af3f76c294a6)
@@ -5,11 +5,11 @@
 // file "LICENCE" distributed with Cforall.
 //
-// CodeGenerator2.cc -- 
+// CodeGenerator.cc -- 
 //
 // Author           : Richard C. Bilson
 // Created On       : Mon May 18 07:44:20 2015
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Wed Jun  3 11:53:32 2015
-// Update Count     : 13
+// Last Modified On : Thu Jun  4 14:05:45 2015
+// Update Count     : 120
 //
 
@@ -35,16 +35,22 @@
 
 namespace CodeGen {
-	int CodeGenerator2::tabsize = 4;
-
-	CodeGenerator2::CodeGenerator2( std::ostream &os ) : cur_indent( 0 ), insideFunction( false ), before( os ), after() { }
-
-	CodeGenerator2::CodeGenerator2( std::ostream &os, std::string init, int indent, bool infunp )
-			: cur_indent( indent ), insideFunction( infunp ), before( os ) {
-		//before << std::string( init );
-	}
-
-	CodeGenerator2::CodeGenerator2( std::ostream &os, char *init, int indent, bool infunp )
-			: cur_indent( indent ), insideFunction( infunp ), before( os ) {
-		//before << std::string( init );
+	int CodeGenerator::tabsize = 4;
+
+	// the kinds of statements that would ideally be separated by more whitespace
+	bool wantSpacing( Statement * stmt) {
+		return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) ||
+			dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * > ( stmt ) || dynamic_cast< SwitchStmt *>( stmt );
+	}
+
+	CodeGenerator::CodeGenerator( std::ostream &os ) : cur_indent( 0 ), insideFunction( false ), output( os ) { }
+
+	CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indent, bool infunp )
+			: cur_indent( indent ), insideFunction( infunp ), output( os ) {
+		//output << std::string( init );
+	}
+
+	CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indent, bool infunp )
+			: cur_indent( indent ), insideFunction( infunp ), output( os ) {
+		//output << std::string( init );
 	}
 
@@ -58,15 +64,15 @@
   
 	//*** Declarations
-	void CodeGenerator2::visit( FunctionDecl *functionDecl ) {
+	void CodeGenerator::visit( FunctionDecl *functionDecl ) {
 		handleStorageClass( functionDecl );
 		if ( functionDecl->get_isInline() ) {
-			before << "inline ";
-		} // if
-		before << genType( functionDecl->get_functionType(), mangleName( functionDecl ) );
+			output << "inline ";
+		} // if
+		output << genType( functionDecl->get_functionType(), mangleName( functionDecl ) );
 
 		// how to get this to the Functype?
 		std::list< Declaration * > olds = functionDecl->get_oldDecls();
 		if ( ! olds.empty() ) {
-			before << " /* function has old declaration */";
+			output << " /* function has old declaration */";
 		} // if
 
@@ -77,111 +83,111 @@
 	}
 
-	void CodeGenerator2::visit( ObjectDecl *objectDecl ) {
+	void CodeGenerator::visit( ObjectDecl *objectDecl ) {
 		handleStorageClass( objectDecl );
-		before << genType( objectDecl->get_type(), mangleName( objectDecl ) );
+		output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
 	
 		if ( objectDecl->get_init() ) {
-			before << " = ";
+			output << " = ";
 			objectDecl->get_init()->accept( *this );
 		} // if
 		if ( objectDecl->get_bitfieldWidth() ) {
-			before << ":";
+			output << ":";
 			objectDecl->get_bitfieldWidth()->accept( *this );
 		} // if
 	}
 
-	void CodeGenerator2::handleAggregate( AggregateDecl *aggDecl ) {
+	void CodeGenerator::handleAggregate( AggregateDecl *aggDecl ) {
 		if ( aggDecl->get_name() != "" )
-			before << aggDecl->get_name();
+			output << aggDecl->get_name();
 	
 		std::list< Declaration * > &memb = aggDecl->get_members();
 
 		if ( ! memb.empty() ) {
-			before << endl << string( cur_indent, ' ' ) << "{" << endl;
-
-			cur_indent += CodeGenerator2::tabsize; 
+			output << endl << string( cur_indent, ' ' ) << "{" << endl;
+
+			cur_indent += CodeGenerator::tabsize; 
 			for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
-				before << string( cur_indent, ' ' ); 
+				output << string( cur_indent, ' ' ); 
 				(*i)->accept(*this );
-				before << ";" << endl;
+				output << ";" << endl;
 			}
 
-			cur_indent -= CodeGenerator2::tabsize; 
-
-			before << string( cur_indent, ' ' ) << "}";
-		} // if
-	}
-
-	void CodeGenerator2::visit( StructDecl *structDecl ) {
-		before << "struct ";
+			cur_indent -= CodeGenerator::tabsize; 
+
+			output << string( cur_indent, ' ' ) << "}";
+		} // if
+	}
+
+	void CodeGenerator::visit( StructDecl *structDecl ) {
+		output << "struct ";
 		handleAggregate( structDecl );
 	}
 
-	void CodeGenerator2::visit( UnionDecl *aggregateDecl ) {
-		before << "union ";
+	void CodeGenerator::visit( UnionDecl *aggregateDecl ) {
+		output << "union ";
 		handleAggregate( aggregateDecl );
 	}
   
-	void CodeGenerator2::visit( EnumDecl *aggDecl ) {
-		before << "enum ";
+	void CodeGenerator::visit( EnumDecl *aggDecl ) {
+		output << "enum ";
 
 		if ( aggDecl->get_name() != "" )
-			before << aggDecl->get_name();
+			output << aggDecl->get_name();
 	
 		std::list< Declaration* > &memb = aggDecl->get_members();
 
 		if ( ! memb.empty() ) {
-			before << endl << "{" << endl;
-
-			cur_indent += CodeGenerator2::tabsize; 
+			output << endl << "{" << endl;
+
+			cur_indent += CodeGenerator::tabsize; 
 			for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
 				ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
 				assert( obj );
-				before << string( cur_indent, ' ' ) << mangleName( obj ); 
+				output << string( cur_indent, ' ' ) << mangleName( obj ); 
 				if ( obj->get_init() ) {
-					before << " = ";
+					output << " = ";
 					obj->get_init()->accept(*this );
 				} // if
-				before << "," << endl;
+				output << "," << endl;
 			} // for
 
-			cur_indent -= CodeGenerator2::tabsize; 
-
-			before << "}" << endl;
-		} // if
-	}
-  
-	void CodeGenerator2::visit( ContextDecl *aggregateDecl ) {}
-  
-	void CodeGenerator2::visit( TypedefDecl *typeDecl ) {
-		before << "typedef ";
-		before << genType( typeDecl->get_base(), typeDecl->get_name() );
-	}
-  
-	void CodeGenerator2::visit( TypeDecl *typeDecl ) {
+			cur_indent -= CodeGenerator::tabsize; 
+
+			output << "}" << endl;
+		} // if
+	}
+  
+	void CodeGenerator::visit( ContextDecl *aggregateDecl ) {}
+  
+	void CodeGenerator::visit( TypedefDecl *typeDecl ) {
+		output << "typedef ";
+		output << genType( typeDecl->get_base(), typeDecl->get_name() );
+	}
+  
+	void CodeGenerator::visit( TypeDecl *typeDecl ) {
 		// really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
 		// still to be done
-		before << "extern unsigned long " << typeDecl->get_name();
+		output << "extern unsigned long " << typeDecl->get_name();
 		if ( typeDecl->get_base() ) {
-			before << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )";
-		} // if
-	}
-
-	void CodeGenerator2::visit( SingleInit *init ) {
+			output << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )";
+		} // if
+	}
+
+	void CodeGenerator::visit( SingleInit *init ) {
 		init->get_value()->accept( *this );
 	}
 
-	void CodeGenerator2::visit( ListInit *init ) {
-		before << "{ ";
+	void CodeGenerator::visit( ListInit *init ) {
+		output << "{ ";
 		genCommaList( init->begin_initializers(), init->end_initializers() );
-		before << " }";
-	}
-
-	void CodeGenerator2::visit( Constant *constant ) { 
-		before << constant->get_value() ;
+		output << " }";
+	}
+
+	void CodeGenerator::visit( Constant *constant ) { 
+		output << constant->get_value() ;
 	}
 
 	//*** Expressions
-	void CodeGenerator2::visit( ApplicationExpr *applicationExpr ) {
+	void CodeGenerator::visit( ApplicationExpr *applicationExpr ) {
 		if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
 			OperatorInfo opInfo;
@@ -214,7 +220,7 @@
 					assert( applicationExpr->get_args().size() == 2 );
 					(*arg++)->accept( *this );
-					before << "[";
-					(*arg)->accept( *this );
-					before << "]";
+					output << "[";
+					(*arg)->accept( *this );
+					output << "]";
 					break;
 	      
@@ -227,8 +233,8 @@
 				  case OT_PREFIXASSIGN:
 					assert( applicationExpr->get_args().size() == 1 );
-					before << "(";
-					before << opInfo.symbol;
-					(*arg)->accept( *this );
-					before << ")";
+					output << "(";
+					output << opInfo.symbol;
+					(*arg)->accept( *this );
+					output << ")";
 					break;
 	      
@@ -237,5 +243,5 @@
 					assert( applicationExpr->get_args().size() == 1 );
 					(*arg)->accept( *this );
-					before << opInfo.symbol;
+					output << opInfo.symbol;
 					break;
 
@@ -243,9 +249,9 @@
 				  case OT_INFIXASSIGN:
 					assert( applicationExpr->get_args().size() == 2 );
-					before << "(";
+					output << "(";
 					(*arg++)->accept( *this );
-					before << opInfo.symbol;
-					(*arg)->accept( *this );
-					before << ")";
+					output << opInfo.symbol;
+					(*arg)->accept( *this );
+					output << ")";
 					break;
 	      
@@ -256,17 +262,17 @@
 			} else {
 				varExpr->accept( *this );
-				before << "(";
+				output << "(";
 				genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
-				before << ")";
+				output << ")";
 			} // if
 		} else {
 			applicationExpr->get_function()->accept( *this );
-			before << "(";
+			output << "(";
 			genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
-			before << ")";
-		} // if
-	}
-  
-	void CodeGenerator2::visit( UntypedExpr *untypedExpr ) {
+			output << ")";
+		} // if
+	}
+  
+	void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
 		if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
 			OperatorInfo opInfo;
@@ -277,7 +283,7 @@
 					assert( untypedExpr->get_args().size() == 2 );
 					(*arg++)->accept( *this );
-					before << "[";
-					(*arg)->accept( *this );
-					before << "]";
+					output << "[";
+					(*arg)->accept( *this );
+					output << "]";
 					break;
 	      
@@ -289,8 +295,8 @@
 				  case OT_PREFIXASSIGN:
 					assert( untypedExpr->get_args().size() == 1 );
-					before << "(";
-					before << opInfo.symbol;
-					(*arg)->accept( *this );
-					before << ")";
+					output << "(";
+					output << opInfo.symbol;
+					(*arg)->accept( *this );
+					output << ")";
 					break;
 	      
@@ -299,5 +305,5 @@
 					assert( untypedExpr->get_args().size() == 1 );
 					(*arg)->accept( *this );
-					before << opInfo.symbol;
+					output << opInfo.symbol;
 					break;
   
@@ -305,9 +311,9 @@
 				  case OT_INFIXASSIGN:
 					assert( untypedExpr->get_args().size() == 2 );
-					before << "(";
+					output << "(";
 					(*arg++)->accept( *this );
-					before << opInfo.symbol;
-					(*arg)->accept( *this );
-					before << ")";
+					output << opInfo.symbol;
+					(*arg)->accept( *this );
+					output << ")";
 					break;
 	      
@@ -318,226 +324,210 @@
 			} else {
 				nameExpr->accept( *this );
-				before << "(";
+				output << "(";
 				genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
-				before << ")";
+				output << ")";
 			} // if
 		} else {
 			untypedExpr->get_function()->accept( *this );
-			before << "(";
+			output << "(";
 			genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
-			before << ")";
-		} // if
-	}
-  
-	void CodeGenerator2::visit( NameExpr *nameExpr ) {
+			output << ")";
+		} // if
+	}
+  
+	void CodeGenerator::visit( NameExpr *nameExpr ) {
 		OperatorInfo opInfo;
 		if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
 			assert( opInfo.type == OT_CONSTANT );
-			before << opInfo.symbol;
-		} else {
-			before << nameExpr->get_name();
-		} // if
-	}
-  
-	void CodeGenerator2::visit( AddressExpr *addressExpr ) {
-		before << "(&";
+			output << opInfo.symbol;
+		} else {
+			output << nameExpr->get_name();
+		} // if
+	}
+  
+	void CodeGenerator::visit( AddressExpr *addressExpr ) {
+		output << "(&";
 		// this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
 		if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
-			before << mangleName( variableExpr->get_var() );
+			output << mangleName( variableExpr->get_var() );
 		} else {
 			addressExpr->get_arg()->accept( *this );
 		} // if
-		before << ")";
-	}
-
-	void CodeGenerator2::visit( CastExpr *castExpr ) {
-		before << "((";
+		output << ")";
+	}
+
+	void CodeGenerator::visit( CastExpr *castExpr ) {
+		output << "((";
 		if ( castExpr->get_results().empty() ) {
-			before << "void" ;
-		} else {
-			before << genType( castExpr->get_results().front(), "" );
-		} // if
-		before << ")";
+			output << "void" ;
+		} else {
+			output << genType( castExpr->get_results().front(), "" );
+		} // if
+		output << ")";
 		castExpr->get_arg()->accept( *this );
-		before << ")";
-	}
-  
-	void CodeGenerator2::visit( UntypedMemberExpr *memberExpr ) {
+		output << ")";
+	}
+  
+	void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
 		assert( false );
 	}
   
-	void CodeGenerator2::visit( MemberExpr *memberExpr ) {
+	void CodeGenerator::visit( MemberExpr *memberExpr ) {
 		memberExpr->get_aggregate()->accept( *this );
-		before << "." << mangleName( memberExpr->get_member() );
-	}
-  
-	void CodeGenerator2::visit( VariableExpr *variableExpr ) {
+		output << "." << mangleName( memberExpr->get_member() );
+	}
+  
+	void CodeGenerator::visit( VariableExpr *variableExpr ) {
 		OperatorInfo opInfo;
 		if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
-			before << opInfo.symbol;
-		} else {
-			before << mangleName( variableExpr->get_var() );
-		} // if
-	}
-  
-	void CodeGenerator2::visit( ConstantExpr *constantExpr ) {
+			output << opInfo.symbol;
+		} else {
+			output << mangleName( variableExpr->get_var() );
+		} // if
+	}
+  
+	void CodeGenerator::visit( ConstantExpr *constantExpr ) {
 		assert( constantExpr->get_constant() );
 		constantExpr->get_constant()->accept( *this );
 	}
   
-	void CodeGenerator2::visit( SizeofExpr *sizeofExpr ) {
-		before << "sizeof(";
+	void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
+		output << "sizeof(";
 		if ( sizeofExpr->get_isType() ) {
-			before << genType( sizeofExpr->get_type(), "" );
+			output << genType( sizeofExpr->get_type(), "" );
 		} else {
 			sizeofExpr->get_expr()->accept( *this );
 		} // if
-		before << ")";
-	}
-  
-	void CodeGenerator2::visit( LogicalExpr *logicalExpr ) {
-		before << "(";
+		output << ")";
+	}
+  
+	void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
+		output << "(";
 		logicalExpr->get_arg1()->accept( *this );
 		if ( logicalExpr->get_isAnd() ) {
-			before << " && ";
-		} else {
-			before << " || ";
+			output << " && ";
+		} else {
+			output << " || ";
 		} // if
 		logicalExpr->get_arg2()->accept( *this );
-		before << ")";
-	}
-  
-	void CodeGenerator2::visit( ConditionalExpr *conditionalExpr ) {
-		before << "(";
+		output << ")";
+	}
+  
+	void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
+		output << "(";
 		conditionalExpr->get_arg1()->accept( *this );
-		before << " ? ";
+		output << " ? ";
 		conditionalExpr->get_arg2()->accept( *this );
-		before << " : ";
+		output << " : ";
 		conditionalExpr->get_arg3()->accept( *this );
-		before << ")";
-	}
-  
-	void CodeGenerator2::visit( CommaExpr *commaExpr ) {
-		before << "(";
+		output << ")";
+	}
+  
+	void CodeGenerator::visit( CommaExpr *commaExpr ) {
+		output << "(";
 		commaExpr->get_arg1()->accept( *this );
-		before << " , ";
+		output << " , ";
 		commaExpr->get_arg2()->accept( *this );
-		before << ")";
-	}
-  
-	void CodeGenerator2::visit( TupleExpr *tupleExpr ) {}
-  
-	void CodeGenerator2::visit( TypeExpr *typeExpr ) {}
-  
-  
+		output << ")";
+	}
+  
+	void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
+  
+	void CodeGenerator::visit( TypeExpr *typeExpr ) {}
+
 	//*** Statements
-	void CodeGenerator2::visit( CompoundStmt *compoundStmt ) {
+	void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
 		std::list<Statement*> ks = compoundStmt->get_kids();
-
-		before << endl << string( cur_indent, ' ' ) << "{" << endl;
-
-		cur_indent += CodeGenerator2::tabsize; 
+		output << "{" << endl;
+
+		cur_indent += CodeGenerator::tabsize;
 
 		for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++) {
-			before << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() )  ;
+			output << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() );
 			(*i)->accept(*this );
-			shift_left();
-			before << endl;
+
+			output << endl;
+			if ( wantSpacing( *i ) ) {
+				output << endl;
+			}
 		}
-		cur_indent -= CodeGenerator2::tabsize; 
-
-		before << string( cur_indent, ' ' ) << "}" << endl;
-	}
-
-	void CodeGenerator2::visit( ExprStmt *exprStmt ) {
-		if ( exprStmt != 0 ) {
-			exprStmt->get_expr()->accept( *this );
-			shift_left();
-			before << ";" ;
-		} // if
-	}
-
-	void CodeGenerator2::visit( IfStmt *ifStmt ) {
-		before << "if (";
+		cur_indent -= CodeGenerator::tabsize; 
+
+		output << string( cur_indent, ' ' ) << "}";
+	}
+
+	void CodeGenerator::visit( ExprStmt *exprStmt ) {
+		// I don't see why this check is necessary. 
+		// If this starts to cause problems then put it back in, 
+		// with an explanation
+		assert( exprStmt );
+
+		// if ( exprStmt != 0 ) {
+		exprStmt->get_expr()->accept( *this );
+		output << ";" ;
+		// } // if
+	}
+
+	void CodeGenerator::visit( IfStmt *ifStmt ) {
+		output << "if (";
 		ifStmt->get_condition()->accept(*this );
-		after += ")\n";
-		shift_left(); 
-
-		cur_indent += CodeGenerator2::tabsize;
-		before << string( cur_indent, ' ' );
+		output << ") ";
+
 		ifStmt->get_thenPart()->accept(*this );
-		cur_indent -= CodeGenerator2::tabsize; 
-		shift_left(); before << endl;
 
 		if ( ifStmt->get_elsePart() != 0) {
-			before << string( cur_indent, ' ' ) << " else " << endl ;
-
-			cur_indent += CodeGenerator2::tabsize; 
+			output << " else ";
 			ifStmt->get_elsePart()->accept(*this );
-			cur_indent -= CodeGenerator2::tabsize; 
-		} // if
-	}
-
-	void CodeGenerator2::visit( SwitchStmt *switchStmt ) {
-		//before << /* "\r" << */ string( cur_indent, ' ' ) << CodeGenerator2::printLabels( switchStmt->get_labels() ) 
-		before << "switch (" ;
+		} // if
+	}
+
+	void CodeGenerator::visit( SwitchStmt *switchStmt ) {
+		//output << /* "\r" << */ string( cur_indent, ' ' ) << CodeGenerator::printLabels( switchStmt->get_labels() ) 
+		output << "switch (" ;
 		switchStmt->get_condition()->accept(*this );
-		after += ")\n";
-		shift_left();
-
-		before << string( cur_indent, ' ' ) << "{" << std::endl;
-		cur_indent += CodeGenerator2::tabsize;
-
-		std::list< Statement * > stmts = switchStmt->get_branches();
-		bool lastBreak = false; 
-
-		// horrible, horrible hack
-		if ( dynamic_cast<BranchStmt *>( stmts.back() ) != 0 ) {
-			lastBreak = true;
-			stmts.pop_back();
-		} // if
-		acceptAll( stmts, *this );
-		if ( lastBreak ) {
-			Statement *st = switchStmt->get_branches().back();
-			before << CodeGenerator2::printLabels( st->get_labels());
-			st->accept( *this );
-		} // if
-	  
-		cur_indent -= CodeGenerator2::tabsize; 
-
-		before << /* "\r" << */ string( cur_indent, ' ' ) << "}" << endl ;
-	}
-
-	void CodeGenerator2::visit( CaseStmt *caseStmt ) {
-		before << string( cur_indent, ' ' );
+		output << ") ";
+		
+		output << "{" << std::endl;
+		cur_indent += CodeGenerator::tabsize;
+
+		acceptAll( switchStmt->get_branches(), *this );
+
+		cur_indent -= CodeGenerator::tabsize;
+
+		output << string( cur_indent, ' ' ) << "}";
+	}
+
+	void CodeGenerator::visit( CaseStmt *caseStmt ) {
+		output << string( cur_indent, ' ' );
 		if ( caseStmt->isDefault()) 
-			before << "default "  ;
+			output << "default";
 		else {
-			before << "case "  ;
+			output << "case ";
 			caseStmt->get_condition()->accept(*this );
 		} // if
-		after += ":\n";
-		shift_left();
-
+		output << ":\n";
+		
 		std::list<Statement *> sts = caseStmt->get_statements();
 
-		cur_indent += CodeGenerator2::tabsize;
+		cur_indent += CodeGenerator::tabsize;
 		for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++) {
-			before << /* "\r" << */ string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() )  ;
+			output << /* "\r" << */ string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() )  ;
 			(*i)->accept(*this );
-			shift_left();
-			before << ";" << endl;
+			output << endl;
 		}
-		cur_indent -= CodeGenerator2::tabsize;
-	}
-
-	void CodeGenerator2::visit( BranchStmt *branchStmt ) {
+		cur_indent -= CodeGenerator::tabsize;
+	}
+
+	void CodeGenerator::visit( BranchStmt *branchStmt ) {
+		output << "\r" << string( cur_indent, ' ' );
+		output << CodeGenerator::printLabels( branchStmt->get_labels());
+
 		switch ( branchStmt->get_type()) {
 		  case BranchStmt::Goto:
 			if ( ! branchStmt->get_target().empty() )
-				before << "goto " << branchStmt->get_target();
+				output << "goto " << branchStmt->get_target();
 			else { 
 				if ( branchStmt->get_computedTarget() != 0 ) {
-					before << "goto *";
+					output << "goto *";
 					branchStmt->get_computedTarget()->accept( *this );
 				} // if
@@ -545,16 +535,16 @@
 			break;
 		  case BranchStmt::Break:
-			before << "break";
+			output << "break";
 			break;
 		  case BranchStmt::Continue:
-			before << "continue";
+			output << "continue";
 			break;
 		}
-		before << ";";
-	}
-
-
-	void CodeGenerator2::visit( ReturnStmt *returnStmt ) {
-		before << "return ";
+		output << ";";
+	}
+
+
+	void CodeGenerator::visit( ReturnStmt *returnStmt ) {
+		output << "return ";
 
 		// xxx -- check for null expression;
@@ -562,73 +552,67 @@
 			returnStmt->get_expr()->accept( *this );
 		} // if
-		after += ";";
-	}
-
-	void CodeGenerator2::visit( WhileStmt *whileStmt ) {
+		output << ";";
+	}
+
+	void CodeGenerator::visit( WhileStmt *whileStmt ) {
 		if ( whileStmt->get_isDoWhile() )
-			before << "do" ;
+			output << "do" ;
 		else {
-			before << "while (" ;
+			output << "while (" ;
 			whileStmt->get_condition()->accept(*this );
-			after += ")";
-		} // if
-		after += "{\n";
-		shift_left();
-
+			output << ")";
+		} // if
+		output << " ";
+
+		output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
 		whileStmt->get_body()->accept( *this );
 
-		before << /* "\r" << */ string( cur_indent, ' ' ) << "}" ;
+		output << /* "\r" << */ string( cur_indent, ' ' );
 
 		if ( whileStmt->get_isDoWhile() ) {
-			before << " while (" ;
+			output << " while (" ;
 			whileStmt->get_condition()->accept(*this );
-			after += ");";
-		} // if
-
-		after += "\n";
-	}
-
-	void CodeGenerator2::visit( ForStmt *forStmt ) {
-		before << "for (";
+			output << ");";
+		} // if
+	}
+
+	void CodeGenerator::visit( ForStmt *forStmt ) {
+		output << "for (";
 
 		if ( forStmt->get_initialization() != 0 )
 			forStmt->get_initialization()->accept( *this );
 		else
-			before << ";";
-		shift_left();
-
+			output << ";";
+		
 		if ( forStmt->get_condition() != 0 )
 			forStmt->get_condition()->accept( *this );
-		shift_left(); before << ";";
+		output << ";";
 
 		if ( forStmt->get_increment() != 0 )
 			forStmt->get_increment()->accept( *this );
-		shift_left(); before << ")" << endl;
+		output << ") ";
 
 		if ( forStmt->get_body() != 0 ) {
-			cur_indent += CodeGenerator2::tabsize; 
-			before << string( cur_indent, ' ' ) << CodeGenerator2::printLabels( forStmt->get_body()->get_labels() );
+			output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
 			forStmt->get_body()->accept( *this );
-			cur_indent -= CodeGenerator2::tabsize; 
-		} // if
-	}
-
-	void CodeGenerator2::visit( NullStmt *nullStmt ) {
-		//before << /* "\r" << */ string( cur_indent, ' ' ) << CodeGenerator2::printLabels( nullStmt->get_labels() );
-		before << "/* null statement */ ;";
-	}
-
-	void CodeGenerator2::visit( DeclStmt *declStmt ) {
+		} // if
+	}
+
+	void CodeGenerator::visit( NullStmt *nullStmt ) {
+		//output << /* "\r" << */ string( cur_indent, ' ' ) << CodeGenerator::printLabels( nullStmt->get_labels() );
+		output << "/* null statement */ ;";
+	}
+
+	void CodeGenerator::visit( DeclStmt *declStmt ) {
 		declStmt->get_decl()->accept( *this );
 	
 		if ( doSemicolon( declStmt->get_decl() ) ) {
-			after += ";";
-		} // if
-		shift_left();
-	}
-
-	std::string CodeGenerator2::printLabels( std::list< Label > &l ) {
+			output << ";";
+		} // if
+	}
+
+	std::string CodeGenerator::printLabels( std::list< Label > &l ) {
 		std::string str( "" );
-		l.unique();
+		l.unique(); // assumes a sorted list. Why not use set?
 
 		for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )
@@ -638,18 +622,13 @@
 	}
 
-	void CodeGenerator2::shift_left() {
-		before << after;
-		after = "";
-	}
-
-	void CodeGenerator2::handleStorageClass( Declaration *decl ) {
+	void CodeGenerator::handleStorageClass( Declaration *decl ) {
 		switch ( decl->get_storageClass() ) {
 		  case Declaration::NoStorageClass:
 			break;
 		  case Declaration::Extern:
-			before << "extern ";
+			output << "extern ";
 			break;
 		  case Declaration::Static:
-			before << "static ";
+			output << "static ";
 			break;
 		  case Declaration::Auto:
@@ -657,5 +636,5 @@
 			break;
 		  case Declaration::Register:
-			before << "register ";
+			output << "register ";
 			break;
 		  case Declaration::Inline:
Index: src/CodeGen/CodeGenerator.h
===================================================================
--- src/CodeGen/CodeGenerator.h	(revision a61fea9ae36d50374d924b984ee54318ba2d7f61)
+++ src/CodeGen/CodeGenerator.h	(revision 9a8930f5bb61faf0b3acd02c38d9af3f76c294a6)
@@ -5,11 +5,11 @@
 // file "LICENCE" distributed with Cforall.
 //
-// CodeGenerator2.h -- 
+// CodeGenerator.h -- 
 //
 // Author           : Richard C. Bilson
 // Created On       : Mon May 18 07:44:20 2015
-// Last Modified By : Peter A. Buhr
-// Last Modified On : Mon May 18 23:35:37 2015
-// Update Count     : 2
+// Last Modified By : Rob Schluntz
+// Last Modified On : Tue Jun 02 13:43:29 2015
+// Update Count     : 14
 //
 
@@ -25,13 +25,11 @@
 
 namespace CodeGen {
-	class CodeGenerator2 : public Visitor {
+	class CodeGenerator : public Visitor {
 	  public:
 		static int tabsize;
 
-		CodeGenerator2( std::ostream &os );
-		CodeGenerator2( std::ostream &os, std::string, int indent = 0, bool infun = false );
-		CodeGenerator2( std::ostream &os, char *, int indent = 0, bool infun = false );
-
-		CodeGenerator2( CodeGenerator2 & );
+		CodeGenerator( std::ostream &os );
+		CodeGenerator( std::ostream &os, std::string, int indent = 0, bool infun = false );
+		CodeGenerator( std::ostream &os, char *, int indent = 0, bool infun = false );
 
 		//*** Declaration
@@ -82,13 +80,9 @@
 		virtual void visit( DeclStmt * ); 
 
-		std::string get_string( void );
-		void add_string_left( std::string s ) { before << s; }
-		void shift_left();
 		template< class Iterator > void genCommaList( Iterator begin, Iterator end );
 	  private:
 		int cur_indent;
 		bool insideFunction;
-		std::ostream &before;
-		std::string after;
+		std::ostream &output;
 
 		static std::string printLabels ( std::list < Label > & );
@@ -100,5 +94,5 @@
 	
 	template< class Iterator >
-	void CodeGenerator2::genCommaList( Iterator begin, Iterator end ) {
+	void CodeGenerator::genCommaList( Iterator begin, Iterator end ) {
 		if ( begin == end ) return;
 
@@ -106,5 +100,5 @@
 			(*begin++)->accept( *this );
 			if ( begin == end ) return;
-			before << ", ";
+			output << ", ";
 		} // for
 	}
Index: src/CodeGen/GenType.cc
===================================================================
--- src/CodeGen/GenType.cc	(revision a61fea9ae36d50374d924b984ee54318ba2d7f61)
+++ src/CodeGen/GenType.cc	(revision 9a8930f5bb61faf0b3acd02c38d9af3f76c294a6)
@@ -10,6 +10,6 @@
 // Created On       : Mon May 18 07:44:20 2015
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Tue Jun  2 11:21:32 2015
-// Update Count     : 3
+// Last Modified On : Thu Jun  4 14:04:58 2015
+// Update Count     : 4
 //
 
@@ -97,5 +97,5 @@
 		} // if
 		if ( dimension != 0 ) {
-			CodeGenerator2 cg( os );
+			CodeGenerator cg( os );
 			dimension->accept( cg );
 		} // if
@@ -148,5 +148,5 @@
 			} // if
 		} else {
-			CodeGenerator2 cg( os );
+			CodeGenerator cg( os );
 			os << "(" ;
 
Index: src/CodeGen/Generate.cc
===================================================================
--- src/CodeGen/Generate.cc	(revision a61fea9ae36d50374d924b984ee54318ba2d7f61)
+++ src/CodeGen/Generate.cc	(revision 9a8930f5bb61faf0b3acd02c38d9af3f76c294a6)
@@ -10,6 +10,6 @@
 // Created On       : Mon May 18 07:44:20 2015
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Tue Jun  2 11:21:06 2015
-// Update Count     : 2
+// Last Modified On : Thu Jun  4 14:04:25 2015
+// Update Count     : 5
 //
 
@@ -27,10 +27,9 @@
 namespace CodeGen {
 	void generate( std::list< Declaration* > translationUnit, std::ostream &os, bool doIntrinsics ) {
-		CodeGen::CodeGenerator2 cgv( os );
+		CodeGen::CodeGenerator cgv( os );
 
 		for ( std::list<Declaration *>::iterator i = translationUnit.begin(); i != translationUnit.end();  i++ ) {
 			if ( LinkageSpec::isGeneratable( (*i)->get_linkage() ) && (doIntrinsics || ! LinkageSpec::isBuiltin( (*i)->get_linkage() ) ) ) {
 				(*i)->accept(cgv);
-				cgv.shift_left();
 				if ( doSemicolon( *i ) ) {
 					os << ";";
