a68g-common.h

     
   1  //! @file a68g-common.h
   2  //! @author J. Marcel van der Veer
   3  //!
   4  //! @section Copyright
   5  //!
   6  //! This file is part of Algol68G - an Algol 68 compiler-interpreter.
   7  //! Copyright 2001-2023 J. Marcel van der Veer [algol68g@xs4all.nl].
   8  //!
   9  //! @section License
  10  //!
  11  //! This program is free software; you can redistribute it and/or modify it 
  12  //! under the terms of the GNU General Public License as published by the 
  13  //! Free Software Foundation; either version 3 of the License, or 
  14  //! (at your option) any later version.
  15  //!
  16  //! This program is distributed in the hope that it will be useful, but 
  17  //! WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  18  //! or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 
  19  //! more details. You should have received a copy of the GNU General Public 
  20  //! License along with this program. If not, see [http://www.gnu.org/licenses/].
  21  
  22  #if !defined (__A68G_COMMON_H__)
  23  #define __A68G_COMMON_H__
  24  
  25  typedef struct MODULE_T MODULE_T;
  26  struct MODULE_T
  27  {
  28    BOOL_T tree_listing_safe, cross_reference_safe;
  29    FILES_T files;
  30    int error_count, warning_count, source_scan;
  31    jmp_buf rendez_vous;
  32    LINE_T *top_line;
  33    MOID_T *top_moid, *standenv_moid;
  34    NODE_T *top_node;
  35    OPTIONS_T options;
  36    PROP_T global_prop;
  37    REFINEMENT_T *top_refinement;
  38    struct
  39    {
  40      LINE_T *save_l;
  41      char *save_s, save_c;
  42    } scan_state;
  43  };
  44  
  45  typedef struct MODE_CACHE_T MODE_CACHE_T;
  46  #define MODE(p)        A68 (a68_modes.p)
  47  struct MODE_CACHE_T
  48  {
  49    MOID_T *proc_bool;
  50    MOID_T *proc_char;
  51    MOID_T *proc_complex_complex;
  52    MOID_T *proc_int;
  53    MOID_T *proc_int_int;
  54    MOID_T *proc_int_int_real;
  55    MOID_T *proc_int_real;
  56    MOID_T *proc_int_real_real;
  57    MOID_T *proc_int_real_real_real;
  58    MOID_T *proc_real;
  59    MOID_T *proc_real_int_real;
  60    MOID_T *proc_real_real;
  61    MOID_T *proc_real_real_int_real;
  62    MOID_T *proc_real_real_real;
  63    MOID_T *proc_real_real_real_int;
  64    MOID_T *proc_real_real_real_real;
  65    MOID_T *proc_real_real_real_real_real;
  66    MOID_T *proc_real_real_real_real_real_real;
  67    MOID_T *proc_real_ref_real_ref_int_void;
  68    MOID_T *proc_void;
  69  };
  70  
  71  #define MAX_OPEN_FILES 64       // Some OS's won't open more than this number
  72  #define MAX_TRANSPUT_BUFFER (MAX_OPEN_FILES)
  73  
  74  typedef struct FILE_ENTRY FILE_ENTRY;
  75  struct FILE_ENTRY
  76  {
  77    NODE_T *pos;
  78    BOOL_T is_open, is_tmp;
  79    FILE_T fd;
  80    A68_REF idf;
  81  };
  82  
  83  // Administration for common (sub) expression elimination.
  84  // BOOK keeps track of already seen (temporary) variables and denotations.
  85  
  86  enum
  87  { BOOK_NONE = 0, BOOK_DECL, BOOK_INIT, BOOK_DEREF, BOOK_ARRAY, BOOK_COMPILE };
  88  
  89  typedef struct BOOK_T BOOK_T;
  90  struct BOOK_T
  91  {
  92    int action, phase;
  93    char *idf;
  94    void *info;
  95    int number;
  96  };
  97  
  98  typedef struct UNIC_T UNIC_T;
  99  struct UNIC_T
 100  {
 101    char *fun;
 102  };
 103  
 104  //
 105  
 106  #define A68(z)         (common.z)
 107  #define A68_JOB        A68 (job)
 108  #define A68_STANDENV   A68 (standenv)
 109  #define A68_MCACHE(z)  A68 (mode_cache.z)
 110  
 111  #define A68_SP         A68 (stack_pointer)
 112  #define A68_FP         A68 (frame_pointer)
 113  #define A68_HP         A68 (heap_pointer)
 114  #define A68_GLOBALS    A68 (global_pointer)
 115  #define A68_STACK      A68 (stack_segment)
 116  #define A68_HEAP       A68 (heap_segment)
 117  #define A68_HANDLES    A68 (handle_segment)
 118  
 119  typedef struct GC_GLOBALS_T GC_GLOBALS_T;
 120  #define A68_GC(z)      A68 (gc.z)
 121  struct GC_GLOBALS_T
 122  {
 123    A68_HANDLE *available_handles, *busy_handles;
 124    UNSIGNED_T free_handles, max_handles, sweeps, refused, freed, total;
 125    unt preemptive;
 126    REAL_T seconds;
 127  };
 128  
 129  typedef struct INDENT_GLOBALS_T INDENT_GLOBALS_T;
 130  #define A68_INDENT(z)  A68 (indent.z)
 131  struct INDENT_GLOBALS_T
 132  {
 133    FILE_T fd;
 134    int ind, col;
 135    int indentation;
 136    BOOL_T use_folder;
 137  };
 138  
 139  #define MON_STACK_SIZE 32
 140  
 141  typedef struct MONITOR_GLOBALS_T MONITOR_GLOBALS_T;
 142  #define A68_MON(z)     A68 (mon.z)
 143  struct MONITOR_GLOBALS_T
 144  {
 145    ADDR_T finish_frame_pointer;
 146    BOOL_T in_monitor;
 147    BOOL_T prompt_set;
 148    BUFFER prompt, symbol, error_text, expr;
 149    char *watchpoint_expression;
 150    int break_proc_level;
 151    int current_frame;
 152    int max_row_elems;
 153    int mon_errors;
 154    int _m_sp;
 155    int pos, attr;
 156    int tabs;
 157    MOID_T *_m_stack[MON_STACK_SIZE];
 158  };
 159  
 160  typedef struct MP_GLOBALS_T MP_GLOBALS_T;
 161  #define A68_MP(z)      A68 (mp.z)
 162  
 163  struct MP_GLOBALS_T
 164  {
 165    int mp_gamma_size;
 166    int mp_ln_10_size;
 167    int mp_ln_scale_size;
 168    int mp_one_size;
 169    int mp_pi_size;
 170    int varying_mp_digits;
 171    MP_T *mp_180_over_pi;
 172    MP_T **mp_gam_ck;
 173    MP_T *mp_half_pi;
 174    MP_T *mp_ln_10;
 175    MP_T *mp_ln_pi;
 176    MP_T *mp_ln_scale;
 177    MP_T *mp_one;
 178    MP_T *mp_pi;
 179    MP_T *mp_pi_over_180;
 180    MP_T *mp_sqrt_pi;
 181    MP_T *mp_sqrt_two_pi;
 182    MP_T *mp_two_pi;
 183  };
 184  
 185  #define MAX_BOOK 1024
 186  #define MAX_UNIC 2048
 187  
 188  typedef struct OPTIMISER_GLOBALS_T OPTIMISER_GLOBALS_T;
 189  #define A68_OPT(z)     A68 (optimiser.z)
 190  struct OPTIMISER_GLOBALS_T
 191  {
 192    BOOK_T cse_book[MAX_BOOK];
 193    BOOL_T put_idf_comma;
 194    DEC_T *root_idf;
 195    int code_errors;
 196    int cse_pointer;
 197    int indentation;
 198    int OPTION_CODE_LEVEL;
 199    int procedures;
 200    int unic_pointer;
 201    UNIC_T unic_functions[MAX_UNIC];
 202  };
 203  
 204  #if defined (BUILD_PARALLEL_CLAUSE)
 205  
 206  typedef struct A68_STACK_DESCRIPTOR A68_STACK_DESCRIPTOR;
 207  typedef struct A68_THREAD_CONTEXT A68_THREAD_CONTEXT;
 208  
 209  struct A68_STACK_DESCRIPTOR
 210  {
 211    ADDR_T cur_ptr, ini_ptr;
 212    BYTE_T *swap, *start;
 213    int bytes;
 214  };
 215  
 216  struct A68_THREAD_CONTEXT
 217  {
 218    A68_STACK_DESCRIPTOR stack, frame;
 219    BOOL_T active;
 220    BYTE_T *thread_stack_offset;
 221    int stack_used;
 222    NODE_T *unit;
 223    pthread_t parent, id;
 224  };
 225  
 226  // Set an upper limit for number of threads.
 227  // Note that _POSIX_THREAD_THREADS_MAX may be ULONG_MAX.
 228  
 229  #define THREAD_LIMIT   256
 230  
 231  #if (_POSIX_THREAD_THREADS_MAX < THREAD_LIMIT)
 232  #undef  THREAD_LIMIT
 233  #define THREAD_LIMIT   _POSIX_THREAD_THREADS_MAX
 234  #endif
 235  
 236  #if !defined _POSIX_THREAD_THREADS_MAX
 237  #define _POSIX_THREAD_THREADS_MAX   (THREAD_LIMIT)
 238  #endif
 239  
 240  #if (_POSIX_THREAD_THREADS_MAX < THREAD_LIMIT)
 241  #define THREAD_MAX     (_POSIX_THREAD_THREADS_MAX)
 242  #else
 243  #define THREAD_MAX     (THREAD_LIMIT)
 244  #endif
 245  
 246  typedef struct PARALLEL_GLOBALS_T PARALLEL_GLOBALS_T;
 247  #define A68_PAR(z)     A68 (parallel.z)
 248  struct PARALLEL_GLOBALS_T
 249  {
 250    A68_THREAD_CONTEXT context[THREAD_MAX];
 251    ADDR_T fp0, sp0;
 252    BOOL_T abend_all_threads, exit_from_threads;
 253    int context_index;
 254    int par_return_code;
 255    jmp_buf *jump_buffer;
 256    NODE_T *jump_label;
 257    pthread_mutex_t unit_sema;
 258    pthread_t main_thread_id;
 259    pthread_t parent_thread_id;
 260  };
 261  
 262  #endif
 263  
 264  typedef struct PARSER_GLOBALS_T PARSER_GLOBALS_T;
 265  #define A68_PARSER(z)  A68 (parser.z)
 266  struct PARSER_GLOBALS_T
 267  {
 268    TAG_T *error_tag;
 269    BOOL_T stop_scanner, read_error, no_preprocessing;
 270    char *scan_buf;
 271    int max_scan_buf_length, source_file_size;
 272    int reductions;
 273    int tag_number;
 274    jmp_buf bottom_up_crash_exit, top_down_crash_exit;
 275  };
 276  
 277  typedef struct GLOBALS_T GLOBALS_T;
 278  
 279  struct GLOBALS_T
 280  {
 281    A68_CHANNEL stand_draw_channel, stand_error_channel, associate_channel, skip_channel;
 282    A68_CHANNEL stand_in_channel, stand_out_channel, stand_back_channel;
 283    A68_PROCEDURE on_gc_event;
 284    A68_REF stand_in, stand_out, stand_back, stand_error, skip_file;
 285    ADDR_T fixed_heap_pointer, temp_heap_pointer;
 286    ADDR_T frame_pointer, stack_pointer, heap_pointer, global_pointer;
 287    ADDR_T frame_start, frame_end, stack_start, stack_end;
 288    BOOL_T close_tty_on_exit;
 289    BOOL_T curses_mode;
 290    BOOL_T do_confirm_exit; 
 291    BOOL_T halt_typing;
 292    BOOL_T heap_is_fluid;
 293    BOOL_T in_execution;
 294    BOOL_T in_monitor;
 295    BOOL_T no_warnings;
 296    BYTE_T *stack_segment, *heap_segment, *handle_segment;
 297    BYTE_T *system_stack_offset;
 298    BUFFER a68_cmd_name;
 299    char **argv;
 300    char *f_plugin;
 301    char *marker[BUFFER_SIZE];
 302    BUFFER output_line, edit_line, input_line;
 303    clock_t clock_res;
 304    FILE_ENTRY file_entries[MAX_OPEN_FILES];
 305    GC_GLOBALS_T gc;
 306    INDENT_GLOBALS_T indent;
 307    int argc;
 308    int chars_in_tty_line;
 309    int global_level, max_lex_lvl;
 310    int max_simplout_size;
 311    int new_nodes, new_modes, new_postulates, new_node_infos, new_genie_infos;
 312    int ret_code, ret_line_number, ret_char_number;
 313    int symbol_table_count, mode_count; 
 314    int term_heigth, term_width;
 315    jmp_buf genie_exit_label;
 316    KEYWORD_T *top_keyword;
 317    MODE_CACHE_T mode_cache;
 318    MODES_T a68_modes;
 319    MODULE_T job;
 320    MONITOR_GLOBALS_T mon;
 321    MP_GLOBALS_T mp;
 322    NODE_T *f_entry;
 323    NODE_T **node_register;
 324    OPTIMISER_GLOBALS_T optimiser;
 325    OPTIONS_T *options;
 326    PARSER_GLOBALS_T parser;
 327    POSTULATE_T *postulates, *top_postulate, *top_postulate_list;
 328    REAL_T cputime_0;
 329    SOID_T *top_soid_list;
 330    TABLE_T *standenv;
 331    TAG_T *error_tag;
 332    TOKEN_T *top_token;
 333    unt frame_stack_size, expr_stack_size, heap_size, handle_pool_size, stack_size;
 334    unt stack_limit, frame_stack_limit, expr_stack_limit;
 335    unt storage_overhead;
 336  #if defined (BUILD_PARALLEL_CLAUSE)
 337    PARALLEL_GLOBALS_T parallel;
 338  #endif
 339  };
 340  
 341  extern GLOBALS_T common;
 342  
 343  #endif