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