author | lagergren |
Fri, 11 Apr 2014 16:52:14 +0200 | |
changeset 24738 | be2026c9717c |
parent 24719 | f726e9d67629 |
child 24751 | ccbd9cd3f720 |
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; |
24738
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
29 |
import java.util.List; |
18852
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
17518
diff
changeset
|
30 |
|
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 |
*/ |
24719 | 39 |
public abstract class ObjectCreator<T> { |
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 |
*/ |
|
16240
e1468b33e201
8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents:
16224
diff
changeset
|
72 |
protected abstract void makeObject(final MethodEmitter method); |
16147 | 73 |
|
74 |
/** |
|
18852
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
17518
diff
changeset
|
75 |
* Construct the property map appropriate for the object. |
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
17518
diff
changeset
|
76 |
* @return the newly created property map |
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
17518
diff
changeset
|
77 |
*/ |
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
17518
diff
changeset
|
78 |
protected abstract PropertyMap makeMap(); |
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
17518
diff
changeset
|
79 |
|
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
17518
diff
changeset
|
80 |
/** |
16147 | 81 |
* Create a new MapCreator |
82 |
* @param clazz type of MapCreator |
|
83 |
* @return map creator instantiated by type |
|
84 |
*/ |
|
24719 | 85 |
protected MapCreator<?> newMapCreator(final Class<? extends ScriptObject> clazz) { |
86 |
return new MapCreator<>(clazz, tuples); |
|
16147 | 87 |
} |
88 |
||
89 |
/** |
|
18852
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
17518
diff
changeset
|
90 |
* 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
|
91 |
* @param method the method emitter to use |
16147 | 92 |
*/ |
18852
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
17518
diff
changeset
|
93 |
protected void loadScope(final MethodEmitter method) { |
604c1d681b6f
8017084: Use spill properties for large object literals
hannesw
parents:
17518
diff
changeset
|
94 |
method.loadCompilerConstant(SCOPE); |
16147 | 95 |
} |
96 |
||
97 |
/** |
|
98 |
* Emit the correct map for the object. |
|
99 |
* @param method method emitter |
|
100 |
* @return the method emitter |
|
101 |
*/ |
|
102 |
protected MethodEmitter loadMap(final MethodEmitter method) { |
|
103 |
codegen.loadConstant(propertyMap); |
|
104 |
return method; |
|
105 |
} |
|
106 |
||
24719 | 107 |
PropertyMap getMap() { |
108 |
return propertyMap; |
|
109 |
} |
|
110 |
||
16147 | 111 |
/** |
112 |
* Is this a scope object |
|
113 |
* @return true if scope |
|
114 |
*/ |
|
115 |
protected boolean isScope() { |
|
116 |
return isScope; |
|
117 |
} |
|
118 |
||
119 |
/** |
|
16224
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16216
diff
changeset
|
120 |
* Does the created object have an "arguments" property |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16216
diff
changeset
|
121 |
* @return true if has an "arguments" property |
16147 | 122 |
*/ |
16224
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16216
diff
changeset
|
123 |
protected boolean hasArguments() { |
0c49ad4e3b55
8006222: Move slot from SpillProperty to Property
jlaskey
parents:
16216
diff
changeset
|
124 |
return hasArguments; |
16147 | 125 |
} |
24719 | 126 |
|
127 |
/** |
|
128 |
* Technique for loading an initial value. Defined by anonymous subclasses in code gen. |
|
129 |
* |
|
130 |
* @param value Value to load. |
|
131 |
*/ |
|
132 |
protected abstract void loadValue(T value); |
|
133 |
||
134 |
MethodEmitter loadTuple(final MethodEmitter method, final MapTuple<T> tuple, final boolean pack) { |
|
135 |
loadValue(tuple.value); |
|
136 |
if (pack && tuple.isPrimitive()) { |
|
137 |
method.pack(); |
|
138 |
} else { |
|
139 |
method.convert(Type.OBJECT); |
|
140 |
} |
|
141 |
return method; |
|
142 |
} |
|
143 |
||
144 |
MethodEmitter loadTuple(final MethodEmitter method, final MapTuple<T> tuple) { |
|
145 |
return loadTuple(method, tuple, true); |
|
146 |
} |
|
147 |
||
16147 | 148 |
} |