diff options
author | Rich Hickey <richhickey@gmail.com> | 2007-10-29 01:40:17 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2007-10-29 01:40:17 +0000 |
commit | c2eec89e260d5d4ccfe6b722498c7a3fa22aa080 (patch) | |
tree | 52b6cf030a15db6e1b6086254c0a88ed81ec0063 /src | |
parent | 9201d6427403d8e716f5cb5b999e95c1c8af9012 (diff) |
array functions, changed primitive return boxing to use Integer, Long etc instead of Num
Diffstat (limited to 'src')
-rw-r--r-- | src/boot.clj | 45 | ||||
-rw-r--r-- | src/jvm/clojure/lang/Compiler.java | 81 |
2 files changed, 120 insertions, 6 deletions
diff --git a/src/boot.clj b/src/boot.clj index 21d0eaf6..d3b1e933 100644 --- a/src/boot.clj +++ b/src/boot.clj @@ -623,7 +623,6 @@ (prn (strcat "Elapsed time: " (/ (- (. System (nanoTime)) start#) 1000000.0) " msecs")) ret#)) -(import '(java.util.concurrent Executors LinkedBlockingQueue)) (defn int [#^Number x] (. x (intValue))) @@ -648,6 +647,48 @@ (. Boolean TRUE) (. Boolean FALSE))) +(import '(java.lang.reflect Array)) + +(defn aget [array idx] + (. Array (get array idx))) + +(defn aset [array idx val] + (. Array (set array idx val)) + val) + +(defn aset-boolean [array idx val] + (. Array (setBoolean array idx (boolean val))) + val) + +(defn aset-int [array idx val] + (. Array (setInt array idx (int val))) + val) + +(defn aset-long [array idx val] + (. Array (setLong array idx (long val))) + val) + +(defn aset-float [array idx val] + (. Array (setFloat array idx (float val))) + val) + +(defn aset-double [array idx val] + (. Array (setDouble array idx (double val))) + val) + +(defn aset-short [array idx val] + (. Array (setShort array idx (short val))) + val) + +(defn aset-byte [array idx val] + (. Array (setByte array idx (byte val))) + val) + +(defn int-array [len] + (. Array (newInstance (. Integer TYPE) (int len)))) + +(import '(java.util.concurrent Executors LinkedBlockingQueue)) + (defn pmap ([f coll] (let [nthreads (.. Runtime (getRuntime) (availableProcessors)) @@ -719,5 +760,7 @@ read *in* time int long float double short byte boolean + aget aset aset-boolean aset-int aset-long aset-float aset-double aset-short aset-byte + int-array )) diff --git a/src/jvm/clojure/lang/Compiler.java b/src/jvm/clojure/lang/Compiler.java index fa3a0e52..249c1411 100644 --- a/src/jvm/clojure/lang/Compiler.java +++ b/src/jvm/clojure/lang/Compiler.java @@ -15,8 +15,14 @@ package clojure.lang; import clojure.asm.*; import clojure.asm.commons.Method; import clojure.asm.commons.GeneratorAdapter; -//import org.objectweb.asm.util.TraceClassVisitor; -//import org.objectweb.asm.util.CheckClassAdapter; + +/* +import org.objectweb.asm.*; +import org.objectweb.asm.commons.Method; +import org.objectweb.asm.commons.GeneratorAdapter; +import org.objectweb.asm.util.TraceClassVisitor; +import org.objectweb.asm.util.CheckClassAdapter; +//*/ import java.io.*; import java.math.BigInteger; @@ -436,9 +442,24 @@ static interface AssignableExpr{ static abstract class HostExpr implements Expr{ final static Type CHAR_TYPE = Type.getType(Character.class); + final static Type INTEGER_TYPE = Type.getType(Integer.class); + final static Type LONG_TYPE = Type.getType(Long.class); + final static Type FLOAT_TYPE = Type.getType(Float.class); + final static Type DOUBLE_TYPE = Type.getType(Double.class); + final static Type SHORT_TYPE = Type.getType(Short.class); + final static Type BYTE_TYPE = Type.getType(Byte.class); final static Type NUMBER_TYPE = Type.getType(Number.class); + final static Method charValueMethod = Method.getMethod("char charValue()"); - final static Method valueOfMethod = Method.getMethod("Character valueOf(char c)"); + + final static Method charValueOfMethod = Method.getMethod("Character valueOf(char)"); + final static Method intValueOfMethod = Method.getMethod("Integer valueOf(int)"); + final static Method longValueOfMethod = Method.getMethod("Long valueOf(long)"); + final static Method floatValueOfMethod = Method.getMethod("Float valueOf(float)"); + final static Method doubleValueOfMethod = Method.getMethod("Double valueOf(double)"); + final static Method shortValueOfMethod = Method.getMethod("Short valueOf(short)"); + final static Method byteValueOfMethod = Method.getMethod("Byte valueOf(byte)"); + final static Method intValueMethod = Method.getMethod("int intValue()"); final static Method longValueMethod = Method.getMethod("long longValue()"); final static Method floatValueMethod = Method.getMethod("float floatValue()"); @@ -450,6 +471,7 @@ static abstract class HostExpr implements Expr{ final static Method fromLongMethod = Method.getMethod("clojure.lang.Num from(long)"); final static Method fromDoubleMethod = Method.getMethod("clojure.lang.Num from(double)"); + /* public static void emitBoxReturn(FnExpr fn, GeneratorAdapter gen, Class returnType){ if(returnType.isPrimitive()) { @@ -470,8 +492,11 @@ static abstract class HostExpr implements Expr{ } else if(returnType == char.class) { - gen.invokeStatic(CHAR_TYPE, valueOfMethod); + gen.invokeStatic(CHAR_TYPE, charValueOfMethod); } + else if(returnType == int.class) + gen.invokeStatic(INTEGER_TYPE, intValueOfMethod); + //m = fromIntMethod; else { Method m = fromIntMethod; @@ -494,7 +519,53 @@ static abstract class HostExpr implements Expr{ } } } - + */ + //* + public static void emitBoxReturn(FnExpr fn, GeneratorAdapter gen, Class returnType){ + if(returnType.isPrimitive()) + { + if(returnType == boolean.class) + { + Label falseLabel = gen.newLabel(); + Label endLabel = gen.newLabel(); + gen.ifZCmp(GeneratorAdapter.EQ, falseLabel); + gen.getStatic(RT_TYPE, "T", KEYWORD_TYPE); + gen.goTo(endLabel); + gen.mark(falseLabel); + NIL_EXPR.emit(C.EXPRESSION, fn, gen); + gen.mark(endLabel); + } + else if(returnType == void.class) + { + NIL_EXPR.emit(C.EXPRESSION, fn, gen); + } + else if(returnType == char.class) + { + gen.invokeStatic(CHAR_TYPE, charValueOfMethod); + } + else + { + if(returnType == int.class) + //gen.invokeStatic(NUM_TYPE, fromIntMethod); + gen.invokeStatic(INTEGER_TYPE, intValueOfMethod); + else if(returnType == float.class) + { + //gen.visitInsn(F2D); + gen.invokeStatic(FLOAT_TYPE, floatValueOfMethod); + //m = floatValueOfMethod; + } + else if(returnType == double.class) + gen.invokeStatic(DOUBLE_TYPE, doubleValueOfMethod); + else if(returnType == long.class) + gen.invokeStatic(LONG_TYPE, longValueOfMethod); + else if(returnType == byte.class) + gen.invokeStatic(BYTE_TYPE, byteValueOfMethod); + else if(returnType == short.class) + gen.invokeStatic(SHORT_TYPE, shortValueOfMethod); + } + } + } +//*/ public static void emitUnboxArg(FnExpr fn, GeneratorAdapter gen, Class paramType){ if(paramType.isPrimitive()) { |