src/hotspot/share/adlc/filebuff.cpp
author goetz
Thu, 12 Jul 2018 16:31:28 +0200
changeset 51078 fc6cfe40e32a
parent 47216 71c04702a3d5
permissions -rw-r--r--
8207049: Minor improvements of compiler code. Reviewed-by: kvn, mdoerr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
51078
fc6cfe40e32a 8207049: Minor improvements of compiler code.
goetz
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 2131
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 2131
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 2131
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
// FILEBUFF.CPP - Routines for handling a parser file buffer
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
#include "adlc.hpp"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
//------------------------------FileBuff---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
// Create a new parsing buffer
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
FileBuff::FileBuff( BufferedFile *fptr, ArchDesc& archDesc) : _fp(fptr), _AD(archDesc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
  _err = fseek(_fp->_fp, 0, SEEK_END);  // Seek to end of file
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  if (_err) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
    file_error(SEMERR, 0, "File seek error reading input file");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
    exit(1);                    // Exit on seek error
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  _filepos = ftell(_fp->_fp);   // Find offset of end of file
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  _bufferSize = _filepos + 5;   // Filepos points to last char, so add padding
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  _err = fseek(_fp->_fp, 0, SEEK_SET);  // Reset to beginning of file
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  if (_err) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
    file_error(SEMERR, 0, "File seek error reading input file\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
    exit(1);                    // Exit on seek error
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  _filepos = ftell(_fp->_fp);      // Reset current file position
1495
128fe18951ed 6754519: don't emit flag fixup for NaN when condition being tested doesn't need it
never
parents: 1
diff changeset
    44
  _linenum = 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  _bigbuf = new char[_bufferSize]; // Create buffer to hold text for parser
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  if( !_bigbuf ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
    file_error(SEMERR, 0, "Buffer allocation failed\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
    exit(1);                    // Exit on allocation failure
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  }
2131
98f9cef66a34 6810672: Comment typos
twisti
parents: 2129
diff changeset
    51
  *_bigbuf = '\n';               // Lead with a sentinel newline
98f9cef66a34 6810672: Comment typos
twisti
parents: 2129
diff changeset
    52
  _buf = _bigbuf+1;                     // Skip sentinel
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  _bufmax = _buf;               // Buffer is empty
2131
98f9cef66a34 6810672: Comment typos
twisti
parents: 2129
diff changeset
    54
  _bufeol = _bigbuf;              // _bufeol points at sentinel
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  _filepos = -1;                 // filepos is in sync with _bufeol
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  _bufoff = _offset = 0L;       // Offset at file start
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  _bufmax += fread(_buf, 1, _bufferSize-2, _fp->_fp); // Fill buffer & set end value
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  if (_bufmax == _buf) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
    file_error(SEMERR, 0, "File read error, no input read\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
    exit(1);                     // Exit on read error
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  }
2131
98f9cef66a34 6810672: Comment typos
twisti
parents: 2129
diff changeset
    63
  *_bufmax = '\n';               // End with a sentinel new-line
98f9cef66a34 6810672: Comment typos
twisti
parents: 2129
diff changeset
    64
  *(_bufmax+1) = '\0';           // Then end with a sentinel NULL
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
//------------------------------~FileBuff--------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
// Nuke the FileBuff
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
FileBuff::~FileBuff() {
46478
f800745ba3cb 8180473: Use proper deallocation for FileBuff::_bigbuf
zmajo
parents: 24425
diff changeset
    70
  delete[] _bigbuf;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
//------------------------------get_line----------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
char *FileBuff::get_line(void) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  char *retval;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  // Check for end of file & return NULL
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  if (_bufeol >= _bufmax) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
1495
128fe18951ed 6754519: don't emit flag fixup for NaN when condition being tested doesn't need it
never
parents: 1
diff changeset
    80
  _linenum++;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  retval = ++_bufeol;      // return character following end of previous line
2131
98f9cef66a34 6810672: Comment typos
twisti
parents: 2129
diff changeset
    82
  if (*retval == '\0') return NULL; // Check for EOF sentinel
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  // Search for newline character which must end each line
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  for(_filepos++; *_bufeol != '\n'; _bufeol++)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
    _filepos++;                    // keep filepos in sync with _bufeol
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  // _bufeol & filepos point at end of current line, so return pointer to start
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  return retval;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
//------------------------------file_error-------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
void FileBuff::file_error(int flag, int linenum, const char *fmt, ...)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  va_list args;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  va_start(args, fmt);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  switch (flag) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  case 0: _AD._warnings += _AD.emit_msg(0, flag, linenum, fmt, args);
51078
fc6cfe40e32a 8207049: Minor improvements of compiler code.
goetz
parents: 47216
diff changeset
    98
    break;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  case 1: _AD._syntax_errs += _AD.emit_msg(0, flag, linenum, fmt, args);
51078
fc6cfe40e32a 8207049: Minor improvements of compiler code.
goetz
parents: 47216
diff changeset
   100
    break;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  case 2: _AD._semantic_errs += _AD.emit_msg(0, flag, linenum, fmt, args);
51078
fc6cfe40e32a 8207049: Minor improvements of compiler code.
goetz
parents: 47216
diff changeset
   102
    break;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  default: assert(0, ""); break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  va_end(args);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  _AD._no_output = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
}