author | hannesw |
Sat, 19 Sep 2015 16:04:28 +0200 | |
changeset 32781 | d8f34ffbbc7a |
parent 30390 | 357f9a3f9394 |
child 34732 | 6605efbe8447 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 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 |
||
16240
e1468b33e201
8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents:
16224
diff
changeset
|
26 |
package jdk.nashorn.internal.codegen; |
16147 | 27 |
|
18852
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
17518
diff
changeset
|
28 |
import static jdk.nashorn.internal.codegen.CompilerConstants.SCOPE; |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24751
diff
changeset
|
29 |
|
24738
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
30 |
import java.util.List; |
24719 | 31 |
import jdk.nashorn.internal.codegen.types.Type; |
16147 | 32 |
import jdk.nashorn.internal.runtime.PropertyMap; |
24719 | 33 |
import jdk.nashorn.internal.runtime.ScriptObject; |
16147 | 34 |
|
35 |
/** |
|
36 |
* Base class for object creation code generation. |
|
24719 | 37 |
* @param <T> value type |
16147 | 38 |
*/ |
32781
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
39 |
public abstract class ObjectCreator<T> implements CodeGenerator.SplitLiteralCreator { |
16147 | 40 |
|
24719 | 41 |
/** List of keys & symbols to initiate in this ObjectCreator */ |
42 |
final List<MapTuple<T>> tuples; |
|
16147 | 43 |
|
44 |
/** Code generator */ |
|
24719 | 45 |
final CodeGenerator codegen; |
16147 | 46 |
|
19472
9476460521b3
8023017: SUB missing for widest op == number for BinaryNode
lagergren
parents:
18852
diff
changeset
|
47 |
/** Property map */ |
9476460521b3
8023017: SUB missing for widest op == number for BinaryNode
lagergren
parents:
18852
diff
changeset
|
48 |
protected PropertyMap propertyMap; |
9476460521b3
8023017: SUB missing for widest op == number for BinaryNode
lagergren
parents:
18852
diff
changeset
|
49 |
|
9476460521b3
8023017: SUB missing for widest op == number for BinaryNode
lagergren
parents:
18852
diff
changeset
|
50 |
private final boolean isScope; |
9476460521b3
8023017: SUB missing for widest op == number for BinaryNode
lagergren
parents:
18852
diff
changeset
|
51 |
private final boolean hasArguments; |
16147 | 52 |
|
53 |
/** |
|
54 |
* Constructor |
|
55 |
* |
|
16224
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16216
diff
changeset
|
56 |
* @param codegen the code generator |
24719 | 57 |
* @param tuples key,symbol,value (optional) tuples |
16224
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16216
diff
changeset
|
58 |
* @param isScope is this object scope |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16216
diff
changeset
|
59 |
* @param hasArguments does the created object have an "arguments" property |
16147 | 60 |
*/ |
24719 | 61 |
ObjectCreator(final CodeGenerator codegen, final List<MapTuple<T>> tuples, final boolean isScope, final boolean hasArguments) { |
16147 | 62 |
this.codegen = codegen; |
24719 | 63 |
this.tuples = tuples; |
16147 | 64 |
this.isScope = isScope; |
16224
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16216
diff
changeset
|
65 |
this.hasArguments = hasArguments; |
16147 | 66 |
} |
67 |
||
68 |
/** |
|
69 |
* Generate code for making the object. |
|
70 |
* @param method Script method. |
|
71 |
*/ |
|
32781
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
72 |
public void makeObject(final MethodEmitter method) { |
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
73 |
createObject(method); |
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
74 |
// We need to store the object in a temporary slot as populateRange expects to load the |
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
75 |
// object from a slot (as it is also invoked within split methods). Note that this also |
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
76 |
// helps optimistic continuations to handle the stack in case an optimistic assumption |
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
77 |
// fails during initialization (see JDK-8079269). |
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
78 |
final int objectSlot = method.getUsedSlotsWithLiveTemporaries(); |
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
79 |
final Type objectType = method.peekType(); |
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
80 |
method.storeTemp(objectType, objectSlot); |
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
81 |
populateRange(method, objectType, objectSlot, 0, tuples.size()); |
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
82 |
} |
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
83 |
|
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
84 |
/** |
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
85 |
* Generate code for creating and initializing the object. |
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
86 |
* @param method the method emitter |
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
87 |
*/ |
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
88 |
protected abstract void createObject(final MethodEmitter method); |
16147 | 89 |
|
90 |
/** |
|
18852
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
17518
diff
changeset
|
91 |
* Construct the property map appropriate for the object. |
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
17518
diff
changeset
|
92 |
* @return the newly created property map |
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
17518
diff
changeset
|
93 |
*/ |
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
17518
diff
changeset
|
94 |
protected abstract PropertyMap makeMap(); |
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
17518
diff
changeset
|
95 |
|
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
17518
diff
changeset
|
96 |
/** |
16147 | 97 |
* Create a new MapCreator |
98 |
* @param clazz type of MapCreator |
|
99 |
* @return map creator instantiated by type |
|
100 |
*/ |
|
24719 | 101 |
protected MapCreator<?> newMapCreator(final Class<? extends ScriptObject> clazz) { |
102 |
return new MapCreator<>(clazz, tuples); |
|
16147 | 103 |
} |
104 |
||
105 |
/** |
|
18852
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
17518
diff
changeset
|
106 |
* Loads the scope on the stack through the passed method emitter. |
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
17518
diff
changeset
|
107 |
* @param method the method emitter to use |
16147 | 108 |
*/ |
18852
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
17518
diff
changeset
|
109 |
protected void loadScope(final MethodEmitter method) { |
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
17518
diff
changeset
|
110 |
method.loadCompilerConstant(SCOPE); |
16147 | 111 |
} |
112 |
||
113 |
/** |
|
114 |
* Emit the correct map for the object. |
|
115 |
* @param method method emitter |
|
116 |
* @return the method emitter |
|
117 |
*/ |
|
118 |
protected MethodEmitter loadMap(final MethodEmitter method) { |
|
119 |
codegen.loadConstant(propertyMap); |
|
120 |
return method; |
|
121 |
} |
|
122 |
||
24719 | 123 |
PropertyMap getMap() { |
124 |
return propertyMap; |
|
125 |
} |
|
126 |
||
16147 | 127 |
/** |
128 |
* Is this a scope object |
|
129 |
* @return true if scope |
|
130 |
*/ |
|
131 |
protected boolean isScope() { |
|
132 |
return isScope; |
|
133 |
} |
|
134 |
||
135 |
/** |
|
16224
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16216
diff
changeset
|
136 |
* Does the created object have an "arguments" property |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16216
diff
changeset
|
137 |
* @return true if has an "arguments" property |
16147 | 138 |
*/ |
16224
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16216
diff
changeset
|
139 |
protected boolean hasArguments() { |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16216
diff
changeset
|
140 |
return hasArguments; |
16147 | 141 |
} |
24719 | 142 |
|
143 |
/** |
|
32781
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
144 |
* Get the class of objects created by this ObjectCreator |
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
145 |
* @return class of created object |
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
146 |
*/ |
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
147 |
abstract protected Class<? extends ScriptObject> getAllocatorClass(); |
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
148 |
|
d8f34ffbbc7a
8135190: Method code too large in Babel browser.js script
hannesw
parents:
30390
diff
changeset
|
149 |
/** |
24719 | 150 |
* Technique for loading an initial value. Defined by anonymous subclasses in code gen. |
151 |
* |
|
152 |
* @param value Value to load. |
|
24751 | 153 |
* @param type the type of the value to load |
24719 | 154 |
*/ |
24751 | 155 |
protected abstract void loadValue(T value, Type type); |
24719 | 156 |
|
157 |
MethodEmitter loadTuple(final MethodEmitter method, final MapTuple<T> tuple, final boolean pack) { |
|
24751 | 158 |
loadValue(tuple.value, tuple.type); |
29834
f678f348c947
8067215: Disable dual fields when not using optimistic types
hannesw
parents:
25865
diff
changeset
|
159 |
if (pack && codegen.useDualFields() && tuple.isPrimitive()) { |
24719 | 160 |
method.pack(); |
161 |
} else { |
|
162 |
method.convert(Type.OBJECT); |
|
163 |
} |
|
164 |
return method; |
|
165 |
} |
|
166 |
||
167 |
MethodEmitter loadTuple(final MethodEmitter method, final MapTuple<T> tuple) { |
|
168 |
return loadTuple(method, tuple, true); |
|
169 |
} |
|
16147 | 170 |
} |