Skip to content

Commit

Permalink
Try to fold constants for all runtimes
Browse files Browse the repository at this point in the history
Go: getInlineTestSetWordSize 32 -> 64
Dart: get rid of BigInt
Swift: optimize TestSetInline

Signed-off-by: Ivan Kochurkin <kvanttt@gmail.com>
  • Loading branch information
KvanTTT committed May 8, 2022
1 parent cb521a0 commit 2e2ccb5
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 58 deletions.
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 @@ -681,12 +681,12 @@ TestSetInline(s) ::= <<

// Java language spec 15.19 - shift operators mask operands rather than overflow to 0... need range test
testShiftInRange(shiftAmount) ::= <<
((<shiftAmount>) & ~0x3f) == 0
(<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(tokenType.(ttype), bits.shift)>)}; separator=" | ">)) != 0)
<testShiftInRange({<offsetShift(s.varName, bits.shift)>})> && ((1L \<\< <offsetShift(s.varName, bits.shift)>) & <bits.calculated>) != 0
%>

isZero ::= [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -803,8 +803,8 @@ testShiftInRange(shiftAmount) ::= <<

// 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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,12 +515,12 @@ TestSetInline(s) ::= <<

// Java language spec 15.19 - shift operators mask operands rather than overflow to 0... need range test
testShiftInRange(shiftAmount) ::= <<
((<shiftAmount>) & ~0x3f) == 0
(<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 Down
4 changes: 2 additions & 2 deletions tool/resources/org/antlr/v4/tool/templates/codegen/Go/Go.stg
Original file line number Diff line number Diff line change
Expand Up @@ -730,12 +730,12 @@ TestSetInline(s) ::= <<

// 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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,12 +608,12 @@ TestSetInline(s) ::= <<

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

isZero ::= [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -672,12 +672,12 @@ TestSetInline(s) ::= <<

// Java language spec 15.19 - shift operators mask operands rather than overflow to 0... need range test
testShiftInRange(shiftAmount) ::= <<
((<shiftAmount>) & ~0x3f) === 0
(<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 \<\< <offsetShiftVar(s.varName, bits.shift)>) & (<bits.ttypes:{ttype | (1 \<\< <offsetShiftConst(ttype, bits.shift)>)}; separator=" | ">)) !== 0)
<testShiftInRange({<offsetShiftVar(s.varName, bits.shift)>})> && ((1 \<\< <offsetShiftVar(s.varName, bits.shift)>) & <bits.calculated>) !== 0
%>

isZero ::= [
Expand Down
23 changes: 3 additions & 20 deletions tool/resources/org/antlr/v4/tool/templates/codegen/Swift/Swift.stg
Original file line number Diff line number Diff line change
Expand Up @@ -609,34 +609,17 @@ Sync(s) ::= "sync(<s.expecting.name>);"
ThrowNoViableAlt(t) ::= "throw ANTLRException.recognition(e: NoViableAltException(self))"

TestSetInline(s) ::= <<
<!<s.bitsets:{bits | <if(rest(rest(bits.ttypes)))><bitsetBitfieldComparison(s, bits)><else><bitsetInlineComparison(s, bits)><endif>}; separator=" || ">!>
//closure
{ () -> Bool in
<if(rest(s.bitsets))>var<else>let<endif> testSet: Bool = <first(s.bitsets):{bits | <if(rest(rest(bits.ttypes)))><bitsetBitfieldComparison(s, bits)><else><bitsetInlineComparison(s, bits)><endif>}>
<rest(s.bitsets):{bits | testSet = testSet || <if(rest(rest(bits.ttypes)))><bitsetBitfieldComparison(s, bits)><else><bitsetInlineComparison(s, bits)><endif>}; separator="\n">
return testSet
}()
<s.bitsets:{bits | <if(rest(rest(bits.ttypes)))><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
(Int64(<shiftAmount>) & ~0x3f) == 0
>>

// produces smaller bytecode only when bits.ttypes contains more than two items
bitsetBitfieldComparison(s, bits) ::= <<
<!(<testShiftInRange({<offsetShift(s.varName, bits.shift)>})> && ((1 \<\< <offsetShift(s.varName, bits.shift)>) & (<bits.ttypes:{ttype | (1 \<\< <offsetShift(ttype, bits.shift)>)}; separator=" | ">)) != 0)!>
{ () -> Bool in
<! let test: Bool = (<testShiftInRange({<offsetShift(s.varName, bits.shift)>})>)!>
<!var temp: Int64 = Int64(<offsetShift(s.varName, bits.shift)>)!>
<!temp = (temp \< 0) ? (64 + (temp % 64 )) : (temp % 64)!>
<!let test1: Int64 = (Int64(1) \<\< temp)!>
<!var test2: Int64 = (<first(bits.ttypes):{ttype | Utils.bitLeftShift(<offsetShift(parserName(ttype), bits.shift)>)}>)!>
<!<rest(bits.ttypes):{ttype | test2 = test2 | Utils.bitLeftShift(<offsetShift(parserName(ttype), bits.shift)>)}; separator="\n">!>
let testArray: [Int] = [<s.varName>, <bits.ttypes:{ttype |<parserName(ttype)>}; separator=",">]
<!var test2: Int64 = Utils.testBitLeftShiftArray(testArray)!>
return Utils.testBitLeftShiftArray(testArray, <bits.shift>)
}()
<testShiftInRange({<offsetShift(s.varName, bits.shift)>})> && ((Int64(1) \<\< <offsetShift(s.varName, bits.shift)>) & <bits.calculated>) != 0
>>

isZero ::= [
Expand Down
5 changes: 0 additions & 5 deletions tool/src/org/antlr/v4/codegen/target/GoTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,6 @@ private void gofmt(File fileName) {
}
}

@Override
public int getInlineTestSetWordSize() {
return 32;
}

@Override
protected STGroup loadTemplates() {
STGroup result = super.loadTemplates();
Expand Down

0 comments on commit 2e2ccb5

Please sign in to comment.