annotate src/share/vm/adlc/filebuff.cpp @ 470:ad8c8ca4ab0f

6785258: Update copyright year Summary: Update copyright for files that have been modified starting July 2008 to Dec 2008 Reviewed-by: katleman, ohair, tbell
author xdono
date Mon, 15 Dec 2008 16:55:11 -0800
parents 4d9884b01ba6
children dbbe28fc66b5
rev   line source
duke@0 1 /*
xdono@470 2 * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
duke@0 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@0 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@0 21 * have any questions.
duke@0 22 *
duke@0 23 */
duke@0 24
duke@0 25 // FILEBUFF.CPP - Routines for handling a parser file buffer
duke@0 26 #include "adlc.hpp"
duke@0 27
duke@0 28 //------------------------------FileBuff---------------------------------------
duke@0 29 // Create a new parsing buffer
duke@0 30 FileBuff::FileBuff( BufferedFile *fptr, ArchDesc& archDesc) : _fp(fptr), _AD(archDesc) {
duke@0 31 _err = fseek(_fp->_fp, 0, SEEK_END); // Seek to end of file
duke@0 32 if (_err) {
duke@0 33 file_error(SEMERR, 0, "File seek error reading input file");
duke@0 34 exit(1); // Exit on seek error
duke@0 35 }
duke@0 36 _filepos = ftell(_fp->_fp); // Find offset of end of file
duke@0 37 _bufferSize = _filepos + 5; // Filepos points to last char, so add padding
duke@0 38 _err = fseek(_fp->_fp, 0, SEEK_SET); // Reset to beginning of file
duke@0 39 if (_err) {
duke@0 40 file_error(SEMERR, 0, "File seek error reading input file\n");
duke@0 41 exit(1); // Exit on seek error
duke@0 42 }
duke@0 43 _filepos = ftell(_fp->_fp); // Reset current file position
never@415 44 _linenum = 0;
duke@0 45
duke@0 46 _bigbuf = new char[_bufferSize]; // Create buffer to hold text for parser
duke@0 47 if( !_bigbuf ) {
duke@0 48 file_error(SEMERR, 0, "Buffer allocation failed\n");
duke@0 49 exit(1); // Exit on allocation failure
duke@0 50 }
duke@0 51 *_bigbuf = '\n'; // Lead with a sentinal newline
duke@0 52 _buf = _bigbuf+1; // Skip sentinal
duke@0 53 _bufmax = _buf; // Buffer is empty
duke@0 54 _bufeol = _bigbuf; // _bufeol points at sentinal
duke@0 55 _filepos = -1; // filepos is in sync with _bufeol
duke@0 56 _bufoff = _offset = 0L; // Offset at file start
duke@0 57
duke@0 58 _bufmax += fread(_buf, 1, _bufferSize-2, _fp->_fp); // Fill buffer & set end value
duke@0 59 if (_bufmax == _buf) {
duke@0 60 file_error(SEMERR, 0, "File read error, no input read\n");
duke@0 61 exit(1); // Exit on read error
duke@0 62 }
duke@0 63 *_bufmax = '\n'; // End with a sentinal new-line
duke@0 64 *(_bufmax+1) = '\0'; // Then end with a sentinal NULL
duke@0 65 }
duke@0 66
duke@0 67 //------------------------------~FileBuff--------------------------------------
duke@0 68 // Nuke the FileBuff
duke@0 69 FileBuff::~FileBuff() {
duke@0 70 delete _bigbuf;
duke@0 71 }
duke@0 72
duke@0 73 //------------------------------get_line----------------------------------------
duke@0 74 char *FileBuff::get_line(void) {
duke@0 75 char *retval;
duke@0 76
duke@0 77 // Check for end of file & return NULL
duke@0 78 if (_bufeol >= _bufmax) return NULL;
duke@0 79
never@415 80 _linenum++;
duke@0 81 retval = ++_bufeol; // return character following end of previous line
duke@0 82 if (*retval == '\0') return NULL; // Check for EOF sentinal
duke@0 83 // Search for newline character which must end each line
duke@0 84 for(_filepos++; *_bufeol != '\n'; _bufeol++)
duke@0 85 _filepos++; // keep filepos in sync with _bufeol
duke@0 86 // _bufeol & filepos point at end of current line, so return pointer to start
duke@0 87 return retval;
duke@0 88 }
duke@0 89
duke@0 90 //------------------------------FileBuffRegion---------------------------------
duke@0 91 // Create a new region in a FileBuff.
duke@0 92 FileBuffRegion::FileBuffRegion( FileBuff* bufr, int soln, int ln,
duke@0 93 int off, int len)
duke@0 94 : _bfr(bufr), _sol(soln), _line(ln), _offset(off), _length(len) {
duke@0 95 _next = NULL; // No chained regions
duke@0 96 }
duke@0 97
duke@0 98 //------------------------------~FileBuffRegion--------------------------------
duke@0 99 // Delete the entire linked list of buffer regions.
duke@0 100 FileBuffRegion::~FileBuffRegion() {
duke@0 101 if( _next ) delete _next;
duke@0 102 }
duke@0 103
duke@0 104 //------------------------------copy-------------------------------------------
duke@0 105 // Deep copy a FileBuffRegion
duke@0 106 FileBuffRegion *FileBuffRegion::copy() {
duke@0 107 if( !this ) return NULL; // The empty buffer region
duke@0 108 FileBuffRegion *br = new FileBuffRegion(_bfr,_sol,_line,_offset,_length);
duke@0 109 if( _next ) br->_next = _next->copy();
duke@0 110 return br;
duke@0 111 }
duke@0 112
duke@0 113 //------------------------------merge------------------------------------------
duke@0 114 // Merge another buffer region into this buffer region. Make overlapping areas
duke@0 115 // become a single region. Remove (delete) the input FileBuffRegion.
duke@0 116 // Since the buffer regions are sorted by file offset, this is a varient of a
duke@0 117 // "sorted-merge" running in linear time.
duke@0 118 FileBuffRegion *FileBuffRegion::merge( FileBuffRegion *br ) {
duke@0 119 if( !br ) return this; // Merging nothing
duke@0 120 if( !this ) return br; // Merging into nothing
duke@0 121
duke@0 122 assert( _bfr == br->_bfr, "" ); // Check for pointer-equivalent buffers
duke@0 123
duke@0 124 if( _offset < br->_offset ) { // "this" starts before "br"
duke@0 125 if( _offset+_length < br->_offset ) { // "this" ends before "br"
duke@0 126 if( _next ) _next->merge( br ); // Merge with remainder of list
duke@0 127 else _next = br; // No more in this list; just append.
duke@0 128 } else { // Regions overlap.
duke@0 129 int l = br->_offset + br->_length - _offset;
duke@0 130 if( l > _length ) _length = l; // Pick larger region
duke@0 131 FileBuffRegion *nr = br->_next; // Get rest of region
duke@0 132 br->_next = NULL; // Remove indication of rest of region
duke@0 133 delete br; // Delete this region (it's been subsumed).
duke@0 134 if( nr ) merge( nr ); // Merge with rest of region
duke@0 135 } // End of if regions overlap or not.
duke@0 136 } else { // "this" starts after "br"
duke@0 137 if( br->_offset+br->_length < _offset ) { // "br" ends before "this"
duke@0 138 FileBuffRegion *nr = new FileBuffRegion(_bfr,_sol,_line,_offset,_length);
duke@0 139 nr->_next = _next; // Structure copy "this" guy to "nr"
duke@0 140 *this = *br; // Structure copy "br" over "this".
duke@0 141 br->_next = NULL; // Remove indication of rest of region
duke@0 142 delete br; // Delete this region (it's been copied)
duke@0 143 merge( nr ); // Finish merging
duke@0 144 } else { // Regions overlap.
duke@0 145 int l = _offset + _length - br->_offset;
duke@0 146 if( l > _length ) _length = l; // Pick larger region
duke@0 147 _offset = br->_offset; // Start with earlier region
duke@0 148 _sol = br->_sol; // Also use earlier line start
duke@0 149 _line = br->_line; // Also use earlier line
duke@0 150 FileBuffRegion *nr = br->_next; // Get rest of region
duke@0 151 br->_next = NULL; // Remove indication of rest of region
duke@0 152 delete br; // Delete this region (it's been subsumed).
duke@0 153 if( nr ) merge( nr ); // Merge with rest of region
duke@0 154 } // End of if regions overlap or not.
duke@0 155 }
duke@0 156 return this;
duke@0 157 }
duke@0 158
duke@0 159 //------------------------------expandtab--------------------------------------
duke@0 160 static int expandtab( ostream &os, int off, char c, char fill1, char fill2 ) {
duke@0 161 if( c == '\t' ) { // Tab?
duke@0 162 do os << fill1; // Expand the tab; Output space
duke@0 163 while( (++off) & 7 ); // Expand to tab stop
duke@0 164 } else { // Normal character
duke@0 165 os << fill2; // Display normal character
duke@0 166 off++; // Increment "cursor" offset
duke@0 167 }
duke@0 168 return off;
duke@0 169 }
duke@0 170
duke@0 171 //------------------------------printline--------------------------------------
duke@0 172 // Print and highlite a region of a line. Return the amount of highliting left
duke@0 173 // to do (i.e. highlite length minus length of line).
duke@0 174 static int printline( ostream& os, const char *fname, int line,
duke@0 175 const char *_sol, int skip, int len ) {
duke@0 176
duke@0 177 // Display the entire tab-expanded line
duke@0 178 os << fname << ":" << line << ": ";
duke@0 179 const char *t = strchr(_sol,'\n')+1; // End of line
duke@0 180 int off = 0; // Cursor offset for tab expansion
duke@0 181 const char *s = _sol; // Nice string pointer
duke@0 182 while( t-s ) { // Display whole line
duke@0 183 char c = *s++; // Get next character to display
duke@0 184 off = expandtab(os,off,c,' ',c);
duke@0 185 }
duke@0 186
duke@0 187 // Display the tab-expanded skippings before underlining.
duke@0 188 os << fname << ":" << line << ": ";
duke@0 189 off = 0; // Cursor offset for tab expansion
duke@0 190 s = _sol; // Restart string pointer
duke@0 191
duke@0 192 // Start underlining.
duke@0 193 if( skip != -1 ) { // The no-start-indicating flag
duke@0 194 const char *u = _sol+skip; // Amount to skip
duke@0 195 while( u-s ) // Display skipped part
duke@0 196 off = expandtab(os,off,*s++,' ',' ');
duke@0 197 os << '^'; // Start region
duke@0 198 off++; // Moved cursor
duke@0 199 len--; // 1 less char to do
duke@0 200 if( *s++ == '\t' ) // Starting character is a tab?
duke@0 201 off = expandtab(os,off,'\t','-','^');
duke@0 202 }
duke@0 203
duke@0 204 // Long region doesn't end on this line
duke@0 205 int llen = (int)(t-s); // Length of line, minus what's already done
duke@0 206 if( len > llen ) { // Doing entire rest of line?
duke@0 207 while( t-s ) // Display rest of line
duke@0 208 off = expandtab(os,off,*s++,'-','-');
duke@0 209 os << '\n'; // EOL
duke@0 210 return len-llen; // Return what's not yet done.
duke@0 211 }
duke@0 212
duke@0 213 // Region does end on this line. This code fails subtly if the region ends
duke@0 214 // in a tab character.
duke@0 215 int i;
duke@0 216 for( i=1; i<len; i++ ) // Underline just what's needed
duke@0 217 off = expandtab(os,off,*s++,'-','-');
duke@0 218 if( i == len ) os << '^'; // Mark end of region
duke@0 219 os << '\n'; // End of marked line
duke@0 220 return 0L; // All done
duke@0 221 }
duke@0 222
duke@0 223 //------------------------------print------------------------------------------
duke@0 224 //std::ostream& operator<< ( std::ostream& os, FileBuffRegion &br ) {
duke@0 225 ostream& operator<< ( ostream& os, FileBuffRegion &br ) {
duke@0 226 if( &br == NULL ) return os; // The empty buffer region
duke@0 227 FileBuffRegion *brp = &br; // Pointer to region
duke@0 228 while( brp ) { // While have chained regions
duke@0 229 brp->print(os); // Print region
duke@0 230 brp = brp->_next; // Chain to next
duke@0 231 }
duke@0 232 return os; // Return final stream
duke@0 233 }
duke@0 234
duke@0 235 //------------------------------print------------------------------------------
duke@0 236 // Print the FileBuffRegion to a stream. FileBuffRegions are printed with the
duke@0 237 // filename and line number to the left, and complete text lines to the right.
duke@0 238 // Selected portions (portions of a line actually in the FileBuffRegion are
duke@0 239 // underlined. Ellipses are used for long multi-line regions.
duke@0 240 //void FileBuffRegion::print( std::ostream& os ) {
duke@0 241 void FileBuffRegion::print( ostream& os ) {
duke@0 242 if( !this ) return; // Nothing to print
duke@0 243 char *s = _bfr->get_line();
duke@0 244 int skip = (int)(_offset - _sol); // Amount to skip to start of data
duke@0 245 int len = printline( os, _bfr->_fp->_name, _line, s, skip, _length );
duke@0 246
duke@0 247 if( !len ) return; // All done; exit
duke@0 248
duke@0 249 // Here we require at least 2 lines
duke@0 250 int off1 = _length - len + skip; // Length of line 1
duke@0 251 int off2 = off1 + _sol; // Offset to start of line 2
duke@0 252 char *s2 = _bfr->get_line(); // Start of line 2
duke@0 253 char *s3 = strchr( s2, '\n' )+1; // Start of line 3 (unread)
duke@0 254 if( len <= (s3-s2) ) { // It all fits on the next line
duke@0 255 printline( os, _bfr->_fp->_name, _line+1, s2, -1, len ); // Print&underline
duke@0 256 return;
duke@0 257 }
duke@0 258
duke@0 259 // Here we require at least 3 lines
duke@0 260 int off3 = off2 + (int)(s3-s2); // Offset to start of line 3
duke@0 261 s3 = _bfr->get_line(); // Start of line 3 (read)
duke@0 262 const char *s4 = strchr( s3, '\n' )+1;// Start of line 4 (unread)
duke@0 263 if( len < (s4-s3) ) { // It all fits on the next 2 lines
duke@0 264 s2 = _bfr->get_line();
duke@0 265 len = printline( os, _bfr->_fp->_name, _line+1, s2, -1, len ); // Line 2
duke@0 266 s3 = _bfr->get_line();
duke@0 267 printline( os, _bfr->_fp->_name, _line+2, s3, -1, len ); // Line 3
duke@0 268 return;
duke@0 269 }
duke@0 270
duke@0 271 // Here we require at least 4 lines.
duke@0 272 // Print only the 1st and last line, with ellipses in middle.
duke@0 273 os << "...\n"; // The ellipses
duke@0 274 int cline = _line+1; // Skipped 2 lines
duke@0 275 do { // Do until find last line
duke@0 276 len -= (int)(s3-s2); // Remove length of line
duke@0 277 cline++; // Next line
duke@0 278 s2 = _bfr->get_line(); // Get next line from end of this line
duke@0 279 s3 = strchr( s2, '\n' ) + 1;// Get end of next line
duke@0 280 } while( len > (s3-s2) ); // Repeat until last line
duke@0 281 printline( os, _bfr->_fp->_name, cline, s2, -1, len ); // Print & underline
duke@0 282 }
duke@0 283
duke@0 284 //------------------------------file_error-------------------------------------
duke@0 285 void FileBuff::file_error(int flag, int linenum, const char *fmt, ...)
duke@0 286 {
duke@0 287 va_list args;
duke@0 288
duke@0 289 va_start(args, fmt);
duke@0 290 switch (flag) {
duke@0 291 case 0: _AD._warnings += _AD.emit_msg(0, flag, linenum, fmt, args);
duke@0 292 case 1: _AD._syntax_errs += _AD.emit_msg(0, flag, linenum, fmt, args);
duke@0 293 case 2: _AD._semantic_errs += _AD.emit_msg(0, flag, linenum, fmt, args);
duke@0 294 default: assert(0, ""); break;
duke@0 295 }
duke@0 296 va_end(args);
duke@0 297 _AD._no_output = 1;
duke@0 298 }