-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Java-specific ATN data optimization (-2) shift
Add ATNDataReader, ATNDataWriter Clean up ATN serializer/deserializer code
- Loading branch information
Showing
14 changed files
with
332 additions
and
395 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
runtime/Java/src/org/antlr/v4/runtime/atn/ATNDataReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.antlr.v4.runtime.atn; | ||
|
||
public class ATNDataReader { | ||
private final static int JavaOptimizeOffset2 = ((1 << 16) - 1) - ATNDataWriter.JavaOptimizeOffset + 1; | ||
|
||
private final char[] data; | ||
private int p; | ||
|
||
public ATNDataReader(char[] data) { | ||
this.data = data; | ||
} | ||
|
||
public int readUInt32() { | ||
return readUInt16() | (readUInt16() << 16); | ||
} | ||
|
||
public int readUInt16() { | ||
return readUInt16(true); | ||
} | ||
|
||
public int readUInt16(boolean normalize) { | ||
int result = data[p++]; | ||
// Each char value in data is shifted by +2 at the entry to this method. | ||
// This is an encoding optimization targeting the serialized values 0 | ||
// and -1 (serialized to 0xFFFF), each of which are very common in the | ||
// serialized form of the ATN. In the modified UTF-8 that Java uses for | ||
// compiled string literals, these two character values have multi-byte | ||
// forms. By shifting each value by +2, they become characters 2 and 1 | ||
// prior to writing the string, each of which have single-byte | ||
// representations. Since the shift occurs in the tool during ATN | ||
// serialization, each target is responsible for adjusting the values | ||
// during deserialization. | ||
// | ||
// As a special case, note that the first element of data is not | ||
// adjusted because it contains the major version number of the | ||
// serialized ATN, which was fixed at 3 at the time the value shifting | ||
// was implemented. | ||
if (normalize) { | ||
return result >= ATNDataWriter.JavaOptimizeOffset | ||
? result - ATNDataWriter.JavaOptimizeOffset | ||
: result + JavaOptimizeOffset2; | ||
} | ||
return result; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
runtime/Java/src/org/antlr/v4/runtime/atn/ATNDataWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.antlr.v4.runtime.atn; | ||
|
||
import org.antlr.v4.runtime.misc.IntegerList; | ||
|
||
public class ATNDataWriter { | ||
public static final int JavaOptimizeOffset = 2; | ||
|
||
private final IntegerList data; | ||
private final String language; | ||
private final boolean isJava; | ||
|
||
public ATNDataWriter(IntegerList data, String language) { | ||
this.data = data; | ||
this.language = language; | ||
this.isJava = language.equals("Java"); | ||
} | ||
|
||
public void writeUInt32(int value) { | ||
writeUInt16((char)value); | ||
writeUInt16((char)(value >> 16)); | ||
} | ||
|
||
public void writeUInt16(int value) { | ||
writeUInt16(value, true); | ||
} | ||
|
||
public void writeUInt16(int value, boolean optimize) { | ||
if (value < Character.MIN_VALUE || value > Character.MAX_VALUE) { | ||
throw new UnsupportedOperationException("Serialized ATN data element "+ | ||
data.size() + " element " + value + " out of range "+ | ||
(int)Character.MIN_VALUE + ".." + (int)Character.MAX_VALUE); | ||
} | ||
|
||
data.add(isJava && optimize ? (value + JavaOptimizeOffset) & 0xFFFF : value); | ||
} | ||
} |
Oops, something went wrong.