author | jlahoda |
Wed, 31 May 2017 14:51:02 +0200 | |
changeset 45316 | 170696ddb377 |
parent 45220 | 7c775294b6f1 |
child 45504 | ea7475564d07 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
43765
f2f8bfe6bcb5
8169200: Gen has a reference to Flow that is not used, should be removed
vromero
parents:
42828
diff
changeset
|
2 |
* Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.jvm; |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
27 |
|
45220 | 28 |
import com.sun.tools.javac.tree.TreeInfo.PosKind; |
10 | 29 |
import com.sun.tools.javac.util.*; |
30 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
|
31 |
import com.sun.tools.javac.util.List; |
|
32 |
import com.sun.tools.javac.code.*; |
|
17578 | 33 |
import com.sun.tools.javac.code.Attribute.TypeCompound; |
34 |
import com.sun.tools.javac.code.Symbol.VarSymbol; |
|
10 | 35 |
import com.sun.tools.javac.comp.*; |
36 |
import com.sun.tools.javac.tree.*; |
|
37 |
||
38 |
import com.sun.tools.javac.code.Symbol.*; |
|
39 |
import com.sun.tools.javac.code.Type.*; |
|
40 |
import com.sun.tools.javac.jvm.Code.*; |
|
41 |
import com.sun.tools.javac.jvm.Items.*; |
|
13077 | 42 |
import com.sun.tools.javac.tree.EndPosTable; |
10 | 43 |
import com.sun.tools.javac.tree.JCTree.*; |
44 |
||
45 |
import static com.sun.tools.javac.code.Flags.*; |
|
27224
228abfa87080
8054457: Refactor Symbol kinds from small ints to an enum
emc
parents:
27123
diff
changeset
|
46 |
import static com.sun.tools.javac.code.Kinds.Kind.*; |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
47 |
import static com.sun.tools.javac.code.TypeTag.*; |
10 | 48 |
import static com.sun.tools.javac.jvm.ByteCodes.*; |
49 |
import static com.sun.tools.javac.jvm.CRTFlags.*; |
|
11314 | 50 |
import static com.sun.tools.javac.main.Option.*; |
10950 | 51 |
import static com.sun.tools.javac.tree.JCTree.Tag.*; |
10 | 52 |
|
53 |
/** This pass maps flat Java (i.e. without inner classes) to bytecodes. |
|
54 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
55 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
56 |
* If you write code that depends on this, you do so at your own risk. |
10 | 57 |
* This code and its internal interfaces are subject to change or |
58 |
* deletion without notice.</b> |
|
59 |
*/ |
|
60 |
public class Gen extends JCTree.Visitor { |
|
22163 | 61 |
protected static final Context.Key<Gen> genKey = new Context.Key<>(); |
10 | 62 |
|
63 |
private final Log log; |
|
64 |
private final Symtab syms; |
|
65 |
private final Check chk; |
|
66 |
private final Resolve rs; |
|
67 |
private final TreeMaker make; |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
10
diff
changeset
|
68 |
private final Names names; |
10 | 69 |
private final Target target; |
43765
f2f8bfe6bcb5
8169200: Gen has a reference to Flow that is not used, should be removed
vromero
parents:
42828
diff
changeset
|
70 |
private final Name accessDollar; |
10 | 71 |
private final Types types; |
14721
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
72 |
private final Lower lower; |
29842
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
73 |
private final Annotate annotate; |
35424 | 74 |
private final StringConcat concat; |
10 | 75 |
|
76 |
/** Format of stackmap tables to be generated. */ |
|
77 |
private final Code.StackMapFormat stackMap; |
|
78 |
||
79 |
/** A type that serves as the expected type for all method expressions. |
|
80 |
*/ |
|
81 |
private final Type methodType; |
|
82 |
||
36269
5b3afff0e70c
8151016: Migrate asserts introduced in Valhalla code generation to JDK9 dev
sadayapalam
parents:
36153
diff
changeset
|
83 |
/** |
5b3afff0e70c
8151016: Migrate asserts introduced in Valhalla code generation to JDK9 dev
sadayapalam
parents:
36153
diff
changeset
|
84 |
* Are we presently traversing a let expression ? Yes if depth != 0 |
5b3afff0e70c
8151016: Migrate asserts introduced in Valhalla code generation to JDK9 dev
sadayapalam
parents:
36153
diff
changeset
|
85 |
*/ |
5b3afff0e70c
8151016: Migrate asserts introduced in Valhalla code generation to JDK9 dev
sadayapalam
parents:
36153
diff
changeset
|
86 |
private int letExprDepth; |
5b3afff0e70c
8151016: Migrate asserts introduced in Valhalla code generation to JDK9 dev
sadayapalam
parents:
36153
diff
changeset
|
87 |
|
10 | 88 |
public static Gen instance(Context context) { |
89 |
Gen instance = context.get(genKey); |
|
90 |
if (instance == null) |
|
91 |
instance = new Gen(context); |
|
92 |
return instance; |
|
93 |
} |
|
94 |
||
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
95 |
/** Constant pool, reset by genClass. |
14949
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14721
diff
changeset
|
96 |
*/ |
43765
f2f8bfe6bcb5
8169200: Gen has a reference to Flow that is not used, should be removed
vromero
parents:
42828
diff
changeset
|
97 |
private final Pool pool; |
14949
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14721
diff
changeset
|
98 |
|
10 | 99 |
protected Gen(Context context) { |
100 |
context.put(genKey, this); |
|
101 |
||
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
10
diff
changeset
|
102 |
names = Names.instance(context); |
10 | 103 |
log = Log.instance(context); |
104 |
syms = Symtab.instance(context); |
|
105 |
chk = Check.instance(context); |
|
106 |
rs = Resolve.instance(context); |
|
107 |
make = TreeMaker.instance(context); |
|
108 |
target = Target.instance(context); |
|
109 |
types = Types.instance(context); |
|
35424 | 110 |
concat = StringConcat.instance(context); |
111 |
||
10 | 112 |
methodType = new MethodType(null, null, null, syms.methodClass); |
113 |
accessDollar = names. |
|
114 |
fromString("access" + target.syntheticNameChar()); |
|
14721
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
115 |
lower = Lower.instance(context); |
10 | 116 |
|
117 |
Options options = Options.instance(context); |
|
118 |
lineDebugInfo = |
|
6721 | 119 |
options.isUnset(G_CUSTOM) || |
120 |
options.isSet(G_CUSTOM, "lines"); |
|
10 | 121 |
varDebugInfo = |
6721 | 122 |
options.isUnset(G_CUSTOM) |
123 |
? options.isSet(G) |
|
124 |
: options.isSet(G_CUSTOM, "vars"); |
|
125 |
genCrt = options.isSet(XJCOV); |
|
39599 | 126 |
debugCode = options.isSet("debug.code"); |
29295
5a367770a074
8073432: Object.getClass() throws stackless NPE, due to C2 intrinsic
mcimadamore
parents:
28800
diff
changeset
|
127 |
allowBetterNullChecks = target.hasObjects(); |
14949
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14721
diff
changeset
|
128 |
pool = new Pool(types); |
10 | 129 |
|
25445
603f0c93d5c9
8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents:
25443
diff
changeset
|
130 |
// ignore cldc because we cannot have both stackmap formats |
603f0c93d5c9
8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents:
25443
diff
changeset
|
131 |
this.stackMap = StackMapFormat.JSR202; |
29842
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
132 |
annotate = Annotate.instance(context); |
10 | 133 |
} |
134 |
||
135 |
/** Switches |
|
136 |
*/ |
|
137 |
private final boolean lineDebugInfo; |
|
138 |
private final boolean varDebugInfo; |
|
139 |
private final boolean genCrt; |
|
140 |
private final boolean debugCode; |
|
29295
5a367770a074
8073432: Object.getClass() throws stackless NPE, due to C2 intrinsic
mcimadamore
parents:
28800
diff
changeset
|
141 |
private final boolean allowBetterNullChecks; |
10 | 142 |
|
143 |
/** Code buffer, set by genMethod. |
|
144 |
*/ |
|
145 |
private Code code; |
|
146 |
||
147 |
/** Items structure, set by genMethod. |
|
148 |
*/ |
|
149 |
private Items items; |
|
150 |
||
151 |
/** Environment for symbol lookup, set by genClass |
|
152 |
*/ |
|
153 |
private Env<AttrContext> attrEnv; |
|
154 |
||
155 |
/** The top level tree. |
|
156 |
*/ |
|
157 |
private JCCompilationUnit toplevel; |
|
158 |
||
159 |
/** The number of code-gen errors in this class. |
|
160 |
*/ |
|
161 |
private int nerrs = 0; |
|
162 |
||
11055 | 163 |
/** An object containing mappings of syntax trees to their |
164 |
* ending source positions. |
|
10 | 165 |
*/ |
11055 | 166 |
EndPosTable endPosTable; |
10 | 167 |
|
168 |
/** Generate code to load an integer constant. |
|
169 |
* @param n The integer to be loaded. |
|
170 |
*/ |
|
171 |
void loadIntConst(int n) { |
|
172 |
items.makeImmediateItem(syms.intType, n).load(); |
|
173 |
} |
|
174 |
||
175 |
/** The opcode that loads a zero constant of a given type code. |
|
176 |
* @param tc The given type code (@see ByteCode). |
|
177 |
*/ |
|
178 |
public static int zero(int tc) { |
|
179 |
switch(tc) { |
|
180 |
case INTcode: case BYTEcode: case SHORTcode: case CHARcode: |
|
181 |
return iconst_0; |
|
182 |
case LONGcode: |
|
183 |
return lconst_0; |
|
184 |
case FLOATcode: |
|
185 |
return fconst_0; |
|
186 |
case DOUBLEcode: |
|
187 |
return dconst_0; |
|
188 |
default: |
|
189 |
throw new AssertionError("zero"); |
|
190 |
} |
|
191 |
} |
|
192 |
||
193 |
/** The opcode that loads a one constant of a given type code. |
|
194 |
* @param tc The given type code (@see ByteCode). |
|
195 |
*/ |
|
196 |
public static int one(int tc) { |
|
197 |
return zero(tc) + 1; |
|
198 |
} |
|
199 |
||
200 |
/** Generate code to load -1 of the given type code (either int or long). |
|
201 |
* @param tc The given type code (@see ByteCode). |
|
202 |
*/ |
|
203 |
void emitMinusOne(int tc) { |
|
204 |
if (tc == LONGcode) { |
|
37635
9834489579e5
8145468: update java.lang APIs with new deprecations
smarks
parents:
36269
diff
changeset
|
205 |
items.makeImmediateItem(syms.longType, Long.valueOf(-1)).load(); |
10 | 206 |
} else { |
207 |
code.emitop0(iconst_m1); |
|
208 |
} |
|
209 |
} |
|
210 |
||
211 |
/** Construct a symbol to reflect the qualifying type that should |
|
212 |
* appear in the byte code as per JLS 13.1. |
|
213 |
* |
|
13844 | 214 |
* For {@literal target >= 1.2}: Clone a method with the qualifier as owner (except |
10 | 215 |
* for those cases where we need to work around VM bugs). |
216 |
* |
|
13844 | 217 |
* For {@literal target <= 1.1}: If qualified variable or method is defined in a |
10 | 218 |
* non-accessible class, clone it with the qualifier class as owner. |
219 |
* |
|
220 |
* @param sym The accessed symbol |
|
221 |
* @param site The qualifier's type. |
|
222 |
*/ |
|
223 |
Symbol binaryQualifier(Symbol sym, Type site) { |
|
224 |
||
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
225 |
if (site.hasTag(ARRAY)) { |
10 | 226 |
if (sym == syms.lengthVar || |
227 |
sym.owner != syms.arrayClass) |
|
228 |
return sym; |
|
229 |
// array clone can be qualified by the array type in later targets |
|
25445
603f0c93d5c9
8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents:
25443
diff
changeset
|
230 |
Symbol qualifier = new ClassSymbol(Flags.PUBLIC, site.tsym.name, |
603f0c93d5c9
8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents:
25443
diff
changeset
|
231 |
site, syms.noSymbol); |
10 | 232 |
return sym.clone(qualifier); |
233 |
} |
|
234 |
||
235 |
if (sym.owner == site.tsym || |
|
236 |
(sym.flags() & (STATIC | SYNTHETIC)) == (STATIC | SYNTHETIC)) { |
|
237 |
return sym; |
|
238 |
} |
|
239 |
||
240 |
// leave alone methods inherited from Object |
|
9303
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
9300
diff
changeset
|
241 |
// JLS 13.1. |
10 | 242 |
if (sym.owner == syms.objectType.tsym) |
243 |
return sym; |
|
244 |
||
245 |
return sym.clone(site.tsym); |
|
246 |
} |
|
247 |
||
248 |
/** Insert a reference to given type in the constant pool, |
|
249 |
* checking for an array with too many dimensions; |
|
250 |
* return the reference's index. |
|
251 |
* @param type The type for which a reference is inserted. |
|
252 |
*/ |
|
253 |
int makeRef(DiagnosticPosition pos, Type type) { |
|
254 |
checkDimension(pos, type); |
|
17578 | 255 |
if (type.isAnnotated()) { |
256 |
return pool.put((Object)type); |
|
257 |
} else { |
|
258 |
return pool.put(type.hasTag(CLASS) ? (Object)type.tsym : (Object)type); |
|
259 |
} |
|
10 | 260 |
} |
261 |
||
262 |
/** Check if the given type is an array with too many dimensions. |
|
263 |
*/ |
|
264 |
private void checkDimension(DiagnosticPosition pos, Type t) { |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
265 |
switch (t.getTag()) { |
10 | 266 |
case METHOD: |
267 |
checkDimension(pos, t.getReturnType()); |
|
268 |
for (List<Type> args = t.getParameterTypes(); args.nonEmpty(); args = args.tail) |
|
269 |
checkDimension(pos, args.head); |
|
270 |
break; |
|
271 |
case ARRAY: |
|
272 |
if (types.dimensions(t) > ClassFile.MAX_DIMENSIONS) { |
|
273 |
log.error(pos, "limit.dimensions"); |
|
274 |
nerrs++; |
|
275 |
} |
|
276 |
break; |
|
277 |
default: |
|
278 |
break; |
|
279 |
} |
|
280 |
} |
|
281 |
||
282 |
/** Create a tempory variable. |
|
283 |
* @param type The variable's type. |
|
284 |
*/ |
|
285 |
LocalItem makeTemp(Type type) { |
|
286 |
VarSymbol v = new VarSymbol(Flags.SYNTHETIC, |
|
287 |
names.empty, |
|
288 |
type, |
|
289 |
env.enclMethod.sym); |
|
290 |
code.newLocal(v); |
|
291 |
return items.makeLocalItem(v); |
|
292 |
} |
|
293 |
||
294 |
/** Generate code to call a non-private method or constructor. |
|
295 |
* @param pos Position to be used for error reporting. |
|
296 |
* @param site The type of which the method is a member. |
|
297 |
* @param name The method's name. |
|
298 |
* @param argtypes The method's argument types. |
|
299 |
* @param isStatic A flag that indicates whether we call a |
|
300 |
* static or instance method. |
|
301 |
*/ |
|
302 |
void callMethod(DiagnosticPosition pos, |
|
303 |
Type site, Name name, List<Type> argtypes, |
|
304 |
boolean isStatic) { |
|
305 |
Symbol msym = rs. |
|
306 |
resolveInternalMethod(pos, attrEnv, site, name, argtypes, null); |
|
307 |
if (isStatic) items.makeStaticItem(msym).invoke(); |
|
308 |
else items.makeMemberItem(msym, name == names.init).invoke(); |
|
309 |
} |
|
310 |
||
311 |
/** Is the given method definition an access method |
|
312 |
* resulting from a qualified super? This is signified by an odd |
|
313 |
* access code. |
|
314 |
*/ |
|
315 |
private boolean isAccessSuper(JCMethodDecl enclMethod) { |
|
316 |
return |
|
317 |
(enclMethod.mods.flags & SYNTHETIC) != 0 && |
|
318 |
isOddAccessName(enclMethod.name); |
|
319 |
} |
|
320 |
||
321 |
/** Does given name start with "access$" and end in an odd digit? |
|
322 |
*/ |
|
323 |
private boolean isOddAccessName(Name name) { |
|
324 |
return |
|
325 |
name.startsWith(accessDollar) && |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
10
diff
changeset
|
326 |
(name.getByteAt(name.getByteLength() - 1) & 1) == 1; |
10 | 327 |
} |
328 |
||
329 |
/* ************************************************************************ |
|
330 |
* Non-local exits |
|
331 |
*************************************************************************/ |
|
332 |
||
333 |
/** Generate code to invoke the finalizer associated with given |
|
334 |
* environment. |
|
335 |
* Any calls to finalizers are appended to the environments `cont' chain. |
|
336 |
* Mark beginning of gap in catch all range for finalizer. |
|
337 |
*/ |
|
338 |
void genFinalizer(Env<GenContext> env) { |
|
339 |
if (code.isAlive() && env.info.finalize != null) |
|
340 |
env.info.finalize.gen(); |
|
341 |
} |
|
342 |
||
343 |
/** Generate code to call all finalizers of structures aborted by |
|
344 |
* a non-local |
|
345 |
* exit. Return target environment of the non-local exit. |
|
346 |
* @param target The tree representing the structure that's aborted |
|
347 |
* @param env The environment current at the non-local exit. |
|
348 |
*/ |
|
349 |
Env<GenContext> unwind(JCTree target, Env<GenContext> env) { |
|
350 |
Env<GenContext> env1 = env; |
|
351 |
while (true) { |
|
352 |
genFinalizer(env1); |
|
353 |
if (env1.tree == target) break; |
|
354 |
env1 = env1.next; |
|
355 |
} |
|
356 |
return env1; |
|
357 |
} |
|
358 |
||
359 |
/** Mark end of gap in catch-all range for finalizer. |
|
360 |
* @param env the environment which might contain the finalizer |
|
361 |
* (if it does, env.info.gaps != null). |
|
362 |
*/ |
|
363 |
void endFinalizerGap(Env<GenContext> env) { |
|
364 |
if (env.info.gaps != null && env.info.gaps.length() % 2 == 1) |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
365 |
env.info.gaps.append(code.curCP()); |
10 | 366 |
} |
367 |
||
368 |
/** Mark end of all gaps in catch-all ranges for finalizers of environments |
|
369 |
* lying between, and including to two environments. |
|
370 |
* @param from the most deeply nested environment to mark |
|
371 |
* @param to the least deeply nested environment to mark |
|
372 |
*/ |
|
373 |
void endFinalizerGaps(Env<GenContext> from, Env<GenContext> to) { |
|
374 |
Env<GenContext> last = null; |
|
375 |
while (last != to) { |
|
376 |
endFinalizerGap(from); |
|
377 |
last = from; |
|
378 |
from = from.next; |
|
379 |
} |
|
380 |
} |
|
381 |
||
382 |
/** Do any of the structures aborted by a non-local exit have |
|
383 |
* finalizers that require an empty stack? |
|
384 |
* @param target The tree representing the structure that's aborted |
|
385 |
* @param env The environment current at the non-local exit. |
|
386 |
*/ |
|
387 |
boolean hasFinally(JCTree target, Env<GenContext> env) { |
|
388 |
while (env.tree != target) { |
|
10950 | 389 |
if (env.tree.hasTag(TRY) && env.info.finalize.hasFinalizer()) |
10 | 390 |
return true; |
391 |
env = env.next; |
|
392 |
} |
|
393 |
return false; |
|
394 |
} |
|
395 |
||
396 |
/* ************************************************************************ |
|
397 |
* Normalizing class-members. |
|
398 |
*************************************************************************/ |
|
399 |
||
14259 | 400 |
/** Distribute member initializer code into constructors and {@code <clinit>} |
10 | 401 |
* method. |
402 |
* @param defs The list of class member declarations. |
|
403 |
* @param c The enclosing class. |
|
404 |
*/ |
|
405 |
List<JCTree> normalizeDefs(List<JCTree> defs, ClassSymbol c) { |
|
22163 | 406 |
ListBuffer<JCStatement> initCode = new ListBuffer<>(); |
407 |
ListBuffer<Attribute.TypeCompound> initTAs = new ListBuffer<>(); |
|
408 |
ListBuffer<JCStatement> clinitCode = new ListBuffer<>(); |
|
409 |
ListBuffer<Attribute.TypeCompound> clinitTAs = new ListBuffer<>(); |
|
410 |
ListBuffer<JCTree> methodDefs = new ListBuffer<>(); |
|
10 | 411 |
// Sort definitions into three listbuffers: |
412 |
// - initCode for instance initializers |
|
413 |
// - clinitCode for class initializers |
|
414 |
// - methodDefs for method definitions |
|
415 |
for (List<JCTree> l = defs; l.nonEmpty(); l = l.tail) { |
|
416 |
JCTree def = l.head; |
|
417 |
switch (def.getTag()) { |
|
10950 | 418 |
case BLOCK: |
10 | 419 |
JCBlock block = (JCBlock)def; |
420 |
if ((block.flags & STATIC) != 0) |
|
421 |
clinitCode.append(block); |
|
27230
361efbfa3079
8061778: Wrong LineNumberTable for default constructors
mcimadamore
parents:
27224
diff
changeset
|
422 |
else if ((block.flags & SYNTHETIC) == 0) |
10 | 423 |
initCode.append(block); |
424 |
break; |
|
10950 | 425 |
case METHODDEF: |
10 | 426 |
methodDefs.append(def); |
427 |
break; |
|
10950 | 428 |
case VARDEF: |
10 | 429 |
JCVariableDecl vdef = (JCVariableDecl) def; |
430 |
VarSymbol sym = vdef.sym; |
|
431 |
checkDimension(vdef.pos(), sym.type); |
|
432 |
if (vdef.init != null) { |
|
433 |
if ((sym.flags() & STATIC) == 0) { |
|
434 |
// Always initialize instance variables. |
|
435 |
JCStatement init = make.at(vdef.pos()). |
|
436 |
Assignment(sym, vdef.init); |
|
437 |
initCode.append(init); |
|
11055 | 438 |
endPosTable.replaceTree(vdef, init); |
17578 | 439 |
initTAs.addAll(getAndRemoveNonFieldTAs(sym)); |
10 | 440 |
} else if (sym.getConstValue() == null) { |
441 |
// Initialize class (static) variables only if |
|
442 |
// they are not compile-time constants. |
|
443 |
JCStatement init = make.at(vdef.pos). |
|
444 |
Assignment(sym, vdef.init); |
|
445 |
clinitCode.append(init); |
|
11055 | 446 |
endPosTable.replaceTree(vdef, init); |
17578 | 447 |
clinitTAs.addAll(getAndRemoveNonFieldTAs(sym)); |
10 | 448 |
} else { |
449 |
checkStringConstant(vdef.init.pos(), sym.getConstValue()); |
|
31000
18bc6fadeb4e
8073372: Redundant CONSTANT_Class entry not generated for inlined constant
vromero
parents:
30406
diff
changeset
|
450 |
/* if the init contains a reference to an external class, add it to the |
18bc6fadeb4e
8073372: Redundant CONSTANT_Class entry not generated for inlined constant
vromero
parents:
30406
diff
changeset
|
451 |
* constant's pool |
18bc6fadeb4e
8073372: Redundant CONSTANT_Class entry not generated for inlined constant
vromero
parents:
30406
diff
changeset
|
452 |
*/ |
18bc6fadeb4e
8073372: Redundant CONSTANT_Class entry not generated for inlined constant
vromero
parents:
30406
diff
changeset
|
453 |
vdef.init.accept(classReferenceVisitor); |
10 | 454 |
} |
455 |
} |
|
456 |
break; |
|
457 |
default: |
|
8032 | 458 |
Assert.error(); |
10 | 459 |
} |
460 |
} |
|
461 |
// Insert any instance initializers into all constructors. |
|
462 |
if (initCode.length() != 0) { |
|
463 |
List<JCStatement> inits = initCode.toList(); |
|
18010
604faee85350
8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents:
17578
diff
changeset
|
464 |
initTAs.addAll(c.getInitTypeAttributes()); |
17578 | 465 |
List<Attribute.TypeCompound> initTAlist = initTAs.toList(); |
10 | 466 |
for (JCTree t : methodDefs) { |
17578 | 467 |
normalizeMethod((JCMethodDecl)t, inits, initTAlist); |
10 | 468 |
} |
469 |
} |
|
470 |
// If there are class initializers, create a <clinit> method |
|
471 |
// that contains them as its body. |
|
472 |
if (clinitCode.length() != 0) { |
|
473 |
MethodSymbol clinit = new MethodSymbol( |
|
15710
5792a085da41
7166455: javac doesn't set ACC_STRICT bit on <clinit> for strictfp class
vromero
parents:
15385
diff
changeset
|
474 |
STATIC | (c.flags() & STRICTFP), |
5792a085da41
7166455: javac doesn't set ACC_STRICT bit on <clinit> for strictfp class
vromero
parents:
15385
diff
changeset
|
475 |
names.clinit, |
10 | 476 |
new MethodType( |
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
41029
diff
changeset
|
477 |
List.nil(), syms.voidType, |
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
41029
diff
changeset
|
478 |
List.nil(), syms.methodClass), |
10 | 479 |
c); |
480 |
c.members().enter(clinit); |
|
481 |
List<JCStatement> clinitStats = clinitCode.toList(); |
|
482 |
JCBlock block = make.at(clinitStats.head.pos()).Block(0, clinitStats); |
|
483 |
block.endpos = TreeInfo.endPos(clinitStats.last()); |
|
484 |
methodDefs.append(make.MethodDef(clinit, block)); |
|
17578 | 485 |
|
486 |
if (!clinitTAs.isEmpty()) |
|
18010
604faee85350
8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents:
17578
diff
changeset
|
487 |
clinit.appendUniqueTypeAttributes(clinitTAs.toList()); |
604faee85350
8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents:
17578
diff
changeset
|
488 |
if (!c.getClassInitTypeAttributes().isEmpty()) |
604faee85350
8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents:
17578
diff
changeset
|
489 |
clinit.appendUniqueTypeAttributes(c.getClassInitTypeAttributes()); |
10 | 490 |
} |
491 |
// Return all method definitions. |
|
492 |
return methodDefs.toList(); |
|
493 |
} |
|
494 |
||
17578 | 495 |
private List<Attribute.TypeCompound> getAndRemoveNonFieldTAs(VarSymbol sym) { |
496 |
List<TypeCompound> tas = sym.getRawTypeAttributes(); |
|
22163 | 497 |
ListBuffer<Attribute.TypeCompound> fieldTAs = new ListBuffer<>(); |
498 |
ListBuffer<Attribute.TypeCompound> nonfieldTAs = new ListBuffer<>(); |
|
17578 | 499 |
for (TypeCompound ta : tas) { |
26532
aa84b6606229
8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents:
25874
diff
changeset
|
500 |
Assert.check(ta.getPosition().type != TargetType.UNKNOWN); |
21010
5ffe0d8a5e24
8008762: Type annotation on inner class in anonymous class show up as regular type annotations
emc
parents:
19941
diff
changeset
|
501 |
if (ta.getPosition().type == TargetType.FIELD) { |
17578 | 502 |
fieldTAs.add(ta); |
503 |
} else { |
|
504 |
nonfieldTAs.add(ta); |
|
505 |
} |
|
506 |
} |
|
18010
604faee85350
8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents:
17578
diff
changeset
|
507 |
sym.setTypeAttributes(fieldTAs.toList()); |
17578 | 508 |
return nonfieldTAs.toList(); |
509 |
} |
|
510 |
||
10 | 511 |
/** Check a constant value and report if it is a string that is |
512 |
* too large. |
|
513 |
*/ |
|
514 |
private void checkStringConstant(DiagnosticPosition pos, Object constValue) { |
|
515 |
if (nerrs != 0 || // only complain about a long string once |
|
516 |
constValue == null || |
|
517 |
!(constValue instanceof String) || |
|
518 |
((String)constValue).length() < Pool.MAX_STRING_LENGTH) |
|
519 |
return; |
|
520 |
log.error(pos, "limit.string"); |
|
521 |
nerrs++; |
|
522 |
} |
|
523 |
||
524 |
/** Insert instance initializer code into initial constructor. |
|
525 |
* @param md The tree potentially representing a |
|
526 |
* constructor's definition. |
|
527 |
* @param initCode The list of instance initializer statements. |
|
17578 | 528 |
* @param initTAs Type annotations from the initializer expression. |
10 | 529 |
*/ |
17578 | 530 |
void normalizeMethod(JCMethodDecl md, List<JCStatement> initCode, List<TypeCompound> initTAs) { |
10 | 531 |
if (md.name == names.init && TreeInfo.isInitialConstructor(md)) { |
532 |
// We are seeing a constructor that does not call another |
|
533 |
// constructor of the same class. |
|
534 |
List<JCStatement> stats = md.body.stats; |
|
22163 | 535 |
ListBuffer<JCStatement> newstats = new ListBuffer<>(); |
10 | 536 |
|
537 |
if (stats.nonEmpty()) { |
|
538 |
// Copy initializers of synthetic variables generated in |
|
539 |
// the translation of inner classes. |
|
540 |
while (TreeInfo.isSyntheticInit(stats.head)) { |
|
541 |
newstats.append(stats.head); |
|
542 |
stats = stats.tail; |
|
543 |
} |
|
544 |
// Copy superclass constructor call |
|
545 |
newstats.append(stats.head); |
|
546 |
stats = stats.tail; |
|
547 |
// Copy remaining synthetic initializers. |
|
548 |
while (stats.nonEmpty() && |
|
549 |
TreeInfo.isSyntheticInit(stats.head)) { |
|
550 |
newstats.append(stats.head); |
|
551 |
stats = stats.tail; |
|
552 |
} |
|
553 |
// Now insert the initializer code. |
|
554 |
newstats.appendList(initCode); |
|
555 |
// And copy all remaining statements. |
|
556 |
while (stats.nonEmpty()) { |
|
557 |
newstats.append(stats.head); |
|
558 |
stats = stats.tail; |
|
559 |
} |
|
560 |
} |
|
561 |
md.body.stats = newstats.toList(); |
|
562 |
if (md.body.endpos == Position.NOPOS) |
|
563 |
md.body.endpos = TreeInfo.endPos(md.body.stats.last()); |
|
17578 | 564 |
|
18010
604faee85350
8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents:
17578
diff
changeset
|
565 |
md.sym.appendUniqueTypeAttributes(initTAs); |
10 | 566 |
} |
567 |
} |
|
568 |
||
569 |
/* ************************************************************************ |
|
570 |
* Traversal methods |
|
571 |
*************************************************************************/ |
|
572 |
||
573 |
/** Visitor argument: The current environment. |
|
574 |
*/ |
|
575 |
Env<GenContext> env; |
|
576 |
||
577 |
/** Visitor argument: The expected type (prototype). |
|
578 |
*/ |
|
579 |
Type pt; |
|
580 |
||
581 |
/** Visitor result: The item representing the computed value. |
|
582 |
*/ |
|
583 |
Item result; |
|
584 |
||
585 |
/** Visitor method: generate code for a definition, catching and reporting |
|
586 |
* any completion failures. |
|
587 |
* @param tree The definition to be visited. |
|
588 |
* @param env The environment current at the definition. |
|
589 |
*/ |
|
590 |
public void genDef(JCTree tree, Env<GenContext> env) { |
|
591 |
Env<GenContext> prevEnv = this.env; |
|
592 |
try { |
|
593 |
this.env = env; |
|
594 |
tree.accept(this); |
|
595 |
} catch (CompletionFailure ex) { |
|
596 |
chk.completionError(tree.pos(), ex); |
|
597 |
} finally { |
|
598 |
this.env = prevEnv; |
|
599 |
} |
|
600 |
} |
|
601 |
||
602 |
/** Derived visitor method: check whether CharacterRangeTable |
|
603 |
* should be emitted, if so, put a new entry into CRTable |
|
604 |
* and call method to generate bytecode. |
|
605 |
* If not, just call method to generate bytecode. |
|
14259 | 606 |
* @see #genStat(JCTree, Env) |
10 | 607 |
* |
608 |
* @param tree The tree to be visited. |
|
609 |
* @param env The environment to use. |
|
610 |
* @param crtFlags The CharacterRangeTable flags |
|
611 |
* indicating type of the entry. |
|
612 |
*/ |
|
613 |
public void genStat(JCTree tree, Env<GenContext> env, int crtFlags) { |
|
614 |
if (!genCrt) { |
|
615 |
genStat(tree, env); |
|
616 |
return; |
|
617 |
} |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
618 |
int startpc = code.curCP(); |
10 | 619 |
genStat(tree, env); |
14949
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14721
diff
changeset
|
620 |
if (tree.hasTag(Tag.BLOCK)) crtFlags |= CRT_BLOCK; |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
621 |
code.crt.put(tree, crtFlags, startpc, code.curCP()); |
10 | 622 |
} |
623 |
||
624 |
/** Derived visitor method: generate code for a statement. |
|
625 |
*/ |
|
626 |
public void genStat(JCTree tree, Env<GenContext> env) { |
|
627 |
if (code.isAlive()) { |
|
628 |
code.statBegin(tree.pos); |
|
629 |
genDef(tree, env); |
|
10950 | 630 |
} else if (env.info.isSwitch && tree.hasTag(VARDEF)) { |
10 | 631 |
// variables whose declarations are in a switch |
632 |
// can be used even if the decl is unreachable. |
|
633 |
code.newLocal(((JCVariableDecl) tree).sym); |
|
634 |
} |
|
635 |
} |
|
636 |
||
637 |
/** Derived visitor method: check whether CharacterRangeTable |
|
638 |
* should be emitted, if so, put a new entry into CRTable |
|
639 |
* and call method to generate bytecode. |
|
640 |
* If not, just call method to generate bytecode. |
|
641 |
* @see #genStats(List, Env) |
|
642 |
* |
|
643 |
* @param trees The list of trees to be visited. |
|
644 |
* @param env The environment to use. |
|
645 |
* @param crtFlags The CharacterRangeTable flags |
|
646 |
* indicating type of the entry. |
|
647 |
*/ |
|
648 |
public void genStats(List<JCStatement> trees, Env<GenContext> env, int crtFlags) { |
|
649 |
if (!genCrt) { |
|
650 |
genStats(trees, env); |
|
651 |
return; |
|
652 |
} |
|
653 |
if (trees.length() == 1) { // mark one statement with the flags |
|
654 |
genStat(trees.head, env, crtFlags | CRT_STATEMENT); |
|
655 |
} else { |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
656 |
int startpc = code.curCP(); |
10 | 657 |
genStats(trees, env); |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
658 |
code.crt.put(trees, crtFlags, startpc, code.curCP()); |
10 | 659 |
} |
660 |
} |
|
661 |
||
662 |
/** Derived visitor method: generate code for a list of statements. |
|
663 |
*/ |
|
664 |
public void genStats(List<? extends JCTree> trees, Env<GenContext> env) { |
|
665 |
for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail) |
|
666 |
genStat(l.head, env, CRT_STATEMENT); |
|
667 |
} |
|
668 |
||
669 |
/** Derived visitor method: check whether CharacterRangeTable |
|
670 |
* should be emitted, if so, put a new entry into CRTable |
|
671 |
* and call method to generate bytecode. |
|
672 |
* If not, just call method to generate bytecode. |
|
14259 | 673 |
* @see #genCond(JCTree,boolean) |
10 | 674 |
* |
675 |
* @param tree The tree to be visited. |
|
676 |
* @param crtFlags The CharacterRangeTable flags |
|
677 |
* indicating type of the entry. |
|
678 |
*/ |
|
679 |
public CondItem genCond(JCTree tree, int crtFlags) { |
|
680 |
if (!genCrt) return genCond(tree, false); |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
681 |
int startpc = code.curCP(); |
10 | 682 |
CondItem item = genCond(tree, (crtFlags & CRT_FLOW_CONTROLLER) != 0); |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
683 |
code.crt.put(tree, crtFlags, startpc, code.curCP()); |
10 | 684 |
return item; |
685 |
} |
|
686 |
||
687 |
/** Derived visitor method: generate code for a boolean |
|
688 |
* expression in a control-flow context. |
|
689 |
* @param _tree The expression to be visited. |
|
690 |
* @param markBranches The flag to indicate that the condition is |
|
691 |
* a flow controller so produced conditions |
|
692 |
* should contain a proper tree to generate |
|
693 |
* CharacterRangeTable branches for them. |
|
694 |
*/ |
|
695 |
public CondItem genCond(JCTree _tree, boolean markBranches) { |
|
696 |
JCTree inner_tree = TreeInfo.skipParens(_tree); |
|
10950 | 697 |
if (inner_tree.hasTag(CONDEXPR)) { |
10 | 698 |
JCConditional tree = (JCConditional)inner_tree; |
699 |
CondItem cond = genCond(tree.cond, CRT_FLOW_CONTROLLER); |
|
700 |
if (cond.isTrue()) { |
|
701 |
code.resolve(cond.trueJumps); |
|
702 |
CondItem result = genCond(tree.truepart, CRT_FLOW_TARGET); |
|
703 |
if (markBranches) result.tree = tree.truepart; |
|
704 |
return result; |
|
705 |
} |
|
706 |
if (cond.isFalse()) { |
|
707 |
code.resolve(cond.falseJumps); |
|
708 |
CondItem result = genCond(tree.falsepart, CRT_FLOW_TARGET); |
|
709 |
if (markBranches) result.tree = tree.falsepart; |
|
710 |
return result; |
|
711 |
} |
|
712 |
Chain secondJumps = cond.jumpFalse(); |
|
713 |
code.resolve(cond.trueJumps); |
|
714 |
CondItem first = genCond(tree.truepart, CRT_FLOW_TARGET); |
|
715 |
if (markBranches) first.tree = tree.truepart; |
|
716 |
Chain falseJumps = first.jumpFalse(); |
|
717 |
code.resolve(first.trueJumps); |
|
718 |
Chain trueJumps = code.branch(goto_); |
|
719 |
code.resolve(secondJumps); |
|
720 |
CondItem second = genCond(tree.falsepart, CRT_FLOW_TARGET); |
|
721 |
CondItem result = items.makeCondItem(second.opcode, |
|
5004
6c2694dc2da0
6929544: langtools source code uses statics qualified by instance variables
jjg
parents:
4875
diff
changeset
|
722 |
Code.mergeChains(trueJumps, second.trueJumps), |
6c2694dc2da0
6929544: langtools source code uses statics qualified by instance variables
jjg
parents:
4875
diff
changeset
|
723 |
Code.mergeChains(falseJumps, second.falseJumps)); |
10 | 724 |
if (markBranches) result.tree = tree.falsepart; |
725 |
return result; |
|
726 |
} else { |
|
727 |
CondItem result = genExpr(_tree, syms.booleanType).mkCond(); |
|
728 |
if (markBranches) result.tree = _tree; |
|
729 |
return result; |
|
730 |
} |
|
731 |
} |
|
732 |
||
35424 | 733 |
public Code getCode() { |
734 |
return code; |
|
735 |
} |
|
736 |
||
737 |
public Items getItems() { |
|
738 |
return items; |
|
739 |
} |
|
740 |
||
741 |
public Env<AttrContext> getAttrEnv() { |
|
742 |
return attrEnv; |
|
743 |
} |
|
744 |
||
14721
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
745 |
/** Visitor class for expressions which might be constant expressions. |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
746 |
* This class is a subset of TreeScanner. Intended to visit trees pruned by |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
747 |
* Lower as long as constant expressions looking for references to any |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
748 |
* ClassSymbol. Any such reference will be added to the constant pool so |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
749 |
* automated tools can detect class dependencies better. |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
750 |
*/ |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
751 |
class ClassReferenceVisitor extends JCTree.Visitor { |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
752 |
|
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
753 |
@Override |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
754 |
public void visitTree(JCTree tree) {} |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
755 |
|
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
756 |
@Override |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
757 |
public void visitBinary(JCBinary tree) { |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
758 |
tree.lhs.accept(this); |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
759 |
tree.rhs.accept(this); |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
760 |
} |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
761 |
|
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
762 |
@Override |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
763 |
public void visitSelect(JCFieldAccess tree) { |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
764 |
if (tree.selected.type.hasTag(CLASS)) { |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
765 |
makeRef(tree.selected.pos(), tree.selected.type); |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
766 |
} |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
767 |
} |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
768 |
|
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
769 |
@Override |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
770 |
public void visitIdent(JCIdent tree) { |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
771 |
if (tree.sym.owner instanceof ClassSymbol) { |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
772 |
pool.put(tree.sym.owner); |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
773 |
} |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
774 |
} |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
775 |
|
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
776 |
@Override |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
777 |
public void visitConditional(JCConditional tree) { |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
778 |
tree.cond.accept(this); |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
779 |
tree.truepart.accept(this); |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
780 |
tree.falsepart.accept(this); |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
781 |
} |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
782 |
|
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
783 |
@Override |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
784 |
public void visitUnary(JCUnary tree) { |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
785 |
tree.arg.accept(this); |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
786 |
} |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
787 |
|
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
788 |
@Override |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
789 |
public void visitParens(JCParens tree) { |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
790 |
tree.expr.accept(this); |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
791 |
} |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
792 |
|
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
793 |
@Override |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
794 |
public void visitTypeCast(JCTypeCast tree) { |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
795 |
tree.expr.accept(this); |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
796 |
} |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
797 |
} |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
798 |
|
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
799 |
private ClassReferenceVisitor classReferenceVisitor = new ClassReferenceVisitor(); |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
800 |
|
10 | 801 |
/** Visitor method: generate code for an expression, catching and reporting |
802 |
* any completion failures. |
|
803 |
* @param tree The expression to be visited. |
|
804 |
* @param pt The expression's expected type (proto-type). |
|
805 |
*/ |
|
806 |
public Item genExpr(JCTree tree, Type pt) { |
|
807 |
Type prevPt = this.pt; |
|
808 |
try { |
|
809 |
if (tree.type.constValue() != null) { |
|
810 |
// Short circuit any expressions which are constants |
|
14721
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
811 |
tree.accept(classReferenceVisitor); |
10 | 812 |
checkStringConstant(tree.pos(), tree.type.constValue()); |
813 |
result = items.makeImmediateItem(tree.type, tree.type.constValue()); |
|
814 |
} else { |
|
815 |
this.pt = pt; |
|
816 |
tree.accept(this); |
|
817 |
} |
|
818 |
return result.coerce(pt); |
|
819 |
} catch (CompletionFailure ex) { |
|
820 |
chk.completionError(tree.pos(), ex); |
|
821 |
code.state.stacksize = 1; |
|
822 |
return items.makeStackItem(pt); |
|
823 |
} finally { |
|
824 |
this.pt = prevPt; |
|
825 |
} |
|
826 |
} |
|
827 |
||
828 |
/** Derived visitor method: generate code for a list of method arguments. |
|
829 |
* @param trees The argument expressions to be visited. |
|
830 |
* @param pts The expression's expected types (i.e. the formal parameter |
|
831 |
* types of the invoked method). |
|
832 |
*/ |
|
833 |
public void genArgs(List<JCExpression> trees, List<Type> pts) { |
|
834 |
for (List<JCExpression> l = trees; l.nonEmpty(); l = l.tail) { |
|
835 |
genExpr(l.head, pts.head).load(); |
|
836 |
pts = pts.tail; |
|
837 |
} |
|
838 |
// require lists be of same length |
|
8032 | 839 |
Assert.check(pts.isEmpty()); |
10 | 840 |
} |
841 |
||
842 |
/* ************************************************************************ |
|
843 |
* Visitor methods for statements and definitions |
|
844 |
*************************************************************************/ |
|
845 |
||
846 |
/** Thrown when the byte code size exceeds limit. |
|
847 |
*/ |
|
848 |
public static class CodeSizeOverflow extends RuntimeException { |
|
849 |
private static final long serialVersionUID = 0; |
|
850 |
public CodeSizeOverflow() {} |
|
851 |
} |
|
852 |
||
853 |
public void visitMethodDef(JCMethodDecl tree) { |
|
854 |
// Create a new local environment that points pack at method |
|
855 |
// definition. |
|
856 |
Env<GenContext> localEnv = env.dup(tree); |
|
857 |
localEnv.enclMethod = tree; |
|
858 |
// The expected type of every return statement in this method |
|
859 |
// is the method's return type. |
|
860 |
this.pt = tree.sym.erasure(types).getReturnType(); |
|
861 |
||
862 |
checkDimension(tree.pos(), tree.sym.erasure(types)); |
|
863 |
genMethod(tree, localEnv, false); |
|
864 |
} |
|
865 |
//where |
|
866 |
/** Generate code for a method. |
|
867 |
* @param tree The tree representing the method definition. |
|
868 |
* @param env The environment current for the method body. |
|
869 |
* @param fatcode A flag that indicates whether all jumps are |
|
870 |
* within 32K. We first invoke this method under |
|
871 |
* the assumption that fatcode == false, i.e. all |
|
872 |
* jumps are within 32K. If this fails, fatcode |
|
873 |
* is set to true and we try again. |
|
874 |
*/ |
|
875 |
void genMethod(JCMethodDecl tree, Env<GenContext> env, boolean fatcode) { |
|
876 |
MethodSymbol meth = tree.sym; |
|
18653
1407564fb956
8014230: Compilation incorrectly succeeds with inner class constructor with 254 parameters
emc
parents:
18010
diff
changeset
|
877 |
int extras = 0; |
1407564fb956
8014230: Compilation incorrectly succeeds with inner class constructor with 254 parameters
emc
parents:
18010
diff
changeset
|
878 |
// Count up extra parameters |
1407564fb956
8014230: Compilation incorrectly succeeds with inner class constructor with 254 parameters
emc
parents:
18010
diff
changeset
|
879 |
if (meth.isConstructor()) { |
1407564fb956
8014230: Compilation incorrectly succeeds with inner class constructor with 254 parameters
emc
parents:
18010
diff
changeset
|
880 |
extras++; |
1407564fb956
8014230: Compilation incorrectly succeeds with inner class constructor with 254 parameters
emc
parents:
18010
diff
changeset
|
881 |
if (meth.enclClass().isInner() && |
1407564fb956
8014230: Compilation incorrectly succeeds with inner class constructor with 254 parameters
emc
parents:
18010
diff
changeset
|
882 |
!meth.enclClass().isStatic()) { |
1407564fb956
8014230: Compilation incorrectly succeeds with inner class constructor with 254 parameters
emc
parents:
18010
diff
changeset
|
883 |
extras++; |
1407564fb956
8014230: Compilation incorrectly succeeds with inner class constructor with 254 parameters
emc
parents:
18010
diff
changeset
|
884 |
} |
1407564fb956
8014230: Compilation incorrectly succeeds with inner class constructor with 254 parameters
emc
parents:
18010
diff
changeset
|
885 |
} else if ((tree.mods.flags & STATIC) == 0) { |
1407564fb956
8014230: Compilation incorrectly succeeds with inner class constructor with 254 parameters
emc
parents:
18010
diff
changeset
|
886 |
extras++; |
1407564fb956
8014230: Compilation incorrectly succeeds with inner class constructor with 254 parameters
emc
parents:
18010
diff
changeset
|
887 |
} |
1407564fb956
8014230: Compilation incorrectly succeeds with inner class constructor with 254 parameters
emc
parents:
18010
diff
changeset
|
888 |
// System.err.println("Generating " + meth + " in " + meth.owner); //DEBUG |
1407564fb956
8014230: Compilation incorrectly succeeds with inner class constructor with 254 parameters
emc
parents:
18010
diff
changeset
|
889 |
if (Code.width(types.erasure(env.enclMethod.sym.type).getParameterTypes()) + extras > |
10 | 890 |
ClassFile.MAX_PARAMETERS) { |
891 |
log.error(tree.pos(), "limit.parameters"); |
|
892 |
nerrs++; |
|
893 |
} |
|
894 |
||
895 |
else if (tree.body != null) { |
|
896 |
// Create a new code structure and initialize it. |
|
897 |
int startpcCrt = initCode(tree, env, fatcode); |
|
898 |
||
899 |
try { |
|
900 |
genStat(tree.body, env); |
|
901 |
} catch (CodeSizeOverflow e) { |
|
902 |
// Failed due to code limit, try again with jsr/ret |
|
903 |
startpcCrt = initCode(tree, env, fatcode); |
|
904 |
genStat(tree.body, env); |
|
905 |
} |
|
906 |
||
907 |
if (code.state.stacksize != 0) { |
|
908 |
log.error(tree.body.pos(), "stack.sim.error", tree); |
|
909 |
throw new AssertionError(); |
|
910 |
} |
|
911 |
||
912 |
// If last statement could complete normally, insert a |
|
913 |
// return at the end. |
|
914 |
if (code.isAlive()) { |
|
915 |
code.statBegin(TreeInfo.endPos(tree.body)); |
|
916 |
if (env.enclMethod == null || |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
917 |
env.enclMethod.sym.type.getReturnType().hasTag(VOID)) { |
10 | 918 |
code.emitop0(return_); |
919 |
} else { |
|
920 |
// sometime dead code seems alive (4415991); |
|
921 |
// generate a small loop instead |
|
922 |
int startpc = code.entryPoint(); |
|
923 |
CondItem c = items.makeCondItem(goto_); |
|
924 |
code.resolve(c.jumpTrue(), startpc); |
|
925 |
} |
|
926 |
} |
|
927 |
if (genCrt) |
|
928 |
code.crt.put(tree.body, |
|
929 |
CRT_BLOCK, |
|
930 |
startpcCrt, |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
931 |
code.curCP()); |
10 | 932 |
|
933 |
code.endScopes(0); |
|
934 |
||
935 |
// If we exceeded limits, panic |
|
936 |
if (code.checkLimits(tree.pos(), log)) { |
|
937 |
nerrs++; |
|
938 |
return; |
|
939 |
} |
|
940 |
||
941 |
// If we generated short code but got a long jump, do it again |
|
942 |
// with fatCode = true. |
|
943 |
if (!fatcode && code.fatcode) genMethod(tree, env, true); |
|
944 |
||
945 |
// Clean up |
|
946 |
if(stackMap == StackMapFormat.JSR202) { |
|
947 |
code.lastFrame = null; |
|
948 |
code.frameBeforeLast = null; |
|
949 |
} |
|
10811
4d4ed480210e
7093325: Redundant entry in bytecode exception table
mcimadamore
parents:
10457
diff
changeset
|
950 |
|
15385 | 951 |
// Compress exception table |
10811
4d4ed480210e
7093325: Redundant entry in bytecode exception table
mcimadamore
parents:
10457
diff
changeset
|
952 |
code.compressCatchTable(); |
15385 | 953 |
|
954 |
// Fill in type annotation positions for exception parameters |
|
955 |
code.fillExceptionParameterPositions(); |
|
10 | 956 |
} |
957 |
} |
|
958 |
||
959 |
private int initCode(JCMethodDecl tree, Env<GenContext> env, boolean fatcode) { |
|
960 |
MethodSymbol meth = tree.sym; |
|
961 |
||
962 |
// Create a new code structure. |
|
963 |
meth.code = code = new Code(meth, |
|
964 |
fatcode, |
|
965 |
lineDebugInfo ? toplevel.lineMap : null, |
|
966 |
varDebugInfo, |
|
967 |
stackMap, |
|
968 |
debugCode, |
|
969 |
genCrt ? new CRTable(tree, env.toplevel.endPositions) |
|
970 |
: null, |
|
971 |
syms, |
|
972 |
types, |
|
28330
d4bcdcac1211
8064857: javac generates LVT entry with length 0 for local variable
vromero
parents:
27230
diff
changeset
|
973 |
pool); |
10 | 974 |
items = new Items(pool, code, syms, types); |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
975 |
if (code.debugCode) { |
10 | 976 |
System.err.println(meth + " for body " + tree); |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
977 |
} |
10 | 978 |
|
979 |
// If method is not static, create a new local variable address |
|
980 |
// for `this'. |
|
981 |
if ((tree.mods.flags & STATIC) == 0) { |
|
982 |
Type selfType = meth.owner.type; |
|
983 |
if (meth.isConstructor() && selfType != syms.objectType) |
|
984 |
selfType = UninitializedType.uninitializedThis(selfType); |
|
985 |
code.setDefined( |
|
986 |
code.newLocal( |
|
987 |
new VarSymbol(FINAL, names._this, selfType, meth.owner))); |
|
988 |
} |
|
989 |
||
990 |
// Mark all parameters as defined from the beginning of |
|
991 |
// the method. |
|
992 |
for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) { |
|
993 |
checkDimension(l.head.pos(), l.head.sym.type); |
|
994 |
code.setDefined(code.newLocal(l.head.sym)); |
|
995 |
} |
|
996 |
||
997 |
// Get ready to generate code for method body. |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
998 |
int startpcCrt = genCrt ? code.curCP() : 0; |
10 | 999 |
code.entryPoint(); |
1000 |
||
1001 |
// Suppress initial stackmap |
|
1002 |
code.pendingStackMap = false; |
|
1003 |
||
1004 |
return startpcCrt; |
|
1005 |
} |
|
1006 |
||
1007 |
public void visitVarDef(JCVariableDecl tree) { |
|
1008 |
VarSymbol v = tree.sym; |
|
1009 |
code.newLocal(v); |
|
1010 |
if (tree.init != null) { |
|
1011 |
checkStringConstant(tree.init.pos(), v.getConstValue()); |
|
1012 |
if (v.getConstValue() == null || varDebugInfo) { |
|
36269
5b3afff0e70c
8151016: Migrate asserts introduced in Valhalla code generation to JDK9 dev
sadayapalam
parents:
36153
diff
changeset
|
1013 |
Assert.check(letExprDepth != 0 || code.state.stacksize == 0); |
10 | 1014 |
genExpr(tree.init, v.erasure(types)).load(); |
1015 |
items.makeLocalItem(v).store(); |
|
36269
5b3afff0e70c
8151016: Migrate asserts introduced in Valhalla code generation to JDK9 dev
sadayapalam
parents:
36153
diff
changeset
|
1016 |
Assert.check(letExprDepth != 0 || code.state.stacksize == 0); |
10 | 1017 |
} |
1018 |
} |
|
1019 |
checkDimension(tree.pos(), v.type); |
|
1020 |
} |
|
1021 |
||
1022 |
public void visitSkip(JCSkip tree) { |
|
1023 |
} |
|
1024 |
||
1025 |
public void visitBlock(JCBlock tree) { |
|
1026 |
int limit = code.nextreg; |
|
1027 |
Env<GenContext> localEnv = env.dup(tree, new GenContext()); |
|
1028 |
genStats(tree.stats, localEnv); |
|
1029 |
// End the scope of all block-local variables in variable info. |
|
10950 | 1030 |
if (!env.tree.hasTag(METHODDEF)) { |
10 | 1031 |
code.statBegin(tree.endpos); |
1032 |
code.endScopes(limit); |
|
1033 |
code.pendingStatPos = Position.NOPOS; |
|
1034 |
} |
|
1035 |
} |
|
1036 |
||
1037 |
public void visitDoLoop(JCDoWhileLoop tree) { |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
41029
diff
changeset
|
1038 |
genLoop(tree, tree.body, tree.cond, List.nil(), false); |
10 | 1039 |
} |
1040 |
||
1041 |
public void visitWhileLoop(JCWhileLoop tree) { |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
41029
diff
changeset
|
1042 |
genLoop(tree, tree.body, tree.cond, List.nil(), true); |
10 | 1043 |
} |
1044 |
||
1045 |
public void visitForLoop(JCForLoop tree) { |
|
1046 |
int limit = code.nextreg; |
|
1047 |
genStats(tree.init, env); |
|
1048 |
genLoop(tree, tree.body, tree.cond, tree.step, true); |
|
1049 |
code.endScopes(limit); |
|
1050 |
} |
|
1051 |
//where |
|
1052 |
/** Generate code for a loop. |
|
1053 |
* @param loop The tree representing the loop. |
|
1054 |
* @param body The loop's body. |
|
1055 |
* @param cond The loop's controling condition. |
|
1056 |
* @param step "Step" statements to be inserted at end of |
|
1057 |
* each iteration. |
|
1058 |
* @param testFirst True if the loop test belongs before the body. |
|
1059 |
*/ |
|
1060 |
private void genLoop(JCStatement loop, |
|
1061 |
JCStatement body, |
|
1062 |
JCExpression cond, |
|
1063 |
List<JCExpressionStatement> step, |
|
1064 |
boolean testFirst) { |
|
1065 |
Env<GenContext> loopEnv = env.dup(loop, new GenContext()); |
|
1066 |
int startpc = code.entryPoint(); |
|
25607 | 1067 |
if (testFirst) { //while or for loop |
10 | 1068 |
CondItem c; |
1069 |
if (cond != null) { |
|
1070 |
code.statBegin(cond.pos); |
|
36269
5b3afff0e70c
8151016: Migrate asserts introduced in Valhalla code generation to JDK9 dev
sadayapalam
parents:
36153
diff
changeset
|
1071 |
Assert.check(code.state.stacksize == 0); |
10 | 1072 |
c = genCond(TreeInfo.skipParens(cond), CRT_FLOW_CONTROLLER); |
1073 |
} else { |
|
1074 |
c = items.makeCondItem(goto_); |
|
1075 |
} |
|
1076 |
Chain loopDone = c.jumpFalse(); |
|
1077 |
code.resolve(c.trueJumps); |
|
36269
5b3afff0e70c
8151016: Migrate asserts introduced in Valhalla code generation to JDK9 dev
sadayapalam
parents:
36153
diff
changeset
|
1078 |
Assert.check(code.state.stacksize == 0); |
10 | 1079 |
genStat(body, loopEnv, CRT_STATEMENT | CRT_FLOW_TARGET); |
1080 |
code.resolve(loopEnv.info.cont); |
|
1081 |
genStats(step, loopEnv); |
|
1082 |
code.resolve(code.branch(goto_), startpc); |
|
1083 |
code.resolve(loopDone); |
|
1084 |
} else { |
|
1085 |
genStat(body, loopEnv, CRT_STATEMENT | CRT_FLOW_TARGET); |
|
1086 |
code.resolve(loopEnv.info.cont); |
|
1087 |
genStats(step, loopEnv); |
|
37753
dbc87c966861
8155028: javac crashes in silly do-while loop
sadayapalam
parents:
37635
diff
changeset
|
1088 |
if (code.isAlive()) { |
dbc87c966861
8155028: javac crashes in silly do-while loop
sadayapalam
parents:
37635
diff
changeset
|
1089 |
CondItem c; |
dbc87c966861
8155028: javac crashes in silly do-while loop
sadayapalam
parents:
37635
diff
changeset
|
1090 |
if (cond != null) { |
dbc87c966861
8155028: javac crashes in silly do-while loop
sadayapalam
parents:
37635
diff
changeset
|
1091 |
code.statBegin(cond.pos); |
dbc87c966861
8155028: javac crashes in silly do-while loop
sadayapalam
parents:
37635
diff
changeset
|
1092 |
Assert.check(code.state.stacksize == 0); |
dbc87c966861
8155028: javac crashes in silly do-while loop
sadayapalam
parents:
37635
diff
changeset
|
1093 |
c = genCond(TreeInfo.skipParens(cond), CRT_FLOW_CONTROLLER); |
dbc87c966861
8155028: javac crashes in silly do-while loop
sadayapalam
parents:
37635
diff
changeset
|
1094 |
} else { |
dbc87c966861
8155028: javac crashes in silly do-while loop
sadayapalam
parents:
37635
diff
changeset
|
1095 |
c = items.makeCondItem(goto_); |
dbc87c966861
8155028: javac crashes in silly do-while loop
sadayapalam
parents:
37635
diff
changeset
|
1096 |
} |
dbc87c966861
8155028: javac crashes in silly do-while loop
sadayapalam
parents:
37635
diff
changeset
|
1097 |
code.resolve(c.jumpTrue(), startpc); |
36269
5b3afff0e70c
8151016: Migrate asserts introduced in Valhalla code generation to JDK9 dev
sadayapalam
parents:
36153
diff
changeset
|
1098 |
Assert.check(code.state.stacksize == 0); |
37753
dbc87c966861
8155028: javac crashes in silly do-while loop
sadayapalam
parents:
37635
diff
changeset
|
1099 |
code.resolve(c.falseJumps); |
10 | 1100 |
} |
1101 |
} |
|
28338
53f6dcec62d1
8067429: java.lang.VerifyError: Inconsistent stackmap frames at branch target
mcimadamore
parents:
28330
diff
changeset
|
1102 |
Chain exit = loopEnv.info.exit; |
53f6dcec62d1
8067429: java.lang.VerifyError: Inconsistent stackmap frames at branch target
mcimadamore
parents:
28330
diff
changeset
|
1103 |
if (exit != null) { |
53f6dcec62d1
8067429: java.lang.VerifyError: Inconsistent stackmap frames at branch target
mcimadamore
parents:
28330
diff
changeset
|
1104 |
code.resolve(exit); |
53f6dcec62d1
8067429: java.lang.VerifyError: Inconsistent stackmap frames at branch target
mcimadamore
parents:
28330
diff
changeset
|
1105 |
exit.state.defined.excludeFrom(code.nextreg); |
25607 | 1106 |
} |
10 | 1107 |
} |
1108 |
||
1109 |
public void visitForeachLoop(JCEnhancedForLoop tree) { |
|
1110 |
throw new AssertionError(); // should have been removed by Lower. |
|
1111 |
} |
|
1112 |
||
1113 |
public void visitLabelled(JCLabeledStatement tree) { |
|
1114 |
Env<GenContext> localEnv = env.dup(tree, new GenContext()); |
|
1115 |
genStat(tree.body, localEnv, CRT_STATEMENT); |
|
28338
53f6dcec62d1
8067429: java.lang.VerifyError: Inconsistent stackmap frames at branch target
mcimadamore
parents:
28330
diff
changeset
|
1116 |
Chain exit = localEnv.info.exit; |
53f6dcec62d1
8067429: java.lang.VerifyError: Inconsistent stackmap frames at branch target
mcimadamore
parents:
28330
diff
changeset
|
1117 |
if (exit != null) { |
53f6dcec62d1
8067429: java.lang.VerifyError: Inconsistent stackmap frames at branch target
mcimadamore
parents:
28330
diff
changeset
|
1118 |
code.resolve(exit); |
53f6dcec62d1
8067429: java.lang.VerifyError: Inconsistent stackmap frames at branch target
mcimadamore
parents:
28330
diff
changeset
|
1119 |
exit.state.defined.excludeFrom(code.nextreg); |
53f6dcec62d1
8067429: java.lang.VerifyError: Inconsistent stackmap frames at branch target
mcimadamore
parents:
28330
diff
changeset
|
1120 |
} |
10 | 1121 |
} |
1122 |
||
1123 |
public void visitSwitch(JCSwitch tree) { |
|
1124 |
int limit = code.nextreg; |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
1125 |
Assert.check(!tree.selector.type.hasTag(CLASS)); |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
1126 |
int startpcCrt = genCrt ? code.curCP() : 0; |
36269
5b3afff0e70c
8151016: Migrate asserts introduced in Valhalla code generation to JDK9 dev
sadayapalam
parents:
36153
diff
changeset
|
1127 |
Assert.check(code.state.stacksize == 0); |
10 | 1128 |
Item sel = genExpr(tree.selector, syms.intType); |
1129 |
List<JCCase> cases = tree.cases; |
|
1130 |
if (cases.isEmpty()) { |
|
1131 |
// We are seeing: switch <sel> {} |
|
1132 |
sel.load().drop(); |
|
1133 |
if (genCrt) |
|
1134 |
code.crt.put(TreeInfo.skipParens(tree.selector), |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
1135 |
CRT_FLOW_CONTROLLER, startpcCrt, code.curCP()); |
10 | 1136 |
} else { |
1137 |
// We are seeing a nonempty switch. |
|
1138 |
sel.load(); |
|
1139 |
if (genCrt) |
|
1140 |
code.crt.put(TreeInfo.skipParens(tree.selector), |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
1141 |
CRT_FLOW_CONTROLLER, startpcCrt, code.curCP()); |
10 | 1142 |
Env<GenContext> switchEnv = env.dup(tree, new GenContext()); |
1143 |
switchEnv.info.isSwitch = true; |
|
1144 |
||
1145 |
// Compute number of labels and minimum and maximum label values. |
|
1146 |
// For each case, store its label in an array. |
|
1147 |
int lo = Integer.MAX_VALUE; // minimum label. |
|
1148 |
int hi = Integer.MIN_VALUE; // maximum label. |
|
1149 |
int nlabels = 0; // number of labels. |
|
1150 |
||
1151 |
int[] labels = new int[cases.length()]; // the label array. |
|
1152 |
int defaultIndex = -1; // the index of the default clause. |
|
1153 |
||
1154 |
List<JCCase> l = cases; |
|
1155 |
for (int i = 0; i < labels.length; i++) { |
|
1156 |
if (l.head.pat != null) { |
|
1157 |
int val = ((Number)l.head.pat.type.constValue()).intValue(); |
|
1158 |
labels[i] = val; |
|
1159 |
if (val < lo) lo = val; |
|
1160 |
if (hi < val) hi = val; |
|
1161 |
nlabels++; |
|
1162 |
} else { |
|
8032 | 1163 |
Assert.check(defaultIndex == -1); |
10 | 1164 |
defaultIndex = i; |
1165 |
} |
|
1166 |
l = l.tail; |
|
1167 |
} |
|
1168 |
||
1169 |
// Determine whether to issue a tableswitch or a lookupswitch |
|
27123 | 1170 |
// instruction. |
1171 |
long table_space_cost = 4 + ((long) hi - lo + 1); // words |
|
1172 |
long table_time_cost = 3; // comparisons |
|
1173 |
long lookup_space_cost = 3 + 2 * (long) nlabels; |
|
1174 |
long lookup_time_cost = nlabels; |
|
1175 |
int opcode = |
|
1176 |
nlabels > 0 && |
|
1177 |
table_space_cost + 3 * table_time_cost <= |
|
1178 |
lookup_space_cost + 3 * lookup_time_cost |
|
1179 |
? |
|
1180 |
tableswitch : lookupswitch; |
|
10 | 1181 |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
1182 |
int startpc = code.curCP(); // the position of the selector operation |
10 | 1183 |
code.emitop0(opcode); |
1184 |
code.align(4); |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
1185 |
int tableBase = code.curCP(); // the start of the jump table |
10 | 1186 |
int[] offsets = null; // a table of offsets for a lookupswitch |
1187 |
code.emit4(-1); // leave space for default offset |
|
1188 |
if (opcode == tableswitch) { |
|
1189 |
code.emit4(lo); // minimum label |
|
1190 |
code.emit4(hi); // maximum label |
|
1191 |
for (long i = lo; i <= hi; i++) { // leave space for jump table |
|
1192 |
code.emit4(-1); |
|
1193 |
} |
|
1194 |
} else { |
|
1195 |
code.emit4(nlabels); // number of labels |
|
1196 |
for (int i = 0; i < nlabels; i++) { |
|
1197 |
code.emit4(-1); code.emit4(-1); // leave space for lookup table |
|
1198 |
} |
|
1199 |
offsets = new int[labels.length]; |
|
1200 |
} |
|
1201 |
Code.State stateSwitch = code.state.dup(); |
|
1202 |
code.markDead(); |
|
1203 |
||
1204 |
// For each case do: |
|
1205 |
l = cases; |
|
1206 |
for (int i = 0; i < labels.length; i++) { |
|
1207 |
JCCase c = l.head; |
|
1208 |
l = l.tail; |
|
1209 |
||
1210 |
int pc = code.entryPoint(stateSwitch); |
|
1211 |
// Insert offset directly into code or else into the |
|
1212 |
// offsets table. |
|
1213 |
if (i != defaultIndex) { |
|
1214 |
if (opcode == tableswitch) { |
|
1215 |
code.put4( |
|
1216 |
tableBase + 4 * (labels[i] - lo + 3), |
|
1217 |
pc - startpc); |
|
1218 |
} else { |
|
1219 |
offsets[i] = pc - startpc; |
|
1220 |
} |
|
1221 |
} else { |
|
1222 |
code.put4(tableBase, pc - startpc); |
|
1223 |
} |
|
1224 |
||
1225 |
// Generate code for the statements in this case. |
|
1226 |
genStats(c.stats, switchEnv, CRT_FLOW_TARGET); |
|
1227 |
} |
|
1228 |
||
1229 |
// Resolve all breaks. |
|
28338
53f6dcec62d1
8067429: java.lang.VerifyError: Inconsistent stackmap frames at branch target
mcimadamore
parents:
28330
diff
changeset
|
1230 |
Chain exit = switchEnv.info.exit; |
53f6dcec62d1
8067429: java.lang.VerifyError: Inconsistent stackmap frames at branch target
mcimadamore
parents:
28330
diff
changeset
|
1231 |
if (exit != null) { |
53f6dcec62d1
8067429: java.lang.VerifyError: Inconsistent stackmap frames at branch target
mcimadamore
parents:
28330
diff
changeset
|
1232 |
code.resolve(exit); |
41029
869e95ade880
8160699: java.lang.VerifyError: Inconsistent stackmap frames at branch target
sadayapalam
parents:
39920
diff
changeset
|
1233 |
exit.state.defined.excludeFrom(limit); |
28338
53f6dcec62d1
8067429: java.lang.VerifyError: Inconsistent stackmap frames at branch target
mcimadamore
parents:
28330
diff
changeset
|
1234 |
} |
10 | 1235 |
|
1236 |
// If we have not set the default offset, we do so now. |
|
1237 |
if (code.get4(tableBase) == -1) { |
|
1238 |
code.put4(tableBase, code.entryPoint(stateSwitch) - startpc); |
|
1239 |
} |
|
1240 |
||
1241 |
if (opcode == tableswitch) { |
|
1242 |
// Let any unfilled slots point to the default case. |
|
1243 |
int defaultOffset = code.get4(tableBase); |
|
1244 |
for (long i = lo; i <= hi; i++) { |
|
1245 |
int t = (int)(tableBase + 4 * (i - lo + 3)); |
|
1246 |
if (code.get4(t) == -1) |
|
1247 |
code.put4(t, defaultOffset); |
|
1248 |
} |
|
1249 |
} else { |
|
1250 |
// Sort non-default offsets and copy into lookup table. |
|
1251 |
if (defaultIndex >= 0) |
|
1252 |
for (int i = defaultIndex; i < labels.length - 1; i++) { |
|
1253 |
labels[i] = labels[i+1]; |
|
1254 |
offsets[i] = offsets[i+1]; |
|
1255 |
} |
|
1256 |
if (nlabels > 0) |
|
1257 |
qsort2(labels, offsets, 0, nlabels - 1); |
|
1258 |
for (int i = 0; i < nlabels; i++) { |
|
1259 |
int caseidx = tableBase + 8 * (i + 1); |
|
1260 |
code.put4(caseidx, labels[i]); |
|
1261 |
code.put4(caseidx + 4, offsets[i]); |
|
1262 |
} |
|
1263 |
} |
|
1264 |
} |
|
1265 |
code.endScopes(limit); |
|
1266 |
} |
|
1267 |
//where |
|
1268 |
/** Sort (int) arrays of keys and values |
|
1269 |
*/ |
|
1270 |
static void qsort2(int[] keys, int[] values, int lo, int hi) { |
|
1271 |
int i = lo; |
|
1272 |
int j = hi; |
|
1273 |
int pivot = keys[(i+j)/2]; |
|
1274 |
do { |
|
1275 |
while (keys[i] < pivot) i++; |
|
1276 |
while (pivot < keys[j]) j--; |
|
1277 |
if (i <= j) { |
|
1278 |
int temp1 = keys[i]; |
|
1279 |
keys[i] = keys[j]; |
|
1280 |
keys[j] = temp1; |
|
1281 |
int temp2 = values[i]; |
|
1282 |
values[i] = values[j]; |
|
1283 |
values[j] = temp2; |
|
1284 |
i++; |
|
1285 |
j--; |
|
1286 |
} |
|
1287 |
} while (i <= j); |
|
1288 |
if (lo < j) qsort2(keys, values, lo, j); |
|
1289 |
if (i < hi) qsort2(keys, values, i, hi); |
|
1290 |
} |
|
1291 |
||
1292 |
public void visitSynchronized(JCSynchronized tree) { |
|
1293 |
int limit = code.nextreg; |
|
1294 |
// Generate code to evaluate lock and save in temporary variable. |
|
1295 |
final LocalItem lockVar = makeTemp(syms.objectType); |
|
36269
5b3afff0e70c
8151016: Migrate asserts introduced in Valhalla code generation to JDK9 dev
sadayapalam
parents:
36153
diff
changeset
|
1296 |
Assert.check(code.state.stacksize == 0); |
10 | 1297 |
genExpr(tree.lock, tree.lock.type).load().duplicate(); |
1298 |
lockVar.store(); |
|
1299 |
||
1300 |
// Generate code to enter monitor. |
|
1301 |
code.emitop0(monitorenter); |
|
1302 |
code.state.lock(lockVar.reg); |
|
1303 |
||
1304 |
// Generate code for a try statement with given body, no catch clauses |
|
1305 |
// in a new environment with the "exit-monitor" operation as finalizer. |
|
1306 |
final Env<GenContext> syncEnv = env.dup(tree, new GenContext()); |
|
1307 |
syncEnv.info.finalize = new GenFinalizer() { |
|
1308 |
void gen() { |
|
1309 |
genLast(); |
|
8032 | 1310 |
Assert.check(syncEnv.info.gaps.length() % 2 == 0); |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
1311 |
syncEnv.info.gaps.append(code.curCP()); |
10 | 1312 |
} |
1313 |
void genLast() { |
|
1314 |
if (code.isAlive()) { |
|
1315 |
lockVar.load(); |
|
1316 |
code.emitop0(monitorexit); |
|
1317 |
code.state.unlock(lockVar.reg); |
|
1318 |
} |
|
1319 |
} |
|
1320 |
}; |
|
22163 | 1321 |
syncEnv.info.gaps = new ListBuffer<>(); |
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
41029
diff
changeset
|
1322 |
genTry(tree.body, List.nil(), syncEnv); |
10 | 1323 |
code.endScopes(limit); |
1324 |
} |
|
1325 |
||
1326 |
public void visitTry(final JCTry tree) { |
|
1327 |
// Generate code for a try statement with given body and catch clauses, |
|
1328 |
// in a new environment which calls the finally block if there is one. |
|
1329 |
final Env<GenContext> tryEnv = env.dup(tree, new GenContext()); |
|
1330 |
final Env<GenContext> oldEnv = env; |
|
1331 |
tryEnv.info.finalize = new GenFinalizer() { |
|
1332 |
void gen() { |
|
36153 | 1333 |
Assert.check(tryEnv.info.gaps.length() % 2 == 0); |
1334 |
tryEnv.info.gaps.append(code.curCP()); |
|
1335 |
genLast(); |
|
10 | 1336 |
} |
1337 |
void genLast() { |
|
1338 |
if (tree.finalizer != null) |
|
1339 |
genStat(tree.finalizer, oldEnv, CRT_BLOCK); |
|
1340 |
} |
|
1341 |
boolean hasFinalizer() { |
|
1342 |
return tree.finalizer != null; |
|
1343 |
} |
|
1344 |
}; |
|
22163 | 1345 |
tryEnv.info.gaps = new ListBuffer<>(); |
10 | 1346 |
genTry(tree.body, tree.catchers, tryEnv); |
1347 |
} |
|
1348 |
//where |
|
1349 |
/** Generate code for a try or synchronized statement |
|
1350 |
* @param body The body of the try or synchronized statement. |
|
1351 |
* @param catchers The lis of catch clauses. |
|
1352 |
* @param env the environment current for the body. |
|
1353 |
*/ |
|
1354 |
void genTry(JCTree body, List<JCCatch> catchers, Env<GenContext> env) { |
|
1355 |
int limit = code.nextreg; |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
1356 |
int startpc = code.curCP(); |
10 | 1357 |
Code.State stateTry = code.state.dup(); |
1358 |
genStat(body, env, CRT_BLOCK); |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
1359 |
int endpc = code.curCP(); |
10 | 1360 |
boolean hasFinalizer = |
1361 |
env.info.finalize != null && |
|
1362 |
env.info.finalize.hasFinalizer(); |
|
1363 |
List<Integer> gaps = env.info.gaps.toList(); |
|
1364 |
code.statBegin(TreeInfo.endPos(body)); |
|
1365 |
genFinalizer(env); |
|
1366 |
code.statBegin(TreeInfo.endPos(env.tree)); |
|
19923
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1367 |
Chain exitChain = code.branch(goto_); |
10 | 1368 |
endFinalizerGap(env); |
19923
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1369 |
if (startpc != endpc) for (List<JCCatch> l = catchers; l.nonEmpty(); l = l.tail) { |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1370 |
// start off with exception on stack |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1371 |
code.entryPoint(stateTry, l.head.param.sym.type); |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1372 |
genCatch(l.head, env, startpc, endpc, gaps); |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1373 |
genFinalizer(env); |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1374 |
if (hasFinalizer || l.tail.nonEmpty()) { |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1375 |
code.statBegin(TreeInfo.endPos(env.tree)); |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1376 |
exitChain = Code.mergeChains(exitChain, |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1377 |
code.branch(goto_)); |
10 | 1378 |
} |
19923
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1379 |
endFinalizerGap(env); |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1380 |
} |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1381 |
if (hasFinalizer) { |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1382 |
// Create a new register segement to avoid allocating |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1383 |
// the same variables in finalizers and other statements. |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1384 |
code.newRegSegment(); |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1385 |
|
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1386 |
// Add a catch-all clause. |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1387 |
|
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1388 |
// start off with exception on stack |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1389 |
int catchallpc = code.entryPoint(stateTry, syms.throwableType); |
10 | 1390 |
|
19923
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1391 |
// Register all exception ranges for catch all clause. |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1392 |
// The range of the catch all clause is from the beginning |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1393 |
// of the try or synchronized block until the present |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1394 |
// code pointer excluding all gaps in the current |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1395 |
// environment's GenContext. |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1396 |
int startseg = startpc; |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1397 |
while (env.info.gaps.nonEmpty()) { |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1398 |
int endseg = env.info.gaps.next().intValue(); |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1399 |
registerCatch(body.pos(), startseg, endseg, |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1400 |
catchallpc, 0); |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1401 |
startseg = env.info.gaps.next().intValue(); |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1402 |
} |
45220 | 1403 |
code.statBegin(TreeInfo.finalizerPos(env.tree, PosKind.FIRST_STAT_POS)); |
19923
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1404 |
code.markStatBegin(); |
10 | 1405 |
|
19923
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1406 |
Item excVar = makeTemp(syms.throwableType); |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1407 |
excVar.store(); |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1408 |
genFinalizer(env); |
45220 | 1409 |
code.resolvePending(); |
1410 |
code.statBegin(TreeInfo.finalizerPos(env.tree, PosKind.END_POS)); |
|
1411 |
code.markStatBegin(); |
|
1412 |
||
19923
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1413 |
excVar.load(); |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1414 |
registerCatch(body.pos(), startseg, |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1415 |
env.info.gaps.next().intValue(), |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1416 |
catchallpc, 0); |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1417 |
code.emitop0(athrow); |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1418 |
code.markDead(); |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1419 |
|
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1420 |
// If there are jsr's to this finalizer, ... |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1421 |
if (env.info.cont != null) { |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1422 |
// Resolve all jsr's. |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1423 |
code.resolve(env.info.cont); |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1424 |
|
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1425 |
// Mark statement line number |
45220 | 1426 |
code.statBegin(TreeInfo.finalizerPos(env.tree, PosKind.FIRST_STAT_POS)); |
10 | 1427 |
code.markStatBegin(); |
1428 |
||
19923
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1429 |
// Save return address. |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1430 |
LocalItem retVar = makeTemp(syms.throwableType); |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1431 |
retVar.store(); |
10 | 1432 |
|
19923
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1433 |
// Generate finalizer code. |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1434 |
env.info.finalize.genLast(); |
10 | 1435 |
|
19923
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1436 |
// Return. |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1437 |
code.emitop1w(ret, retVar.reg); |
4895f15b3845
8024039: javac, previous solution for JDK-8022186 was incorrect
vromero
parents:
19247
diff
changeset
|
1438 |
code.markDead(); |
10 | 1439 |
} |
1440 |
} |
|
1441 |
// Resolve all breaks. |
|
1442 |
code.resolve(exitChain); |
|
1443 |
||
1444 |
code.endScopes(limit); |
|
1445 |
} |
|
1446 |
||
1447 |
/** Generate code for a catch clause. |
|
1448 |
* @param tree The catch clause. |
|
1449 |
* @param env The environment current in the enclosing try. |
|
1450 |
* @param startpc Start pc of try-block. |
|
1451 |
* @param endpc End pc of try-block. |
|
1452 |
*/ |
|
1453 |
void genCatch(JCCatch tree, |
|
1454 |
Env<GenContext> env, |
|
1455 |
int startpc, int endpc, |
|
1456 |
List<Integer> gaps) { |
|
1457 |
if (startpc != endpc) { |
|
29842
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1458 |
List<Pair<List<Attribute.TypeCompound>, JCExpression>> catchTypeExprs |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1459 |
= catchTypesWithAnnotations(tree); |
6354
f50c012cd1f0
6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents:
5857
diff
changeset
|
1460 |
while (gaps.nonEmpty()) { |
29842
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1461 |
for (Pair<List<Attribute.TypeCompound>, JCExpression> subCatch1 : catchTypeExprs) { |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1462 |
JCExpression subCatch = subCatch1.snd; |
6354
f50c012cd1f0
6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents:
5857
diff
changeset
|
1463 |
int catchType = makeRef(tree.pos(), subCatch.type); |
f50c012cd1f0
6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents:
5857
diff
changeset
|
1464 |
int end = gaps.head.intValue(); |
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5004
diff
changeset
|
1465 |
registerCatch(tree.pos(), |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
1466 |
startpc, end, code.curCP(), |
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5004
diff
changeset
|
1467 |
catchType); |
29842
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1468 |
for (Attribute.TypeCompound tc : subCatch1.fst) { |
24396 | 1469 |
tc.position.setCatchInfo(catchType, startpc); |
17578 | 1470 |
} |
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5004
diff
changeset
|
1471 |
} |
6354
f50c012cd1f0
6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents:
5857
diff
changeset
|
1472 |
gaps = gaps.tail; |
f50c012cd1f0
6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents:
5857
diff
changeset
|
1473 |
startpc = gaps.head.intValue(); |
f50c012cd1f0
6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents:
5857
diff
changeset
|
1474 |
gaps = gaps.tail; |
f50c012cd1f0
6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents:
5857
diff
changeset
|
1475 |
} |
f50c012cd1f0
6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents:
5857
diff
changeset
|
1476 |
if (startpc < endpc) { |
29842
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1477 |
for (Pair<List<Attribute.TypeCompound>, JCExpression> subCatch1 : catchTypeExprs) { |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1478 |
JCExpression subCatch = subCatch1.snd; |
6354
f50c012cd1f0
6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents:
5857
diff
changeset
|
1479 |
int catchType = makeRef(tree.pos(), subCatch.type); |
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5004
diff
changeset
|
1480 |
registerCatch(tree.pos(), |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
1481 |
startpc, endpc, code.curCP(), |
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
5004
diff
changeset
|
1482 |
catchType); |
29842
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1483 |
for (Attribute.TypeCompound tc : subCatch1.fst) { |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1484 |
tc.position.setCatchInfo(catchType, startpc); |
17578 | 1485 |
} |
6354
f50c012cd1f0
6978574: return statement in try block with multi-catch causes ClassFormatError
mcimadamore
parents:
5857
diff
changeset
|
1486 |
} |
10 | 1487 |
} |
1488 |
VarSymbol exparam = tree.param.sym; |
|
1489 |
code.statBegin(tree.pos); |
|
1490 |
code.markStatBegin(); |
|
1491 |
int limit = code.nextreg; |
|
29842
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1492 |
code.newLocal(exparam); |
10 | 1493 |
items.makeLocalItem(exparam).store(); |
1494 |
code.statBegin(TreeInfo.firstStatPos(tree.body)); |
|
1495 |
genStat(tree.body, env, CRT_BLOCK); |
|
1496 |
code.endScopes(limit); |
|
1497 |
code.statBegin(TreeInfo.endPos(tree.body)); |
|
1498 |
} |
|
1499 |
} |
|
29842
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1500 |
// where |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1501 |
List<Pair<List<Attribute.TypeCompound>, JCExpression>> catchTypesWithAnnotations(JCCatch tree) { |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1502 |
return TreeInfo.isMultiCatch(tree) ? |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1503 |
catchTypesWithAnnotationsFromMulticatch((JCTypeUnion)tree.param.vartype, tree.param.sym.getRawTypeAttributes()) : |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1504 |
List.of(new Pair<>(tree.param.sym.getRawTypeAttributes(), tree.param.vartype)); |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1505 |
} |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1506 |
// where |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1507 |
List<Pair<List<Attribute.TypeCompound>, JCExpression>> catchTypesWithAnnotationsFromMulticatch(JCTypeUnion tree, List<TypeCompound> first) { |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1508 |
List<JCExpression> alts = tree.alternatives; |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1509 |
List<Pair<List<TypeCompound>, JCExpression>> res = List.of(new Pair<>(first, alts.head)); |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1510 |
alts = alts.tail; |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1511 |
|
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1512 |
while(alts != null && alts.head != null) { |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1513 |
JCExpression alt = alts.head; |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1514 |
if (alt instanceof JCAnnotatedType) { |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1515 |
JCAnnotatedType a = (JCAnnotatedType)alt; |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1516 |
res = res.prepend(new Pair<>(annotate.fromAnnotations(a.annotations), alt)); |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1517 |
} else { |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1518 |
res = res.prepend(new Pair<>(List.nil(), alt)); |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1519 |
} |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1520 |
alts = alts.tail; |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1521 |
} |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1522 |
return res.reverse(); |
826ac2519523
8031744: Annotations on many Language Model elements are not returned
jjg
parents:
29777
diff
changeset
|
1523 |
} |
10 | 1524 |
|
1525 |
/** Register a catch clause in the "Exceptions" code-attribute. |
|
1526 |
*/ |
|
1527 |
void registerCatch(DiagnosticPosition pos, |
|
1528 |
int startpc, int endpc, |
|
1529 |
int handler_pc, int catch_type) { |
|
10811
4d4ed480210e
7093325: Redundant entry in bytecode exception table
mcimadamore
parents:
10457
diff
changeset
|
1530 |
char startpc1 = (char)startpc; |
4d4ed480210e
7093325: Redundant entry in bytecode exception table
mcimadamore
parents:
10457
diff
changeset
|
1531 |
char endpc1 = (char)endpc; |
4d4ed480210e
7093325: Redundant entry in bytecode exception table
mcimadamore
parents:
10457
diff
changeset
|
1532 |
char handler_pc1 = (char)handler_pc; |
4d4ed480210e
7093325: Redundant entry in bytecode exception table
mcimadamore
parents:
10457
diff
changeset
|
1533 |
if (startpc1 == startpc && |
4d4ed480210e
7093325: Redundant entry in bytecode exception table
mcimadamore
parents:
10457
diff
changeset
|
1534 |
endpc1 == endpc && |
4d4ed480210e
7093325: Redundant entry in bytecode exception table
mcimadamore
parents:
10457
diff
changeset
|
1535 |
handler_pc1 == handler_pc) { |
4d4ed480210e
7093325: Redundant entry in bytecode exception table
mcimadamore
parents:
10457
diff
changeset
|
1536 |
code.addCatch(startpc1, endpc1, handler_pc1, |
4d4ed480210e
7093325: Redundant entry in bytecode exception table
mcimadamore
parents:
10457
diff
changeset
|
1537 |
(char)catch_type); |
4d4ed480210e
7093325: Redundant entry in bytecode exception table
mcimadamore
parents:
10457
diff
changeset
|
1538 |
} else { |
25445
603f0c93d5c9
8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents:
25443
diff
changeset
|
1539 |
log.error(pos, "limit.code.too.large.for.try.stmt"); |
603f0c93d5c9
8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents:
25443
diff
changeset
|
1540 |
nerrs++; |
10 | 1541 |
} |
1542 |
} |
|
1543 |
||
1544 |
public void visitIf(JCIf tree) { |
|
1545 |
int limit = code.nextreg; |
|
1546 |
Chain thenExit = null; |
|
36269
5b3afff0e70c
8151016: Migrate asserts introduced in Valhalla code generation to JDK9 dev
sadayapalam
parents:
36153
diff
changeset
|
1547 |
Assert.check(code.state.stacksize == 0); |
10 | 1548 |
CondItem c = genCond(TreeInfo.skipParens(tree.cond), |
1549 |
CRT_FLOW_CONTROLLER); |
|
1550 |
Chain elseChain = c.jumpFalse(); |
|
36269
5b3afff0e70c
8151016: Migrate asserts introduced in Valhalla code generation to JDK9 dev
sadayapalam
parents:
36153
diff
changeset
|
1551 |
Assert.check(code.state.stacksize == 0); |
10 | 1552 |
if (!c.isFalse()) { |
1553 |
code.resolve(c.trueJumps); |
|
1554 |
genStat(tree.thenpart, env, CRT_STATEMENT | CRT_FLOW_TARGET); |
|
1555 |
thenExit = code.branch(goto_); |
|
1556 |
} |
|
1557 |
if (elseChain != null) { |
|
1558 |
code.resolve(elseChain); |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
1559 |
if (tree.elsepart != null) { |
10 | 1560 |
genStat(tree.elsepart, env,CRT_STATEMENT | CRT_FLOW_TARGET); |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
1561 |
} |
10 | 1562 |
} |
1563 |
code.resolve(thenExit); |
|
1564 |
code.endScopes(limit); |
|
36269
5b3afff0e70c
8151016: Migrate asserts introduced in Valhalla code generation to JDK9 dev
sadayapalam
parents:
36153
diff
changeset
|
1565 |
Assert.check(code.state.stacksize == 0); |
10 | 1566 |
} |
1567 |
||
1568 |
public void visitExec(JCExpressionStatement tree) { |
|
1569 |
// Optimize x++ to ++x and x-- to --x. |
|
1570 |
JCExpression e = tree.expr; |
|
1571 |
switch (e.getTag()) { |
|
10950 | 1572 |
case POSTINC: |
1573 |
((JCUnary) e).setTag(PREINC); |
|
10 | 1574 |
break; |
10950 | 1575 |
case POSTDEC: |
1576 |
((JCUnary) e).setTag(PREDEC); |
|
10 | 1577 |
break; |
1578 |
} |
|
36269
5b3afff0e70c
8151016: Migrate asserts introduced in Valhalla code generation to JDK9 dev
sadayapalam
parents:
36153
diff
changeset
|
1579 |
Assert.check(code.state.stacksize == 0); |
10 | 1580 |
genExpr(tree.expr, tree.expr.type).drop(); |
36269
5b3afff0e70c
8151016: Migrate asserts introduced in Valhalla code generation to JDK9 dev
sadayapalam
parents:
36153
diff
changeset
|
1581 |
Assert.check(code.state.stacksize == 0); |
10 | 1582 |
} |
1583 |
||
1584 |
public void visitBreak(JCBreak tree) { |
|
45088
b85080f2f0cc
8180141: Missing entry in LineNumberTable for break statement that jumps out of try-finally
vromero
parents:
43765
diff
changeset
|
1585 |
int tmpPos = code.pendingStatPos; |
10 | 1586 |
Env<GenContext> targetEnv = unwind(tree.target, env); |
45088
b85080f2f0cc
8180141: Missing entry in LineNumberTable for break statement that jumps out of try-finally
vromero
parents:
43765
diff
changeset
|
1587 |
code.pendingStatPos = tmpPos; |
8032 | 1588 |
Assert.check(code.state.stacksize == 0); |
10 | 1589 |
targetEnv.info.addExit(code.branch(goto_)); |
1590 |
endFinalizerGaps(env, targetEnv); |
|
1591 |
} |
|
1592 |
||
1593 |
public void visitContinue(JCContinue tree) { |
|
45088
b85080f2f0cc
8180141: Missing entry in LineNumberTable for break statement that jumps out of try-finally
vromero
parents:
43765
diff
changeset
|
1594 |
int tmpPos = code.pendingStatPos; |
10 | 1595 |
Env<GenContext> targetEnv = unwind(tree.target, env); |
45088
b85080f2f0cc
8180141: Missing entry in LineNumberTable for break statement that jumps out of try-finally
vromero
parents:
43765
diff
changeset
|
1596 |
code.pendingStatPos = tmpPos; |
8032 | 1597 |
Assert.check(code.state.stacksize == 0); |
10 | 1598 |
targetEnv.info.addCont(code.branch(goto_)); |
1599 |
endFinalizerGaps(env, targetEnv); |
|
1600 |
} |
|
1601 |
||
1602 |
public void visitReturn(JCReturn tree) { |
|
1603 |
int limit = code.nextreg; |
|
1604 |
final Env<GenContext> targetEnv; |
|
33371
b6f6281b4c07
8134759: jdb: Incorrect stepping inside finally block
aeriksso
parents:
31000
diff
changeset
|
1605 |
|
b6f6281b4c07
8134759: jdb: Incorrect stepping inside finally block
aeriksso
parents:
31000
diff
changeset
|
1606 |
/* Save and then restore the location of the return in case a finally |
b6f6281b4c07
8134759: jdb: Incorrect stepping inside finally block
aeriksso
parents:
31000
diff
changeset
|
1607 |
* is expanded (with unwind()) in the middle of our bytecodes. |
b6f6281b4c07
8134759: jdb: Incorrect stepping inside finally block
aeriksso
parents:
31000
diff
changeset
|
1608 |
*/ |
b6f6281b4c07
8134759: jdb: Incorrect stepping inside finally block
aeriksso
parents:
31000
diff
changeset
|
1609 |
int tmpPos = code.pendingStatPos; |
10 | 1610 |
if (tree.expr != null) { |
36269
5b3afff0e70c
8151016: Migrate asserts introduced in Valhalla code generation to JDK9 dev
sadayapalam
parents:
36153
diff
changeset
|
1611 |
Assert.check(code.state.stacksize == 0); |
10 | 1612 |
Item r = genExpr(tree.expr, pt).load(); |
1613 |
if (hasFinally(env.enclMethod, env)) { |
|
1614 |
r = makeTemp(pt); |
|
1615 |
r.store(); |
|
1616 |
} |
|
1617 |
targetEnv = unwind(env.enclMethod, env); |
|
33371
b6f6281b4c07
8134759: jdb: Incorrect stepping inside finally block
aeriksso
parents:
31000
diff
changeset
|
1618 |
code.pendingStatPos = tmpPos; |
10 | 1619 |
r.load(); |
1620 |
code.emitop0(ireturn + Code.truncate(Code.typecode(pt))); |
|
1621 |
} else { |
|
1622 |
targetEnv = unwind(env.enclMethod, env); |
|
18658
55e4c45e091c
7008643: inlined finally clauses confuse debuggers
vromero
parents:
18653
diff
changeset
|
1623 |
code.pendingStatPos = tmpPos; |
10 | 1624 |
code.emitop0(return_); |
1625 |
} |
|
1626 |
endFinalizerGaps(env, targetEnv); |
|
1627 |
code.endScopes(limit); |
|
1628 |
} |
|
1629 |
||
1630 |
public void visitThrow(JCThrow tree) { |
|
36269
5b3afff0e70c
8151016: Migrate asserts introduced in Valhalla code generation to JDK9 dev
sadayapalam
parents:
36153
diff
changeset
|
1631 |
Assert.check(code.state.stacksize == 0); |
10 | 1632 |
genExpr(tree.expr, tree.expr.type).load(); |
1633 |
code.emitop0(athrow); |
|
36269
5b3afff0e70c
8151016: Migrate asserts introduced in Valhalla code generation to JDK9 dev
sadayapalam
parents:
36153
diff
changeset
|
1634 |
Assert.check(code.state.stacksize == 0); |
10 | 1635 |
} |
1636 |
||
1637 |
/* ************************************************************************ |
|
1638 |
* Visitor methods for expressions |
|
1639 |
*************************************************************************/ |
|
1640 |
||
1641 |
public void visitApply(JCMethodInvocation tree) { |
|
15385 | 1642 |
setTypeAnnotationPositions(tree.pos); |
10 | 1643 |
// Generate code for method. |
1644 |
Item m = genExpr(tree.meth, methodType); |
|
1645 |
// Generate code for all arguments, where the expected types are |
|
1646 |
// the parameters of the method's external type (that is, any implicit |
|
1647 |
// outer instance of a super(...) call appears as first parameter). |
|
16807
b442b47d3ae9
8010404: Lambda debugging: redundant LineNumberTable entry for lambda capture
mcimadamore
parents:
16801
diff
changeset
|
1648 |
MethodSymbol msym = (MethodSymbol)TreeInfo.symbol(tree.meth); |
10 | 1649 |
genArgs(tree.args, |
16807
b442b47d3ae9
8010404: Lambda debugging: redundant LineNumberTable entry for lambda capture
mcimadamore
parents:
16801
diff
changeset
|
1650 |
msym.externalType(types).getParameterTypes()); |
b442b47d3ae9
8010404: Lambda debugging: redundant LineNumberTable entry for lambda capture
mcimadamore
parents:
16801
diff
changeset
|
1651 |
if (!msym.isDynamic()) { |
b442b47d3ae9
8010404: Lambda debugging: redundant LineNumberTable entry for lambda capture
mcimadamore
parents:
16801
diff
changeset
|
1652 |
code.statBegin(tree.pos); |
b442b47d3ae9
8010404: Lambda debugging: redundant LineNumberTable entry for lambda capture
mcimadamore
parents:
16801
diff
changeset
|
1653 |
} |
10 | 1654 |
result = m.invoke(); |
1655 |
} |
|
1656 |
||
1657 |
public void visitConditional(JCConditional tree) { |
|
1658 |
Chain thenExit = null; |
|
37754
7b4f67ce5cb5
8050993: There is no record for condition in ternary operator in LineNumberTable.
jlahoda
parents:
37753
diff
changeset
|
1659 |
code.statBegin(tree.cond.pos); |
10 | 1660 |
CondItem c = genCond(tree.cond, CRT_FLOW_CONTROLLER); |
1661 |
Chain elseChain = c.jumpFalse(); |
|
1662 |
if (!c.isFalse()) { |
|
1663 |
code.resolve(c.trueJumps); |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
1664 |
int startpc = genCrt ? code.curCP() : 0; |
23391
3425f886b641
8034091: There is no records in LineNumberTable attribute for ternary operator ?: splitted to several lines.
pgovereau
parents:
22697
diff
changeset
|
1665 |
code.statBegin(tree.truepart.pos); |
10 | 1666 |
genExpr(tree.truepart, pt).load(); |
1667 |
code.state.forceStackTop(tree.type); |
|
1668 |
if (genCrt) code.crt.put(tree.truepart, CRT_FLOW_TARGET, |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
1669 |
startpc, code.curCP()); |
10 | 1670 |
thenExit = code.branch(goto_); |
1671 |
} |
|
1672 |
if (elseChain != null) { |
|
1673 |
code.resolve(elseChain); |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
1674 |
int startpc = genCrt ? code.curCP() : 0; |
23391
3425f886b641
8034091: There is no records in LineNumberTable attribute for ternary operator ?: splitted to several lines.
pgovereau
parents:
22697
diff
changeset
|
1675 |
code.statBegin(tree.falsepart.pos); |
10 | 1676 |
genExpr(tree.falsepart, pt).load(); |
1677 |
code.state.forceStackTop(tree.type); |
|
1678 |
if (genCrt) code.crt.put(tree.falsepart, CRT_FLOW_TARGET, |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
1679 |
startpc, code.curCP()); |
10 | 1680 |
} |
1681 |
code.resolve(thenExit); |
|
1682 |
result = items.makeStackItem(pt); |
|
1683 |
} |
|
1684 |
||
17578 | 1685 |
private void setTypeAnnotationPositions(int treePos) { |
1686 |
MethodSymbol meth = code.meth; |
|
1687 |
boolean initOrClinit = code.meth.getKind() == javax.lang.model.element.ElementKind.CONSTRUCTOR |
|
1688 |
|| code.meth.getKind() == javax.lang.model.element.ElementKind.STATIC_INIT; |
|
15385 | 1689 |
|
17578 | 1690 |
for (Attribute.TypeCompound ta : meth.getRawTypeAttributes()) { |
26532
aa84b6606229
8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents:
25874
diff
changeset
|
1691 |
if (ta.hasUnknownPosition()) |
aa84b6606229
8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents:
25874
diff
changeset
|
1692 |
ta.tryFixPosition(); |
aa84b6606229
8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents:
25874
diff
changeset
|
1693 |
|
aa84b6606229
8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents:
25874
diff
changeset
|
1694 |
if (ta.position.matchesPos(treePos)) |
17578 | 1695 |
ta.position.updatePosOffset(code.cp); |
1696 |
} |
|
1697 |
||
1698 |
if (!initOrClinit) |
|
1699 |
return; |
|
15385 | 1700 |
|
17578 | 1701 |
for (Attribute.TypeCompound ta : meth.owner.getRawTypeAttributes()) { |
26532
aa84b6606229
8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents:
25874
diff
changeset
|
1702 |
if (ta.hasUnknownPosition()) |
aa84b6606229
8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents:
25874
diff
changeset
|
1703 |
ta.tryFixPosition(); |
aa84b6606229
8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents:
25874
diff
changeset
|
1704 |
|
aa84b6606229
8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents:
25874
diff
changeset
|
1705 |
if (ta.position.matchesPos(treePos)) |
17578 | 1706 |
ta.position.updatePosOffset(code.cp); |
1707 |
} |
|
15385 | 1708 |
|
17578 | 1709 |
ClassSymbol clazz = meth.enclClass(); |
1710 |
for (Symbol s : new com.sun.tools.javac.model.FilteredMemberList(clazz.members())) { |
|
1711 |
if (!s.getKind().isField()) |
|
1712 |
continue; |
|
1713 |
||
1714 |
for (Attribute.TypeCompound ta : s.getRawTypeAttributes()) { |
|
26532
aa84b6606229
8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents:
25874
diff
changeset
|
1715 |
if (ta.hasUnknownPosition()) |
aa84b6606229
8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents:
25874
diff
changeset
|
1716 |
ta.tryFixPosition(); |
aa84b6606229
8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents:
25874
diff
changeset
|
1717 |
|
aa84b6606229
8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents:
25874
diff
changeset
|
1718 |
if (ta.position.matchesPos(treePos)) |
17578 | 1719 |
ta.position.updatePosOffset(code.cp); |
1720 |
} |
|
1721 |
} |
|
1722 |
} |
|
15385 | 1723 |
|
10 | 1724 |
public void visitNewClass(JCNewClass tree) { |
1725 |
// Enclosing instances or anonymous classes should have been eliminated |
|
1726 |
// by now. |
|
8032 | 1727 |
Assert.check(tree.encl == null && tree.def == null); |
15385 | 1728 |
setTypeAnnotationPositions(tree.pos); |
10 | 1729 |
|
1730 |
code.emitop2(new_, makeRef(tree.pos(), tree.type)); |
|
1731 |
code.emitop0(dup); |
|
1732 |
||
1733 |
// Generate code for all arguments, where the expected types are |
|
1734 |
// the parameters of the constructor's external type (that is, |
|
1735 |
// any implicit outer instance appears as first parameter). |
|
1736 |
genArgs(tree.args, tree.constructor.externalType(types).getParameterTypes()); |
|
1737 |
||
1738 |
items.makeMemberItem(tree.constructor, true).invoke(); |
|
1739 |
result = items.makeStackItem(tree.type); |
|
1740 |
} |
|
1741 |
||
1742 |
public void visitNewArray(JCNewArray tree) { |
|
15385 | 1743 |
setTypeAnnotationPositions(tree.pos); |
3149 | 1744 |
|
10 | 1745 |
if (tree.elems != null) { |
1746 |
Type elemtype = types.elemtype(tree.type); |
|
1747 |
loadIntConst(tree.elems.length()); |
|
1748 |
Item arr = makeNewArray(tree.pos(), tree.type, 1); |
|
1749 |
int i = 0; |
|
1750 |
for (List<JCExpression> l = tree.elems; l.nonEmpty(); l = l.tail) { |
|
1751 |
arr.duplicate(); |
|
1752 |
loadIntConst(i); |
|
1753 |
i++; |
|
1754 |
genExpr(l.head, elemtype).load(); |
|
1755 |
items.makeIndexedItem(elemtype).store(); |
|
1756 |
} |
|
1757 |
result = arr; |
|
1758 |
} else { |
|
1759 |
for (List<JCExpression> l = tree.dims; l.nonEmpty(); l = l.tail) { |
|
1760 |
genExpr(l.head, syms.intType).load(); |
|
1761 |
} |
|
1762 |
result = makeNewArray(tree.pos(), tree.type, tree.dims.length()); |
|
1763 |
} |
|
1764 |
} |
|
1765 |
//where |
|
1766 |
/** Generate code to create an array with given element type and number |
|
1767 |
* of dimensions. |
|
1768 |
*/ |
|
1769 |
Item makeNewArray(DiagnosticPosition pos, Type type, int ndims) { |
|
1770 |
Type elemtype = types.elemtype(type); |
|
7630 | 1771 |
if (types.dimensions(type) > ClassFile.MAX_DIMENSIONS) { |
10 | 1772 |
log.error(pos, "limit.dimensions"); |
1773 |
nerrs++; |
|
1774 |
} |
|
1775 |
int elemcode = Code.arraycode(elemtype); |
|
1776 |
if (elemcode == 0 || (elemcode == 1 && ndims == 1)) { |
|
1777 |
code.emitAnewarray(makeRef(pos, elemtype), type); |
|
1778 |
} else if (elemcode == 1) { |
|
1779 |
code.emitMultianewarray(ndims, makeRef(pos, type), type); |
|
1780 |
} else { |
|
1781 |
code.emitNewarray(elemcode, type); |
|
1782 |
} |
|
1783 |
return items.makeStackItem(type); |
|
1784 |
} |
|
1785 |
||
1786 |
public void visitParens(JCParens tree) { |
|
1787 |
result = genExpr(tree.expr, tree.expr.type); |
|
1788 |
} |
|
1789 |
||
1790 |
public void visitAssign(JCAssign tree) { |
|
1791 |
Item l = genExpr(tree.lhs, tree.lhs.type); |
|
1792 |
genExpr(tree.rhs, tree.lhs.type).load(); |
|
29777
95a89a2efd89
7040592: Gen.java: fix code for handling 'null' literal when expected type is array
sadayapalam
parents:
29295
diff
changeset
|
1793 |
if (tree.rhs.type.hasTag(BOT)) { |
95a89a2efd89
7040592: Gen.java: fix code for handling 'null' literal when expected type is array
sadayapalam
parents:
29295
diff
changeset
|
1794 |
/* This is just a case of widening reference conversion that per 5.1.5 simply calls |
95a89a2efd89
7040592: Gen.java: fix code for handling 'null' literal when expected type is array
sadayapalam
parents:
29295
diff
changeset
|
1795 |
for "regarding a reference as having some other type in a manner that can be proved |
95a89a2efd89
7040592: Gen.java: fix code for handling 'null' literal when expected type is array
sadayapalam
parents:
29295
diff
changeset
|
1796 |
correct at compile time." |
95a89a2efd89
7040592: Gen.java: fix code for handling 'null' literal when expected type is array
sadayapalam
parents:
29295
diff
changeset
|
1797 |
*/ |
95a89a2efd89
7040592: Gen.java: fix code for handling 'null' literal when expected type is array
sadayapalam
parents:
29295
diff
changeset
|
1798 |
code.state.forceStackTop(tree.lhs.type); |
95a89a2efd89
7040592: Gen.java: fix code for handling 'null' literal when expected type is array
sadayapalam
parents:
29295
diff
changeset
|
1799 |
} |
10 | 1800 |
result = items.makeAssignItem(l); |
1801 |
} |
|
1802 |
||
1803 |
public void visitAssignop(JCAssignOp tree) { |
|
39920
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39599
diff
changeset
|
1804 |
OperatorSymbol operator = tree.operator; |
10 | 1805 |
Item l; |
1806 |
if (operator.opcode == string_add) { |
|
35424 | 1807 |
l = concat.makeConcat(tree); |
10 | 1808 |
} else { |
1809 |
// Generate code for first expression |
|
1810 |
l = genExpr(tree.lhs, tree.lhs.type); |
|
1811 |
||
1812 |
// If we have an increment of -32768 to +32767 of a local |
|
1813 |
// int variable we can use an incr instruction instead of |
|
1814 |
// proceeding further. |
|
10950 | 1815 |
if ((tree.hasTag(PLUS_ASG) || tree.hasTag(MINUS_ASG)) && |
10 | 1816 |
l instanceof LocalItem && |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
1817 |
tree.lhs.type.getTag().isSubRangeOf(INT) && |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
1818 |
tree.rhs.type.getTag().isSubRangeOf(INT) && |
10 | 1819 |
tree.rhs.type.constValue() != null) { |
1820 |
int ival = ((Number) tree.rhs.type.constValue()).intValue(); |
|
10950 | 1821 |
if (tree.hasTag(MINUS_ASG)) ival = -ival; |
10 | 1822 |
((LocalItem)l).incr(ival); |
1823 |
result = l; |
|
1824 |
return; |
|
1825 |
} |
|
1826 |
// Otherwise, duplicate expression, load one copy |
|
1827 |
// and complete binary operation. |
|
1828 |
l.duplicate(); |
|
1829 |
l.coerce(operator.type.getParameterTypes().head).load(); |
|
1830 |
completeBinop(tree.lhs, tree.rhs, operator).coerce(tree.lhs.type); |
|
1831 |
} |
|
1832 |
result = items.makeAssignItem(l); |
|
1833 |
} |
|
1834 |
||
1835 |
public void visitUnary(JCUnary tree) { |
|
39920
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39599
diff
changeset
|
1836 |
OperatorSymbol operator = tree.operator; |
10950 | 1837 |
if (tree.hasTag(NOT)) { |
10 | 1838 |
CondItem od = genCond(tree.arg, false); |
1839 |
result = od.negate(); |
|
1840 |
} else { |
|
1841 |
Item od = genExpr(tree.arg, operator.type.getParameterTypes().head); |
|
1842 |
switch (tree.getTag()) { |
|
10950 | 1843 |
case POS: |
10 | 1844 |
result = od.load(); |
1845 |
break; |
|
10950 | 1846 |
case NEG: |
10 | 1847 |
result = od.load(); |
1848 |
code.emitop0(operator.opcode); |
|
1849 |
break; |
|
10950 | 1850 |
case COMPL: |
10 | 1851 |
result = od.load(); |
1852 |
emitMinusOne(od.typecode); |
|
1853 |
code.emitop0(operator.opcode); |
|
1854 |
break; |
|
10950 | 1855 |
case PREINC: case PREDEC: |
10 | 1856 |
od.duplicate(); |
1857 |
if (od instanceof LocalItem && |
|
1858 |
(operator.opcode == iadd || operator.opcode == isub)) { |
|
10950 | 1859 |
((LocalItem)od).incr(tree.hasTag(PREINC) ? 1 : -1); |
10 | 1860 |
result = od; |
1861 |
} else { |
|
1862 |
od.load(); |
|
1863 |
code.emitop0(one(od.typecode)); |
|
1864 |
code.emitop0(operator.opcode); |
|
1865 |
// Perform narrowing primitive conversion if byte, |
|
1866 |
// char, or short. Fix for 4304655. |
|
1867 |
if (od.typecode != INTcode && |
|
1868 |
Code.truncate(od.typecode) == INTcode) |
|
1869 |
code.emitop0(int2byte + od.typecode - BYTEcode); |
|
1870 |
result = items.makeAssignItem(od); |
|
1871 |
} |
|
1872 |
break; |
|
10950 | 1873 |
case POSTINC: case POSTDEC: |
10 | 1874 |
od.duplicate(); |
1875 |
if (od instanceof LocalItem && |
|
1876 |
(operator.opcode == iadd || operator.opcode == isub)) { |
|
1877 |
Item res = od.load(); |
|
10950 | 1878 |
((LocalItem)od).incr(tree.hasTag(POSTINC) ? 1 : -1); |
10 | 1879 |
result = res; |
1880 |
} else { |
|
1881 |
Item res = od.load(); |
|
1882 |
od.stash(od.typecode); |
|
1883 |
code.emitop0(one(od.typecode)); |
|
1884 |
code.emitop0(operator.opcode); |
|
1885 |
// Perform narrowing primitive conversion if byte, |
|
1886 |
// char, or short. Fix for 4304655. |
|
1887 |
if (od.typecode != INTcode && |
|
1888 |
Code.truncate(od.typecode) == INTcode) |
|
1889 |
code.emitop0(int2byte + od.typecode - BYTEcode); |
|
1890 |
od.store(); |
|
1891 |
result = res; |
|
1892 |
} |
|
1893 |
break; |
|
10950 | 1894 |
case NULLCHK: |
10 | 1895 |
result = od.load(); |
1896 |
code.emitop0(dup); |
|
45316
170696ddb377
8172880: Incorrect line number for NPE generated by instance-bound method reference
jlahoda
parents:
45220
diff
changeset
|
1897 |
genNullCheck(tree); |
10 | 1898 |
break; |
1899 |
default: |
|
8032 | 1900 |
Assert.error(); |
10 | 1901 |
} |
1902 |
} |
|
1903 |
} |
|
1904 |
||
1905 |
/** Generate a null check from the object value at stack top. */ |
|
45316
170696ddb377
8172880: Incorrect line number for NPE generated by instance-bound method reference
jlahoda
parents:
45220
diff
changeset
|
1906 |
private void genNullCheck(JCTree tree) { |
170696ddb377
8172880: Incorrect line number for NPE generated by instance-bound method reference
jlahoda
parents:
45220
diff
changeset
|
1907 |
code.statBegin(tree.pos); |
29295
5a367770a074
8073432: Object.getClass() throws stackless NPE, due to C2 intrinsic
mcimadamore
parents:
28800
diff
changeset
|
1908 |
if (allowBetterNullChecks) { |
45316
170696ddb377
8172880: Incorrect line number for NPE generated by instance-bound method reference
jlahoda
parents:
45220
diff
changeset
|
1909 |
callMethod(tree.pos(), syms.objectsType, names.requireNonNull, |
29295
5a367770a074
8073432: Object.getClass() throws stackless NPE, due to C2 intrinsic
mcimadamore
parents:
28800
diff
changeset
|
1910 |
List.of(syms.objectType), true); |
5a367770a074
8073432: Object.getClass() throws stackless NPE, due to C2 intrinsic
mcimadamore
parents:
28800
diff
changeset
|
1911 |
} else { |
45316
170696ddb377
8172880: Incorrect line number for NPE generated by instance-bound method reference
jlahoda
parents:
45220
diff
changeset
|
1912 |
callMethod(tree.pos(), syms.objectType, names.getClass, |
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
41029
diff
changeset
|
1913 |
List.nil(), false); |
29295
5a367770a074
8073432: Object.getClass() throws stackless NPE, due to C2 intrinsic
mcimadamore
parents:
28800
diff
changeset
|
1914 |
} |
10 | 1915 |
code.emitop0(pop); |
1916 |
} |
|
1917 |
||
1918 |
public void visitBinary(JCBinary tree) { |
|
39920
4923274643f2
8161708: javac, consider a different way to handle access code for operators
vromero
parents:
39599
diff
changeset
|
1919 |
OperatorSymbol operator = tree.operator; |
10 | 1920 |
if (operator.opcode == string_add) { |
35424 | 1921 |
result = concat.makeConcat(tree); |
10950 | 1922 |
} else if (tree.hasTag(AND)) { |
10 | 1923 |
CondItem lcond = genCond(tree.lhs, CRT_FLOW_CONTROLLER); |
1924 |
if (!lcond.isFalse()) { |
|
1925 |
Chain falseJumps = lcond.jumpFalse(); |
|
1926 |
code.resolve(lcond.trueJumps); |
|
1927 |
CondItem rcond = genCond(tree.rhs, CRT_FLOW_TARGET); |
|
1928 |
result = items. |
|
1929 |
makeCondItem(rcond.opcode, |
|
1930 |
rcond.trueJumps, |
|
5004
6c2694dc2da0
6929544: langtools source code uses statics qualified by instance variables
jjg
parents:
4875
diff
changeset
|
1931 |
Code.mergeChains(falseJumps, |
10 | 1932 |
rcond.falseJumps)); |
1933 |
} else { |
|
1934 |
result = lcond; |
|
1935 |
} |
|
10950 | 1936 |
} else if (tree.hasTag(OR)) { |
10 | 1937 |
CondItem lcond = genCond(tree.lhs, CRT_FLOW_CONTROLLER); |
1938 |
if (!lcond.isTrue()) { |
|
1939 |
Chain trueJumps = lcond.jumpTrue(); |
|
1940 |
code.resolve(lcond.falseJumps); |
|
1941 |
CondItem rcond = genCond(tree.rhs, CRT_FLOW_TARGET); |
|
1942 |
result = items. |
|
1943 |
makeCondItem(rcond.opcode, |
|
5004
6c2694dc2da0
6929544: langtools source code uses statics qualified by instance variables
jjg
parents:
4875
diff
changeset
|
1944 |
Code.mergeChains(trueJumps, rcond.trueJumps), |
10 | 1945 |
rcond.falseJumps); |
1946 |
} else { |
|
1947 |
result = lcond; |
|
1948 |
} |
|
1949 |
} else { |
|
1950 |
Item od = genExpr(tree.lhs, operator.type.getParameterTypes().head); |
|
1951 |
od.load(); |
|
1952 |
result = completeBinop(tree.lhs, tree.rhs, operator); |
|
1953 |
} |
|
1954 |
} |
|
1955 |
||
1956 |
||
1957 |
/** Complete generating code for operation, with left operand |
|
1958 |
* already on stack. |
|
1959 |
* @param lhs The tree representing the left operand. |
|
1960 |
* @param rhs The tree representing the right operand. |
|
1961 |
* @param operator The operator symbol. |
|
1962 |
*/ |
|
1963 |
Item completeBinop(JCTree lhs, JCTree rhs, OperatorSymbol operator) { |
|
1964 |
MethodType optype = (MethodType)operator.type; |
|
1965 |
int opcode = operator.opcode; |
|
1966 |
if (opcode >= if_icmpeq && opcode <= if_icmple && |
|
1967 |
rhs.type.constValue() instanceof Number && |
|
1968 |
((Number) rhs.type.constValue()).intValue() == 0) { |
|
1969 |
opcode = opcode + (ifeq - if_icmpeq); |
|
1970 |
} else if (opcode >= if_acmpeq && opcode <= if_acmpne && |
|
1971 |
TreeInfo.isNull(rhs)) { |
|
1972 |
opcode = opcode + (if_acmp_null - if_acmpeq); |
|
1973 |
} else { |
|
1974 |
// The expected type of the right operand is |
|
1975 |
// the second parameter type of the operator, except for |
|
1976 |
// shifts with long shiftcount, where we convert the opcode |
|
1977 |
// to a short shift and the expected type to int. |
|
1978 |
Type rtype = operator.erasure(types).getParameterTypes().tail.head; |
|
1979 |
if (opcode >= ishll && opcode <= lushrl) { |
|
1980 |
opcode = opcode + (ishl - ishll); |
|
1981 |
rtype = syms.intType; |
|
1982 |
} |
|
1983 |
// Generate code for right operand and load. |
|
1984 |
genExpr(rhs, rtype).load(); |
|
1985 |
// If there are two consecutive opcode instructions, |
|
1986 |
// emit the first now. |
|
1987 |
if (opcode >= (1 << preShift)) { |
|
1988 |
code.emitop0(opcode >> preShift); |
|
1989 |
opcode = opcode & 0xFF; |
|
1990 |
} |
|
1991 |
} |
|
1992 |
if (opcode >= ifeq && opcode <= if_acmpne || |
|
1993 |
opcode == if_acmp_null || opcode == if_acmp_nonnull) { |
|
1994 |
return items.makeCondItem(opcode); |
|
1995 |
} else { |
|
1996 |
code.emitop0(opcode); |
|
1997 |
return items.makeStackItem(optype.restype); |
|
1998 |
} |
|
1999 |
} |
|
2000 |
||
2001 |
public void visitTypeCast(JCTypeCast tree) { |
|
35418
8681d57fc3f8
8144168: No type annotations generated for nested lambdas
sadayapalam
parents:
33371
diff
changeset
|
2002 |
result = genExpr(tree.expr, tree.clazz.type).load(); |
15385 | 2003 |
setTypeAnnotationPositions(tree.pos); |
10 | 2004 |
// Additional code is only needed if we cast to a reference type |
2005 |
// which is not statically a supertype of the expression's type. |
|
2006 |
// For basic types, the coerce(...) in genExpr(...) will do |
|
2007 |
// the conversion. |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
2008 |
if (!tree.clazz.type.isPrimitive() && |
28800
35f9042d53b3
8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
28338
diff
changeset
|
2009 |
!types.isSameType(tree.expr.type, tree.clazz.type) && |
35f9042d53b3
8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
28338
diff
changeset
|
2010 |
types.asSuper(tree.expr.type, tree.clazz.type.tsym) == null) { |
10 | 2011 |
code.emitop2(checkcast, makeRef(tree.pos(), tree.clazz.type)); |
2012 |
} |
|
2013 |
} |
|
2014 |
||
2015 |
public void visitWildcard(JCWildcard tree) { |
|
2016 |
throw new AssertionError(this.getClass().getName()); |
|
2017 |
} |
|
2018 |
||
2019 |
public void visitTypeTest(JCInstanceOf tree) { |
|
35418
8681d57fc3f8
8144168: No type annotations generated for nested lambdas
sadayapalam
parents:
33371
diff
changeset
|
2020 |
genExpr(tree.expr, tree.expr.type).load(); |
26532
aa84b6606229
8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents:
25874
diff
changeset
|
2021 |
setTypeAnnotationPositions(tree.pos); |
10 | 2022 |
code.emitop2(instanceof_, makeRef(tree.pos(), tree.clazz.type)); |
2023 |
result = items.makeStackItem(syms.booleanType); |
|
2024 |
} |
|
2025 |
||
2026 |
public void visitIndexed(JCArrayAccess tree) { |
|
2027 |
genExpr(tree.indexed, tree.indexed.type).load(); |
|
2028 |
genExpr(tree.index, syms.intType).load(); |
|
2029 |
result = items.makeIndexedItem(tree.type); |
|
2030 |
} |
|
2031 |
||
2032 |
public void visitIdent(JCIdent tree) { |
|
2033 |
Symbol sym = tree.sym; |
|
2034 |
if (tree.name == names._this || tree.name == names._super) { |
|
2035 |
Item res = tree.name == names._this |
|
2036 |
? items.makeThisItem() |
|
2037 |
: items.makeSuperItem(); |
|
2038 |
if (sym.kind == MTH) { |
|
2039 |
// Generate code to address the constructor. |
|
2040 |
res.load(); |
|
2041 |
res = items.makeMemberItem(sym, true); |
|
2042 |
} |
|
2043 |
result = res; |
|
2044 |
} else if (sym.kind == VAR && sym.owner.kind == MTH) { |
|
2045 |
result = items.makeLocalItem((VarSymbol)sym); |
|
14046
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
2046 |
} else if (isInvokeDynamic(sym)) { |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
2047 |
result = items.makeDynamicItem(sym); |
10 | 2048 |
} else if ((sym.flags() & STATIC) != 0) { |
2049 |
if (!isAccessSuper(env.enclMethod)) |
|
2050 |
sym = binaryQualifier(sym, env.enclClass.type); |
|
2051 |
result = items.makeStaticItem(sym); |
|
2052 |
} else { |
|
2053 |
items.makeThisItem().load(); |
|
2054 |
sym = binaryQualifier(sym, env.enclClass.type); |
|
2055 |
result = items.makeMemberItem(sym, (sym.flags() & PRIVATE) != 0); |
|
2056 |
} |
|
2057 |
} |
|
2058 |
||
2059 |
public void visitSelect(JCFieldAccess tree) { |
|
2060 |
Symbol sym = tree.sym; |
|
2061 |
||
2062 |
if (tree.name == names._class) { |
|
16801 | 2063 |
code.emitLdc(makeRef(tree.pos(), tree.selected.type)); |
10 | 2064 |
result = items.makeStackItem(pt); |
2065 |
return; |
|
15385 | 2066 |
} |
10 | 2067 |
|
2068 |
Symbol ssym = TreeInfo.symbol(tree.selected); |
|
2069 |
||
2070 |
// Are we selecting via super? |
|
2071 |
boolean selectSuper = |
|
2072 |
ssym != null && (ssym.kind == TYP || ssym.name == names._super); |
|
2073 |
||
2074 |
// Are we accessing a member of the superclass in an access method |
|
2075 |
// resulting from a qualified super? |
|
2076 |
boolean accessSuper = isAccessSuper(env.enclMethod); |
|
2077 |
||
2078 |
Item base = (selectSuper) |
|
2079 |
? items.makeSuperItem() |
|
2080 |
: genExpr(tree.selected, tree.selected.type); |
|
2081 |
||
2082 |
if (sym.kind == VAR && ((VarSymbol) sym).getConstValue() != null) { |
|
2083 |
// We are seeing a variable that is constant but its selecting |
|
2084 |
// expression is not. |
|
2085 |
if ((sym.flags() & STATIC) != 0) { |
|
2086 |
if (!selectSuper && (ssym == null || ssym.kind != TYP)) |
|
2087 |
base = base.load(); |
|
2088 |
base.drop(); |
|
2089 |
} else { |
|
2090 |
base.load(); |
|
45316
170696ddb377
8172880: Incorrect line number for NPE generated by instance-bound method reference
jlahoda
parents:
45220
diff
changeset
|
2091 |
genNullCheck(tree.selected); |
10 | 2092 |
} |
2093 |
result = items. |
|
2094 |
makeImmediateItem(sym.type, ((VarSymbol) sym).getConstValue()); |
|
2095 |
} else { |
|
14046
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
2096 |
if (isInvokeDynamic(sym)) { |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
2097 |
result = items.makeDynamicItem(sym); |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
2098 |
return; |
21897
ba025087b9f9
8027789: Access method for Outer.super.m() references indirect superclass
jlahoda
parents:
21498
diff
changeset
|
2099 |
} else { |
10 | 2100 |
sym = binaryQualifier(sym, tree.selected.type); |
14046
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
2101 |
} |
10 | 2102 |
if ((sym.flags() & STATIC) != 0) { |
2103 |
if (!selectSuper && (ssym == null || ssym.kind != TYP)) |
|
2104 |
base = base.load(); |
|
2105 |
base.drop(); |
|
2106 |
result = items.makeStaticItem(sym); |
|
2107 |
} else { |
|
2108 |
base.load(); |
|
2109 |
if (sym == syms.lengthVar) { |
|
2110 |
code.emitop0(arraylength); |
|
2111 |
result = items.makeStackItem(syms.intType); |
|
2112 |
} else { |
|
2113 |
result = items. |
|
2114 |
makeMemberItem(sym, |
|
2115 |
(sym.flags() & PRIVATE) != 0 || |
|
2116 |
selectSuper || accessSuper); |
|
2117 |
} |
|
2118 |
} |
|
2119 |
} |
|
2120 |
} |
|
2121 |
||
14046
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
2122 |
public boolean isInvokeDynamic(Symbol sym) { |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
2123 |
return sym.kind == MTH && ((MethodSymbol)sym).isDynamic(); |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
2124 |
} |
8ef5d5b19998
7194586: Add back-end support for invokedynamic
mcimadamore
parents:
13844
diff
changeset
|
2125 |
|
10 | 2126 |
public void visitLiteral(JCLiteral tree) { |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
2127 |
if (tree.type.hasTag(BOT)) { |
10 | 2128 |
code.emitop0(aconst_null); |
29777
95a89a2efd89
7040592: Gen.java: fix code for handling 'null' literal when expected type is array
sadayapalam
parents:
29295
diff
changeset
|
2129 |
result = items.makeStackItem(tree.type); |
10 | 2130 |
} |
2131 |
else |
|
2132 |
result = items.makeImmediateItem(tree.type, tree.value); |
|
2133 |
} |
|
2134 |
||
2135 |
public void visitLetExpr(LetExpr tree) { |
|
36269
5b3afff0e70c
8151016: Migrate asserts introduced in Valhalla code generation to JDK9 dev
sadayapalam
parents:
36153
diff
changeset
|
2136 |
letExprDepth++; |
10 | 2137 |
int limit = code.nextreg; |
2138 |
genStats(tree.defs, env); |
|
2139 |
result = genExpr(tree.expr, tree.expr.type).load(); |
|
2140 |
code.endScopes(limit); |
|
36269
5b3afff0e70c
8151016: Migrate asserts introduced in Valhalla code generation to JDK9 dev
sadayapalam
parents:
36153
diff
changeset
|
2141 |
letExprDepth--; |
10 | 2142 |
} |
2143 |
||
14721
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
2144 |
private void generateReferencesToPrunedTree(ClassSymbol classSymbol, Pool pool) { |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
2145 |
List<JCTree> prunedInfo = lower.prunedTree.get(classSymbol); |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
2146 |
if (prunedInfo != null) { |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
2147 |
for (JCTree prunedTree: prunedInfo) { |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
2148 |
prunedTree.accept(classReferenceVisitor); |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
2149 |
} |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
2150 |
} |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
2151 |
} |
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
2152 |
|
10 | 2153 |
/* ************************************************************************ |
2154 |
* main method |
|
2155 |
*************************************************************************/ |
|
2156 |
||
2157 |
/** Generate code for a class definition. |
|
2158 |
* @param env The attribution environment that belongs to the |
|
2159 |
* outermost class containing this class definition. |
|
2160 |
* We need this for resolving some additional symbols. |
|
2161 |
* @param cdef The tree representing the class definition. |
|
2162 |
* @return True if code is generated with no errors. |
|
2163 |
*/ |
|
2164 |
public boolean genClass(Env<AttrContext> env, JCClassDecl cdef) { |
|
2165 |
try { |
|
2166 |
attrEnv = env; |
|
2167 |
ClassSymbol c = cdef.sym; |
|
2168 |
this.toplevel = env.toplevel; |
|
11055 | 2169 |
this.endPosTable = toplevel.endPositions; |
10 | 2170 |
c.pool = pool; |
2171 |
pool.reset(); |
|
31000
18bc6fadeb4e
8073372: Redundant CONSTANT_Class entry not generated for inlined constant
vromero
parents:
30406
diff
changeset
|
2172 |
/* method normalizeDefs() can add references to external classes into the constant pool |
18bc6fadeb4e
8073372: Redundant CONSTANT_Class entry not generated for inlined constant
vromero
parents:
30406
diff
changeset
|
2173 |
*/ |
18bc6fadeb4e
8073372: Redundant CONSTANT_Class entry not generated for inlined constant
vromero
parents:
30406
diff
changeset
|
2174 |
cdef.defs = normalizeDefs(cdef.defs, c); |
14721
071e3587f212
7153958: add constant pool reference to class containing inlined constants
vromero
parents:
14359
diff
changeset
|
2175 |
generateReferencesToPrunedTree(c, pool); |
22163 | 2176 |
Env<GenContext> localEnv = new Env<>(cdef, new GenContext()); |
10 | 2177 |
localEnv.toplevel = env.toplevel; |
2178 |
localEnv.enclClass = cdef; |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
2179 |
|
10 | 2180 |
for (List<JCTree> l = cdef.defs; l.nonEmpty(); l = l.tail) { |
2181 |
genDef(l.head, localEnv); |
|
2182 |
} |
|
2183 |
if (pool.numEntries() > Pool.MAX_ENTRIES) { |
|
2184 |
log.error(cdef.pos(), "limit.pool"); |
|
2185 |
nerrs++; |
|
2186 |
} |
|
2187 |
if (nerrs != 0) { |
|
2188 |
// if errors, discard code |
|
2189 |
for (List<JCTree> l = cdef.defs; l.nonEmpty(); l = l.tail) { |
|
10950 | 2190 |
if (l.head.hasTag(METHODDEF)) |
10 | 2191 |
((JCMethodDecl) l.head).sym.code = null; |
2192 |
} |
|
2193 |
} |
|
2194 |
cdef.defs = List.nil(); // discard trees |
|
2195 |
return nerrs == 0; |
|
2196 |
} finally { |
|
2197 |
// note: this method does NOT support recursion. |
|
2198 |
attrEnv = null; |
|
2199 |
this.env = null; |
|
2200 |
toplevel = null; |
|
11055 | 2201 |
endPosTable = null; |
10 | 2202 |
nerrs = 0; |
2203 |
} |
|
2204 |
} |
|
2205 |
||
2206 |
/* ************************************************************************ |
|
2207 |
* Auxiliary classes |
|
2208 |
*************************************************************************/ |
|
2209 |
||
2210 |
/** An abstract class for finalizer generation. |
|
2211 |
*/ |
|
2212 |
abstract class GenFinalizer { |
|
2213 |
/** Generate code to clean up when unwinding. */ |
|
2214 |
abstract void gen(); |
|
2215 |
||
2216 |
/** Generate code to clean up at last. */ |
|
2217 |
abstract void genLast(); |
|
2218 |
||
2219 |
/** Does this finalizer have some nontrivial cleanup to perform? */ |
|
2220 |
boolean hasFinalizer() { return true; } |
|
2221 |
} |
|
2222 |
||
2223 |
/** code generation contexts, |
|
2224 |
* to be used as type parameter for environments. |
|
2225 |
*/ |
|
2226 |
static class GenContext { |
|
2227 |
||
2228 |
/** A chain for all unresolved jumps that exit the current environment. |
|
2229 |
*/ |
|
2230 |
Chain exit = null; |
|
2231 |
||
2232 |
/** A chain for all unresolved jumps that continue in the |
|
2233 |
* current environment. |
|
2234 |
*/ |
|
2235 |
Chain cont = null; |
|
2236 |
||
2237 |
/** A closure that generates the finalizer of the current environment. |
|
2238 |
* Only set for Synchronized and Try contexts. |
|
2239 |
*/ |
|
2240 |
GenFinalizer finalize = null; |
|
2241 |
||
2242 |
/** Is this a switch statement? If so, allocate registers |
|
2243 |
* even when the variable declaration is unreachable. |
|
2244 |
*/ |
|
2245 |
boolean isSwitch = false; |
|
2246 |
||
2247 |
/** A list buffer containing all gaps in the finalizer range, |
|
2248 |
* where a catch all exception should not apply. |
|
2249 |
*/ |
|
2250 |
ListBuffer<Integer> gaps = null; |
|
2251 |
||
2252 |
/** Add given chain to exit chain. |
|
2253 |
*/ |
|
2254 |
void addExit(Chain c) { |
|
2255 |
exit = Code.mergeChains(c, exit); |
|
2256 |
} |
|
2257 |
||
2258 |
/** Add given chain to cont chain. |
|
2259 |
*/ |
|
2260 |
void addCont(Chain c) { |
|
2261 |
cont = Code.mergeChains(c, cont); |
|
2262 |
} |
|
2263 |
} |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
19923
diff
changeset
|
2264 |
|
10 | 2265 |
} |