emit.c

     
   1  //! @file emit.c
   2  //! @author J. Marcel van der Veer
   3  //
   4  //! @section Copyright
   5  //
   6  // This file is part of VIF - vintage FORTRAN compiler.
   7  // Copyright 2020-2024 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  //! @section Synopsis
  23  //!
  24  //! Emit C object code.
  25  
  26  #include <vif.h>
  27  
  28  //
  29  // Write intermediate code
  30  //
  31  
  32  char *newpage (char *module, char *section)
  33  {
  34    RECORD str;
  35    _srecordf (str, "\f %s %s", module, section);
  36    return stralloc (str);
  37  }
  38  
  39  void newline (FILE * obj, char *info, int_4 phase, int_4 force)
  40  {
  41    if (page == 0 || (info != NULL && info[0] == '\f')) {
  42      sscanf (&info[1], "%s %s", hmodule, hsection);
  43    }
  44    if (force || line >= LINES_PER_PAGE) {
  45      RECORD str;
  46      page++;
  47      _srecordf (str, "// %s  %16s  ** %-28s ** %-48s PAGE %05d", _strupper (PACKAGE), _strupper (hdate), _strupper (hmodule), _strupper (hsection), page);
  48      for (int_4 k = 0; k < (int_4) strlen (str); k++) {
  49        if (str[k] == '-') {
  50          str[k] = ' ';
  51        }
  52      }
  53      if (page == 1) {
  54        fprintf (obj, "%s\n\n", str);
  55        line = 1;
  56      } else {
  57        fprintf (obj, "\f\n%s\n\n", str);
  58        line = 1;
  59      }
  60      if (phase == LIST) {
  61        fprintf (obj, "\n// I Line    ISN *....*....|....1....|....2....|....3....|....4....|....5....|....6....|....7..*.|....8\n");
  62        line += 2;
  63      }
  64    } else {
  65      fprintf (obj, "\n");
  66      line++;
  67    }
  68  }
  69  
  70  void indentation (FILE *obj, int_4 ind) {
  71    for (int_4 k = 1; k <= ind; k++) {
  72      fprintf (obj, " ");
  73    }
  74  }
  75  
  76  void emit_code (FILE * obj, int_4 proc)
  77  {
  78  // Write the procedure 'proc'.
  79    int_4 nl = FALSE, printed = 0;
  80    indent = 0;
  81    for (int_4 phase = HEADER; phase < MAXPHA; phase++) {
  82      int_4 N = 0;
  83      RECORD prev;
  84      prev[0] = '\0';
  85      for (int_4 c_src = 0; c_src < n_c_src; c_src++) {
  86        C_SRC *lin = &object[c_src];
  87        if (! (lin->proc == proc && lin->phase == phase)) {
  88          continue;
  89        } else if (lin->text != NULL) {
  90          if (lin->text[0] == '\f') {
  91            newline (obj, lin->text, phase, TRUE);
  92            prev[0] = '\0';
  93            N = 0;
  94          } else {
  95            int_4 last = strlen (lin->text) - 1;
  96  // Close block - indent less.
  97            if (strchr (lin->text, '}') != NULL && strchr (lin->text, '{') == NULL) {
  98              indent = _max (0, indent - INDENT);
  99            }
 100  // Indent, but not comments or cpp directives.
 101            if (nl && 
 102                strncmp (lin->text, "# ", 2) != 0 && 
 103                strncmp (lin->text, "//", 2) != 0) {
 104              if (strncmp (lin->text, "#define", 7) != 0) { 
 105                indentation (obj, indent);
 106                printed += INDENT;
 107              }
 108              nl = FALSE;
 109            }
 110  // Write new line.
 111            if (lin->text[last] == '\n') {
 112              nl = TRUE;
 113              last--;
 114            }
 115            if (last >= 0) {
 116  // Open block - indent more.
 117              if (strchr (lin->text, '{') != NULL && strchr (lin->text, '}') == NULL) {
 118                indent += INDENT;
 119              }
 120  // Write respecting LINE_WIDTH.
 121              if (strncmp (lin->text, "#", 1) == 0 || strncmp (lin->text, "//", 2) == 0) {
 122                RECORD str;
 123                bufcpy (str, lin->text, RECLN);
 124                int_4 len = strlen (str);
 125                if (len > 0 && str[len - 1] == '\n') {
 126                  str[len - 1] = '\0';
 127                }
 128                fprintf (obj, "%s", str);
 129              } else if (strncmp (lin->text, "~", 1) == 0) {
 130                RECORD str;
 131                bufcpy (str, lin->text, RECLN);
 132                int_4 len = strlen (str);
 133                if (len > 0 && str[len - 1] == '\n') {
 134                  str[len - 1] = '\0';
 135                }
 136                fprintf (obj, "%s", &str[1]);
 137              } else {
 138                RECORD str;
 139                bufcpy (str, lin->text, RECLN);
 140                char *rest = NULL, *token;
 141                for (token = strtok_r (str, " ", &rest); token != NULL; token = strtok_r (NULL, " ", &rest)) {
 142                  int_4 len = strlen (token);
 143                  if (N > 0) {
 144                    if (strchr (",;)}", token[0]) == NULL) {
 145                      if (strlen (prev) > 0 && strchr ("([", prev[strlen(prev) - 1]) == NULL) {
 146                        fprintf (obj, " ");
 147                        printed++;
 148                      }
 149                    }
 150                  }
 151                  if (printed + len >= LINE_WIDTH) {
 152                    newline (obj, lin->text, phase, FALSE);
 153                    indentation (obj, indent);
 154                    printed = INDENT;
 155                    prev[0] = '\0';
 156                    N = 0;
 157                  }
 158                  RECORD tok;
 159                  bufcpy (tok, token, RECLN);
 160                  int_4 M = strlen (tok);
 161                  if (M > 0 && tok[M - 1] == '\n') {
 162                    tok[M - 1] = '\0';
 163                  }
 164                  fprintf (obj, "%s", tok);
 165                  bufcpy (prev, tok /* token ? */, RECLN);
 166                  printed += strlen (tok);
 167                  N++;
 168                }
 169              }
 170            }
 171  // New line afterwards.
 172            if (nl) {
 173              newline (obj, lin->text, phase, FALSE);
 174              printed = 0;
 175              prev[0] = '\0';
 176              N = 0;
 177            }
 178          }
 179        }
 180      }
 181  // Final new line.
 182      if (proc == 0) {
 183        newline (obj, "\n", MAXPHA, FALSE);
 184      }
 185    }
 186    if (proc > 0) {
 187      newline (obj, "\n", MAXPHA, FALSE);
 188    }
 189  }
 190  
 191  void write_object (char *name)
 192  {
 193  // Object code to file.
 194    FILE *obj;
 195    int_4 proc;
 196    if ((obj = fopen (name, "w")) == NULL) {
 197      FATAL (1101, "cannot open for writing", name);
 198      exit (EXIT_FAILURE);
 199    };
 200    page = 0;
 201    for (proc = 0; proc <= nprocs; proc++) {
 202      emit_code (obj, proc);
 203    }
 204    fclose (obj);
 205  }
     


© 2002-2024 J.M. van der Veer (jmvdveer@xs4all.nl)