Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add partial P6bigint REPR and some basic ops.
This is enough to pass the first 5 of 60-bigint.t. Note that P6bigint
currently is not able to embed itself into a P6opaque.
  • Loading branch information
jnthn committed Apr 7, 2013
1 parent d489e2e commit 0f21904
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/QAST/JASTCompiler.nqp
Expand Up @@ -1675,6 +1675,15 @@ QAST::OperationsJAST.map_classlib_core_op('isle_i', $TYPE_OPS, 'isle_i', [$RT_IN
QAST::OperationsJAST.map_classlib_core_op('isgt_i', $TYPE_OPS, 'isgt_i', [$RT_INT, $RT_INT], $RT_INT);
QAST::OperationsJAST.map_classlib_core_op('isge_i', $TYPE_OPS, 'isge_i', [$RT_INT, $RT_INT], $RT_INT);

QAST::OperationsJAST.map_classlib_core_op('bool_I', $TYPE_OPS, 'bool_I', [$RT_OBJ], $RT_INT, :tc);
QAST::OperationsJAST.map_classlib_core_op('cmp_I', $TYPE_OPS, 'cmp_I', [$RT_OBJ, $RT_OBJ], $RT_INT, :tc);
QAST::OperationsJAST.map_classlib_core_op('iseq_I', $TYPE_OPS, 'iseq_I', [$RT_OBJ, $RT_OBJ], $RT_INT, :tc);
QAST::OperationsJAST.map_classlib_core_op('isne_I', $TYPE_OPS, 'isne_I', [$RT_OBJ, $RT_OBJ], $RT_INT, :tc);
QAST::OperationsJAST.map_classlib_core_op('islt_I', $TYPE_OPS, 'islt_I', [$RT_OBJ, $RT_OBJ], $RT_INT, :tc);
QAST::OperationsJAST.map_classlib_core_op('isle_I', $TYPE_OPS, 'isle_I', [$RT_OBJ, $RT_OBJ], $RT_INT, :tc);
QAST::OperationsJAST.map_classlib_core_op('isgt_I', $TYPE_OPS, 'isgt_I', [$RT_OBJ, $RT_OBJ], $RT_INT, :tc);
QAST::OperationsJAST.map_classlib_core_op('isge_I', $TYPE_OPS, 'isge_I', [$RT_OBJ, $RT_OBJ], $RT_INT, :tc);

QAST::OperationsJAST.map_classlib_core_op('cmp_n', $TYPE_OPS, 'cmp_n', [$RT_NUM, $RT_NUM], $RT_INT);
QAST::OperationsJAST.map_classlib_core_op('iseq_n', $TYPE_OPS, 'iseq_n', [$RT_NUM, $RT_NUM], $RT_INT);
QAST::OperationsJAST.map_classlib_core_op('isne_n', $TYPE_OPS, 'isne_n', [$RT_NUM, $RT_NUM], $RT_INT);
Expand All @@ -1691,6 +1700,9 @@ QAST::OperationsJAST.map_classlib_core_op('isle_s', $TYPE_OPS, 'isle_s', [$RT_ST
QAST::OperationsJAST.map_classlib_core_op('isgt_s', $TYPE_OPS, 'isgt_s', [$RT_STR, $RT_STR], $RT_INT);
QAST::OperationsJAST.map_classlib_core_op('isge_s', $TYPE_OPS, 'isge_s', [$RT_STR, $RT_STR], $RT_INT);

# bigint ops
QAST::OperationsJAST.map_classlib_core_op('fromstr_I', $TYPE_OPS, 'fromstr_I', [$RT_STR, $RT_OBJ], $RT_OBJ, :tc);
QAST::OperationsJAST.map_classlib_core_op('tostr_I', $TYPE_OPS, 'tostr_I', [$RT_OBJ], $RT_STR, :tc);

# boolean opcodes
QAST::OperationsJAST.map_classlib_core_op('not_i', $TYPE_OPS, 'not_i', [$RT_INT], $RT_INT);
Expand Down
60 changes: 60 additions & 0 deletions src/org/perl6/nqp/runtime/Ops.java
Expand Up @@ -3111,6 +3111,66 @@ public static String join_literal(String[] parts) {
return retval.toString();
}

/* Big integer operations. */

private static BigInteger getBI(ThreadContext tc, SixModelObject obj) {
if (obj instanceof P6bigintInstance)
return ((P6bigintInstance)obj).value;
throw new RuntimeException("Inlined case of P6bigint NYI");
}

private static SixModelObject makeBI(ThreadContext tc, SixModelObject type, BigInteger value) {
SixModelObject res = type.st.REPR.allocate(tc, type.st);
res.initialize(tc);
if (res instanceof P6bigintInstance) {
((P6bigintInstance)res).value = value;
}
else {
throw new RuntimeException("Inlined case of P6bigint NYI");
}
return res;
}

public static SixModelObject fromstr_I(String str, SixModelObject type, ThreadContext tc) {
return makeBI(tc, type, new BigInteger(str));
}

public static String tostr_I(SixModelObject value, ThreadContext tc) {
return getBI(tc, value).toString();
}

public static long bool_I(SixModelObject a, ThreadContext tc) {
return getBI(tc, a).compareTo(BigInteger.ZERO) == 0 ? 0 : 1;
}

public static long cmp_I(SixModelObject a, SixModelObject b, ThreadContext tc) {
return getBI(tc, a).compareTo(getBI(tc, b));
}

public static long iseq_I(SixModelObject a, SixModelObject b, ThreadContext tc) {
return getBI(tc, a).compareTo(getBI(tc, b)) == 0 ? 1 : 0;
}

public static long isne_I(SixModelObject a, SixModelObject b, ThreadContext tc) {
return getBI(tc, a).compareTo(getBI(tc, b)) == 0 ? 0 : 1;
}

public static long islt_I(SixModelObject a, SixModelObject b, ThreadContext tc) {
return getBI(tc, a).compareTo(getBI(tc, b)) < 0 ? 1 : 0;
}

public static long isle_I(SixModelObject a, SixModelObject b, ThreadContext tc) {
return getBI(tc, a).compareTo(getBI(tc, b)) <= 0 ? 1 : 0;
}

public static long isgt_I(SixModelObject a, SixModelObject b, ThreadContext tc) {
return getBI(tc, a).compareTo(getBI(tc, b)) > 0 ? 1 : 0;
}

public static long isge_I(SixModelObject a, SixModelObject b, ThreadContext tc) {
return getBI(tc, a).compareTo(getBI(tc, b)) >= 0 ? 1 : 0;
}

/* Evaluation of code; JVM-specific ops. */
public static SixModelObject compilejast(String dump, ThreadContext tc) {
EvalResult res = new EvalResult();
Expand Down
1 change: 1 addition & 0 deletions src/org/perl6/nqp/sixmodel/REPRRegistry.java
Expand Up @@ -46,5 +46,6 @@ public static void setup() {
addREPR("NFA", new NFA());
addREPR("VMException", new VMException());
addREPR("IOHandle", new IOHandle());
addREPR("P6bigint", new P6bigint());
}
}
43 changes: 43 additions & 0 deletions src/org/perl6/nqp/sixmodel/reprs/P6bigint.java
@@ -0,0 +1,43 @@
package org.perl6.nqp.sixmodel.reprs;

import org.perl6.nqp.runtime.ThreadContext;
import org.perl6.nqp.sixmodel.REPR;
import org.perl6.nqp.sixmodel.STable;
import org.perl6.nqp.sixmodel.SerializationReader;
import org.perl6.nqp.sixmodel.SixModelObject;
import org.perl6.nqp.sixmodel.StorageSpec;
import org.perl6.nqp.sixmodel.TypeObject;

public class P6bigint extends REPR {
public SixModelObject type_object_for(ThreadContext tc, SixModelObject HOW) {
STable st = new STable(this, HOW);
SixModelObject obj = new TypeObject();
obj.st = st;
st.WHAT = obj;
return st.WHAT;
}

public SixModelObject allocate(ThreadContext tc, STable st) {
P6bigintInstance obj = new P6bigintInstance();
obj.st = st;
return obj;
}

public StorageSpec get_storage_spec(ThreadContext tc, STable st) {
StorageSpec ss = new StorageSpec();
ss.inlineable = StorageSpec.INLINED;
ss.boxed_primitive = StorageSpec.BP_INT;
ss.bits = 64;
ss.can_box = StorageSpec.CAN_BOX_INT;
return ss;
}

public SixModelObject deserialize_stub(ThreadContext tc, STable st) {
throw new RuntimeException("Deserialization NYI for P6bigint");
}

public void deserialize_finish(ThreadContext tc, STable st,
SerializationReader reader, SixModelObject obj) {
throw new RuntimeException("Deserialization NYI for P6bigint");
}
}
22 changes: 22 additions & 0 deletions src/org/perl6/nqp/sixmodel/reprs/P6bigintInstance.java
@@ -0,0 +1,22 @@
package org.perl6.nqp.sixmodel.reprs;

import java.math.BigInteger;

import org.perl6.nqp.runtime.ThreadContext;
import org.perl6.nqp.sixmodel.SixModelObject;

public class P6bigintInstance extends SixModelObject {
public BigInteger value;

public void initialize(ThreadContext tc) {
value = BigInteger.ZERO;
}

public void set_int(ThreadContext tc, long value) {
this.value = BigInteger.valueOf(value);
}

public long get_int(ThreadContext tc) {
return value.longValue();
}
}

0 comments on commit 0f21904

Please sign in to comment.