Index: translator/ControlStruct/CaseRangeMutator.cc
===================================================================
--- translator/ControlStruct/CaseRangeMutator.cc	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
+++ translator/ControlStruct/CaseRangeMutator.cc	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
@@ -0,0 +1,190 @@
+#include <list>
+#include <cassert>
+#include <cstdlib>
+#include <iterator>
+
+#include "utility.h"
+
+#include "SynTree/Statement.h"
+#include "SynTree/Expression.h"
+#include "SynTree/Constant.h"
+#include "SynTree/Type.h"
+#include "CaseRangeMutator.h"
+
+namespace ControlStruct {
+
+  Statement* CaseRangeMutator::mutate(ChooseStmt   *chooseStmt)
+  {
+    /* There shouldn't be any `choose' statements by now, throw an exception or something. */
+    throw( 0 ) ; /* FIXME */
+  }
+
+  Statement* CaseRangeMutator::mutate(SwitchStmt   *switchStmt)
+  {
+    std::list< Statement * > &cases = switchStmt->get_branches();
+
+    // a `for' would be more natural... all this contortions are because `replace' invalidates the iterator
+    std::list< Statement * >::iterator i = cases.begin();
+    while (  i != cases.end() ) {
+      (*i)->acceptMutator( *this );
+
+      if ( ! newCaseLabels.empty() ) {
+	std::list< Statement * > newCases;
+
+	// transform( newCaseLabels.begin(), newCaseLabels.end(), bnd1st( ptr_fun( ctor< CaseStmt, Label, Expression * > ) ) );
+
+	for ( std::list< Expression * >::iterator j = newCaseLabels.begin();
+	      j != newCaseLabels.end(); j++ ) {
+	  std::list<Label> emptyLabels;
+	  std::list< Statement *> emptyStmts;
+	  newCases.push_back( new CaseStmt( emptyLabels, *j, emptyStmts ) );
+	}
+
+	if( CaseStmt *currentCase = dynamic_cast< CaseStmt * > ( *i ) )
+	  if ( !currentCase->get_statements().empty() ) {
+	    CaseStmt *lastCase = dynamic_cast< CaseStmt * > ( newCases.back() );
+	    if ( lastCase == 0 ) { throw ( 0 ); /* FIXME */ } // something is very wrong, as I just made these, and they were all cases
+	    // transfer the statement block (if any) to the new list:
+	    lastCase->set_statements( currentCase->get_statements() );
+	  }
+	std::list< Statement * >::iterator j = i; advance( j, 1 );
+	replace ( cases, i, newCases );
+	i = j;
+	newCaseLabels.clear();
+      } else
+	i++;
+    }
+
+    return switchStmt;
+  }
+
+  Statement* CaseRangeMutator::mutate(FallthruStmt *fallthruStmt)
+  {
+    //delete fallthruStmt;
+    return new NullStmt();
+  }
+
+  Statement* CaseRangeMutator::mutate(CaseStmt *caseStmt)
+  {
+    UntypedExpr *cond;
+    if ( (cond = dynamic_cast< UntypedExpr * >( caseStmt->get_condition() )) != 0 ) {
+      NameExpr *nmfunc;
+      if ( (nmfunc = dynamic_cast< NameExpr *>( cond->get_function() )) != 0 ) {
+	if ( nmfunc->get_name() == std::string("Range") ) {
+	  assert( cond->get_args().size() == 2 );
+	  std::list<Expression *>::iterator i = cond->get_args().begin();
+	  Expression *lo = *i, *hi = *(++i); // "unnecessary" temporaries
+	  fillRange( lo, hi);
+	}
+      }
+    } else if ( TupleExpr *tcond = dynamic_cast< TupleExpr * >( caseStmt->get_condition() ) ) {
+      // case list
+      assert( ! tcond->get_exprs().empty() );
+      for ( std::list< Expression * >::iterator i = tcond->get_exprs().begin(); i != tcond->get_exprs().end(); i++ )
+	newCaseLabels.push_back( *i ); // do I need to clone them?
+    }
+
+    std::list< Statement * > &stmts = caseStmt->get_statements();
+    mutateAll ( stmts, *this );
+
+    return caseStmt;
+  }
+
+  void CaseRangeMutator::fillRange(Expression *lo, Expression *hi) {
+    // generate the actual range (and check for consistency)
+    Constant *c_lo, *c_hi;
+    ConstantExpr *ce_lo, *ce_hi;
+    ce_lo = dynamic_cast< ConstantExpr * >( lo );
+    ce_hi = dynamic_cast< ConstantExpr * >( hi );
+
+    if ( ce_lo && ce_hi ) {
+      c_lo = ce_lo->get_constant(); c_hi = ce_hi->get_constant();
+    } /* else {
+      if ( !ce_lo ) ;
+      if ( !ce_hi ) ;
+      } */
+    BasicType *ty_lo = dynamic_cast< BasicType * >( c_lo->get_type() ),
+                       *ty_hi = dynamic_cast< BasicType * >( c_hi->get_type() );
+    
+    if( !ty_lo || !ty_hi )
+      return; // one of them is not a constant
+
+
+    switch( ty_lo->get_kind() ) {
+    case BasicType::Char:
+    case BasicType::UnsignedChar:
+      switch( ty_hi->get_kind() ) 
+	{
+	case BasicType::Char:
+	case BasicType::UnsignedChar:
+	  // first case, they are both printable ASCII characters represented as 'x'
+	  if ( c_lo->get_value().size() == 3 && c_hi->get_value().size() == 3 ) {
+	    char ch_lo = (c_lo->get_value())[1], ch_hi = (c_hi->get_value())[1];
+
+	    if ( ch_lo > ch_hi ) { char t=ch_lo; ch_lo=ch_hi; ch_hi=t; }
+
+	    for( char c = ch_lo; c <=  ch_hi; c++ ){
+	      Type::Qualifiers q;
+	      Constant cnst( new BasicType(q, BasicType::Char),
+				      std::string("'") + c + std::string("'") );
+	      newCaseLabels.push_back( new ConstantExpr( cnst ) );
+	    }
+
+	    return;
+	  }
+	  break;
+	default:
+	  // error: incompatible constants
+	  break;
+	}
+      break;
+    case BasicType::ShortSignedInt:
+    case BasicType::ShortUnsignedInt:
+    case BasicType::SignedInt:
+    case BasicType::UnsignedInt:
+    case BasicType::LongSignedInt:
+    case BasicType::LongUnsignedInt:
+    case BasicType::LongLongSignedInt:
+    case BasicType::LongLongUnsignedInt:
+      switch( ty_hi->get_kind() ) 
+	{
+	case BasicType::ShortSignedInt:
+	case BasicType::ShortUnsignedInt:
+	case BasicType::SignedInt:
+	case BasicType::UnsignedInt:
+	case BasicType::LongSignedInt:
+	case BasicType::LongUnsignedInt:
+	case BasicType::LongLongSignedInt:
+	case BasicType::LongLongUnsignedInt: {
+	  int i_lo = atoi(c_lo->get_value().c_str()),
+	      i_hi = atoi(c_hi->get_value().c_str());
+
+	  if ( i_lo > i_hi ) { int t=i_lo; i_lo=i_hi; i_hi=t; }
+
+	  for( int c = i_lo; c <=  i_hi; c++ ){
+	    Type::Qualifiers q;
+	    Constant cnst( new BasicType(q, ty_hi->get_kind()), // figure can't hurt (used to think in positives)
+				    toString< int >( c ) );
+	    newCaseLabels.push_back( new ConstantExpr( cnst ) );
+	  }
+
+	  return;
+	}
+	default:
+	  // error: incompatible constants
+	  break;
+	}
+      break;
+    default:
+      break;
+    }
+
+/* End: */{ 
+      // invalid range, signal a warning (it still generates the two case labels)
+      newCaseLabels.push_back( lo );
+      newCaseLabels.push_back( hi );
+      return;
+    }
+  }
+
+} // namespace ControlStruct
Index: translator/ControlStruct/CaseRangeMutator.h
===================================================================
--- translator/ControlStruct/CaseRangeMutator.h	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
+++ translator/ControlStruct/CaseRangeMutator.h	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
@@ -0,0 +1,35 @@
+#ifndef CASERNG_MUTATOR_H
+#define CASERNG_MUTATOR_H
+
+#include <list>
+
+#include "SynTree/Mutator.h"
+
+namespace ControlStruct {
+
+  class CaseRangeMutator : public Mutator
+  {
+  public:
+    CaseRangeMutator() {}
+
+    virtual Statement* mutate(ChooseStmt   *);
+    virtual Statement* mutate(SwitchStmt   *);
+    virtual Statement* mutate(FallthruStmt *);
+    virtual Statement* mutate(CaseStmt     *);
+
+  private:
+    void fillRange(Expression *lo, Expression *hi);
+
+    Expression *currentCondition;
+    std::list< Expression * > newCaseLabels;
+  };
+
+} // namespace ControlStruct
+
+#endif // #ifndef CASERNG_MUTATOR_H
+
+/*
+  Local Variables:
+  mode: c++
+  End:
+*/
Index: translator/ControlStruct/ChooseMutator.cc
===================================================================
--- translator/ControlStruct/ChooseMutator.cc	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
+++ translator/ControlStruct/ChooseMutator.cc	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
@@ -0,0 +1,55 @@
+#include <list>
+
+#include "SynTree/Statement.h"
+#include "ChooseMutator.h"
+
+namespace ControlStruct {
+
+  Statement* ChooseMutator::mutate(ChooseStmt   *chooseStmt)
+  {
+    bool enclosingChoose = insideChoose;
+    insideChoose = true;
+    mutateAll( chooseStmt->get_branches(), *this );
+    insideChoose = enclosingChoose;
+
+    return new SwitchStmt( chooseStmt->get_labels(),  chooseStmt->get_condition(), chooseStmt->get_branches() );
+  }
+
+  Statement* ChooseMutator::mutate(SwitchStmt   *switchStmt)
+  {
+    bool enclosingChoose = insideChoose;
+    insideChoose = false;
+    mutateAll( switchStmt->get_branches(), *this );
+    insideChoose = enclosingChoose;
+
+    return switchStmt;
+  }
+
+  Statement* ChooseMutator::mutate(FallthruStmt *fallthruStmt)
+  {
+    delete fallthruStmt;
+    return new NullStmt();
+  }
+
+  Statement* ChooseMutator::mutate(CaseStmt *caseStmt)
+  {
+
+    std::list< Statement * > &stmts = caseStmt->get_statements();
+
+    if ( insideChoose ) {
+      BranchStmt *posBrk;
+      if ( (( posBrk = dynamic_cast< BranchStmt * > ( stmts.back() ) ) && 
+	    ( posBrk->get_type() == BranchStmt::Break ))  // last statement in the list is a (superfluous) 'break' 
+	   || dynamic_cast< FallthruStmt * > ( stmts.back() ) )
+	; 
+      else {
+	stmts.push_back( new BranchStmt( std::list< Label >(), "", BranchStmt::Break ) );
+      }
+    }
+
+    mutateAll ( stmts, *this );
+
+    return caseStmt;
+  }
+
+} // namespace ControlStruct
Index: translator/ControlStruct/ChooseMutator.h
===================================================================
--- translator/ControlStruct/ChooseMutator.h	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
+++ translator/ControlStruct/ChooseMutator.h	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
@@ -0,0 +1,31 @@
+#ifndef CHOOSE_MUTATOR_H
+#define CHOOSE_MUTATOR_H
+
+#include "SynTree/Mutator.h"
+
+#include "utility.h"
+
+namespace ControlStruct {
+
+  class ChooseMutator : public Mutator
+  {
+  public:
+    ChooseMutator() : insideChoose( false ) {}
+
+    virtual Statement* mutate(ChooseStmt   *);
+    virtual Statement* mutate(SwitchStmt   *);
+    virtual Statement* mutate(FallthruStmt *);
+    virtual Statement* mutate(CaseStmt     *);
+  private:
+    bool insideChoose;
+  };
+
+} // namespace ControlStruct
+
+#endif // #ifndef CHOOSE_MUTATOR_H
+
+/*
+  Local Variables:
+  mode: c++
+  End:
+*/
Index: translator/ControlStruct/ForExprMutator.cc
===================================================================
--- translator/ControlStruct/ForExprMutator.cc	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
+++ translator/ControlStruct/ForExprMutator.cc	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
@@ -0,0 +1,26 @@
+#include "SynTree/Mutator.h"
+#include "SynTree/Statement.h"
+#include "ForExprMutator.h"
+
+namespace ControlStruct {
+  Statement* ForExprMutator::mutate(ForStmt *forStmt)
+  {
+    DeclStmt *decl;
+    if (( decl = dynamic_cast< DeclStmt * > ( forStmt->get_initialization() )) != 0 )
+      {
+	// create compound statement, move declaration outside, leave _for_ as-is
+	CompoundStmt *block = new CompoundStmt( std::list< Label >() );
+	std::list<Statement *> &stmts = block->get_kids();
+
+	stmts.push_back( decl );
+	forStmt->set_initialization( 0 );
+	stmts.push_back( forStmt );
+
+	return block;
+      }
+    // ForStmt still needs to be fixed
+    else
+      return forStmt;
+  }
+
+} // namespace ControlStruct
Index: translator/ControlStruct/ForExprMutator.h
===================================================================
--- translator/ControlStruct/ForExprMutator.h	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
+++ translator/ControlStruct/ForExprMutator.h	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
@@ -0,0 +1,24 @@
+#ifndef FOR_MUTATOR_H
+#define FOR_MUTATOR_H
+
+#include "SynTree/Mutator.h"
+
+#include "utility.h"
+
+namespace ControlStruct {
+
+  class ForExprMutator : public Mutator
+  {
+  public:
+    virtual Statement* mutate(ForStmt   *);
+  };
+
+} // namespace ControlStruct
+
+#endif // #ifndef CHOOSE_MUTATOR_H
+
+/*
+  Local Variables:
+  mode: c++
+  End:
+*/
Index: translator/ControlStruct/LabelFixer.cc
===================================================================
--- translator/ControlStruct/LabelFixer.cc	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
+++ translator/ControlStruct/LabelFixer.cc	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
@@ -0,0 +1,140 @@
+#include <list>
+#include <cassert>
+
+#include "LabelFixer.h"
+#include "MLEMutator.h"
+#include "SynTree/Statement.h"
+#include "SynTree/Declaration.h"
+#include "utility.h"
+
+namespace ControlStruct {
+  LabelFixer::Entry::Entry( Statement *to, Statement *from ) :
+    definition ( to )
+  {
+    if ( from != 0 )
+    usage.push_back( from );
+  }
+
+  bool LabelFixer::Entry::insideLoop()
+  {
+    return ( dynamic_cast< ForStmt * > ( definition ) ||
+	     dynamic_cast< WhileStmt * > ( definition )  );
+  }
+
+  LabelFixer::LabelFixer( LabelGenerator *gen ) : generator ( gen )
+  {
+    if ( generator == 0 )
+      generator = LabelGenerator::getGenerator();
+  }
+
+  void LabelFixer::visit(FunctionDecl *functionDecl)
+  {
+    if ( functionDecl->get_statements() != 0 )
+      functionDecl->get_statements()->accept( *this );
+
+    MLEMutator mlemut( resolveJumps(), generator );
+    functionDecl->acceptMutator( mlemut );
+  }
+
+  void LabelFixer::visit(Statement *stmt )
+  {
+    std::list< Label > &labels = stmt->get_labels();
+
+    if ( ! labels.empty() ) {
+      Label current = setLabelsDef( labels, stmt );
+      labels.clear();
+      labels.push_front( current );
+    }
+  }
+
+  void LabelFixer::visit(BranchStmt *branchStmt)
+  {
+    visit ( ( Statement * )branchStmt );  // the labels this statement might have
+
+    Label target;
+    if ( (target = branchStmt->get_target()) != "" ) {
+      setLabelsUsg( target, branchStmt );
+    } //else       /* computed goto or normal exit-loop statements */
+  }
+
+
+  Label LabelFixer::setLabelsDef( std::list< Label > &llabel, Statement *definition )
+  {
+    assert( definition != 0 );
+    Entry *entry = new Entry( definition );
+    bool used = false;
+
+    for ( std::list< Label >::iterator i = llabel.begin(); i != llabel.end(); i++ )
+      if ( labelTable.find( *i ) == labelTable.end() )
+	{ used = true; labelTable[ *i ] = entry; } // undefined and unused
+      else
+	if( labelTable[ *i ]->defined() )
+	  throw SemanticError("Duplicate definition of label: " + *i );
+	else
+	  labelTable[ *i ]->set_definition( definition );
+
+    if (! used ) delete entry;
+
+    return labelTable[ llabel.front() ]->get_label();  // this *must* exist
+  }
+
+  Label LabelFixer::setLabelsUsg( Label orgValue, Statement *use )
+  {
+    assert( use != 0 );
+
+    if ( labelTable.find( orgValue ) != labelTable.end() )
+      labelTable[ orgValue ]->add_use( use );         // the label has been defined or used before
+    else
+      labelTable[ orgValue ] = new Entry( 0, use );
+
+    return labelTable[ orgValue ]->get_label();
+  }
+
+  std::map < Label, Statement * > *LabelFixer::resolveJumps() throw ( SemanticError )
+  {
+    std::map < Statement *, Entry * > def_us;
+
+    for( std::map < Label, Entry *>::iterator i = labelTable.begin(); i != labelTable.end(); i++ ) {
+      Entry *e = i->second;
+
+      if ( def_us.find ( e->get_definition() ) == def_us.end() )
+	  def_us[ e->get_definition() ] = e;
+      else
+	if(e->used())
+	  def_us[ e->get_definition() ]->add_uses( e->get_uses() );
+    }
+
+    // get rid of labelTable
+    for( std::map < Statement *, Entry * >::iterator i = def_us.begin(); i != def_us.end(); i++ ) {
+      Statement *to = (*i).first;
+      std::list < Statement *> &from = (*i).second->get_uses();
+      Label finalLabel = generator->newLabel();
+      (*i).second->set_label( finalLabel );
+
+      if ( to == 0 ) {
+	BranchStmt *first_use = dynamic_cast<BranchStmt *>(from.back());
+	Label undef("");
+	if ( first_use != 0 )
+	  undef = first_use->get_target();
+	throw SemanticError ( "'" + undef + "' label not defined");
+      }
+
+      to->get_labels().clear();
+      to->get_labels().push_back( finalLabel );
+
+      for ( std::list< Statement *>::iterator j = from.begin(); j != from.end(); j++ ) {
+	BranchStmt *jumpTo = dynamic_cast < BranchStmt * > ( *j );
+	assert( jumpTo != 0 );
+	jumpTo->set_target( finalLabel );
+      }
+    }
+
+    // reverse table
+    std::map < Label, Statement * > *ret = new std::map < Label, Statement * >();
+    for(std::map < Statement *, Entry * >::iterator i = def_us.begin(); i != def_us.end(); i++ ) 
+      (*ret)[ (*i).second->get_label() ] = (*i).first;
+
+    return ret;
+  }
+
+}  // namespace ControlStruct
Index: translator/ControlStruct/LabelFixer.h
===================================================================
--- translator/ControlStruct/LabelFixer.h	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
+++ translator/ControlStruct/LabelFixer.h	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
@@ -0,0 +1,85 @@
+#ifndef LABEL_FIXER_H
+#define LABEL_FIXER_H
+
+#include "utility.h"
+
+#include "SynTree/SynTree.h"
+#include "SynTree/Visitor.h"
+
+#include "LabelGenerator.h"
+
+#include <map>
+
+namespace ControlStruct {
+
+  class LabelFixer : public Visitor 
+    {
+      typedef Visitor Parent;
+
+    public:
+      LabelFixer( LabelGenerator *gen = 0 );
+
+      std::map < Label, Statement * > *resolveJumps() throw ( SemanticError );
+
+      // Declarations
+      virtual void visit(FunctionDecl *functionDecl);
+
+      // Statements
+      void visit(Statement *stmt);
+
+      virtual void visit(CompoundStmt *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
+      virtual void visit(NullStmt     *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
+      virtual void visit(ExprStmt     *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
+      virtual void visit(IfStmt       *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
+      virtual void visit(WhileStmt    *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
+      virtual void visit(ForStmt      *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
+      virtual void visit(SwitchStmt   *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
+      virtual void visit(ChooseStmt   *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
+      virtual void visit(FallthruStmt *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
+      virtual void visit(CaseStmt     *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
+      virtual void visit(ReturnStmt   *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
+      virtual void visit(TryStmt      *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
+      virtual void visit(CatchStmt    *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
+      virtual void visit(DeclStmt     *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
+      virtual void visit(BranchStmt   *branchStmt);
+
+      Label setLabelsDef( std::list< Label > &, Statement *definition );
+      Label setLabelsUsg( Label, Statement *usage = 0 );
+
+    private:
+      class Entry
+      {
+      public:
+	Entry( Statement *to = 0, Statement *from = 0 );
+	bool used() { return ( usage.empty() ); }
+	bool defined() { return ( definition != 0 ); }
+	bool insideLoop();
+
+	Label get_label() const { return label; }
+	Statement *get_definition() const { return definition; }
+	std::list< Statement *> &get_uses() { return usage; }
+
+	void add_use ( Statement *use ) { usage.push_back(use); }
+	void add_uses ( std::list<Statement *> uses ) { usage.insert( usage.end(), uses.begin(), uses.end() ); }
+	void set_definition( Statement *def ) { definition = def; }
+
+	void set_label( Label lab ) { label = lab; }
+	Label gset_label() const { return label; }
+      private:
+	Label label;  
+	Statement *definition;
+	std::list<Statement *> usage;
+      };
+              
+      std::map < Label, Entry *> labelTable;
+      LabelGenerator *generator;
+    };
+} // namespace ControlStruct
+
+#endif // #ifndef LABEL_FIXER_H
+
+/*
+  Local Variables:
+  mode: c++
+  End:
+*/
Index: translator/ControlStruct/LabelGenerator.cc
===================================================================
--- translator/ControlStruct/LabelGenerator.cc	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
+++ translator/ControlStruct/LabelGenerator.cc	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
@@ -0,0 +1,25 @@
+#include <iostream>
+#include <strstream>
+
+#include "LabelGenerator.h"
+
+namespace ControlStruct {
+
+  LabelGenerator *LabelGenerator::labelGenerator = 0;
+
+  LabelGenerator *LabelGenerator::getGenerator() {
+    if ( LabelGenerator::labelGenerator == 0 )
+      LabelGenerator::labelGenerator = new LabelGenerator();
+
+    return labelGenerator;
+  }
+
+  Label LabelGenerator::newLabel() {
+    std::ostrstream os;
+    os << "__L" << current++ << "__";// << std::ends;
+    os.freeze( false );
+    std::string ret = std::string (os.str(), os.pcount());
+    return Label( ret );
+  }
+
+} // namespace ControlStruct
Index: translator/ControlStruct/LabelGenerator.h
===================================================================
--- translator/ControlStruct/LabelGenerator.h	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
+++ translator/ControlStruct/LabelGenerator.h	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
@@ -0,0 +1,32 @@
+#ifndef LABEL_GENERATOR_H
+#define LABEL_GENERATOR_H
+
+#include "SynTree/SynTree.h"
+
+namespace ControlStruct {
+
+  class LabelGenerator
+  {
+  public:
+    static LabelGenerator *getGenerator();
+    Label newLabel();
+    void reset() { current = 0; }
+    void rewind() { current--; }
+
+  protected:
+    LabelGenerator(): current(0) {}
+
+  private:
+    int current;
+    static LabelGenerator *labelGenerator;
+  };
+
+} // namespace ControlStruct
+
+#endif // #ifndef LABEL_GENERATOR_H
+
+/*
+  Local Variables:
+  mode: c++
+  End:
+*/
Index: translator/ControlStruct/LabelTypeChecker.cc
===================================================================
--- translator/ControlStruct/LabelTypeChecker.cc	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
+++ translator/ControlStruct/LabelTypeChecker.cc	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
@@ -0,0 +1,72 @@
+#include <list>
+#include <cassert>
+
+#include "SynTree/Type.h"
+#include "SynTree/Statement.h"
+#include "SynTree/Expression.h"
+#include "SynTree/Declaration.h"
+
+#include "LabelTypeChecker.h"
+
+
+namespace ControlStruct {
+
+  void LabelTypeChecker::visit(UntypedExpr *untypedExpr){
+    assert( untypedExpr != 0 );
+    NameExpr *fname;
+    if( ((fname = dynamic_cast<NameExpr *>(untypedExpr->get_function())) != 0) 
+	&& fname->get_name() == std::string("LabAddress") )
+      std::cerr << "Taking the label of an address." << std::endl;
+    else {
+      acceptAll( untypedExpr->get_results(), *this );
+      acceptAll( untypedExpr->get_args(), *this );
+    }
+    return;
+  }
+
+  void LabelTypeChecker::visit(CompoundStmt *compoundStmt) {
+    index.enterScope();
+    acceptAll( compoundStmt->get_kids(), *this );
+    index.leaveScope();
+  }
+
+  void LabelTypeChecker::visit(DeclStmt *declStmt){
+    declStmt->accept( index );
+
+    //ObjectDecl *odecl = 0;
+    // if( ( odecl = dynamic_cast<ObjectDecl *>(declStmt->get_decl()) ) != 0 ){
+    return;
+  }
+
+  void LabelTypeChecker::visit(BranchStmt *branchStmt) {
+    if( branchStmt->get_type() != BranchStmt::Goto ) return;
+    Expression *target;
+    if( (target = branchStmt->get_computedTarget()) == 0 ) return;
+
+    NameExpr *name;
+    if( ((name = dynamic_cast<NameExpr *>(target)) == 0) )
+      return; // Not a name expression
+    
+    std::list< DeclarationWithType * > interps;
+    index.lookupId(name->get_name(), interps);
+    if ( interps.size() != 1)
+      // in case of multiple declarations
+      throw SemanticError("Illegal label expression: " + name->get_name() );
+
+    PointerType *ptr;
+    if ( (ptr = dynamic_cast<PointerType *>(interps.front()->get_type())) != 0 )
+      if ( dynamic_cast<VoidType *>(ptr->get_base()) != 0 )
+	return;
+      else
+	throw SemanticError("Wrong type of parameter for computed goto");
+    else
+	throw SemanticError("Wrong type of parameter for computed goto");
+
+    return;
+  }
+} // namespace ControlStruct
+
+
+
+
+
Index: translator/ControlStruct/LabelTypeChecker.h
===================================================================
--- translator/ControlStruct/LabelTypeChecker.h	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
+++ translator/ControlStruct/LabelTypeChecker.h	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
@@ -0,0 +1,36 @@
+#ifndef LABEL_TYPE_H
+#define LABEL_TYPE_H
+
+#include "SynTree/Visitor.h"
+#include "SymTab/Indexer.h"
+#include "SynTree/Statement.h"
+
+#include "utility.h"
+
+namespace ControlStruct {
+
+  class LabelTypeChecker : public Visitor
+  {
+  public:
+    //LabelTypeChecker() {
+
+    virtual void visit(CompoundStmt *compoundStmt);
+    virtual void visit(DeclStmt *declStmt);
+    virtual void visit(BranchStmt *branchStmt);
+    virtual void visit(UntypedExpr *untypedExpr);
+  private:
+    SymTab::Indexer index;
+  };
+
+} // namespace ControlStruct
+
+#endif // #ifndef LABEL_TYPE_H
+
+/*
+  Local Variables:
+  mode: c++
+  End:
+*/
+
+
+
Index: translator/ControlStruct/MLEMutator.cc
===================================================================
--- translator/ControlStruct/MLEMutator.cc	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
+++ translator/ControlStruct/MLEMutator.cc	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
@@ -0,0 +1,203 @@
+#include <cassert>
+#include <algorithm>
+
+#include "MLEMutator.h"
+#include "SynTree/Statement.h"
+
+
+namespace ControlStruct {
+  MLEMutator::~MLEMutator()
+  {
+    delete targetTable;
+    targetTable = 0;
+  }
+
+  CompoundStmt* MLEMutator::mutate(CompoundStmt *cmpndStmt)
+  {
+    bool labeledBlock = false;
+    if (!((cmpndStmt->get_labels()).empty())) {
+      labeledBlock = true;
+      enclosingBlocks.push_back( Entry( cmpndStmt ) );
+    }
+
+    std::list< Statement * > &kids = cmpndStmt->get_kids();
+    for( std::list< Statement * >::iterator k = kids.begin(); k != kids.end(); k++ ) {
+      *k = (*k)->acceptMutator(*this);
+
+      if (!get_breakLabel().empty()) {
+	std::list< Statement * >::iterator next = k; next++;
+	if( next == kids.end() ) {
+	  std::list<Label> ls; ls.push_back( get_breakLabel() );
+	  kids.push_back( new NullStmt(ls) );
+	} else
+	  (*next)->get_labels().push_back( get_breakLabel() );
+
+	set_breakLabel("");
+      }
+    }
+
+    if ( labeledBlock ) {
+      assert( ! enclosingBlocks.empty() );
+      if( ! enclosingBlocks.back().get_breakExit().empty() )
+	set_breakLabel( enclosingBlocks.back().get_breakExit() );
+      enclosingBlocks.pop_back();
+    }
+
+    //mutateAll( cmpndStmt->get_kids(), *this );
+    return cmpndStmt;
+  }
+
+  Statement *MLEMutator::mutate( WhileStmt *whileStmt )
+  {
+    enclosingLoops.push_back( Entry( whileStmt ) );
+    whileStmt->set_body ( whileStmt->get_body()->acceptMutator( *this ) );
+
+    Entry &e = enclosingLoops.back();
+    assert ( e == whileStmt );
+    whileStmt->set_body( mutateLoop( whileStmt->get_body(), e ) );
+    enclosingLoops.pop_back();
+
+    return whileStmt;
+  }
+
+  Statement *MLEMutator::mutate( ForStmt *forStmt )
+  {
+    enclosingLoops.push_back( Entry( forStmt ) );
+    maybeMutate( forStmt->get_body(), *this );
+
+    Entry &e = enclosingLoops.back();
+    assert ( e == forStmt );
+    forStmt->set_body( mutateLoop( forStmt->get_body(), e ) );
+    enclosingLoops.pop_back();
+
+    return forStmt;
+  }
+
+  Statement *MLEMutator::mutate( BranchStmt *branchStmt )
+    throw ( SemanticError )
+  {
+    if ( branchStmt->get_type() == BranchStmt::Goto )
+      return branchStmt;
+
+    if ( branchStmt->get_type() == BranchStmt::Continue && enclosingLoops.empty() )
+      throw SemanticError( "'continue' outside a loop" );
+
+  if ( branchStmt->get_type() == BranchStmt::Break && (enclosingLoops.empty() && enclosingSwitches.empty() && enclosingBlocks.empty() ) )
+      throw SemanticError( "'break' outside a loop or switch" );
+
+    if ( branchStmt->get_target() == "" ) return branchStmt;
+
+    if ( targetTable->find( branchStmt->get_target() ) == targetTable->end() )
+      throw SemanticError("The label defined in the exit loop statement does not exist." );  // shouldn't happen (since that's already checked)
+
+    std::list< Entry >::iterator check;
+    if ( ( check = std::find( enclosingLoops.begin(), enclosingLoops.end(), (*targetTable)[branchStmt->get_target()] ) ) == enclosingLoops.end() )
+      // not in loop, checking if in switch/choose
+      if ( (check = std::find( enclosingBlocks.begin(), enclosingBlocks.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingBlocks.end() )
+	// neither in loop nor in block, checking if in switch/choose
+	if ( (check = std::find( enclosingSwitches.begin(), enclosingSwitches.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingSwitches.end() )
+	  throw SemanticError("The target specified in the exit loop statement does not correspond to an enclosing loop.");
+
+    if ( enclosingLoops.back() == (*check) )
+      return branchStmt;                      // exit the innermost loop (labels not necessary)
+
+    Label newLabel;
+    switch( branchStmt->get_type() ) {
+    case BranchStmt::Break:
+      if ( check->get_breakExit() != "" )
+	newLabel = check->get_breakExit();
+      else
+	{ newLabel = generator->newLabel(); check->set_breakExit( newLabel ); }
+      break;
+    case BranchStmt::Continue:
+      if ( check->get_contExit() != "" )
+	newLabel = check->get_contExit();
+      else
+	{ newLabel = generator->newLabel(); check->set_contExit( newLabel ); }
+      break;
+
+    default:
+      // shouldn't be here
+      return 0;
+    }
+
+    return new BranchStmt(std::list<Label>(), newLabel, BranchStmt::Goto );
+  }
+
+
+  Statement *MLEMutator::mutate(SwitchStmt *switchStmt)
+  {
+    Label brkLabel = generator->newLabel();
+    enclosingSwitches.push_back( Entry(switchStmt, "", brkLabel) );
+    mutateAll( switchStmt->get_branches(), *this );
+    {
+      // check if this is necessary (if there is a break to this point, otherwise do not generate
+      std::list<Label> temp; temp.push_back( brkLabel );
+      switchStmt->get_branches().push_back( new BranchStmt( temp, Label(""), BranchStmt::Break ) );
+    }
+    assert ( enclosingSwitches.back() == switchStmt );
+    enclosingSwitches.pop_back();
+    return switchStmt;
+  }
+
+  Statement *MLEMutator::mutate(ChooseStmt *switchStmt)
+  {
+    Label brkLabel = generator->newLabel();
+    enclosingSwitches.push_back( Entry(switchStmt,"", brkLabel) );
+    mutateAll( switchStmt->get_branches(), *this );
+    {
+      // check if this is necessary (if there is a break to this point, otherwise do not generate
+      std::list<Label> temp; temp.push_back( brkLabel );
+      switchStmt->get_branches().push_back( new BranchStmt( temp, Label(""), BranchStmt::Break ) );
+    }
+    assert ( enclosingSwitches.back() == switchStmt );
+    enclosingSwitches.pop_back();
+    return switchStmt;
+  }
+
+  Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) {
+    CompoundStmt *newBody;
+    if ( ! (newBody = dynamic_cast<CompoundStmt *>( bodyLoop )) ) {
+      newBody = new CompoundStmt( std::list< Label >() );
+      newBody->get_kids().push_back( bodyLoop );
+    }
+
+    Label endLabel = e.get_contExit();
+
+    if ( e.get_breakExit() != "" ) {
+      if ( endLabel == "" ) endLabel = generator->newLabel();
+      // check for whether this label is used or not, so as to not generate extraneous gotos
+      if (e.breakExitUsed)
+	newBody->get_kids().push_back( new BranchStmt( std::list< Label >(), endLabel, BranchStmt::Goto ) );
+      // xxx
+      //std::list< Label > ls; ls.push_back( e.get_breakExit() );
+      set_breakLabel( e.get_breakExit() );
+      //newBody->get_kids().push_back( new BranchStmt( ls, "", BranchStmt::Break ) );
+    }
+
+    if ( e.get_breakExit() != "" || e.get_contExit() != "" ){
+      if(dynamic_cast< NullStmt *>( newBody->get_kids().back() ))
+	newBody->get_kids().back()->get_labels().push_back( endLabel );
+      else {
+	std::list < Label > ls; ls.push_back( endLabel );
+	newBody->get_kids().push_back( new NullStmt( ls ) );
+      }
+    }
+
+    return newBody;
+  }
+
+  //*** Entry's methods
+  void MLEMutator::Entry::set_contExit( Label l )
+  {
+    assert ( contExit == "" || contExit == l );
+    contExit = l;
+  }
+
+  void MLEMutator::Entry::set_breakExit( Label l )
+  {
+    assert ( breakExit == "" || breakExit == l );
+    breakExit = l;
+  }
+
+} // namespace ControlStruct
Index: translator/ControlStruct/MLEMutator.h
===================================================================
--- translator/ControlStruct/MLEMutator.h	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
+++ translator/ControlStruct/MLEMutator.h	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
@@ -0,0 +1,75 @@
+#ifndef MLE_MUTATOR_H
+#define MLE_MUTATOR_H
+
+#include <map>
+#include <list>
+
+#include "utility.h"
+#include "SynTree/SynTree.h"
+#include "SynTree/Mutator.h"
+
+#include "LabelGenerator.h"
+
+namespace ControlStruct {
+
+  class MLEMutator : public Mutator {
+    class Entry;
+
+  public:
+    MLEMutator( std::map <Label, Statement *> *t, LabelGenerator *gen = 0 ) : targetTable( t ), breakLabel(std::string("")), generator( gen ) {}
+    ~MLEMutator();
+
+    CompoundStmt *mutate( CompoundStmt *cmpndStmt );
+    Statement *mutate( WhileStmt *whileStmt );
+    Statement *mutate( ForStmt *forStmt );
+    Statement *mutate( BranchStmt *branchStmt ) throw ( SemanticError );
+
+    Statement *mutate( SwitchStmt *switchStmt );
+    Statement *mutate( ChooseStmt *switchStmt );
+
+    Statement *mutateLoop( Statement *bodyLoop, Entry &e );
+
+    Label &get_breakLabel() { return breakLabel; }
+    void set_breakLabel( Label newValue ) { breakLabel = newValue; }
+
+  private:
+    class Entry {
+    public:
+      explicit Entry( Statement *_loop = 0, Label _contExit = Label(""), Label _breakExit = Label("") ) :
+	loop( _loop ), contExit( _contExit ), breakExit( _breakExit ), contExitUsed( false ), breakExitUsed( false ) {}
+
+      bool operator==( const Statement *stmt ) { return ( loop == stmt ) ; }
+      bool operator!=( const Statement *stmt ) { return ( loop != stmt ) ; }
+
+      bool operator==( const Entry &other ) { return ( loop == other.get_loop() ) ; }
+
+      Statement *get_loop() const { return loop; }
+
+      Label get_contExit() const { return contExit; }
+      void set_contExit( Label );
+
+      Label get_breakExit() const { return breakExit; }
+      void set_breakExit( Label );
+
+    private:
+      Statement *loop;
+      Label contExit, breakExit;
+    public: // hack, provide proper [sg]etters
+      bool contExitUsed, breakExitUsed;
+    };
+
+    std::map <Label, Statement *> *targetTable;
+    std::list < Entry > enclosingBlocks,enclosingLoops,enclosingSwitches;
+    Label breakLabel;
+    LabelGenerator *generator;
+  };
+
+} // namespace ControlStruct
+
+#endif
+
+/*
+  Local Variables:
+  mode: c++
+  End:
+*/
Index: translator/ControlStruct/Mutate.cc
===================================================================
--- translator/ControlStruct/Mutate.cc	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
+++ translator/ControlStruct/Mutate.cc	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
@@ -0,0 +1,42 @@
+#include <algorithm>
+#include <iostream>
+#include <cassert>
+#include <list>
+
+#include "Mutate.h"
+#include "ChooseMutator.h"
+#include "LabelFixer.h"
+#include "MLEMutator.h"
+#include "CaseRangeMutator.h"
+#include "ForExprMutator.h"
+#include "LabelTypeChecker.h"
+//#include "ExceptMutator.h"
+
+#include "utility.h"
+
+#include "SynTree/Visitor.h"
+
+using namespace std;
+
+namespace ControlStruct {
+
+  void mutate( std::list< Declaration * > translationUnit )
+  {
+    ChooseMutator chmut;
+    ForExprMutator formut;
+    CaseRangeMutator ranges;  // has to run after ChooseMutator
+    LabelFixer lfix;
+    //ExceptMutator exc;
+    LabelTypeChecker lbl;
+
+    mutateAll( translationUnit , formut );
+    acceptAll( translationUnit , lfix );
+    mutateAll( translationUnit , chmut );
+    mutateAll( translationUnit , ranges );
+    //mutateAll( translationUnit , exc );
+    //acceptAll( translationUnit , lbl );
+  }
+
+} // namespace CodeGen
+
+
Index: translator/ControlStruct/Mutate.h
===================================================================
--- translator/ControlStruct/Mutate.h	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
+++ translator/ControlStruct/Mutate.h	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
@@ -0,0 +1,21 @@
+#ifndef CTRLS_MUTATE_H
+#define CTRLS_MUTATE_H
+
+#include <list>
+#include <iostream>
+
+#include "SynTree/Declaration.h"
+
+namespace ControlStruct {
+
+  void mutate( std::list< Declaration* > translationUnit );
+
+} // namespace ControlStruct
+
+#endif // #ifndef CTRLS_MUTATE_H
+
+/*
+  Local Variables:
+  mode: c++
+  End:
+*/
Index: translator/ControlStruct/module.mk
===================================================================
--- translator/ControlStruct/module.mk	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
+++ translator/ControlStruct/module.mk	(revision 51b734528489f81a5af985bfee9aa3b6625b9774)
@@ -0,0 +1,10 @@
+SRC +=  ControlStruct/LabelGenerator.cc \
+        ControlStruct/LabelFixer.cc \
+        ControlStruct/MLEMutator.cc \
+	ControlStruct/CaseRangeMutator.cc \
+	ControlStruct/Mutate.cc \
+	ControlStruct/ChooseMutator.cc \
+	ControlStruct/ForExprMutator.cc \
+	ControlStruct/LabelTypeChecker.cc \
+	$(NULL)
+
