public static void cppOutputAllUnitsToFile(String filename) {
    { String codeoutputfile = Stella.makeFileName(filename, Stella.KWD_CPP_CODE, false);
      String headeroutputfile = Stella.makeFileName(filename, Stella.KWD_CPP_HEADER, false);
      List headerunits = List.cppSortUnitsForHeaderFile(((List)(Stella.$TRANSLATIONUNITS$.get())));
      Cons auxiliaryglobals = ((Cons)(headerunits.fifth()));
      TranslationUnit main = ((TranslationUnit)(((Cons)(headerunits.nth(5))).value));
      Module currentnamespacemodule = null;
      Module currentunitmodule = null;

      { OutputFileStream codeoutputstream = null;

        try {
          codeoutputstream = Stella.openOutputFile(codeoutputfile, Stella.NIL);
          { OutputFileStream headeroutputstream = null;

            try {
              headeroutputstream = Stella.openOutputFile(headeroutputfile, Stella.NIL);
              { Object old$CurrentStream$000 = Stella.$CURRENT_STREAM$.get();

                try {
                  Native.setSpecial(Stella.$CURRENT_STREAM$, headeroutputstream);
                  System.out.println("Writing `" + headeroutputfile + "'...");
                  OutputStream.cppOutputFileHeader(headeroutputstream, headeroutputfile);
                  { Cons bucket = null;
                    Cons iter000 = headerunits.theConsList;

                    for (;!(iter000 == Stella.NIL); iter000 = iter000.rest) {
                      bucket = ((Cons)(iter000.value));
                      { TranslationUnit unit = null;
                        Cons iter001 = bucket;
                        int i = Stella.NULL_INTEGER;
                        int iter002 = 1;

                        for (;!(iter001 == Stella.NIL); iter001 = iter001.rest, iter002 = iter002 + 1) {
                          unit = ((TranslationUnit)(iter001.value));
                          i = iter002;
                          if ((unit.theObject != null) &&
                              (!(unit.category == Stella.SYM_STELLA_VERBATIM))) {
                            currentunitmodule = unit.theObject.homeModule();
                            Module.cppOutputNamespaceChange(currentnamespacemodule, currentunitmodule);
                            currentnamespacemodule = currentunitmodule;
                          }
                          { Symbol testValue000 = unit.category;

                            if (testValue000 == Stella.SYM_STELLA_VERBATIM) {
                              if (i == 1) {
                                ((OutputStream)(Stella.$CURRENT_STREAM$.get())).nativeStream.println("// Header files and macros:");
                              }
                              Stella_Object.cppOutputStatement(unit.translation);
                              ((OutputStream)(Stella.$CURRENT_STREAM$.get())).nativeStream.println();
                            }
                            else if (testValue000 == Stella.SYM_STELLA_CLASS) {
                              if (i == 1) {
                                ((OutputStream)(Stella.$CURRENT_STREAM$.get())).nativeStream.println("// Class definitions:");
                              }
                              Cons.cppOutputClass(((Cons)(unit.translation)).rest);
                            }
                            else if (testValue000 == Stella.SYM_STELLA_GLOBAL_VARIABLE) {
                              if (i == 1) {
                                ((OutputStream)(Stella.$CURRENT_STREAM$.get())).nativeStream.println("// Global declarations:");
                              }
                              Cons.cppOutputGlobalDeclaration(((Cons)(unit.translation)));
                            }
                            else if ((testValue000 == Stella.SYM_STELLA_METHOD) ||
                                ((testValue000 == Stella.SYM_STELLA_PRINT_METHOD) ||
                                 (testValue000 == Stella.SYM_STELLA_MACRO))) {
                              if (i == 1) {
                                ((OutputStream)(Stella.$CURRENT_STREAM$.get())).nativeStream.println("// Function signatures:");
                              }
                              { Cons translation = ((Cons)(unit.translation));

                                { GeneralizedSymbol testValue001 = ((GeneralizedSymbol)(translation.value));

                                  if (testValue001 == Stella.SYM_STELLA_CPP_FUNCTION) {
                                    Cons.cppOutputFunctionSignature(translation.rest);
                                    ((OutputStream)(Stella.$CURRENT_STREAM$.get())).nativeStream.println(";");
                                  }
                                  else if (testValue001 == Stella.SYM_STELLA_CPP_DEFPRINT) {
                                    Cons.cppOutputDefprintSignature(translation.rest);
                                    ((OutputStream)(Stella.$CURRENT_STREAM$.get())).nativeStream.println(";");
                                  }
                                  else {
                                  }
                                }
                              }
                            }
                            else if (testValue000 == Stella.SYM_STELLA_AUXILIARY_VARIABLE) {
                              if (i == 1) {
                                ((OutputStream)(Stella.$CURRENT_STREAM$.get())).nativeStream.println("// Auxiliary global declarations:");
                              }
                              Cons.cppOutputGlobalDeclaration(((Cons)(unit.translation)));
                            }
                            else {
                              { OutputStringStream stream000 = OutputStringStream.newOutputStringStream();

                                stream000.nativeStream.print("`" + testValue000 + "' is not a valid case option");
                                throw ((StellaException)(StellaException.newStellaException(stream000.theStringReader()).fillInStackTrace()));
                              }
                            }
                          }
                        }
                      }
                      ((OutputStream)(Stella.$CURRENT_STREAM$.get())).nativeStream.println();
                    }
                  }
                  Module.cppOutputNamespaceFooter(currentnamespacemodule);
                  System.out.println("Writing `" + codeoutputfile + "'...");
                  Native.setSpecial(Stella.$CURRENT_STREAM$, codeoutputstream);
                  OutputStream.cppOutputFileHeader(codeoutputstream, codeoutputfile);
                  if (((Keyword)(Stella.$TRANSLATOROUTPUTLANGUAGE$.get())) == Stella.KWD_CPP) {
                    Stella.cppOutputImportDeclarations(null);
                  }
                  else if (((Keyword)(Stella.$TRANSLATOROUTPUTLANGUAGE$.get())) == Stella.KWD_CPP_STANDALONE) {
                    Stella.cppStandaloneOutputImportDeclarations(headeroutputfile);
                  }
                  else {
                    { OutputStringStream stream001 = OutputStringStream.newOutputStringStream();

                      stream001.nativeStream.print("`" + ((Keyword)(Stella.$TRANSLATOROUTPUTLANGUAGE$.get())) + "' is not a valid case option");
                      throw ((StellaException)(StellaException.newStellaException(stream001.theStringReader()).fillInStackTrace()));
                    }
                  }
                  currentnamespacemodule = null;
                  { TranslationUnit unit = null;
                    Cons iter003 = ((List)(Stella.$TRANSLATIONUNITS$.get())).theConsList;

                    for (;!(iter003 == Stella.NIL); iter003 = iter003.rest) {
                      unit = ((TranslationUnit)(iter003.value));
                      if ((unit.theObject != null) &&
                          ((!(unit.category == Stella.SYM_STELLA_VERBATIM)) &&
                           (!(unit.category == Stella.SYM_STELLA_AUXILIARY_VARIABLE)))) {
                        currentunitmodule = unit.theObject.homeModule();
                        Module.cppOutputNamespaceChange(currentnamespacemodule, currentunitmodule);
                        currentnamespacemodule = currentunitmodule;
                      }
                      { Symbol testValue002 = unit.category;

                        if ((testValue002 == Stella.SYM_STELLA_METHOD) ||
                            ((testValue002 == Stella.SYM_STELLA_PRINT_METHOD) ||
                             (testValue002 == Stella.SYM_STELLA_MACRO))) {
                          if (unit == main) {
                            Stella_Object.cppOutputStatement(Stella_Object.copyConsTree(unit.translation));
                          }
                          else {
                            Stella_Object.cppOutputStatement(unit.translation);
                          }
                        }
                        else if (testValue002 == Stella.SYM_STELLA_VERBATIM) {
                          StringWrapper.cppOutputVerbatim(((StringWrapper)(((Cons)(unit.translation)).rest.value)));
                        }
                        else if (testValue002 == Stella.SYM_STELLA_GLOBAL_VARIABLE) {
                          if (!(TranslationUnit.auxiliaryVariableUnitP(unit))) {
                            Cons.cppOutputVariableDefinition(((Cons)(unit.translation)));
                          }
                        }
                        else {
                        }
                      }
                    }
                  }
                  { TranslationUnit unit = null;
                    Cons iter004 = auxiliaryglobals;

                    for (;!(iter004 == Stella.NIL); iter004 = iter004.rest) {
                      unit = ((TranslationUnit)(iter004.value));
                      currentunitmodule = unit.theObject.homeModule();
                      Module.cppOutputNamespaceChange(currentnamespacemodule, currentunitmodule);
                      currentnamespacemodule = currentunitmodule;
                      Cons.cppOutputVariableDefinition(((Cons)(unit.translation)));
                    }
                  }
                  Module.cppOutputNamespaceFooter(currentnamespacemodule);
                  if (main != null) {
                    {
                      ((OutputStream)(Stella.$CURRENT_STREAM$.get())).nativeStream.println();
                      ((OutputStream)(Stella.$CURRENT_STREAM$.get())).nativeStream.println();
                    }
;
                    TranslationUnit.cppOutputMain(main);
                  }

                } finally {
                  Stella.$CURRENT_STREAM$.set(old$CurrentStream$000);
                }
              }

            } finally {
              if (headeroutputstream != null) {
                headeroutputstream.free();
              }
            }
          }

        } finally {
          if (codeoutputstream != null) {
            codeoutputstream.free();
          }
        }
      }
    }
  }