1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.codegen;
  27 
  28 import static jdk.nashorn.internal.codegen.ClassEmitter.Flag.PRIVATE;
  29 import static jdk.nashorn.internal.codegen.ClassEmitter.Flag.STATIC;
  30 import static jdk.nashorn.internal.codegen.CompilerConstants.ARGUMENTS;
  31 import static jdk.nashorn.internal.codegen.CompilerConstants.CALLEE;
  32 import static jdk.nashorn.internal.codegen.CompilerConstants.CREATE_PROGRAM_FUNCTION;
  33 import static jdk.nashorn.internal.codegen.CompilerConstants.GET_MAP;
  34 import static jdk.nashorn.internal.codegen.CompilerConstants.GET_STRING;
  35 import static jdk.nashorn.internal.codegen.CompilerConstants.QUICK_PREFIX;
  36 import static jdk.nashorn.internal.codegen.CompilerConstants.REGEX_PREFIX;
  37 import static jdk.nashorn.internal.codegen.CompilerConstants.SCOPE;
  38 import static jdk.nashorn.internal.codegen.CompilerConstants.SPLIT_PREFIX;
  39 import static jdk.nashorn.internal.codegen.CompilerConstants.THIS;
  40 import static jdk.nashorn.internal.codegen.CompilerConstants.VARARGS;
  41 import static jdk.nashorn.internal.codegen.CompilerConstants.interfaceCallNoLookup;
  42 import static jdk.nashorn.internal.codegen.CompilerConstants.methodDescriptor;
  43 import static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup;
  44 import static jdk.nashorn.internal.codegen.CompilerConstants.typeDescriptor;
  45 import static jdk.nashorn.internal.codegen.CompilerConstants.virtualCallNoLookup;
  46 import static jdk.nashorn.internal.ir.Symbol.HAS_SLOT;
  47 import static jdk.nashorn.internal.ir.Symbol.IS_INTERNAL;
  48 import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.INVALID_PROGRAM_POINT;
  49 import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.isValid;
  50 import static jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor.CALLSITE_APPLY_TO_CALL;
  51 import static jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor.CALLSITE_DECLARE;
  52 import static jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor.CALLSITE_FAST_SCOPE;
  53 import static jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor.CALLSITE_OPTIMISTIC;
  54 import static jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor.CALLSITE_PROGRAM_POINT_SHIFT;
  55 import static jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor.CALLSITE_SCOPE;
  56 
  57 import java.io.PrintWriter;
  58 import java.util.ArrayDeque;
  59 import java.util.ArrayList;
  60 import java.util.Arrays;
  61 import java.util.BitSet;
  62 import java.util.Collection;
  63 import java.util.Collections;
  64 import java.util.Deque;
  65 import java.util.EnumSet;
  66 import java.util.HashMap;
  67 import java.util.HashSet;
  68 import java.util.Iterator;
  69 import java.util.LinkedList;
  70 import java.util.List;
  71 import java.util.Map;
  72 import java.util.Set;
  73 import java.util.TreeMap;
  74 import java.util.function.Supplier;
  75 import jdk.nashorn.internal.AssertsEnabled;
  76 import jdk.nashorn.internal.IntDeque;
  77 import jdk.nashorn.internal.codegen.ClassEmitter.Flag;
  78 import jdk.nashorn.internal.codegen.CompilerConstants.Call;
  79 import jdk.nashorn.internal.codegen.types.ArrayType;
  80 import jdk.nashorn.internal.codegen.types.Type;
  81 import jdk.nashorn.internal.ir.AccessNode;
  82 import jdk.nashorn.internal.ir.BaseNode;
  83 import jdk.nashorn.internal.ir.BinaryNode;
  84 import jdk.nashorn.internal.ir.Block;
  85 import jdk.nashorn.internal.ir.BlockStatement;
  86 import jdk.nashorn.internal.ir.BreakNode;
  87 import jdk.nashorn.internal.ir.CallNode;
  88 import jdk.nashorn.internal.ir.CaseNode;
  89 import jdk.nashorn.internal.ir.CatchNode;
  90 import jdk.nashorn.internal.ir.ContinueNode;
  91 import jdk.nashorn.internal.ir.EmptyNode;
  92 import jdk.nashorn.internal.ir.Expression;
  93 import jdk.nashorn.internal.ir.ExpressionStatement;
  94 import jdk.nashorn.internal.ir.ForNode;
  95 import jdk.nashorn.internal.ir.FunctionNode;
  96 import jdk.nashorn.internal.ir.FunctionNode.CompilationState;
  97 import jdk.nashorn.internal.ir.GetSplitState;
  98 import jdk.nashorn.internal.ir.IdentNode;
  99 import jdk.nashorn.internal.ir.IfNode;
 100 import jdk.nashorn.internal.ir.IndexNode;
 101 import jdk.nashorn.internal.ir.JoinPredecessorExpression;
 102 import jdk.nashorn.internal.ir.JumpStatement;
 103 import jdk.nashorn.internal.ir.JumpToInlinedFinally;
 104 import jdk.nashorn.internal.ir.LabelNode;
 105 import jdk.nashorn.internal.ir.LexicalContext;
 106 import jdk.nashorn.internal.ir.LexicalContextNode;
 107 import jdk.nashorn.internal.ir.LiteralNode;
 108 import jdk.nashorn.internal.ir.LiteralNode.ArrayLiteralNode;
 109 import jdk.nashorn.internal.ir.LiteralNode.ArrayLiteralNode.ArrayUnit;
 110 import jdk.nashorn.internal.ir.LiteralNode.PrimitiveLiteralNode;
 111 import jdk.nashorn.internal.ir.LocalVariableConversion;
 112 import jdk.nashorn.internal.ir.LoopNode;
 113 import jdk.nashorn.internal.ir.Node;
 114 import jdk.nashorn.internal.ir.ObjectNode;
 115 import jdk.nashorn.internal.ir.Optimistic;
 116 import jdk.nashorn.internal.ir.PropertyNode;
 117 import jdk.nashorn.internal.ir.ReturnNode;
 118 import jdk.nashorn.internal.ir.RuntimeNode;
 119 import jdk.nashorn.internal.ir.RuntimeNode.Request;
 120 import jdk.nashorn.internal.ir.SetSplitState;
 121 import jdk.nashorn.internal.ir.SplitReturn;
 122 import jdk.nashorn.internal.ir.Statement;
 123 import jdk.nashorn.internal.ir.SwitchNode;
 124 import jdk.nashorn.internal.ir.Symbol;
 125 import jdk.nashorn.internal.ir.TernaryNode;
 126 import jdk.nashorn.internal.ir.ThrowNode;
 127 import jdk.nashorn.internal.ir.TryNode;
 128 import jdk.nashorn.internal.ir.UnaryNode;
 129 import jdk.nashorn.internal.ir.VarNode;
 130 import jdk.nashorn.internal.ir.WhileNode;
 131 import jdk.nashorn.internal.ir.WithNode;
 132 import jdk.nashorn.internal.ir.visitor.NodeOperatorVisitor;
 133 import jdk.nashorn.internal.ir.visitor.NodeVisitor;
 134 import jdk.nashorn.internal.objects.Global;
 135 import jdk.nashorn.internal.objects.ScriptFunctionImpl;
 136 import jdk.nashorn.internal.parser.Lexer.RegexToken;
 137 import jdk.nashorn.internal.parser.TokenType;
 138 import jdk.nashorn.internal.runtime.Context;
 139 import jdk.nashorn.internal.runtime.Debug;
 140 import jdk.nashorn.internal.runtime.ECMAException;
 141 import jdk.nashorn.internal.runtime.JSType;
 142 import jdk.nashorn.internal.runtime.OptimisticReturnFilters;
 143 import jdk.nashorn.internal.runtime.PropertyMap;
 144 import jdk.nashorn.internal.runtime.RecompilableScriptFunctionData;
 145 import jdk.nashorn.internal.runtime.RewriteException;
 146 import jdk.nashorn.internal.runtime.Scope;
 147 import jdk.nashorn.internal.runtime.ScriptEnvironment;
 148 import jdk.nashorn.internal.runtime.ScriptFunction;
 149 import jdk.nashorn.internal.runtime.ScriptObject;
 150 import jdk.nashorn.internal.runtime.ScriptRuntime;
 151 import jdk.nashorn.internal.runtime.Source;
 152 import jdk.nashorn.internal.runtime.Undefined;
 153 import jdk.nashorn.internal.runtime.UnwarrantedOptimismException;
 154 import jdk.nashorn.internal.runtime.arrays.ArrayData;
 155 import jdk.nashorn.internal.runtime.linker.LinkerCallSite;
 156 import jdk.nashorn.internal.runtime.logging.DebugLogger;
 157 import jdk.nashorn.internal.runtime.logging.Loggable;
 158 import jdk.nashorn.internal.runtime.logging.Logger;
 159 import jdk.nashorn.internal.runtime.options.Options;
 160 
 161 /**
 162  * This is the lowest tier of the code generator. It takes lowered ASTs emitted
 163  * from Lower and emits Java byte code. The byte code emission logic is broken
 164  * out into MethodEmitter. MethodEmitter works internally with a type stack, and
 165  * keeps track of the contents of the byte code stack. This way we avoid a large
 166  * number of special cases on the form
 167  * <pre>
 168  * if (type == INT) {
 169  *     visitInsn(ILOAD, slot);
 170  * } else if (type == DOUBLE) {
 171  *     visitInsn(DOUBLE, slot);
 172  * }
 173  * </pre>
 174  * This quickly became apparent when the code generator was generalized to work
 175  * with all types, and not just numbers or objects.
 176  * <p>
 177  * The CodeGenerator visits nodes only once and emits bytecode for them.
 178  */
 179 @Logger(name="codegen")
 180 final class CodeGenerator extends NodeOperatorVisitor<CodeGeneratorLexicalContext> implements Loggable {
 181 
 182     private static final Type SCOPE_TYPE = Type.typeFor(ScriptObject.class);
 183 
 184     private static final String GLOBAL_OBJECT = Type.getInternalName(Global.class);
 185 
 186     private static final Call CREATE_REWRITE_EXCEPTION = CompilerConstants.staticCallNoLookup(RewriteException.class,
 187             "create", RewriteException.class, UnwarrantedOptimismException.class, Object[].class, String[].class);
 188     private static final Call CREATE_REWRITE_EXCEPTION_REST_OF = CompilerConstants.staticCallNoLookup(RewriteException.class,
 189             "create", RewriteException.class, UnwarrantedOptimismException.class, Object[].class, String[].class, int[].class);
 190 
 191     private static final Call ENSURE_INT = CompilerConstants.staticCallNoLookup(OptimisticReturnFilters.class,
 192             "ensureInt", int.class, Object.class, int.class);
 193     private static final Call ENSURE_LONG = CompilerConstants.staticCallNoLookup(OptimisticReturnFilters.class,
 194             "ensureLong", long.class, Object.class, int.class);
 195     private static final Call ENSURE_NUMBER = CompilerConstants.staticCallNoLookup(OptimisticReturnFilters.class,
 196             "ensureNumber", double.class, Object.class, int.class);
 197 
 198     private static final Call CREATE_FUNCTION_OBJECT = CompilerConstants.staticCallNoLookup(ScriptFunctionImpl.class,
 199             "create", ScriptFunction.class, Object[].class, int.class, ScriptObject.class);
 200     private static final Call CREATE_FUNCTION_OBJECT_NO_SCOPE = CompilerConstants.staticCallNoLookup(ScriptFunctionImpl.class,
 201             "create", ScriptFunction.class, Object[].class, int.class);
 202 
 203     private static final Call TO_NUMBER_FOR_EQ = CompilerConstants.staticCallNoLookup(JSType.class,
 204             "toNumberForEq", double.class, Object.class);
 205     private static final Call TO_NUMBER_FOR_STRICT_EQ = CompilerConstants.staticCallNoLookup(JSType.class,
 206             "toNumberForStrictEq", double.class, Object.class);
 207 
 208 
 209     private static final Class<?> ITERATOR_CLASS = Iterator.class;
 210     static {
 211         assert ITERATOR_CLASS == CompilerConstants.ITERATOR_PREFIX.type();
 212     }
 213     private static final Type ITERATOR_TYPE = Type.typeFor(ITERATOR_CLASS);
 214     private static final Type EXCEPTION_TYPE = Type.typeFor(CompilerConstants.EXCEPTION_PREFIX.type());
 215 
 216     private static final Integer INT_ZERO = 0;
 217 
 218     /** Constant data & installation. The only reason the compiler keeps this is because it is assigned
 219      *  by reflection in class installation */
 220     private final Compiler compiler;
 221 
 222     /** Is the current code submitted by 'eval' call? */
 223     private final boolean evalCode;
 224 
 225     /** Call site flags given to the code generator to be used for all generated call sites */
 226     private final int callSiteFlags;
 227 
 228     /** How many regexp fields have been emitted */
 229     private int regexFieldCount;
 230 
 231     /** Line number for last statement. If we encounter a new line number, line number bytecode information
 232      *  needs to be generated */
 233     private int lastLineNumber = -1;
 234 
 235     /** When should we stop caching regexp expressions in fields to limit bytecode size? */
 236     private static final int MAX_REGEX_FIELDS = 2 * 1024;
 237 
 238     /** Current method emitter */
 239     private MethodEmitter method;
 240 
 241     /** Current compile unit */
 242     private CompileUnit unit;
 243 
 244     private final DebugLogger log;
 245 
 246     /** From what size should we use spill instead of fields for JavaScript objects? */
 247     private static final int OBJECT_SPILL_THRESHOLD = Options.getIntProperty("nashorn.spill.threshold", 256);
 248 
 249     private final Set<String> emittedMethods = new HashSet<>();
 250 
 251     // Function Id -> ContinuationInfo. Used by compilation of rest-of function only.
 252     private final Map<Integer, ContinuationInfo> fnIdToContinuationInfo = new HashMap<>();
 253 
 254     private final Deque<Label> scopeEntryLabels = new ArrayDeque<>();
 255 
 256     private static final Label METHOD_BOUNDARY = new Label("");
 257     private final Deque<Label> catchLabels = new ArrayDeque<>();
 258     // Number of live locals on entry to (and thus also break from) labeled blocks.
 259     private final IntDeque labeledBlockBreakLiveLocals = new IntDeque();
 260 
 261     //is this a rest of compilation
 262     private final int[] continuationEntryPoints;
 263 
 264     /**
 265      * Constructor.
 266      *
 267      * @param compiler
 268      */
 269     CodeGenerator(final Compiler compiler, final int[] continuationEntryPoints) {
 270         super(new CodeGeneratorLexicalContext());
 271         this.compiler                = compiler;
 272         this.evalCode                = compiler.getSource().isEvalCode();
 273         this.continuationEntryPoints = continuationEntryPoints;
 274         this.callSiteFlags           = compiler.getScriptEnvironment()._callsite_flags;
 275         this.log                     = initLogger(compiler.getContext());
 276     }
 277 
 278     @Override
 279     public DebugLogger getLogger() {
 280         return log;
 281     }
 282 
 283     @Override
 284     public DebugLogger initLogger(final Context context) {
 285         return context.getLogger(this.getClass());
 286     }
 287 
 288     /**
 289      * Gets the call site flags, adding the strict flag if the current function
 290      * being generated is in strict mode
 291      *
 292      * @return the correct flags for a call site in the current function
 293      */
 294     int getCallSiteFlags() {
 295         return lc.getCurrentFunction().getCallSiteFlags() | callSiteFlags;
 296     }
 297 
 298     /**
 299      * Gets the flags for a scope call site.
 300      * @param symbol a scope symbol
 301      * @return the correct flags for the scope call site
 302      */
 303     private int getScopeCallSiteFlags(final Symbol symbol) {
 304         assert symbol.isScope();
 305         final int flags = getCallSiteFlags() | CALLSITE_SCOPE;
 306         if (isEvalCode() && symbol.isGlobal()) {
 307             return flags; // Don't set fast-scope flag on non-declared globals in eval code - see JDK-8077955.
 308         }
 309         return isFastScope(symbol) ? flags | CALLSITE_FAST_SCOPE : flags;
 310     }
 311 
 312     /**
 313      * Are we generating code for 'eval' code?
 314      * @return true if currently compiled code is 'eval' code.
 315      */
 316     boolean isEvalCode() {
 317         return evalCode;
 318     }
 319 
 320     /**
 321      * Are we using dual primitive/object field representation?
 322      * @return true if using dual field representation, false for object-only fields
 323      */
 324     boolean useDualFields() {
 325         return compiler.getContext().useDualFields();
 326     }
 327 
 328     /**
 329      * Load an identity node
 330      *
 331      * @param identNode an identity node to load
 332      * @return the method generator used
 333      */
 334     private MethodEmitter loadIdent(final IdentNode identNode, final TypeBounds resultBounds) {
 335         checkTemporalDeadZone(identNode);
 336         final Symbol symbol = identNode.getSymbol();
 337 
 338         if (!symbol.isScope()) {
 339             final Type type = identNode.getType();
 340             if(type == Type.UNDEFINED) {
 341                 return method.loadUndefined(resultBounds.widest);
 342             }
 343 
 344             assert symbol.hasSlot() || symbol.isParam();
 345             return method.load(identNode);
 346         }
 347 
 348         assert identNode.getSymbol().isScope() : identNode + " is not in scope!";
 349         final int flags = getScopeCallSiteFlags(symbol);
 350         if (isFastScope(symbol)) {
 351             // Only generate shared scope getter for fast-scope symbols so we know we can dial in correct scope.
 352             if (symbol.getUseCount() > SharedScopeCall.FAST_SCOPE_GET_THRESHOLD && !isOptimisticOrRestOf()) {
 353                 method.loadCompilerConstant(SCOPE);
 354                 // As shared scope vars are only used in non-optimistic compilation, we switch from using TypeBounds to
 355                 // just a single definitive type, resultBounds.widest.
 356                 loadSharedScopeVar(resultBounds.widest, symbol, flags);
 357             } else {
 358                 new LoadFastScopeVar(identNode, resultBounds, flags).emit();
 359             }
 360         } else {
 361             //slow scope load, we have no proto depth
 362             new LoadScopeVar(identNode, resultBounds, flags).emit();
 363         }
 364 
 365         return method;
 366     }
 367 
 368     // Any access to LET and CONST variables before their declaration must throw ReferenceError.
 369     // This is called the temporal dead zone (TDZ). See https://gist.github.com/rwaldron/f0807a758aa03bcdd58a
 370     private void checkTemporalDeadZone(final IdentNode identNode) {
 371         if (identNode.isDead()) {
 372             method.load(identNode.getSymbol().getName()).invoke(ScriptRuntime.THROW_REFERENCE_ERROR);
 373         }
 374     }
 375 
 376     // Runtime check for assignment to ES6 const
 377     private void checkAssignTarget(final Expression expression) {
 378         if (expression instanceof IdentNode && ((IdentNode)expression).getSymbol().isConst()) {
 379             method.load(((IdentNode)expression).getSymbol().getName()).invoke(ScriptRuntime.THROW_CONST_TYPE_ERROR);
 380         }
 381     }
 382 
 383     private boolean isRestOf() {
 384         return continuationEntryPoints != null;
 385     }
 386 
 387     private boolean isOptimisticOrRestOf() {
 388         return useOptimisticTypes() || isRestOf();
 389     }
 390 
 391     private boolean isCurrentContinuationEntryPoint(final int programPoint) {
 392         return isRestOf() && getCurrentContinuationEntryPoint() == programPoint;
 393     }
 394 
 395     private int[] getContinuationEntryPoints() {
 396         return isRestOf() ? continuationEntryPoints : null;
 397     }
 398 
 399     private int getCurrentContinuationEntryPoint() {
 400         return isRestOf() ? continuationEntryPoints[0] : INVALID_PROGRAM_POINT;
 401     }
 402 
 403     private boolean isContinuationEntryPoint(final int programPoint) {
 404         if (isRestOf()) {
 405             assert continuationEntryPoints != null;
 406             for (final int cep : continuationEntryPoints) {
 407                 if (cep == programPoint) {
 408                     return true;
 409                 }
 410             }
 411         }
 412         return false;
 413     }
 414 
 415     /**
 416      * Check if this symbol can be accessed directly with a putfield or getfield or dynamic load
 417      *
 418      * @param symbol symbol to check for fast scope
 419      * @return true if fast scope
 420      */
 421     private boolean isFastScope(final Symbol symbol) {
 422         if (!symbol.isScope()) {
 423             return false;
 424         }
 425 
 426         if (!lc.inDynamicScope()) {
 427             // If there's no with or eval in context, and the symbol is marked as scoped, it is fast scoped. Such a
 428             // symbol must either be global, or its defining block must need scope.
 429             assert symbol.isGlobal() || lc.getDefiningBlock(symbol).needsScope() : symbol.getName();
 430             return true;
 431         }
 432 
 433         if (symbol.isGlobal()) {
 434             // Shortcut: if there's a with or eval in context, globals can't be fast scoped
 435             return false;
 436         }
 437 
 438         // Otherwise, check if there's a dynamic scope between use of the symbol and its definition
 439         final String name = symbol.getName();
 440         boolean previousWasBlock = false;
 441         for (final Iterator<LexicalContextNode> it = lc.getAllNodes(); it.hasNext();) {
 442             final LexicalContextNode node = it.next();
 443             if (node instanceof Block) {
 444                 // If this block defines the symbol, then we can fast scope the symbol.
 445                 final Block block = (Block)node;
 446                 if (block.getExistingSymbol(name) == symbol) {
 447                     assert block.needsScope();
 448                     return true;
 449                 }
 450                 previousWasBlock = true;
 451             } else {
 452                 if (node instanceof WithNode && previousWasBlock || node instanceof FunctionNode && ((FunctionNode)node).needsDynamicScope()) {
 453                     // If we hit a scope that can have symbols introduced into it at run time before finding the defining
 454                     // block, the symbol can't be fast scoped. A WithNode only counts if we've immediately seen a block
 455                     // before - its block. Otherwise, we are currently processing the WithNode's expression, and that's
 456                     // obviously not subjected to introducing new symbols.
 457                     return false;
 458                 }
 459                 previousWasBlock = false;
 460             }
 461         }
 462         // Should've found the symbol defined in a block
 463         throw new AssertionError();
 464     }
 465 
 466     private MethodEmitter loadSharedScopeVar(final Type valueType, final Symbol symbol, final int flags) {
 467         assert !isOptimisticOrRestOf();
 468         if (isFastScope(symbol)) {
 469             method.load(getScopeProtoDepth(lc.getCurrentBlock(), symbol));
 470         } else {
 471             method.load(-1);
 472         }
 473         return lc.getScopeGet(unit, symbol, valueType, flags).generateInvoke(method);
 474     }
 475 
 476     private class LoadScopeVar extends OptimisticOperation {
 477         final IdentNode identNode;
 478         private final int flags;
 479 
 480         LoadScopeVar(final IdentNode identNode, final TypeBounds resultBounds, final int flags) {
 481             super(identNode, resultBounds);
 482             this.identNode = identNode;
 483             this.flags = flags;
 484         }
 485 
 486         @Override
 487         void loadStack() {
 488             method.loadCompilerConstant(SCOPE);
 489             getProto();
 490         }
 491 
 492         void getProto() {
 493             //empty
 494         }
 495 
 496         @Override
 497         void consumeStack() {
 498             // If this is either __FILE__, __DIR__, or __LINE__ then load the property initially as Object as we'd convert
 499             // it anyway for replaceLocationPropertyPlaceholder.
 500             if(identNode.isCompileTimePropertyName()) {
 501                 method.dynamicGet(Type.OBJECT, identNode.getSymbol().getName(), flags, identNode.isFunction(), false);
 502                 replaceCompileTimeProperty();
 503             } else {
 504                 dynamicGet(identNode.getSymbol().getName(), flags, identNode.isFunction(), false);
 505             }
 506         }
 507     }
 508 
 509     private class LoadFastScopeVar extends LoadScopeVar {
 510         LoadFastScopeVar(final IdentNode identNode, final TypeBounds resultBounds, final int flags) {
 511             super(identNode, resultBounds, flags);
 512         }
 513 
 514         @Override
 515         void getProto() {
 516             loadFastScopeProto(identNode.getSymbol(), false);
 517         }
 518     }
 519 
 520     private MethodEmitter storeFastScopeVar(final Symbol symbol, final int flags) {
 521         loadFastScopeProto(symbol, true);
 522         method.dynamicSet(symbol.getName(), flags, false);
 523         return method;
 524     }
 525 
 526     private int getScopeProtoDepth(final Block startingBlock, final Symbol symbol) {
 527         //walk up the chain from starting block and when we bump into the current function boundary, add the external
 528         //information.
 529         final FunctionNode fn   = lc.getCurrentFunction();
 530         final int externalDepth = compiler.getScriptFunctionData(fn.getId()).getExternalSymbolDepth(symbol.getName());
 531 
 532         //count the number of scopes from this place to the start of the function
 533 
 534         final int internalDepth = FindScopeDepths.findInternalDepth(lc, fn, startingBlock, symbol);
 535         final int scopesToStart = FindScopeDepths.findScopesToStart(lc, fn, startingBlock);
 536         int depth = 0;
 537         if (internalDepth == -1) {
 538             depth = scopesToStart + externalDepth;
 539         } else {
 540             assert internalDepth <= scopesToStart;
 541             depth = internalDepth;
 542         }
 543 
 544         return depth;
 545     }
 546 
 547     private void loadFastScopeProto(final Symbol symbol, final boolean swap) {
 548         final int depth = getScopeProtoDepth(lc.getCurrentBlock(), symbol);
 549         assert depth != -1 : "Couldn't find scope depth for symbol " + symbol.getName() + " in " + lc.getCurrentFunction();
 550         if (depth > 0) {
 551             if (swap) {
 552                 method.swap();
 553             }
 554             for (int i = 0; i < depth; i++) {
 555                 method.invoke(ScriptObject.GET_PROTO);
 556             }
 557             if (swap) {
 558                 method.swap();
 559             }
 560         }
 561     }
 562 
 563     /**
 564      * Generate code that loads this node to the stack, not constraining its type
 565      *
 566      * @param expr node to load
 567      *
 568      * @return the method emitter used
 569      */
 570     private MethodEmitter loadExpressionUnbounded(final Expression expr) {
 571         return loadExpression(expr, TypeBounds.UNBOUNDED);
 572     }
 573 
 574     private MethodEmitter loadExpressionAsObject(final Expression expr) {
 575         return loadExpression(expr, TypeBounds.OBJECT);
 576     }
 577 
 578     MethodEmitter loadExpressionAsBoolean(final Expression expr) {
 579         return loadExpression(expr, TypeBounds.BOOLEAN);
 580     }
 581 
 582     // Test whether conversion from source to target involves a call of ES 9.1 ToPrimitive
 583     // with possible side effects from calling an object's toString or valueOf methods.
 584     private static boolean noToPrimitiveConversion(final Type source, final Type target) {
 585         // Object to boolean conversion does not cause ToPrimitive call
 586         return source.isJSPrimitive() || !target.isJSPrimitive() || target.isBoolean();
 587     }
 588 
 589     MethodEmitter loadBinaryOperands(final BinaryNode binaryNode) {
 590         return loadBinaryOperands(binaryNode.lhs(), binaryNode.rhs(), TypeBounds.UNBOUNDED.notWiderThan(binaryNode.getWidestOperandType()), false, false);
 591     }
 592 
 593     private MethodEmitter loadBinaryOperands(final Expression lhs, final Expression rhs, final TypeBounds explicitOperandBounds, final boolean baseAlreadyOnStack, final boolean forceConversionSeparation) {
 594         // ECMAScript 5.1 specification (sections 11.5-11.11 and 11.13) prescribes that when evaluating a binary
 595         // expression "LEFT op RIGHT", the order of operations must be: LOAD LEFT, LOAD RIGHT, CONVERT LEFT, CONVERT
 596         // RIGHT, EXECUTE OP. Unfortunately, doing it in this order defeats potential optimizations that arise when we
 597         // can combine a LOAD with a CONVERT operation (e.g. use a dynamic getter with the conversion target type as its
 598         // return value). What we do here is reorder LOAD RIGHT and CONVERT LEFT when possible; it is possible only when
 599         // we can prove that executing CONVERT LEFT can't have a side effect that changes the value of LOAD RIGHT.
 600         // Basically, if we know that either LEFT already is a primitive value, or does not have to be converted to
 601         // a primitive value, or RIGHT is an expression that loads without side effects, then we can do the
 602         // reordering and collapse LOAD/CONVERT into a single operation; otherwise we need to do the more costly
 603         // separate operations to preserve specification semantics.
 604 
 605         // Operands' load type should not be narrower than the narrowest of the individual operand types, nor narrower
 606         // than the lower explicit bound, but it should also not be wider than
 607         final Type lhsType = undefinedToNumber(lhs.getType());
 608         final Type rhsType = undefinedToNumber(rhs.getType());
 609         final Type narrowestOperandType = Type.narrowest(Type.widest(lhsType, rhsType), explicitOperandBounds.widest);
 610         final TypeBounds operandBounds = explicitOperandBounds.notNarrowerThan(narrowestOperandType);
 611         if (noToPrimitiveConversion(lhsType, explicitOperandBounds.widest) || rhs.isLocal()) {
 612             // Can reorder. We might still need to separate conversion, but at least we can do it with reordering
 613             if (forceConversionSeparation) {
 614                 // Can reorder, but can't move conversion into the operand as the operation depends on operands
 615                 // exact types for its overflow guarantees. E.g. with {L}{%I}expr1 {L}* {L}{%I}expr2 we are not allowed
 616                 // to merge {L}{%I} into {%L}, as that can cause subsequent overflows; test for JDK-8058610 contains
 617                 // concrete cases where this could happen.
 618                 final TypeBounds safeConvertBounds = TypeBounds.UNBOUNDED.notNarrowerThan(narrowestOperandType);
 619                 loadExpression(lhs, safeConvertBounds, baseAlreadyOnStack);
 620                 method.convert(operandBounds.within(method.peekType()));
 621                 loadExpression(rhs, safeConvertBounds, false);
 622                 method.convert(operandBounds.within(method.peekType()));
 623             } else {
 624                 // Can reorder and move conversion into the operand. Combine load and convert into single operations.
 625                 loadExpression(lhs, operandBounds, baseAlreadyOnStack);
 626                 loadExpression(rhs, operandBounds, false);
 627             }
 628         } else {
 629             // Can't reorder. Load and convert separately.
 630             final TypeBounds safeConvertBounds = TypeBounds.UNBOUNDED.notNarrowerThan(narrowestOperandType);
 631             loadExpression(lhs, safeConvertBounds, baseAlreadyOnStack);
 632             final Type lhsLoadedType = method.peekType();
 633             loadExpression(rhs, safeConvertBounds, false);
 634             final Type convertedLhsType = operandBounds.within(method.peekType());
 635             if (convertedLhsType != lhsLoadedType) {
 636                 // Do it conditionally, so that if conversion is a no-op we don't introduce a SWAP, SWAP.
 637                 method.swap().convert(convertedLhsType).swap();
 638             }
 639             method.convert(operandBounds.within(method.peekType()));
 640         }
 641         assert Type.generic(method.peekType()) == operandBounds.narrowest;
 642         assert Type.generic(method.peekType(1)) == operandBounds.narrowest;
 643 
 644         return method;
 645     }
 646 
 647     /**
 648      * Similar to {@link #loadBinaryOperands(BinaryNode)} but used specifically for loading operands of
 649      * relational and equality comparison operators where at least one argument is non-object. (When both
 650      * arguments are objects, we use {@link ScriptRuntime#EQ(Object, Object)}, {@link ScriptRuntime#LT(Object, Object)}
 651      * etc. methods instead. Additionally, {@code ScriptRuntime} methods are used for strict (in)equality comparison
 652      * of a boolean to anything that isn't a boolean.) This method handles the special case where one argument
 653      * is an object and another is a primitive. Naively, these could also be delegated to {@code ScriptRuntime} methods
 654      * by boxing the primitive. However, in all such cases the comparison is performed on numeric values, so it is
 655      * possible to strength-reduce the operation by taking the number value of the object argument instead and
 656      * comparing that to the primitive value ("primitive" will always be int, long, double, or boolean, and booleans
 657      * compare as ints in these cases, so they're essentially numbers too). This method will emit code for loading
 658      * arguments for such strength-reduced comparison. When both arguments are primitives, it just delegates to
 659      * {@link #loadBinaryOperands(BinaryNode)}.
 660      *
 661      * @param cmp the comparison operation for which the operands need to be loaded on stack.
 662      * @return the current method emitter.
 663      */
 664     MethodEmitter loadComparisonOperands(final BinaryNode cmp) {
 665         final Expression lhs = cmp.lhs();
 666         final Expression rhs = cmp.rhs();
 667         final Type lhsType = lhs.getType();
 668         final Type rhsType = rhs.getType();
 669 
 670         // Only used when not both are object, for that we have ScriptRuntime.LT etc.
 671         assert !(lhsType.isObject() && rhsType.isObject());
 672 
 673         if (lhsType.isObject() || rhsType.isObject()) {
 674             // We can reorder CONVERT LEFT and LOAD RIGHT only if either the left is a primitive, or the right
 675             // is a local. This is more strict than loadBinaryNode reorder criteria, as it can allow JS primitive
 676             // types too (notably: String is a JS primitive, but not a JVM primitive). We disallow String otherwise
 677             // we would prematurely convert it to number when comparing to an optimistic expression, e.g. in
 678             // "Hello" === String("Hello") the RHS starts out as an optimistic-int function call. If we allowed
 679             // reordering, we'd end up with ToNumber("Hello") === {I%}String("Hello") that is obviously incorrect.
 680             final boolean canReorder = lhsType.isPrimitive() || rhs.isLocal();
 681             // If reordering is allowed, and we're using a relational operator (that is, <, <=, >, >=) and not an
 682             // (in)equality operator, then we encourage combining of LOAD and CONVERT into a single operation.
 683             // This is because relational operators' semantics prescribes vanilla ToNumber() conversion, while
 684             // (in)equality operators need the specialized JSType.toNumberFor[Strict]Equals. E.g. in the code snippet
 685             // "i < obj.size" (where i is primitive and obj.size is statically an object), ".size" will thus be allowed
 686             // to compile as:
 687             //   invokedynamic dyn:getProp|getElem|getMethod:size(Object;)D
 688             // instead of the more costly:
 689             //   invokedynamic dyn:getProp|getElem|getMethod:size(Object;)Object
 690             //   invokestatic JSType.toNumber(Object)D
 691             // Note also that even if this is allowed, we're only using it on operands that are non-optimistic, as
 692             // otherwise the logic for determining effective optimistic-ness would turn an optimistic double return
 693             // into a freely coercible one, which would be wrong.
 694             final boolean canCombineLoadAndConvert = canReorder && cmp.isRelational();
 695 
 696             // LOAD LEFT
 697             loadExpression(lhs, canCombineLoadAndConvert && !lhs.isOptimistic() ? TypeBounds.NUMBER : TypeBounds.UNBOUNDED);
 698 
 699             final Type lhsLoadedType = method.peekType();
 700             final TokenType tt = cmp.tokenType();
 701             if (canReorder) {
 702                 // Can reorder CONVERT LEFT and LOAD RIGHT
 703                 emitObjectToNumberComparisonConversion(method, tt);
 704                 loadExpression(rhs, canCombineLoadAndConvert && !rhs.isOptimistic() ? TypeBounds.NUMBER : TypeBounds.UNBOUNDED);
 705             } else {
 706                 // Can't reorder CONVERT LEFT and LOAD RIGHT
 707                 loadExpression(rhs, TypeBounds.UNBOUNDED);
 708                 if (lhsLoadedType != Type.NUMBER) {
 709                     method.swap();
 710                     emitObjectToNumberComparisonConversion(method, tt);
 711                     method.swap();
 712                 }
 713             }
 714 
 715             // CONVERT RIGHT
 716             emitObjectToNumberComparisonConversion(method, tt);
 717             return method;
 718         }
 719         // For primitive operands, just don't do anything special.
 720         return loadBinaryOperands(cmp);
 721     }
 722 
 723     private static void emitObjectToNumberComparisonConversion(final MethodEmitter method, final TokenType tt) {
 724         switch(tt) {
 725         case EQ:
 726         case NE:
 727             if (method.peekType().isObject()) {
 728                 TO_NUMBER_FOR_EQ.invoke(method);
 729                 return;
 730             }
 731             break;
 732         case EQ_STRICT:
 733         case NE_STRICT:
 734             if (method.peekType().isObject()) {
 735                 TO_NUMBER_FOR_STRICT_EQ.invoke(method);
 736                 return;
 737             }
 738             break;
 739         default:
 740             break;
 741         }
 742         method.convert(Type.NUMBER);
 743     }
 744 
 745     private static Type undefinedToNumber(final Type type) {
 746         return type == Type.UNDEFINED ? Type.NUMBER : type;
 747     }
 748 
 749     private static final class TypeBounds {
 750         final Type narrowest;
 751         final Type widest;
 752 
 753         static final TypeBounds UNBOUNDED = new TypeBounds(Type.UNKNOWN, Type.OBJECT);
 754         static final TypeBounds INT = exact(Type.INT);
 755         static final TypeBounds NUMBER = exact(Type.NUMBER);
 756         static final TypeBounds OBJECT = exact(Type.OBJECT);
 757         static final TypeBounds BOOLEAN = exact(Type.BOOLEAN);
 758 
 759         static TypeBounds exact(final Type type) {
 760             return new TypeBounds(type, type);
 761         }
 762 
 763         TypeBounds(final Type narrowest, final Type widest) {
 764             assert widest    != null && widest    != Type.UNDEFINED && widest != Type.UNKNOWN : widest;
 765             assert narrowest != null && narrowest != Type.UNDEFINED : narrowest;
 766             assert !narrowest.widerThan(widest) : narrowest + " wider than " + widest;
 767             assert !widest.narrowerThan(narrowest);
 768             this.narrowest = Type.generic(narrowest);
 769             this.widest = Type.generic(widest);
 770         }
 771 
 772         TypeBounds notNarrowerThan(final Type type) {
 773             return maybeNew(Type.narrowest(Type.widest(narrowest, type), widest), widest);
 774         }
 775 
 776         TypeBounds notWiderThan(final Type type) {
 777             return maybeNew(Type.narrowest(narrowest, type), Type.narrowest(widest, type));
 778         }
 779 
 780         boolean canBeNarrowerThan(final Type type) {
 781             return narrowest.narrowerThan(type);
 782         }
 783 
 784         TypeBounds maybeNew(final Type newNarrowest, final Type newWidest) {
 785             if(newNarrowest == narrowest && newWidest == widest) {
 786                 return this;
 787             }
 788             return new TypeBounds(newNarrowest, newWidest);
 789         }
 790 
 791         TypeBounds booleanToInt() {
 792             return maybeNew(CodeGenerator.booleanToInt(narrowest), CodeGenerator.booleanToInt(widest));
 793         }
 794 
 795         TypeBounds objectToNumber() {
 796             return maybeNew(CodeGenerator.objectToNumber(narrowest), CodeGenerator.objectToNumber(widest));
 797         }
 798 
 799         Type within(final Type type) {
 800             if(type.narrowerThan(narrowest)) {
 801                 return narrowest;
 802             }
 803             if(type.widerThan(widest)) {
 804                 return widest;
 805             }
 806             return type;
 807         }
 808 
 809         @Override
 810         public String toString() {
 811             return "[" + narrowest + ", " + widest + "]";
 812         }
 813     }
 814 
 815     private static Type booleanToInt(final Type t) {
 816         return t == Type.BOOLEAN ? Type.INT : t;
 817     }
 818 
 819     private static Type objectToNumber(final Type t) {
 820         return t.isObject() ? Type.NUMBER : t;
 821     }
 822 
 823     MethodEmitter loadExpressionAsType(final Expression expr, final Type type) {
 824         if(type == Type.BOOLEAN) {
 825             return loadExpressionAsBoolean(expr);
 826         } else if(type == Type.UNDEFINED) {
 827             assert expr.getType() == Type.UNDEFINED;
 828             return loadExpressionAsObject(expr);
 829         }
 830         // having no upper bound preserves semantics of optimistic operations in the expression (by not having them
 831         // converted early) and then applies explicit conversion afterwards.
 832         return loadExpression(expr, TypeBounds.UNBOUNDED.notNarrowerThan(type)).convert(type);
 833     }
 834 
 835     private MethodEmitter loadExpression(final Expression expr, final TypeBounds resultBounds) {
 836         return loadExpression(expr, resultBounds, false);
 837     }
 838 
 839     /**
 840      * Emits code for evaluating an expression and leaving its value on top of the stack, narrowing or widening it if
 841      * necessary.
 842      * @param expr the expression to load
 843      * @param resultBounds the incoming type bounds. The value on the top of the stack is guaranteed to not be of narrower
 844      * type than the narrowest bound, or wider type than the widest bound after it is loaded.
 845      * @param baseAlreadyOnStack true if the base of an access or index node is already on the stack. Used to avoid
 846      * double evaluation of bases in self-assignment expressions to access and index nodes. {@code Type.OBJECT} is used
 847      * to indicate the widest possible type.
 848      * @return the method emitter
 849      */
 850     private MethodEmitter loadExpression(final Expression expr, final TypeBounds resultBounds, final boolean baseAlreadyOnStack) {
 851 
 852         /*
 853          * The load may be of type IdentNode, e.g. "x", AccessNode, e.g. "x.y"
 854          * or IndexNode e.g. "x[y]". Both AccessNodes and IndexNodes are
 855          * BaseNodes and the logic for loading the base object is reused
 856          */
 857         final CodeGenerator codegen = this;
 858 
 859         final boolean isCurrentDiscard = codegen.lc.isCurrentDiscard(expr);
 860         expr.accept(new NodeOperatorVisitor<LexicalContext>(new LexicalContext()) {
 861             @Override
 862             public boolean enterIdentNode(final IdentNode identNode) {
 863                 loadIdent(identNode, resultBounds);
 864                 return false;
 865             }
 866 
 867             @Override
 868             public boolean enterAccessNode(final AccessNode accessNode) {
 869                 new OptimisticOperation(accessNode, resultBounds) {
 870                     @Override
 871                     void loadStack() {
 872                         if (!baseAlreadyOnStack) {
 873                             loadExpressionAsObject(accessNode.getBase());
 874                         }
 875                         assert method.peekType().isObject();
 876                     }
 877                     @Override
 878                     void consumeStack() {
 879                         final int flags = getCallSiteFlags();
 880                         dynamicGet(accessNode.getProperty(), flags, accessNode.isFunction(), accessNode.isIndex());
 881                     }
 882                 }.emit(baseAlreadyOnStack ? 1 : 0);
 883                 return false;
 884             }
 885 
 886             @Override
 887             public boolean enterIndexNode(final IndexNode indexNode) {
 888                 new OptimisticOperation(indexNode, resultBounds) {
 889                     @Override
 890                     void loadStack() {
 891                         if (!baseAlreadyOnStack) {
 892                             loadExpressionAsObject(indexNode.getBase());
 893                             loadExpressionUnbounded(indexNode.getIndex());
 894                         }
 895                     }
 896                     @Override
 897                     void consumeStack() {
 898                         final int flags = getCallSiteFlags();
 899                         dynamicGetIndex(flags, indexNode.isFunction());
 900                     }
 901                 }.emit(baseAlreadyOnStack ? 2 : 0);
 902                 return false;
 903             }
 904 
 905             @Override
 906             public boolean enterFunctionNode(final FunctionNode functionNode) {
 907                 // function nodes will always leave a constructed function object on stack, no need to load the symbol
 908                 // separately as in enterDefault()
 909                 lc.pop(functionNode);
 910                 functionNode.accept(codegen);
 911                 // NOTE: functionNode.accept() will produce a different FunctionNode that we discard. This incidentally
 912                 // doesn't cause problems as we're never touching FunctionNode again after it's visited here - codegen
 913                 // is the last element in the compilation pipeline, the AST it produces is not used externally. So, we
 914                 // re-push the original functionNode.
 915                 lc.push(functionNode);
 916                 return false;
 917             }
 918 
 919             @Override
 920             public boolean enterASSIGN(final BinaryNode binaryNode) {
 921                 checkAssignTarget(binaryNode.lhs());
 922                 loadASSIGN(binaryNode);
 923                 return false;
 924             }
 925 
 926             @Override
 927             public boolean enterASSIGN_ADD(final BinaryNode binaryNode) {
 928                 checkAssignTarget(binaryNode.lhs());
 929                 loadASSIGN_ADD(binaryNode);
 930                 return false;
 931             }
 932 
 933             @Override
 934             public boolean enterASSIGN_BIT_AND(final BinaryNode binaryNode) {
 935                 checkAssignTarget(binaryNode.lhs());
 936                 loadASSIGN_BIT_AND(binaryNode);
 937                 return false;
 938             }
 939 
 940             @Override
 941             public boolean enterASSIGN_BIT_OR(final BinaryNode binaryNode) {
 942                 checkAssignTarget(binaryNode.lhs());
 943                 loadASSIGN_BIT_OR(binaryNode);
 944                 return false;
 945             }
 946 
 947             @Override
 948             public boolean enterASSIGN_BIT_XOR(final BinaryNode binaryNode) {
 949                 checkAssignTarget(binaryNode.lhs());
 950                 loadASSIGN_BIT_XOR(binaryNode);
 951                 return false;
 952             }
 953 
 954             @Override
 955             public boolean enterASSIGN_DIV(final BinaryNode binaryNode) {
 956                 checkAssignTarget(binaryNode.lhs());
 957                 loadASSIGN_DIV(binaryNode);
 958                 return false;
 959             }
 960 
 961             @Override
 962             public boolean enterASSIGN_MOD(final BinaryNode binaryNode) {
 963                 checkAssignTarget(binaryNode.lhs());
 964                 loadASSIGN_MOD(binaryNode);
 965                 return false;
 966             }
 967 
 968             @Override
 969             public boolean enterASSIGN_MUL(final BinaryNode binaryNode) {
 970                 checkAssignTarget(binaryNode.lhs());
 971                 loadASSIGN_MUL(binaryNode);
 972                 return false;
 973             }
 974 
 975             @Override
 976             public boolean enterASSIGN_SAR(final BinaryNode binaryNode) {
 977                 checkAssignTarget(binaryNode.lhs());
 978                 loadASSIGN_SAR(binaryNode);
 979                 return false;
 980             }
 981 
 982             @Override
 983             public boolean enterASSIGN_SHL(final BinaryNode binaryNode) {
 984                 checkAssignTarget(binaryNode.lhs());
 985                 loadASSIGN_SHL(binaryNode);
 986                 return false;
 987             }
 988 
 989             @Override
 990             public boolean enterASSIGN_SHR(final BinaryNode binaryNode) {
 991                 checkAssignTarget(binaryNode.lhs());
 992                 loadASSIGN_SHR(binaryNode);
 993                 return false;
 994             }
 995 
 996             @Override
 997             public boolean enterASSIGN_SUB(final BinaryNode binaryNode) {
 998                 checkAssignTarget(binaryNode.lhs());
 999                 loadASSIGN_SUB(binaryNode);
1000                 return false;
1001             }
1002 
1003             @Override
1004             public boolean enterCallNode(final CallNode callNode) {
1005                 return loadCallNode(callNode, resultBounds);
1006             }
1007 
1008             @Override
1009             public boolean enterLiteralNode(final LiteralNode<?> literalNode) {
1010                 loadLiteral(literalNode, resultBounds);
1011                 return false;
1012             }
1013 
1014             @Override
1015             public boolean enterTernaryNode(final TernaryNode ternaryNode) {
1016                 loadTernaryNode(ternaryNode, resultBounds);
1017                 return false;
1018             }
1019 
1020             @Override
1021             public boolean enterADD(final BinaryNode binaryNode) {
1022                 loadADD(binaryNode, resultBounds);
1023                 return false;
1024             }
1025 
1026             @Override
1027             public boolean enterSUB(final UnaryNode unaryNode) {
1028                 loadSUB(unaryNode, resultBounds);
1029                 return false;
1030             }
1031 
1032             @Override
1033             public boolean enterSUB(final BinaryNode binaryNode) {
1034                 loadSUB(binaryNode, resultBounds);
1035                 return false;
1036             }
1037 
1038             @Override
1039             public boolean enterMUL(final BinaryNode binaryNode) {
1040                 loadMUL(binaryNode, resultBounds);
1041                 return false;
1042             }
1043 
1044             @Override
1045             public boolean enterDIV(final BinaryNode binaryNode) {
1046                 loadDIV(binaryNode, resultBounds);
1047                 return false;
1048             }
1049 
1050             @Override
1051             public boolean enterMOD(final BinaryNode binaryNode) {
1052                 loadMOD(binaryNode, resultBounds);
1053                 return false;
1054             }
1055 
1056             @Override
1057             public boolean enterSAR(final BinaryNode binaryNode) {
1058                 loadSAR(binaryNode);
1059                 return false;
1060             }
1061 
1062             @Override
1063             public boolean enterSHL(final BinaryNode binaryNode) {
1064                 loadSHL(binaryNode);
1065                 return false;
1066             }
1067 
1068             @Override
1069             public boolean enterSHR(final BinaryNode binaryNode) {
1070                 loadSHR(binaryNode);
1071                 return false;
1072             }
1073 
1074             @Override
1075             public boolean enterCOMMALEFT(final BinaryNode binaryNode) {
1076                 loadCOMMALEFT(binaryNode, resultBounds);
1077                 return false;
1078             }
1079 
1080             @Override
1081             public boolean enterCOMMARIGHT(final BinaryNode binaryNode) {
1082                 loadCOMMARIGHT(binaryNode, resultBounds);
1083                 return false;
1084             }
1085 
1086             @Override
1087             public boolean enterAND(final BinaryNode binaryNode) {
1088                 loadAND_OR(binaryNode, resultBounds, true);
1089                 return false;
1090             }
1091 
1092             @Override
1093             public boolean enterOR(final BinaryNode binaryNode) {
1094                 loadAND_OR(binaryNode, resultBounds, false);
1095                 return false;
1096             }
1097 
1098             @Override
1099             public boolean enterNOT(final UnaryNode unaryNode) {
1100                 loadNOT(unaryNode);
1101                 return false;
1102             }
1103 
1104             @Override
1105             public boolean enterADD(final UnaryNode unaryNode) {
1106                 loadADD(unaryNode, resultBounds);
1107                 return false;
1108             }
1109 
1110             @Override
1111             public boolean enterBIT_NOT(final UnaryNode unaryNode) {
1112                 loadBIT_NOT(unaryNode);
1113                 return false;
1114             }
1115 
1116             @Override
1117             public boolean enterBIT_AND(final BinaryNode binaryNode) {
1118                 loadBIT_AND(binaryNode);
1119                 return false;
1120             }
1121 
1122             @Override
1123             public boolean enterBIT_OR(final BinaryNode binaryNode) {
1124                 loadBIT_OR(binaryNode);
1125                 return false;
1126             }
1127 
1128             @Override
1129             public boolean enterBIT_XOR(final BinaryNode binaryNode) {
1130                 loadBIT_XOR(binaryNode);
1131                 return false;
1132             }
1133 
1134             @Override
1135             public boolean enterVOID(final UnaryNode unaryNode) {
1136                 loadVOID(unaryNode, resultBounds);
1137                 return false;
1138             }
1139 
1140             @Override
1141             public boolean enterEQ(final BinaryNode binaryNode) {
1142                 loadCmp(binaryNode, Condition.EQ);
1143                 return false;
1144             }
1145 
1146             @Override
1147             public boolean enterEQ_STRICT(final BinaryNode binaryNode) {
1148                 loadCmp(binaryNode, Condition.EQ);
1149                 return false;
1150             }
1151 
1152             @Override
1153             public boolean enterGE(final BinaryNode binaryNode) {
1154                 loadCmp(binaryNode, Condition.GE);
1155                 return false;
1156             }
1157 
1158             @Override
1159             public boolean enterGT(final BinaryNode binaryNode) {
1160                 loadCmp(binaryNode, Condition.GT);
1161                 return false;
1162             }
1163 
1164             @Override
1165             public boolean enterLE(final BinaryNode binaryNode) {
1166                 loadCmp(binaryNode, Condition.LE);
1167                 return false;
1168             }
1169 
1170             @Override
1171             public boolean enterLT(final BinaryNode binaryNode) {
1172                 loadCmp(binaryNode, Condition.LT);
1173                 return false;
1174             }
1175 
1176             @Override
1177             public boolean enterNE(final BinaryNode binaryNode) {
1178                 loadCmp(binaryNode, Condition.NE);
1179                 return false;
1180             }
1181 
1182             @Override
1183             public boolean enterNE_STRICT(final BinaryNode binaryNode) {
1184                 loadCmp(binaryNode, Condition.NE);
1185                 return false;
1186             }
1187 
1188             @Override
1189             public boolean enterObjectNode(final ObjectNode objectNode) {
1190                 loadObjectNode(objectNode);
1191                 return false;
1192             }
1193 
1194             @Override
1195             public boolean enterRuntimeNode(final RuntimeNode runtimeNode) {
1196                 loadRuntimeNode(runtimeNode);
1197                 return false;
1198             }
1199 
1200             @Override
1201             public boolean enterNEW(final UnaryNode unaryNode) {
1202                 loadNEW(unaryNode);
1203                 return false;
1204             }
1205 
1206             @Override
1207             public boolean enterDECINC(final UnaryNode unaryNode) {
1208                 checkAssignTarget(unaryNode.getExpression());
1209                 loadDECINC(unaryNode);
1210                 return false;
1211             }
1212 
1213             @Override
1214             public boolean enterJoinPredecessorExpression(final JoinPredecessorExpression joinExpr) {
1215                 loadMaybeDiscard(joinExpr, joinExpr.getExpression(), resultBounds);
1216                 return false;
1217             }
1218 
1219             @Override
1220             public boolean enterGetSplitState(final GetSplitState getSplitState) {
1221                 method.loadScope();
1222                 method.invoke(Scope.GET_SPLIT_STATE);
1223                 return false;
1224             }
1225 
1226             @Override
1227             public boolean enterDefault(final Node otherNode) {
1228                 // Must have handled all expressions that can legally be encountered.
1229                 throw new AssertionError(otherNode.getClass().getName());
1230             }
1231         });
1232         if(!isCurrentDiscard) {
1233             coerceStackTop(resultBounds);
1234         }
1235         return method;
1236     }
1237 
1238     private MethodEmitter coerceStackTop(final TypeBounds typeBounds) {
1239         return method.convert(typeBounds.within(method.peekType()));
1240     }
1241 
1242     /**
1243      * Closes any still open entries for this block's local variables in the bytecode local variable table.
1244      *
1245      * @param block block containing symbols.
1246      */
1247     private void closeBlockVariables(final Block block) {
1248         for (final Symbol symbol : block.getSymbols()) {
1249             if (symbol.isBytecodeLocal()) {
1250                 method.closeLocalVariable(symbol, block.getBreakLabel());
1251             }
1252         }
1253     }
1254 
1255     @Override
1256     public boolean enterBlock(final Block block) {
1257         final Label entryLabel = block.getEntryLabel();
1258         if (entryLabel.isBreakTarget()) {
1259             // Entry label is a break target only for an inlined finally block.
1260             assert !method.isReachable();
1261             method.breakLabel(entryLabel, lc.getUsedSlotCount());
1262         } else {
1263             method.label(entryLabel);
1264         }
1265         if(!method.isReachable()) {
1266             return false;
1267         }
1268         if(lc.isFunctionBody() && emittedMethods.contains(lc.getCurrentFunction().getName())) {
1269             return false;
1270         }
1271         initLocals(block);
1272 
1273         assert lc.getUsedSlotCount() == method.getFirstTemp();
1274         return true;
1275     }
1276 
1277     boolean useOptimisticTypes() {
1278         return !lc.inSplitNode() && compiler.useOptimisticTypes();
1279     }
1280 
1281     @Override
1282     public Node leaveBlock(final Block block) {
1283         popBlockScope(block);
1284         method.beforeJoinPoint(block);
1285 
1286         closeBlockVariables(block);
1287         lc.releaseSlots();
1288         assert !method.isReachable() || (lc.isFunctionBody() ? 0 : lc.getUsedSlotCount()) == method.getFirstTemp() :
1289             "reachable="+method.isReachable() +
1290             " isFunctionBody=" + lc.isFunctionBody() +
1291             " usedSlotCount=" + lc.getUsedSlotCount() +
1292             " firstTemp=" + method.getFirstTemp();
1293 
1294         return block;
1295     }
1296 
1297     private void popBlockScope(final Block block) {
1298         final Label breakLabel = block.getBreakLabel();
1299 
1300         if(!block.needsScope() || lc.isFunctionBody()) {
1301             emitBlockBreakLabel(breakLabel);
1302             return;
1303         }
1304 
1305         final Label beginTryLabel = scopeEntryLabels.pop();
1306         final Label recoveryLabel = new Label("block_popscope_catch");
1307         emitBlockBreakLabel(breakLabel);
1308         final boolean bodyCanThrow = breakLabel.isAfter(beginTryLabel);
1309         if(bodyCanThrow) {
1310             method._try(beginTryLabel, breakLabel, recoveryLabel);
1311         }
1312 
1313         Label afterCatchLabel = null;
1314 
1315         if(method.isReachable()) {
1316             popScope();
1317             if(bodyCanThrow) {
1318                 afterCatchLabel = new Label("block_after_catch");
1319                 method._goto(afterCatchLabel);
1320             }
1321         }
1322 
1323         if(bodyCanThrow) {
1324             assert !method.isReachable();
1325             method._catch(recoveryLabel);
1326             popScopeException();
1327             method.athrow();
1328         }
1329         if(afterCatchLabel != null) {
1330             method.label(afterCatchLabel);
1331         }
1332     }
1333 
1334     private void emitBlockBreakLabel(final Label breakLabel) {
1335         // TODO: this is totally backwards. Block should not be breakable, LabelNode should be breakable.
1336         final LabelNode labelNode = lc.getCurrentBlockLabelNode();
1337         if(labelNode != null) {
1338             // Only have conversions if we're reachable
1339             assert labelNode.getLocalVariableConversion() == null || method.isReachable();
1340             method.beforeJoinPoint(labelNode);
1341             method.breakLabel(breakLabel, labeledBlockBreakLiveLocals.pop());
1342         } else {
1343             method.label(breakLabel);
1344         }
1345     }
1346 
1347     private void popScope() {
1348         popScopes(1);
1349     }
1350 
1351     /**
1352      * Pop scope as part of an exception handler. Similar to {@code popScope()} but also takes care of adjusting the
1353      * number of scopes that needs to be popped in case a rest-of continuation handler encounters an exception while
1354      * performing a ToPrimitive conversion.
1355      */
1356     private void popScopeException() {
1357         popScope();
1358         final ContinuationInfo ci = getContinuationInfo();
1359         if(ci != null) {
1360             final Label catchLabel = ci.catchLabel;
1361             if(catchLabel != METHOD_BOUNDARY && catchLabel == catchLabels.peek()) {
1362                 ++ci.exceptionScopePops;
1363             }
1364         }
1365     }
1366 
1367     private void popScopesUntil(final LexicalContextNode until) {
1368         popScopes(lc.getScopeNestingLevelTo(until));
1369     }
1370 
1371     private void popScopes(final int count) {
1372         if(count == 0) {
1373             return;
1374         }
1375         assert count > 0; // together with count == 0 check, asserts nonnegative count
1376         if (!method.hasScope()) {
1377             // We can sometimes invoke this method even if the method has no slot for the scope object. Typical example:
1378             // for(;;) { with({}) { break; } }. WithNode normally creates a scope, but if it uses no identifiers and
1379             // nothing else forces creation of a scope in the method, we just won't have the :scope local variable.
1380             return;
1381         }
1382         method.loadCompilerConstant(SCOPE);
1383         for(int i = 0; i < count; ++i) {
1384             method.invoke(ScriptObject.GET_PROTO);
1385         }
1386         method.storeCompilerConstant(SCOPE);
1387     }
1388 
1389     @Override
1390     public boolean enterBreakNode(final BreakNode breakNode) {
1391         return enterJumpStatement(breakNode);
1392     }
1393 
1394     @Override
1395     public boolean enterJumpToInlinedFinally(final JumpToInlinedFinally jumpToInlinedFinally) {
1396         return enterJumpStatement(jumpToInlinedFinally);
1397     }
1398 
1399     private boolean enterJumpStatement(final JumpStatement jump) {
1400         if(!method.isReachable()) {
1401             return false;
1402         }
1403         enterStatement(jump);
1404 
1405         method.beforeJoinPoint(jump);
1406         popScopesUntil(jump.getPopScopeLimit(lc));
1407         final Label targetLabel = jump.getTargetLabel(lc);
1408         targetLabel.markAsBreakTarget();
1409         method._goto(targetLabel);
1410 
1411         return false;
1412     }
1413 
1414     private int loadArgs(final List<Expression> args) {
1415         final int argCount = args.size();
1416         // arg have already been converted to objects here.
1417         if (argCount > LinkerCallSite.ARGLIMIT) {
1418             loadArgsArray(args);
1419             return 1;
1420         }
1421 
1422         for (final Expression arg : args) {
1423             assert arg != null;
1424             loadExpressionUnbounded(arg);
1425         }
1426         return argCount;
1427     }
1428 
1429     private boolean loadCallNode(final CallNode callNode, final TypeBounds resultBounds) {
1430         lineNumber(callNode.getLineNumber());
1431 
1432         final List<Expression> args = callNode.getArgs();
1433         final Expression function = callNode.getFunction();
1434         final Block currentBlock = lc.getCurrentBlock();
1435         final CodeGeneratorLexicalContext codegenLexicalContext = lc;
1436 
1437         function.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
1438 
1439             private MethodEmitter sharedScopeCall(final IdentNode identNode, final int flags) {
1440                 final Symbol symbol = identNode.getSymbol();
1441                 final boolean isFastScope = isFastScope(symbol);
1442                 new OptimisticOperation(callNode, resultBounds) {
1443                     @Override
1444                     void loadStack() {
1445                         method.loadCompilerConstant(SCOPE);
1446                         if (isFastScope) {
1447                             method.load(getScopeProtoDepth(currentBlock, symbol));
1448                         } else {
1449                             method.load(-1); // Bypass fast-scope code in shared callsite
1450                         }
1451                         loadArgs(args);
1452                     }
1453                     @Override
1454                     void consumeStack() {
1455                         final Type[] paramTypes = method.getTypesFromStack(args.size());
1456                         // We have trouble finding e.g. in Type.typeFor(asm.Type) because it can't see the Context class
1457                         // loader, so we need to weaken reference signatures to Object.
1458                         for(int i = 0; i < paramTypes.length; ++i) {
1459                             paramTypes[i] = Type.generic(paramTypes[i]);
1460                         }
1461                         // As shared scope calls are only used in non-optimistic compilation, we switch from using
1462                         // TypeBounds to just a single definitive type, resultBounds.widest.
1463                         final SharedScopeCall scopeCall = codegenLexicalContext.getScopeCall(unit, symbol,
1464                                 identNode.getType(), resultBounds.widest, paramTypes, flags);
1465                         scopeCall.generateInvoke(method);
1466                     }
1467                 }.emit();
1468                 return method;
1469             }
1470 
1471             private void scopeCall(final IdentNode ident, final int flags) {
1472                 new OptimisticOperation(callNode, resultBounds) {
1473                     int argsCount;
1474                     @Override
1475                     void loadStack() {
1476                         loadExpressionAsObject(ident); // foo() makes no sense if foo == 3
1477                         // ScriptFunction will see CALLSITE_SCOPE and will bind scope accordingly.
1478                         method.loadUndefined(Type.OBJECT); //the 'this'
1479                         argsCount = loadArgs(args);
1480                     }
1481                     @Override
1482                     void consumeStack() {
1483                         dynamicCall(2 + argsCount, flags, ident.getName());
1484                     }
1485                 }.emit();
1486             }
1487 
1488             private void evalCall(final IdentNode ident, final int flags) {
1489                 final Label invoke_direct_eval  = new Label("invoke_direct_eval");
1490                 final Label is_not_eval  = new Label("is_not_eval");
1491                 final Label eval_done = new Label("eval_done");
1492 
1493                 new OptimisticOperation(callNode, resultBounds) {
1494                     int argsCount;
1495                     @Override
1496                     void loadStack() {
1497                         /**
1498                          * We want to load 'eval' to check if it is indeed global builtin eval.
1499                          * If this eval call is inside a 'with' statement, dyn:getMethod|getProp|getElem
1500                          * would be generated if ident is a "isFunction". But, that would result in a
1501                          * bound function from WithObject. We don't want that as bound function as that
1502                          * won't be detected as builtin eval. So, we make ident as "not a function" which
1503                          * results in "dyn:getProp|getElem|getMethod" being generated and so WithObject
1504                          * would return unbounded eval function.
1505                          *
1506                          * Example:
1507                          *
1508                          *  var global = this;
1509                          *  function func() {
1510                          *      with({ eval: global.eval) { eval("var x = 10;") }
1511                          *  }
1512                          */
1513                         loadExpressionAsObject(ident.setIsNotFunction()); // Type.OBJECT as foo() makes no sense if foo == 3
1514                         globalIsEval();
1515                         method.ifeq(is_not_eval);
1516 
1517                         // Load up self (scope).
1518                         method.loadCompilerConstant(SCOPE);
1519                         final List<Expression> evalArgs = callNode.getEvalArgs().getArgs();
1520                         // load evaluated code
1521                         loadExpressionAsObject(evalArgs.get(0));
1522                         // load second and subsequent args for side-effect
1523                         final int numArgs = evalArgs.size();
1524                         for (int i = 1; i < numArgs; i++) {
1525                             loadAndDiscard(evalArgs.get(i));
1526                         }
1527                         method._goto(invoke_direct_eval);
1528 
1529                         method.label(is_not_eval);
1530                         // load this time but with dyn:getMethod|getProp|getElem
1531                         loadExpressionAsObject(ident); // Type.OBJECT as foo() makes no sense if foo == 3
1532                         // This is some scope 'eval' or global eval replaced by user
1533                         // but not the built-in ECMAScript 'eval' function call
1534                         method.loadNull();
1535                         argsCount = loadArgs(callNode.getArgs());
1536                     }
1537 
1538                     @Override
1539                     void consumeStack() {
1540                         // Ordinary call
1541                         dynamicCall(2 + argsCount, flags, "eval");
1542                         method._goto(eval_done);
1543 
1544                         method.label(invoke_direct_eval);
1545                         // Special/extra 'eval' arguments. These can be loaded late (in consumeStack) as we know none of
1546                         // them can ever be optimistic.
1547                         method.loadCompilerConstant(THIS);
1548                         method.load(callNode.getEvalArgs().getLocation());
1549                         method.load(CodeGenerator.this.lc.getCurrentFunction().isStrict());
1550                         // direct call to Global.directEval
1551                         globalDirectEval();
1552                         convertOptimisticReturnValue();
1553                         coerceStackTop(resultBounds);
1554                     }
1555                 }.emit();
1556 
1557                 method.label(eval_done);
1558             }
1559 
1560             @Override
1561             public boolean enterIdentNode(final IdentNode node) {
1562                 final Symbol symbol = node.getSymbol();
1563 
1564                 if (symbol.isScope()) {
1565                     final int flags = getScopeCallSiteFlags(symbol);
1566                     final int useCount = symbol.getUseCount();
1567 
1568                     // Threshold for generating shared scope callsite is lower for fast scope symbols because we know
1569                     // we can dial in the correct scope. However, we also need to enable it for non-fast scopes to
1570                     // support huge scripts like mandreel.js.
1571                     if (callNode.isEval()) {
1572                         evalCall(node, flags);
1573                     } else if (useCount <= SharedScopeCall.FAST_SCOPE_CALL_THRESHOLD
1574                             || !isFastScope(symbol) && useCount <= SharedScopeCall.SLOW_SCOPE_CALL_THRESHOLD
1575                             || CodeGenerator.this.lc.inDynamicScope()
1576                             || isOptimisticOrRestOf()) {
1577                         scopeCall(node, flags);
1578                     } else {
1579                         sharedScopeCall(node, flags);
1580                     }
1581                     assert method.peekType().equals(resultBounds.within(callNode.getType())) : method.peekType() + " != " + resultBounds + "(" + callNode.getType() + ")";
1582                 } else {
1583                     enterDefault(node);
1584                 }
1585 
1586                 return false;
1587             }
1588 
1589             @Override
1590             public boolean enterAccessNode(final AccessNode node) {
1591                 //check if this is an apply to call node. only real applies, that haven't been
1592                 //shadowed from their way to the global scope counts
1593 
1594                 //call nodes have program points.
1595 
1596                 final int flags = getCallSiteFlags() | (callNode.isApplyToCall() ? CALLSITE_APPLY_TO_CALL : 0);
1597 
1598                 new OptimisticOperation(callNode, resultBounds) {
1599                     int argCount;
1600                     @Override
1601                     void loadStack() {
1602                         loadExpressionAsObject(node.getBase());
1603                         method.dup();
1604                         // NOTE: not using a nested OptimisticOperation on this dynamicGet, as we expect to get back
1605                         // a callable object. Nobody in their right mind would optimistically type this call site.
1606                         assert !node.isOptimistic();
1607                         method.dynamicGet(node.getType(), node.getProperty(), flags, true, node.isIndex());
1608                         method.swap();
1609                         argCount = loadArgs(args);
1610                     }
1611                     @Override
1612                     void consumeStack() {
1613                         dynamicCall(2 + argCount, flags, node.getProperty());
1614                     }
1615                 }.emit();
1616 
1617                 return false;
1618             }
1619 
1620             @Override
1621             public boolean enterFunctionNode(final FunctionNode origCallee) {
1622                 new OptimisticOperation(callNode, resultBounds) {
1623                     FunctionNode callee;
1624                     int argsCount;
1625                     @Override
1626                     void loadStack() {
1627                         callee = (FunctionNode)origCallee.accept(CodeGenerator.this);
1628                         if (callee.isStrict()) { // "this" is undefined
1629                             method.loadUndefined(Type.OBJECT);
1630                         } else { // get global from scope (which is the self)
1631                             globalInstance();
1632                         }
1633                         argsCount = loadArgs(args);
1634                     }
1635 
1636                     @Override
1637                     void consumeStack() {
1638                         final int flags = getCallSiteFlags();
1639                         //assert callNodeType.equals(callee.getReturnType()) : callNodeType + " != " + callee.getReturnType();
1640                         dynamicCall(2 + argsCount, flags, origCallee.getName());
1641                     }
1642                 }.emit();
1643                 return false;
1644             }
1645 
1646             @Override
1647             public boolean enterIndexNode(final IndexNode node) {
1648                 new OptimisticOperation(callNode, resultBounds) {
1649                     int argsCount;
1650                     @Override
1651                     void loadStack() {
1652                         loadExpressionAsObject(node.getBase());
1653                         method.dup();
1654                         final Type indexType = node.getIndex().getType();
1655                         if (indexType.isObject() || indexType.isBoolean()) {
1656                             loadExpressionAsObject(node.getIndex()); //TODO boolean
1657                         } else {
1658                             loadExpressionUnbounded(node.getIndex());
1659                         }
1660                         // NOTE: not using a nested OptimisticOperation on this dynamicGetIndex, as we expect to get
1661                         // back a callable object. Nobody in their right mind would optimistically type this call site.
1662                         assert !node.isOptimistic();
1663                         method.dynamicGetIndex(node.getType(), getCallSiteFlags(), true);
1664                         method.swap();
1665                         argsCount = loadArgs(args);
1666                     }
1667                     @Override
1668                     void consumeStack() {
1669                         final int flags = getCallSiteFlags();
1670                         dynamicCall(2 + argsCount, flags, null);
1671                     }
1672                 }.emit();
1673                 return false;
1674             }
1675 
1676             @Override
1677             protected boolean enterDefault(final Node node) {
1678                 new OptimisticOperation(callNode, resultBounds) {
1679                     int argsCount;
1680                     @Override
1681                     void loadStack() {
1682                         // Load up function.
1683                         loadExpressionAsObject(function); //TODO, e.g. booleans can be used as functions
1684                         method.loadUndefined(Type.OBJECT); // ScriptFunction will figure out the correct this when it sees CALLSITE_SCOPE
1685                         argsCount = loadArgs(args);
1686                         }
1687                         @Override
1688                         void consumeStack() {
1689                             final int flags = getCallSiteFlags() | CALLSITE_SCOPE;
1690                             dynamicCall(2 + argsCount, flags, null);
1691                         }
1692                 }.emit();
1693                 return false;
1694             }
1695         });
1696 
1697         return false;
1698     }
1699 
1700     /**
1701      * Returns the flags with optimistic flag and program point removed.
1702      * @param flags the flags that need optimism stripped from them.
1703      * @return flags without optimism
1704      */
1705     static int nonOptimisticFlags(final int flags) {
1706         return flags & ~(CALLSITE_OPTIMISTIC | -1 << CALLSITE_PROGRAM_POINT_SHIFT);
1707     }
1708 
1709     @Override
1710     public boolean enterContinueNode(final ContinueNode continueNode) {
1711         return enterJumpStatement(continueNode);
1712     }
1713 
1714     @Override
1715     public boolean enterEmptyNode(final EmptyNode emptyNode) {
1716         // Don't even record the line number, it's irrelevant as there's no code.
1717         return false;
1718     }
1719 
1720     @Override
1721     public boolean enterExpressionStatement(final ExpressionStatement expressionStatement) {
1722         if(!method.isReachable()) {
1723             return false;
1724         }
1725         enterStatement(expressionStatement);
1726 
1727         loadAndDiscard(expressionStatement.getExpression());
1728         assert method.getStackSize() == 0;
1729 
1730         return false;
1731     }
1732 
1733     @Override
1734     public boolean enterBlockStatement(final BlockStatement blockStatement) {
1735         if(!method.isReachable()) {
1736             return false;
1737         }
1738         enterStatement(blockStatement);
1739 
1740         blockStatement.getBlock().accept(this);
1741 
1742         return false;
1743     }
1744 
1745     @Override
1746     public boolean enterForNode(final ForNode forNode) {
1747         if(!method.isReachable()) {
1748             return false;
1749         }
1750         enterStatement(forNode);
1751         if (forNode.isForIn()) {
1752             enterForIn(forNode);
1753         } else {
1754             final Expression init = forNode.getInit();
1755             if (init != null) {
1756                 loadAndDiscard(init);
1757             }
1758             enterForOrWhile(forNode, forNode.getModify());
1759         }
1760 
1761         return false;
1762     }
1763 
1764     private void enterForIn(final ForNode forNode) {
1765         loadExpression(forNode.getModify(), TypeBounds.OBJECT);
1766         method.invoke(forNode.isForEach() ? ScriptRuntime.TO_VALUE_ITERATOR : ScriptRuntime.TO_PROPERTY_ITERATOR);
1767         final Symbol iterSymbol = forNode.getIterator();
1768         final int iterSlot = iterSymbol.getSlot(Type.OBJECT);
1769         method.store(iterSymbol, ITERATOR_TYPE);
1770 
1771         method.beforeJoinPoint(forNode);
1772 
1773         final Label continueLabel = forNode.getContinueLabel();
1774         final Label breakLabel    = forNode.getBreakLabel();
1775 
1776         method.label(continueLabel);
1777         method.load(ITERATOR_TYPE, iterSlot);
1778         method.invoke(interfaceCallNoLookup(ITERATOR_CLASS, "hasNext", boolean.class));
1779         final JoinPredecessorExpression test = forNode.getTest();
1780         final Block body = forNode.getBody();
1781         if(LocalVariableConversion.hasLiveConversion(test)) {
1782             final Label afterConversion = new Label("for_in_after_test_conv");
1783             method.ifne(afterConversion);
1784             method.beforeJoinPoint(test);
1785             method._goto(breakLabel);
1786             method.label(afterConversion);
1787         } else {
1788             method.ifeq(breakLabel);
1789         }
1790 
1791         new Store<Expression>(forNode.getInit()) {
1792             @Override
1793             protected void storeNonDiscard() {
1794                 // This expression is neither part of a discard, nor needs to be left on the stack after it was
1795                 // stored, so we override storeNonDiscard to be a no-op.
1796             }
1797 
1798             @Override
1799             protected void evaluate() {
1800                 new OptimisticOperation((Optimistic)forNode.getInit(), TypeBounds.UNBOUNDED) {
1801                     @Override
1802                     void loadStack() {
1803                         method.load(ITERATOR_TYPE, iterSlot);
1804                     }
1805 
1806                     @Override
1807                     void consumeStack() {
1808                         method.invoke(interfaceCallNoLookup(ITERATOR_CLASS, "next", Object.class));
1809                         convertOptimisticReturnValue();
1810                     }
1811                 }.emit();
1812             }
1813         }.store();
1814         body.accept(this);
1815 
1816         if(method.isReachable()) {
1817             method._goto(continueLabel);
1818         }
1819         method.label(breakLabel);
1820     }
1821 
1822     /**
1823      * Initialize the slots in a frame to undefined.
1824      *
1825      * @param block block with local vars.
1826      */
1827     private void initLocals(final Block block) {
1828         lc.onEnterBlock(block);
1829 
1830         final boolean isFunctionBody = lc.isFunctionBody();
1831         final FunctionNode function = lc.getCurrentFunction();
1832         if (isFunctionBody) {
1833             initializeMethodParameters(function);
1834             if(!function.isVarArg()) {
1835                 expandParameterSlots(function);
1836             }
1837             if (method.hasScope()) {
1838                 if (function.needsParentScope()) {
1839                     method.loadCompilerConstant(CALLEE);
1840                     method.invoke(ScriptFunction.GET_SCOPE);
1841                 } else {
1842                     assert function.hasScopeBlock();
1843                     method.loadNull();
1844                 }
1845                 method.storeCompilerConstant(SCOPE);
1846             }
1847             if (function.needsArguments()) {
1848                 initArguments(function);
1849             }
1850         }
1851 
1852         /*
1853          * Determine if block needs scope, if not, just do initSymbols for this block.
1854          */
1855         if (block.needsScope()) {
1856             /*
1857              * Determine if function is varargs and consequently variables have to
1858              * be in the scope.
1859              */
1860             final boolean varsInScope = function.allVarsInScope();
1861 
1862             // TODO for LET we can do better: if *block* does not contain any eval/with, we don't need its vars in scope.
1863 
1864             final boolean hasArguments = function.needsArguments();
1865             final List<MapTuple<Symbol>> tuples = new ArrayList<>();
1866             final Iterator<IdentNode> paramIter = function.getParameters().iterator();
1867             for (final Symbol symbol : block.getSymbols()) {
1868                 if (symbol.isInternal() || symbol.isThis()) {
1869                     continue;
1870                 }
1871 
1872                 if (symbol.isVar()) {
1873                     assert !varsInScope || symbol.isScope();
1874                     if (varsInScope || symbol.isScope()) {
1875                         assert symbol.isScope()   : "scope for " + symbol + " should have been set in Lower already " + function.getName();
1876                         assert !symbol.hasSlot()  : "slot for " + symbol + " should have been removed in Lower already" + function.getName();
1877 
1878                         //this tuple will not be put fielded, as it has no value, just a symbol
1879                         tuples.add(new MapTuple<Symbol>(symbol.getName(), symbol, null));
1880                     } else {
1881                         assert symbol.hasSlot() || symbol.slotCount() == 0 : symbol + " should have a slot only, no scope";
1882                     }
1883                 } else if (symbol.isParam() && (varsInScope || hasArguments || symbol.isScope())) {
1884                     assert symbol.isScope()   : "scope for " + symbol + " should have been set in AssignSymbols already " + function.getName() + " varsInScope="+varsInScope+" hasArguments="+hasArguments+" symbol.isScope()=" + symbol.isScope();
1885                     assert !(hasArguments && symbol.hasSlot())  : "slot for " + symbol + " should have been removed in Lower already " + function.getName();
1886 
1887                     final Type   paramType;
1888                     final Symbol paramSymbol;
1889 
1890                     if (hasArguments) {
1891                         assert !symbol.hasSlot()  : "slot for " + symbol + " should have been removed in Lower already ";
1892                         paramSymbol = null;
1893                         paramType   = null;
1894                     } else {
1895                         paramSymbol = symbol;
1896                         // NOTE: We're relying on the fact here that Block.symbols is a LinkedHashMap, hence it will
1897                         // return symbols in the order they were defined, and parameters are defined in the same order
1898                         // they appear in the function. That's why we can have a single pass over the parameter list
1899                         // with an iterator, always just scanning forward for the next parameter that matches the symbol
1900                         // name.
1901                         for(;;) {
1902                             final IdentNode nextParam = paramIter.next();
1903                             if(nextParam.getName().equals(symbol.getName())) {
1904                                 paramType = nextParam.getType();
1905                                 break;
1906                             }
1907                         }
1908                     }
1909 
1910                     tuples.add(new MapTuple<Symbol>(symbol.getName(), symbol, paramType, paramSymbol) {
1911                         //this symbol will be put fielded, we can't initialize it as undefined with a known type
1912                         @Override
1913                         public Class<?> getValueType() {
1914                             if (!useDualFields() ||  value == null || paramType == null || paramType.isBoolean()) {
1915                                 return Object.class;
1916                             }
1917                             return paramType.getTypeClass();
1918                         }
1919                     });
1920                 }
1921             }
1922 
1923             /*
1924              * Create a new object based on the symbols and values, generate
1925              * bootstrap code for object
1926              */
1927             new FieldObjectCreator<Symbol>(this, tuples, true, hasArguments) {
1928                 @Override
1929                 protected void loadValue(final Symbol value, final Type type) {
1930                     method.load(value, type);
1931                 }
1932             }.makeObject(method);
1933             // program function: merge scope into global
1934             if (isFunctionBody && function.isProgram()) {
1935                 method.invoke(ScriptRuntime.MERGE_SCOPE);
1936             }
1937 
1938             method.storeCompilerConstant(SCOPE);
1939             if(!isFunctionBody) {
1940                 // Function body doesn't need a try/catch to restore scope, as it'd be a dead store anyway. Allowing it
1941                 // actually causes issues with UnwarrantedOptimismException handlers as ASM will sort this handler to
1942                 // the top of the exception handler table, so it'll be triggered instead of the UOE handlers.
1943                 final Label scopeEntryLabel = new Label("scope_entry");
1944                 scopeEntryLabels.push(scopeEntryLabel);
1945                 method.label(scopeEntryLabel);
1946             }
1947         } else if (isFunctionBody && function.isVarArg()) {
1948             // Since we don't have a scope, parameters didn't get assigned array indices by the FieldObjectCreator, so
1949             // we need to assign them separately here.
1950             int nextParam = 0;
1951             for (final IdentNode param : function.getParameters()) {
1952                 param.getSymbol().setFieldIndex(nextParam++);
1953             }
1954         }
1955 
1956         // Debugging: print symbols? @see --print-symbols flag
1957         printSymbols(block, function, (isFunctionBody ? "Function " : "Block in ") + (function.getIdent() == null ? "<anonymous>" : function.getIdent().getName()));
1958     }
1959 
1960     /**
1961      * Incoming method parameters are always declared on method entry; declare them in the local variable table.
1962      * @param function function for which code is being generated.
1963      */
1964     private void initializeMethodParameters(final FunctionNode function) {
1965         final Label functionStart = new Label("fn_start");
1966         method.label(functionStart);
1967         int nextSlot = 0;
1968         if(function.needsCallee()) {
1969             initializeInternalFunctionParameter(CALLEE, function, functionStart, nextSlot++);
1970         }
1971         initializeInternalFunctionParameter(THIS, function, functionStart, nextSlot++);
1972         if(function.isVarArg()) {
1973             initializeInternalFunctionParameter(VARARGS, function, functionStart, nextSlot++);
1974         } else {
1975             for(final IdentNode param: function.getParameters()) {
1976                 final Symbol symbol = param.getSymbol();
1977                 if(symbol.isBytecodeLocal()) {
1978                     method.initializeMethodParameter(symbol, param.getType(), functionStart);
1979                 }
1980             }
1981         }
1982     }
1983 
1984     private void initializeInternalFunctionParameter(final CompilerConstants cc, final FunctionNode fn, final Label functionStart, final int slot) {
1985         final Symbol symbol = initializeInternalFunctionOrSplitParameter(cc, fn, functionStart, slot);
1986         // Internal function params (:callee, this, and :varargs) are never expanded to multiple slots
1987         assert symbol.getFirstSlot() == slot;
1988     }
1989 
1990     private Symbol initializeInternalFunctionOrSplitParameter(final CompilerConstants cc, final FunctionNode fn, final Label functionStart, final int slot) {
1991         final Symbol symbol = fn.getBody().getExistingSymbol(cc.symbolName());
1992         final Type type = Type.typeFor(cc.type());
1993         method.initializeMethodParameter(symbol, type, functionStart);
1994         method.onLocalStore(type, slot);
1995         return symbol;
1996     }
1997 
1998     /**
1999      * Parameters come into the method packed into local variable slots next to each other. Nashorn on the other hand
2000      * can use 1-6 slots for a local variable depending on all the types it needs to store. When this method is invoked,
2001      * the symbols are already allocated such wider slots, but the values are still in tightly packed incoming slots,
2002      * and we need to spread them into their new locations.
2003      * @param function the function for which parameter-spreading code needs to be emitted
2004      */
2005     private void expandParameterSlots(final FunctionNode function) {
2006         final List<IdentNode> parameters = function.getParameters();
2007         // Calculate the total number of incoming parameter slots
2008         int currentIncomingSlot = function.needsCallee() ? 2 : 1;
2009         for(final IdentNode parameter: parameters) {
2010             currentIncomingSlot += parameter.getType().getSlots();
2011         }
2012         // Starting from last parameter going backwards, move the parameter values into their new slots.
2013         for(int i = parameters.size(); i-- > 0;) {
2014             final IdentNode parameter = parameters.get(i);
2015             final Type parameterType = parameter.getType();
2016             final int typeWidth = parameterType.getSlots();
2017             currentIncomingSlot -= typeWidth;
2018             final Symbol symbol = parameter.getSymbol();
2019             final int slotCount = symbol.slotCount();
2020             assert slotCount > 0;
2021             // Scoped parameters must not hold more than one value
2022             assert symbol.isBytecodeLocal() || slotCount == typeWidth;
2023 
2024             // Mark it as having its value stored into it by the method invocation.
2025             method.onLocalStore(parameterType, currentIncomingSlot);
2026             if(currentIncomingSlot != symbol.getSlot(parameterType)) {
2027                 method.load(parameterType, currentIncomingSlot);
2028                 method.store(symbol, parameterType);
2029             }
2030         }
2031     }
2032 
2033     private void initArguments(final FunctionNode function) {
2034         method.loadCompilerConstant(VARARGS);
2035         if (function.needsCallee()) {
2036             method.loadCompilerConstant(CALLEE);
2037         } else {
2038             // If function is strict mode, "arguments.callee" is not populated, so we don't necessarily need the
2039             // caller.
2040             assert function.isStrict();
2041             method.loadNull();
2042         }
2043         method.load(function.getParameters().size());
2044         globalAllocateArguments();
2045         method.storeCompilerConstant(ARGUMENTS);
2046     }
2047 
2048     private boolean skipFunction(final FunctionNode functionNode) {
2049         final ScriptEnvironment env = compiler.getScriptEnvironment();
2050         final boolean lazy = env._lazy_compilation;
2051         final boolean onDemand = compiler.isOnDemandCompilation();
2052 
2053         // If this is on-demand or lazy compilation, don't compile a nested (not topmost) function.
2054         if((onDemand || lazy) && lc.getOutermostFunction() != functionNode) {
2055             return true;
2056         }
2057 
2058         // If lazy compiling with optimistic types, don't compile the program eagerly either. It will soon be
2059         // invalidated anyway. In presence of a class cache, this further means that an obsoleted program version
2060         // lingers around. Also, currently loading previously persisted optimistic types information only works if
2061         // we're on-demand compiling a function, so with this strategy the :program method can also have the warmup
2062         // benefit of using previously persisted types.
2063         //
2064         // NOTE that this means the first compiled class will effectively just have a :createProgramFunction method, and
2065         // the RecompilableScriptFunctionData (RSFD) object in its constants array. It won't even have the :program
2066         // method. This is by design. It does mean that we're wasting one compiler execution (and we could minimize this
2067         // by just running it up to scope depth calculation, which creates the RSFDs and then this limited codegen).
2068         // We could emit an initial separate compile unit with the initial version of :program in it to better utilize
2069         // the compilation pipeline, but that would need more invasive changes, as currently the assumption that
2070         // :program is emitted into the first compilation unit of the function lives in many places.
2071         return !onDemand && lazy && env._optimistic_types && functionNode.isProgram();
2072     }
2073 
2074     @Override
2075     public boolean enterFunctionNode(final FunctionNode functionNode) {
2076         final int fnId = functionNode.getId();
2077 
2078         if (skipFunction(functionNode)) {
2079             // In case we are not generating code for the function, we must create or retrieve the function object and
2080             // load it on the stack here.
2081             newFunctionObject(functionNode, false);
2082             return false;
2083         }
2084 
2085         final String fnName = functionNode.getName();
2086 
2087         // NOTE: we only emit the method for a function with the given name once. We can have multiple functions with
2088         // the same name as a result of inlining finally blocks. However, in the future -- with type specialization,
2089         // notably -- we might need to check for both name *and* signature. Of course, even that might not be
2090         // sufficient; the function might have a code dependency on the type of the variables in its enclosing scopes,
2091         // and the type of such a variable can be different in catch and finally blocks. So, in the future we will have
2092         // to decide to either generate a unique method for each inlined copy of the function, maybe figure out its
2093         // exact type closure and deduplicate based on that, or just decide that functions in finally blocks aren't
2094         // worth it, and generate one method with most generic type closure.
2095         if (!emittedMethods.contains(fnName)) {
2096             log.info("=== BEGIN ", fnName);
2097 
2098             assert functionNode.getCompileUnit() != null : "no compile unit for " + fnName + " " + Debug.id(functionNode);
2099             unit = lc.pushCompileUnit(functionNode.getCompileUnit());
2100             assert lc.hasCompileUnits();
2101 
2102             final ClassEmitter classEmitter = unit.getClassEmitter();
2103             pushMethodEmitter(isRestOf() ? classEmitter.restOfMethod(functionNode) : classEmitter.method(functionNode));
2104             method.setPreventUndefinedLoad();
2105             if(useOptimisticTypes()) {
2106                 lc.pushUnwarrantedOptimismHandlers();
2107             }
2108 
2109             // new method - reset last line number
2110             lastLineNumber = -1;
2111 
2112             method.begin();
2113 
2114             if (isRestOf()) {
2115                 final ContinuationInfo ci = new ContinuationInfo();
2116                 fnIdToContinuationInfo.put(fnId, ci);
2117                 method.gotoLoopStart(ci.getHandlerLabel());
2118             }
2119         }
2120 
2121         return true;
2122     }
2123 
2124     private void pushMethodEmitter(final MethodEmitter newMethod) {
2125         method = lc.pushMethodEmitter(newMethod);
2126         catchLabels.push(METHOD_BOUNDARY);
2127     }
2128 
2129     private void popMethodEmitter() {
2130         method = lc.popMethodEmitter(method);
2131         assert catchLabels.peek() == METHOD_BOUNDARY;
2132         catchLabels.pop();
2133     }
2134 
2135     @Override
2136     public Node leaveFunctionNode(final FunctionNode functionNode) {
2137         try {
2138             final boolean markOptimistic;
2139             if (emittedMethods.add(functionNode.getName())) {
2140                 markOptimistic = generateUnwarrantedOptimismExceptionHandlers(functionNode);
2141                 generateContinuationHandler();
2142                 method.end(); // wrap up this method
2143                 unit   = lc.popCompileUnit(functionNode.getCompileUnit());
2144                 popMethodEmitter();
2145                 log.info("=== END ", functionNode.getName());
2146             } else {
2147                 markOptimistic = false;
2148             }
2149 
2150             FunctionNode newFunctionNode = functionNode.setState(lc, CompilationState.BYTECODE_GENERATED);
2151             if (markOptimistic) {
2152                 newFunctionNode = newFunctionNode.setFlag(lc, FunctionNode.IS_DEOPTIMIZABLE);
2153             }
2154 
2155             newFunctionObject(newFunctionNode, true);
2156             return newFunctionNode;
2157         } catch (final Throwable t) {
2158             Context.printStackTrace(t);
2159             final VerifyError e = new VerifyError("Code generation bug in \"" + functionNode.getName() + "\": likely stack misaligned: " + t + " " + functionNode.getSource().getName());
2160             e.initCause(t);
2161             throw e;
2162         }
2163     }
2164 
2165     @Override
2166     public boolean enterIfNode(final IfNode ifNode) {
2167         if(!method.isReachable()) {
2168             return false;
2169         }
2170         enterStatement(ifNode);
2171 
2172         final Expression test = ifNode.getTest();
2173         final Block pass = ifNode.getPass();
2174         final Block fail = ifNode.getFail();
2175 
2176         if (Expression.isAlwaysTrue(test)) {
2177             loadAndDiscard(test);
2178             pass.accept(this);
2179             return false;
2180         } else if (Expression.isAlwaysFalse(test)) {
2181             loadAndDiscard(test);
2182             if (fail != null) {
2183                 fail.accept(this);
2184             }
2185             return false;
2186         }
2187 
2188         final boolean hasFailConversion = LocalVariableConversion.hasLiveConversion(ifNode);
2189 
2190         final Label failLabel  = new Label("if_fail");
2191         final Label afterLabel = (fail == null && !hasFailConversion) ? null : new Label("if_done");
2192 
2193         emitBranch(test, failLabel, false);
2194 
2195         pass.accept(this);
2196         if(method.isReachable() && afterLabel != null) {
2197             method._goto(afterLabel); //don't fallthru to fail block
2198         }
2199         method.label(failLabel);
2200 
2201         if (fail != null) {
2202             fail.accept(this);
2203         } else if(hasFailConversion) {
2204             method.beforeJoinPoint(ifNode);
2205         }
2206 
2207         if(afterLabel != null && afterLabel.isReachable()) {
2208             method.label(afterLabel);
2209         }
2210 
2211         return false;
2212     }
2213 
2214     private void emitBranch(final Expression test, final Label label, final boolean jumpWhenTrue) {
2215         new BranchOptimizer(this, method).execute(test, label, jumpWhenTrue);
2216     }
2217 
2218     private void enterStatement(final Statement statement) {
2219         lineNumber(statement);
2220     }
2221 
2222     private void lineNumber(final Statement statement) {
2223         lineNumber(statement.getLineNumber());
2224     }
2225 
2226     private void lineNumber(final int lineNumber) {
2227         if (lineNumber != lastLineNumber && lineNumber != Node.NO_LINE_NUMBER) {
2228             method.lineNumber(lineNumber);
2229             lastLineNumber = lineNumber;
2230         }
2231     }
2232 
2233     int getLastLineNumber() {
2234         return lastLineNumber;
2235     }
2236 
2237     /**
2238      * Load a list of nodes as an array of a specific type
2239      * The array will contain the visited nodes.
2240      *
2241      * @param arrayLiteralNode the array of contents
2242      * @param arrayType        the type of the array, e.g. ARRAY_NUMBER or ARRAY_OBJECT
2243      *
2244      * @return the method generator that was used
2245      */
2246     private MethodEmitter loadArray(final ArrayLiteralNode arrayLiteralNode, final ArrayType arrayType) {
2247         assert arrayType == Type.INT_ARRAY || arrayType == Type.LONG_ARRAY || arrayType == Type.NUMBER_ARRAY || arrayType == Type.OBJECT_ARRAY;
2248 
2249         final Expression[]    nodes    = arrayLiteralNode.getValue();
2250         final Object          presets  = arrayLiteralNode.getPresets();
2251         final int[]           postsets = arrayLiteralNode.getPostsets();
2252         final Class<?>        type     = arrayType.getTypeClass();
2253         final List<ArrayUnit> units    = arrayLiteralNode.getUnits();
2254 
2255         loadConstant(presets);
2256 
2257         final Type elementType = arrayType.getElementType();
2258 
2259         if (units != null) {
2260             final MethodEmitter savedMethod     = method;
2261             final FunctionNode  currentFunction = lc.getCurrentFunction();
2262 
2263             for (final ArrayUnit arrayUnit : units) {
2264                 unit = lc.pushCompileUnit(arrayUnit.getCompileUnit());
2265 
2266                 final String className = unit.getUnitClassName();
2267                 assert unit != null;
2268                 final String name      = currentFunction.uniqueName(SPLIT_PREFIX.symbolName());
2269                 final String signature = methodDescriptor(type, ScriptFunction.class, Object.class, ScriptObject.class, type);
2270 
2271                 pushMethodEmitter(unit.getClassEmitter().method(EnumSet.of(Flag.PUBLIC, Flag.STATIC), name, signature));
2272 
2273                 method.setFunctionNode(currentFunction);
2274                 method.begin();
2275 
2276                 defineCommonSplitMethodParameters();
2277                 defineSplitMethodParameter(CompilerConstants.SPLIT_ARRAY_ARG.slot(), arrayType);
2278 
2279                 // NOTE: when this is no longer needed, SplitIntoFunctions will no longer have to add IS_SPLIT
2280                 // to synthetic functions, and FunctionNode.needsCallee() will no longer need to test for isSplit().
2281                 final int arraySlot = fixScopeSlot(currentFunction, 3);
2282 
2283                 lc.enterSplitNode();
2284 
2285                 for (int i = arrayUnit.getLo(); i < arrayUnit.getHi(); i++) {
2286                     method.load(arrayType, arraySlot);
2287                     storeElement(nodes, elementType, postsets[i]);
2288                 }
2289 
2290                 method.load(arrayType, arraySlot);
2291                 method._return();
2292                 lc.exitSplitNode();
2293                 method.end();
2294                 lc.releaseSlots();
2295                 popMethodEmitter();
2296 
2297                 assert method == savedMethod;
2298                 method.loadCompilerConstant(CALLEE);
2299                 method.swap();
2300                 method.loadCompilerConstant(THIS);
2301                 method.swap();
2302                 method.loadCompilerConstant(SCOPE);
2303                 method.swap();
2304                 method.invokestatic(className, name, signature);
2305 
2306                 unit = lc.popCompileUnit(unit);
2307             }
2308 
2309             return method;
2310         }
2311 
2312         if(postsets.length > 0) {
2313             final int arraySlot = method.getUsedSlotsWithLiveTemporaries();
2314             method.storeTemp(arrayType, arraySlot);
2315             for (final int postset : postsets) {
2316                 method.load(arrayType, arraySlot);
2317                 storeElement(nodes, elementType, postset);
2318             }
2319             method.load(arrayType, arraySlot);
2320         }
2321         return method;
2322     }
2323 
2324     private void storeElement(final Expression[] nodes, final Type elementType, final int index) {
2325         method.load(index);
2326 
2327         final Expression element = nodes[index];
2328 
2329         if (element == null) {
2330             method.loadEmpty(elementType);
2331         } else {
2332             loadExpressionAsType(element, elementType);
2333         }
2334 
2335         method.arraystore();
2336     }
2337 
2338     private MethodEmitter loadArgsArray(final List<Expression> args) {
2339         final Object[] array = new Object[args.size()];
2340         loadConstant(array);
2341 
2342         for (int i = 0; i < args.size(); i++) {
2343             method.dup();
2344             method.load(i);
2345             loadExpression(args.get(i), TypeBounds.OBJECT); // variable arity methods always take objects
2346             method.arraystore();
2347         }
2348 
2349         return method;
2350     }
2351 
2352     /**
2353      * Load a constant from the constant array. This is only public to be callable from the objects
2354      * subpackage. Do not call directly.
2355      *
2356      * @param string string to load
2357      */
2358     void loadConstant(final String string) {
2359         final String       unitClassName = unit.getUnitClassName();
2360         final ClassEmitter classEmitter  = unit.getClassEmitter();
2361         final int          index         = compiler.getConstantData().add(string);
2362 
2363         method.load(index);
2364         method.invokestatic(unitClassName, GET_STRING.symbolName(), methodDescriptor(String.class, int.class));
2365         classEmitter.needGetConstantMethod(String.class);
2366     }
2367 
2368     /**
2369      * Load a constant from the constant array. This is only public to be callable from the objects
2370      * subpackage. Do not call directly.
2371      *
2372      * @param object object to load
2373      */
2374     void loadConstant(final Object object) {
2375         loadConstant(object, unit, method);
2376     }
2377 
2378     private void loadConstant(final Object object, final CompileUnit compileUnit, final MethodEmitter methodEmitter) {
2379         final String       unitClassName = compileUnit.getUnitClassName();
2380         final ClassEmitter classEmitter  = compileUnit.getClassEmitter();
2381         final int          index         = compiler.getConstantData().add(object);
2382         final Class<?>     cls           = object.getClass();
2383 
2384         if (cls == PropertyMap.class) {
2385             methodEmitter.load(index);
2386             methodEmitter.invokestatic(unitClassName, GET_MAP.symbolName(), methodDescriptor(PropertyMap.class, int.class));
2387             classEmitter.needGetConstantMethod(PropertyMap.class);
2388         } else if (cls.isArray()) {
2389             methodEmitter.load(index);
2390             final String methodName = ClassEmitter.getArrayMethodName(cls);
2391             methodEmitter.invokestatic(unitClassName, methodName, methodDescriptor(cls, int.class));
2392             classEmitter.needGetConstantMethod(cls);
2393         } else {
2394             methodEmitter.loadConstants().load(index).arrayload();
2395             if (object instanceof ArrayData) {
2396                 methodEmitter.checkcast(ArrayData.class);
2397                 methodEmitter.invoke(virtualCallNoLookup(ArrayData.class, "copy", ArrayData.class));
2398             } else if (cls != Object.class) {
2399                 methodEmitter.checkcast(cls);
2400             }
2401         }
2402     }
2403 
2404     private void loadConstantsAndIndex(final Object object, final MethodEmitter methodEmitter) {
2405         methodEmitter.loadConstants().load(compiler.getConstantData().add(object));
2406     }
2407 
2408     // literal values
2409     private void loadLiteral(final LiteralNode<?> node, final TypeBounds resultBounds) {
2410         final Object value = node.getValue();
2411 
2412         if (value == null) {
2413             method.loadNull();
2414         } else if (value instanceof Undefined) {
2415             method.loadUndefined(resultBounds.within(Type.OBJECT));
2416         } else if (value instanceof String) {
2417             final String string = (String)value;
2418 
2419             if (string.length() > MethodEmitter.LARGE_STRING_THRESHOLD / 3) { // 3 == max bytes per encoded char
2420                 loadConstant(string);
2421             } else {
2422                 method.load(string);
2423             }
2424         } else if (value instanceof RegexToken) {
2425             loadRegex((RegexToken)value);
2426         } else if (value instanceof Boolean) {
2427             method.load((Boolean)value);
2428         } else if (value instanceof Integer) {
2429             if(!resultBounds.canBeNarrowerThan(Type.OBJECT)) {
2430                 method.load((Integer)value);
2431                 method.convert(Type.OBJECT);
2432             } else if(!resultBounds.canBeNarrowerThan(Type.NUMBER)) {
2433                 method.load(((Integer)value).doubleValue());
2434             } else if(!resultBounds.canBeNarrowerThan(Type.LONG)) {
2435                 method.load(((Integer)value).longValue());
2436             } else {
2437                 method.load((Integer)value);
2438             }
2439         } else if (value instanceof Long) {
2440             if(!resultBounds.canBeNarrowerThan(Type.OBJECT)) {
2441                 method.load((Long)value);
2442                 method.convert(Type.OBJECT);
2443             } else if(!resultBounds.canBeNarrowerThan(Type.NUMBER)) {
2444                 method.load(((Long)value).doubleValue());
2445             } else {
2446                 method.load((Long)value);
2447             }
2448         } else if (value instanceof Double) {
2449             if(!resultBounds.canBeNarrowerThan(Type.OBJECT)) {
2450                 method.load((Double)value);
2451                 method.convert(Type.OBJECT);
2452             } else {
2453                 method.load((Double)value);
2454             }
2455         } else if (node instanceof ArrayLiteralNode) {
2456             final ArrayLiteralNode arrayLiteral = (ArrayLiteralNode)node;
2457             final ArrayType atype = arrayLiteral.getArrayType();
2458             loadArray(arrayLiteral, atype);
2459             globalAllocateArray(atype);
2460         } else {
2461             throw new UnsupportedOperationException("Unknown literal for " + node.getClass() + " " + value.getClass() + " " + value);
2462         }
2463     }
2464 
2465     private MethodEmitter loadRegexToken(final RegexToken value) {
2466         method.load(value.getExpression());
2467         method.load(value.getOptions());
2468         return globalNewRegExp();
2469     }
2470 
2471     private MethodEmitter loadRegex(final RegexToken regexToken) {
2472         if (regexFieldCount > MAX_REGEX_FIELDS) {
2473             return loadRegexToken(regexToken);
2474         }
2475         // emit field
2476         final String       regexName    = lc.getCurrentFunction().uniqueName(REGEX_PREFIX.symbolName());
2477         final ClassEmitter classEmitter = unit.getClassEmitter();
2478 
2479         classEmitter.field(EnumSet.of(PRIVATE, STATIC), regexName, Object.class);
2480         regexFieldCount++;
2481 
2482         // get field, if null create new regex, finally clone regex object
2483         method.getStatic(unit.getUnitClassName(), regexName, typeDescriptor(Object.class));
2484         method.dup();
2485         final Label cachedLabel = new Label("cached");
2486         method.ifnonnull(cachedLabel);
2487 
2488         method.pop();
2489         loadRegexToken(regexToken);
2490         method.dup();
2491         method.putStatic(unit.getUnitClassName(), regexName, typeDescriptor(Object.class));
2492 
2493         method.label(cachedLabel);
2494         globalRegExpCopy();
2495 
2496         return method;
2497     }
2498 
2499     /**
2500      * Check if a property value contains a particular program point
2501      * @param value value
2502      * @param pp    program point
2503      * @return true if it's there.
2504      */
2505     private static boolean propertyValueContains(final Expression value, final int pp) {
2506         return new Supplier<Boolean>() {
2507             boolean contains;
2508 
2509             @Override
2510             public Boolean get() {
2511                 value.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
2512                     @Override
2513                     public boolean enterFunctionNode(final FunctionNode functionNode) {
2514                         return false;
2515                     }
2516 
2517                     @Override
2518                     public boolean enterObjectNode(final ObjectNode objectNode) {
2519                         return false;
2520                     }
2521 
2522                     @Override
2523                     public boolean enterDefault(final Node node) {
2524                         if (contains) {
2525                             return false;
2526                         }
2527                         if (node instanceof Optimistic && ((Optimistic)node).getProgramPoint() == pp) {
2528                             contains = true;
2529                             return false;
2530                         }
2531                         return true;
2532                     }
2533                 });
2534 
2535                 return contains;
2536             }
2537         }.get();
2538     }
2539 
2540     private void loadObjectNode(final ObjectNode objectNode) {
2541         final List<PropertyNode> elements = objectNode.getElements();
2542 
2543         final List<MapTuple<Expression>> tuples = new ArrayList<>();
2544         final List<PropertyNode> gettersSetters = new ArrayList<>();
2545         final int ccp = getCurrentContinuationEntryPoint();
2546 
2547         Expression protoNode = null;
2548         boolean restOfProperty = false;
2549 
2550         for (final PropertyNode propertyNode : elements) {
2551             final Expression value = propertyNode.getValue();
2552             final String key = propertyNode.getKeyName();
2553             // Just use a pseudo-symbol. We just need something non null; use the name and zero flags.
2554             final Symbol symbol = value == null ? null : new Symbol(key, 0);
2555 
2556             if (value == null) {
2557                 gettersSetters.add(propertyNode);
2558             } else if (propertyNode.getKey() instanceof IdentNode &&
2559                        key.equals(ScriptObject.PROTO_PROPERTY_NAME)) {
2560                 // ES6 draft compliant __proto__ inside object literal
2561                 // Identifier key and name is __proto__
2562                 protoNode = value;
2563                 continue;
2564             }
2565 
2566             restOfProperty |=
2567                 value != null &&
2568                 isValid(ccp) &&
2569                 propertyValueContains(value, ccp);
2570 
2571             //for literals, a value of null means object type, i.e. the value null or getter setter function
2572             //(I think)
2573             final Class<?> valueType = (!useDualFields() || value == null || value.getType().isBoolean()) ? Object.class : value.getType().getTypeClass();
2574             tuples.add(new MapTuple<Expression>(key, symbol, Type.typeFor(valueType), value) {
2575                 @Override
2576                 public Class<?> getValueType() {
2577                     return type.getTypeClass();
2578                 }
2579             });
2580         }
2581 
2582         final ObjectCreator<?> oc;
2583         if (elements.size() > OBJECT_SPILL_THRESHOLD) {
2584             oc = new SpillObjectCreator(this, tuples);
2585         } else {
2586             oc = new FieldObjectCreator<Expression>(this, tuples) {
2587                 @Override
2588                 protected void loadValue(final Expression node, final Type type) {
2589                     loadExpressionAsType(node, type);
2590                 }};
2591         }
2592         oc.makeObject(method);
2593 
2594         //if this is a rest of method and our continuation point was found as one of the values
2595         //in the properties above, we need to reset the map to oc.getMap() in the continuation
2596         //handler
2597         if (restOfProperty) {
2598             final ContinuationInfo ci = getContinuationInfo();
2599             // Can be set at most once for a single rest-of method
2600             assert ci.getObjectLiteralMap() == null;
2601             ci.setObjectLiteralMap(oc.getMap());
2602             ci.setObjectLiteralStackDepth(method.getStackSize());
2603         }
2604 
2605         method.dup();
2606         if (protoNode != null) {
2607             loadExpressionAsObject(protoNode);
2608             // take care of { __proto__: 34 } or some such!
2609             method.convert(Type.OBJECT);
2610             method.invoke(ScriptObject.SET_PROTO_FROM_LITERAL);
2611         } else {
2612             method.invoke(ScriptObject.SET_GLOBAL_OBJECT_PROTO);
2613         }
2614 
2615         for (final PropertyNode propertyNode : gettersSetters) {
2616             final FunctionNode getter = propertyNode.getGetter();
2617             final FunctionNode setter = propertyNode.getSetter();
2618 
2619             assert getter != null || setter != null;
2620 
2621             method.dup().loadKey(propertyNode.getKey());
2622             if (getter == null) {
2623                 method.loadNull();
2624             } else {
2625                 getter.accept(this);
2626             }
2627 
2628             if (setter == null) {
2629                 method.loadNull();
2630             } else {
2631                 setter.accept(this);
2632             }
2633 
2634             method.invoke(ScriptObject.SET_USER_ACCESSORS);
2635         }
2636     }
2637 
2638     @Override
2639     public boolean enterReturnNode(final ReturnNode returnNode) {
2640         if(!method.isReachable()) {
2641             return false;
2642         }
2643         enterStatement(returnNode);
2644 
2645         final Type returnType = lc.getCurrentFunction().getReturnType();
2646 
2647         final Expression expression = returnNode.getExpression();
2648         if (expression != null) {
2649             loadExpressionUnbounded(expression);
2650         } else {
2651             method.loadUndefined(returnType);
2652         }
2653 
2654         method._return(returnType);
2655 
2656         return false;
2657     }
2658 
2659     private boolean undefinedCheck(final RuntimeNode runtimeNode, final List<Expression> args) {
2660         final Request request = runtimeNode.getRequest();
2661 
2662         if (!Request.isUndefinedCheck(request)) {
2663             return false;
2664         }
2665 
2666         final Expression lhs = args.get(0);
2667         final Expression rhs = args.get(1);
2668 
2669         final Symbol lhsSymbol = lhs instanceof IdentNode ? ((IdentNode)lhs).getSymbol() : null;
2670         final Symbol rhsSymbol = rhs instanceof IdentNode ? ((IdentNode)rhs).getSymbol() : null;
2671         // One must be a "undefined" identifier, otherwise we can't get here
2672         assert lhsSymbol != null || rhsSymbol != null;
2673 
2674         final Symbol undefinedSymbol;
2675         if (isUndefinedSymbol(lhsSymbol)) {
2676             undefinedSymbol = lhsSymbol;
2677         } else {
2678             assert isUndefinedSymbol(rhsSymbol);
2679             undefinedSymbol = rhsSymbol;
2680         }
2681 
2682         assert undefinedSymbol != null; //remove warning
2683         if (!undefinedSymbol.isScope()) {
2684             return false; //disallow undefined as local var or parameter
2685         }
2686 
2687         if (lhsSymbol == undefinedSymbol && lhs.getType().isPrimitive()) {
2688             //we load the undefined first. never mind, because this will deoptimize anyway
2689             return false;
2690         }
2691 
2692         if(isDeoptimizedExpression(lhs)) {
2693             // This is actually related to "lhs.getType().isPrimitive()" above: any expression being deoptimized in
2694             // the current chain of rest-of compilations used to have a type narrower than Object (so it was primitive).
2695             // We must not perform undefined check specialization for them, as then we'd violate the basic rule of
2696             // "Thou shalt not alter the stack shape between a deoptimized method and any of its (transitive) rest-ofs."
2697             return false;
2698         }
2699 
2700         //make sure that undefined has not been overridden or scoped as a local var
2701         //between us and global
2702         if (!compiler.isGlobalSymbol(lc.getCurrentFunction(), "undefined")) {
2703             return false;
2704         }
2705 
2706         final boolean isUndefinedCheck = request == Request.IS_UNDEFINED;
2707         final Expression expr = undefinedSymbol == lhsSymbol ? rhs : lhs;
2708         if (expr.getType().isPrimitive()) {
2709             loadAndDiscard(expr); //throw away lhs, but it still needs to be evaluated for side effects, even if not in scope, as it can be optimistic
2710             method.load(!isUndefinedCheck);
2711         } else {
2712             final Label checkTrue  = new Label("ud_check_true");
2713             final Label end        = new Label("end");
2714             loadExpressionAsObject(expr);
2715             method.loadUndefined(Type.OBJECT);
2716             method.if_acmpeq(checkTrue);
2717             method.load(!isUndefinedCheck);
2718             method._goto(end);
2719             method.label(checkTrue);
2720             method.load(isUndefinedCheck);
2721             method.label(end);
2722         }
2723 
2724         return true;
2725     }
2726 
2727     private static boolean isUndefinedSymbol(final Symbol symbol) {
2728         return symbol != null && "undefined".equals(symbol.getName());
2729     }
2730 
2731     private static boolean isNullLiteral(final Node node) {
2732         return node instanceof LiteralNode<?> && ((LiteralNode<?>) node).isNull();
2733     }
2734 
2735     private boolean nullCheck(final RuntimeNode runtimeNode, final List<Expression> args) {
2736         final Request request = runtimeNode.getRequest();
2737 
2738         if (!Request.isEQ(request) && !Request.isNE(request)) {
2739             return false;
2740         }
2741 
2742         assert args.size() == 2 : "EQ or NE or TYPEOF need two args";
2743 
2744         Expression lhs = args.get(0);
2745         Expression rhs = args.get(1);
2746 
2747         if (isNullLiteral(lhs)) {
2748             final Expression tmp = lhs;
2749             lhs = rhs;
2750             rhs = tmp;
2751         }
2752 
2753         if (!isNullLiteral(rhs)) {
2754             return false;
2755         }
2756 
2757         if (!lhs.getType().isObject()) {
2758             return false;
2759         }
2760 
2761         if(isDeoptimizedExpression(lhs)) {
2762             // This is actually related to "!lhs.getType().isObject()" above: any expression being deoptimized in
2763             // the current chain of rest-of compilations used to have a type narrower than Object. We must not
2764             // perform null check specialization for them, as then we'd no longer be loading aconst_null on stack
2765             // and thus violate the basic rule of "Thou shalt not alter the stack shape between a deoptimized
2766             // method and any of its (transitive) rest-ofs."
2767             // NOTE also that if we had a representation for well-known constants (e.g. null, 0, 1, -1, etc.) in
2768             // Label$Stack.localLoads then this wouldn't be an issue, as we would never (somewhat ridiculously)
2769             // allocate a temporary local to hold the result of aconst_null before attempting an optimistic
2770             // operation.
2771             return false;
2772         }
2773 
2774         // this is a null literal check, so if there is implicit coercion
2775         // involved like {D}x=null, we will fail - this is very rare
2776         final Label trueLabel  = new Label("trueLabel");
2777         final Label falseLabel = new Label("falseLabel");
2778         final Label endLabel   = new Label("end");
2779 
2780         loadExpressionUnbounded(lhs);    //lhs
2781         final Label popLabel;
2782         if (!Request.isStrict(request)) {
2783             method.dup(); //lhs lhs
2784             popLabel = new Label("pop");
2785         } else {
2786             popLabel = null;
2787         }
2788 
2789         if (Request.isEQ(request)) {
2790             method.ifnull(!Request.isStrict(request) ? popLabel : trueLabel);
2791             if (!Request.isStrict(request)) {
2792                 method.loadUndefined(Type.OBJECT);
2793                 method.if_acmpeq(trueLabel);
2794             }
2795             method.label(falseLabel);
2796             method.load(false);
2797             method._goto(endLabel);
2798             if (!Request.isStrict(request)) {
2799                 method.label(popLabel);
2800                 method.pop();
2801             }
2802             method.label(trueLabel);
2803             method.load(true);
2804             method.label(endLabel);
2805         } else if (Request.isNE(request)) {
2806             method.ifnull(!Request.isStrict(request) ? popLabel : falseLabel);
2807             if (!Request.isStrict(request)) {
2808                 method.loadUndefined(Type.OBJECT);
2809                 method.if_acmpeq(falseLabel);
2810             }
2811             method.label(trueLabel);
2812             method.load(true);
2813             method._goto(endLabel);
2814             if (!Request.isStrict(request)) {
2815                 method.label(popLabel);
2816                 method.pop();
2817             }
2818             method.label(falseLabel);
2819             method.load(false);
2820             method.label(endLabel);
2821         }
2822 
2823         assert runtimeNode.getType().isBoolean();
2824         method.convert(runtimeNode.getType());
2825 
2826         return true;
2827     }
2828 
2829     /**
2830      * Was this expression or any of its subexpressions deoptimized in the current recompilation chain of rest-of methods?
2831      * @param rootExpr the expression being tested
2832      * @return true if the expression or any of its subexpressions was deoptimized in the current recompilation chain.
2833      */
2834     private boolean isDeoptimizedExpression(final Expression rootExpr) {
2835         if(!isRestOf()) {
2836             return false;
2837         }
2838         return new Supplier<Boolean>() {
2839             boolean contains;
2840             @Override
2841             public Boolean get() {
2842                 rootExpr.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
2843                     @Override
2844                     public boolean enterFunctionNode(final FunctionNode functionNode) {
2845                         return false;
2846                     }
2847                     @Override
2848                     public boolean enterDefault(final Node node) {
2849                         if(!contains && node instanceof Optimistic) {
2850                             final int pp = ((Optimistic)node).getProgramPoint();
2851                             contains = isValid(pp) && isContinuationEntryPoint(pp);
2852                         }
2853                         return !contains;
2854                     }
2855                 });
2856                 return contains;
2857             }
2858         }.get();
2859     }
2860 
2861     private void loadRuntimeNode(final RuntimeNode runtimeNode) {
2862         final List<Expression> args = new ArrayList<>(runtimeNode.getArgs());
2863         if (nullCheck(runtimeNode, args)) {
2864            return;
2865         } else if(undefinedCheck(runtimeNode, args)) {
2866             return;
2867         }
2868         // Revert a false undefined check to a strict equality check
2869         final RuntimeNode newRuntimeNode;
2870         final Request request = runtimeNode.getRequest();
2871         if (Request.isUndefinedCheck(request)) {
2872             newRuntimeNode = runtimeNode.setRequest(request == Request.IS_UNDEFINED ? Request.EQ_STRICT : Request.NE_STRICT);
2873         } else {
2874             newRuntimeNode = runtimeNode;
2875         }
2876 
2877         for (final Expression arg : args) {
2878             loadExpression(arg, TypeBounds.OBJECT);
2879         }
2880 
2881         method.invokestatic(
2882                 CompilerConstants.className(ScriptRuntime.class),
2883                 newRuntimeNode.getRequest().toString(),
2884                 new FunctionSignature(
2885                     false,
2886                     false,
2887                     newRuntimeNode.getType(),
2888                     args.size()).toString());
2889 
2890         method.convert(newRuntimeNode.getType());
2891     }
2892 
2893     private void defineCommonSplitMethodParameters() {
2894         defineSplitMethodParameter(0, CALLEE);
2895         defineSplitMethodParameter(1, THIS);
2896         defineSplitMethodParameter(2, SCOPE);
2897     }
2898 
2899     private void defineSplitMethodParameter(final int slot, final CompilerConstants cc) {
2900         defineSplitMethodParameter(slot, Type.typeFor(cc.type()));
2901     }
2902 
2903     private void defineSplitMethodParameter(final int slot, final Type type) {
2904         method.defineBlockLocalVariable(slot, slot + type.getSlots());
2905         method.onLocalStore(type, slot);
2906     }
2907 
2908     private int fixScopeSlot(final FunctionNode functionNode, final int extraSlot) {
2909         // TODO hack to move the scope to the expected slot (needed because split methods reuse the same slots as the root method)
2910         final int actualScopeSlot = functionNode.compilerConstant(SCOPE).getSlot(SCOPE_TYPE);
2911         final int defaultScopeSlot = SCOPE.slot();
2912         int newExtraSlot = extraSlot;
2913         if (actualScopeSlot != defaultScopeSlot) {
2914             if (actualScopeSlot == extraSlot) {
2915                 newExtraSlot = extraSlot + 1;
2916                 method.defineBlockLocalVariable(newExtraSlot, newExtraSlot + 1);
2917                 method.load(Type.OBJECT, extraSlot);
2918                 method.storeHidden(Type.OBJECT, newExtraSlot);
2919             } else {
2920                 method.defineBlockLocalVariable(actualScopeSlot, actualScopeSlot + 1);
2921             }
2922             method.load(SCOPE_TYPE, defaultScopeSlot);
2923             method.storeCompilerConstant(SCOPE);
2924         }
2925         return newExtraSlot;
2926     }
2927 
2928     @Override
2929     public boolean enterSplitReturn(final SplitReturn splitReturn) {
2930         if (method.isReachable()) {
2931             method.loadUndefined(lc.getCurrentFunction().getReturnType())._return();
2932         }
2933         return false;
2934     }
2935 
2936     @Override
2937     public boolean enterSetSplitState(final SetSplitState setSplitState) {
2938         if (method.isReachable()) {
2939             method.setSplitState(setSplitState.getState());
2940         }
2941         return false;
2942     }
2943 
2944     @Override
2945     public boolean enterSwitchNode(final SwitchNode switchNode) {
2946         if(!method.isReachable()) {
2947             return false;
2948         }
2949         enterStatement(switchNode);
2950 
2951         final Expression     expression  = switchNode.getExpression();
2952         final List<CaseNode> cases       = switchNode.getCases();
2953 
2954         if (cases.isEmpty()) {
2955             // still evaluate expression for side-effects.
2956             loadAndDiscard(expression);
2957             return false;
2958         }
2959 
2960         final CaseNode defaultCase       = switchNode.getDefaultCase();
2961         final Label    breakLabel        = switchNode.getBreakLabel();
2962         final int      liveLocalsOnBreak = method.getUsedSlotsWithLiveTemporaries();
2963 
2964         if (defaultCase != null && cases.size() == 1) {
2965             // default case only
2966             assert cases.get(0) == defaultCase;
2967             loadAndDiscard(expression);
2968             defaultCase.getBody().accept(this);
2969             method.breakLabel(breakLabel, liveLocalsOnBreak);
2970             return false;
2971         }
2972 
2973         // NOTE: it can still change in the tableswitch/lookupswitch case if there's no default case
2974         // but we need to add a synthetic default case for local variable conversions
2975         Label defaultLabel = defaultCase != null ? defaultCase.getEntry() : breakLabel;
2976         final boolean hasSkipConversion = LocalVariableConversion.hasLiveConversion(switchNode);
2977 
2978         if (switchNode.isUniqueInteger()) {
2979             // Tree for sorting values.
2980             final TreeMap<Integer, Label> tree = new TreeMap<>();
2981 
2982             // Build up sorted tree.
2983             for (final CaseNode caseNode : cases) {
2984                 final Node test = caseNode.getTest();
2985 
2986                 if (test != null) {
2987                     final Integer value = (Integer)((LiteralNode<?>)test).getValue();
2988                     final Label   entry = caseNode.getEntry();
2989 
2990                     // Take first duplicate.
2991                     if (!tree.containsKey(value)) {
2992                         tree.put(value, entry);
2993                     }
2994                 }
2995             }
2996 
2997             // Copy values and labels to arrays.
2998             final int       size   = tree.size();
2999             final Integer[] values = tree.keySet().toArray(new Integer[size]);
3000             final Label[]   labels = tree.values().toArray(new Label[size]);
3001 
3002             // Discern low, high and range.
3003             final int lo    = values[0];
3004             final int hi    = values[size - 1];
3005             final long range = (long)hi - (long)lo + 1;
3006 
3007             // Find an unused value for default.
3008             int deflt = Integer.MIN_VALUE;
3009             for (final int value : values) {
3010                 if (deflt == value) {
3011                     deflt++;
3012                 } else if (deflt < value) {
3013                     break;
3014                 }
3015             }
3016 
3017             // Load switch expression.
3018             loadExpressionUnbounded(expression);
3019             final Type type = expression.getType();
3020 
3021             // If expression not int see if we can convert, if not use deflt to trigger default.
3022             if (!type.isInteger()) {
3023                 method.load(deflt);
3024                 final Class<?> exprClass = type.getTypeClass();
3025                 method.invoke(staticCallNoLookup(ScriptRuntime.class, "switchTagAsInt", int.class, exprClass.isPrimitive()? exprClass : Object.class, int.class));
3026             }
3027 
3028             if(hasSkipConversion) {
3029                 assert defaultLabel == breakLabel;
3030                 defaultLabel = new Label("switch_skip");
3031             }
3032             // TABLESWITCH needs (range + 3) 32-bit values; LOOKUPSWITCH needs ((size * 2) + 2). Choose the one with
3033             // smaller representation, favor TABLESWITCH when they're equal size.
3034             if (range + 1 <= (size * 2) && range <= Integer.MAX_VALUE) {
3035                 final Label[] table = new Label[(int)range];
3036                 Arrays.fill(table, defaultLabel);
3037                 for (int i = 0; i < size; i++) {
3038                     final int value = values[i];
3039                     table[value - lo] = labels[i];
3040                 }
3041 
3042                 method.tableswitch(lo, hi, defaultLabel, table);
3043             } else {
3044                 final int[] ints = new int[size];
3045                 for (int i = 0; i < size; i++) {
3046                     ints[i] = values[i];
3047                 }
3048 
3049                 method.lookupswitch(defaultLabel, ints, labels);
3050             }
3051             // This is a synthetic "default case" used in absence of actual default case, created if we need to apply
3052             // local variable conversions if neither case is taken.
3053             if(hasSkipConversion) {
3054                 method.label(defaultLabel);
3055                 method.beforeJoinPoint(switchNode);
3056                 method._goto(breakLabel);
3057             }
3058         } else {
3059             final Symbol tagSymbol = switchNode.getTag();
3060             // TODO: we could have non-object tag
3061             final int tagSlot = tagSymbol.getSlot(Type.OBJECT);
3062             loadExpressionAsObject(expression);
3063             method.store(tagSymbol, Type.OBJECT);
3064 
3065             for (final CaseNode caseNode : cases) {
3066                 final Expression test = caseNode.getTest();
3067 
3068                 if (test != null) {
3069                     method.load(Type.OBJECT, tagSlot);
3070                     loadExpressionAsObject(test);
3071                     method.invoke(ScriptRuntime.EQ_STRICT);
3072                     method.ifne(caseNode.getEntry());
3073                 }
3074             }
3075 
3076             if (defaultCase != null) {
3077                 method._goto(defaultLabel);
3078             } else {
3079                 method.beforeJoinPoint(switchNode);
3080                 method._goto(breakLabel);
3081             }
3082         }
3083 
3084         // First case is only reachable through jump
3085         assert !method.isReachable();
3086 
3087         for (final CaseNode caseNode : cases) {
3088             final Label fallThroughLabel;
3089             if(caseNode.getLocalVariableConversion() != null && method.isReachable()) {
3090                 fallThroughLabel = new Label("fallthrough");
3091                 method._goto(fallThroughLabel);
3092             } else {
3093                 fallThroughLabel = null;
3094             }
3095             method.label(caseNode.getEntry());
3096             method.beforeJoinPoint(caseNode);
3097             if(fallThroughLabel != null) {
3098                 method.label(fallThroughLabel);
3099             }
3100             caseNode.getBody().accept(this);
3101         }
3102 
3103         method.breakLabel(breakLabel, liveLocalsOnBreak);
3104 
3105         return false;
3106     }
3107 
3108     @Override
3109     public boolean enterThrowNode(final ThrowNode throwNode) {
3110         if(!method.isReachable()) {
3111             return false;
3112         }
3113         enterStatement(throwNode);
3114 
3115         if (throwNode.isSyntheticRethrow()) {
3116             method.beforeJoinPoint(throwNode);
3117 
3118             //do not wrap whatever this is in an ecma exception, just rethrow it
3119             final IdentNode exceptionExpr = (IdentNode)throwNode.getExpression();
3120             final Symbol exceptionSymbol = exceptionExpr.getSymbol();
3121             method.load(exceptionSymbol, EXCEPTION_TYPE);
3122             method.checkcast(EXCEPTION_TYPE.getTypeClass());
3123             method.athrow();
3124             return false;
3125         }
3126 
3127         final Source     source     = getCurrentSource();
3128         final Expression expression = throwNode.getExpression();
3129         final int        position   = throwNode.position();
3130         final int        line       = throwNode.getLineNumber();
3131         final int        column     = source.getColumn(position);
3132 
3133         // NOTE: we first evaluate the expression, and only after it was evaluated do we create the new ECMAException
3134         // object and then somewhat cumbersomely move it beneath the evaluated expression on the stack. The reason for
3135         // this is that if expression is optimistic (or contains an optimistic subexpression), we'd potentially access
3136         // the not-yet-<init>ialized object on the stack from the UnwarrantedOptimismException handler, and bytecode
3137         // verifier forbids that.
3138         loadExpressionAsObject(expression);
3139 
3140         method.load(source.getName());
3141         method.load(line);
3142         method.load(column);
3143         method.invoke(ECMAException.CREATE);
3144 
3145         method.beforeJoinPoint(throwNode);
3146         method.athrow();
3147 
3148         return false;
3149     }
3150 
3151     private Source getCurrentSource() {
3152         return lc.getCurrentFunction().getSource();
3153     }
3154 
3155     @Override
3156     public boolean enterTryNode(final TryNode tryNode) {
3157         if(!method.isReachable()) {
3158             return false;
3159         }
3160         enterStatement(tryNode);
3161 
3162         final Block       body        = tryNode.getBody();
3163         final List<Block> catchBlocks = tryNode.getCatchBlocks();
3164         final Symbol      vmException = tryNode.getException();
3165         final Label       entry       = new Label("try");
3166         final Label       recovery    = new Label("catch");
3167         final Label       exit        = new Label("end_try");
3168         final Label       skip        = new Label("skip");
3169 
3170         method.canThrow(recovery);
3171         // Effect any conversions that might be observed at the entry of the catch node before entering the try node.
3172         // This is because even the first instruction in the try block must be presumed to be able to transfer control
3173         // to the catch block. Note that this doesn't kill the original values; in this regard it works a lot like
3174         // conversions of assignments within the try block.
3175         method.beforeTry(tryNode, recovery);
3176         method.label(entry);
3177         catchLabels.push(recovery);
3178         try {
3179             body.accept(this);
3180         } finally {
3181             assert catchLabels.peek() == recovery;
3182             catchLabels.pop();
3183         }
3184 
3185         method.label(exit);
3186         final boolean bodyCanThrow = exit.isAfter(entry);
3187         if(!bodyCanThrow) {
3188             // The body can't throw an exception; don't even bother emitting the catch handlers, they're all dead code.
3189             return false;
3190         }
3191 
3192         method._try(entry, exit, recovery, Throwable.class);
3193 
3194         if (method.isReachable()) {
3195             method._goto(skip);
3196         }
3197 
3198         for (final Block inlinedFinally : tryNode.getInlinedFinallies()) {
3199             TryNode.getLabelledInlinedFinallyBlock(inlinedFinally).accept(this);
3200             // All inlined finallies end with a jump or a return
3201             assert !method.isReachable();
3202         }
3203 
3204 
3205         method._catch(recovery);
3206         method.store(vmException, EXCEPTION_TYPE);
3207 
3208         final int catchBlockCount = catchBlocks.size();
3209         final Label afterCatch = new Label("after_catch");
3210         for (int i = 0; i < catchBlockCount; i++) {
3211             assert method.isReachable();
3212             final Block catchBlock = catchBlocks.get(i);
3213 
3214             // Because of the peculiarities of the flow control, we need to use an explicit push/enterBlock/leaveBlock
3215             // here.
3216             lc.push(catchBlock);
3217             enterBlock(catchBlock);
3218 
3219             final CatchNode  catchNode          = (CatchNode)catchBlocks.get(i).getStatements().get(0);
3220             final IdentNode  exception          = catchNode.getException();
3221             final Expression exceptionCondition = catchNode.getExceptionCondition();
3222             final Block      catchBody          = catchNode.getBody();
3223 
3224             new Store<IdentNode>(exception) {
3225                 @Override
3226                 protected void storeNonDiscard() {
3227                     // This expression is neither part of a discard, nor needs to be left on the stack after it was
3228                     // stored, so we override storeNonDiscard to be a no-op.
3229                 }
3230 
3231                 @Override
3232                 protected void evaluate() {
3233                     if (catchNode.isSyntheticRethrow()) {
3234                         method.load(vmException, EXCEPTION_TYPE);
3235                         return;
3236                     }
3237                     /*
3238                      * If caught object is an instance of ECMAException, then
3239                      * bind obj.thrown to the script catch var. Or else bind the
3240                      * caught object itself to the script catch var.
3241                      */
3242                     final Label notEcmaException = new Label("no_ecma_exception");
3243                     method.load(vmException, EXCEPTION_TYPE).dup()._instanceof(ECMAException.class).ifeq(notEcmaException);
3244                     method.checkcast(ECMAException.class); //TODO is this necessary?
3245                     method.getField(ECMAException.THROWN);
3246                     method.label(notEcmaException);
3247                 }
3248             }.store();
3249 
3250             final boolean isConditionalCatch = exceptionCondition != null;
3251             final Label nextCatch;
3252             if (isConditionalCatch) {
3253                 loadExpressionAsBoolean(exceptionCondition);
3254                 nextCatch = new Label("next_catch");
3255                 nextCatch.markAsBreakTarget();
3256                 method.ifeq(nextCatch);
3257             } else {
3258                 nextCatch = null;
3259             }
3260 
3261             catchBody.accept(this);
3262             leaveBlock(catchBlock);
3263             lc.pop(catchBlock);
3264             if(nextCatch != null) {
3265                 if(method.isReachable()) {
3266                     method._goto(afterCatch);
3267                 }
3268                 method.breakLabel(nextCatch, lc.getUsedSlotCount());
3269             }
3270         }
3271 
3272         // afterCatch could be the same as skip, except that we need to establish that the vmException is dead.
3273         method.label(afterCatch);
3274         if(method.isReachable()) {
3275             method.markDeadLocalVariable(vmException);
3276         }
3277         method.label(skip);
3278 
3279         // Finally body is always inlined elsewhere so it doesn't need to be emitted
3280         assert tryNode.getFinallyBody() == null;
3281 
3282         return false;
3283     }
3284 
3285     @Override
3286     public boolean enterVarNode(final VarNode varNode) {
3287         if(!method.isReachable()) {
3288             return false;
3289         }
3290         final Expression init = varNode.getInit();
3291         final IdentNode identNode = varNode.getName();
3292         final Symbol identSymbol = identNode.getSymbol();
3293         assert identSymbol != null : "variable node " + varNode + " requires a name with a symbol";
3294         final boolean needsScope = identSymbol.isScope();
3295 
3296         if (init == null) {
3297             if (needsScope && varNode.isBlockScoped()) {
3298                 // block scoped variables need a DECLARE flag to signal end of temporal dead zone (TDZ)
3299                 method.loadCompilerConstant(SCOPE);
3300                 method.loadUndefined(Type.OBJECT);
3301                 final int flags = getScopeCallSiteFlags(identSymbol) | (varNode.isBlockScoped() ? CALLSITE_DECLARE : 0);
3302                 assert isFastScope(identSymbol);
3303                 storeFastScopeVar(identSymbol, flags);
3304             }
3305             return false;
3306         }
3307 
3308         enterStatement(varNode);
3309         assert method != null;
3310 
3311         if (needsScope) {
3312             method.loadCompilerConstant(SCOPE);
3313         }
3314 
3315         if (needsScope) {
3316             loadExpressionUnbounded(init);
3317             // block scoped variables need a DECLARE flag to signal end of temporal dead zone (TDZ)
3318             final int flags = getScopeCallSiteFlags(identSymbol) | (varNode.isBlockScoped() ? CALLSITE_DECLARE : 0);
3319             if (isFastScope(identSymbol)) {
3320                 storeFastScopeVar(identSymbol, flags);
3321             } else {
3322                 method.dynamicSet(identNode.getName(), flags, false);
3323             }
3324         } else {
3325             final Type identType = identNode.getType();
3326             if(identType == Type.UNDEFINED) {
3327                 // The initializer is either itself undefined (explicit assignment of undefined to undefined),
3328                 // or the left hand side is a dead variable.
3329                 assert init.getType() == Type.UNDEFINED || identNode.getSymbol().slotCount() == 0;
3330                 loadAndDiscard(init);
3331                 return false;
3332             }
3333             loadExpressionAsType(init, identType);
3334             storeIdentWithCatchConversion(identNode, identType);
3335         }
3336 
3337         return false;
3338     }
3339 
3340     private void storeIdentWithCatchConversion(final IdentNode identNode, final Type type) {
3341         // Assignments happening in try/catch blocks need to ensure that they also store a possibly wider typed value
3342         // that will be live at the exit from the try block
3343         final LocalVariableConversion conversion = identNode.getLocalVariableConversion();
3344         final Symbol symbol = identNode.getSymbol();
3345         if(conversion != null && conversion.isLive()) {
3346             assert symbol == conversion.getSymbol();
3347             assert symbol.isBytecodeLocal();
3348             // Only a single conversion from the target type to the join type is expected.
3349             assert conversion.getNext() == null;
3350             assert conversion.getFrom() == type;
3351             // We must propagate potential type change to the catch block
3352             final Label catchLabel = catchLabels.peek();
3353             assert catchLabel != METHOD_BOUNDARY; // ident conversion only exists in try blocks
3354             assert catchLabel.isReachable();
3355             final Type joinType = conversion.getTo();
3356             final Label.Stack catchStack = catchLabel.getStack();
3357             final int joinSlot = symbol.getSlot(joinType);
3358             // With nested try/catch blocks (incl. synthetic ones for finally), we can have a supposed conversion for
3359             // the exception symbol in the nested catch, but it isn't live in the outer catch block, so prevent doing
3360             // conversions for it. E.g. in "try { try { ... } catch(e) { e = 1; } } catch(e2) { ... }", we must not
3361             // introduce an I->O conversion on "e = 1" assignment as "e" is not live in "catch(e2)".
3362             if(catchStack.getUsedSlotsWithLiveTemporaries() > joinSlot) {
3363                 method.dup();
3364                 method.convert(joinType);
3365                 method.store(symbol, joinType);
3366                 catchLabel.getStack().onLocalStore(joinType, joinSlot, true);
3367                 method.canThrow(catchLabel);
3368                 // Store but keep the previous store live too.
3369                 method.store(symbol, type, false);
3370                 return;
3371             }
3372         }
3373 
3374         method.store(symbol, type, true);
3375     }
3376 
3377     @Override
3378     public boolean enterWhileNode(final WhileNode whileNode) {
3379         if(!method.isReachable()) {
3380             return false;
3381         }
3382         if(whileNode.isDoWhile()) {
3383             enterDoWhile(whileNode);
3384         } else {
3385             enterStatement(whileNode);
3386             enterForOrWhile(whileNode, null);
3387         }
3388         return false;
3389     }
3390 
3391     private void enterForOrWhile(final LoopNode loopNode, final JoinPredecessorExpression modify) {
3392         // NOTE: the usual pattern for compiling test-first loops is "GOTO test; body; test; IFNE body". We use the less
3393         // conventional "test; IFEQ break; body; GOTO test; break;". It has one extra unconditional GOTO in each repeat
3394         // of the loop, but it's not a problem for modern JIT compilers. We do this because our local variable type
3395         // tracking is unfortunately not really prepared for out-of-order execution, e.g. compiling the following
3396         // contrived but legal JavaScript code snippet would fail because the test changes the type of "i" from object
3397         // to double: var i = {valueOf: function() { return 1} }; while(--i >= 0) { ... }
3398         // Instead of adding more complexity to the local variable type tracking, we instead choose to emit this
3399         // different code shape.
3400         final int liveLocalsOnBreak = method.getUsedSlotsWithLiveTemporaries();
3401         final JoinPredecessorExpression test = loopNode.getTest();
3402         if(Expression.isAlwaysFalse(test)) {
3403             loadAndDiscard(test);
3404             return;
3405         }
3406 
3407         method.beforeJoinPoint(loopNode);
3408 
3409         final Label continueLabel = loopNode.getContinueLabel();
3410         final Label repeatLabel = modify != null ? new Label("for_repeat") : continueLabel;
3411         method.label(repeatLabel);
3412         final int liveLocalsOnContinue = method.getUsedSlotsWithLiveTemporaries();
3413 
3414         final Block   body                  = loopNode.getBody();
3415         final Label   breakLabel            = loopNode.getBreakLabel();
3416         final boolean testHasLiveConversion = test != null && LocalVariableConversion.hasLiveConversion(test);
3417 
3418         if(Expression.isAlwaysTrue(test)) {
3419             if(test != null) {
3420                 loadAndDiscard(test);
3421                 if(testHasLiveConversion) {
3422                     method.beforeJoinPoint(test);
3423                 }
3424             }
3425         } else if (test != null) {
3426             if (testHasLiveConversion) {
3427                 emitBranch(test.getExpression(), body.getEntryLabel(), true);
3428                 method.beforeJoinPoint(test);
3429                 method._goto(breakLabel);
3430             } else {
3431                 emitBranch(test.getExpression(), breakLabel, false);
3432             }
3433         }
3434 
3435         body.accept(this);
3436         if(repeatLabel != continueLabel) {
3437             emitContinueLabel(continueLabel, liveLocalsOnContinue);
3438         }
3439 
3440         if (loopNode.hasPerIterationScope() && lc.getCurrentBlock().needsScope()) {
3441             // ES6 for loops with LET init need a new scope for each iteration. We just create a shallow copy here.
3442             method.loadCompilerConstant(SCOPE);
3443             method.invoke(virtualCallNoLookup(ScriptObject.class, "copy", ScriptObject.class));
3444             method.storeCompilerConstant(SCOPE);
3445         }
3446 
3447         if(method.isReachable()) {
3448             if(modify != null) {
3449                 lineNumber(loopNode);
3450                 loadAndDiscard(modify);
3451                 method.beforeJoinPoint(modify);
3452             }
3453             method._goto(repeatLabel);
3454         }
3455 
3456         method.breakLabel(breakLabel, liveLocalsOnBreak);
3457     }
3458 
3459     private void emitContinueLabel(final Label continueLabel, final int liveLocals) {
3460         final boolean reachable = method.isReachable();
3461         method.breakLabel(continueLabel, liveLocals);
3462         // If we reach here only through a continue statement (e.g. body does not exit normally) then the
3463         // continueLabel can have extra non-temp symbols (e.g. exception from a try/catch contained in the body). We
3464         // must make sure those are thrown away.
3465         if(!reachable) {
3466             method.undefineLocalVariables(lc.getUsedSlotCount(), false);
3467         }
3468     }
3469 
3470     private void enterDoWhile(final WhileNode whileNode) {
3471         final int liveLocalsOnContinueOrBreak = method.getUsedSlotsWithLiveTemporaries();
3472         method.beforeJoinPoint(whileNode);
3473 
3474         final Block body = whileNode.getBody();
3475         body.accept(this);
3476 
3477         emitContinueLabel(whileNode.getContinueLabel(), liveLocalsOnContinueOrBreak);
3478         if(method.isReachable()) {
3479             lineNumber(whileNode);
3480             final JoinPredecessorExpression test = whileNode.getTest();
3481             final Label bodyEntryLabel = body.getEntryLabel();
3482             final boolean testHasLiveConversion = LocalVariableConversion.hasLiveConversion(test);
3483             if(Expression.isAlwaysFalse(test)) {
3484                 loadAndDiscard(test);
3485                 if(testHasLiveConversion) {
3486                     method.beforeJoinPoint(test);
3487                 }
3488             } else if(testHasLiveConversion) {
3489                 // If we have conversions after the test in do-while, they need to be effected on both branches.
3490                 final Label beforeExit = new Label("do_while_preexit");
3491                 emitBranch(test.getExpression(), beforeExit, false);
3492                 method.beforeJoinPoint(test);
3493                 method._goto(bodyEntryLabel);
3494                 method.label(beforeExit);
3495                 method.beforeJoinPoint(test);
3496             } else {
3497                 emitBranch(test.getExpression(), bodyEntryLabel, true);
3498             }
3499         }
3500         method.breakLabel(whileNode.getBreakLabel(), liveLocalsOnContinueOrBreak);
3501     }
3502 
3503 
3504     @Override
3505     public boolean enterWithNode(final WithNode withNode) {
3506         if(!method.isReachable()) {
3507             return false;
3508         }
3509         enterStatement(withNode);
3510         final Expression expression = withNode.getExpression();
3511         final Block      body       = withNode.getBody();
3512 
3513         // It is possible to have a "pathological" case where the with block does not reference *any* identifiers. It's
3514         // pointless, but legal. In that case, if nothing else in the method forced the assignment of a slot to the
3515         // scope object, its' possible that it won't have a slot assigned. In this case we'll only evaluate expression
3516         // for its side effect and visit the body, and not bother opening and closing a WithObject.
3517         final boolean hasScope = method.hasScope();
3518 
3519         if (hasScope) {
3520             method.loadCompilerConstant(SCOPE);
3521         }
3522 
3523         loadExpressionAsObject(expression);
3524 
3525         final Label tryLabel;
3526         if (hasScope) {
3527             // Construct a WithObject if we have a scope
3528             method.invoke(ScriptRuntime.OPEN_WITH);
3529             method.storeCompilerConstant(SCOPE);
3530             tryLabel = new Label("with_try");
3531             method.label(tryLabel);
3532         } else {
3533             // We just loaded the expression for its side effect and to check
3534             // for null or undefined value.
3535             globalCheckObjectCoercible();
3536             tryLabel = null;
3537         }
3538 
3539         // Always process body
3540         body.accept(this);
3541 
3542         if (hasScope) {
3543             // Ensure we always close the WithObject
3544             final Label endLabel   = new Label("with_end");
3545             final Label catchLabel = new Label("with_catch");
3546             final Label exitLabel  = new Label("with_exit");
3547 
3548             method.label(endLabel);
3549             // Somewhat conservatively presume that if the body is not empty, it can throw an exception. In any case,
3550             // we must prevent trying to emit a try-catch for empty range, as it causes a verification error.
3551             final boolean bodyCanThrow = endLabel.isAfter(tryLabel);
3552             if(bodyCanThrow) {
3553                 method._try(tryLabel, endLabel, catchLabel);
3554             }
3555 
3556             final boolean reachable = method.isReachable();
3557             if(reachable) {
3558                 popScope();
3559                 if(bodyCanThrow) {
3560                     method._goto(exitLabel);
3561                 }
3562             }
3563 
3564             if(bodyCanThrow) {
3565                 method._catch(catchLabel);
3566                 popScopeException();
3567                 method.athrow();
3568                 if(reachable) {
3569                     method.label(exitLabel);
3570                 }
3571             }
3572         }
3573         return false;
3574     }
3575 
3576     private void loadADD(final UnaryNode unaryNode, final TypeBounds resultBounds) {
3577         loadExpression(unaryNode.getExpression(), resultBounds.booleanToInt().notWiderThan(Type.NUMBER));
3578         if(method.peekType() == Type.BOOLEAN) {
3579             // It's a no-op in bytecode, but we must make sure it is treated as an int for purposes of type signatures
3580             method.convert(Type.INT);
3581         }
3582     }
3583 
3584     private void loadBIT_NOT(final UnaryNode unaryNode) {
3585         loadExpression(unaryNode.getExpression(), TypeBounds.INT).load(-1).xor();
3586     }
3587 
3588     private void loadDECINC(final UnaryNode unaryNode) {
3589         final Expression operand     = unaryNode.getExpression();
3590         final Type       type        = unaryNode.getType();
3591         final TypeBounds typeBounds  = new TypeBounds(type, Type.NUMBER);
3592         final TokenType  tokenType   = unaryNode.tokenType();
3593         final boolean    isPostfix   = tokenType == TokenType.DECPOSTFIX || tokenType == TokenType.INCPOSTFIX;
3594         final boolean    isIncrement = tokenType == TokenType.INCPREFIX || tokenType == TokenType.INCPOSTFIX;
3595 
3596         assert !type.isObject();
3597 
3598         new SelfModifyingStore<UnaryNode>(unaryNode, operand) {
3599 
3600             private void loadRhs() {
3601                 loadExpression(operand, typeBounds, true);
3602             }
3603 
3604             @Override
3605             protected void evaluate() {
3606                 if(isPostfix) {
3607                     loadRhs();
3608                 } else {
3609                     new OptimisticOperation(unaryNode, typeBounds) {
3610                         @Override
3611                         void loadStack() {
3612                             loadRhs();
3613                             loadMinusOne();
3614                         }
3615                         @Override
3616                         void consumeStack() {
3617                             doDecInc(getProgramPoint());
3618                         }
3619                     }.emit(getOptimisticIgnoreCountForSelfModifyingExpression(operand));
3620                 }
3621             }
3622 
3623             @Override
3624             protected void storeNonDiscard() {
3625                 super.storeNonDiscard();
3626                 if (isPostfix) {
3627                     new OptimisticOperation(unaryNode, typeBounds) {
3628                         @Override
3629                         void loadStack() {
3630                             loadMinusOne();
3631                         }
3632                         @Override
3633                         void consumeStack() {
3634                             doDecInc(getProgramPoint());
3635                         }
3636                     }.emit(1); // 1 for non-incremented result on the top of the stack pushed in evaluate()
3637                 }
3638             }
3639 
3640             private void loadMinusOne() {
3641                 if (type.isInteger()) {
3642                     method.load(isIncrement ? 1 : -1);
3643                 } else if (type.isLong()) {
3644                     method.load(isIncrement ? 1L : -1L);
3645                 } else {
3646                     method.load(isIncrement ? 1.0 : -1.0);
3647                 }
3648             }
3649 
3650             private void doDecInc(final int programPoint) {
3651                 method.add(programPoint);
3652             }
3653         }.store();
3654     }
3655 
3656     private static int getOptimisticIgnoreCountForSelfModifyingExpression(final Expression target) {
3657         return target instanceof AccessNode ? 1 : target instanceof IndexNode ? 2 : 0;
3658     }
3659 
3660     private void loadAndDiscard(final Expression expr) {
3661         // TODO: move checks for discarding to actual expression load code (e.g. as we do with void). That way we might
3662         // be able to eliminate even more checks.
3663         if(expr instanceof PrimitiveLiteralNode | isLocalVariable(expr)) {
3664             assert !lc.isCurrentDiscard(expr);
3665             // Don't bother evaluating expressions without side effects. Typical usage is "void 0" for reliably generating
3666             // undefined.
3667             return;
3668         }
3669 
3670         lc.pushDiscard(expr);
3671         loadExpression(expr, TypeBounds.UNBOUNDED);
3672         if (lc.popDiscardIfCurrent(expr)) {
3673             assert !expr.isAssignment();
3674             // NOTE: if we had a way to load with type void, we could avoid popping
3675             method.pop();
3676         }
3677     }
3678 
3679     /**
3680      * Loads the expression with the specified type bounds, but if the parent expression is the current discard,
3681      * then instead loads and discards the expression.
3682      * @param parent the parent expression that's tested for being the current discard
3683      * @param expr the expression that's either normally loaded or discard-loaded
3684      * @param resultBounds result bounds for when loading the expression normally
3685      */
3686     private void loadMaybeDiscard(final Expression parent, final Expression expr, final TypeBounds resultBounds) {
3687         loadMaybeDiscard(lc.popDiscardIfCurrent(parent), expr, resultBounds);
3688     }
3689 
3690     /**
3691      * Loads the expression with the specified type bounds, or loads and discards the expression, depending on the
3692      * value of the discard flag. Useful as a helper for expressions with control flow where you often can't combine
3693      * testing for being the current discard and loading the subexpressions.
3694      * @param discard if true, the expression is loaded and discarded
3695      * @param expr the expression that's either normally loaded or discard-loaded
3696      * @param resultBounds result bounds for when loading the expression normally
3697      */
3698     private void loadMaybeDiscard(final boolean discard, final Expression expr, final TypeBounds resultBounds) {
3699         if (discard) {
3700             loadAndDiscard(expr);
3701         } else {
3702             loadExpression(expr, resultBounds);
3703         }
3704     }
3705 
3706     private void loadNEW(final UnaryNode unaryNode) {
3707         final CallNode callNode = (CallNode)unaryNode.getExpression();
3708         final List<Expression> args   = callNode.getArgs();
3709 
3710         final Expression func = callNode.getFunction();
3711         // Load function reference.
3712         loadExpressionAsObject(func); // must detect type error
3713 
3714         method.dynamicNew(1 + loadArgs(args), getCallSiteFlags(),
3715             func instanceof IdentNode? ((IdentNode)func).getName() : null);
3716     }
3717 
3718     private void loadNOT(final UnaryNode unaryNode) {
3719         final Expression expr = unaryNode.getExpression();
3720         if(expr instanceof UnaryNode && expr.isTokenType(TokenType.NOT)) {
3721             // !!x is idiomatic boolean cast in JavaScript
3722             loadExpressionAsBoolean(((UnaryNode)expr).getExpression());
3723         } else {
3724             final Label trueLabel  = new Label("true");
3725             final Label afterLabel = new Label("after");
3726 
3727             emitBranch(expr, trueLabel, true);
3728             method.load(true);
3729             method._goto(afterLabel);
3730             method.label(trueLabel);
3731             method.load(false);
3732             method.label(afterLabel);
3733         }
3734     }
3735 
3736     private void loadSUB(final UnaryNode unaryNode, final TypeBounds resultBounds) {
3737         final Type type = unaryNode.getType();
3738         assert type.isNumeric();
3739         final TypeBounds numericBounds = resultBounds.booleanToInt();
3740         new OptimisticOperation(unaryNode, numericBounds) {
3741             @Override
3742             void loadStack() {
3743                 final Expression expr = unaryNode.getExpression();
3744                 loadExpression(expr, numericBounds.notWiderThan(Type.NUMBER));
3745             }
3746             @Override
3747             void consumeStack() {
3748                 // Must do an explicit conversion to the operation's type when it's double so that we correctly handle
3749                 // negation of an int 0 to a double -0. With this, we get the correct negation of a local variable after
3750                 // it deoptimized, e.g. "iload_2; i2d; dneg". Without this, we get "iload_2; ineg; i2d".
3751                 if(type.isNumber()) {
3752                     method.convert(type);
3753                 }
3754                 method.neg(getProgramPoint());
3755             }
3756         }.emit();
3757     }
3758 
3759     public void loadVOID(final UnaryNode unaryNode, final TypeBounds resultBounds) {
3760         loadAndDiscard(unaryNode.getExpression());
3761         if (!lc.popDiscardIfCurrent(unaryNode)) {
3762             method.loadUndefined(resultBounds.widest);
3763         }
3764     }
3765 
3766     public void loadADD(final BinaryNode binaryNode, final TypeBounds resultBounds) {
3767         new OptimisticOperation(binaryNode, resultBounds) {
3768             @Override
3769             void loadStack() {
3770                 final TypeBounds operandBounds;
3771                 final boolean isOptimistic = isValid(getProgramPoint());
3772                 boolean forceConversionSeparation = false;
3773                 if(isOptimistic) {
3774                     operandBounds = new TypeBounds(binaryNode.getType(), Type.OBJECT);
3775                 } else {
3776                     // Non-optimistic, non-FP +. Allow it to overflow.
3777                     final Type widestOperationType = binaryNode.getWidestOperationType();
3778                     operandBounds = new TypeBounds(Type.narrowest(binaryNode.getWidestOperandType(), resultBounds.widest), widestOperationType);
3779                     forceConversionSeparation = widestOperationType.narrowerThan(resultBounds.widest);
3780                 }
3781                 loadBinaryOperands(binaryNode.lhs(), binaryNode.rhs(), operandBounds, false, forceConversionSeparation);
3782             }
3783 
3784             @Override
3785             void consumeStack() {
3786                 method.add(getProgramPoint());
3787             }
3788         }.emit();
3789     }
3790 
3791     private void loadAND_OR(final BinaryNode binaryNode, final TypeBounds resultBounds, final boolean isAnd) {
3792         final Type narrowestOperandType = Type.widestReturnType(binaryNode.lhs().getType(), binaryNode.rhs().getType());
3793 
3794         final boolean isCurrentDiscard = lc.popDiscardIfCurrent(binaryNode);
3795 
3796         final Label skip = new Label("skip");
3797         if(narrowestOperandType == Type.BOOLEAN) {
3798             // optimize all-boolean logical expressions
3799             final Label onTrue = new Label("andor_true");
3800             emitBranch(binaryNode, onTrue, true);
3801             if (isCurrentDiscard) {
3802                 method.label(onTrue);
3803             } else {
3804                 method.load(false);
3805                 method._goto(skip);
3806                 method.label(onTrue);
3807                 method.load(true);
3808                 method.label(skip);
3809             }
3810             return;
3811         }
3812 
3813         final TypeBounds outBounds = resultBounds.notNarrowerThan(narrowestOperandType);
3814         final JoinPredecessorExpression lhs = (JoinPredecessorExpression)binaryNode.lhs();
3815         final boolean lhsConvert = LocalVariableConversion.hasLiveConversion(lhs);
3816         final Label evalRhs = lhsConvert ? new Label("eval_rhs") : null;
3817 
3818         loadExpression(lhs, outBounds);
3819         if (!isCurrentDiscard) {
3820             method.dup();
3821         }
3822         method.convert(Type.BOOLEAN);
3823         if (isAnd) {
3824             if(lhsConvert) {
3825                 method.ifne(evalRhs);
3826             } else {
3827                 method.ifeq(skip);
3828             }
3829         } else if(lhsConvert) {
3830             method.ifeq(evalRhs);
3831         } else {
3832             method.ifne(skip);
3833         }
3834 
3835         if(lhsConvert) {
3836             method.beforeJoinPoint(lhs);
3837             method._goto(skip);
3838             method.label(evalRhs);
3839         }
3840 
3841         if (!isCurrentDiscard) {
3842             method.pop();
3843         }
3844         final JoinPredecessorExpression rhs = (JoinPredecessorExpression)binaryNode.rhs();
3845         loadMaybeDiscard(isCurrentDiscard, rhs, outBounds);
3846         method.beforeJoinPoint(rhs);
3847         method.label(skip);
3848     }
3849 
3850     private static boolean isLocalVariable(final Expression lhs) {
3851         return lhs instanceof IdentNode && isLocalVariable((IdentNode)lhs);
3852     }
3853 
3854     private static boolean isLocalVariable(final IdentNode lhs) {
3855         return lhs.getSymbol().isBytecodeLocal();
3856     }
3857 
3858     // NOTE: does not use resultBounds as the assignment is driven by the type of the RHS
3859     private void loadASSIGN(final BinaryNode binaryNode) {
3860         final Expression lhs = binaryNode.lhs();
3861         final Expression rhs = binaryNode.rhs();
3862 
3863         final Type rhsType = rhs.getType();
3864         // Detect dead assignments
3865         if(lhs instanceof IdentNode) {
3866             final Symbol symbol = ((IdentNode)lhs).getSymbol();
3867             if(!symbol.isScope() && !symbol.hasSlotFor(rhsType) && lc.popDiscardIfCurrent(binaryNode)) {
3868                 loadAndDiscard(rhs);
3869                 method.markDeadLocalVariable(symbol);
3870                 return;
3871             }
3872         }
3873 
3874         new Store<BinaryNode>(binaryNode, lhs) {
3875             @Override
3876             protected void evaluate() {
3877                 // NOTE: we're loading with "at least as wide as" so optimistic operations on the right hand side
3878                 // remain optimistic, and then explicitly convert to the required type if needed.
3879                 loadExpressionAsType(rhs, rhsType);
3880             }
3881         }.store();
3882     }
3883 
3884     /**
3885      * Binary self-assignment that can be optimistic: +=, -=, *=, and /=.
3886      */
3887     private abstract class BinaryOptimisticSelfAssignment extends SelfModifyingStore<BinaryNode> {
3888 
3889         /**
3890          * Constructor
3891          *
3892          * @param node the assign op node
3893          */
3894         BinaryOptimisticSelfAssignment(final BinaryNode node) {
3895             super(node, node.lhs());
3896         }
3897 
3898         protected abstract void op(OptimisticOperation oo);
3899 
3900         @Override
3901         protected void evaluate() {
3902             final Expression lhs = assignNode.lhs();
3903             final Expression rhs = assignNode.rhs();
3904             final Type widestOperationType = assignNode.getWidestOperationType();
3905             final TypeBounds bounds = new TypeBounds(assignNode.getType(), widestOperationType);
3906             new OptimisticOperation(assignNode, bounds) {
3907                 @Override
3908                 void loadStack() {
3909                     final boolean forceConversionSeparation;
3910                     if (isValid(getProgramPoint()) || widestOperationType == Type.NUMBER) {
3911                         forceConversionSeparation = false;
3912                     } else {
3913                         final Type operandType = Type.widest(booleanToInt(objectToNumber(lhs.getType())), booleanToInt(objectToNumber(rhs.getType())));
3914                         forceConversionSeparation = operandType.narrowerThan(widestOperationType);
3915                     }
3916                     loadBinaryOperands(lhs, rhs, bounds, true, forceConversionSeparation);
3917                 }
3918                 @Override
3919                 void consumeStack() {
3920                     op(this);
3921                 }
3922             }.emit(getOptimisticIgnoreCountForSelfModifyingExpression(lhs));
3923             method.convert(assignNode.getType());
3924         }
3925     }
3926 
3927     /**
3928      * Non-optimistic binary self-assignment operation. Basically, everything except +=, -=, *=, and /=.
3929      */
3930     private abstract class BinarySelfAssignment extends SelfModifyingStore<BinaryNode> {
3931         BinarySelfAssignment(final BinaryNode node) {
3932             super(node, node.lhs());
3933         }
3934 
3935         protected abstract void op();
3936 
3937         @Override
3938         protected void evaluate() {
3939             loadBinaryOperands(assignNode.lhs(), assignNode.rhs(), TypeBounds.UNBOUNDED.notWiderThan(assignNode.getWidestOperandType()), true, false);
3940             op();
3941         }
3942     }
3943 
3944     private void loadASSIGN_ADD(final BinaryNode binaryNode) {
3945         new BinaryOptimisticSelfAssignment(binaryNode) {
3946             @Override
3947             protected void op(final OptimisticOperation oo) {
3948                 assert !(binaryNode.getType().isObject() && oo.isOptimistic);
3949                 method.add(oo.getProgramPoint());
3950             }
3951         }.store();
3952     }
3953 
3954     private void loadASSIGN_BIT_AND(final BinaryNode binaryNode) {
3955         new BinarySelfAssignment(binaryNode) {
3956             @Override
3957             protected void op() {
3958                 method.and();
3959             }
3960         }.store();
3961     }
3962 
3963     private void loadASSIGN_BIT_OR(final BinaryNode binaryNode) {
3964         new BinarySelfAssignment(binaryNode) {
3965             @Override
3966             protected void op() {
3967                 method.or();
3968             }
3969         }.store();
3970     }
3971 
3972     private void loadASSIGN_BIT_XOR(final BinaryNode binaryNode) {
3973         new BinarySelfAssignment(binaryNode) {
3974             @Override
3975             protected void op() {
3976                 method.xor();
3977             }
3978         }.store();
3979     }
3980 
3981     private void loadASSIGN_DIV(final BinaryNode binaryNode) {
3982         new BinaryOptimisticSelfAssignment(binaryNode) {
3983             @Override
3984             protected void op(final OptimisticOperation oo) {
3985                 method.div(oo.getProgramPoint());
3986             }
3987         }.store();
3988     }
3989 
3990     private void loadASSIGN_MOD(final BinaryNode binaryNode) {
3991         new BinaryOptimisticSelfAssignment(binaryNode) {
3992             @Override
3993             protected void op(final OptimisticOperation oo) {
3994                 method.rem(oo.getProgramPoint());
3995             }
3996         }.store();
3997     }
3998 
3999     private void loadASSIGN_MUL(final BinaryNode binaryNode) {
4000         new BinaryOptimisticSelfAssignment(binaryNode) {
4001             @Override
4002             protected void op(final OptimisticOperation oo) {
4003                 method.mul(oo.getProgramPoint());
4004             }
4005         }.store();
4006     }
4007 
4008     private void loadASSIGN_SAR(final BinaryNode binaryNode) {
4009         new BinarySelfAssignment(binaryNode) {
4010             @Override
4011             protected void op() {
4012                 method.sar();
4013             }
4014         }.store();
4015     }
4016 
4017     private void loadASSIGN_SHL(final BinaryNode binaryNode) {
4018         new BinarySelfAssignment(binaryNode) {
4019             @Override
4020             protected void op() {
4021                 method.shl();
4022             }
4023         }.store();
4024     }
4025 
4026     private void loadASSIGN_SHR(final BinaryNode binaryNode) {
4027         new BinarySelfAssignment(binaryNode) {
4028             @Override
4029             protected void op() {
4030                 doSHR();
4031             }
4032 
4033         }.store();
4034     }
4035 
4036     private void doSHR() {
4037         // TODO: make SHR optimistic
4038         method.shr();
4039         toUint();
4040     }
4041 
4042     private void toUint() {
4043         JSType.TO_UINT32_I.invoke(method);
4044     }
4045 
4046     private void loadASSIGN_SUB(final BinaryNode binaryNode) {
4047         new BinaryOptimisticSelfAssignment(binaryNode) {
4048             @Override
4049             protected void op(final OptimisticOperation oo) {
4050                 method.sub(oo.getProgramPoint());
4051             }
4052         }.store();
4053     }
4054 
4055     /**
4056      * Helper class for binary arithmetic ops
4057      */
4058     private abstract class BinaryArith {
4059         protected abstract void op(int programPoint);
4060 
4061         protected void evaluate(final BinaryNode node, final TypeBounds resultBounds) {
4062             final TypeBounds numericBounds = resultBounds.booleanToInt().objectToNumber();
4063             new OptimisticOperation(node, numericBounds) {
4064                 @Override
4065                 void loadStack() {
4066                     final TypeBounds operandBounds;
4067                     boolean forceConversionSeparation = false;
4068                     if(numericBounds.narrowest == Type.NUMBER) {
4069                         // Result should be double always. Propagate it into the operands so we don't have lots of I2D
4070                         // and L2D after operand evaluation.
4071                         assert numericBounds.widest == Type.NUMBER;
4072                         operandBounds = numericBounds;
4073                     } else {
4074                         final boolean isOptimistic = isValid(getProgramPoint());
4075                         if(isOptimistic || node.isTokenType(TokenType.DIV) || node.isTokenType(TokenType.MOD)) {
4076                             operandBounds = new TypeBounds(node.getType(), Type.NUMBER);
4077                         } else {
4078                             // Non-optimistic, non-FP subtraction or multiplication. Allow them to overflow.
4079                             operandBounds = new TypeBounds(Type.narrowest(node.getWidestOperandType(),
4080                                     numericBounds.widest), Type.NUMBER);
4081                             forceConversionSeparation = node.getWidestOperationType().narrowerThan(numericBounds.widest);
4082                         }
4083                     }
4084                     loadBinaryOperands(node.lhs(), node.rhs(), operandBounds, false, forceConversionSeparation);
4085                 }
4086 
4087                 @Override
4088                 void consumeStack() {
4089                     op(getProgramPoint());
4090                 }
4091             }.emit();
4092         }
4093     }
4094 
4095     private void loadBIT_AND(final BinaryNode binaryNode) {
4096         loadBinaryOperands(binaryNode);
4097         method.and();
4098     }
4099 
4100     private void loadBIT_OR(final BinaryNode binaryNode) {
4101         // Optimize x|0 to (int)x
4102         if (isRhsZero(binaryNode)) {
4103             loadExpressionAsType(binaryNode.lhs(), Type.INT);
4104         } else {
4105             loadBinaryOperands(binaryNode);
4106             method.or();
4107         }
4108     }
4109 
4110     private static boolean isRhsZero(final BinaryNode binaryNode) {
4111         final Expression rhs = binaryNode.rhs();
4112         return rhs instanceof LiteralNode && INT_ZERO.equals(((LiteralNode<?>)rhs).getValue());
4113     }
4114 
4115     private void loadBIT_XOR(final BinaryNode binaryNode) {
4116         loadBinaryOperands(binaryNode);
4117         method.xor();
4118     }
4119 
4120     private void loadCOMMARIGHT(final BinaryNode binaryNode, final TypeBounds resultBounds) {
4121         loadAndDiscard(binaryNode.lhs());
4122         loadMaybeDiscard(binaryNode, binaryNode.rhs(), resultBounds);
4123     }
4124 
4125     private void loadCOMMALEFT(final BinaryNode binaryNode, final TypeBounds resultBounds) {
4126         loadMaybeDiscard(binaryNode, binaryNode.lhs(), resultBounds);
4127         loadAndDiscard(binaryNode.rhs());
4128     }
4129 
4130     private void loadDIV(final BinaryNode binaryNode, final TypeBounds resultBounds) {
4131         new BinaryArith() {
4132             @Override
4133             protected void op(final int programPoint) {
4134                 method.div(programPoint);
4135             }
4136         }.evaluate(binaryNode, resultBounds);
4137     }
4138 
4139     private void loadCmp(final BinaryNode binaryNode, final Condition cond) {
4140         loadComparisonOperands(binaryNode);
4141 
4142         final Label trueLabel  = new Label("trueLabel");
4143         final Label afterLabel = new Label("skip");
4144 
4145         method.conditionalJump(cond, trueLabel);
4146 
4147         method.load(Boolean.FALSE);
4148         method._goto(afterLabel);
4149         method.label(trueLabel);
4150         method.load(Boolean.TRUE);
4151         method.label(afterLabel);
4152     }
4153 
4154     private void loadMOD(final BinaryNode binaryNode, final TypeBounds resultBounds) {
4155         new BinaryArith() {
4156             @Override
4157             protected void op(final int programPoint) {
4158                 method.rem(programPoint);
4159             }
4160         }.evaluate(binaryNode, resultBounds);
4161     }
4162 
4163     private void loadMUL(final BinaryNode binaryNode, final TypeBounds resultBounds) {
4164         new BinaryArith() {
4165             @Override
4166             protected void op(final int programPoint) {
4167                 method.mul(programPoint);
4168             }
4169         }.evaluate(binaryNode, resultBounds);
4170     }
4171 
4172     private void loadSAR(final BinaryNode binaryNode) {
4173         loadBinaryOperands(binaryNode);
4174         method.sar();
4175     }
4176 
4177     private void loadSHL(final BinaryNode binaryNode) {
4178         loadBinaryOperands(binaryNode);
4179         method.shl();
4180     }
4181 
4182     private void loadSHR(final BinaryNode binaryNode) {
4183         // Optimize x >>> 0 to (uint)x
4184         if (isRhsZero(binaryNode)) {
4185             loadExpressionAsType(binaryNode.lhs(), Type.INT);
4186             toUint();
4187         } else {
4188             loadBinaryOperands(binaryNode);
4189             doSHR();
4190         }
4191     }
4192 
4193     private void loadSUB(final BinaryNode binaryNode, final TypeBounds resultBounds) {
4194         new BinaryArith() {
4195             @Override
4196             protected void op(final int programPoint) {
4197                 method.sub(programPoint);
4198             }
4199         }.evaluate(binaryNode, resultBounds);
4200     }
4201 
4202     @Override
4203     public boolean enterLabelNode(final LabelNode labelNode) {
4204         labeledBlockBreakLiveLocals.push(lc.getUsedSlotCount());
4205         return true;
4206     }
4207 
4208     @Override
4209     protected boolean enterDefault(final Node node) {
4210         throw new AssertionError("Code generator entered node of type " + node.getClass().getName());
4211     }
4212 
4213     private void loadTernaryNode(final TernaryNode ternaryNode, final TypeBounds resultBounds) {
4214         final Expression test = ternaryNode.getTest();
4215         final JoinPredecessorExpression trueExpr  = ternaryNode.getTrueExpression();
4216         final JoinPredecessorExpression falseExpr = ternaryNode.getFalseExpression();
4217 
4218         final Label falseLabel = new Label("ternary_false");
4219         final Label exitLabel  = new Label("ternary_exit");
4220 
4221         final Type outNarrowest = Type.narrowest(resultBounds.widest, Type.generic(Type.widestReturnType(trueExpr.getType(), falseExpr.getType())));
4222         final TypeBounds outBounds = resultBounds.notNarrowerThan(outNarrowest);
4223 
4224         emitBranch(test, falseLabel, false);
4225 
4226         final boolean isCurrentDiscard = lc.popDiscardIfCurrent(ternaryNode);
4227         loadMaybeDiscard(isCurrentDiscard, trueExpr.getExpression(), outBounds);
4228         assert isCurrentDiscard || Type.generic(method.peekType()) == outBounds.narrowest;
4229         method.beforeJoinPoint(trueExpr);
4230         method._goto(exitLabel);
4231         method.label(falseLabel);
4232         loadMaybeDiscard(isCurrentDiscard, falseExpr.getExpression(), outBounds);
4233         assert isCurrentDiscard || Type.generic(method.peekType()) == outBounds.narrowest;
4234         method.beforeJoinPoint(falseExpr);
4235         method.label(exitLabel);
4236     }
4237 
4238     /**
4239      * Generate all shared scope calls generated during codegen.
4240      */
4241     void generateScopeCalls() {
4242         for (final SharedScopeCall scopeAccess : lc.getScopeCalls()) {
4243             scopeAccess.generateScopeCall();
4244         }
4245     }
4246 
4247     /**
4248      * Debug code used to print symbols
4249      *
4250      * @param block the block we are in
4251      * @param function the function we are in
4252      * @param ident identifier for block or function where applicable
4253      */
4254     private void printSymbols(final Block block, final FunctionNode function, final String ident) {
4255         if (compiler.getScriptEnvironment()._print_symbols || function.getFlag(FunctionNode.IS_PRINT_SYMBOLS)) {
4256             final PrintWriter out = compiler.getScriptEnvironment().getErr();
4257             out.println("[BLOCK in '" + ident + "']");
4258             if (!block.printSymbols(out)) {
4259                 out.println("<no symbols>");
4260             }
4261             out.println();
4262         }
4263     }
4264 
4265 
4266     /**
4267      * The difference between a store and a self modifying store is that
4268      * the latter may load part of the target on the stack, e.g. the base
4269      * of an AccessNode or the base and index of an IndexNode. These are used
4270      * both as target and as an extra source. Previously it was problematic
4271      * for self modifying stores if the target/lhs didn't belong to one
4272      * of three trivial categories: IdentNode, AcessNodes, IndexNodes. In that
4273      * case it was evaluated and tagged as "resolved", which meant at the second
4274      * time the lhs of this store was read (e.g. in a = a (second) + b for a += b,
4275      * it would be evaluated to a nop in the scope and cause stack underflow
4276      *
4277      * see NASHORN-703
4278      *
4279      * @param <T>
4280      */
4281     private abstract class SelfModifyingStore<T extends Expression> extends Store<T> {
4282         protected SelfModifyingStore(final T assignNode, final Expression target) {
4283             super(assignNode, target);
4284         }
4285 
4286         @Override
4287         protected boolean isSelfModifying() {
4288             return true;
4289         }
4290     }
4291 
4292     /**
4293      * Helper class to generate stores
4294      */
4295     private abstract class Store<T extends Expression> {
4296 
4297         /** An assignment node, e.g. x += y */
4298         protected final T assignNode;
4299 
4300         /** The target node to store to, e.g. x */
4301         private final Expression target;
4302 
4303         /** How deep on the stack do the arguments go if this generates an indy call */
4304         private int depth;
4305 
4306         /** If we have too many arguments, we need temporary storage, this is stored in 'quick' */
4307         private IdentNode quick;
4308 
4309         /**
4310          * Constructor
4311          *
4312          * @param assignNode the node representing the whole assignment
4313          * @param target     the target node of the assignment (destination)
4314          */
4315         protected Store(final T assignNode, final Expression target) {
4316             this.assignNode = assignNode;
4317             this.target = target;
4318         }
4319 
4320         /**
4321          * Constructor
4322          *
4323          * @param assignNode the node representing the whole assignment
4324          */
4325         protected Store(final T assignNode) {
4326             this(assignNode, assignNode);
4327         }
4328 
4329         /**
4330          * Is this a self modifying store operation, e.g. *= or ++
4331          * @return true if self modifying store
4332          */
4333         protected boolean isSelfModifying() {
4334             return false;
4335         }
4336 
4337         private void prologue() {
4338             /**
4339              * This loads the parts of the target, e.g base and index. they are kept
4340              * on the stack throughout the store and used at the end to execute it
4341              */
4342 
4343             target.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
4344                 @Override
4345                 public boolean enterIdentNode(final IdentNode node) {
4346                     if (node.getSymbol().isScope()) {
4347                         method.loadCompilerConstant(SCOPE);
4348                         depth += Type.SCOPE.getSlots();
4349                         assert depth == 1;
4350                     }
4351                     return false;
4352                 }
4353 
4354                 private void enterBaseNode() {
4355                     assert target instanceof BaseNode : "error - base node " + target + " must be instanceof BaseNode";
4356                     final BaseNode   baseNode = (BaseNode)target;
4357                     final Expression base     = baseNode.getBase();
4358 
4359                     loadExpressionAsObject(base);
4360                     depth += Type.OBJECT.getSlots();
4361                     assert depth == 1;
4362 
4363                     if (isSelfModifying()) {
4364                         method.dup();
4365                     }
4366                 }
4367 
4368                 @Override
4369                 public boolean enterAccessNode(final AccessNode node) {
4370                     enterBaseNode();
4371                     return false;
4372                 }
4373 
4374                 @Override
4375                 public boolean enterIndexNode(final IndexNode node) {
4376                     enterBaseNode();
4377 
4378                     final Expression index = node.getIndex();
4379                     if (!index.getType().isNumeric()) {
4380                         // could be boolean here as well
4381                         loadExpressionAsObject(index);
4382                     } else {
4383                         loadExpressionUnbounded(index);
4384                     }
4385                     depth += index.getType().getSlots();
4386 
4387                     if (isSelfModifying()) {
4388                         //convert "base base index" to "base index base index"
4389                         method.dup(1);
4390                     }
4391 
4392                     return false;
4393                 }
4394 
4395             });
4396         }
4397 
4398         /**
4399          * Generates an extra local variable, always using the same slot, one that is available after the end of the
4400          * frame.
4401          *
4402          * @param type the type of the variable
4403          *
4404          * @return the quick variable
4405          */
4406         private IdentNode quickLocalVariable(final Type type) {
4407             final String name = lc.getCurrentFunction().uniqueName(QUICK_PREFIX.symbolName());
4408             final Symbol symbol = new Symbol(name, IS_INTERNAL | HAS_SLOT);
4409             symbol.setHasSlotFor(type);
4410             symbol.setFirstSlot(lc.quickSlot(type));
4411 
4412             final IdentNode quickIdent = IdentNode.createInternalIdentifier(symbol).setType(type);
4413 
4414             return quickIdent;
4415         }
4416 
4417         // store the result that "lives on" after the op, e.g. "i" in i++ postfix.
4418         protected void storeNonDiscard() {
4419             if (lc.popDiscardIfCurrent(assignNode)) {
4420                 assert assignNode.isAssignment();
4421                 return;
4422             }
4423 
4424             if (method.dup(depth) == null) {
4425                 method.dup();
4426                 final Type quickType = method.peekType();
4427                 this.quick = quickLocalVariable(quickType);
4428                 final Symbol quickSymbol = quick.getSymbol();
4429                 method.storeTemp(quickType, quickSymbol.getFirstSlot());
4430             }
4431         }
4432 
4433         private void epilogue() {
4434             /**
4435              * Take the original target args from the stack and use them
4436              * together with the value to be stored to emit the store code
4437              *
4438              * The case that targetSymbol is in scope (!hasSlot) and we actually
4439              * need to do a conversion on non-equivalent types exists, but is
4440              * very rare. See for example test/script/basic/access-specializer.js
4441              */
4442             target.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
4443                 @Override
4444                 protected boolean enterDefault(final Node node) {
4445                     throw new AssertionError("Unexpected node " + node + " in store epilogue");
4446                 }
4447 
4448                 @Override
4449                 public boolean enterIdentNode(final IdentNode node) {
4450                     final Symbol symbol = node.getSymbol();
4451                     assert symbol != null;
4452                     if (symbol.isScope()) {
4453                         final int flags = getScopeCallSiteFlags(symbol);
4454                         if (isFastScope(symbol)) {
4455                             storeFastScopeVar(symbol, flags);
4456                         } else {
4457                             method.dynamicSet(node.getName(), flags, false);
4458                         }
4459                     } else {
4460                         final Type storeType = assignNode.getType();
4461                         if (symbol.hasSlotFor(storeType)) {
4462                             // Only emit a convert for a store known to be live; converts for dead stores can
4463                             // give us an unnecessary ClassCastException.
4464                             method.convert(storeType);
4465                         }
4466                         storeIdentWithCatchConversion(node, storeType);
4467                     }
4468                     return false;
4469 
4470                 }
4471 
4472                 @Override
4473                 public boolean enterAccessNode(final AccessNode node) {
4474                     method.dynamicSet(node.getProperty(), getCallSiteFlags(), node.isIndex());
4475                     return false;
4476                 }
4477 
4478                 @Override
4479                 public boolean enterIndexNode(final IndexNode node) {
4480                     method.dynamicSetIndex(getCallSiteFlags());
4481                     return false;
4482                 }
4483             });
4484 
4485 
4486             // whatever is on the stack now is the final answer
4487         }
4488 
4489         protected abstract void evaluate();
4490 
4491         void store() {
4492             if (target instanceof IdentNode) {
4493                 checkTemporalDeadZone((IdentNode)target);
4494             }
4495             prologue();
4496             evaluate(); // leaves an operation of whatever the operationType was on the stack
4497             storeNonDiscard();
4498             epilogue();
4499             if (quick != null) {
4500                 method.load(quick);
4501             }
4502         }
4503     }
4504 
4505     private void newFunctionObject(final FunctionNode functionNode, final boolean addInitializer) {
4506         assert lc.peek() == functionNode;
4507 
4508         final RecompilableScriptFunctionData data = compiler.getScriptFunctionData(functionNode.getId());
4509 
4510         if (functionNode.isProgram() && !compiler.isOnDemandCompilation()) {
4511             final MethodEmitter createFunction = functionNode.getCompileUnit().getClassEmitter().method(
4512                     EnumSet.of(Flag.PUBLIC, Flag.STATIC), CREATE_PROGRAM_FUNCTION.symbolName(),
4513                     ScriptFunction.class, ScriptObject.class);
4514             createFunction.begin();
4515             loadConstantsAndIndex(data, createFunction);
4516             createFunction.load(SCOPE_TYPE, 0);
4517             createFunction.invoke(CREATE_FUNCTION_OBJECT);
4518             createFunction._return();
4519             createFunction.end();
4520         }
4521 
4522         if (addInitializer && !compiler.isOnDemandCompilation()) {
4523             functionNode.getCompileUnit().addFunctionInitializer(data, functionNode);
4524         }
4525 
4526         // We don't emit a ScriptFunction on stack for the outermost compiled function (as there's no code being
4527         // generated in its outer context that'd need it as a callee).
4528         if (lc.getOutermostFunction() == functionNode) {
4529             return;
4530         }
4531 
4532         loadConstantsAndIndex(data, method);
4533 
4534         if (functionNode.needsParentScope()) {
4535             method.loadCompilerConstant(SCOPE);
4536             method.invoke(CREATE_FUNCTION_OBJECT);
4537         } else {
4538             method.invoke(CREATE_FUNCTION_OBJECT_NO_SCOPE);
4539         }
4540     }
4541 
4542     // calls on Global class.
4543     private MethodEmitter globalInstance() {
4544         return method.invokestatic(GLOBAL_OBJECT, "instance", "()L" + GLOBAL_OBJECT + ';');
4545     }
4546 
4547     private MethodEmitter globalAllocateArguments() {
4548         return method.invokestatic(GLOBAL_OBJECT, "allocateArguments", methodDescriptor(ScriptObject.class, Object[].class, Object.class, int.class));
4549     }
4550 
4551     private MethodEmitter globalNewRegExp() {
4552         return method.invokestatic(GLOBAL_OBJECT, "newRegExp", methodDescriptor(Object.class, String.class, String.class));
4553     }
4554 
4555     private MethodEmitter globalRegExpCopy() {
4556         return method.invokestatic(GLOBAL_OBJECT, "regExpCopy", methodDescriptor(Object.class, Object.class));
4557     }
4558 
4559     private MethodEmitter globalAllocateArray(final ArrayType type) {
4560         //make sure the native array is treated as an array type
4561         return method.invokestatic(GLOBAL_OBJECT, "allocate", "(" + type.getDescriptor() + ")Ljdk/nashorn/internal/objects/NativeArray;");
4562     }
4563 
4564     private MethodEmitter globalIsEval() {
4565         return method.invokestatic(GLOBAL_OBJECT, "isEval", methodDescriptor(boolean.class, Object.class));
4566     }
4567 
4568     private MethodEmitter globalReplaceLocationPropertyPlaceholder() {
4569         return method.invokestatic(GLOBAL_OBJECT, "replaceLocationPropertyPlaceholder", methodDescriptor(Object.class, Object.class, Object.class));
4570     }
4571 
4572     private MethodEmitter globalCheckObjectCoercible() {
4573         return method.invokestatic(GLOBAL_OBJECT, "checkObjectCoercible", methodDescriptor(void.class, Object.class));
4574     }
4575 
4576     private MethodEmitter globalDirectEval() {
4577         return method.invokestatic(GLOBAL_OBJECT, "directEval",
4578                 methodDescriptor(Object.class, Object.class, Object.class, Object.class, Object.class, boolean.class));
4579     }
4580 
4581     private abstract class OptimisticOperation {
4582         private final boolean isOptimistic;
4583         // expression and optimistic are the same reference
4584         private final Expression expression;
4585         private final Optimistic optimistic;
4586         private final TypeBounds resultBounds;
4587 
4588         OptimisticOperation(final Optimistic optimistic, final TypeBounds resultBounds) {
4589             this.optimistic = optimistic;
4590             this.expression = (Expression)optimistic;
4591             this.resultBounds = resultBounds;
4592             this.isOptimistic = isOptimistic(optimistic) && useOptimisticTypes() &&
4593                     // Operation is only effectively optimistic if its type, after being coerced into the result bounds
4594                     // is narrower than the upper bound.
4595                     resultBounds.within(Type.generic(((Expression)optimistic).getType())).narrowerThan(resultBounds.widest);
4596         }
4597 
4598         MethodEmitter emit() {
4599             return emit(0);
4600         }
4601 
4602         MethodEmitter emit(final int ignoredArgCount) {
4603             final int     programPoint                  = optimistic.getProgramPoint();
4604             final boolean optimisticOrContinuation      = isOptimistic || isContinuationEntryPoint(programPoint);
4605             final boolean currentContinuationEntryPoint = isCurrentContinuationEntryPoint(programPoint);
4606             final int     stackSizeOnEntry              = method.getStackSize() - ignoredArgCount;
4607 
4608             // First store the values on the stack opportunistically into local variables. Doing it before loadStack()
4609             // allows us to not have to pop/load any arguments that are pushed onto it by loadStack() in the second
4610             // storeStack().
4611             storeStack(ignoredArgCount, optimisticOrContinuation);
4612 
4613             // Now, load the stack
4614             loadStack();
4615 
4616             // Now store the values on the stack ultimately into local variables. In vast majority of cases, this is
4617             // (aside from creating the local types map) a no-op, as the first opportunistic stack store will already
4618             // store all variables. However, there can be operations in the loadStack() that invalidate some of the
4619             // stack stores, e.g. in "x[i] = x[++i]", "++i" will invalidate the already stored value for "i". In such
4620             // unfortunate cases this second storeStack() will restore the invariant that everything on the stack is
4621             // stored into a local variable, although at the cost of doing a store/load on the loaded arguments as well.
4622             final int liveLocalsCount = storeStack(method.getStackSize() - stackSizeOnEntry, optimisticOrContinuation);
4623             assert optimisticOrContinuation == (liveLocalsCount != -1);
4624 
4625             final Label beginTry;
4626             final Label catchLabel;
4627             final Label afterConsumeStack = isOptimistic || currentContinuationEntryPoint ? new Label("after_consume_stack") : null;
4628             if(isOptimistic) {
4629                 beginTry = new Label("try_optimistic");
4630                 final String catchLabelName = (afterConsumeStack == null ? "" : afterConsumeStack.toString()) + "_handler";
4631                 catchLabel = new Label(catchLabelName);
4632                 method.label(beginTry);
4633             } else {
4634                 beginTry = catchLabel = null;
4635             }
4636 
4637             consumeStack();
4638 
4639             if(isOptimistic) {
4640                 method._try(beginTry, afterConsumeStack, catchLabel, UnwarrantedOptimismException.class);
4641             }
4642 
4643             if(isOptimistic || currentContinuationEntryPoint) {
4644                 method.label(afterConsumeStack);
4645 
4646                 final int[] localLoads = method.getLocalLoadsOnStack(0, stackSizeOnEntry);
4647                 assert everyStackValueIsLocalLoad(localLoads) : Arrays.toString(localLoads) + ", " + stackSizeOnEntry + ", " + ignoredArgCount;
4648                 final List<Type> localTypesList = method.getLocalVariableTypes();
4649                 final int usedLocals = method.getUsedSlotsWithLiveTemporaries();
4650                 final List<Type> localTypes = method.getWidestLiveLocals(localTypesList.subList(0, usedLocals));
4651                 assert everyLocalLoadIsValid(localLoads, usedLocals) : Arrays.toString(localLoads) + " ~ " + localTypes;
4652 
4653                 if(isOptimistic) {
4654                     addUnwarrantedOptimismHandlerLabel(localTypes, catchLabel);
4655                 }
4656                 if(currentContinuationEntryPoint) {
4657                     final ContinuationInfo ci = getContinuationInfo();
4658                     assert ci != null : "no continuation info found for " + lc.getCurrentFunction();
4659                     assert !ci.hasTargetLabel(); // No duplicate program points
4660                     ci.setTargetLabel(afterConsumeStack);
4661                     ci.getHandlerLabel().markAsOptimisticContinuationHandlerFor(afterConsumeStack);
4662                     // Can't rely on targetLabel.stack.localVariableTypes.length, as it can be higher due to effectively
4663                     // dead local variables.
4664                     ci.lvarCount = localTypes.size();
4665                     ci.setStackStoreSpec(localLoads);
4666                     ci.setStackTypes(Arrays.copyOf(method.getTypesFromStack(method.getStackSize()), stackSizeOnEntry));
4667                     assert ci.getStackStoreSpec().length == ci.getStackTypes().length;
4668                     ci.setReturnValueType(method.peekType());
4669                     ci.lineNumber = getLastLineNumber();
4670                     ci.catchLabel = catchLabels.peek();
4671                 }
4672             }
4673             return method;
4674         }
4675 
4676         /**
4677          * Stores the current contents of the stack into local variables so they are not lost before invoking something that
4678          * can result in an {@code UnwarantedOptimizationException}.
4679          * @param ignoreArgCount the number of topmost arguments on stack to ignore when deciding on the shape of the catch
4680          * block. Those are used in the situations when we could not place the call to {@code storeStack} early enough
4681          * (before emitting code for pushing the arguments that the optimistic call will pop). This is admittedly a
4682          * deficiency in the design of the code generator when it deals with self-assignments and we should probably look
4683          * into fixing it.
4684          * @return types of the significant local variables after the stack was stored (types for local variables used
4685          * for temporary storage of ignored arguments are not returned).
4686          * @param optimisticOrContinuation if false, this method should not execute
4687          * a label for a catch block for the {@code UnwarantedOptimizationException}, suitable for capturing the
4688          * currently live local variables, tailored to their types.
4689          */
4690         private int storeStack(final int ignoreArgCount, final boolean optimisticOrContinuation) {
4691             if(!optimisticOrContinuation) {
4692                 return -1; // NOTE: correct value to return is lc.getUsedSlotCount(), but it wouldn't be used anyway
4693             }
4694 
4695             final int stackSize = method.getStackSize();
4696             final Type[] stackTypes = method.getTypesFromStack(stackSize);
4697             final int[] localLoadsOnStack = method.getLocalLoadsOnStack(0, stackSize);
4698             final int usedSlots = method.getUsedSlotsWithLiveTemporaries();
4699 
4700             final int firstIgnored = stackSize - ignoreArgCount;
4701             // Find the first value on the stack (from the bottom) that is not a load from a local variable.
4702             int firstNonLoad = 0;
4703             while(firstNonLoad < firstIgnored && localLoadsOnStack[firstNonLoad] != Label.Stack.NON_LOAD) {
4704                 firstNonLoad++;
4705             }
4706 
4707             // Only do the store/load if first non-load is not an ignored argument. Otherwise, do nothing and return
4708             // the number of used slots as the number of live local variables.
4709             if(firstNonLoad >= firstIgnored) {
4710                 return usedSlots;
4711             }
4712 
4713             // Find the number of new temporary local variables that we need; it's the number of values on the stack that
4714             // are not direct loads of existing local variables.
4715             int tempSlotsNeeded = 0;
4716             for(int i = firstNonLoad; i < stackSize; ++i) {
4717                 if(localLoadsOnStack[i] == Label.Stack.NON_LOAD) {
4718                     tempSlotsNeeded += stackTypes[i].getSlots();
4719                 }
4720             }
4721 
4722             // Ensure all values on the stack that weren't directly loaded from a local variable are stored in a local
4723             // variable. We're starting from highest local variable index, so that in case ignoreArgCount > 0 the ignored
4724             // ones end up at the end of the local variable table.
4725             int lastTempSlot = usedSlots + tempSlotsNeeded;
4726             int ignoreSlotCount = 0;
4727             for(int i = stackSize; i -- > firstNonLoad;) {
4728                 final int loadSlot = localLoadsOnStack[i];
4729                 if(loadSlot == Label.Stack.NON_LOAD) {
4730                     final Type type = stackTypes[i];
4731                     final int slots = type.getSlots();
4732                     lastTempSlot -= slots;
4733                     if(i >= firstIgnored) {
4734                         ignoreSlotCount += slots;
4735                     }
4736                     method.storeTemp(type, lastTempSlot);
4737                 } else {
4738                     method.pop();
4739                 }
4740             }
4741             assert lastTempSlot == usedSlots; // used all temporary locals
4742 
4743             final List<Type> localTypesList = method.getLocalVariableTypes();
4744 
4745             // Load values back on stack.
4746             for(int i = firstNonLoad; i < stackSize; ++i) {
4747                 final int loadSlot = localLoadsOnStack[i];
4748                 final Type stackType = stackTypes[i];
4749                 final boolean isLoad = loadSlot != Label.Stack.NON_LOAD;
4750                 final int lvarSlot = isLoad ? loadSlot : lastTempSlot;
4751                 final Type lvarType = localTypesList.get(lvarSlot);
4752                 method.load(lvarType, lvarSlot);
4753                 if(isLoad) {
4754                     // Conversion operators (I2L etc.) preserve "load"-ness of the value despite the fact that, in the
4755                     // strict sense they are creating a derived value from the loaded value. This special behavior of
4756                     // on-stack conversion operators is necessary to accommodate for differences in local variable types
4757                     // after deoptimization; having a conversion operator throw away "load"-ness would create different
4758                     // local variable table shapes between optimism-failed code and its deoptimized rest-of method).
4759                     // After we load the value back, we need to redo the conversion to the stack type if stack type is
4760                     // different.
4761                     // NOTE: this would only strictly be necessary for widening conversions (I2L, L2D, I2D), and not for
4762                     // narrowing ones (L2I, D2L, D2I) as only widening conversions are the ones that can get eliminated
4763                     // in a deoptimized method, as their original input argument got widened. Maybe experiment with
4764                     // throwing away "load"-ness for narrowing conversions in MethodEmitter.convert()?
4765                     method.convert(stackType);
4766                 } else {
4767                     // temporary stores never needs a convert, as their type is always the same as the stack type.
4768                     assert lvarType == stackType;
4769                     lastTempSlot += lvarType.getSlots();
4770                 }
4771             }
4772             // used all temporaries
4773             assert lastTempSlot == usedSlots + tempSlotsNeeded;
4774 
4775             return lastTempSlot - ignoreSlotCount;
4776         }
4777 
4778         private void addUnwarrantedOptimismHandlerLabel(final List<Type> localTypes, final Label label) {
4779             final String lvarTypesDescriptor = getLvarTypesDescriptor(localTypes);
4780             final Map<String, Collection<Label>> unwarrantedOptimismHandlers = lc.getUnwarrantedOptimismHandlers();
4781             Collection<Label> labels = unwarrantedOptimismHandlers.get(lvarTypesDescriptor);
4782             if(labels == null) {
4783                 labels = new LinkedList<>();
4784                 unwarrantedOptimismHandlers.put(lvarTypesDescriptor, labels);
4785             }
4786             method.markLabelAsOptimisticCatchHandler(label, localTypes.size());
4787             labels.add(label);
4788         }
4789 
4790         abstract void loadStack();
4791 
4792         // Make sure that whatever indy call site you emit from this method uses {@code getCallSiteFlagsOptimistic(node)}
4793         // or otherwise ensure optimistic flag is correctly set in the call site, otherwise it doesn't make much sense
4794         // to use OptimisticExpression for emitting it.
4795         abstract void consumeStack();
4796 
4797         /**
4798          * Emits the correct dynamic getter code. Normally just delegates to method emitter, except when the target
4799          * expression is optimistic, and the desired type is narrower than the optimistic type. In that case, it'll emit a
4800          * dynamic getter with its original optimistic type, and explicitly insert a narrowing conversion. This way we can
4801          * preserve the optimism of the values even if they're subsequently immediately coerced into a narrower type. This
4802          * is beneficial because in this case we can still presume that since the original getter was optimistic, the
4803          * conversion has no side effects.
4804          * @param name the name of the property being get
4805          * @param flags call site flags
4806          * @param isMethod whether we're preferrably retrieving a function
4807          * @return the current method emitter
4808          */
4809         MethodEmitter dynamicGet(final String name, final int flags, final boolean isMethod, final boolean isIndex) {
4810             if(isOptimistic) {
4811                 return method.dynamicGet(getOptimisticCoercedType(), name, getOptimisticFlags(flags), isMethod, isIndex);
4812             }
4813             return method.dynamicGet(resultBounds.within(expression.getType()), name, nonOptimisticFlags(flags), isMethod, isIndex);
4814         }
4815 
4816         MethodEmitter dynamicGetIndex(final int flags, final boolean isMethod) {
4817             if(isOptimistic) {
4818                 return method.dynamicGetIndex(getOptimisticCoercedType(), getOptimisticFlags(flags), isMethod);
4819             }
4820             return method.dynamicGetIndex(resultBounds.within(expression.getType()), nonOptimisticFlags(flags), isMethod);
4821         }
4822 
4823         MethodEmitter dynamicCall(final int argCount, final int flags, final String msg) {
4824             if (isOptimistic) {
4825                 return method.dynamicCall(getOptimisticCoercedType(), argCount, getOptimisticFlags(flags), msg);
4826             }
4827             return method.dynamicCall(resultBounds.within(expression.getType()), argCount, nonOptimisticFlags(flags), msg);
4828         }
4829 
4830         int getOptimisticFlags(final int flags) {
4831             return flags | CALLSITE_OPTIMISTIC | (optimistic.getProgramPoint() << CALLSITE_PROGRAM_POINT_SHIFT); //encode program point in high bits
4832         }
4833 
4834         int getProgramPoint() {
4835             return isOptimistic ? optimistic.getProgramPoint() : INVALID_PROGRAM_POINT;
4836         }
4837 
4838         void convertOptimisticReturnValue() {
4839             if (isOptimistic) {
4840                 final Type optimisticType = getOptimisticCoercedType();
4841                 if(!optimisticType.isObject()) {
4842                     method.load(optimistic.getProgramPoint());
4843                     if(optimisticType.isInteger()) {
4844                         method.invoke(ENSURE_INT);
4845                     } else if(optimisticType.isLong()) {
4846                         method.invoke(ENSURE_LONG);
4847                     } else if(optimisticType.isNumber()) {
4848                         method.invoke(ENSURE_NUMBER);
4849                     } else {
4850                         throw new AssertionError(optimisticType);
4851                     }
4852                 }
4853             }
4854         }
4855 
4856         void replaceCompileTimeProperty() {
4857             final IdentNode identNode = (IdentNode)expression;
4858             final String name = identNode.getSymbol().getName();
4859             if (CompilerConstants.__FILE__.name().equals(name)) {
4860                 replaceCompileTimeProperty(getCurrentSource().getName());
4861             } else if (CompilerConstants.__DIR__.name().equals(name)) {
4862                 replaceCompileTimeProperty(getCurrentSource().getBase());
4863             } else if (CompilerConstants.__LINE__.name().equals(name)) {
4864                 replaceCompileTimeProperty(getCurrentSource().getLine(identNode.position()));
4865             }
4866         }
4867 
4868         /**
4869          * When an ident with name __FILE__, __DIR__, or __LINE__ is loaded, we'll try to look it up as any other
4870          * identifier. However, if it gets all the way up to the Global object, it will send back a special value that
4871          * represents a placeholder for these compile-time location properties. This method will generate code that loads
4872          * the value of the compile-time location property and then invokes a method in Global that will replace the
4873          * placeholder with the value. Effectively, if the symbol for these properties is defined anywhere in the lexical
4874          * scope, they take precedence, but if they aren't, then they resolve to the compile-time location property.
4875          * @param propertyValue the actual value of the property
4876          */
4877         private void replaceCompileTimeProperty(final Object propertyValue) {
4878             assert method.peekType().isObject();
4879             if(propertyValue instanceof String || propertyValue == null) {
4880                 method.load((String)propertyValue);
4881             } else if(propertyValue instanceof Integer) {
4882                 method.load(((Integer)propertyValue));
4883                 method.convert(Type.OBJECT);
4884             } else {
4885                 throw new AssertionError();
4886             }
4887             globalReplaceLocationPropertyPlaceholder();
4888             convertOptimisticReturnValue();
4889         }
4890 
4891         /**
4892          * Returns the type that should be used as the return type of the dynamic invocation that is emitted as the code
4893          * for the current optimistic operation. If the type bounds is exact boolean or narrower than the expression's
4894          * optimistic type, then the optimistic type is returned, otherwise the coercing type. Effectively, this method
4895          * allows for moving the coercion into the optimistic type when it won't adversely affect the optimistic
4896          * evaluation semantics, and for preserving the optimistic type and doing a separate coercion when it would
4897          * affect it.
4898          * @return
4899          */
4900         private Type getOptimisticCoercedType() {
4901             final Type optimisticType = expression.getType();
4902             assert resultBounds.widest.widerThan(optimisticType);
4903             final Type narrowest = resultBounds.narrowest;
4904 
4905             if(narrowest.isBoolean() || narrowest.narrowerThan(optimisticType)) {
4906                 assert !optimisticType.isObject();
4907                 return optimisticType;
4908             }
4909             assert !narrowest.isObject();
4910             return narrowest;
4911         }
4912     }
4913 
4914     private static boolean isOptimistic(final Optimistic optimistic) {
4915         if(!optimistic.canBeOptimistic()) {
4916             return false;
4917         }
4918         final Expression expr = (Expression)optimistic;
4919         return expr.getType().narrowerThan(expr.getWidestOperationType());
4920     }
4921 
4922     private static boolean everyLocalLoadIsValid(final int[] loads, final int localCount) {
4923         for (final int load : loads) {
4924             if(load < 0 || load >= localCount) {
4925                 return false;
4926             }
4927         }
4928         return true;
4929     }
4930 
4931     private static boolean everyStackValueIsLocalLoad(final int[] loads) {
4932         for (final int load : loads) {
4933             if(load == Label.Stack.NON_LOAD) {
4934                 return false;
4935             }
4936         }
4937         return true;
4938     }
4939 
4940     private String getLvarTypesDescriptor(final List<Type> localVarTypes) {
4941         final int count = localVarTypes.size();
4942         final StringBuilder desc = new StringBuilder(count);
4943         for(int i = 0; i < count;) {
4944             i += appendType(desc, localVarTypes.get(i));
4945         }
4946         return method.markSymbolBoundariesInLvarTypesDescriptor(desc.toString());
4947     }
4948 
4949     private static int appendType(final StringBuilder b, final Type t) {
4950         b.append(t.getBytecodeStackType());
4951         return t.getSlots();
4952     }
4953 
4954     private static int countSymbolsInLvarTypeDescriptor(final String lvarTypeDescriptor) {
4955         int count = 0;
4956         for(int i = 0; i < lvarTypeDescriptor.length(); ++i) {
4957             if(Character.isUpperCase(lvarTypeDescriptor.charAt(i))) {
4958                 ++count;
4959             }
4960         }
4961         return count;
4962 
4963     }
4964     /**
4965      * Generates all the required {@code UnwarrantedOptimismException} handlers for the current function. The employed
4966      * strategy strives to maximize code reuse. Every handler constructs an array to hold the local variables, then
4967      * fills in some trailing part of the local variables (those for which it has a unique suffix in the descriptor),
4968      * then jumps to a handler for a prefix that's shared with other handlers. A handler that fills up locals up to
4969      * position 0 will not jump to a prefix handler (as it has no prefix), but instead end with constructing and
4970      * throwing a {@code RewriteException}. Since we lexicographically sort the entries, we only need to check every
4971      * entry to its immediately preceding one for longest matching prefix.
4972      * @return true if there is at least one exception handler
4973      */
4974     private boolean generateUnwarrantedOptimismExceptionHandlers(final FunctionNode fn) {
4975         if(!useOptimisticTypes()) {
4976             return false;
4977         }
4978 
4979         // Take the mapping of lvarSpecs -> labels, and turn them into a descending lexicographically sorted list of
4980         // handler specifications.
4981         final Map<String, Collection<Label>> unwarrantedOptimismHandlers = lc.popUnwarrantedOptimismHandlers();
4982         if(unwarrantedOptimismHandlers.isEmpty()) {
4983             return false;
4984         }
4985 
4986         method.lineNumber(0);
4987 
4988         final List<OptimismExceptionHandlerSpec> handlerSpecs = new ArrayList<>(unwarrantedOptimismHandlers.size() * 4/3);
4989         for(final String spec: unwarrantedOptimismHandlers.keySet()) {
4990             handlerSpecs.add(new OptimismExceptionHandlerSpec(spec, true));
4991         }
4992         Collections.sort(handlerSpecs, Collections.reverseOrder());
4993 
4994         // Map of local variable specifications to labels for populating the array for that local variable spec.
4995         final Map<String, Label> delegationLabels = new HashMap<>();
4996 
4997         // Do everything in a single pass over the handlerSpecs list. Note that the list can actually grow as we're
4998         // passing through it as we might add new prefix handlers into it, so can't hoist size() outside of the loop.
4999         for(int handlerIndex = 0; handlerIndex < handlerSpecs.size(); ++handlerIndex) {
5000             final OptimismExceptionHandlerSpec spec = handlerSpecs.get(handlerIndex);
5001             final String lvarSpec = spec.lvarSpec;
5002             if(spec.catchTarget) {
5003                 assert !method.isReachable();
5004                 // Start a catch block and assign the labels for this lvarSpec with it.
5005                 method._catch(unwarrantedOptimismHandlers.get(lvarSpec));
5006                 // This spec is a catch target, so emit array creation code. The length of the array is the number of
5007                 // symbols - the number of uppercase characters.
5008                 method.load(countSymbolsInLvarTypeDescriptor(lvarSpec));
5009                 method.newarray(Type.OBJECT_ARRAY);
5010             }
5011             if(spec.delegationTarget) {
5012                 // If another handler can delegate to this handler as its prefix, then put a jump target here for the
5013                 // shared code (after the array creation code, which is never shared).
5014                 method.label(delegationLabels.get(lvarSpec)); // label must exist
5015             }
5016 
5017             final boolean lastHandler = handlerIndex == handlerSpecs.size() - 1;
5018 
5019             int lvarIndex;
5020             final int firstArrayIndex;
5021             final int firstLvarIndex;
5022             Label delegationLabel;
5023             final String commonLvarSpec;
5024             if(lastHandler) {
5025                 // Last handler block, doesn't delegate to anything.
5026                 lvarIndex = 0;
5027                 firstLvarIndex = 0;
5028                 firstArrayIndex = 0;
5029                 delegationLabel = null;
5030                 commonLvarSpec = null;
5031             } else {
5032                 // Not yet the last handler block, will definitely delegate to another handler; let's figure out which
5033                 // one. It can be an already declared handler further down the list, or it might need to declare a new
5034                 // prefix handler.
5035 
5036                 // Since we're lexicographically ordered, the common prefix handler is defined by the common prefix of
5037                 // this handler and the next handler on the list.
5038                 final int nextHandlerIndex = handlerIndex + 1;
5039                 final String nextLvarSpec = handlerSpecs.get(nextHandlerIndex).lvarSpec;
5040                 commonLvarSpec = commonPrefix(lvarSpec, nextLvarSpec);
5041                 // We don't chop symbols in half
5042                 assert Character.isUpperCase(commonLvarSpec.charAt(commonLvarSpec.length() - 1));
5043 
5044                 // Let's find if we already have a declaration for such handler, or we need to insert it.
5045                 {
5046                     boolean addNewHandler = true;
5047                     int commonHandlerIndex = nextHandlerIndex;
5048                     for(; commonHandlerIndex < handlerSpecs.size(); ++commonHandlerIndex) {
5049                         final OptimismExceptionHandlerSpec forwardHandlerSpec = handlerSpecs.get(commonHandlerIndex);
5050                         final String forwardLvarSpec = forwardHandlerSpec.lvarSpec;
5051                         if(forwardLvarSpec.equals(commonLvarSpec)) {
5052                             // We already have a handler for the common prefix.
5053                             addNewHandler = false;
5054                             // Make sure we mark it as a delegation target.
5055                             forwardHandlerSpec.delegationTarget = true;
5056                             break;
5057                         } else if(!forwardLvarSpec.startsWith(commonLvarSpec)) {
5058                             break;
5059                         }
5060                     }
5061                     if(addNewHandler) {
5062                         // We need to insert a common prefix handler. Note handlers created with catchTarget == false
5063                         // will automatically have delegationTarget == true (because that's the only reason for their
5064                         // existence).
5065                         handlerSpecs.add(commonHandlerIndex, new OptimismExceptionHandlerSpec(commonLvarSpec, false));
5066                     }
5067                 }
5068 
5069                 firstArrayIndex = countSymbolsInLvarTypeDescriptor(commonLvarSpec);
5070                 lvarIndex = 0;
5071                 for(int j = 0; j < commonLvarSpec.length(); ++j) {
5072                     lvarIndex += CodeGeneratorLexicalContext.getTypeForSlotDescriptor(commonLvarSpec.charAt(j)).getSlots();
5073                 }
5074                 firstLvarIndex = lvarIndex;
5075 
5076                 // Create a delegation label if not already present
5077                 delegationLabel = delegationLabels.get(commonLvarSpec);
5078                 if(delegationLabel == null) {
5079                     // uo_pa == "unwarranted optimism, populate array"
5080                     delegationLabel = new Label("uo_pa_" + commonLvarSpec);
5081                     delegationLabels.put(commonLvarSpec, delegationLabel);
5082                 }
5083             }
5084 
5085             // Load local variables handled by this handler on stack
5086             int args = 0;
5087             boolean symbolHadValue = false;
5088             for(int typeIndex = commonLvarSpec == null ? 0 : commonLvarSpec.length(); typeIndex < lvarSpec.length(); ++typeIndex) {
5089                 final char typeDesc = lvarSpec.charAt(typeIndex);
5090                 final Type lvarType = CodeGeneratorLexicalContext.getTypeForSlotDescriptor(typeDesc);
5091                 if (!lvarType.isUnknown()) {
5092                     method.load(lvarType, lvarIndex);
5093                     symbolHadValue = true;
5094                     args++;
5095                 } else if(typeDesc == 'U' && !symbolHadValue) {
5096                     // Symbol boundary with undefined last value. Check if all previous values for this symbol were also
5097                     // undefined; if so, emit one explicit Undefined. This serves to ensure that we're emiting exactly
5098                     // one value for every symbol that uses local slots. While we could in theory ignore symbols that
5099                     // are undefined (in other words, dead) at the point where this exception was thrown, unfortunately
5100                     // we can't do it in practice. The reason for this is that currently our liveness analysis is
5101                     // coarse (it can determine whether a symbol has not been read with a particular type anywhere in
5102                     // the function being compiled, but that's it), and a symbol being promoted to Object due to a
5103                     // deoptimization will suddenly show up as "live for Object type", and previously dead U->O
5104                     // conversions on loop entries will suddenly become alive in the deoptimized method which will then
5105                     // expect a value for that slot in its continuation handler. If we had precise liveness analysis, we
5106                     // could go back to excluding known dead symbols from the payload of the RewriteException.
5107                     if(method.peekType() == Type.UNDEFINED) {
5108                         method.dup();
5109                     } else {
5110                         method.loadUndefined(Type.OBJECT);
5111                     }
5112                     args++;
5113                 }
5114                 if(Character.isUpperCase(typeDesc)) {
5115                     // Reached symbol boundary; reset flag for the next symbol.
5116                     symbolHadValue = false;
5117                 }
5118                 lvarIndex += lvarType.getSlots();
5119             }
5120             assert args > 0;
5121             // Delegate actual storing into array to an array populator utility method.
5122             //on the stack:
5123             // object array to be populated
5124             // start index
5125             // a lot of types
5126             method.dynamicArrayPopulatorCall(args + 1, firstArrayIndex);
5127             if(delegationLabel != null) {
5128                 // We cascade to a prefix handler to fill out the rest of the local variables and throw the
5129                 // RewriteException.
5130                 assert !lastHandler;
5131                 assert commonLvarSpec != null;
5132                 // Must undefine the local variables that we have already processed for the sake of correct join on the
5133                 // delegate label
5134                 method.undefineLocalVariables(firstLvarIndex, true);
5135                 final OptimismExceptionHandlerSpec nextSpec = handlerSpecs.get(handlerIndex + 1);
5136                 // If the delegate immediately follows, and it's not a catch target (so it doesn't have array setup
5137                 // code) don't bother emitting a jump, as we'd just jump to the next instruction.
5138                 if(!nextSpec.lvarSpec.equals(commonLvarSpec) || nextSpec.catchTarget) {
5139                     method._goto(delegationLabel);
5140                 }
5141             } else {
5142                 assert lastHandler;
5143                 // Nothing to delegate to, so this handler must create and throw the RewriteException.
5144                 // At this point we have the UnwarrantedOptimismException and the Object[] with local variables on
5145                 // stack. We need to create a RewriteException, push two references to it below the constructor
5146                 // arguments, invoke the constructor, and throw the exception.
5147                 loadConstant(getByteCodeSymbolNames(fn));
5148                 if (isRestOf()) {
5149                     loadConstant(getContinuationEntryPoints());
5150                     method.invoke(CREATE_REWRITE_EXCEPTION_REST_OF);
5151                 } else {
5152                     method.invoke(CREATE_REWRITE_EXCEPTION);
5153                 }
5154                 method.athrow();
5155             }
5156         }
5157         return true;
5158     }
5159 
5160     private static String[] getByteCodeSymbolNames(final FunctionNode fn) {
5161         // Only names of local variables on the function level are captured. This information is used to reduce
5162         // deoptimizations, so as much as we can capture will help. We rely on the fact that function wide variables are
5163         // all live all the time, so the array passed to rewrite exception contains one element for every slotted symbol
5164         // here.
5165         final List<String> names = new ArrayList<>();
5166         for (final Symbol symbol: fn.getBody().getSymbols()) {
5167             if (symbol.hasSlot()) {
5168                 if (symbol.isScope()) {
5169                     // slot + scope can only be true for parameters
5170                     assert symbol.isParam();
5171                     names.add(null);
5172                 } else {
5173                     names.add(symbol.getName());
5174                 }
5175             }
5176         }
5177         return names.toArray(new String[names.size()]);
5178     }
5179 
5180     private static String commonPrefix(final String s1, final String s2) {
5181         final int l1 = s1.length();
5182         final int l = Math.min(l1, s2.length());
5183         int lms = -1; // last matching symbol
5184         for(int i = 0; i < l; ++i) {
5185             final char c1 = s1.charAt(i);
5186             if(c1 != s2.charAt(i)) {
5187                 return s1.substring(0, lms + 1);
5188             } else if(Character.isUpperCase(c1)) {
5189                 lms = i;
5190             }
5191         }
5192         return l == l1 ? s1 : s2;
5193     }
5194 
5195     private static class OptimismExceptionHandlerSpec implements Comparable<OptimismExceptionHandlerSpec> {
5196         private final String lvarSpec;
5197         private final boolean catchTarget;
5198         private boolean delegationTarget;
5199 
5200         OptimismExceptionHandlerSpec(final String lvarSpec, final boolean catchTarget) {
5201             this.lvarSpec = lvarSpec;
5202             this.catchTarget = catchTarget;
5203             if(!catchTarget) {
5204                 delegationTarget = true;
5205             }
5206         }
5207 
5208         @Override
5209         public int compareTo(final OptimismExceptionHandlerSpec o) {
5210             return lvarSpec.compareTo(o.lvarSpec);
5211         }
5212 
5213         @Override
5214         public String toString() {
5215             final StringBuilder b = new StringBuilder(64).append("[HandlerSpec ").append(lvarSpec);
5216             if(catchTarget) {
5217                 b.append(", catchTarget");
5218             }
5219             if(delegationTarget) {
5220                 b.append(", delegationTarget");
5221             }
5222             return b.append("]").toString();
5223         }
5224     }
5225 
5226     private static class ContinuationInfo {
5227         private final Label handlerLabel;
5228         private Label targetLabel; // Label for the target instruction.
5229         int lvarCount;
5230         // Indices of local variables that need to be loaded on the stack when this node completes
5231         private int[] stackStoreSpec;
5232         // Types of values loaded on the stack
5233         private Type[] stackTypes;
5234         // If non-null, this node should perform the requisite type conversion
5235         private Type returnValueType;
5236         // If we are in the middle of an object literal initialization, we need to update the map
5237         private PropertyMap objectLiteralMap;
5238         // Object literal stack depth for object literal - not necessarly top if property is a tree
5239         private int objectLiteralStackDepth = -1;
5240         // The line number at the continuation point
5241         private int lineNumber;
5242         // The active catch label, in case the continuation point is in a try/catch block
5243         private Label catchLabel;
5244         // The number of scopes that need to be popped before control is transferred to the catch label.
5245         private int exceptionScopePops;
5246 
5247         ContinuationInfo() {
5248             this.handlerLabel = new Label("continuation_handler");
5249         }
5250 
5251         Label getHandlerLabel() {
5252             return handlerLabel;
5253         }
5254 
5255         boolean hasTargetLabel() {
5256             return targetLabel != null;
5257         }
5258 
5259         Label getTargetLabel() {
5260             return targetLabel;
5261         }
5262 
5263         void setTargetLabel(final Label targetLabel) {
5264             this.targetLabel = targetLabel;
5265         }
5266 
5267         int[] getStackStoreSpec() {
5268             return stackStoreSpec.clone();
5269         }
5270 
5271         void setStackStoreSpec(final int[] stackStoreSpec) {
5272             this.stackStoreSpec = stackStoreSpec;
5273         }
5274 
5275         Type[] getStackTypes() {
5276             return stackTypes.clone();
5277         }
5278 
5279         void setStackTypes(final Type[] stackTypes) {
5280             this.stackTypes = stackTypes;
5281         }
5282 
5283         Type getReturnValueType() {
5284             return returnValueType;
5285         }
5286 
5287         void setReturnValueType(final Type returnValueType) {
5288             this.returnValueType = returnValueType;
5289         }
5290 
5291         int getObjectLiteralStackDepth() {
5292             return objectLiteralStackDepth;
5293         }
5294 
5295         void setObjectLiteralStackDepth(final int objectLiteralStackDepth) {
5296             this.objectLiteralStackDepth = objectLiteralStackDepth;
5297         }
5298 
5299         PropertyMap getObjectLiteralMap() {
5300             return objectLiteralMap;
5301         }
5302 
5303         void setObjectLiteralMap(final PropertyMap objectLiteralMap) {
5304             this.objectLiteralMap = objectLiteralMap;
5305         }
5306 
5307         @Override
5308         public String toString() {
5309              return "[localVariableTypes=" + targetLabel.getStack().getLocalVariableTypesCopy() + ", stackStoreSpec=" +
5310                      Arrays.toString(stackStoreSpec) + ", returnValueType=" + returnValueType + "]";
5311         }
5312     }
5313 
5314     private ContinuationInfo getContinuationInfo() {
5315         return fnIdToContinuationInfo.get(lc.getCurrentFunction().getId());
5316     }
5317 
5318     private void generateContinuationHandler() {
5319         if (!isRestOf()) {
5320             return;
5321         }
5322 
5323         final ContinuationInfo ci = getContinuationInfo();
5324         method.label(ci.getHandlerLabel());
5325 
5326         // There should never be an exception thrown from the continuation handler, but in case there is (meaning,
5327         // Nashorn has a bug), then line number 0 will be an indication of where it came from (line numbers are Uint16).
5328         method.lineNumber(0);
5329 
5330         final Label.Stack stack = ci.getTargetLabel().getStack();
5331         final List<Type> lvarTypes = stack.getLocalVariableTypesCopy();
5332         final BitSet symbolBoundary = stack.getSymbolBoundaryCopy();
5333         final int lvarCount = ci.lvarCount;
5334 
5335         final Type rewriteExceptionType = Type.typeFor(RewriteException.class);
5336         // Store the RewriteException into an unused local variable slot.
5337         method.load(rewriteExceptionType, 0);
5338         method.storeTemp(rewriteExceptionType, lvarCount);
5339         // Get local variable array
5340         method.load(rewriteExceptionType, 0);
5341         method.invoke(RewriteException.GET_BYTECODE_SLOTS);
5342         // Store local variables. Note that deoptimization might introduce new value types for existing local variables,
5343         // so we must use both liveLocals and symbolBoundary, as in some cases (when the continuation is inside of a try
5344         // block) we need to store the incoming value into multiple slots. The optimism exception handlers will have
5345         // exactly one array element for every symbol that uses bytecode storage. If in the originating method the value
5346         // was undefined, there will be an explicit Undefined value in the array.
5347         int arrayIndex = 0;
5348         for(int lvarIndex = 0; lvarIndex < lvarCount;) {
5349             final Type lvarType = lvarTypes.get(lvarIndex);
5350             if(!lvarType.isUnknown()) {
5351                 method.dup();
5352                 method.load(arrayIndex).arrayload();
5353                 final Class<?> typeClass = lvarType.getTypeClass();
5354                 // Deoptimization in array initializers can cause arrays to undergo component type widening
5355                 if(typeClass == long[].class) {
5356                     method.load(rewriteExceptionType, lvarCount);
5357                     method.invoke(RewriteException.TO_LONG_ARRAY);
5358                 } else if(typeClass == double[].class) {
5359                     method.load(rewriteExceptionType, lvarCount);
5360                     method.invoke(RewriteException.TO_DOUBLE_ARRAY);
5361                 } else if(typeClass == Object[].class) {
5362                     method.load(rewriteExceptionType, lvarCount);
5363                     method.invoke(RewriteException.TO_OBJECT_ARRAY);
5364                 } else {
5365                     if(!(typeClass.isPrimitive() || typeClass == Object.class)) {
5366                         // NOTE: this can only happen with dead stores. E.g. for the program "1; []; f();" in which the
5367                         // call to f() will deoptimize the call site, but it'll expect :return to have the type
5368                         // NativeArray. However, in the more optimal version, :return's only live type is int, therefore
5369                         // "{O}:return = []" is a dead store, and the variable will be sent into the continuation as
5370                         // Undefined, however NativeArray can't hold Undefined instance.
5371                         method.loadType(Type.getInternalName(typeClass));
5372                         method.invoke(RewriteException.INSTANCE_OR_NULL);
5373                     }
5374                     method.convert(lvarType);
5375                 }
5376                 method.storeHidden(lvarType, lvarIndex, false);
5377             }
5378             final int nextLvarIndex = lvarIndex + lvarType.getSlots();
5379             if(symbolBoundary.get(nextLvarIndex - 1)) {
5380                 ++arrayIndex;
5381             }
5382             lvarIndex = nextLvarIndex;
5383         }
5384         if (AssertsEnabled.assertsEnabled()) {
5385             method.load(arrayIndex);
5386             method.invoke(RewriteException.ASSERT_ARRAY_LENGTH);
5387         } else {
5388             method.pop();
5389         }
5390 
5391         final int[]   stackStoreSpec = ci.getStackStoreSpec();
5392         final Type[]  stackTypes     = ci.getStackTypes();
5393         final boolean isStackEmpty   = stackStoreSpec.length == 0;
5394         boolean replacedObjectLiteralMap = false;
5395         if(!isStackEmpty) {
5396             // Load arguments on the stack
5397             final int objectLiteralStackDepth = ci.getObjectLiteralStackDepth();
5398             for(int i = 0; i < stackStoreSpec.length; ++i) {
5399                 final int slot = stackStoreSpec[i];
5400                 method.load(lvarTypes.get(slot), slot);
5401                 method.convert(stackTypes[i]);
5402                 // stack: s0=object literal being initialized
5403                 // change map of s0 so that the property we are initilizing when we failed
5404                 // is now ci.returnValueType
5405                 if (i == objectLiteralStackDepth) {
5406                     method.dup();
5407                     assert ci.getObjectLiteralMap() != null;
5408                     assert ScriptObject.class.isAssignableFrom(method.peekType().getTypeClass()) : method.peekType().getTypeClass() + " is not a script object";
5409                     loadConstant(ci.getObjectLiteralMap());
5410                     method.invoke(ScriptObject.SET_MAP);
5411                     replacedObjectLiteralMap = true;
5412                 }
5413             }
5414         }
5415         // Must have emitted the code for replacing the map of an object literal if we have a set object literal stack depth
5416         assert ci.getObjectLiteralStackDepth() == -1 || replacedObjectLiteralMap;
5417         // Load RewriteException back.
5418         method.load(rewriteExceptionType, lvarCount);
5419         // Get rid of the stored reference
5420         method.loadNull();
5421         method.storeHidden(Type.OBJECT, lvarCount);
5422         // Mark it dead
5423         method.markDeadSlots(lvarCount, Type.OBJECT.getSlots());
5424 
5425         // Load return value on the stack
5426         method.invoke(RewriteException.GET_RETURN_VALUE);
5427 
5428         final Type returnValueType = ci.getReturnValueType();
5429 
5430         // Set up an exception handler for primitive type conversion of return value if needed
5431         boolean needsCatch = false;
5432         final Label targetCatchLabel = ci.catchLabel;
5433         Label _try = null;
5434         if(returnValueType.isPrimitive()) {
5435             // If the conversion throws an exception, we want to report the line number of the continuation point.
5436             method.lineNumber(ci.lineNumber);
5437 
5438             if(targetCatchLabel != METHOD_BOUNDARY) {
5439                 _try = new Label("");
5440                 method.label(_try);
5441                 needsCatch = true;
5442             }
5443         }
5444 
5445         // Convert return value
5446         method.convert(returnValueType);
5447 
5448         final int scopePopCount = needsCatch ? ci.exceptionScopePops : 0;
5449 
5450         // Declare a try/catch for the conversion. If no scopes need to be popped until the target catch block, just
5451         // jump into it. Otherwise, we'll need to create a scope-popping catch block below.
5452         final Label catchLabel = scopePopCount > 0 ? new Label("") : targetCatchLabel;
5453         if(needsCatch) {
5454             final Label _end_try = new Label("");
5455             method.label(_end_try);
5456             method._try(_try, _end_try, catchLabel);
5457         }
5458 
5459         // Jump to continuation point
5460         method._goto(ci.getTargetLabel());
5461 
5462         // Make a scope-popping exception delegate if needed
5463         if(catchLabel != targetCatchLabel) {
5464             method.lineNumber(0);
5465             assert scopePopCount > 0;
5466             method._catch(catchLabel);
5467             popScopes(scopePopCount);
5468             method.uncheckedGoto(targetCatchLabel);
5469         }
5470     }
5471 }