Skip to content

Commit

Permalink
[Truffle] Tidy up pack and format nodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed May 16, 2015
1 parent 7db5522 commit 6fa1751
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 165 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/**
* Read a {@code long} value from the source, or a {@link BigInteger} if the
* value is that large. This is only used with BER - in all other cases
* we could truncate a {@code Bignum}.
* we would truncate a {@code Bignum}.
*/
@NodeChildren({
@NodeChild(value = "source", type = SourceNode.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
import org.jruby.truffle.runtime.RubyContext;

/**
* Re-interpret a value as a {@code long}.
* Re-interpret a value as a {@code long}. In other words, get the raw bytes
* as a long.
*/
@NodeChildren({
@NodeChild(value = "value", type = PackNode.class),
})
public abstract class AsLongNode extends PackNode {
public abstract class ReinterpretLongNode extends PackNode {

public AsLongNode(RubyContext context) {
public ReinterpretLongNode(RubyContext context) {
super(context);
}

Expand Down
133 changes: 45 additions & 88 deletions truffle/src/main/java/org/jruby/truffle/pack/parser/PackParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@
import org.jruby.truffle.pack.nodes.PackRootNode;
import org.jruby.truffle.pack.nodes.SourceNode;
import org.jruby.truffle.pack.nodes.control.*;
import org.jruby.truffle.pack.nodes.read.ReadDoubleNodeGen;
import org.jruby.truffle.pack.nodes.read.ReadLongNodeGen;
import org.jruby.truffle.pack.nodes.read.ReadLongOrBigIntegerNodeGen;
import org.jruby.truffle.pack.nodes.read.ReadStringNodeGen;
import org.jruby.truffle.pack.nodes.type.AsLongNodeGen;
import org.jruby.truffle.pack.nodes.type.AsSinglePrecisionNodeGen;
import org.jruby.truffle.pack.nodes.read.*;
import org.jruby.truffle.pack.nodes.type.*;
import org.jruby.truffle.pack.nodes.write.*;
import org.jruby.truffle.pack.runtime.Endianness;
import org.jruby.truffle.pack.runtime.PackEncoding;
Expand Down Expand Up @@ -80,10 +76,10 @@ public PackNode parse(PackTokenizer tokenizer, boolean inParens) {
throw new UnsupportedOperationException("unbalanced parens");
}
case 'C':
node = writeInteger(8, Signedness.UNSIGNED, nativeEndianness());
node = writeInteger(8, nativeEndianness());
break;
case 'c':
node = writeInteger(8, Signedness.SIGNED, nativeEndianness());
node = writeInteger(8, nativeEndianness());
break;
case 'S':
case 'L':
Expand Down Expand Up @@ -157,19 +153,19 @@ public PackNode parse(PackTokenizer tokenizer, boolean inParens) {
tokenizer.next();
}

node = writeInteger(size, signedness, endianness);
node = writeInteger(size, endianness);
} break;
case 'n':
node = writeInteger(16, Signedness.UNSIGNED, Endianness.BIG);
node = writeInteger(16, Endianness.BIG);
break;
case 'N':
node = writeInteger(32, Signedness.UNSIGNED, Endianness.BIG);
node = writeInteger(32, Endianness.BIG);
break;
case 'v':
node = writeInteger(16, Signedness.UNSIGNED, Endianness.LITTLE);
node = writeInteger(16, Endianness.LITTLE);
break;
case 'V':
node = writeInteger(32, Signedness.UNSIGNED, Endianness.LITTLE);
node = writeInteger(32, Endianness.LITTLE);
break;
case 'A':
case 'Z':
Expand Down Expand Up @@ -343,7 +339,9 @@ public PackNode parse(PackTokenizer tokenizer, boolean inParens) {
} break;
case 'U':
encoding = encoding.unifyWith(PackEncoding.UTF_8);
node = WriteUTF8CharacterNodeGen.create(context, ReadLongNodeGen.create(context, new SourceNode()));
node = WriteUTF8CharacterNodeGen.create(context,
ToLongNodeGen.create(context,
ReadValueNodeGen.create(context, new SourceNode())));
break;
case 'X':
node = new BackNode(context);
Expand All @@ -362,42 +360,42 @@ public PackNode parse(PackTokenizer tokenizer, boolean inParens) {
} break;
case 'D':
case 'd':
node = writeInteger(64, Signedness.UNSIGNED, nativeEndianness(),
AsLongNodeGen.create(context,
node = writeInteger(64, nativeEndianness(),
ReinterpretLongNodeGen.create(context,
ReadDoubleNodeGen.create(context, new SourceNode())));
break;
case 'F':
case 'f':
node = writeInteger(32, Signedness.UNSIGNED, nativeEndianness(),
AsLongNodeGen.create(context,
node = writeInteger(32, nativeEndianness(),
ReinterpretLongNodeGen.create(context,
AsSinglePrecisionNodeGen.create(context,
ReadDoubleNodeGen.create(context, new SourceNode()))));
break;
case 'E':
node = writeInteger(64, Signedness.UNSIGNED, Endianness.LITTLE,
AsLongNodeGen.create(context,
node = writeInteger(64, Endianness.LITTLE,
ReinterpretLongNodeGen.create(context,
ReadDoubleNodeGen.create(context, new SourceNode())));
break;
case 'e':
node = writeInteger(32, Signedness.UNSIGNED, Endianness.LITTLE,
AsLongNodeGen.create(context,
node = writeInteger(32, Endianness.LITTLE,
ReinterpretLongNodeGen.create(context,
AsSinglePrecisionNodeGen.create(context,
ReadDoubleNodeGen.create(context, new SourceNode()))));
break;
case 'G':
node = writeInteger(64, Signedness.UNSIGNED, Endianness.BIG,
AsLongNodeGen.create(context,
node = writeInteger(64, Endianness.BIG,
ReinterpretLongNodeGen.create(context,
ReadDoubleNodeGen.create(context, new SourceNode())));
break;
case 'g':
node = writeInteger(32, Signedness.UNSIGNED, Endianness.BIG,
AsLongNodeGen.create(context,
node = writeInteger(32, Endianness.BIG,
ReinterpretLongNodeGen.create(context,
AsSinglePrecisionNodeGen.create(context,
ReadDoubleNodeGen.create(context, new SourceNode()))));
break;
case 'P':
case 'p':
node = writeInteger(64, Signedness.UNSIGNED, nativeEndianness(),
node = writeInteger(64, nativeEndianness(),
new PNode(context));
break;
case 'w':
Expand Down Expand Up @@ -535,77 +533,36 @@ private static Endianness nativeEndianness() {
}
}

private PackNode writeInteger(int size, Signedness signedness, Endianness endianness) {
final PackNode readNode = ReadLongNodeGen.create(context, new SourceNode());
return writeInteger(size, signedness, endianness, readNode);
private PackNode writeInteger(int size, Endianness endianness) {
final PackNode readNode = ToLongNodeGen.create(context,
ReadValueNodeGen.create(context, new SourceNode()));
return writeInteger(size, endianness, readNode);
}

private PackNode writeInteger(int size, Signedness signedness, Endianness endianness, PackNode readNode) {
private PackNode writeInteger(int size, Endianness endianness, PackNode readNode) {
switch (size) {
case 8:
return Write8NodeGen.create(context, readNode);
case 16:
switch (signedness) {
case UNSIGNED:
switch (endianness) {
case LITTLE:
return Write16LittleNodeGen.create(context, readNode);
case BIG:
return Write16BigNodeGen.create(context, readNode);
}
case SIGNED:
switch (endianness) {
case LITTLE:
// Can I just use the same node?
return Write16LittleNodeGen.create(context, readNode);
case BIG:
// Can I just use the same node?
return Write16BigNodeGen.create(context, readNode);
}
default:
throw new UnsupportedOperationException();
switch (endianness) {
case LITTLE:
return Write16LittleNodeGen.create(context, readNode);
case BIG:
return Write16BigNodeGen.create(context, readNode);
}
case 32:
switch (signedness) {
case UNSIGNED:
switch (endianness) {
case LITTLE:
return Write32LittleNodeGen.create(context, readNode);
case BIG:
return Write32BigNodeGen.create(context, readNode);
}
case SIGNED:
switch (endianness) {
case LITTLE:
// Can I just use the same node?
return Write32LittleNodeGen.create(context, readNode);
case BIG:
// Can I just use the same node?
return Write32BigNodeGen.create(context, readNode);
}
default:
throw new UnsupportedOperationException();
switch (endianness) {
case LITTLE:
return Write32LittleNodeGen.create(context, readNode);
case BIG:
return Write32BigNodeGen.create(context, readNode);
}
case 64:
switch (signedness) {
case UNSIGNED:
switch (endianness) {
case LITTLE:
return Write64LittleNodeGen.create(context, readNode);
case BIG:
return Write64BigNodeGen.create(context, readNode);
}
case SIGNED:
switch (endianness) {
case LITTLE:
// Can I just use the same node?
return Write64LittleNodeGen.create(context, readNode);
case BIG:
// Can I just use the same node?
return Write64BigNodeGen.create(context, readNode);
}
default:
throw new UnsupportedOperationException();
switch (endianness) {
case LITTLE:
return Write64LittleNodeGen.create(context, readNode);
case BIG:
return Write64BigNodeGen.create(context, readNode);
}
default:
throw new UnsupportedOperationException();
Expand Down

2 comments on commit 6fa1751

@bjfish
Copy link
Contributor

@bjfish bjfish commented on 6fa1751 May 17, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrisseaton In case you missed it, I think there's a new findbugs error at this commit
https://travis-ci.org/jruby/jruby/jobs/62838329

L D DB: org.jruby.truffle.pack.parser.PackParser.parse(PackTokenizer, boolean) uses the same code for two switch clauses  At PackParser.java:[lines 79-80]

@chrisseaton
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks

Please sign in to comment.