Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constant folding for Python and other runtimes #3701

Merged
merged 2 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public class CustomDescriptors {
getLargeLexerDescriptor(),
getAtnStatesSizeMoreThan65535Descriptor()
});
descriptors.put("ParserExec",
new RuntimeTestDescriptor[] {
getMultiTokenAlternativeDescriptor()
});
}

private static RuntimeTestDescriptor getLineSeparatorLfDescriptor() {
Expand Down Expand Up @@ -146,4 +150,48 @@ private static RuntimeTestDescriptor getAtnStatesSizeMoreThan65535Descriptor() {
null, false, false,
new String[] {"CSharp", "Python2", "Python3", "Go", "PHP", "Swift", "JavaScript", "Dart"}, uri);
}

private static RuntimeTestDescriptor getMultiTokenAlternativeDescriptor() {
final int tokensCount = 64;

StringBuilder rule = new StringBuilder("r1: ");
StringBuilder tokens = new StringBuilder();
StringBuilder input = new StringBuilder();
StringBuilder output = new StringBuilder();

for (int i = 0; i < tokensCount; i++) {
String currentToken = "T" + i;
rule.append(currentToken);
if (i < tokensCount - 1) {
rule.append(" | ");
} else {
rule.append(";");
}
tokens.append(currentToken).append(": '").append(currentToken).append("';\n");
input.append(currentToken).append(" ");
output.append(currentToken);
}
String currentToken = "T" + tokensCount;
tokens.append(currentToken).append(": '").append(currentToken).append("';\n");
input.append(currentToken).append(" ");
output.append(currentToken);

String grammar = "grammar P;\n" +
"r: (r1 | T" + tokensCount + ")+ EOF {<writeln(\"$text\")>};\n" +
rule + "\n" +
tokens + "\n" +
"WS: [ ]+ -> skip;";

return new RuntimeTestDescriptor(
GrammarType.Parser,
"MultiTokenAlternative",
"https://github.com/antlr/antlr4/issues/3698, https://github.com/antlr/antlr4/issues/3703",
input.toString(),
output + "\n",
"",
"r",
"P",
grammar,
null, false, false, null, uri);
}
}
22 changes: 1 addition & 21 deletions runtime/Swift/Sources/Antlr4/misc/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/// Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
/// Use of this file is governed by the BSD 3-clause license that
/// can be found in the LICENSE.txt file in the project root.
///
///


import Foundation
Expand Down Expand Up @@ -39,24 +39,4 @@ public class Utils {
}
return m
}

public static func bitLeftShift(_ n: Int) -> Int64 {
return (Int64(1) &<< n)
}


public static func testBitLeftShiftArray(_ nArray: [Int],_ bitsShift: Int) -> Bool {
let test: Bool = (((nArray[0] - bitsShift) & ~0x3f) == 0)

var temp: Int64 = Int64(nArray[0] - bitsShift)
temp = (temp < 0) ? (64 + (temp % 64 )) : (temp % 64)
let test1: Int64 = (Int64(1) << temp)

var test2: Int64 = Utils.bitLeftShift(nArray[1] - bitsShift)

for i in 1 ..< nArray.count {
test2 = test2 | Utils.bitLeftShift(nArray[i] - bitsShift)
}
return test && (( test1 & test2 ) != 0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ ErrorHandler.Sync(this);
<if(choice.label)><labelref(choice.label)> = TokenStream.LT(1);<endif>
<preamble; separator="\n">
switch (TokenStream.LA(1)) {
<choice.altLook,alts:{look,alt| <cases(ttypes=look)>
<choice.altLook,alts:{look,alt| <cases(tokens=look)>
<alt>
break;}; separator="\n">
default:
Expand All @@ -569,7 +569,7 @@ LL1OptionalBlock(choice, alts, error) ::= <<
State = <choice.stateNumber>;
ErrorHandler.Sync(this);
switch (TokenStream.LA(1)) {
<choice.altLook,alts:{look,alt| <cases(ttypes=look)>
<choice.altLook,alts:{look,alt| <cases(tokens=look)>
<alt>
break;}; separator="\n">
default:
Expand Down Expand Up @@ -676,17 +676,17 @@ Sync(s) ::= "Sync(<s.expecting.name>);"
ThrowNoViableAlt(t) ::= "throw new NoViableAltException(this);"

TestSetInline(s) ::= <<
<s.bitsets:{bits | <if(rest(rest(bits.ttypes)))><bitsetBitfieldComparison(s, bits)><else><bitsetInlineComparison(s, bits)><endif>}; separator=" || ">
<s.bitsets:{bits | <if(rest(rest(bits.tokens)))><bitsetBitfieldComparison(s, bits)><else><bitsetInlineComparison(s, bits)><endif>}; separator=" || ">
>>

// Java language spec 15.19 - shift operators mask operands rather than overflow to 0... need range test
testShiftInRange(shiftAmount) ::= <<
((<shiftAmount>) & ~0x3f) == 0
>>

// produces smaller bytecode only when bits.ttypes contains more than two items
// produces smaller bytecode only when bits.tokens contains more than two items
bitsetBitfieldComparison(s, bits) ::= <%
(<testShiftInRange({<offsetShift(s.varName, bits.shift)>})> && ((1L \<\< <offsetShift(s.varName, bits.shift)>) & (<bits.ttypes:{ttype | (1L \<\< <offsetShift(tokenType.(ttype), bits.shift)>)}; separator=" | ">)) != 0)
<testShiftInRange({<offsetShift(s.varName, bits.shift)>})> && ((1L \<\< <offsetShift(s.varName, bits.shift)>) & <bits.calculated>L) != 0
%>

isZero ::= [
Expand All @@ -698,13 +698,12 @@ offsetShift(shiftAmount, offset) ::= <%
<if(!isZero.(offset))>(<shiftAmount> - <offset>)<else><shiftAmount><endif>
%>

// produces more efficient bytecode when bits.ttypes contains at most two items
bitsetInlineComparison(s, bits) ::= <%
<bits.ttypes:{ttype | <s.varName>==<tokenType.(ttype)>}; separator=" || ">
<bits.tokens:{t | <s.varName>==<tokenType.(t.name)>}; separator=" || ">
%>

cases(ttypes) ::= <<
<ttypes:{t | case <tokenType.(t)>:}; separator="\n">
cases(tokens) ::= <<
<tokens:{t | case <tokenType.(t.name)>:}; separator="\n">
>>

InvokeRule(r, argExprsChunks) ::= <<
Expand Down
18 changes: 8 additions & 10 deletions tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ _errHandler->sync(this);
<! TODO: untested !><if (choice.label)>LL1AltBlock(choice, preamble, alts, error) <labelref(choice.label)> = _input->LT(1);<endif>
<preamble; separator="\n">
switch (_input->LA(1)) {
<choice.altLook, alts: {look, alt | <cases(ttypes = look)> {
<choice.altLook, alts: {look, alt | <cases(tokens = look)> {
<alt>
break;
\}
Expand All @@ -667,7 +667,7 @@ LL1OptionalBlock(choice, alts, error) ::= <<
setState(<choice.stateNumber>);
_errHandler->sync(this);
switch (_input->LA(1)) {
<choice.altLook, alts: {look, alt | <cases(ttypes = look)> {
<choice.altLook, alts: {look, alt | <cases(tokens = look)> {
<alt>
break;
\}
Expand Down Expand Up @@ -793,18 +793,17 @@ ThrowNoViableAlt(t) ::= "throw NoViableAltException(this);"

TestSetInlineHeader(s) ::= "<! Required but unused. !>"
TestSetInline(s) ::= <<
<s.bitsets: {bits | <if (rest(rest(bits.ttypes)))><bitsetBitfieldComparison(s, bits)><else><bitsetInlineComparison(s, bits)><endif>}; separator=" || ">
<s.bitsets: {bits | <if (rest(rest(bits.tokens)))><bitsetBitfieldComparison(s, bits)><else><bitsetInlineComparison(s, bits)><endif>}; separator=" || ">
>>

// Java language spec 15.19 - shift operators mask operands rather than overflow to 0... need range test
testShiftInRange(shiftAmount) ::= <<
((<shiftAmount> & ~ 0x3fULL) == 0)
>>

// produces smaller bytecode only when bits.ttypes contains more than two items
bitsetBitfieldComparison(s, bits) ::= <<
(<testShiftInRange({<offsetShift(s.varName, bits.shift)>})> &&
((1ULL \<\< <offsetShift(s.varName, bits.shift)>) & (<bits.ttypes: {ttype | (1ULL \<\< <offsetShift(ttype, bits.shift, true)>)}; separator = "\n | ">)) != 0)
<testShiftInRange({<offsetShift(s.varName, bits.shift)>})> &&
((1ULL \<\< <offsetShift(s.varName, bits.shift)>) & <bits.calculated>) != 0
>>

isZero ::= [
Expand All @@ -816,13 +815,12 @@ offsetShift(shiftAmount, offset, prefix = false) ::= <%
<if (!isZero.(offset))>(<if (prefix)><parser.name>::<endif><shiftAmount> - <offset>)<else><if (prefix)><parser.name>::<endif><shiftAmount><endif>
%>

// produces more efficient bytecode when bits.ttypes contains at most two items
bitsetInlineComparison(s, bits) ::= <%
<bits.ttypes: {ttype | <s.varName> == <parser.name>::<ttype>}; separator = "\n\n|| ">
<bits.tokens: {t | <s.varName> == <parser.name>::<t.name>}; separator = "\n\n|| ">
%>

cases(ttypes) ::= <<
<ttypes: {t | case <parser.name>::<t>:}; separator="\n">
cases(tokens) ::= <<
<tokens: {t | case <parser.name>::<t.name>:}; separator="\n">
>>

InvokeRuleHeader(r, argExprsChunks) ::= "InvokeRuleHeader"
Expand Down
16 changes: 7 additions & 9 deletions tool/resources/org/antlr/v4/tool/templates/codegen/Dart/Dart.stg
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ errorHandler.sync(this);
<if(choice.label)><labelref(choice.label)> = tokenStream.LT(1);<endif>
<preamble; separator="\n">
switch (tokenStream.LA(1)!) {
<choice.altLook,alts:{look,alt| <cases(ttypes=look)>
<choice.altLook,alts:{look,alt| <cases(tokens=look)>
<alt>
break;}; separator="\n">
default:
Expand All @@ -403,7 +403,7 @@ LL1OptionalBlock(choice, alts, error) ::= <<
state = <choice.stateNumber>;
errorHandler.sync(this);
switch (tokenStream.LA(1)!) {
<choice.altLook,alts:{look,alt| <cases(ttypes=look)>
<choice.altLook,alts:{look,alt| <cases(tokens=look)>
<alt>
break;}; separator="\n">
default:
Expand Down Expand Up @@ -510,17 +510,16 @@ Sync(s) ::= "sync(<s.expecting.name>);"
ThrowNoViableAlt(t) ::= "throw NoViableAltException(this);"

TestSetInline(s) ::= <<
<s.bitsets:{bits | <if(rest(rest(bits.ttypes)))><bitsetBitfieldComparison(s, bits)><else><bitsetInlineComparison(s, bits)><endif>}; separator=" || ">
<s.bitsets:{bits | <if(rest(rest(bits.tokens)))><bitsetBitfieldComparison(s, bits)><else><bitsetInlineComparison(s, bits)><endif>}; separator=" || ">
>>

// Java language spec 15.19 - shift operators mask operands rather than overflow to 0... need range test
testShiftInRange(shiftAmount) ::= <<
((<shiftAmount>) & ~0x3f) == 0
>>

// produces smaller bytecode only when bits.ttypes contains more than two items
bitsetBitfieldComparison(s, bits) ::= <%
(<testShiftInRange({<offsetShift(s.varName, bits.shift)>})> && ((BigInt.one \<\< <offsetShift(s.varName, bits.shift)>) & (<bits.ttypes:{ttype | (BigInt.one \<\< <offsetShift({TOKEN_<ttype>}, bits.shift)>)}; separator=" | ">)) != BigInt.zero)
<testShiftInRange({<offsetShift(s.varName, bits.shift)>})> && ((1 \<\< <offsetShift(s.varName, bits.shift)>) & <bits.calculated>) != 0
%>

isZero ::= [
Expand All @@ -532,13 +531,12 @@ offsetShift(shiftAmount, offset) ::= <%
<if(!isZero.(offset))>(<shiftAmount> - <offset>)<else><shiftAmount><endif>
%>

// produces more efficient bytecode when bits.ttypes contains at most two items
bitsetInlineComparison(s, bits) ::= <%
<bits.ttypes:{ttype | <s.varName> == TOKEN_<ttype>}; separator=" || ">
<bits.tokens:{t | <s.varName> == TOKEN_<t.name>}; separator=" || ">
%>

cases(ttypes) ::= <<
<ttypes:{t | case TOKEN_<t>:}; separator="\n">
cases(tokens) ::= <<
<tokens:{t | case TOKEN_<t.name>:}; separator="\n">
>>

InvokeRule(r, argExprsChunks) ::=<<
Expand Down
14 changes: 6 additions & 8 deletions tool/resources/org/antlr/v4/tool/templates/codegen/Go/Go.stg
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ p.GetErrorHandler().Sync(p)

switch p.GetTokenStream().LA(1) {
<if(choice.altLook && alts)>
<choice.altLook, alts:{look, alt | case <look:{l | <parser.name><l>}; separator=", ">:
<choice.altLook, alts:{look, alt | case <look:{l | <parser.name><l.name>}; separator=", ">:
<alt>}; separator="\n\n">


Expand All @@ -571,7 +571,7 @@ p.GetErrorHandler().Sync(p)

switch p.GetTokenStream().LA(1) {
<if(choice.altLook && alts)>
<choice.altLook, alts:{look, alt | case <look:{l | <parser.name><l>}; separator=", ">:
<choice.altLook, alts:{look, alt | case <look:{l | <parser.name><l.name>}; separator=", ">:
<alt>}; separator="\n\n">


Expand Down Expand Up @@ -725,17 +725,16 @@ Sync(s) ::= "Sync(<s.expecting.name>)"
ThrowNoViableAlt(t) ::= "panic(antlr.NewNoViableAltException(p, nil, nil, nil, nil, nil))"

TestSetInline(s) ::= <<
<s.bitsets:{bits | <if(rest(rest(bits.ttypes)))><bitsetBitfieldComparison(s, bits)><else><bitsetInlineComparison(s, bits)><endif>}; separator=" || ">
<s.bitsets:{bits | <if(rest(rest(bits.tokens)))><bitsetBitfieldComparison(s, bits)><else><bitsetInlineComparison(s, bits)><endif>}; separator=" || ">
>>

// Javascript language spec - shift operators are 32 bits long max
testShiftInRange(shiftAmount) ::= <<
((<shiftAmount>) & -(0x1f+1)) == 0
(int64(<shiftAmount>) & ^0x3f) == 0
>>

// produces smaller bytecode only when bits.ttypes contains more than two items
bitsetBitfieldComparison(s, bits) ::= <%
(<testShiftInRange({<offsetShiftVar(s.varName, bits.shift)>})> && ((1 \<\< uint(<offsetShiftVar(s.varName, bits.shift)>)) & (<bits.ttypes:{ttype | (1 \<\< <offsetShiftType(ttype, bits.shift)>)}; separator=" | ">)) != 0)
<testShiftInRange({<offsetShiftVar(s.varName, bits.shift)>})> && ((int64(1) \<\< <offsetShiftVar(s.varName, bits.shift)>) & <bits.calculated>) != 0
%>

isZero ::= [
Expand All @@ -751,9 +750,8 @@ offsetShiftType(shiftAmount, offset) ::= <%
<if(!isZero.(offset))>(<parser.name><shiftAmount> - <offset>)<else><parser.name><shiftAmount><endif>
%>

// produces more efficient bytecode when bits.ttypes contains at most two items
bitsetInlineComparison(s, bits) ::= <%
<bits.ttypes:{ttype | <s.varName> == <parser.name><ttype>}; separator=" || ">
<bits.tokens:{t | <s.varName> == <parser.name><t.name>}; separator=" || ">
%>

InvokeRule(r, argExprsChunks) ::= <<
Expand Down
16 changes: 7 additions & 9 deletions tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ _errHandler.sync(this);
<if(choice.label)><labelref(choice.label)> = _input.LT(1);<endif>
<preamble; separator="\n">
switch (_input.LA(1)) {
<choice.altLook,alts:{look,alt| <cases(ttypes=look)>
<choice.altLook,alts:{look,alt| <cases(tokens=look)>
<alt>
break;}; separator="\n">
default:
Expand All @@ -498,7 +498,7 @@ LL1OptionalBlock(choice, alts, error) ::= <<
setState(<choice.stateNumber>);
_errHandler.sync(this);
switch (_input.LA(1)) {
<choice.altLook,alts:{look,alt| <cases(ttypes=look)>
<choice.altLook,alts:{look,alt| <cases(tokens=look)>
<alt>
break;}; separator="\n">
default:
Expand Down Expand Up @@ -605,17 +605,16 @@ Sync(s) ::= "sync(<s.expecting.name>);"
ThrowNoViableAlt(t) ::= "throw new NoViableAltException(this);"

TestSetInline(s) ::= <<
<s.bitsets:{bits | <if(rest(rest(bits.ttypes)))><bitsetBitfieldComparison(s, bits)><else><bitsetInlineComparison(s, bits)><endif>}; separator=" || ">
<s.bitsets:{bits | <if(rest(rest(bits.tokens)))><bitsetBitfieldComparison(s, bits)><else><bitsetInlineComparison(s, bits)><endif>}; separator=" || ">
>>

// Java language spec 15.19 - shift operators mask operands rather than overflow to 0... need range test
testShiftInRange(shiftAmount) ::= <<
((<shiftAmount>) & ~0x3f) == 0
>>

// produces smaller bytecode only when bits.ttypes contains more than two items
bitsetBitfieldComparison(s, bits) ::= <%
(<testShiftInRange({<offsetShift(s.varName, bits.shift)>})> && ((1L \<\< <offsetShift(s.varName, bits.shift)>) & (<bits.ttypes:{ttype | (1L \<\< <offsetShift(ttype, bits.shift)>)}; separator=" | ">)) != 0)
<testShiftInRange({<offsetShift(s.varName, bits.shift)>})> && ((1L \<\< <offsetShift(s.varName, bits.shift)>) & <bits.calculated>L) != 0
%>

isZero ::= [
Expand All @@ -627,13 +626,12 @@ offsetShift(shiftAmount, offset) ::= <%
<if(!isZero.(offset))>(<shiftAmount> - <offset>)<else><shiftAmount><endif>
%>

// produces more efficient bytecode when bits.ttypes contains at most two items
bitsetInlineComparison(s, bits) ::= <%
<bits.ttypes:{ttype | <s.varName>==<ttype>}; separator=" || ">
<bits.tokens:{t | <s.varName>==<t.name>}; separator=" || ">
%>

cases(ttypes) ::= <<
<ttypes:{t | case <t>:}; separator="\n">
cases(tokens) ::= <<
<tokens:{t | case <t.name>:}; separator="\n">
>>

InvokeRule(r, argExprsChunks) ::= <<
Expand Down
Loading