1 /* 2 * Copyright (c) 2010-2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.nashorn.internal.codegen; 27 28 import static jdk.nashorn.internal.codegen.CompilerConstants.constructorNoLookup; 29 import static jdk.nashorn.internal.codegen.CompilerConstants.virtualCallNoLookup; 30 31 import java.util.LinkedHashSet; 32 import java.util.List; 33 import java.util.Set; 34 import jdk.nashorn.internal.codegen.types.Type; 35 import jdk.nashorn.internal.ir.Expression; 36 import jdk.nashorn.internal.ir.LiteralNode; 37 import jdk.nashorn.internal.runtime.JSType; 38 import jdk.nashorn.internal.runtime.Property; 39 import jdk.nashorn.internal.runtime.PropertyMap; 40 import jdk.nashorn.internal.runtime.ScriptObject; 41 import jdk.nashorn.internal.runtime.ScriptRuntime; 42 import jdk.nashorn.internal.runtime.arrays.ArrayData; 43 import jdk.nashorn.internal.runtime.arrays.ArrayIndex; 44 import jdk.nashorn.internal.scripts.JD; 45 import jdk.nashorn.internal.scripts.JO; 46 47 /** 48 * An object creator that uses spill properties. 49 */ 50 public final class SpillObjectCreator extends ObjectCreator<Expression> { 51 52 /** 53 * Constructor 54 * 55 * @param codegen code generator 56 * @param tuples tuples for key, symbol, value 57 */ 58 SpillObjectCreator(final CodeGenerator codegen, final List<MapTuple<Expression>> tuples) { 59 super(codegen, tuples, false, false); 60 makeMap(); 61 } 62 63 @Override 64 public void createObject(final MethodEmitter method) { 65 assert !isScope() : "spill scope objects are not currently supported"; 66 67 final int length = tuples.size(); 68 final boolean dualFields = codegen.useDualFields(); 69 final int spillLength = ScriptObject.spillAllocationLength(length); 70 final long[] jpresetValues = dualFields ? new long[spillLength] : null; 71 final Object[] opresetValues = new Object[spillLength]; 72 final Class<?> objectClass = getAllocatorClass(); 73 ArrayData arrayData = ArrayData.allocate(ScriptRuntime.EMPTY_ARRAY); 74 75 // Compute constant property values 76 int pos = 0; 77 for (final MapTuple<Expression> tuple : tuples) { 78 final String key = tuple.key; 79 final Expression value = tuple.value; 80 81 //this is a nop of tuple.key isn't e.g. "apply" or another special name 82 method.invalidateSpecialName(tuple.key); 83 84 if (value != null) { 85 final Object constantValue = LiteralNode.objectAsConstant(value); 86 if (constantValue != LiteralNode.POSTSET_MARKER) { 87 final Property property = propertyMap.findProperty(key); 88 if (property != null) { 89 // normal property key 90 property.setType(dualFields ? JSType.unboxedFieldType(constantValue) : Object.class); 91 final int slot = property.getSlot(); 92 if (dualFields && constantValue instanceof Number) { 93 jpresetValues[slot] = ObjectClassGenerator.pack((Number)constantValue); 94 } else { 95 opresetValues[slot] = constantValue; 96 } 97 } else { 98 // array index key 99 final long oldLength = arrayData.length(); 100 final int index = ArrayIndex.getArrayIndex(key); 101 final long longIndex = ArrayIndex.toLongIndex(index); 102 103 assert ArrayIndex.isValidArrayIndex(index); 104 105 if (longIndex >= oldLength) { 106 arrayData = arrayData.ensure(longIndex); 107 } 108 109 //avoid blowing up the array if we can 110 if (constantValue instanceof Integer) { 111 arrayData = arrayData.set(index, ((Integer)constantValue).intValue(), false); 112 } else if (constantValue instanceof Long) { 113 arrayData = arrayData.set(index, ((Long)constantValue).longValue(), false); 114 } else if (constantValue instanceof Double) { 115 arrayData = arrayData.set(index, ((Double)constantValue).doubleValue(), false); 116 } else { 117 arrayData = arrayData.set(index, constantValue, false); 118 } 119 120 if (longIndex > oldLength) { 121 arrayData = arrayData.delete(oldLength, longIndex - 1); 122 } 123 } 124 } 125 } 126 pos++; 127 } 128 129 // create object and invoke constructor 130 method._new(objectClass).dup(); 131 codegen.loadConstant(propertyMap); 132 133 // load primitive value spill array 134 if (dualFields) { 135 codegen.loadConstant(jpresetValues); 136 } else { 137 method.loadNull(); 138 } 139 // load object value spill array 140 codegen.loadConstant(opresetValues); 141 142 // instantiate the script object with spill objects 143 method.invoke(constructorNoLookup(objectClass, PropertyMap.class, long[].class, Object[].class)); 144 145 // Set prefix array data if any 146 if (arrayData.length() > 0) { 147 method.dup(); 148 codegen.loadConstant(arrayData); 149 method.invoke(virtualCallNoLookup(ScriptObject.class, "setArray", void.class, ArrayData.class)); 150 } 151 } 152 153 @Override 154 public void populateRange(final MethodEmitter method, final Type objectType, final int objectSlot, final int start, final int end) { 155 final int callSiteFlags = codegen.getCallSiteFlags(); 156 method.load(objectType, objectSlot); 157 158 // set postfix values 159 for (int i = start; i < end; i++) { 160 final MapTuple<Expression> tuple = tuples.get(i); 161 162 if (LiteralNode.isConstant(tuple.value)) { 163 continue; 164 } 165 166 final Property property = propertyMap.findProperty(tuple.key); 167 168 if (property == null) { 169 final int index = ArrayIndex.getArrayIndex(tuple.key); 170 assert ArrayIndex.isValidArrayIndex(index); 171 method.dup(); 172 method.load(ArrayIndex.toLongIndex(index)); 173 loadTuple(method, tuple); 174 method.dynamicSetIndex(callSiteFlags); 175 } else { 176 method.dup(); 177 loadTuple(method, tuple); 178 method.dynamicSet(property.getKey(), codegen.getCallSiteFlags(), false); 179 } 180 } 181 } 182 183 @Override 184 protected PropertyMap makeMap() { 185 assert propertyMap == null : "property map already initialized"; 186 final Class<? extends ScriptObject> clazz = getAllocatorClass(); 187 propertyMap = new MapCreator<>(clazz, tuples).makeSpillMap(false, codegen.useDualFields()); 188 return propertyMap; 189 } 190 191 @Override 192 protected void loadValue(final Expression expr, final Type type) { 193 codegen.loadExpressionAsType(expr, type); 194 } 195 196 @Override 197 protected Class<? extends ScriptObject> getAllocatorClass() { 198 return codegen.useDualFields() ? JD.class : JO.class; 199 } 200 }