Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 08eb90b9665e
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 1acfaf041dff
Choose a head ref
  • 3 commits
  • 18 files changed
  • 1 contributor

Commits on Dec 24, 2015

  1. Copy the full SHA
    38240a2 View commit details

Commits on Dec 25, 2015

  1. Copy the full SHA
    a3763e0 View commit details
  2. Copy the full SHA
    1acfaf0 View commit details
Showing with 544 additions and 157 deletions.
  1. +59 −54 truffle/src/main/antlr4/org/jruby/truffle/format/parser/Pack.g4
  2. +22 −0 truffle/src/main/java/org/jruby/truffle/format/nodes/PackNode.java
  3. +46 −0 truffle/src/main/java/org/jruby/truffle/format/nodes/decode/DecodeByteNode.java
  4. +19 −2 truffle/src/main/java/org/jruby/truffle/format/nodes/decode/DecodeInteger16BigNode.java
  5. +18 −2 truffle/src/main/java/org/jruby/truffle/format/nodes/decode/DecodeInteger16LittleNode.java
  6. +18 −1 truffle/src/main/java/org/jruby/truffle/format/nodes/decode/DecodeInteger32BigNode.java
  7. +18 −1 truffle/src/main/java/org/jruby/truffle/format/nodes/decode/DecodeInteger32LittleNode.java
  8. +18 −1 truffle/src/main/java/org/jruby/truffle/format/nodes/decode/DecodeInteger64BigNode.java
  9. +18 −1 truffle/src/main/java/org/jruby/truffle/format/nodes/decode/DecodeInteger64LittleNode.java
  10. +8 −2 truffle/src/main/java/org/jruby/truffle/format/nodes/read/ReadByteNode.java
  11. +15 −3 truffle/src/main/java/org/jruby/truffle/format/nodes/read/ReadBytesNode.java
  12. +82 −0 truffle/src/main/java/org/jruby/truffle/format/nodes/type/AsUnsignedNode.java
  13. +10 −0 truffle/src/main/java/org/jruby/truffle/format/nodes/write/WriteValueNode.java
  14. +74 −32 truffle/src/main/java/org/jruby/truffle/format/parser/PackTreeBuilder.java
  15. +93 −39 truffle/src/main/java/org/jruby/truffle/format/parser/UnpackTreeBuilder.java
  16. +19 −0 truffle/src/main/java/org/jruby/truffle/format/runtime/MissingValue.java
  17. +3 −19 truffle/src/main/java/org/jruby/truffle/nodes/core/StringNodes.java
  18. +4 −0 truffle/src/main/java/org/jruby/truffle/runtime/array/ArrayUtils.java
113 changes: 59 additions & 54 deletions truffle/src/main/antlr4/org/jruby/truffle/format/parser/Pack.g4
Original file line number Diff line number Diff line change
@@ -11,64 +11,69 @@ grammar Pack;

sequence : directive* ;

directive : C count? # character
| (S NATIVE? LITTLE | 'v') count? # shortLittle
| (S NATIVE? BIG | 'n') count? # shortBig
| S NATIVE? count? # shortNative
| I (LITTLE NATIVE? | NATIVE? LITTLE) count? # intLittle
| I (BIG NATIVE? | NATIVE? BIG) count? # intBig
| I NATIVE? count? # intNative
| (L LITTLE | 'V') count? # longLittle
| (L BIG | 'N') count? # longBig
| L count? # longNative
| (Q | L NATIVE) LITTLE count? # quadLittle
| (Q | L NATIVE) BIG count? # quadBig
| (Q | L NATIVE) count? # quadNative
| 'U' count? # utf8Character
| 'w' count? # berInteger
| D count? # doubleNative
| F count? # floatNative
| 'E' count? # doubleLittle
| 'e' count? # floatLittle
| 'G' count? # doubleBig
| 'g' count? # floatBig
| 'A' count? # binaryStringSpacePadded
| 'a' count? # binaryStringNullPadded
| 'Z' count? # binaryStringNullStar
| 'B' count? # bitStringMSBFirst
| 'b' count? # bitStringMSBLast
| 'H' count? # hexStringHighFirst
| 'h' count? # hexStringLowFirst
| 'u' count? # uuString
| 'M' INT? # mimeString
| 'm' count? # base64String
| ('p' | 'P') # pointer
| '@' INT? # at
| 'X' count? # back
| 'x' count? # nullByte
| subSequence # subSequenceAlternate
| ('v' | 'n' | 'V' | 'N' | 'U' | 'w' | D |
F | 'E' | 'e' | 'g' | 'G' | 'A' | 'a' |
directive : 'c' count? # int8
| 'C' count? # uint8
| 's' nativeOptLittle count? # int16Little
| 's' nativeOptBig count? # int16Big
| 's' NATIVE? count? # int16Native
| ('S' nativeOptLittle | 'v') count? # uint16Little
| ('S' nativeOptBig | 'n') count? # uint16Big
| 'S' NATIVE? count? # uint16Native
| ('i' nativeOptLittle | 'l' LITTLE) count? # int32Little
| ('i' nativeOptBig | 'l' BIG) count? # int32Big
| ('i' NATIVE? | 'l') count? # int32Native
| (('I' nativeOptLittle | 'L' LITTLE) | 'V') count? # uint32Little
| (('I' nativeOptBig | 'L' BIG) | 'N') count? # uint32Big
| ('I' NATIVE? | 'L') count? # uint32Native
| ('q' nativeOptLittle | 'l' nativeLittle) count? # int64Little
| ('q' nativeOptBig | 'l' nativeBig) count? # int64Big
| ('q' NATIVE? | 'l' NATIVE) count? # int64Native
| ('Q' nativeOptLittle | 'L' nativeLittle) count? # uint64Little
| ('Q' nativeOptBig | 'L' nativeBig) count? # uint64Big
| ('Q' NATIVE? | 'L' NATIVE) count? # uint64Native
| 'U' count? # utf8Character
| 'w' count? # berInteger
| ('d' | 'D') count? # f64Native
| ('f' | 'F') count? # f32Native
| 'E' count? # f64Little
| 'e' count? # f32Little
| 'G' count? # f64Big
| 'g' count? # f32Big
| 'A' count? # binaryStringSpacePadded
| 'a' count? # binaryStringNullPadded
| 'Z' count? # binaryStringNullStar
| 'B' count? # bitStringMSBFirst
| 'b' count? # bitStringMSBLast
| 'H' count? # hexStringHighFirst
| 'h' count? # hexStringLowFirst
| 'u' count? # uuString
| 'M' INT? # mimeString
| 'm' count? # base64String
| ('p' | 'P') # pointer
| '@' INT? # at
| 'X' count? # back
| 'x' count? # nullByte
| subSequence # subSequenceAlternate
| ('v' | 'n' | 'V' | 'N' | 'U' | 'w' | 'd' | 'D' |
'f' | 'F' | 'E' | 'e' | 'g' | 'G' | 'A' | 'a' |
'Z' | 'B' | 'b' | 'H' | 'h' | 'u' | 'M' |
'm' | 'p' | 'P' | 'X' | 'x') NATIVE #errorDisallowedNative ;
'm' | 'p' | 'P' | 'X' | 'x') NATIVE #errorDisallowedNative ;

subSequence : '(' directive+ ')' INT? ;
count : INT | '*' ;

count : INT | '*' ;
subSequence : '(' directive+ ')' INT? ;

C : [cC] ;
S : [sS] ;
I : [iI] ;
L : [lL] ;
Q : [qQ] ;
D : [dD] ;
F : [fF] ;
nativeOptLittle : NATIVE* LITTLE NATIVE* ;
nativeOptBig : NATIVE* BIG NATIVE* ;

LITTLE : '<' ;
BIG : '>' ;
NATIVE : [_!] ;
nativeLittle : NATIVE+ LITTLE NATIVE* | NATIVE* LITTLE NATIVE+ ;
nativeBig : NATIVE+ BIG NATIVE* | NATIVE* BIG NATIVE+ ;

INT : [0-9]+ ;
LITTLE : '<' ;
BIG : '>' ;
NATIVE : [!_] ;

WS : [ \t\n\u000b\f\r\u0000]+ -> skip ;
COMMENT : '#' .*? (('\r'? '\n') | EOF) -> skip ;
INT : [0-9]+ ;

WS : [ \t\n\u000b\f\r\u0000]+ -> skip ;
COMMENT : '#' .*? (('\r'? '\n') | EOF) -> skip ;
22 changes: 22 additions & 0 deletions truffle/src/main/java/org/jruby/truffle/format/nodes/PackNode.java
Original file line number Diff line number Diff line change
@@ -93,6 +93,28 @@ protected int advanceSourcePosition(VirtualFrame frame, int count) {
return sourcePosition;
}

protected int advanceSourcePositionNoThrow(VirtualFrame frame) {
return advanceSourcePositionNoThrow(frame, 1, false);
}

protected int advanceSourcePositionNoThrow(VirtualFrame frame, int count, boolean consumePartial) {
final int sourcePosition = getSourcePosition(frame);

final int sourceLength = getSourceLength(frame);

if (sourcePosition + count > sourceLength) {
if (consumePartial) {
setSourcePosition(frame, sourceLength);
}

return -1;
}

setSourcePosition(frame, sourcePosition + count);

return sourcePosition;
}

/**
* Get the output array we are writing to.
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.format.nodes.decode;

import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.NodeChildren;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.object.DynamicObject;
import org.jruby.truffle.format.nodes.PackNode;
import org.jruby.truffle.runtime.RubyContext;

@NodeChildren({
@NodeChild(value = "value", type = PackNode.class),
})
public abstract class DecodeByteNode extends PackNode {

public boolean signed;

public DecodeByteNode(RubyContext context, boolean signed) {
super(context);
this.signed = signed;
}

@Specialization(guards = "isNil(nil)")
public DynamicObject decode(VirtualFrame frame, DynamicObject nil) {
return nil;
}

@Specialization
public int decode(VirtualFrame frame, byte value) {
if (signed) {
return value;
} else {
return value & 0xff;
}
}

}
Original file line number Diff line number Diff line change
@@ -13,9 +13,14 @@
import com.oracle.truffle.api.dsl.NodeChildren;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.object.DynamicObject;
import org.jruby.truffle.format.nodes.PackNode;
import org.jruby.truffle.format.runtime.MissingValue;
import org.jruby.truffle.runtime.RubyContext;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

@NodeChildren({
@NodeChild(value = "bytes", type = PackNode.class),
})
@@ -26,8 +31,20 @@ public DecodeInteger16BigNode(RubyContext context) {
}

@Specialization
public int decode(VirtualFrame frame, byte[] bytes) {
return bytes[1] | bytes[0] << 8;
public MissingValue decode(VirtualFrame frame, MissingValue missingValue) {
return missingValue;
}

@Specialization(guards = "isNil(nil)")
public DynamicObject decode(VirtualFrame frame, DynamicObject nil) {
return nil;
}

@Specialization
public short decode(VirtualFrame frame, byte[] bytes) {
final ByteBuffer buffer = ByteBuffer.wrap(bytes);
buffer.order(ByteOrder.BIG_ENDIAN);
return buffer.getShort();
}

}
Original file line number Diff line number Diff line change
@@ -16,10 +16,14 @@
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.object.DynamicObject;
import org.jruby.truffle.format.nodes.PackNode;
import org.jruby.truffle.format.runtime.MissingValue;
import org.jruby.truffle.nodes.dispatch.CallDispatchHeadNode;
import org.jruby.truffle.nodes.dispatch.DispatchHeadNodeFactory;
import org.jruby.truffle.runtime.RubyContext;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

@NodeChildren({
@NodeChild(value = "bytes", type = PackNode.class),
})
@@ -30,8 +34,20 @@ public DecodeInteger16LittleNode(RubyContext context) {
}

@Specialization
public int decode(VirtualFrame frame, byte[] bytes) {
return bytes[0] | bytes[1] << 8;
public MissingValue decode(VirtualFrame frame, MissingValue missingValue) {
return missingValue;
}

@Specialization(guards = "isNil(nil)")
public DynamicObject decode(VirtualFrame frame, DynamicObject nil) {
return nil;
}

@Specialization
public short decode(VirtualFrame frame, byte[] bytes) {
final ByteBuffer buffer = ByteBuffer.wrap(bytes);
buffer.order(ByteOrder.LITTLE_ENDIAN);
return buffer.getShort();
}

}
Original file line number Diff line number Diff line change
@@ -13,9 +13,14 @@
import com.oracle.truffle.api.dsl.NodeChildren;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.object.DynamicObject;
import org.jruby.truffle.format.nodes.PackNode;
import org.jruby.truffle.format.runtime.MissingValue;
import org.jruby.truffle.runtime.RubyContext;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

@NodeChildren({
@NodeChild(value = "bytes", type = PackNode.class),
})
@@ -25,9 +30,21 @@ public DecodeInteger32BigNode(RubyContext context) {
super(context);
}

@Specialization
public MissingValue decode(VirtualFrame frame, MissingValue missingValue) {
return missingValue;
}

@Specialization(guards = "isNil(nil)")
public DynamicObject decode(VirtualFrame frame, DynamicObject nil) {
return nil;
}

@Specialization
public int decode(VirtualFrame frame, byte[] bytes) {
return bytes[3] | bytes[2] << 8 | bytes[1] << 16 | bytes[0] << 24;
final ByteBuffer buffer = ByteBuffer.wrap(bytes);
buffer.order(ByteOrder.BIG_ENDIAN);
return buffer.getInt();
}

}
Original file line number Diff line number Diff line change
@@ -13,9 +13,14 @@
import com.oracle.truffle.api.dsl.NodeChildren;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.object.DynamicObject;
import org.jruby.truffle.format.nodes.PackNode;
import org.jruby.truffle.format.runtime.MissingValue;
import org.jruby.truffle.runtime.RubyContext;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

@NodeChildren({
@NodeChild(value = "bytes", type = PackNode.class),
})
@@ -25,9 +30,21 @@ public DecodeInteger32LittleNode(RubyContext context) {
super(context);
}

@Specialization
public MissingValue decode(VirtualFrame frame, MissingValue missingValue) {
return missingValue;
}

@Specialization(guards = "isNil(nil)")
public DynamicObject decode(VirtualFrame frame, DynamicObject nil) {
return nil;
}

@Specialization
public int decode(VirtualFrame frame, byte[] bytes) {
return bytes[0] | bytes[1] << 8 | bytes[2] << 16 | bytes[3] << 24;
final ByteBuffer buffer = ByteBuffer.wrap(bytes);
buffer.order(ByteOrder.LITTLE_ENDIAN);
return buffer.getInt();
}

}
Original file line number Diff line number Diff line change
@@ -13,9 +13,14 @@
import com.oracle.truffle.api.dsl.NodeChildren;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.object.DynamicObject;
import org.jruby.truffle.format.nodes.PackNode;
import org.jruby.truffle.format.runtime.MissingValue;
import org.jruby.truffle.runtime.RubyContext;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

@NodeChildren({
@NodeChild(value = "bytes", type = PackNode.class),
})
@@ -25,9 +30,21 @@ public DecodeInteger64BigNode(RubyContext context) {
super(context);
}

@Specialization
public MissingValue decode(VirtualFrame frame, MissingValue missingValue) {
return missingValue;
}

@Specialization(guards = "isNil(nil)")
public DynamicObject decode(VirtualFrame frame, DynamicObject nil) {
return nil;
}

@Specialization
public long decode(VirtualFrame frame, byte[] bytes) {
return bytes[7] | bytes[6] << 8 | bytes[5] << 16 | bytes[4] << 24 | bytes[3] << 32 | bytes[2] << 40 | bytes[1] << 48 | bytes[0] << 56;
final ByteBuffer buffer = ByteBuffer.wrap(bytes);
buffer.order(ByteOrder.BIG_ENDIAN);
return buffer.getLong();
}

}
Loading