Skip to content

Commit 18413b4

Browse files
committed
Support long file offsets in parser
1 parent 168bb22 commit 18413b4

File tree

2 files changed

+72
-69
lines changed

2 files changed

+72
-69
lines changed

visualizer/C1Visualizer/CompilationModel/src/main/java/at/ssw/visualizer/parser/Parser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ void Bytecodes(ControlFlowGraphImpl cfg) {
273273
String StringValue() {
274274
String res;
275275
Expect(53);
276-
int beg = la.pos;
276+
long beg = la.pos;
277277
while (StartOf(2)) {
278278
Get();
279279
}
@@ -500,7 +500,7 @@ IRInstructionImpl HIRInstruction() {
500500

501501
String FreeValue() {
502502
String res;
503-
int beg = la.pos;
503+
long beg = la.pos;
504504
while (StartOf(3)) {
505505
Get();
506506
}
@@ -582,7 +582,7 @@ UsePositionImpl UsePosition() {
582582

583583
String NoTrimFreeValue() {
584584
String res;
585-
int beg = la.pos;
585+
long beg = la.pos;
586586
while (StartOf(3)) {
587587
Get();
588588
}

visualizer/C1Visualizer/CompilationModel/src/main/java/at/ssw/visualizer/parser/Scanner.java

Lines changed: 69 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232

3333
class Token {
3434
public int kind; // token kind
35-
public int pos; // token position in bytes in the source text (starting at 0)
36-
public int charPos; // token position in characters in the source text (starting at 0)
35+
public long pos; // token position in bytes in the source text (starting at 0)
36+
public long charPos; // token position in characters in the source text (starting at 0)
3737
public int col; // token column (starting at 1)
3838
public int line; // token line (starting at 1)
3939
public String val; // token value
@@ -54,9 +54,9 @@ class Buffer {
5454
private static final int MIN_BUFFER_LENGTH = 1024; // 1KB
5555
private static final int MAX_BUFFER_LENGTH = MIN_BUFFER_LENGTH * 64; // 64KB
5656
private byte[] buf; // input buffer
57-
private int bufStart; // position of first byte in buffer relative to input stream
57+
private long bufStart; // position of first byte in buffer relative to input stream
5858
private int bufLen; // length of buffer
59-
private int fileLen; // length of input stream (may change if stream is no file)
59+
private long fileLen; // length of input stream (may change if stream is no file)
6060
private int bufPos; // current position in buffer
6161
private RandomAccessFile file; // input stream (seekable)
6262
private InputStream stream; // growing input stream (e.g.: console, network)
@@ -65,21 +65,24 @@ class Buffer {
6565

6666
public Buffer(InputStream s) {
6767
stream = s;
68-
fileLen = bufLen = bufStart = bufPos = 0;
68+
fileLen = 0;
69+
bufLen = 0;
70+
bufStart = 0;
71+
bufPos = 0;
6972
buf = new byte[MIN_BUFFER_LENGTH];
7073
}
7174

7275
public Buffer(String fileName, ProgressHandle progressHandle) {
7376
this.progressHandle = progressHandle;
7477
try {
7578
file = new RandomAccessFile(fileName, "r");
76-
fileLen = (int) file.length();
79+
fileLen = file.length();
7780
if (progressHandle != null) {
7881
progressHandle.start((int)(fileLen / MAX_BUFFER_LENGTH));
7982
}
80-
bufLen = Math.min(fileLen, MAX_BUFFER_LENGTH);
83+
bufLen = (int) Math.min(fileLen, MAX_BUFFER_LENGTH);
8184
buf = new byte[bufLen];
82-
bufStart = Integer.MAX_VALUE; // nothing in buffer so far
85+
bufStart = Long.MAX_VALUE; // nothing in buffer so far
8386
if (fileLen > 0) setPos(0); // setup buffer to position 0 (start)
8487
else bufPos = 0; // index 0 is already after the file, thus setPos(0) is invalid
8588
if (bufLen == fileLen) Close();
@@ -132,29 +135,29 @@ public int Read() {
132135
}
133136

134137
public int Peek() {
135-
int curPos = getPos();
138+
long curPos = getPos();
136139
int ch = Read();
137140
setPos(curPos);
138141
return ch;
139142
}
140143

141144
// beg .. begin, zero-based, inclusive, in byte
142145
// end .. end, zero-based, exclusive, in byte
143-
public String GetString(int beg, int end) {
146+
public String GetString(long beg, long end) {
144147
int len = 0;
145-
char[] buf = new char[end - beg];
146-
int oldPos = getPos();
148+
char[] buf = new char[(int) (end - beg)];
149+
long oldPos = getPos();
147150
setPos(beg);
148151
while (getPos() < end) buf[len++] = (char) Read();
149152
setPos(oldPos);
150153
return new String(buf, 0, len);
151154
}
152155

153-
public int getPos() {
156+
public long getPos() {
154157
return bufPos + bufStart;
155158
}
156159

157-
public void setPos(int value) {
160+
public void setPos(long value) {
158161
if (value >= fileLen && stream != null) {
159162
// Wanted position is after buffer and the stream
160163
// is not seek-able e.g. network or console,
@@ -168,7 +171,7 @@ public void setPos(int value) {
168171
}
169172

170173
if (value >= bufStart && value < bufStart + bufLen) { // already in buffer
171-
bufPos = value - bufStart;
174+
bufPos = (int) (value - bufStart);
172175
} else if (file != null) { // must be swapped in
173176
try {
174177
file.seek(value);
@@ -184,7 +187,7 @@ public void setPos(int value) {
184187
}
185188
} else {
186189
// set the position to the end of the file, Pos will return fileLen.
187-
bufPos = fileLen - bufStart;
190+
throw new InternalError();
188191
}
189192
}
190193

@@ -294,8 +297,8 @@ public class Scanner {
294297

295298
Token t; // current token
296299
int ch; // current input character
297-
int pos; // byte position of current character
298-
int charPos; // position by unicode characters starting with 0
300+
long pos; // byte position of current character
301+
long charPos; // position by unicode characters starting with 0
299302
int col; // column number of current character
300303
int line; // line number of current character
301304
int oldEols; // EOLs that appeared in a comment;
@@ -328,52 +331,52 @@ public class Scanner {
328331
start.set(44, 8);
329332
start.set(34, 9);
330333
start.set(Buffer.EOF, -1);
331-
literals.put("begin_compilation", new Integer(2));
332-
literals.put("name", new Integer(3));
333-
literals.put("method", new Integer(4));
334-
literals.put("date", new Integer(5));
335-
literals.put("end_compilation", new Integer(6));
336-
literals.put("begin_cfg", new Integer(7));
337-
literals.put("id", new Integer(8));
338-
literals.put("caller_id", new Integer(9));
339-
literals.put("end_cfg", new Integer(10));
340-
literals.put("begin_block", new Integer(11));
341-
literals.put("from_bci", new Integer(12));
342-
literals.put("to_bci", new Integer(13));
343-
literals.put("predecessors", new Integer(14));
344-
literals.put("successors", new Integer(15));
345-
literals.put("xhandlers", new Integer(16));
346-
literals.put("flags", new Integer(17));
347-
literals.put("dominator", new Integer(18));
348-
literals.put("loop_index", new Integer(19));
349-
literals.put("loop_depth", new Integer(20));
350-
literals.put("first_lir_id", new Integer(21));
351-
literals.put("last_lir_id", new Integer(22));
352-
literals.put("probability", new Integer(23));
353-
literals.put("end_block", new Integer(24));
354-
literals.put("begin_states", new Integer(25));
355-
literals.put("begin_stack", new Integer(26));
356-
literals.put("end_stack", new Integer(27));
357-
literals.put("begin_locks", new Integer(28));
358-
literals.put("end_locks", new Integer(29));
359-
literals.put("begin_locals", new Integer(30));
360-
literals.put("end_locals", new Integer(31));
361-
literals.put("end_states", new Integer(32));
362-
literals.put("size", new Integer(33));
363-
literals.put("begin_HIR", new Integer(36));
364-
literals.put("end_HIR", new Integer(37));
365-
literals.put("begin_LIR", new Integer(39));
366-
literals.put("end_LIR", new Integer(40));
367-
literals.put("begin_IR", new Integer(41));
368-
literals.put("HIR", new Integer(42));
369-
literals.put("LIR", new Integer(43));
370-
literals.put("end_IR", new Integer(44));
371-
literals.put("begin_intervals", new Integer(46));
372-
literals.put("end_intervals", new Integer(47));
373-
literals.put("begin_nmethod", new Integer(49));
374-
literals.put("end_nmethod", new Integer(50));
375-
literals.put("begin_bytecodes", new Integer(51));
376-
literals.put("end_bytecodes", new Integer(52));
334+
literals.put("begin_compilation", 2);
335+
literals.put("name", 3);
336+
literals.put("method", 4);
337+
literals.put("date", 5);
338+
literals.put("end_compilation", 6);
339+
literals.put("begin_cfg", 7);
340+
literals.put("id", 8);
341+
literals.put("caller_id", 9);
342+
literals.put("end_cfg", 10);
343+
literals.put("begin_block", 11);
344+
literals.put("from_bci", 12);
345+
literals.put("to_bci", 13);
346+
literals.put("predecessors", 14);
347+
literals.put("successors", 15);
348+
literals.put("xhandlers", 16);
349+
literals.put("flags", 17);
350+
literals.put("dominator", 18);
351+
literals.put("loop_index", 19);
352+
literals.put("loop_depth", 20);
353+
literals.put("first_lir_id", 21);
354+
literals.put("last_lir_id", 22);
355+
literals.put("probability", 23);
356+
literals.put("end_block", 24);
357+
literals.put("begin_states", 25);
358+
literals.put("begin_stack", 26);
359+
literals.put("end_stack", 27);
360+
literals.put("begin_locks", 28);
361+
literals.put("end_locks", 29);
362+
literals.put("begin_locals", 30);
363+
literals.put("end_locals", 31);
364+
literals.put("end_states", 32);
365+
literals.put("size", 33);
366+
literals.put("begin_HIR", 36);
367+
literals.put("end_HIR", 37);
368+
literals.put("begin_LIR", 39);
369+
literals.put("end_LIR", 40);
370+
literals.put("begin_IR", 41);
371+
literals.put("HIR", 42);
372+
literals.put("LIR", 43);
373+
literals.put("end_IR", 44);
374+
literals.put("begin_intervals", 46);
375+
literals.put("end_intervals", 47);
376+
literals.put("begin_nmethod", 49);
377+
literals.put("end_nmethod", 50);
378+
literals.put("begin_bytecodes", 51);
379+
literals.put("end_bytecodes", 52);
377380

378381
literalFirstChar = new boolean[0];
379382
for (String literal : literals.keySet()) {
@@ -464,7 +467,7 @@ Token NextToken() {
464467
) NextCh();
465468

466469
int recKind = noSym;
467-
int recEnd = pos;
470+
long recEnd = pos;
468471
t = new Token();
469472
t.pos = pos; t.col = col; t.line = line; t.charPos = charPos;
470473
int state = start.state(ch);
@@ -475,7 +478,7 @@ Token NextToken() {
475478
case -1: { t.kind = eofSym; break loop; } // NextCh already done
476479
case 0: {
477480
if (recKind != noSym) {
478-
tlen = recEnd - t.pos;
481+
tlen = Math.toIntExact(recEnd - t.pos);
479482
SetScannerBehindT();
480483
}
481484
t.kind = recKind; break loop;

0 commit comments

Comments
 (0)