Path: blob/master/src/hotspot/share/adlc/filebuff.cpp
41145 views
/*1* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324// FILEBUFF.CPP - Routines for handling a parser file buffer25#include "adlc.hpp"2627//------------------------------FileBuff---------------------------------------28// Create a new parsing buffer29FileBuff::FileBuff( BufferedFile *fptr, ArchDesc& archDesc) : _fp(fptr), _AD(archDesc) {30_err = fseek(_fp->_fp, 0, SEEK_END); // Seek to end of file31if (_err) {32file_error(SEMERR, 0, "File seek error reading input file");33exit(1); // Exit on seek error34}35_filepos = ftell(_fp->_fp); // Find offset of end of file36_bufferSize = _filepos + 5; // Filepos points to last char, so add padding37_err = fseek(_fp->_fp, 0, SEEK_SET); // Reset to beginning of file38if (_err) {39file_error(SEMERR, 0, "File seek error reading input file\n");40exit(1); // Exit on seek error41}42_filepos = ftell(_fp->_fp); // Reset current file position43_linenum = 0;4445_bigbuf = new char[_bufferSize]; // Create buffer to hold text for parser46if( !_bigbuf ) {47file_error(SEMERR, 0, "Buffer allocation failed\n");48exit(1); // Exit on allocation failure49}50*_bigbuf = '\n'; // Lead with a sentinel newline51_buf = _bigbuf+1; // Skip sentinel52_bufmax = _buf; // Buffer is empty53_bufeol = _bigbuf; // _bufeol points at sentinel54_filepos = -1; // filepos is in sync with _bufeol55_bufoff = _offset = 0L; // Offset at file start5657_bufmax += fread(_buf, 1, _bufferSize-2, _fp->_fp); // Fill buffer & set end value58if (_bufmax == _buf) {59file_error(SEMERR, 0, "File read error, no input read\n");60exit(1); // Exit on read error61}62*_bufmax = '\n'; // End with a sentinel new-line63*(_bufmax+1) = '\0'; // Then end with a sentinel NULL64}6566//------------------------------~FileBuff--------------------------------------67// Nuke the FileBuff68FileBuff::~FileBuff() {69delete[] _bigbuf;70}7172//------------------------------get_line----------------------------------------73char *FileBuff::get_line(void) {74char *retval;7576// Check for end of file & return NULL77if (_bufeol >= _bufmax) return NULL;7879_linenum++;80retval = ++_bufeol; // return character following end of previous line81if (*retval == '\0') return NULL; // Check for EOF sentinel82// Search for newline character which must end each line83for(_filepos++; *_bufeol != '\n'; _bufeol++)84_filepos++; // keep filepos in sync with _bufeol85// _bufeol & filepos point at end of current line, so return pointer to start86return retval;87}8889//------------------------------file_error-------------------------------------90void FileBuff::file_error(int flag, int linenum, const char *fmt, ...)91{92va_list args;9394va_start(args, fmt);95switch (flag) {96case 0: _AD._warnings += _AD.emit_msg(0, flag, linenum, fmt, args);97break;98case 1: _AD._syntax_errs += _AD.emit_msg(0, flag, linenum, fmt, args);99break;100case 2: _AD._semantic_errs += _AD.emit_msg(0, flag, linenum, fmt, args);101break;102default: assert(0, ""); break;103}104va_end(args);105_AD._no_output = 1;106}107108109