Lhogho  0.0.027
Data Structures | Defines | Functions | Variables
options.h File Reference

Data Structures

struct  stats_t
 statistics structure More...

Defines

#define EXE_EXT   ""
 default extension for executable files
#define EXE_EXT2   ".run"
 alternative extension for executable files used to avoid overwriting the source file

Functions

void init_options ()
 initializes the Options module
void finit_options ()
 finalizes the Options module
void set_options (int argc, char *argv[])
 sets options from command-line parameters
void output_compiler_name (int show_help)
 outputs the name of the compier
void dump_statistics ()
 dump statistics

Variables

int option_turned_on [OPTIONS_COUNT]
 Values for options.
int option_dump_ast
int option_make_executable
int option_make_executable_compiler
int option_case_insensitive
int option_traditional
int option_memory_statistics
int option_detailed_memory_statistics
int option_variables
int option_user_variables
int option_assembler
int option_runtime
char * option_compiler_filename
 ASCII name of current compiler.
chars_t option_compiler_filename_chars
 name of current compiler
char * option_source_filename
 ASCII name of current source file.
chars_t option_source_filename_chars
 name of current source file
int standalone
 indicate using of standalone compiler (i.e. -x option)
atom_t standalone_arguments
 list of command-line arguments
int stats_allocs
 statistics for usage of ALLOC and DEALLOC
stats_t stats [MAX_ID]
 statistics for each type of atoms

Names of compiler options

#define OPTION_HELP   option_turned_on[0]
 option "-h" and "--help" print this help message
#define OPTION_MAKE_EXECUTABLE   option_turned_on[1]
 option "-x" and "--executable" create executable file
#define OPTION_MAKE_EXECUTABLE_COMPILER   option_turned_on[2]
 option "-xc" and "--executable-compiler" generate executable compiler
#define OPTION_CASE_INSENSITIVE   option_turned_on[3]
 option "-ci" and "--case-insensitive" switch to case-insensitive mode
#define OPTION_TRADITIONAL   option_turned_on[4]
 option "-t" and "--traditional" switch to traditional mode
#define OPTION_ASSEMBLER   option_turned_on[5]
 option "-Za" and "--Zassembler" dump assembler code
#define OPTION_MEMORY_STATISTICS   option_turned_on[6]
 option "-Zm" and "--Zmemory" dump short memory statistics
#define OPTION_DETAILED_MEMORY_STATISTICS   option_turned_on[7]
 option "-Zmad" and "--Zmemory-all-details" dump detailed memory statistics
#define OPTION_RUNTIME   option_turned_on[8]
 option "-Zrt" and "--Zrun-time" dump evaluations at runtime
#define OPTION_DUMP_AST   option_turned_on[9]
 option "-Zt" and "--Ztree" dump abstract syntax trees
#define OPTION_VARIABLES   option_turned_on[10]
 option "-Zv" and "--Zvariables" dump created variables
#define OPTION_USER_VARIABLES   option_turned_on[11]
 option "-Zuv" and "--Zuser-variables" dump user variables
#define FIRST_BASIC_OPTION   0
#define LAST_BASIC_OPTION   4
#define FIRST_ADVANCED_OPTION   5
#define LAST_ADVANCED_OPTION   11
#define OPTIONS_COUNT   (LAST_ADVANCED_OPTION-FIRST_BASIC_OPTION+1)
#define OPTIONS_SUPPORTED_COUNT   OPTIONS_COUNT

Memory tracing

#define ALLOC(size)   alloc(size)
 allocate memory
#define REALLOC(ptr, size)
 reallocate memory
#define XALLOC(size)   xalloc(size)
 allocate executable memory
#define DEALLOC(ptr)   dealloc(ptr)
 deallocate memory
voidalloc (int size)
 allocates dynamic memory
voidreallocate (void *ptr, int size)
voidxalloc (int size)
void dealloc (void *ptr)
 deallocates dynamic memory

Define Documentation

#define OPTION_HELP   option_turned_on[0]
#define FIRST_BASIC_OPTION   0
#define LAST_BASIC_OPTION   4
#define FIRST_ADVANCED_OPTION   5
#define LAST_ADVANCED_OPTION   11
#define EXE_EXT   ""
#define EXE_EXT2   ".run"
#define ALLOC (   size)    alloc(size)
#define REALLOC (   ptr,
  size 
)
Value:
reallocate(ptr, \
                                        size)
#define XALLOC (   size)    xalloc(size)
#define DEALLOC (   ptr)    dealloc(ptr)

Function Documentation

Initalizes options with their default values.

Dumps statistics if -Zm option was switched on

  {
    int j;
    for( j = 0; j<4; j++ )
    {
      DEUSE(option_texts[j][i]);
    }
  }
  DEUSE( standalone_arguments );

  DEALLOC( option_compiler_filename );
  DEALLOC( option_compiler_filename_chars );

  if( option_source_filename_chars )
  {
    DEALLOC( option_source_filename_chars );
  }

#ifdef ADVANCED
  int stats_used=0;
  if( OPTION_DETAILED_MEMORY_STATISTICS )
  {
    dump_statistics();
  }

  if( OPTION_MEMORY_STATISTICS )
  {
    for( i=MIN_ID; i<MAX_ID; i++ )
    stats_used += stats[i].allocs-stats[i].deallocs;
    PRINT("{MEM#%d:%d}\n",stats_used,stats_allocs);
  }
#endif //ADVANCED
}


void set_options ( int  argc,
char *  argv[] 
)
Parameters:
argcargc parameter from the main function
argvargv parameter from the main function

Reads options from the argv array and sets appropriate parameters.

  { // standalone mode -- collect arguments in a list
    for( argc--,argv++; argc--; argv++ )
    {
      atom_t word = decode_word( (unsigned char*)*argv, strlen(*argv), 0 );
      append( word, &standalone_arguments, &last );
    }
  }
  else
  { // compiler mode -- process options till file name and then collect arguments
    for( argc--,argv++; argc--; argv++ )
    {
      if( option_source_filename_chars )
      {
        atom_t word = decode_word( (unsigned char*)*argv, strlen(*argv), 0 );
        append ( word, &standalone_arguments, &last );
      }
      else
      {
        int found = 0;
        chars_t option = UNFILENAME(*argv);
        int len = STRLEN(option);
        int i;
        for( i=0; i<OPTIONS_SUPPORTED_COUNT; i++ )
        {
          int j;
          for( j=0; j<4; j++ )
          {
            if( len==LENGTH(option_texts[j][i]) )
            {
              if( same_strings(1,option,STRING(option_texts[j][i]),len) )
              {
                found = 1;
                option_turned_on[i] = 1;
                break;
              }
            }
          }
        }
        DEALLOC( option );

        if( OPTION_HELP )
        {
          output_compiler_name( 1 );
          exit(0);
        }

        if( found ) continue;
        
        // check for invalid option or second file name
        if( **argv=='-'  )
        {
          chars_t name = UNFILENAME(*argv); // has ALLOC inside
          atom_t word = new_word( name, UNKNOWN );
          atom_t err = new_error( ERROR_UNKNOWN_OPTION, word );
          dumpln( err );
          DEUSE( word );
          DEALLOC( name );
          continue;
        }

        // set file name
        option_source_filename = *argv;
        option_source_filename_chars = UNFILENAME(*argv);
      }
    } // for
  }
}


void output_compiler_name ( int  show_help)
Parameters:
show_helpif set also output commandline options

Outputs the name of the compiler, its language, its platform and build date. If show_help is not zero, output a list of command-line options

  {
    dump( CAR(t) ); t = CDR(t);       // Usage
    
    int i;
    for( i=0; i<OPTIONS_COUNT; i++ )
    {
      int len;

      if( i==FIRST_ADVANCED_OPTION )
      {
        #ifndef ADVANCED
          t = CDR(t);
        #endif
        dumpln( CAR(t) ); t = CDR(t);       // options
      }
      
      // show short version
      len = LENGTH(option_texts[0][i]);
      outter( TEXT("  "), -1 );
      dump( option_texts[0][i] );
      for( ; len<6; len++ ) outter( TEXT(" "), -1 );

      // show long version
      len = LENGTH(option_texts[2][i]);
      if( len )
      {
        dump( option_texts[2][i] );
      }
      else
      {
        len = LENGTH(option_texts[1][i]);
        dump( option_texts[1][i] );
      }
      for( ; len<30; len++ ) outter( TEXT(" "), -1 );

      // show description      
      dumpln( option_texts[3][i] );
    }
  }
  
  DEUSE( texts );
  DEUSE( help_texts );
}


Dumps statistics of memory usage

  {
    stats_used += stats[i].allocs-stats[i].deallocs;
    if( stats[i].allocs )
    {
      PRINT("   %10s %8d %8d %8d %8d\n",
      id_names[i],
      stats[i].max,
      stats[i].allocs,
      stats[i].deallocs,
      stats[i].allocs-stats[i].deallocs );
    }
  }
  PRINT(" %39s\n","----------------");
  PRINT(" %30s %8d\n","Used",stats_used);
  PRINT(" %30s %8d\n","Free",stats_free);
  PRINT(" %39s\n","----------------");
  PRINT(" %30s %8d\n","OS alloc balance",stats_allocs);
}
#endif

void* alloc ( int  size)
Parameters:
sizesize of memory (in bytes) to allocate
Returns:
pointer to allocated memory

This function is used to allocate memory. Depending on the existence of DEBUG_HEAP symbol it either just allocates memory or allocates and dump on the screen allocation data.

                { dumpln(error_texts[ERROR_OUT_OF_MEM]); exit(1); }
  #ifdef ADVANCED
  stats_allocs++;
  #endif
  return buffer;
}

void* reallocate(void * mem, int size)
void* reallocate ( void ptr,
int  size 
)
                { dumpln(error_texts[ERROR_OUT_OF_MEM]); exit(1); }
  return buffer;
}

void* xalloc( int size )
void* xalloc ( int  size)
                { dumpln(error_texts[ERROR_OUT_OF_MEM]); exit(1); }

  #ifdef ADVANCED
  stats_allocs++;
  #endif
  return buffer;
}
void dealloc( void* ptr )
{
void dealloc ( void ptr)
Parameters:
ptrpointer to the memory to deallocate

This function is used to deallocate memory. Depending on the existence of DEBUG_HEAP symbol it either just deallocates memory or deallocates and dumps on the screen deallocation data.


Variable Documentation


[ HOME | INDEX | ATOMS | VARS | REFERENCE ]
Lhogho Developer's Documentation
Tue Feb 7 2012