[Nice-commit] Nice/src/nice/tools/code Types.java,1.54,1.55 TupleType.java,1.2,1.3 SpecialArray.java
Brought to you by:
bonniot
From: <bo...@us...> - 2004-02-11 12:50:48
|
Update of /cvsroot/nice/Nice/src/nice/tools/code In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5377/src/nice/tools/code Modified Files: Types.java TupleType.java SpecialArray.java LiteralArrayProc.java Log Message: Use more typing information from the context when deciding what bytecode types to use for literal arrays and tuples, especially when they are nested inside other literal arrays or tuples. This allows to generate more efficient code, and fixes several runtime errors (fixes #761629 and #892625). Index: Types.java =================================================================== RCS file: /cvsroot/nice/Nice/src/nice/tools/code/Types.java,v retrieving revision 1.54 retrieving revision 1.55 diff -C2 -d -r1.54 -r1.55 *** Types.java 3 Dec 2003 21:50:41 -0000 1.54 --- Types.java 11 Feb 2004 12:46:39 -0000 1.55 *************** *** 260,263 **** --- 260,290 ---- } + public static Type lowestUpperBound(Expression[] exps) + { + if (exps.length == 0) + return Type.pointer_type; + + // Start with the minimal type. + Type res = Type.neverReturnsType; + + for (int i = 0; i < exps.length; i++) + { + res = Type.lowestCommonSuperType(res, exps[i].getType()); + + if (res == null) + return Type.pointer_type; + } + + return res; + } + + public static Type componentType(ArrayType type, int rank) + { + if (type instanceof TupleType) + return ((TupleType) type).componentTypes[rank]; + + return type.getComponentType(); + } + /**************************************************************** * Converting a bytecode type (coming from reflection for instance) *************** *** 700,703 **** --- 727,756 ---- } + /** @return the <code>rank</code>th type parameter of this type, or null. */ + public static Monotype getTypeParameter(Polytype type, int rank) + { + // This can only help + type.simplify(); + + return getTypeParameter(type.getMonotype(), rank); + } + + /** @return the <code>rank</code>th type parameter of this type, or null. */ + public static Monotype getTypeParameter(Monotype type, int rank) + { + // get rid of the nullness part + type = nice.tools.code.Types.rawType(type); + + if (! (type instanceof MonotypeConstructor)) + return null; + + Monotype[] parameters = ((MonotypeConstructor) type).getTP(); + + if (parameters.length <= rank) + return null; + else + return parameters[rank]; + } + /** Transforms \forall T:K.U into \forall T:K.sure<U> Index: TupleType.java =================================================================== RCS file: /cvsroot/nice/Nice/src/nice/tools/code/TupleType.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** TupleType.java 25 Nov 2003 19:22:58 -0000 1.2 --- TupleType.java 11 Feb 2004 12:46:39 -0000 1.3 *************** *** 37,40 **** --- 37,46 ---- public Type[] componentTypes; + public String toString() + { + return "tuple[" + getComponentType() + "](" + + bossa.util.Util.map("", ",", ")", componentTypes); + } + public static gnu.expr.Expression createExp (Type arrayType, Type[] componentTypes, *************** *** 43,47 **** return new gnu.expr.ApplyExp (new nice.tools.code.LiteralArrayProc ! (new TupleType(arrayType, componentTypes), components.length), components); } --- 49,53 ---- return new gnu.expr.ApplyExp (new nice.tools.code.LiteralArrayProc ! (new TupleType(arrayType, componentTypes), components.length, false), components); } Index: SpecialArray.java =================================================================== RCS file: /cvsroot/nice/Nice/src/nice/tools/code/SpecialArray.java,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** SpecialArray.java 28 Jan 2004 13:10:11 -0000 1.16 --- SpecialArray.java 11 Feb 2004 12:46:39 -0000 1.17 *************** *** 264,267 **** --- 264,272 ---- public void emitCoerceToObject (CodeAttr code) { + emitCoerceToCollection(code); + } + + public static void emitCoerceToCollection (CodeAttr code) + { code.emitInvokeStatic(makeMethod); } Index: LiteralArrayProc.java =================================================================== RCS file: /cvsroot/nice/Nice/src/nice/tools/code/LiteralArrayProc.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** LiteralArrayProc.java 10 Feb 2004 13:27:33 -0000 1.6 --- LiteralArrayProc.java 11 Feb 2004 12:46:39 -0000 1.7 *************** *** 31,42 **** of the procedure. */ ! public LiteralArrayProc(ArrayType arrayType, int nbElements) { this.arrayType = arrayType; this.nbElements = nbElements; } private ArrayType arrayType; private int nbElements; public void compile (ApplyExp exp, Compilation comp, Target target) --- 31,45 ---- of the procedure. */ ! public LiteralArrayProc(ArrayType arrayType, int nbElements, ! boolean wrapAsCollection) { this.arrayType = arrayType; this.nbElements = nbElements; + this.wrapAsCollection = wrapAsCollection; } private ArrayType arrayType; private int nbElements; + private boolean wrapAsCollection; public void compile (ApplyExp exp, Compilation comp, Target target) *************** *** 47,56 **** CodeAttr code = comp.getCode(); code.emitPushInt(nbElements); ! code.emitNewArray(arrayType.getComponentType()); // Set a special type, not the legacy array type. code.popType(); ! code.pushType(arrayType); --- 50,61 ---- CodeAttr code = comp.getCode(); + Type componentType = getComponentType(args); + code.emitPushInt(nbElements); ! code.emitNewArray(componentType); // Set a special type, not the legacy array type. code.popType(); ! code.pushType(SpecialTypes.array(componentType)); *************** *** 80,94 **** code.emitDup(); code.emitPushInt(i); ! args[i].compile(comp, arrayType.elements); ! code.emitArrayStore(arrayType.elements); } ! target.compileFromStack(comp, arrayType); } ! public Type getReturnType(Expression[] args) { ! return arrayType; } --- 85,133 ---- code.emitDup(); + // Get the specific type for this rank of the array (useful for tuples) + Type specificType = Types.componentType(arrayType, i); + // Only use it if it is more specific than the expected type. + // For instance don't use int if we store it in an Object[] anyway. + if (! specificType.isAssignableTo(componentType)) + specificType = componentType; + code.emitPushInt(i); ! args[i].compile(comp, specificType); ! code.emitArrayStore(componentType); } ! if (wrapAsCollection) ! SpecialArray.emitCoerceToCollection(code); ! else ! target.compileFromStack(comp, code.topType()); } ! ! private Type getComponentType (Expression[] args) ! { ! Type type = Types.lowestUpperBound(args); ! ! if (type.isSubtype(arrayType.getComponentType())) ! { ! // We could precise that type of the array, but this is not ! // necessary given the context, and it would even be bas for primitive ! // types (byte[] will need conversion if int[] is expected). ! // Just keep the original type. ! type = arrayType.getComponentType(); ! } ! ! if (type == Type.nullType) ! // All we know is that this array will contain only null, and will be ! // used generically. Let's make that Object. ! type = Type.pointer_type; ! ! return type; ! } ! public Type getReturnType(Expression[] args) { ! if (wrapAsCollection) ! return ClassType.make("java.util.List"); ! ! return SpecialTypes.array(getComponentType(args)); } |