Skip to content

Commit

Permalink
Make all integer nodes extend common superclass
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Sep 13, 2023
1 parent dad0fb1 commit cc4ff63
Show file tree
Hide file tree
Showing 25 changed files with 95 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.node.expression.builtin.number.utils.ToEnsoNumberNode;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "abs", description = "Absolute value of a number")
public abstract class AbsNode extends Node {
public abstract class AbsNode extends IntegerNode {
private @Child ToEnsoNumberNode toEnsoNumberNode = ToEnsoNumberNode.create();

public static AbsNode build() {
Expand Down Expand Up @@ -39,6 +39,6 @@ Object doBigInt(EnsoBigInteger self) {

@Fallback
Object doOther(Object self) {
throw IntegerUtils.throwTypeErrorIfNotInt(self, this);
throw throwTypeErrorIfNotInt(self, this);
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package org.enso.interpreter.node.expression.builtin.number.integer;

import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.node.expression.builtin.number.utils.ToEnsoNumberNode;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.ImportStatic;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.nodes.Node;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.node.expression.builtin.number.utils.ToEnsoNumberNode;
import org.enso.interpreter.runtime.number.EnsoBigInteger;
import com.oracle.truffle.api.nodes.Node.Child;

@BuiltinMethod(type = "Integer", name = "+", description = "Addition of numbers.")
@ImportStatic(IntegerUtils.class)
public abstract class AddNode extends Node {
public abstract class AddNode extends IntegerNode {
private @Child ToEnsoNumberNode toEnsoNumberNode = ToEnsoNumberNode.create();

public abstract Object execute(Object self, Object that);
Expand Down Expand Up @@ -83,6 +82,6 @@ Object doInterop(

@Fallback
Object doOther(Object self, Object that) {
throw IntegerUtils.throwTypeErrorIfNotInt(self, that, this);
throw throwTypeErrorIfNotInt(self, that);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.node.expression.builtin.number.utils.ToEnsoNumberNode;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "bit_and", description = "Bitwise and.")
public abstract class BitAndNode extends Node {
public abstract class BitAndNode extends IntegerNode {
private @Child ToEnsoNumberNode toEnsoNumberNode = ToEnsoNumberNode.create();

public abstract Object execute(Object self, Object that);
Expand Down Expand Up @@ -40,6 +40,6 @@ Object doBigIntBigInt(EnsoBigInteger self, EnsoBigInteger that) {

@Fallback
Object doOther(Object self, Object that) {
throw IntegerUtils.throwTypeErrorIfNotInt(self, that, this);
throw throwTypeErrorIfNotInt(self, that);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "bit_not", description = "Bitwise negation.")
public abstract class BitNotNode extends Node {
public abstract class BitNotNode extends IntegerNode {
abstract Object execute(Object self);

static BitNotNode build() {
Expand All @@ -28,6 +28,6 @@ EnsoBigInteger doBigInteger(EnsoBigInteger self) {

@Fallback
Object doOther(Object self) {
throw IntegerUtils.throwTypeErrorIfNotInt(self, this);
throw throwTypeErrorIfNotInt(self, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.node.expression.builtin.number.utils.ToEnsoNumberNode;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "bit_or", description = "Bitwise or.")
public abstract class BitOrNode extends Node {
public abstract class BitOrNode extends IntegerNode {
private @Child ToEnsoNumberNode toEnsoNumberNode = ToEnsoNumberNode.create();

abstract Object execute(Object self, Object that);
Expand Down Expand Up @@ -40,6 +40,6 @@ Object doBigInteger(EnsoBigInteger self, EnsoBigInteger that) {

@Fallback
Object doOther(Object self, Object that) {
throw IntegerUtils.throwTypeErrorIfNotInt(self, that, this);
throw throwTypeErrorIfNotInt(self, that);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.oracle.truffle.api.dsl.ImportStatic;
import com.oracle.truffle.api.dsl.NeverDefault;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.Node.Child;
import com.oracle.truffle.api.profiles.CountingConditionProfile;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
Expand All @@ -17,7 +17,7 @@

@ImportStatic(BigIntegerOps.class)
@BuiltinMethod(type = "Integer", name = "bit_shift", description = "Bitwise shift.")
public abstract class BitShiftNode extends Node {
public abstract class BitShiftNode extends IntegerNode {
private @Child ToEnsoNumberNode toEnsoNumberNode = ToEnsoNumberNode.create();
private final CountingConditionProfile canShiftLeftInLongProfile =
CountingConditionProfile.create();
Expand Down Expand Up @@ -126,7 +126,7 @@ Object doBigIntThat(EnsoBigInteger self, EnsoBigInteger that) {

@Fallback
Object doOther(Object self, Object that) {
throw IntegerUtils.throwTypeErrorIfNotInt(self, that, this);
throw throwTypeErrorIfNotInt(self, that);
}

boolean hasFreeBitsLeftShift(long number, long shift) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import com.oracle.truffle.api.dsl.Cached.Shared;
import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "bit_shift_r", description = "Bitwise right-shift.")
public abstract class BitShiftRightNode extends Node {
public abstract class BitShiftRightNode extends IntegerNode {
abstract Object execute(Object self, Object that);

static BitShiftRightNode build() {
Expand Down Expand Up @@ -49,6 +49,6 @@ Object doBigInteger(

@Fallback
Object doOther(Object self, Object that) {
throw IntegerUtils.throwTypeErrorIfNotInt(self, that, this);
throw throwTypeErrorIfNotInt(self, that);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.node.expression.builtin.number.utils.ToEnsoNumberNode;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "bit_xor", description = "Bitwise exclusive or.")
public abstract class BitXorNode extends Node {
public abstract class BitXorNode extends IntegerNode {
private @Child ToEnsoNumberNode toEnsoNumberNode = ToEnsoNumberNode.create();

abstract Object execute(Object self, Object that);
Expand Down Expand Up @@ -40,6 +40,6 @@ Object doBigInteger(EnsoBigInteger self, EnsoBigInteger that) {

@Fallback
Object doOther(Object self, Object that) {
throw IntegerUtils.throwTypeErrorIfNotInt(self, that, this);
throw throwTypeErrorIfNotInt(self, that);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "ceil", description = "Small integer ceiling.")
public abstract class CeilNode extends Node {
public abstract class CeilNode extends IntegerNode {
abstract Object execute(Object self);

public static CeilNode build() {
Expand All @@ -26,6 +26,6 @@ EnsoBigInteger doBigInt(EnsoBigInteger self) {

@Fallback
Object doOther(Object self) {
throw IntegerUtils.throwTypeErrorIfNotInt(self, this);
throw throwTypeErrorIfNotInt(self, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.node.expression.builtin.number.utils.ToEnsoNumberNode;
Expand All @@ -11,7 +11,7 @@
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "div", description = "Division of numbers.")
public abstract class DivNode extends Node {
public abstract class DivNode extends IntegerNode {

private @Child ToEnsoNumberNode toEnsoNumberNode = ToEnsoNumberNode.create();

Expand Down Expand Up @@ -55,6 +55,6 @@ Object doBigInteger(EnsoBigInteger self, EnsoBigInteger that) {

@Fallback
Object doOther(Object self, Object that) {
throw IntegerUtils.throwTypeErrorIfNotInt(self, that, this);
throw throwTypeErrorIfNotInt(self, that);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "/", description = "Division of numbers.")
public abstract class DivideNode extends Node {
public abstract class DivideNode extends IntegerNode {
abstract double execute(Object self, Object that);

static DivideNode build() {
Expand Down Expand Up @@ -47,6 +47,6 @@ static DivideNode build() {

@Fallback
double doOther(Object self, Object that) {
throw IntegerUtils.throwTypeErrorIfNotInt(self, that, this);
throw throwTypeErrorIfNotInt(self, that);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "floor", description = "Small integer floor.")
public abstract class FloorNode extends Node {
public abstract class FloorNode extends IntegerNode {

public abstract Object execute(Object self);

Expand All @@ -27,6 +27,6 @@ EnsoBigInteger doBigInt(EnsoBigInteger self) {

@Fallback
Object doOther(Object self) {
throw IntegerUtils.throwTypeErrorIfNotInt(self, this);
throw throwTypeErrorIfNotInt(self, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.error.DataflowError;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = ">", description = "Comparison of numbers.")
public abstract class GreaterNode extends Node {
public abstract class GreaterNode extends IntegerNode {

abstract Object execute(Object self, Object that);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.Node.Child;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.error.DataflowError;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = ">=", description = "Comparison of numbers.")
public abstract class GreaterOrEqualNode extends Node {
public abstract class GreaterOrEqualNode extends IntegerNode {

abstract Object execute(Object self, Object that);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
package org.enso.interpreter.node.expression.builtin.number.integer;

import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.nodes.Node;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.library.dispatch.TypesLibrary;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

public final class IntegerUtils {
private IntegerUtils() {}
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.nodes.Node;

abstract class IntegerNode extends Node {
IntegerNode() {}

@TruffleBoundary
static PanicException throwTypeErrorIfNotInt(Object self, Object that, Node node) {
var builtins = EnsoContext.get(node).getBuiltins();
final PanicException throwTypeErrorIfNotInt(Object self, Object that) {
var builtins = EnsoContext.get(this).getBuiltins();
var intType = builtins.number().getInteger();
var selfType = TypesLibrary.getUncached().getType(self);
if (selfType != intType) {
return new PanicException(builtins.error().makeTypeError(intType, self, "self"), node);
return new PanicException(builtins.error().makeTypeError(intType, self, "self"), this);
} else {
return new PanicException(builtins.error().makeTypeError(intType, that, "that"), node);
return new PanicException(builtins.error().makeTypeError(intType, that, "that"), this);
}
}

@TruffleBoundary
static PanicException throwTypeErrorIfNotInt(Object self, Node node) {
var builtins = EnsoContext.get(node).getBuiltins();
final PanicException throwTypeErrorIfNotInt(Object self) {
var builtins = EnsoContext.get(this).getBuiltins();
var intType = builtins.number().getInteger();
return new PanicException(builtins.error().makeTypeError(intType, self, "self"), node);
return new PanicException(builtins.error().makeTypeError(intType, self, "self"), this);
}

static boolean isForeignNumber(InteropLibrary iop, TruffleObject obj) {
final boolean isForeignNumber(InteropLibrary iop, TruffleObject obj) {
if (obj instanceof EnsoBigInteger) {
return false;
}
Expand Down
Loading

0 comments on commit cc4ff63

Please sign in to comment.