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.GET_MAP;
  33 import static jdk.nashorn.internal.codegen.CompilerConstants.GET_STRING;
  34 import static jdk.nashorn.internal.codegen.CompilerConstants.QUICK_PREFIX;
  35 import static jdk.nashorn.internal.codegen.CompilerConstants.REGEX_PREFIX;
  36 import static jdk.nashorn.internal.codegen.CompilerConstants.RETURN;
  37 import static jdk.nashorn.internal.codegen.CompilerConstants.SCOPE;
  38 import static jdk.nashorn.internal.codegen.CompilerConstants.SPLIT_ARRAY_ARG;
  39 import static jdk.nashorn.internal.codegen.CompilerConstants.SPLIT_PREFIX;
  40 import static jdk.nashorn.internal.codegen.CompilerConstants.THIS;
  41 import static jdk.nashorn.internal.codegen.CompilerConstants.VARARGS;
  42 import static jdk.nashorn.internal.codegen.CompilerConstants.constructorNoLookup;
  43 import static jdk.nashorn.internal.codegen.CompilerConstants.interfaceCallNoLookup;
  44 import static jdk.nashorn.internal.codegen.CompilerConstants.methodDescriptor;
  45 import static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup;
  46 import static jdk.nashorn.internal.codegen.CompilerConstants.staticField;
  47 import static jdk.nashorn.internal.codegen.CompilerConstants.typeDescriptor;
  48 import static jdk.nashorn.internal.codegen.CompilerConstants.virtualCallNoLookup;
  49 import static jdk.nashorn.internal.ir.Symbol.IS_INTERNAL;
  50 import static jdk.nashorn.internal.ir.Symbol.IS_TEMP;
  51 import static jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor.CALLSITE_FAST_SCOPE;
  52 import static jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor.CALLSITE_SCOPE;
  53 import static jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor.CALLSITE_STRICT;
  54 
  55 import java.io.PrintWriter;
  56 import java.util.ArrayList;
  57 import java.util.Arrays;
  58 import java.util.EnumSet;
  59 import java.util.HashSet;
  60 import java.util.Iterator;
  61 import java.util.LinkedList;
  62 import java.util.List;
  63 import java.util.Locale;
  64 import java.util.Set;
  65 import java.util.TreeMap;
  66 import jdk.nashorn.internal.codegen.ClassEmitter.Flag;
  67 import jdk.nashorn.internal.codegen.CompilerConstants.Call;
  68 import jdk.nashorn.internal.codegen.RuntimeCallSite.SpecializedRuntimeNode;
  69 import jdk.nashorn.internal.codegen.types.ArrayType;
  70 import jdk.nashorn.internal.codegen.types.Type;
  71 import jdk.nashorn.internal.ir.AccessNode;
  72 import jdk.nashorn.internal.ir.BaseNode;
  73 import jdk.nashorn.internal.ir.BinaryNode;
  74 import jdk.nashorn.internal.ir.Block;
  75 import jdk.nashorn.internal.ir.BlockStatement;
  76 import jdk.nashorn.internal.ir.BreakNode;
  77 import jdk.nashorn.internal.ir.BreakableNode;
  78 import jdk.nashorn.internal.ir.CallNode;
  79 import jdk.nashorn.internal.ir.CaseNode;
  80 import jdk.nashorn.internal.ir.CatchNode;
  81 import jdk.nashorn.internal.ir.ContinueNode;
  82 import jdk.nashorn.internal.ir.EmptyNode;
  83 import jdk.nashorn.internal.ir.Expression;
  84 import jdk.nashorn.internal.ir.ExpressionStatement;
  85 import jdk.nashorn.internal.ir.ForNode;
  86 import jdk.nashorn.internal.ir.FunctionNode;
  87 import jdk.nashorn.internal.ir.FunctionNode.CompilationState;
  88 import jdk.nashorn.internal.ir.IdentNode;
  89 import jdk.nashorn.internal.ir.IfNode;
  90 import jdk.nashorn.internal.ir.IndexNode;
  91 import jdk.nashorn.internal.ir.LexicalContext;
  92 import jdk.nashorn.internal.ir.LexicalContextNode;
  93 import jdk.nashorn.internal.ir.LiteralNode;
  94 import jdk.nashorn.internal.ir.LiteralNode.ArrayLiteralNode;
  95 import jdk.nashorn.internal.ir.LiteralNode.ArrayLiteralNode.ArrayUnit;
  96 import jdk.nashorn.internal.ir.LoopNode;
  97 import jdk.nashorn.internal.ir.Node;
  98 import jdk.nashorn.internal.ir.ObjectNode;
  99 import jdk.nashorn.internal.ir.PropertyNode;
 100 import jdk.nashorn.internal.ir.ReturnNode;
 101 import jdk.nashorn.internal.ir.RuntimeNode;
 102 import jdk.nashorn.internal.ir.RuntimeNode.Request;
 103 import jdk.nashorn.internal.ir.SplitNode;
 104 import jdk.nashorn.internal.ir.Statement;
 105 import jdk.nashorn.internal.ir.SwitchNode;
 106 import jdk.nashorn.internal.ir.Symbol;
 107 import jdk.nashorn.internal.ir.TernaryNode;
 108 import jdk.nashorn.internal.ir.ThrowNode;
 109 import jdk.nashorn.internal.ir.TryNode;
 110 import jdk.nashorn.internal.ir.UnaryNode;
 111 import jdk.nashorn.internal.ir.VarNode;
 112 import jdk.nashorn.internal.ir.WhileNode;
 113 import jdk.nashorn.internal.ir.WithNode;
 114 import jdk.nashorn.internal.ir.debug.ASTWriter;
 115 import jdk.nashorn.internal.ir.visitor.NodeOperatorVisitor;
 116 import jdk.nashorn.internal.ir.visitor.NodeVisitor;
 117 import jdk.nashorn.internal.objects.Global;
 118 import jdk.nashorn.internal.objects.ScriptFunctionImpl;
 119 import jdk.nashorn.internal.parser.Lexer.RegexToken;
 120 import jdk.nashorn.internal.parser.TokenType;
 121 import jdk.nashorn.internal.runtime.Context;
 122 import jdk.nashorn.internal.runtime.Debug;
 123 import jdk.nashorn.internal.runtime.DebugLogger;
 124 import jdk.nashorn.internal.runtime.ECMAException;
 125 import jdk.nashorn.internal.runtime.JSType;
 126 import jdk.nashorn.internal.runtime.Property;
 127 import jdk.nashorn.internal.runtime.PropertyMap;
 128 import jdk.nashorn.internal.runtime.RecompilableScriptFunctionData;
 129 import jdk.nashorn.internal.runtime.Scope;
 130 import jdk.nashorn.internal.runtime.ScriptFunction;
 131 import jdk.nashorn.internal.runtime.ScriptObject;
 132 import jdk.nashorn.internal.runtime.ScriptRuntime;
 133 import jdk.nashorn.internal.runtime.Source;
 134 import jdk.nashorn.internal.runtime.Undefined;
 135 import jdk.nashorn.internal.runtime.arrays.ArrayData;
 136 import jdk.nashorn.internal.runtime.linker.LinkerCallSite;
 137 
 138 /**
 139  * This is the lowest tier of the code generator. It takes lowered ASTs emitted
 140  * from Lower and emits Java byte code. The byte code emission logic is broken
 141  * out into MethodEmitter. MethodEmitter works internally with a type stack, and
 142  * keeps track of the contents of the byte code stack. This way we avoid a large
 143  * number of special cases on the form
 144  * <pre>
 145  * if (type == INT) {
 146  *     visitInsn(ILOAD, slot);
 147  * } else if (type == DOUBLE) {
 148  *     visitInsn(DOUBLE, slot);
 149  * }
 150  * </pre>
 151  * This quickly became apparent when the code generator was generalized to work
 152  * with all types, and not just numbers or objects.
 153  * <p>
 154  * The CodeGenerator visits nodes only once, tags them as resolved and emits
 155  * bytecode for them.
 156  */
 157 final class CodeGenerator extends NodeOperatorVisitor<CodeGeneratorLexicalContext> {
 158 
 159     private static final String GLOBAL_OBJECT = Type.getInternalName(Global.class);
 160 
 161     private static final String SCRIPTFUNCTION_IMPL_OBJECT = Type.getInternalName(ScriptFunctionImpl.class);
 162 
 163     /** Constant data & installation. The only reason the compiler keeps this is because it is assigned
 164      *  by reflection in class installation */
 165     private final Compiler compiler;
 166 
 167     /** Call site flags given to the code generator to be used for all generated call sites */
 168     private final int callSiteFlags;
 169 
 170     /** How many regexp fields have been emitted */
 171     private int regexFieldCount;
 172 
 173     /** Line number for last statement. If we encounter a new line number, line number bytecode information
 174      *  needs to be generated */
 175     private int lastLineNumber = -1;
 176 
 177     /** When should we stop caching regexp expressions in fields to limit bytecode size? */
 178     private static final int MAX_REGEX_FIELDS = 2 * 1024;
 179 
 180     /** Current method emitter */
 181     private MethodEmitter method;
 182 
 183     /** Current compile unit */
 184     private CompileUnit unit;
 185 
 186     private static final DebugLogger LOG   = new DebugLogger("codegen", "nashorn.codegen.debug");
 187 
 188     /** From what size should we use spill instead of fields for JavaScript objects? */
 189     private static final int OBJECT_SPILL_THRESHOLD = 300;
 190 
 191     private final Set<String> emittedMethods = new HashSet<>();
 192 
 193     /**
 194      * Constructor.
 195      *
 196      * @param compiler
 197      */
 198     CodeGenerator(final Compiler compiler) {
 199         super(new CodeGeneratorLexicalContext());
 200         this.compiler      = compiler;
 201         this.callSiteFlags = compiler.getEnv()._callsite_flags;
 202     }
 203 
 204     /**
 205      * Gets the call site flags, adding the strict flag if the current function
 206      * being generated is in strict mode
 207      *
 208      * @return the correct flags for a call site in the current function
 209      */
 210     int getCallSiteFlags() {
 211         return lc.getCurrentFunction().isStrict() ? callSiteFlags | CALLSITE_STRICT : callSiteFlags;
 212     }
 213 
 214     /**
 215      * Load an identity node
 216      *
 217      * @param identNode an identity node to load
 218      * @return the method generator used
 219      */
 220     private MethodEmitter loadIdent(final IdentNode identNode) {
 221         final Symbol symbol = identNode.getSymbol();
 222 
 223         if (!symbol.isScope()) {
 224             assert symbol.hasSlot() || symbol.isParam();
 225             return method.load(symbol);
 226         }
 227 
 228         final String name   = symbol.getName();
 229         final Source source = lc.getCurrentFunction().getSource();
 230 
 231         if (CompilerConstants.__FILE__.name().equals(name)) {
 232             return method.load(source.getName());
 233         } else if (CompilerConstants.__DIR__.name().equals(name)) {
 234             return method.load(source.getBase());
 235         } else if (CompilerConstants.__LINE__.name().equals(name)) {
 236             return method.load(source.getLine(identNode.position())).convert(Type.OBJECT);
 237         } else {
 238             assert identNode.getSymbol().isScope() : identNode + " is not in scope!";
 239 
 240             final int flags = CALLSITE_SCOPE | getCallSiteFlags();
 241             method.loadCompilerConstant(SCOPE);
 242 
 243             if (isFastScope(symbol)) {
 244                 // Only generate shared scope getter for fast-scope symbols so we know we can dial in correct scope.
 245                 if (symbol.getUseCount() > SharedScopeCall.FAST_SCOPE_GET_THRESHOLD) {
 246                     return loadSharedScopeVar(identNode.getType(), symbol, flags);
 247                 }
 248                 return loadFastScopeVar(identNode.getType(), symbol, flags, identNode.isFunction());
 249             }
 250             return method.dynamicGet(identNode.getType(), identNode.getName(), flags, identNode.isFunction());
 251         }
 252     }
 253 
 254     /**
 255      * Check if this symbol can be accessed directly with a putfield or getfield or dynamic load
 256      *
 257      * @param symbol symbol to check for fast scope
 258      * @return true if fast scope
 259      */
 260     private boolean isFastScope(final Symbol symbol) {
 261         if (!symbol.isScope()) {
 262             return false;
 263         }
 264 
 265         if (!lc.inDynamicScope()) {
 266             // If there's no with or eval in context, and the symbol is marked as scoped, it is fast scoped. Such a
 267             // symbol must either be global, or its defining block must need scope.
 268             assert symbol.isGlobal() || lc.getDefiningBlock(symbol).needsScope() : symbol.getName();
 269             return true;
 270         }
 271 
 272         if (symbol.isGlobal()) {
 273             // Shortcut: if there's a with or eval in context, globals can't be fast scoped
 274             return false;
 275         }
 276 
 277         // Otherwise, check if there's a dynamic scope between use of the symbol and its definition
 278         final String name = symbol.getName();
 279         boolean previousWasBlock = false;
 280         for (final Iterator<LexicalContextNode> it = lc.getAllNodes(); it.hasNext();) {
 281             final LexicalContextNode node = it.next();
 282             if (node instanceof Block) {
 283                 // If this block defines the symbol, then we can fast scope the symbol.
 284                 final Block block = (Block)node;
 285                 if (block.getExistingSymbol(name) == symbol) {
 286                     assert block.needsScope();
 287                     return true;
 288                 }
 289                 previousWasBlock = true;
 290             } else {
 291                 if ((node instanceof WithNode && previousWasBlock) || (node instanceof FunctionNode && CodeGeneratorLexicalContext.isFunctionDynamicScope((FunctionNode)node))) {
 292                     // If we hit a scope that can have symbols introduced into it at run time before finding the defining
 293                     // block, the symbol can't be fast scoped. A WithNode only counts if we've immediately seen a block
 294                     // before - its block. Otherwise, we are currently processing the WithNode's expression, and that's
 295                     // obviously not subjected to introducing new symbols.
 296                     return false;
 297                 }
 298                 previousWasBlock = false;
 299             }
 300         }
 301         // Should've found the symbol defined in a block
 302         throw new AssertionError();
 303     }
 304 
 305     private MethodEmitter loadSharedScopeVar(final Type valueType, final Symbol symbol, final int flags) {
 306         method.load(isFastScope(symbol) ? getScopeProtoDepth(lc.getCurrentBlock(), symbol) : -1);
 307         final SharedScopeCall scopeCall = lc.getScopeGet(unit, valueType, symbol, flags | CALLSITE_FAST_SCOPE);
 308         return scopeCall.generateInvoke(method);
 309     }
 310 
 311     private MethodEmitter loadFastScopeVar(final Type valueType, final Symbol symbol, final int flags, final boolean isMethod) {
 312         loadFastScopeProto(symbol, false);
 313         return method.dynamicGet(valueType, symbol.getName(), flags | CALLSITE_FAST_SCOPE, isMethod);
 314     }
 315 
 316     private MethodEmitter storeFastScopeVar(final Type valueType, final Symbol symbol, final int flags) {
 317         loadFastScopeProto(symbol, true);
 318         method.dynamicSet(valueType, symbol.getName(), flags | CALLSITE_FAST_SCOPE);
 319         return method;
 320     }
 321 
 322     private int getScopeProtoDepth(final Block startingBlock, final Symbol symbol) {
 323         int depth = 0;
 324         final String name = symbol.getName();
 325         for(final Iterator<Block> blocks = lc.getBlocks(startingBlock); blocks.hasNext();) {
 326             final Block currentBlock = blocks.next();
 327             if (currentBlock.getExistingSymbol(name) == symbol) {
 328                 return depth;
 329             }
 330             if (currentBlock.needsScope()) {
 331                 ++depth;
 332             }
 333         }
 334         return -1;
 335     }
 336 
 337     private void loadFastScopeProto(final Symbol symbol, final boolean swap) {
 338         final int depth = getScopeProtoDepth(lc.getCurrentBlock(), symbol);
 339         assert depth != -1;
 340         if (depth > 0) {
 341             if (swap) {
 342                 method.swap();
 343             }
 344             for (int i = 0; i < depth; i++) {
 345                 method.invoke(ScriptObject.GET_PROTO);
 346             }
 347             if (swap) {
 348                 method.swap();
 349             }
 350         }
 351     }
 352 
 353     /**
 354      * Generate code that loads this node to the stack. This method is only
 355      * public to be accessible from the maps sub package. Do not call externally
 356      *
 357      * @param node node to load
 358      *
 359      * @return the method emitter used
 360      */
 361     MethodEmitter load(final Expression node) {
 362         return load(node, false);
 363     }
 364 
 365     private MethodEmitter load(final Expression node, final boolean baseAlreadyOnStack) {
 366         final Symbol symbol = node.getSymbol();
 367 
 368         // If we lack symbols, we just generate what we see.
 369         if (symbol == null) {
 370             node.accept(this);
 371             return method;
 372         }
 373 
 374         /*
 375          * The load may be of type IdentNode, e.g. "x", AccessNode, e.g. "x.y"
 376          * or IndexNode e.g. "x[y]". Both AccessNodes and IndexNodes are
 377          * BaseNodes and the logic for loading the base object is reused
 378          */
 379         final CodeGenerator codegen = this;
 380 
 381         node.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
 382             @Override
 383             public boolean enterIdentNode(final IdentNode identNode) {
 384                 loadIdent(identNode);
 385                 return false;
 386             }
 387 
 388             @Override
 389             public boolean enterAccessNode(final AccessNode accessNode) {
 390                 if (!baseAlreadyOnStack) {
 391                     load(accessNode.getBase()).convert(Type.OBJECT);
 392                 }
 393                 assert method.peekType().isObject();
 394                 method.dynamicGet(node.getType(), accessNode.getProperty().getName(), getCallSiteFlags(), accessNode.isFunction());
 395                 return false;
 396             }
 397 
 398             @Override
 399             public boolean enterIndexNode(final IndexNode indexNode) {
 400                 if (!baseAlreadyOnStack) {
 401                     load(indexNode.getBase()).convert(Type.OBJECT);
 402                     load(indexNode.getIndex());
 403                 }
 404                 method.dynamicGetIndex(node.getType(), getCallSiteFlags(), indexNode.isFunction());
 405                 return false;
 406             }
 407 
 408             @Override
 409             public boolean enterFunctionNode(FunctionNode functionNode) {
 410                 // function nodes will always leave a constructed function object on stack, no need to load the symbol
 411                 // separately as in enterDefault()
 412                 functionNode.accept(codegen);
 413                 return false;
 414             }
 415 
 416             @Override
 417             public boolean enterDefault(final Node otherNode) {
 418                 otherNode.accept(codegen); // generate code for whatever we are looking at.
 419                 method.load(symbol); // load the final symbol to the stack (or nop if no slot, then result is already there)
 420                 return false;
 421             }
 422         });
 423 
 424         return method;
 425     }
 426 
 427     @Override
 428     public boolean enterAccessNode(final AccessNode accessNode) {
 429         load(accessNode);
 430         return false;
 431     }
 432 
 433     /**
 434      * Initialize a specific set of vars to undefined. This has to be done at
 435      * the start of each method for local variables that aren't passed as
 436      * parameters.
 437      *
 438      * @param symbols list of symbols.
 439      */
 440     private void initSymbols(final Iterable<Symbol> symbols) {
 441         final LinkedList<Symbol> numbers = new LinkedList<>();
 442         final LinkedList<Symbol> objects = new LinkedList<>();
 443 
 444         for (final Symbol symbol : symbols) {
 445             /*
 446              * The following symbols are guaranteed to be defined and thus safe
 447              * from having undefined written to them: parameters internals this
 448              *
 449              * Otherwise we must, unless we perform control/escape analysis,
 450              * assign them undefined.
 451              */
 452             final boolean isInternal = symbol.isParam() || symbol.isInternal() || symbol.isThis() || !symbol.canBeUndefined();
 453 
 454             if (symbol.hasSlot() && !isInternal) {
 455                 assert symbol.getSymbolType().isNumber() || symbol.getSymbolType().isObject() : "no potentially undefined narrower local vars than doubles are allowed: " + symbol + " in " + lc.getCurrentFunction();
 456                 if (symbol.getSymbolType().isNumber()) {
 457                     numbers.add(symbol);
 458                 } else if (symbol.getSymbolType().isObject()) {
 459                     objects.add(symbol);
 460                 }
 461             }
 462         }
 463 
 464         initSymbols(numbers, Type.NUMBER);
 465         initSymbols(objects, Type.OBJECT);
 466     }
 467 
 468     private void initSymbols(final LinkedList<Symbol> symbols, final Type type) {
 469         final Iterator<Symbol> it = symbols.iterator();
 470         if(it.hasNext()) {
 471             method.loadUndefined(type);
 472             boolean hasNext;
 473             do {
 474                 final Symbol symbol = it.next();
 475                 hasNext = it.hasNext();
 476                 if(hasNext) {
 477                     method.dup();
 478                 }
 479                 method.store(symbol);
 480             } while(hasNext);
 481         }
 482     }
 483 
 484     /**
 485      * Create symbol debug information.
 486      *
 487      * @param block block containing symbols.
 488      */
 489     private void symbolInfo(final Block block) {
 490         for (final Symbol symbol : block.getSymbols()) {
 491             if (symbol.hasSlot()) {
 492                 method.localVariable(symbol, block.getEntryLabel(), block.getBreakLabel());
 493             }
 494         }
 495     }
 496 
 497     @Override
 498     public boolean enterBlock(final Block block) {
 499         if(lc.isFunctionBody() && emittedMethods.contains(lc.getCurrentFunction().getName())) {
 500             return false;
 501         }
 502         method.label(block.getEntryLabel());
 503         initLocals(block);
 504 
 505         return true;
 506     }
 507 
 508     @Override
 509     public Node leaveBlock(final Block block) {
 510         method.label(block.getBreakLabel());
 511         symbolInfo(block);
 512 
 513         if (block.needsScope() && !block.isTerminal()) {
 514             popBlockScope(block);
 515         }
 516         return block;
 517     }
 518 
 519     private void popBlockScope(final Block block) {
 520         final Label exitLabel     = new Label("block_exit");
 521         final Label recoveryLabel = new Label("block_catch");
 522         final Label skipLabel     = new Label("skip_catch");
 523 
 524         /* pop scope a la try-finally */
 525         method.loadCompilerConstant(SCOPE);
 526         method.invoke(ScriptObject.GET_PROTO);
 527         method.storeCompilerConstant(SCOPE);
 528         method._goto(skipLabel);
 529         method.label(exitLabel);
 530 
 531         method._catch(recoveryLabel);
 532         method.loadCompilerConstant(SCOPE);
 533         method.invoke(ScriptObject.GET_PROTO);
 534         method.storeCompilerConstant(SCOPE);
 535         method.athrow();
 536         method.label(skipLabel);
 537         method._try(block.getEntryLabel(), exitLabel, recoveryLabel, Throwable.class);
 538     }
 539 
 540     @Override
 541     public boolean enterBreakNode(final BreakNode breakNode) {
 542         lineNumber(breakNode);
 543 
 544         final BreakableNode breakFrom = lc.getBreakable(breakNode.getLabel());
 545         for (int i = 0; i < lc.getScopeNestingLevelTo(breakFrom); i++) {
 546             closeWith();
 547         }
 548         method.splitAwareGoto(lc, breakFrom.getBreakLabel());
 549 
 550         return false;
 551     }
 552 
 553     private int loadArgs(final List<Expression> args) {
 554         return loadArgs(args, null, false, args.size());
 555     }
 556 
 557     private int loadArgs(final List<Expression> args, final String signature, final boolean isVarArg, final int argCount) {
 558         // arg have already been converted to objects here.
 559         if (isVarArg || argCount > LinkerCallSite.ARGLIMIT) {
 560             loadArgsArray(args);
 561             return 1;
 562         }
 563 
 564         // pad with undefined if size is too short. argCount is the real number of args
 565         int n = 0;
 566         final Type[] params = signature == null ? null : Type.getMethodArguments(signature);
 567         for (final Expression arg : args) {
 568             assert arg != null;
 569             load(arg);
 570             if (n >= argCount) {
 571                 method.pop(); // we had to load the arg for its side effects
 572             } else if (params != null) {
 573                 method.convert(params[n]);
 574             }
 575             n++;
 576         }
 577 
 578         while (n < argCount) {
 579             method.loadUndefined(Type.OBJECT);
 580             n++;
 581         }
 582 
 583         return argCount;
 584     }
 585 
 586     @Override
 587     public boolean enterCallNode(final CallNode callNode) {
 588         lineNumber(callNode.getLineNumber());
 589 
 590         final List<Expression> args = callNode.getArgs();
 591         final Expression function = callNode.getFunction();
 592         final Block currentBlock = lc.getCurrentBlock();
 593         final CodeGeneratorLexicalContext codegenLexicalContext = lc;
 594         final Type callNodeType = callNode.getType();
 595 
 596         function.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
 597 
 598             private MethodEmitter sharedScopeCall(final IdentNode identNode, final int flags) {
 599                 final Symbol symbol = identNode.getSymbol();
 600                 int    scopeCallFlags = flags;
 601                 method.loadCompilerConstant(SCOPE);
 602                 if (isFastScope(symbol)) {
 603                     method.load(getScopeProtoDepth(currentBlock, symbol));
 604                     scopeCallFlags |= CALLSITE_FAST_SCOPE;
 605                 } else {
 606                     method.load(-1); // Bypass fast-scope code in shared callsite
 607                 }
 608                 loadArgs(args);
 609                 final Type[] paramTypes = method.getTypesFromStack(args.size());
 610                 final SharedScopeCall scopeCall = codegenLexicalContext.getScopeCall(unit, symbol, identNode.getType(), callNodeType, paramTypes, scopeCallFlags);
 611                 return scopeCall.generateInvoke(method);
 612             }
 613 
 614             private void scopeCall(final IdentNode node, final int flags) {
 615                 load(node);
 616                 method.convert(Type.OBJECT); // foo() makes no sense if foo == 3
 617                 // ScriptFunction will see CALLSITE_SCOPE and will bind scope accordingly.
 618                 method.loadNull(); //the 'this'
 619                 method.dynamicCall(callNodeType, 2 + loadArgs(args), flags);
 620             }
 621 
 622             private void evalCall(final IdentNode node, final int flags) {
 623                 load(node);
 624                 method.convert(Type.OBJECT); // foo() makes no sense if foo == 3
 625 
 626                 final Label not_eval  = new Label("not_eval");
 627                 final Label eval_done = new Label("eval_done");
 628 
 629                 // check if this is the real built-in eval
 630                 method.dup();
 631                 globalIsEval();
 632 
 633                 method.ifeq(not_eval);
 634                 // We don't need ScriptFunction object for 'eval'
 635                 method.pop();
 636 
 637                 method.loadCompilerConstant(SCOPE); // Load up self (scope).
 638 
 639                 final CallNode.EvalArgs evalArgs = callNode.getEvalArgs();
 640                 // load evaluated code
 641                 load(evalArgs.getCode());
 642                 method.convert(Type.OBJECT);
 643                 // special/extra 'eval' arguments
 644                 load(evalArgs.getThis());
 645                 method.load(evalArgs.getLocation());
 646                 method.load(evalArgs.getStrictMode());
 647                 method.convert(Type.OBJECT);
 648 
 649                 // direct call to Global.directEval
 650                 globalDirectEval();
 651                 method.convert(callNodeType);
 652                 method._goto(eval_done);
 653 
 654                 method.label(not_eval);
 655                 // This is some scope 'eval' or global eval replaced by user
 656                 // but not the built-in ECMAScript 'eval' function call
 657                 method.loadNull();
 658                 method.dynamicCall(callNodeType, 2 + loadArgs(args), flags);
 659 
 660                 method.label(eval_done);
 661             }
 662 
 663             @Override
 664             public boolean enterIdentNode(final IdentNode node) {
 665                 final Symbol symbol = node.getSymbol();
 666 
 667                 if (symbol.isScope()) {
 668                     final int flags = getCallSiteFlags() | CALLSITE_SCOPE;
 669                     final int useCount = symbol.getUseCount();
 670 
 671                     // Threshold for generating shared scope callsite is lower for fast scope symbols because we know
 672                     // we can dial in the correct scope. However, we also need to enable it for non-fast scopes to
 673                     // support huge scripts like mandreel.js.
 674                     if (callNode.isEval()) {
 675                         evalCall(node, flags);
 676                     } else if (useCount <= SharedScopeCall.FAST_SCOPE_CALL_THRESHOLD
 677                             || (!isFastScope(symbol) && useCount <= SharedScopeCall.SLOW_SCOPE_CALL_THRESHOLD)
 678                             || CodeGenerator.this.lc.inDynamicScope()) {
 679                         scopeCall(node, flags);
 680                     } else {
 681                         sharedScopeCall(node, flags);
 682                     }
 683                     assert method.peekType().equals(callNodeType) : method.peekType() + "!=" + callNode.getType();
 684                 } else {
 685                     enterDefault(node);
 686                 }
 687 
 688                 return false;
 689             }
 690 
 691             @Override
 692             public boolean enterAccessNode(final AccessNode node) {
 693                 load(node.getBase());
 694                 method.convert(Type.OBJECT);
 695                 method.dup();
 696                 method.dynamicGet(node.getType(), node.getProperty().getName(), getCallSiteFlags(), true);
 697                 method.swap();
 698                 method.dynamicCall(callNodeType, 2 + loadArgs(args), getCallSiteFlags());
 699                 assert method.peekType().equals(callNodeType);
 700 
 701                 return false;
 702             }
 703 
 704             @Override
 705             public boolean enterFunctionNode(final FunctionNode origCallee) {
 706                 // NOTE: visiting the callee will leave a constructed ScriptFunction object on the stack if
 707                 // callee.needsCallee() == true
 708                 final FunctionNode callee = (FunctionNode)origCallee.accept(CodeGenerator.this);
 709 
 710                 final boolean      isVarArg = callee.isVarArg();
 711                 final int          argCount = isVarArg ? -1 : callee.getParameters().size();
 712 
 713                 final String signature = new FunctionSignature(true, callee.needsCallee(), callee.getReturnType(), isVarArg ? null : callee.getParameters()).toString();
 714 
 715                 if (callee.isStrict()) { // self is undefined
 716                     method.loadUndefined(Type.OBJECT);
 717                 } else { // get global from scope (which is the self)
 718                     globalInstance();
 719                 }
 720                 loadArgs(args, signature, isVarArg, argCount);
 721                 assert callee.getCompileUnit() != null : "no compile unit for " + callee.getName() + " " + Debug.id(callee) + " " + callNode;
 722                 method.invokestatic(callee.getCompileUnit().getUnitClassName(), callee.getName(), signature);
 723                 assert method.peekType().equals(callee.getReturnType()) : method.peekType() + " != " + callee.getReturnType();
 724                 method.convert(callNodeType);
 725                 return false;
 726             }
 727 
 728             @Override
 729             public boolean enterIndexNode(final IndexNode node) {
 730                 load(node.getBase());
 731                 method.convert(Type.OBJECT);
 732                 method.dup();
 733                 load(node.getIndex());
 734                 final Type indexType = node.getIndex().getType();
 735                 if (indexType.isObject() || indexType.isBoolean()) {
 736                     method.convert(Type.OBJECT); //TODO
 737                 }
 738                 method.dynamicGetIndex(node.getType(), getCallSiteFlags(), true);
 739                 method.swap();
 740                 method.dynamicCall(callNodeType, 2 + loadArgs(args), getCallSiteFlags());
 741                 assert method.peekType().equals(callNode.getType());
 742 
 743                 return false;
 744             }
 745 
 746             @Override
 747             protected boolean enterDefault(final Node node) {
 748                 // Load up function.
 749                 load(function);
 750                 method.convert(Type.OBJECT); //TODO, e.g. booleans can be used as functions
 751                 method.loadNull(); // ScriptFunction will figure out the correct this when it sees CALLSITE_SCOPE
 752                 method.dynamicCall(callNodeType, 2 + loadArgs(args), getCallSiteFlags() | CALLSITE_SCOPE);
 753                 assert method.peekType().equals(callNode.getType());
 754 
 755                 return false;
 756             }
 757         });
 758 
 759         method.store(callNode.getSymbol());
 760 
 761         return false;
 762     }
 763 
 764     @Override
 765     public boolean enterContinueNode(final ContinueNode continueNode) {
 766         lineNumber(continueNode);
 767 
 768         final LoopNode continueTo = lc.getContinueTo(continueNode.getLabel());
 769         for (int i = 0; i < lc.getScopeNestingLevelTo(continueTo); i++) {
 770             closeWith();
 771         }
 772         method.splitAwareGoto(lc, continueTo.getContinueLabel());
 773 
 774         return false;
 775     }
 776 
 777     @Override
 778     public boolean enterEmptyNode(final EmptyNode emptyNode) {
 779         lineNumber(emptyNode);
 780 
 781         return false;
 782     }
 783 
 784     @Override
 785     public boolean enterExpressionStatement(final ExpressionStatement expressionStatement) {
 786         lineNumber(expressionStatement);
 787 
 788         expressionStatement.getExpression().accept(this);
 789 
 790         return false;
 791     }
 792 
 793     @Override
 794     public boolean enterBlockStatement(final BlockStatement blockStatement) {
 795         lineNumber(blockStatement);
 796 
 797         blockStatement.getBlock().accept(this);
 798 
 799         return false;
 800     }
 801 
 802     @Override
 803     public boolean enterForNode(final ForNode forNode) {
 804         lineNumber(forNode);
 805 
 806         if (forNode.isForIn()) {
 807             enterForIn(forNode);
 808         } else {
 809             enterFor(forNode);
 810         }
 811 
 812         return false;
 813     }
 814 
 815     private void enterFor(final ForNode forNode) {
 816         final Expression init   = forNode.getInit();
 817         final Expression test   = forNode.getTest();
 818         final Block      body   = forNode.getBody();
 819         final Expression modify = forNode.getModify();
 820 
 821         if (init != null) {
 822             init.accept(this);
 823         }
 824 
 825         final Label loopLabel = new Label("loop");
 826         final Label testLabel = new Label("test");
 827 
 828         method._goto(testLabel);
 829         method.label(loopLabel);
 830         body.accept(this);
 831         method.label(forNode.getContinueLabel());
 832 
 833         if (!body.isTerminal() && modify != null) {
 834             load(modify);
 835         }
 836 
 837         method.label(testLabel);
 838         if (test != null) {
 839             new BranchOptimizer(this, method).execute(test, loopLabel, true);
 840         } else {
 841             method._goto(loopLabel);
 842         }
 843 
 844         method.label(forNode.getBreakLabel());
 845     }
 846 
 847     private void enterForIn(final ForNode forNode) {
 848         final Block body   = forNode.getBody();
 849         final Expression  modify = forNode.getModify();
 850 
 851         final Symbol iter      = forNode.getIterator();
 852         final Label  loopLabel = new Label("loop");
 853 
 854         final Expression init = forNode.getInit();
 855 
 856         load(modify);
 857         assert modify.getType().isObject();
 858         method.invoke(forNode.isForEach() ? ScriptRuntime.TO_VALUE_ITERATOR : ScriptRuntime.TO_PROPERTY_ITERATOR);
 859         method.store(iter);
 860         method._goto(forNode.getContinueLabel());
 861         method.label(loopLabel);
 862 
 863         new Store<Expression>(init) {
 864             @Override
 865             protected void storeNonDiscard() {
 866                 return;
 867             }
 868             @Override
 869             protected void evaluate() {
 870                 method.load(iter);
 871                 method.invoke(interfaceCallNoLookup(Iterator.class, "next", Object.class));
 872             }
 873         }.store();
 874 
 875         body.accept(this);
 876 
 877         method.label(forNode.getContinueLabel());
 878         method.load(iter);
 879         method.invoke(interfaceCallNoLookup(Iterator.class, "hasNext", boolean.class));
 880         method.ifne(loopLabel);
 881         method.label(forNode.getBreakLabel());
 882     }
 883 
 884     /**
 885      * Initialize the slots in a frame to undefined.
 886      *
 887      * @param block block with local vars.
 888      */
 889     private void initLocals(final Block block) {
 890         lc.nextFreeSlot(block);
 891 
 892         final boolean isFunctionBody = lc.isFunctionBody();
 893 
 894         final FunctionNode function = lc.getCurrentFunction();
 895         if (isFunctionBody) {
 896             if(method.hasScope()) {
 897                 if (function.needsParentScope()) {
 898                     method.loadCompilerConstant(CALLEE);
 899                     method.invoke(ScriptFunction.GET_SCOPE);
 900                 } else {
 901                     assert function.hasScopeBlock();
 902                     method.loadNull();
 903                 }
 904                 method.storeCompilerConstant(SCOPE);
 905             }
 906             if (function.needsArguments()) {
 907                 initArguments(function);
 908             }
 909         }
 910 
 911         /*
 912          * Determine if block needs scope, if not, just do initSymbols for this block.
 913          */
 914         if (block.needsScope()) {
 915             /*
 916              * Determine if function is varargs and consequently variables have to
 917              * be in the scope.
 918              */
 919             final boolean varsInScope = function.allVarsInScope();
 920 
 921             // TODO for LET we can do better: if *block* does not contain any eval/with, we don't need its vars in scope.
 922 
 923             final List<String> nameList = new ArrayList<>();
 924             final List<Symbol> locals   = new ArrayList<>();
 925 
 926             // Initalize symbols and values
 927             final List<Symbol> newSymbols = new ArrayList<>();
 928             final List<Symbol> values     = new ArrayList<>();
 929 
 930             final boolean hasArguments = function.needsArguments();
 931 
 932             for (final Symbol symbol : block.getSymbols()) {
 933 
 934                 if (symbol.isInternal() || symbol.isThis() || symbol.isTemp()) {
 935                     continue;
 936                 }
 937 
 938                 if (symbol.isVar()) {
 939                     if (varsInScope || symbol.isScope()) {
 940                         nameList.add(symbol.getName());
 941                         newSymbols.add(symbol);
 942                         values.add(null);
 943                         assert symbol.isScope()   : "scope for " + symbol + " should have been set in Lower already " + function.getName();
 944                         assert !symbol.hasSlot()  : "slot for " + symbol + " should have been removed in Lower already" + function.getName();
 945                     } else {
 946                         assert symbol.hasSlot() : symbol + " should have a slot only, no scope";
 947                         locals.add(symbol);
 948                     }
 949                 } else if (symbol.isParam() && (varsInScope || hasArguments || symbol.isScope())) {
 950                     nameList.add(symbol.getName());
 951                     newSymbols.add(symbol);
 952                     values.add(hasArguments ? null : symbol);
 953                     assert symbol.isScope()   : "scope for " + symbol + " should have been set in Lower already " + function.getName() + " varsInScope="+varsInScope+" hasArguments="+hasArguments+" symbol.isScope()=" + symbol.isScope();
 954                     assert !(hasArguments && symbol.hasSlot())  : "slot for " + symbol + " should have been removed in Lower already " + function.getName();
 955                 }
 956             }
 957 
 958             // we may have locals that need to be initialized
 959             initSymbols(locals);
 960 
 961             /*
 962              * Create a new object based on the symbols and values, generate
 963              * bootstrap code for object
 964              */
 965             new FieldObjectCreator<Symbol>(this, nameList, newSymbols, values, true, hasArguments) {
 966                 @Override
 967                 protected void loadValue(final Symbol value) {
 968                     method.load(value);
 969                 }
 970             }.makeObject(method);
 971 
 972             // runScript(): merge scope into global
 973             if (isFunctionBody && function.isProgram()) {
 974                 method.invoke(ScriptRuntime.MERGE_SCOPE);
 975             }
 976 
 977             method.storeCompilerConstant(SCOPE);
 978         } else {
 979             // Since we don't have a scope, parameters didn't get assigned array indices by the FieldObjectCreator, so
 980             // we need to assign them separately here.
 981             int nextParam = 0;
 982             if (isFunctionBody && function.isVarArg()) {
 983                 for (final IdentNode param : function.getParameters()) {
 984                     param.getSymbol().setFieldIndex(nextParam++);
 985                 }
 986             }
 987 
 988             initSymbols(block.getSymbols());
 989         }
 990 
 991         // Debugging: print symbols? @see --print-symbols flag
 992         printSymbols(block, (isFunctionBody ? "Function " : "Block in ") + (function.getIdent() == null ? "<anonymous>" : function.getIdent().getName()));
 993     }
 994 
 995     private void initArguments(final FunctionNode function) {
 996         method.loadCompilerConstant(VARARGS);
 997         if (function.needsCallee()) {
 998             method.loadCompilerConstant(CALLEE);
 999         } else {
1000             // If function is strict mode, "arguments.callee" is not populated, so we don't necessarily need the
1001             // caller.
1002             assert function.isStrict();
1003             method.loadNull();
1004         }
1005         method.load(function.getParameters().size());
1006         globalAllocateArguments();
1007         method.storeCompilerConstant(ARGUMENTS);
1008     }
1009 
1010     @Override
1011     public boolean enterFunctionNode(final FunctionNode functionNode) {
1012         if (functionNode.isLazy()) {
1013             // Must do it now; can't postpone it until leaveFunctionNode()
1014             newFunctionObject(functionNode, functionNode);
1015             return false;
1016         }
1017 
1018         final String fnName = functionNode.getName();
1019         // NOTE: we only emit the method for a function with the given name once. We can have multiple functions with
1020         // the same name as a result of inlining finally blocks. However, in the future -- with type specialization,
1021         // notably -- we might need to check for both name *and* signature. Of course, even that might not be
1022         // sufficient; the function might have a code dependency on the type of the variables in its enclosing scopes,
1023         // and the type of such a variable can be different in catch and finally blocks. So, in the future we will have
1024         // to decide to either generate a unique method for each inlined copy of the function, maybe figure out its
1025         // exact type closure and deduplicate based on that, or just decide that functions in finally blocks aren't
1026         // worth it, and generate one method with most generic type closure.
1027         if(!emittedMethods.contains(fnName)) {
1028             LOG.info("=== BEGIN ", fnName);
1029 
1030             assert functionNode.getCompileUnit() != null : "no compile unit for " + fnName + " " + Debug.id(functionNode);
1031             unit = lc.pushCompileUnit(functionNode.getCompileUnit());
1032             assert lc.hasCompileUnits();
1033 
1034             method = lc.pushMethodEmitter(unit.getClassEmitter().method(functionNode));
1035             // new method - reset last line number
1036             lastLineNumber = -1;
1037             // Mark end for variable tables.
1038             method.begin();
1039         }
1040 
1041         return true;
1042     }
1043 
1044     @Override
1045     public Node leaveFunctionNode(final FunctionNode functionNode) {
1046         try {
1047             if(emittedMethods.add(functionNode.getName())) {
1048                 method.end(); // wrap up this method
1049                 unit   = lc.popCompileUnit(functionNode.getCompileUnit());
1050                 method = lc.popMethodEmitter(method);
1051                 LOG.info("=== END ", functionNode.getName());
1052             }
1053 
1054             final FunctionNode newFunctionNode = functionNode.setState(lc, CompilationState.EMITTED);
1055             newFunctionObject(newFunctionNode, functionNode);
1056             return newFunctionNode;
1057         } catch (final Throwable t) {
1058             Context.printStackTrace(t);
1059             final VerifyError e = new VerifyError("Code generation bug in \"" + functionNode.getName() + "\": likely stack misaligned: " + t + " " + functionNode.getSource().getName());
1060             e.initCause(t);
1061             throw e;
1062         }
1063     }
1064 
1065     @Override
1066     public boolean enterIdentNode(final IdentNode identNode) {
1067         return false;
1068     }
1069 
1070     @Override
1071     public boolean enterIfNode(final IfNode ifNode) {
1072         lineNumber(ifNode);
1073 
1074         final Expression test = ifNode.getTest();
1075         final Block pass = ifNode.getPass();
1076         final Block fail = ifNode.getFail();
1077 
1078         final Label failLabel  = new Label("if_fail");
1079         final Label afterLabel = fail == null ? failLabel : new Label("if_done");
1080 
1081         new BranchOptimizer(this, method).execute(test, failLabel, false);
1082 
1083         boolean passTerminal = false;
1084         boolean failTerminal = false;
1085 
1086         pass.accept(this);
1087         if (!pass.hasTerminalFlags()) {
1088             method._goto(afterLabel); //don't fallthru to fail block
1089         } else {
1090             passTerminal = pass.isTerminal();
1091         }
1092 
1093         if (fail != null) {
1094             method.label(failLabel);
1095             fail.accept(this);
1096             failTerminal = fail.isTerminal();
1097         }
1098 
1099         //if if terminates, put the after label there
1100         if (!passTerminal || !failTerminal) {
1101             method.label(afterLabel);
1102         }
1103 
1104         return false;
1105     }
1106 
1107     @Override
1108     public boolean enterIndexNode(final IndexNode indexNode) {
1109         load(indexNode);
1110         return false;
1111     }
1112 
1113     private void lineNumber(final Statement statement) {
1114         lineNumber(statement.getLineNumber());
1115     }
1116 
1117     private void lineNumber(int lineNumber) {
1118         if (lineNumber != lastLineNumber) {
1119             method.lineNumber(lineNumber);
1120         }
1121         lastLineNumber = lineNumber;
1122     }
1123 
1124     /**
1125      * Load a list of nodes as an array of a specific type
1126      * The array will contain the visited nodes.
1127      *
1128      * @param arrayLiteralNode the array of contents
1129      * @param arrayType        the type of the array, e.g. ARRAY_NUMBER or ARRAY_OBJECT
1130      *
1131      * @return the method generator that was used
1132      */
1133     private MethodEmitter loadArray(final ArrayLiteralNode arrayLiteralNode, final ArrayType arrayType) {
1134         assert arrayType == Type.INT_ARRAY || arrayType == Type.LONG_ARRAY || arrayType == Type.NUMBER_ARRAY || arrayType == Type.OBJECT_ARRAY;
1135 
1136         final Expression[]    nodes    = arrayLiteralNode.getValue();
1137         final Object          presets  = arrayLiteralNode.getPresets();
1138         final int[]           postsets = arrayLiteralNode.getPostsets();
1139         final Class<?>        type     = arrayType.getTypeClass();
1140         final List<ArrayUnit> units    = arrayLiteralNode.getUnits();
1141 
1142         loadConstant(presets);
1143 
1144         final Type elementType = arrayType.getElementType();
1145 
1146         if (units != null) {
1147             final MethodEmitter savedMethod     = method;
1148             final FunctionNode  currentFunction = lc.getCurrentFunction();
1149 
1150             for (final ArrayUnit arrayUnit : units) {
1151                 unit = lc.pushCompileUnit(arrayUnit.getCompileUnit());
1152 
1153                 final String className = unit.getUnitClassName();
1154                 final String name      = currentFunction.uniqueName(SPLIT_PREFIX.symbolName());
1155                 final String signature = methodDescriptor(type, ScriptFunction.class, Object.class, ScriptObject.class, type);
1156 
1157                 final MethodEmitter me = unit.getClassEmitter().method(EnumSet.of(Flag.PUBLIC, Flag.STATIC), name, signature);
1158                 method = lc.pushMethodEmitter(me);
1159 
1160                 method.setFunctionNode(currentFunction);
1161                 method.begin();
1162 
1163                 fixScopeSlot(currentFunction);
1164 
1165                 method.load(arrayType, SPLIT_ARRAY_ARG.slot());
1166 
1167                 for (int i = arrayUnit.getLo(); i < arrayUnit.getHi(); i++) {
1168                     storeElement(nodes, elementType, postsets[i]);
1169                 }
1170 
1171                 method._return();
1172                 method.end();
1173                 method = lc.popMethodEmitter(me);
1174 
1175                 assert method == savedMethod;
1176                 method.loadCompilerConstant(CALLEE);
1177                 method.swap();
1178                 method.loadCompilerConstant(THIS);
1179                 method.swap();
1180                 method.loadCompilerConstant(SCOPE);
1181                 method.swap();
1182                 method.invokestatic(className, name, signature);
1183 
1184                 unit = lc.popCompileUnit(unit);
1185             }
1186 
1187             return method;
1188         }
1189 
1190         for (final int postset : postsets) {
1191             storeElement(nodes, elementType, postset);
1192         }
1193 
1194         return method;
1195     }
1196 
1197     private void storeElement(final Expression[] nodes, final Type elementType, final int index) {
1198         method.dup();
1199         method.load(index);
1200 
1201         final Expression element = nodes[index];
1202 
1203         if (element == null) {
1204             method.loadEmpty(elementType);
1205         } else {
1206             assert elementType.isEquivalentTo(element.getType()) : "array element type doesn't match array type";
1207             load(element);
1208         }
1209 
1210         method.arraystore();
1211     }
1212 
1213     private MethodEmitter loadArgsArray(final List<Expression> args) {
1214         final Object[] array = new Object[args.size()];
1215         loadConstant(array);
1216 
1217         for (int i = 0; i < args.size(); i++) {
1218             method.dup();
1219             method.load(i);
1220             load(args.get(i)).convert(Type.OBJECT); //has to be upcast to object or we fail
1221             method.arraystore();
1222         }
1223 
1224         return method;
1225     }
1226 
1227     /**
1228      * Load a constant from the constant array. This is only public to be callable from the objects
1229      * subpackage. Do not call directly.
1230      *
1231      * @param string string to load
1232      */
1233     void loadConstant(final String string) {
1234         final String       unitClassName = unit.getUnitClassName();
1235         final ClassEmitter classEmitter  = unit.getClassEmitter();
1236         final int          index         = compiler.getConstantData().add(string);
1237 
1238         method.load(index);
1239         method.invokestatic(unitClassName, GET_STRING.symbolName(), methodDescriptor(String.class, int.class));
1240         classEmitter.needGetConstantMethod(String.class);
1241     }
1242 
1243     /**
1244      * Load a constant from the constant array. This is only public to be callable from the objects
1245      * subpackage. Do not call directly.
1246      *
1247      * @param object object to load
1248      */
1249     void loadConstant(final Object object) {
1250         final String       unitClassName = unit.getUnitClassName();
1251         final ClassEmitter classEmitter  = unit.getClassEmitter();
1252         final int          index         = compiler.getConstantData().add(object);
1253         final Class<?>     cls           = object.getClass();
1254 
1255         if (cls == PropertyMap.class) {
1256             method.load(index);
1257             method.invokestatic(unitClassName, GET_MAP.symbolName(), methodDescriptor(PropertyMap.class, int.class));
1258             classEmitter.needGetConstantMethod(PropertyMap.class);
1259         } else if (cls.isArray()) {
1260             method.load(index);
1261             final String methodName = ClassEmitter.getArrayMethodName(cls);
1262             method.invokestatic(unitClassName, methodName, methodDescriptor(cls, int.class));
1263             classEmitter.needGetConstantMethod(cls);
1264         } else {
1265             method.loadConstants().load(index).arrayload();
1266             if (object instanceof ArrayData) {
1267                 // avoid cast to non-public ArrayData subclass
1268                 method.checkcast(ArrayData.class);
1269                 method.invoke(virtualCallNoLookup(ArrayData.class, "copy", ArrayData.class));
1270             } else if (cls != Object.class) {
1271                 method.checkcast(cls);
1272             }
1273         }
1274     }
1275 
1276     // literal values
1277     private MethodEmitter load(final LiteralNode<?> node) {
1278         final Object value = node.getValue();
1279 
1280         if (value == null) {
1281             method.loadNull();
1282         } else if (value instanceof Undefined) {
1283             method.loadUndefined(Type.OBJECT);
1284         } else if (value instanceof String) {
1285             final String string = (String)value;
1286 
1287             if (string.length() > (MethodEmitter.LARGE_STRING_THRESHOLD / 3)) { // 3 == max bytes per encoded char
1288                 loadConstant(string);
1289             } else {
1290                 method.load(string);
1291             }
1292         } else if (value instanceof RegexToken) {
1293             loadRegex((RegexToken)value);
1294         } else if (value instanceof Boolean) {
1295             method.load((Boolean)value);
1296         } else if (value instanceof Integer) {
1297             method.load((Integer)value);
1298         } else if (value instanceof Long) {
1299             method.load((Long)value);
1300         } else if (value instanceof Double) {
1301             method.load((Double)value);
1302         } else if (node instanceof ArrayLiteralNode) {
1303             final ArrayType type = (ArrayType)node.getType();
1304             loadArray((ArrayLiteralNode)node, type);
1305             globalAllocateArray(type);
1306         } else {
1307             assert false : "Unknown literal for " + node.getClass() + " " + value.getClass() + " " + value;
1308         }
1309 
1310         return method;
1311     }
1312 
1313     private MethodEmitter loadRegexToken(final RegexToken value) {
1314         method.load(value.getExpression());
1315         method.load(value.getOptions());
1316         return globalNewRegExp();
1317     }
1318 
1319     private MethodEmitter loadRegex(final RegexToken regexToken) {
1320         if (regexFieldCount > MAX_REGEX_FIELDS) {
1321             return loadRegexToken(regexToken);
1322         }
1323         // emit field
1324         final String       regexName    = lc.getCurrentFunction().uniqueName(REGEX_PREFIX.symbolName());
1325         final ClassEmitter classEmitter = unit.getClassEmitter();
1326 
1327         classEmitter.field(EnumSet.of(PRIVATE, STATIC), regexName, Object.class);
1328         regexFieldCount++;
1329 
1330         // get field, if null create new regex, finally clone regex object
1331         method.getStatic(unit.getUnitClassName(), regexName, typeDescriptor(Object.class));
1332         method.dup();
1333         final Label cachedLabel = new Label("cached");
1334         method.ifnonnull(cachedLabel);
1335 
1336         method.pop();
1337         loadRegexToken(regexToken);
1338         method.dup();
1339         method.putStatic(unit.getUnitClassName(), regexName, typeDescriptor(Object.class));
1340 
1341         method.label(cachedLabel);
1342         globalRegExpCopy();
1343 
1344         return method;
1345     }
1346 
1347     @Override
1348     public boolean enterLiteralNode(final LiteralNode<?> literalNode) {
1349         assert literalNode.getSymbol() != null : literalNode + " has no symbol";
1350         load(literalNode).store(literalNode.getSymbol());
1351         return false;
1352     }
1353 
1354     @Override
1355     public boolean enterObjectNode(final ObjectNode objectNode) {
1356         final List<PropertyNode> elements = objectNode.getElements();
1357 
1358         final List<String>     keys    = new ArrayList<>();
1359         final List<Symbol>     symbols = new ArrayList<>();
1360         final List<Expression> values  = new ArrayList<>();
1361 
1362         boolean hasGettersSetters = false;
1363         Expression protoNode = null;
1364 
1365         for (PropertyNode propertyNode: elements) {
1366             final Expression   value        = propertyNode.getValue();
1367             final String       key          = propertyNode.getKeyName();
1368             final Symbol       symbol       = value == null ? null : propertyNode.getKey().getSymbol();
1369 
1370             if (value == null) {
1371                 hasGettersSetters = true;
1372             } else if (key.equals(ScriptObject.PROTO_PROPERTY_NAME)) {
1373                 protoNode = value;
1374                 continue;
1375             }
1376 
1377             keys.add(key);
1378             symbols.add(symbol);
1379             values.add(value);
1380         }
1381 
1382         if (elements.size() > OBJECT_SPILL_THRESHOLD) {
1383             new SpillObjectCreator(this, keys, symbols, values).makeObject(method);
1384         } else {
1385             new FieldObjectCreator<Expression>(this, keys, symbols, values) {
1386                 @Override
1387                 protected void loadValue(final Expression node) {
1388                     load(node);
1389                 }
1390 
1391                 /**
1392                  * Ensure that the properties start out as object types so that
1393                  * we can do putfield initializations instead of dynamicSetIndex
1394                  * which would be the case to determine initial property type
1395                  * otherwise.
1396                  *
1397                  * Use case, it's very expensive to do a million var x = {a:obj, b:obj}
1398                  * just to have to invalidate them immediately on initialization
1399                  *
1400                  * see NASHORN-594
1401                  */
1402                 @Override
1403                 protected MapCreator newMapCreator(final Class<?> fieldObjectClass) {
1404                     return new MapCreator(fieldObjectClass, keys, symbols) {
1405                         @Override
1406                         protected int getPropertyFlags(final Symbol symbol, final boolean hasArguments) {
1407                             return super.getPropertyFlags(symbol, hasArguments) | Property.IS_ALWAYS_OBJECT;
1408                         }
1409                     };
1410                 }
1411 
1412             }.makeObject(method);
1413         }
1414 
1415         method.dup();
1416         if (protoNode != null) {
1417             load(protoNode);
1418             method.invoke(ScriptObject.SET_PROTO_CHECK);
1419         } else {
1420             globalObjectPrototype();
1421             method.invoke(ScriptObject.SET_PROTO);
1422         }
1423 
1424         if (hasGettersSetters) {
1425             for (final PropertyNode propertyNode : elements) {
1426                 final FunctionNode getter       = propertyNode.getGetter();
1427                 final FunctionNode setter       = propertyNode.getSetter();
1428 
1429                 if (getter == null && setter == null) {
1430                     continue;
1431                 }
1432 
1433                 method.dup().loadKey(propertyNode.getKey());
1434 
1435                 if (getter == null) {
1436                     method.loadNull();
1437                 } else {
1438                     getter.accept(this);
1439                 }
1440 
1441                 if (setter == null) {
1442                     method.loadNull();
1443                 } else {
1444                     setter.accept(this);
1445                 }
1446 
1447                 method.invoke(ScriptObject.SET_USER_ACCESSORS);
1448             }
1449         }
1450 
1451         method.store(objectNode.getSymbol());
1452         return false;
1453     }
1454 
1455     @Override
1456     public boolean enterReturnNode(final ReturnNode returnNode) {
1457         lineNumber(returnNode);
1458 
1459         method.registerReturn();
1460 
1461         final Type returnType = lc.getCurrentFunction().getReturnType();
1462 
1463         final Expression expression = returnNode.getExpression();
1464         if (expression != null) {
1465             load(expression);
1466         } else {
1467             method.loadUndefined(returnType);
1468         }
1469 
1470         method._return(returnType);
1471 
1472         return false;
1473     }
1474 
1475     private static boolean isNullLiteral(final Node node) {
1476         return node instanceof LiteralNode<?> && ((LiteralNode<?>) node).isNull();
1477     }
1478 
1479     private boolean nullCheck(final RuntimeNode runtimeNode, final List<Expression> args, final String signature) {
1480         final Request request = runtimeNode.getRequest();
1481 
1482         if (!Request.isEQ(request) && !Request.isNE(request)) {
1483             return false;
1484         }
1485 
1486         assert args.size() == 2 : "EQ or NE or TYPEOF need two args";
1487 
1488         Expression lhs = args.get(0);
1489         Expression rhs = args.get(1);
1490 
1491         if (isNullLiteral(lhs)) {
1492             final Expression tmp = lhs;
1493             lhs = rhs;
1494             rhs = tmp;
1495         }
1496 
1497         // this is a null literal check, so if there is implicit coercion
1498         // involved like {D}x=null, we will fail - this is very rare
1499         if (isNullLiteral(rhs) && lhs.getType().isObject()) {
1500             final Label trueLabel  = new Label("trueLabel");
1501             final Label falseLabel = new Label("falseLabel");
1502             final Label endLabel   = new Label("end");
1503 
1504             load(lhs);
1505             method.dup();
1506             if (Request.isEQ(request)) {
1507                 method.ifnull(trueLabel);
1508             } else if (Request.isNE(request)) {
1509                 method.ifnonnull(trueLabel);
1510             } else {
1511                 assert false : "Invalid request " + request;
1512             }
1513 
1514             method.label(falseLabel);
1515             load(rhs);
1516             method.invokestatic(CompilerConstants.className(ScriptRuntime.class), request.toString(), signature);
1517             method._goto(endLabel);
1518 
1519             method.label(trueLabel);
1520             // if NE (not strict) this can be "undefined != null" which is supposed to be false
1521             if (request == Request.NE) {
1522                 method.loadUndefined(Type.OBJECT);
1523                 final Label isUndefined = new Label("isUndefined");
1524                 final Label afterUndefinedCheck = new Label("afterUndefinedCheck");
1525                 method.if_acmpeq(isUndefined);
1526                 // not undefined
1527                 method.load(true);
1528                 method._goto(afterUndefinedCheck);
1529                 method.label(isUndefined);
1530                 method.load(false);
1531                 method.label(afterUndefinedCheck);
1532             } else {
1533                 method.pop();
1534                 method.load(true);
1535             }
1536             method.label(endLabel);
1537             method.convert(runtimeNode.getType());
1538             method.store(runtimeNode.getSymbol());
1539 
1540             return true;
1541         }
1542 
1543         return false;
1544     }
1545 
1546     private boolean specializationCheck(final RuntimeNode.Request request, final Expression node, final List<Expression> args) {
1547         if (!request.canSpecialize()) {
1548             return false;
1549         }
1550 
1551         assert args.size() == 2;
1552         final Type returnType = node.getType();
1553 
1554         load(args.get(0));
1555         load(args.get(1));
1556 
1557         Request finalRequest = request;
1558 
1559         //if the request is a comparison, i.e. one that can be reversed
1560         //it keeps its semantic, but make sure that the object comes in
1561         //last
1562         final Request reverse = Request.reverse(request);
1563         if (method.peekType().isObject() && reverse != null) { //rhs is object
1564             if (!method.peekType(1).isObject()) { //lhs is not object
1565                 method.swap(); //prefer object as lhs
1566                 finalRequest = reverse;
1567             }
1568         }
1569 
1570         method.dynamicRuntimeCall(
1571                 new SpecializedRuntimeNode(
1572                     finalRequest,
1573                     new Type[] {
1574                         method.peekType(1),
1575                         method.peekType()
1576                     },
1577                     returnType).getInitialName(),
1578                 returnType,
1579                 finalRequest);
1580 
1581         method.convert(node.getType());
1582         method.store(node.getSymbol());
1583 
1584         return true;
1585     }
1586 
1587     private static boolean isReducible(final Request request) {
1588         return Request.isComparison(request) || request == Request.ADD;
1589     }
1590 
1591     @Override
1592     public boolean enterRuntimeNode(final RuntimeNode runtimeNode) {
1593         /*
1594          * First check if this should be something other than a runtime node
1595          * AccessSpecializer might have changed the type
1596          *
1597          * TODO - remove this - Access Specializer will always know after Attr/Lower
1598          */
1599         final List<Expression> args = runtimeNode.getArgs();
1600         if (runtimeNode.isPrimitive() && !runtimeNode.isFinal() && isReducible(runtimeNode.getRequest())) {
1601             final Expression lhs = args.get(0);
1602             assert args.size() > 1 : runtimeNode + " must have two args";
1603             final Expression rhs = args.get(1);
1604 
1605             final Type   type   = runtimeNode.getType();
1606             final Symbol symbol = runtimeNode.getSymbol();
1607 
1608             switch (runtimeNode.getRequest()) {
1609             case EQ:
1610             case EQ_STRICT:
1611                 return enterCmp(lhs, rhs, Condition.EQ, type, symbol);
1612             case NE:
1613             case NE_STRICT:
1614                 return enterCmp(lhs, rhs, Condition.NE, type, symbol);
1615             case LE:
1616                 return enterCmp(lhs, rhs, Condition.LE, type, symbol);
1617             case LT:
1618                 return enterCmp(lhs, rhs, Condition.LT, type, symbol);
1619             case GE:
1620                 return enterCmp(lhs, rhs, Condition.GE, type, symbol);
1621             case GT:
1622                 return enterCmp(lhs, rhs, Condition.GT, type, symbol);
1623             case ADD:
1624                 Type widest = Type.widest(lhs.getType(), rhs.getType());
1625                 load(lhs);
1626                 method.convert(widest);
1627                 load(rhs);
1628                 method.convert(widest);
1629                 method.add();
1630                 method.convert(type);
1631                 method.store(symbol);
1632                 return false;
1633             default:
1634                 // it's ok to send this one on with only primitive arguments, maybe INSTANCEOF(true, true) or similar
1635                 // assert false : runtimeNode + " has all primitive arguments. This is an inconsistent state";
1636                 break;
1637             }
1638         }
1639 
1640         if (nullCheck(runtimeNode, args, new FunctionSignature(false, false, runtimeNode.getType(), args).toString())) {
1641             return false;
1642         }
1643 
1644         if (!runtimeNode.isFinal() && specializationCheck(runtimeNode.getRequest(), runtimeNode, args)) {
1645             return false;
1646         }
1647 
1648         for (final Expression arg : args) {
1649             load(arg).convert(Type.OBJECT); //TODO this should not be necessary below Lower
1650         }
1651 
1652         method.invokestatic(
1653             CompilerConstants.className(ScriptRuntime.class),
1654             runtimeNode.getRequest().toString(),
1655             new FunctionSignature(
1656                 false,
1657                 false,
1658                 runtimeNode.getType(),
1659                 args.size()).toString());
1660         method.convert(runtimeNode.getType());
1661         method.store(runtimeNode.getSymbol());
1662 
1663         return false;
1664     }
1665 
1666     @Override
1667     public boolean enterSplitNode(final SplitNode splitNode) {
1668         final CompileUnit splitCompileUnit = splitNode.getCompileUnit();
1669 
1670         final FunctionNode fn   = lc.getCurrentFunction();
1671         final String className  = splitCompileUnit.getUnitClassName();
1672         final String name       = splitNode.getName();
1673 
1674         final Class<?>   rtype          = fn.getReturnType().getTypeClass();
1675         final boolean    needsArguments = fn.needsArguments();
1676         final Class<?>[] ptypes         = needsArguments ?
1677                 new Class<?>[] {ScriptFunction.class, Object.class, ScriptObject.class, Object.class} :
1678                 new Class<?>[] {ScriptFunction.class, Object.class, ScriptObject.class};
1679 
1680         final MethodEmitter caller = method;
1681         unit = lc.pushCompileUnit(splitCompileUnit);
1682 
1683         final Call splitCall = staticCallNoLookup(
1684             className,
1685             name,
1686             methodDescriptor(rtype, ptypes));
1687 
1688         final MethodEmitter splitEmitter =
1689                 splitCompileUnit.getClassEmitter().method(
1690                         splitNode,
1691                         name,
1692                         rtype,
1693                         ptypes);
1694 
1695         method = lc.pushMethodEmitter(splitEmitter);
1696         method.setFunctionNode(fn);
1697 
1698         assert fn.needsCallee() : "split function should require callee";
1699         caller.loadCompilerConstant(CALLEE);
1700         caller.loadCompilerConstant(THIS);
1701         caller.loadCompilerConstant(SCOPE);
1702         if (needsArguments) {
1703             caller.loadCompilerConstant(ARGUMENTS);
1704         }
1705         caller.invoke(splitCall);
1706         caller.storeCompilerConstant(RETURN);
1707 
1708         method.begin();
1709         // Copy scope to its target slot as first thing because the original slot could be used by return symbol.
1710         fixScopeSlot(fn);
1711 
1712         method.loadUndefined(fn.getReturnType());
1713         method.storeCompilerConstant(RETURN);
1714 
1715         return true;
1716     }
1717 
1718     private void fixScopeSlot(final FunctionNode functionNode) {
1719         // TODO hack to move the scope to the expected slot (needed because split methods reuse the same slots as the root method)
1720         if (functionNode.compilerConstant(SCOPE).getSlot() != SCOPE.slot()) {
1721             method.load(Type.typeFor(ScriptObject.class), SCOPE.slot());
1722             method.storeCompilerConstant(SCOPE);
1723         }
1724     }
1725 
1726     @Override
1727     public Node leaveSplitNode(final SplitNode splitNode) {
1728         assert method instanceof SplitMethodEmitter;
1729         final boolean     hasReturn = method.hasReturn();
1730         final List<Label> targets   = method.getExternalTargets();
1731 
1732         try {
1733             // Wrap up this method.
1734 
1735             method.loadCompilerConstant(RETURN);
1736             method._return(lc.getCurrentFunction().getReturnType());
1737             method.end();
1738 
1739             unit   = lc.popCompileUnit(splitNode.getCompileUnit());
1740             method = lc.popMethodEmitter(method);
1741 
1742         } catch (final Throwable t) {
1743             Context.printStackTrace(t);
1744             final VerifyError e = new VerifyError("Code generation bug in \"" + splitNode.getName() + "\": likely stack misaligned: " + t + " " + lc.getCurrentFunction().getSource().getName());
1745             e.initCause(t);
1746             throw e;
1747         }
1748 
1749         // Handle return from split method if there was one.
1750         final MethodEmitter caller = method;
1751         final int     targetCount = targets.size();
1752 
1753         //no external jump targets or return in switch node
1754         if (!hasReturn && targets.isEmpty()) {
1755             return splitNode;
1756         }
1757 
1758         caller.loadCompilerConstant(SCOPE);
1759         caller.checkcast(Scope.class);
1760         caller.invoke(Scope.GET_SPLIT_STATE);
1761 
1762         final Label breakLabel = new Label("no_split_state");
1763         // Split state is -1 for no split state, 0 for return, 1..n+1 for break/continue
1764 
1765         //the common case is that we don't need a switch
1766         if (targetCount == 0) {
1767             assert hasReturn;
1768             caller.ifne(breakLabel);
1769             //has to be zero
1770             caller.label(new Label("split_return"));
1771             caller.loadCompilerConstant(RETURN);
1772             caller._return(lc.getCurrentFunction().getReturnType());
1773             caller.label(breakLabel);
1774         } else {
1775             assert !targets.isEmpty();
1776 
1777             final int     low         = hasReturn ? 0 : 1;
1778             final int     labelCount  = targetCount + 1 - low;
1779             final Label[] labels      = new Label[labelCount];
1780 
1781             for (int i = 0; i < labelCount; i++) {
1782                 labels[i] = new Label(i == 0 ? "split_return" : "split_" + targets.get(i - 1));
1783             }
1784             caller.tableswitch(low, targetCount, breakLabel, labels);
1785             for (int i = low; i <= targetCount; i++) {
1786                 caller.label(labels[i - low]);
1787                 if (i == 0) {
1788                     caller.loadCompilerConstant(RETURN);
1789                     caller._return(lc.getCurrentFunction().getReturnType());
1790                 } else {
1791                     // Clear split state.
1792                     caller.loadCompilerConstant(SCOPE);
1793                     caller.checkcast(Scope.class);
1794                     caller.load(-1);
1795                     caller.invoke(Scope.SET_SPLIT_STATE);
1796                     caller.splitAwareGoto(lc, targets.get(i - 1));
1797                 }
1798             }
1799             caller.label(breakLabel);
1800         }
1801 
1802         // If split has a return and caller is itself a split method it needs to propagate the return.
1803         if (hasReturn) {
1804             caller.setHasReturn();
1805         }
1806 
1807         return splitNode;
1808     }
1809 
1810     @Override
1811     public boolean enterSwitchNode(final SwitchNode switchNode) {
1812         lineNumber(switchNode);
1813 
1814         final Expression     expression  = switchNode.getExpression();
1815         final Symbol         tag         = switchNode.getTag();
1816         final boolean        allInteger  = tag.getSymbolType().isInteger();
1817         final List<CaseNode> cases       = switchNode.getCases();
1818         final CaseNode       defaultCase = switchNode.getDefaultCase();
1819         final Label          breakLabel  = switchNode.getBreakLabel();
1820 
1821         Label defaultLabel = breakLabel;
1822         boolean hasDefault = false;
1823 
1824         if (defaultCase != null) {
1825             defaultLabel = defaultCase.getEntry();
1826             hasDefault = true;
1827         }
1828 
1829         if (cases.isEmpty()) {
1830             method.label(breakLabel);
1831             return false;
1832         }
1833 
1834         if (allInteger) {
1835             // Tree for sorting values.
1836             final TreeMap<Integer, Label> tree = new TreeMap<>();
1837 
1838             // Build up sorted tree.
1839             for (final CaseNode caseNode : cases) {
1840                 final Node test = caseNode.getTest();
1841 
1842                 if (test != null) {
1843                     final Integer value = (Integer)((LiteralNode<?>)test).getValue();
1844                     final Label   entry = caseNode.getEntry();
1845 
1846                     // Take first duplicate.
1847                     if (!(tree.containsKey(value))) {
1848                         tree.put(value, entry);
1849                     }
1850                 }
1851             }
1852 
1853             // Copy values and labels to arrays.
1854             final int       size   = tree.size();
1855             final Integer[] values = tree.keySet().toArray(new Integer[size]);
1856             final Label[]   labels = tree.values().toArray(new Label[size]);
1857 
1858             // Discern low, high and range.
1859             final int lo    = values[0];
1860             final int hi    = values[size - 1];
1861             final int range = hi - lo + 1;
1862 
1863             // Find an unused value for default.
1864             int deflt = Integer.MIN_VALUE;
1865             for (final int value : values) {
1866                 if (deflt == value) {
1867                     deflt++;
1868                 } else if (deflt < value) {
1869                     break;
1870                 }
1871             }
1872 
1873             // Load switch expression.
1874             load(expression);
1875             final Type type = expression.getType();
1876 
1877             // If expression not int see if we can convert, if not use deflt to trigger default.
1878             if (!type.isInteger()) {
1879                 method.load(deflt);
1880                 final Class<?> exprClass = type.getTypeClass();
1881                 method.invoke(staticCallNoLookup(ScriptRuntime.class, "switchTagAsInt", int.class, exprClass.isPrimitive()? exprClass : Object.class, int.class));
1882             }
1883 
1884             // If reasonable size and not too sparse (80%), use table otherwise use lookup.
1885             if (range > 0 && range < 4096 && range < (size * 5 / 4)) {
1886                 final Label[] table = new Label[range];
1887                 Arrays.fill(table, defaultLabel);
1888 
1889                 for (int i = 0; i < size; i++) {
1890                     final int value = values[i];
1891                     table[value - lo] = labels[i];
1892                 }
1893 
1894                 method.tableswitch(lo, hi, defaultLabel, table);
1895             } else {
1896                 final int[] ints = new int[size];
1897                 for (int i = 0; i < size; i++) {
1898                     ints[i] = values[i];
1899                 }
1900 
1901                 method.lookupswitch(defaultLabel, ints, labels);
1902             }
1903         } else {
1904             load(expression);
1905 
1906             if (expression.getType().isInteger()) {
1907                 method.convert(Type.NUMBER).dup();
1908                 method.store(tag);
1909                 method.conditionalJump(Condition.NE, true, defaultLabel);
1910             } else {
1911                 assert tag.getSymbolType().isObject();
1912                 method.convert(Type.OBJECT); //e.g. 1 literal pushed and tag is object
1913                 method.store(tag);
1914             }
1915 
1916             for (final CaseNode caseNode : cases) {
1917                 final Expression test = caseNode.getTest();
1918 
1919                 if (test != null) {
1920                     method.load(tag);
1921                     load(test);
1922                     method.invoke(ScriptRuntime.EQ_STRICT);
1923                     method.ifne(caseNode.getEntry());
1924                 }
1925             }
1926 
1927             method._goto(hasDefault ? defaultLabel : breakLabel);
1928         }
1929 
1930         for (final CaseNode caseNode : cases) {
1931             method.label(caseNode.getEntry());
1932             caseNode.getBody().accept(this);
1933         }
1934 
1935         if (!switchNode.isTerminal()) {
1936             method.label(breakLabel);
1937         }
1938 
1939         return false;
1940     }
1941 
1942     @Override
1943     public boolean enterThrowNode(final ThrowNode throwNode) {
1944         lineNumber(throwNode);
1945 
1946         if (throwNode.isSyntheticRethrow()) {
1947             //do not wrap whatever this is in an ecma exception, just rethrow it
1948             load(throwNode.getExpression());
1949             method.athrow();
1950             return false;
1951         }
1952 
1953         method._new(ECMAException.class).dup();
1954 
1955         final Source source     = lc.getCurrentFunction().getSource();
1956 
1957         final Expression expression = throwNode.getExpression();
1958         final int        position   = throwNode.position();
1959         final int        line       = source.getLine(position);
1960         final int        column     = source.getColumn(position);
1961 
1962         load(expression);
1963         assert expression.getType().isObject();
1964 
1965         method.load(source.getName());
1966         method.load(line);
1967         method.load(column);
1968         method.invoke(ECMAException.THROW_INIT);
1969 
1970         method.athrow();
1971 
1972         return false;
1973     }
1974 
1975     @Override
1976     public boolean enterTryNode(final TryNode tryNode) {
1977         lineNumber(tryNode);
1978 
1979         final Block       body        = tryNode.getBody();
1980         final List<Block> catchBlocks = tryNode.getCatchBlocks();
1981         final Symbol      symbol      = tryNode.getException();
1982         final Label       entry       = new Label("try");
1983         final Label       recovery    = new Label("catch");
1984         final Label       exit        = tryNode.getExit();
1985         final Label       skip        = new Label("skip");
1986 
1987         method.label(entry);
1988 
1989         body.accept(this);
1990 
1991         if (!body.hasTerminalFlags()) {
1992             method._goto(skip);
1993         }
1994 
1995         method.label(exit);
1996 
1997         method._catch(recovery);
1998         method.store(symbol);
1999 
2000         for (int i = 0; i < catchBlocks.size(); i++) {
2001             final Block catchBlock = catchBlocks.get(i);
2002 
2003             //TODO this is very ugly - try not to call enter/leave methods directly
2004             //better to use the implicit lexical context scoping given by the visitor's
2005             //accept method.
2006             lc.push(catchBlock);
2007             enterBlock(catchBlock);
2008 
2009             final CatchNode  catchNode          = (CatchNode)catchBlocks.get(i).getStatements().get(0);
2010             final IdentNode  exception          = catchNode.getException();
2011             final Expression exceptionCondition = catchNode.getExceptionCondition();
2012             final Block      catchBody          = catchNode.getBody();
2013 
2014             new Store<IdentNode>(exception) {
2015                 @Override
2016                 protected void storeNonDiscard() {
2017                     return;
2018                 }
2019 
2020                 @Override
2021                 protected void evaluate() {
2022                     if (catchNode.isSyntheticRethrow()) {
2023                         method.load(symbol);
2024                         return;
2025                     }
2026                     /*
2027                      * If caught object is an instance of ECMAException, then
2028                      * bind obj.thrown to the script catch var. Or else bind the
2029                      * caught object itself to the script catch var.
2030                      */
2031                     final Label notEcmaException = new Label("no_ecma_exception");
2032                     method.load(symbol).dup()._instanceof(ECMAException.class).ifeq(notEcmaException);
2033                     method.checkcast(ECMAException.class); //TODO is this necessary?
2034                     method.getField(ECMAException.THROWN);
2035                     method.label(notEcmaException);
2036                 }
2037             }.store();
2038 
2039             final Label next;
2040 
2041             if (exceptionCondition != null) {
2042                 next = new Label("next");
2043                 load(exceptionCondition).convert(Type.BOOLEAN).ifeq(next);
2044             } else {
2045                 next = null;
2046             }
2047 
2048             catchBody.accept(this);
2049 
2050             if (i + 1 != catchBlocks.size() && !catchBody.hasTerminalFlags()) {
2051                 method._goto(skip);
2052             }
2053 
2054             if (next != null) {
2055                 if (i + 1 == catchBlocks.size()) {
2056                     // no next catch block - rethrow if condition failed
2057                     method._goto(skip);
2058                     method.label(next);
2059                     method.load(symbol).athrow();
2060                 } else {
2061                     method.label(next);
2062                 }
2063             }
2064 
2065             leaveBlock(catchBlock);
2066             lc.pop(catchBlock);
2067         }
2068 
2069         method.label(skip);
2070         method._try(entry, exit, recovery, Throwable.class);
2071 
2072         // Finally body is always inlined elsewhere so it doesn't need to be emitted
2073 
2074         return false;
2075     }
2076 
2077     @Override
2078     public boolean enterVarNode(final VarNode varNode) {
2079 
2080         final Expression init = varNode.getInit();
2081 
2082         if (init == null) {
2083             return false;
2084         }
2085 
2086         lineNumber(varNode);
2087 
2088         final Symbol varSymbol = varNode.getName().getSymbol();
2089         assert varSymbol != null : "variable node " + varNode + " requires a name with a symbol";
2090 
2091         assert method != null;
2092 
2093         final boolean needsScope = varSymbol.isScope();
2094         if (needsScope) {
2095             method.loadCompilerConstant(SCOPE);
2096         }
2097         load(init);
2098 
2099         if (needsScope) {
2100             int flags = CALLSITE_SCOPE | getCallSiteFlags();
2101             final IdentNode identNode = varNode.getName();
2102             final Type type = identNode.getType();
2103             if (isFastScope(varSymbol)) {
2104                 storeFastScopeVar(type, varSymbol, flags);
2105             } else {
2106                 method.dynamicSet(type, identNode.getName(), flags);
2107             }
2108         } else {
2109             method.convert(varNode.getName().getType()); // aw: convert moved here
2110             method.store(varSymbol);
2111         }
2112 
2113         return false;
2114     }
2115 
2116     @Override
2117     public boolean enterWhileNode(final WhileNode whileNode) {
2118         lineNumber(whileNode);
2119 
2120         final Expression test          = whileNode.getTest();
2121         final Block      body          = whileNode.getBody();
2122         final Label      breakLabel    = whileNode.getBreakLabel();
2123         final Label      continueLabel = whileNode.getContinueLabel();
2124         final Label      loopLabel     = new Label("loop");
2125 
2126         if (!whileNode.isDoWhile()) {
2127             method._goto(continueLabel);
2128         }
2129 
2130         method.label(loopLabel);
2131         body.accept(this);
2132         if (!whileNode.isTerminal()) {
2133             method.label(continueLabel);
2134             new BranchOptimizer(this, method).execute(test, loopLabel, true);
2135             method.label(breakLabel);
2136         }
2137 
2138         return false;
2139     }
2140 
2141     private void closeWith() {
2142         if (method.hasScope()) {
2143             method.loadCompilerConstant(SCOPE);
2144             method.invoke(ScriptRuntime.CLOSE_WITH);
2145             method.storeCompilerConstant(SCOPE);
2146         }
2147     }
2148 
2149     @Override
2150     public boolean enterWithNode(final WithNode withNode) {
2151         final Expression expression = withNode.getExpression();
2152         final Node       body       = withNode.getBody();
2153 
2154         // It is possible to have a "pathological" case where the with block does not reference *any* identifiers. It's
2155         // pointless, but legal. In that case, if nothing else in the method forced the assignment of a slot to the
2156         // scope object, its' possible that it won't have a slot assigned. In this case we'll only evaluate expression
2157         // for its side effect and visit the body, and not bother opening and closing a WithObject.
2158         final boolean hasScope = method.hasScope();
2159 
2160         final Label tryLabel;
2161         if (hasScope) {
2162             tryLabel = new Label("with_try");
2163             method.label(tryLabel);
2164             method.loadCompilerConstant(SCOPE);
2165         } else {
2166             tryLabel = null;
2167         }
2168 
2169         load(expression);
2170         assert expression.getType().isObject() : "with expression needs to be object: " + expression;
2171 
2172         if (hasScope) {
2173             // Construct a WithObject if we have a scope
2174             method.invoke(ScriptRuntime.OPEN_WITH);
2175             method.storeCompilerConstant(SCOPE);
2176         } else {
2177             // We just loaded the expression for its side effect and to check
2178             // for null or undefined value.
2179             globalCheckObjectCoercible();
2180         }
2181 
2182 
2183         // Always process body
2184         body.accept(this);
2185 
2186         if (hasScope) {
2187             // Ensure we always close the WithObject
2188             final Label endLabel   = new Label("with_end");
2189             final Label catchLabel = new Label("with_catch");
2190             final Label exitLabel  = new Label("with_exit");
2191 
2192             if (!body.isTerminal()) {
2193                 closeWith();
2194                 method._goto(exitLabel);
2195             }
2196 
2197             method.label(endLabel);
2198 
2199             method._catch(catchLabel);
2200             closeWith();
2201             method.athrow();
2202 
2203             method.label(exitLabel);
2204 
2205             method._try(tryLabel, endLabel, catchLabel);
2206         }
2207         return false;
2208     }
2209 
2210     @Override
2211     public boolean enterADD(final UnaryNode unaryNode) {
2212         load(unaryNode.rhs());
2213         assert unaryNode.rhs().getType().isNumber() : unaryNode.rhs().getType() + " "+ unaryNode.getSymbol();
2214         method.store(unaryNode.getSymbol());
2215 
2216         return false;
2217     }
2218 
2219     @Override
2220     public boolean enterBIT_NOT(final UnaryNode unaryNode) {
2221         load(unaryNode.rhs()).convert(Type.INT).load(-1).xor().store(unaryNode.getSymbol());
2222         return false;
2223     }
2224 
2225     // do this better with convert calls to method. TODO
2226     @Override
2227     public boolean enterCONVERT(final UnaryNode unaryNode) {
2228         final Expression rhs = unaryNode.rhs();
2229         final Type to  = unaryNode.getType();
2230 
2231         if (to.isObject() && rhs instanceof LiteralNode) {
2232             final LiteralNode<?> literalNode = (LiteralNode<?>)rhs;
2233             final Object value = literalNode.getValue();
2234 
2235             if (value instanceof Number) {
2236                 assert !to.isArray() : "type hygiene - cannot convert number to array: (" + to.getTypeClass().getSimpleName() + ')' + value;
2237                 if (value instanceof Integer) {
2238                     method.load((Integer)value);
2239                 } else if (value instanceof Long) {
2240                     method.load((Long)value);
2241                 } else if (value instanceof Double) {
2242                     method.load((Double)value);
2243                 } else {
2244                     assert false;
2245                 }
2246                 method.convert(Type.OBJECT);
2247             } else if (value instanceof Boolean) {
2248                 method.getField(staticField(Boolean.class, value.toString().toUpperCase(Locale.ENGLISH), Boolean.class));
2249             } else {
2250                 load(rhs);
2251                 method.convert(unaryNode.getType());
2252             }
2253         } else {
2254             load(rhs);
2255             method.convert(unaryNode.getType());
2256         }
2257 
2258         method.store(unaryNode.getSymbol());
2259 
2260         return false;
2261     }
2262 
2263     @Override
2264     public boolean enterDECINC(final UnaryNode unaryNode) {
2265         final Expression rhs         = unaryNode.rhs();
2266         final Type       type        = unaryNode.getType();
2267         final TokenType  tokenType   = unaryNode.tokenType();
2268         final boolean    isPostfix   = tokenType == TokenType.DECPOSTFIX || tokenType == TokenType.INCPOSTFIX;
2269         final boolean    isIncrement = tokenType == TokenType.INCPREFIX || tokenType == TokenType.INCPOSTFIX;
2270 
2271         assert !type.isObject();
2272 
2273         new SelfModifyingStore<UnaryNode>(unaryNode, rhs) {
2274 
2275             @Override
2276             protected void evaluate() {
2277                 load(rhs, true);
2278 
2279                 method.convert(type);
2280                 if (!isPostfix) {
2281                     if (type.isInteger()) {
2282                         method.load(isIncrement ? 1 : -1);
2283                     } else if (type.isLong()) {
2284                         method.load(isIncrement ? 1L : -1L);
2285                     } else {
2286                         method.load(isIncrement ? 1.0 : -1.0);
2287                     }
2288                     method.add();
2289                 }
2290             }
2291 
2292             @Override
2293             protected void storeNonDiscard() {
2294                 super.storeNonDiscard();
2295                 if (isPostfix) {
2296                     if (type.isInteger()) {
2297                         method.load(isIncrement ? 1 : -1);
2298                     } else if (type.isLong()) {
2299                         method.load(isIncrement ? 1L : 1L);
2300                     } else {
2301                         method.load(isIncrement ? 1.0 : -1.0);
2302                     }
2303                     method.add();
2304                 }
2305             }
2306         }.store();
2307 
2308         return false;
2309     }
2310 
2311     @Override
2312     public boolean enterDISCARD(final UnaryNode unaryNode) {
2313         final Expression rhs = unaryNode.rhs();
2314 
2315         lc.pushDiscard(rhs);
2316         load(rhs);
2317 
2318         if (lc.getCurrentDiscard() == rhs) {
2319             assert !rhs.isAssignment();
2320             method.pop();
2321             lc.popDiscard();
2322         }
2323 
2324         return false;
2325     }
2326 
2327     @Override
2328     public boolean enterNEW(final UnaryNode unaryNode) {
2329         final CallNode callNode = (CallNode)unaryNode.rhs();
2330         final List<Expression> args   = callNode.getArgs();
2331 
2332         // Load function reference.
2333         load(callNode.getFunction()).convert(Type.OBJECT); // must detect type error
2334 
2335         method.dynamicNew(1 + loadArgs(args), getCallSiteFlags());
2336         method.store(unaryNode.getSymbol());
2337 
2338         return false;
2339     }
2340 
2341     @Override
2342     public boolean enterNOT(final UnaryNode unaryNode) {
2343         final Expression rhs = unaryNode.rhs();
2344 
2345         load(rhs);
2346 
2347         final Label trueLabel  = new Label("true");
2348         final Label afterLabel = new Label("after");
2349 
2350         method.convert(Type.BOOLEAN);
2351         method.ifne(trueLabel);
2352         method.load(true);
2353         method._goto(afterLabel);
2354         method.label(trueLabel);
2355         method.load(false);
2356         method.label(afterLabel);
2357         method.store(unaryNode.getSymbol());
2358 
2359         return false;
2360     }
2361 
2362     @Override
2363     public boolean enterSUB(final UnaryNode unaryNode) {
2364         load(unaryNode.rhs()).neg().store(unaryNode.getSymbol());
2365 
2366         return false;
2367     }
2368 
2369     @Override
2370     public boolean enterVOID(final UnaryNode unaryNode) {
2371         load(unaryNode.rhs()).pop();
2372         method.loadUndefined(Type.OBJECT);
2373 
2374         return false;
2375     }
2376 
2377     private void enterNumericAdd(final Expression lhs, final Expression rhs, final Type type, final Symbol symbol) {
2378         assert lhs.getType().equals(rhs.getType()) && lhs.getType().equals(type) : lhs.getType() + " != " + rhs.getType() + " != " + type + " " + new ASTWriter(lhs) + " " + new ASTWriter(rhs);
2379         load(lhs);
2380         load(rhs);
2381         method.add(); //if the symbol is optimistic, it always needs to be written, not on the stack?
2382         method.store(symbol);
2383     }
2384 
2385     @Override
2386     public boolean enterADD(final BinaryNode binaryNode) {
2387         final Expression lhs = binaryNode.lhs();
2388         final Expression rhs = binaryNode.rhs();
2389 
2390         final Type type = binaryNode.getType();
2391         if (type.isNumeric()) {
2392             enterNumericAdd(lhs, rhs, type, binaryNode.getSymbol());
2393         } else {
2394             load(lhs).convert(Type.OBJECT);
2395             load(rhs).convert(Type.OBJECT);
2396             method.add();
2397             method.store(binaryNode.getSymbol());
2398         }
2399 
2400         return false;
2401     }
2402 
2403     private boolean enterAND_OR(final BinaryNode binaryNode) {
2404         final Expression lhs = binaryNode.lhs();
2405         final Expression rhs = binaryNode.rhs();
2406 
2407         final Label skip = new Label("skip");
2408 
2409         load(lhs).convert(Type.OBJECT).dup().convert(Type.BOOLEAN);
2410 
2411         if (binaryNode.tokenType() == TokenType.AND) {
2412             method.ifeq(skip);
2413         } else {
2414             method.ifne(skip);
2415         }
2416 
2417         method.pop();
2418         load(rhs).convert(Type.OBJECT);
2419         method.label(skip);
2420         method.store(binaryNode.getSymbol());
2421 
2422         return false;
2423     }
2424 
2425     @Override
2426     public boolean enterAND(final BinaryNode binaryNode) {
2427         return enterAND_OR(binaryNode);
2428     }
2429 
2430     @Override
2431     public boolean enterASSIGN(final BinaryNode binaryNode) {
2432         final Expression lhs = binaryNode.lhs();
2433         final Expression rhs = binaryNode.rhs();
2434 
2435         final Type lhsType = lhs.getType();
2436         final Type rhsType = rhs.getType();
2437 
2438         if (!lhsType.isEquivalentTo(rhsType)) {
2439             //this is OK if scoped, only locals are wrong
2440             assert !(lhs instanceof IdentNode) || lhs.getSymbol().isScope() : new ASTWriter(binaryNode);
2441         }
2442 
2443         new Store<BinaryNode>(binaryNode, lhs) {
2444             @Override
2445             protected void evaluate() {
2446                 load(rhs);
2447             }
2448         }.store();
2449 
2450         return false;
2451     }
2452 
2453     /**
2454      * Helper class for assignment ops, e.g. *=, += and so on..
2455      */
2456     private abstract class AssignOp extends SelfModifyingStore<BinaryNode> {
2457 
2458         /** The type of the resulting operation */
2459         private final Type opType;
2460 
2461         /**
2462          * Constructor
2463          *
2464          * @param node the assign op node
2465          */
2466         AssignOp(final BinaryNode node) {
2467             this(node.getType(), node);
2468         }
2469 
2470         /**
2471          * Constructor
2472          *
2473          * @param opType type of the computation - overriding the type of the node
2474          * @param node the assign op node
2475          */
2476         AssignOp(final Type opType, final BinaryNode node) {
2477             super(node, node.lhs());
2478             this.opType = opType;
2479         }
2480 
2481         protected abstract void op();
2482 
2483         @Override
2484         protected void evaluate() {
2485             load(assignNode.lhs(), true).convert(opType);
2486             load(assignNode.rhs()).convert(opType);
2487             op();
2488             method.convert(assignNode.getType());
2489         }
2490     }
2491 
2492     @Override
2493     public boolean enterASSIGN_ADD(final BinaryNode binaryNode) {
2494         assert RuntimeNode.Request.ADD.canSpecialize();
2495         final Type lhsType = binaryNode.lhs().getType();
2496         final Type rhsType = binaryNode.rhs().getType();
2497         final boolean specialize = binaryNode.getType() == Type.OBJECT;
2498 
2499         new AssignOp(binaryNode) {
2500 
2501             @Override
2502             protected void op() {
2503                 if (specialize) {
2504                     method.dynamicRuntimeCall(
2505                             new SpecializedRuntimeNode(
2506                                 Request.ADD,
2507                                 new Type[] {
2508                                     lhsType,
2509                                     rhsType,
2510                                 },
2511                                 Type.OBJECT).getInitialName(),
2512                             Type.OBJECT,
2513                             Request.ADD);
2514                 } else {
2515                     method.add();
2516                 }
2517             }
2518 
2519             @Override
2520             protected void evaluate() {
2521                 super.evaluate();
2522             }
2523         }.store();
2524 
2525         return false;
2526     }
2527 
2528     @Override
2529     public boolean enterASSIGN_BIT_AND(final BinaryNode binaryNode) {
2530         new AssignOp(Type.INT, binaryNode) {
2531             @Override
2532             protected void op() {
2533                 method.and();
2534             }
2535         }.store();
2536 
2537         return false;
2538     }
2539 
2540     @Override
2541     public boolean enterASSIGN_BIT_OR(final BinaryNode binaryNode) {
2542         new AssignOp(Type.INT, binaryNode) {
2543             @Override
2544             protected void op() {
2545                 method.or();
2546             }
2547         }.store();
2548 
2549         return false;
2550     }
2551 
2552     @Override
2553     public boolean enterASSIGN_BIT_XOR(final BinaryNode binaryNode) {
2554         new AssignOp(Type.INT, binaryNode) {
2555             @Override
2556             protected void op() {
2557                 method.xor();
2558             }
2559         }.store();
2560 
2561         return false;
2562     }
2563 
2564     @Override
2565     public boolean enterASSIGN_DIV(final BinaryNode binaryNode) {
2566         new AssignOp(binaryNode) {
2567             @Override
2568             protected void op() {
2569                 method.div();
2570             }
2571         }.store();
2572 
2573         return false;
2574     }
2575 
2576     @Override
2577     public boolean enterASSIGN_MOD(final BinaryNode binaryNode) {
2578         new AssignOp(binaryNode) {
2579             @Override
2580             protected void op() {
2581                 method.rem();
2582             }
2583         }.store();
2584 
2585         return false;
2586     }
2587 
2588     @Override
2589     public boolean enterASSIGN_MUL(final BinaryNode binaryNode) {
2590         new AssignOp(binaryNode) {
2591             @Override
2592             protected void op() {
2593                 method.mul();
2594             }
2595         }.store();
2596 
2597         return false;
2598     }
2599 
2600     @Override
2601     public boolean enterASSIGN_SAR(final BinaryNode binaryNode) {
2602         new AssignOp(Type.INT, binaryNode) {
2603             @Override
2604             protected void op() {
2605                 method.sar();
2606             }
2607         }.store();
2608 
2609         return false;
2610     }
2611 
2612     @Override
2613     public boolean enterASSIGN_SHL(final BinaryNode binaryNode) {
2614         new AssignOp(Type.INT, binaryNode) {
2615             @Override
2616             protected void op() {
2617                 method.shl();
2618             }
2619         }.store();
2620 
2621         return false;
2622     }
2623 
2624     @Override
2625     public boolean enterASSIGN_SHR(final BinaryNode binaryNode) {
2626         new AssignOp(Type.INT, binaryNode) {
2627             @Override
2628             protected void op() {
2629                 method.shr();
2630                 method.convert(Type.LONG).load(JSType.MAX_UINT).and();
2631             }
2632         }.store();
2633 
2634         return false;
2635     }
2636 
2637     @Override
2638     public boolean enterASSIGN_SUB(final BinaryNode binaryNode) {
2639         new AssignOp(binaryNode) {
2640             @Override
2641             protected void op() {
2642                 method.sub();
2643             }
2644         }.store();
2645 
2646         return false;
2647     }
2648 
2649     /**
2650      * Helper class for binary arithmetic ops
2651      */
2652     private abstract class BinaryArith {
2653 
2654         protected abstract void op();
2655 
2656         protected void evaluate(final BinaryNode node) {
2657             load(node.lhs());
2658             load(node.rhs());
2659             op();
2660             method.store(node.getSymbol());
2661         }
2662     }
2663 
2664     @Override
2665     public boolean enterBIT_AND(final BinaryNode binaryNode) {
2666         new BinaryArith() {
2667             @Override
2668             protected void op() {
2669                 method.and();
2670             }
2671         }.evaluate(binaryNode);
2672 
2673         return false;
2674     }
2675 
2676     @Override
2677     public boolean enterBIT_OR(final BinaryNode binaryNode) {
2678         new BinaryArith() {
2679             @Override
2680             protected void op() {
2681                 method.or();
2682             }
2683         }.evaluate(binaryNode);
2684 
2685         return false;
2686     }
2687 
2688     @Override
2689     public boolean enterBIT_XOR(final BinaryNode binaryNode) {
2690         new BinaryArith() {
2691             @Override
2692             protected void op() {
2693                 method.xor();
2694             }
2695         }.evaluate(binaryNode);
2696 
2697         return false;
2698     }
2699 
2700     private boolean enterComma(final BinaryNode binaryNode) {
2701         final Expression lhs = binaryNode.lhs();
2702         final Expression rhs = binaryNode.rhs();
2703 
2704         load(lhs);
2705         load(rhs);
2706         method.store(binaryNode.getSymbol());
2707 
2708         return false;
2709     }
2710 
2711     @Override
2712     public boolean enterCOMMARIGHT(final BinaryNode binaryNode) {
2713         return enterComma(binaryNode);
2714     }
2715 
2716     @Override
2717     public boolean enterCOMMALEFT(final BinaryNode binaryNode) {
2718         return enterComma(binaryNode);
2719     }
2720 
2721     @Override
2722     public boolean enterDIV(final BinaryNode binaryNode) {
2723         new BinaryArith() {
2724             @Override
2725             protected void op() {
2726                 method.div();
2727             }
2728         }.evaluate(binaryNode);
2729 
2730         return false;
2731     }
2732 
2733     private boolean enterCmp(final Expression lhs, final Expression rhs, final Condition cond, final Type type, final Symbol symbol) {
2734         final Type lhsType = lhs.getType();
2735         final Type rhsType = rhs.getType();
2736 
2737         final Type widest = Type.widest(lhsType, rhsType);
2738         assert widest.isNumeric() || widest.isBoolean() : widest;
2739 
2740         load(lhs);
2741         method.convert(widest);
2742         load(rhs);
2743         method.convert(widest);
2744 
2745         final Label trueLabel  = new Label("trueLabel");
2746         final Label afterLabel = new Label("skip");
2747 
2748         method.conditionalJump(cond, trueLabel);
2749 
2750         method.load(Boolean.FALSE);
2751         method._goto(afterLabel);
2752         method.label(trueLabel);
2753         method.load(Boolean.TRUE);
2754         method.label(afterLabel);
2755 
2756         method.convert(type);
2757         method.store(symbol);
2758 
2759         return false;
2760     }
2761 
2762     private boolean enterCmp(final BinaryNode binaryNode, final Condition cond) {
2763         return enterCmp(binaryNode.lhs(), binaryNode.rhs(), cond, binaryNode.getType(), binaryNode.getSymbol());
2764     }
2765 
2766     @Override
2767     public boolean enterEQ(final BinaryNode binaryNode) {
2768         return enterCmp(binaryNode, Condition.EQ);
2769     }
2770 
2771     @Override
2772     public boolean enterEQ_STRICT(final BinaryNode binaryNode) {
2773         return enterCmp(binaryNode, Condition.EQ);
2774     }
2775 
2776     @Override
2777     public boolean enterGE(final BinaryNode binaryNode) {
2778         return enterCmp(binaryNode, Condition.GE);
2779     }
2780 
2781     @Override
2782     public boolean enterGT(final BinaryNode binaryNode) {
2783         return enterCmp(binaryNode, Condition.GT);
2784     }
2785 
2786     @Override
2787     public boolean enterLE(final BinaryNode binaryNode) {
2788         return enterCmp(binaryNode, Condition.LE);
2789     }
2790 
2791     @Override
2792     public boolean enterLT(final BinaryNode binaryNode) {
2793         return enterCmp(binaryNode, Condition.LT);
2794     }
2795 
2796     @Override
2797     public boolean enterMOD(final BinaryNode binaryNode) {
2798         new BinaryArith() {
2799             @Override
2800             protected void op() {
2801                 method.rem();
2802             }
2803         }.evaluate(binaryNode);
2804 
2805         return false;
2806     }
2807 
2808     @Override
2809     public boolean enterMUL(final BinaryNode binaryNode) {
2810         new BinaryArith() {
2811             @Override
2812             protected void op() {
2813                 method.mul();
2814             }
2815         }.evaluate(binaryNode);
2816 
2817         return false;
2818     }
2819 
2820     @Override
2821     public boolean enterNE(final BinaryNode binaryNode) {
2822         return enterCmp(binaryNode, Condition.NE);
2823     }
2824 
2825     @Override
2826     public boolean enterNE_STRICT(final BinaryNode binaryNode) {
2827         return enterCmp(binaryNode, Condition.NE);
2828     }
2829 
2830     @Override
2831     public boolean enterOR(final BinaryNode binaryNode) {
2832         return enterAND_OR(binaryNode);
2833     }
2834 
2835     @Override
2836     public boolean enterSAR(final BinaryNode binaryNode) {
2837         new BinaryArith() {
2838             @Override
2839             protected void op() {
2840                 method.sar();
2841             }
2842         }.evaluate(binaryNode);
2843 
2844         return false;
2845     }
2846 
2847     @Override
2848     public boolean enterSHL(final BinaryNode binaryNode) {
2849         new BinaryArith() {
2850             @Override
2851             protected void op() {
2852                 method.shl();
2853             }
2854         }.evaluate(binaryNode);
2855 
2856         return false;
2857     }
2858 
2859     @Override
2860     public boolean enterSHR(final BinaryNode binaryNode) {
2861         new BinaryArith() {
2862             @Override
2863             protected void op() {
2864                 method.shr();
2865                 method.convert(Type.LONG).load(JSType.MAX_UINT).and();
2866             }
2867         }.evaluate(binaryNode);
2868 
2869         return false;
2870     }
2871 
2872     @Override
2873     public boolean enterSUB(final BinaryNode binaryNode) {
2874         new BinaryArith() {
2875             @Override
2876             protected void op() {
2877                 method.sub();
2878             }
2879         }.evaluate(binaryNode);
2880 
2881         return false;
2882     }
2883 
2884     @Override
2885     public boolean enterTernaryNode(final TernaryNode ternaryNode) {
2886         final Expression test      = ternaryNode.getTest();
2887         final Expression trueExpr  = ternaryNode.getTrueExpression();
2888         final Expression falseExpr = ternaryNode.getFalseExpression();
2889 
2890         final Symbol symbol     = ternaryNode.getSymbol();
2891         final Label  falseLabel = new Label("ternary_false");
2892         final Label  exitLabel  = new Label("ternary_exit");
2893 
2894         Type widest = Type.widest(trueExpr.getType(), falseExpr.getType());
2895         if (trueExpr.getType().isArray() || falseExpr.getType().isArray()) { //loadArray creates a Java array type on the stack, calls global allocate, which creates a native array type
2896             widest = Type.OBJECT;
2897         }
2898 
2899         load(test);
2900         assert test.getType().isBoolean() : "lhs in ternary must be boolean";
2901 
2902         // we still keep the conversion here as the AccessSpecializer can have separated the types, e.g. var y = x ? x=55 : 17
2903         // will left as (Object)x=55 : (Object)17 by Lower. Then the first term can be {I}x=55 of type int, which breaks the
2904         // symmetry for the temporary slot for this TernaryNode. This is evidence that we assign types and explicit conversions
2905         // to early, or Apply the AccessSpecializer too late. We are mostly probably looking for a separate type pass to
2906         // do this property. Then we never need any conversions in CodeGenerator
2907         method.ifeq(falseLabel);
2908         load(trueExpr);
2909         method.convert(widest);
2910         method._goto(exitLabel);
2911         method.label(falseLabel);
2912         load(falseExpr);
2913         method.convert(widest);
2914         method.label(exitLabel);
2915         method.store(symbol);
2916 
2917         return false;
2918     }
2919 
2920     /**
2921      * Generate all shared scope calls generated during codegen.
2922      */
2923     protected void generateScopeCalls() {
2924         for (final SharedScopeCall scopeAccess : lc.getScopeCalls()) {
2925             scopeAccess.generateScopeCall();
2926         }
2927     }
2928 
2929     /**
2930      * Debug code used to print symbols
2931      *
2932      * @param block the block we are in
2933      * @param ident identifier for block or function where applicable
2934      */
2935     @SuppressWarnings("resource")
2936     private void printSymbols(final Block block, final String ident) {
2937         if (!compiler.getEnv()._print_symbols) {
2938             return;
2939         }
2940 
2941         final PrintWriter out = compiler.getEnv().getErr();
2942         out.println("[BLOCK in '" + ident + "']");
2943         if (!block.printSymbols(out)) {
2944             out.println("<no symbols>");
2945         }
2946         out.println();
2947     }
2948 
2949 
2950     /**
2951      * The difference between a store and a self modifying store is that
2952      * the latter may load part of the target on the stack, e.g. the base
2953      * of an AccessNode or the base and index of an IndexNode. These are used
2954      * both as target and as an extra source. Previously it was problematic
2955      * for self modifying stores if the target/lhs didn't belong to one
2956      * of three trivial categories: IdentNode, AcessNodes, IndexNodes. In that
2957      * case it was evaluated and tagged as "resolved", which meant at the second
2958      * time the lhs of this store was read (e.g. in a = a (second) + b for a += b,
2959      * it would be evaluated to a nop in the scope and cause stack underflow
2960      *
2961      * see NASHORN-703
2962      *
2963      * @param <T>
2964      */
2965     private abstract class SelfModifyingStore<T extends Expression> extends Store<T> {
2966         protected SelfModifyingStore(final T assignNode, final Expression target) {
2967             super(assignNode, target);
2968         }
2969 
2970         @Override
2971         protected boolean isSelfModifying() {
2972             return true;
2973         }
2974     }
2975 
2976     /**
2977      * Helper class to generate stores
2978      */
2979     private abstract class Store<T extends Expression> {
2980 
2981         /** An assignment node, e.g. x += y */
2982         protected final T assignNode;
2983 
2984         /** The target node to store to, e.g. x */
2985         private final Expression target;
2986 
2987         /** How deep on the stack do the arguments go if this generates an indy call */
2988         private int depth;
2989 
2990         /** If we have too many arguments, we need temporary storage, this is stored in 'quick' */
2991         private Symbol quick;
2992 
2993         /**
2994          * Constructor
2995          *
2996          * @param assignNode the node representing the whole assignment
2997          * @param target     the target node of the assignment (destination)
2998          */
2999         protected Store(final T assignNode, final Expression target) {
3000             this.assignNode = assignNode;
3001             this.target = target;
3002         }
3003 
3004         /**
3005          * Constructor
3006          *
3007          * @param assignNode the node representing the whole assignment
3008          */
3009         protected Store(final T assignNode) {
3010             this(assignNode, assignNode);
3011         }
3012 
3013         /**
3014          * Is this a self modifying store operation, e.g. *= or ++
3015          * @return true if self modifying store
3016          */
3017         protected boolean isSelfModifying() {
3018             return false;
3019         }
3020 
3021         private void prologue() {
3022             final Symbol targetSymbol = target.getSymbol();
3023             final Symbol scopeSymbol  = lc.getCurrentFunction().compilerConstant(SCOPE);
3024 
3025             /**
3026              * This loads the parts of the target, e.g base and index. they are kept
3027              * on the stack throughout the store and used at the end to execute it
3028              */
3029 
3030             target.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
3031                 @Override
3032                 public boolean enterIdentNode(final IdentNode node) {
3033                     if (targetSymbol.isScope()) {
3034                         method.load(scopeSymbol);
3035                         depth++;
3036                     }
3037                     return false;
3038                 }
3039 
3040                 private void enterBaseNode() {
3041                     assert target instanceof BaseNode : "error - base node " + target + " must be instanceof BaseNode";
3042                     final BaseNode   baseNode = (BaseNode)target;
3043                     final Expression base     = baseNode.getBase();
3044 
3045                     load(base);
3046                     method.convert(Type.OBJECT);
3047                     depth += Type.OBJECT.getSlots();
3048 
3049                     if (isSelfModifying()) {
3050                         method.dup();
3051                     }
3052                 }
3053 
3054                 @Override
3055                 public boolean enterAccessNode(final AccessNode node) {
3056                     enterBaseNode();
3057                     return false;
3058                 }
3059 
3060                 @Override
3061                 public boolean enterIndexNode(final IndexNode node) {
3062                     enterBaseNode();
3063 
3064                     final Expression index = node.getIndex();
3065                     // could be boolean here as well
3066                     load(index);
3067                     if (!index.getType().isNumeric()) {
3068                         method.convert(Type.OBJECT);
3069                     }
3070                     depth += index.getType().getSlots();
3071 
3072                     if (isSelfModifying()) {
3073                         //convert "base base index" to "base index base index"
3074                         method.dup(1);
3075                     }
3076 
3077                     return false;
3078                 }
3079 
3080             });
3081         }
3082 
3083         private Symbol quickSymbol(final Type type) {
3084             return quickSymbol(type, QUICK_PREFIX.symbolName());
3085         }
3086 
3087         /**
3088          * Quick symbol generates an extra local variable, always using the same
3089          * slot, one that is available after the end of the frame.
3090          *
3091          * @param type the type of the symbol
3092          * @param prefix the prefix for the variable name for the symbol
3093          *
3094          * @return the quick symbol
3095          */
3096         private Symbol quickSymbol(final Type type, final String prefix) {
3097             final String name = lc.getCurrentFunction().uniqueName(prefix);
3098             final Symbol symbol = new Symbol(name, IS_TEMP | IS_INTERNAL);
3099 
3100             symbol.setType(type);
3101 
3102             symbol.setSlot(lc.quickSlot(symbol));
3103 
3104             return symbol;
3105         }
3106 
3107         // store the result that "lives on" after the op, e.g. "i" in i++ postfix.
3108         protected void storeNonDiscard() {
3109             if (lc.getCurrentDiscard() == assignNode) {
3110                 assert assignNode.isAssignment();
3111                 lc.popDiscard();
3112                 return;
3113             }
3114 
3115             final Symbol symbol = assignNode.getSymbol();
3116             if (symbol.hasSlot()) {
3117                 method.dup().store(symbol);
3118                 return;
3119             }
3120 
3121             if (method.dup(depth) == null) {
3122                 method.dup();
3123                 this.quick = quickSymbol(method.peekType());
3124                 method.store(quick);
3125             }
3126         }
3127 
3128         private void epilogue() {
3129             /**
3130              * Take the original target args from the stack and use them
3131              * together with the value to be stored to emit the store code
3132              *
3133              * The case that targetSymbol is in scope (!hasSlot) and we actually
3134              * need to do a conversion on non-equivalent types exists, but is
3135              * very rare. See for example test/script/basic/access-specializer.js
3136              */
3137             method.convert(target.getType());
3138 
3139             target.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
3140                 @Override
3141                 protected boolean enterDefault(Node node) {
3142                     throw new AssertionError("Unexpected node " + node + " in store epilogue");
3143                 }
3144 
3145                 @Override
3146                 public boolean enterUnaryNode(final UnaryNode node) {
3147                     if (node.tokenType() == TokenType.CONVERT && node.getSymbol() != null) {
3148                         method.convert(node.rhs().getType());
3149                     }
3150                     return true;
3151                 }
3152 
3153                 @Override
3154                 public boolean enterIdentNode(final IdentNode node) {
3155                     final Symbol symbol = node.getSymbol();
3156                     assert symbol != null;
3157                     if (symbol.isScope()) {
3158                         if (isFastScope(symbol)) {
3159                             storeFastScopeVar(node.getType(), symbol, CALLSITE_SCOPE | getCallSiteFlags());
3160                         } else {
3161                             method.dynamicSet(node.getType(), node.getName(), CALLSITE_SCOPE | getCallSiteFlags());
3162                         }
3163                     } else {
3164                         method.store(symbol);
3165                     }
3166                     return false;
3167 
3168                 }
3169 
3170                 @Override
3171                 public boolean enterAccessNode(final AccessNode node) {
3172                     method.dynamicSet(node.getProperty().getType(), node.getProperty().getName(), getCallSiteFlags());
3173                     return false;
3174                 }
3175 
3176                 @Override
3177                 public boolean enterIndexNode(final IndexNode node) {
3178                     method.dynamicSetIndex(getCallSiteFlags());
3179                     return false;
3180                 }
3181             });
3182 
3183 
3184             // whatever is on the stack now is the final answer
3185         }
3186 
3187         protected abstract void evaluate();
3188 
3189         void store() {
3190             prologue();
3191             evaluate(); // leaves an operation of whatever the operationType was on the stack
3192             storeNonDiscard();
3193             epilogue();
3194             if (quick != null) {
3195                 method.load(quick);
3196             }
3197         }
3198     }
3199 
3200     private void newFunctionObject(final FunctionNode functionNode, final FunctionNode originalFunctionNode) {
3201         assert lc.peek() == functionNode;
3202         // We don't emit a ScriptFunction on stack for:
3203         // 1. the outermost compiled function (as there's no code being generated in its outer context that'd need it
3204         //    as a callee), and
3205         // 2. for functions that are immediately called upon definition and they don't need a callee, e.g. (function(){})().
3206         //    Such immediately-called functions are invoked using INVOKESTATIC (see enterFunctionNode() of the embedded
3207         //    visitor of enterCallNode() for details), and if they don't need a callee, they don't have it on their
3208         //    static method's parameter list.
3209         if (lc.getOutermostFunction() == functionNode ||
3210                 (!functionNode.needsCallee()) && lc.isFunctionDefinedInCurrentCall(originalFunctionNode)) {
3211             return;
3212         }
3213 
3214         // Generate the object class and property map in case this function is ever used as constructor
3215         final String      className          = SCRIPTFUNCTION_IMPL_OBJECT;
3216         final int         fieldCount         = ObjectClassGenerator.getPaddedFieldCount(functionNode.countThisProperties());
3217         final String      allocatorClassName = Compiler.binaryName(ObjectClassGenerator.getClassName(fieldCount));
3218         final PropertyMap allocatorMap       = PropertyMap.newMap(null, 0, fieldCount, 0);
3219 
3220         method._new(className).dup();
3221         loadConstant(new RecompilableScriptFunctionData(functionNode, compiler.getCodeInstaller(), allocatorClassName, allocatorMap));
3222 
3223         if (functionNode.isLazy() || functionNode.needsParentScope()) {
3224             method.loadCompilerConstant(SCOPE);
3225         } else {
3226             method.loadNull();
3227         }
3228         method.invoke(constructorNoLookup(className, RecompilableScriptFunctionData.class, ScriptObject.class));
3229     }
3230 
3231     // calls on Global class.
3232     private MethodEmitter globalInstance() {
3233         return method.invokestatic(GLOBAL_OBJECT, "instance", "()L" + GLOBAL_OBJECT + ';');
3234     }
3235 
3236     private MethodEmitter globalObjectPrototype() {
3237         return method.invokestatic(GLOBAL_OBJECT, "objectPrototype", methodDescriptor(ScriptObject.class));
3238     }
3239 
3240     private MethodEmitter globalAllocateArguments() {
3241         return method.invokestatic(GLOBAL_OBJECT, "allocateArguments", methodDescriptor(ScriptObject.class, Object[].class, Object.class, int.class));
3242     }
3243 
3244     private MethodEmitter globalNewRegExp() {
3245         return method.invokestatic(GLOBAL_OBJECT, "newRegExp", methodDescriptor(Object.class, String.class, String.class));
3246     }
3247 
3248     private MethodEmitter globalRegExpCopy() {
3249         return method.invokestatic(GLOBAL_OBJECT, "regExpCopy", methodDescriptor(Object.class, Object.class));
3250     }
3251 
3252     private MethodEmitter globalAllocateArray(final ArrayType type) {
3253         //make sure the native array is treated as an array type
3254         return method.invokestatic(GLOBAL_OBJECT, "allocate", "(" + type.getDescriptor() + ")Ljdk/nashorn/internal/objects/NativeArray;");
3255     }
3256 
3257     private MethodEmitter globalIsEval() {
3258         return method.invokestatic(GLOBAL_OBJECT, "isEval", methodDescriptor(boolean.class, Object.class));
3259     }
3260 
3261     private MethodEmitter globalCheckObjectCoercible() {
3262         return method.invokestatic(GLOBAL_OBJECT, "checkObjectCoercible", methodDescriptor(void.class, Object.class));
3263     }
3264 
3265     private MethodEmitter globalDirectEval() {
3266         return method.invokestatic(GLOBAL_OBJECT, "directEval",
3267                 methodDescriptor(Object.class, Object.class, Object.class, Object.class, Object.class, Object.class));
3268     }
3269 }