SplitIntoFunctions.java revision 1426:751ada854e5a
1/* 2 * Copyright (c) 2014, 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 26package jdk.nashorn.internal.codegen; 27 28import static jdk.nashorn.internal.ir.Node.NO_FINISH; 29import static jdk.nashorn.internal.ir.Node.NO_LINE_NUMBER; 30import static jdk.nashorn.internal.ir.Node.NO_TOKEN; 31 32import java.util.ArrayDeque; 33import java.util.ArrayList; 34import java.util.Arrays; 35import java.util.Collections; 36import java.util.Deque; 37import java.util.List; 38import java.util.Objects; 39import jdk.nashorn.internal.ir.AccessNode; 40import jdk.nashorn.internal.ir.BinaryNode; 41import jdk.nashorn.internal.ir.Block; 42import jdk.nashorn.internal.ir.BlockLexicalContext; 43import jdk.nashorn.internal.ir.BreakNode; 44import jdk.nashorn.internal.ir.CallNode; 45import jdk.nashorn.internal.ir.CaseNode; 46import jdk.nashorn.internal.ir.ContinueNode; 47import jdk.nashorn.internal.ir.Expression; 48import jdk.nashorn.internal.ir.ExpressionStatement; 49import jdk.nashorn.internal.ir.FunctionNode; 50import jdk.nashorn.internal.ir.GetSplitState; 51import jdk.nashorn.internal.ir.IdentNode; 52import jdk.nashorn.internal.ir.IfNode; 53import jdk.nashorn.internal.ir.JumpStatement; 54import jdk.nashorn.internal.ir.JumpToInlinedFinally; 55import jdk.nashorn.internal.ir.LiteralNode; 56import jdk.nashorn.internal.ir.Node; 57import jdk.nashorn.internal.ir.ReturnNode; 58import jdk.nashorn.internal.ir.SetSplitState; 59import jdk.nashorn.internal.ir.SplitNode; 60import jdk.nashorn.internal.ir.SplitReturn; 61import jdk.nashorn.internal.ir.Statement; 62import jdk.nashorn.internal.ir.SwitchNode; 63import jdk.nashorn.internal.ir.VarNode; 64import jdk.nashorn.internal.ir.visitor.NodeVisitor; 65import jdk.nashorn.internal.parser.Token; 66import jdk.nashorn.internal.parser.TokenType; 67 68/** 69 * A node visitor that replaces {@link SplitNode}s with anonymous function invocations and some additional constructs 70 * to support control flow across splits. By using this transformation, split functions are translated into ordinary 71 * JavaScript functions with nested anonymous functions. The transformations however introduce several AST nodes that 72 * have no JavaScript source representations ({@link GetSplitState}, {@link SetSplitState}, and {@link SplitReturn}), 73 * and therefore such function is no longer reparseable from its source. For that reason, split functions and their 74 * fragments are serialized in-memory and deserialized when they need to be recompiled either for deoptimization or 75 * for type specialization. 76 * NOTE: all {@code leave*()} methods for statements are returning their input nodes. That way, they will not mutate 77 * the original statement list in the block containing the statement, which is fine, as it'll be replaced by the 78 * lexical context when the block is left. If we returned something else (e.g. null), we'd cause a mutation in the 79 * enclosing block's statement list that is otherwise overwritten later anyway. 80 */ 81final class SplitIntoFunctions extends NodeVisitor<BlockLexicalContext> { 82 private static final int FALLTHROUGH_STATE = -1; 83 private static final int RETURN_STATE = 0; 84 private static final int BREAK_STATE = 1; 85 private static final int FIRST_JUMP_STATE = 2; 86 87 private static final String THIS_NAME = CompilerConstants.THIS.symbolName(); 88 private static final String RETURN_NAME = CompilerConstants.RETURN.symbolName(); 89 // Used as the name of the formal parameter for passing the current value of :return symbol into a split fragment. 90 private static final String RETURN_PARAM_NAME = RETURN_NAME + "-in"; 91 92 private final Deque<FunctionState> functionStates = new ArrayDeque<>(); 93 private final Deque<SplitState> splitStates = new ArrayDeque<>(); 94 private final Namespace namespace; 95 96 private boolean artificialBlock = false; 97 98 // -1 is program; we need to use negative ones 99 private int nextFunctionId = -2; 100 101 public SplitIntoFunctions(final Compiler compiler) { 102 super(new BlockLexicalContext() { 103 @Override 104 protected Block afterSetStatements(final Block block) { 105 for(Statement stmt: block.getStatements()) { 106 assert !(stmt instanceof SplitNode); 107 } 108 return block; 109 } 110 }); 111 namespace = new Namespace(compiler.getScriptEnvironment().getNamespace()); 112 } 113 114 @Override 115 public boolean enterFunctionNode(final FunctionNode functionNode) { 116 functionStates.push(new FunctionState(functionNode)); 117 return true; 118 } 119 120 @Override 121 public Node leaveFunctionNode(final FunctionNode functionNode) { 122 functionStates.pop(); 123 return functionNode; 124 } 125 126 @Override 127 protected Node leaveDefault(final Node node) { 128 if (node instanceof Statement) { 129 appendStatement((Statement)node); 130 } 131 return node; 132 } 133 134 @Override 135 public boolean enterSplitNode(final SplitNode splitNode) { 136 getCurrentFunctionState().splitDepth++; 137 splitStates.push(new SplitState(splitNode)); 138 return true; 139 } 140 141 @Override 142 public Node leaveSplitNode(final SplitNode splitNode) { 143 // Replace the split node with an anonymous function expression call. 144 145 final FunctionState fnState = getCurrentFunctionState(); 146 147 final String name = splitNode.getName(); 148 Block body = splitNode.getBody(); 149 final int firstLineNumber = body.getFirstStatementLineNumber(); 150 final long token = body.getToken(); 151 final int finish = body.getFinish(); 152 153 final FunctionNode originalFn = fnState.fn; 154 assert originalFn == lc.getCurrentFunction(); 155 final boolean isProgram = originalFn.isProgram(); 156 157 // Change SplitNode({...}) into "function () { ... }", or "function (:return-in) () { ... }" (for program) 158 final long newFnToken = Token.toDesc(TokenType.FUNCTION, nextFunctionId--, 0); 159 final FunctionNode fn = new FunctionNode( 160 originalFn.getSource(), 161 body.getFirstStatementLineNumber(), 162 newFnToken, 163 finish, 164 newFnToken, 165 NO_TOKEN, 166 namespace, 167 createIdent(name), 168 originalFn.getName() + "$" + name, 169 isProgram ? Collections.singletonList(createReturnParamIdent()) : Collections.<IdentNode>emptyList(), 170 FunctionNode.Kind.NORMAL, 171 // We only need IS_SPLIT conservatively, in case it contains any array units so that we force 172 // the :callee's existence, to force :scope to never be in a slot lower than 2. This is actually 173 // quite a horrible hack to do with CodeGenerator.fixScopeSlot not trampling other parameters 174 // and should go away once we no longer have array unit handling in codegen. Note however that 175 // we still use IS_SPLIT as the criteria in CompilationPhase.SERIALIZE_SPLIT_PHASE. 176 FunctionNode.IS_ANONYMOUS | FunctionNode.USES_ANCESTOR_SCOPE | FunctionNode.IS_SPLIT, 177 body, 178 null 179 ) 180 .setCompileUnit(lc, splitNode.getCompileUnit()); 181 182 // Call the function: 183 // either "(function () { ... }).call(this)" 184 // or "(function (:return-in) { ... }).call(this, :return)" 185 // NOTE: Function.call() has optimized linking that basically does a pass-through to the function being invoked. 186 // NOTE: CompilationPhase.PROGRAM_POINT_PHASE happens after this, so these calls are subject to optimistic 187 // assumptions on their return value (when they return a value), as they should be. 188 final IdentNode thisIdent = createIdent(THIS_NAME); 189 final CallNode callNode = new CallNode(firstLineNumber, token, finish, new AccessNode(NO_TOKEN, NO_FINISH, fn, "call"), 190 isProgram ? Arrays.<Expression>asList(thisIdent, createReturnIdent()) 191 : Collections.<Expression>singletonList(thisIdent), 192 false); 193 194 final SplitState splitState = splitStates.pop(); 195 fnState.splitDepth--; 196 197 final Expression callWithReturn; 198 final boolean hasReturn = splitState.hasReturn; 199 if (hasReturn && fnState.splitDepth > 0) { 200 final SplitState parentSplit = splitStates.peek(); 201 if (parentSplit != null) { 202 // Propagate hasReturn to parent split 203 parentSplit.hasReturn = true; 204 } 205 } 206 if (hasReturn || isProgram) { 207 // capture return value: ":return = (function () { ... })();" 208 callWithReturn = new BinaryNode(Token.recast(token, TokenType.ASSIGN), createReturnIdent(), callNode); 209 } else { 210 // no return value, just call : "(function () { ... })();" 211 callWithReturn = callNode; 212 } 213 appendStatement(new ExpressionStatement(firstLineNumber, token, finish, callWithReturn)); 214 215 Statement splitStateHandler; 216 217 final List<JumpStatement> jumpStatements = splitState.jumpStatements; 218 final int jumpCount = jumpStatements.size(); 219 // There are jumps (breaks or continues) that need to be propagated outside the split node. We need to 220 // set up a switch statement for them: 221 // switch(:scope.getScopeState()) { ... } 222 if (jumpCount > 0) { 223 final List<CaseNode> cases = new ArrayList<>(jumpCount + (hasReturn ? 1 : 0)); 224 if (hasReturn) { 225 // If the split node also contained a return, we'll slip it as a case in the switch statement 226 addCase(cases, RETURN_STATE, createReturnFromSplit()); 227 } 228 int i = FIRST_JUMP_STATE; 229 for (final JumpStatement jump: jumpStatements) { 230 addCase(cases, i++, enblockAndVisit(jump)); 231 } 232 splitStateHandler = new SwitchNode(NO_LINE_NUMBER, token, finish, GetSplitState.INSTANCE, cases, null); 233 } else { 234 splitStateHandler = null; 235 } 236 237 // As the switch statement itself is breakable, an unlabelled break can't be in the switch statement, 238 // so we need to test for it separately. 239 if (splitState.hasBreak) { 240 // if(:scope.getScopeState() == Scope.BREAK) { break; } 241 splitStateHandler = makeIfStateEquals(firstLineNumber, token, finish, BREAK_STATE, 242 enblockAndVisit(new BreakNode(NO_LINE_NUMBER, token, finish, null)), splitStateHandler); 243 } 244 245 // Finally, if the split node had a return statement, but there were no external jumps, we didn't have 246 // the switch statement to handle the return, so we need a separate if for it. 247 if (hasReturn && jumpCount == 0) { 248 // if (:scope.getScopeState() == Scope.RETURN) { return :return; } 249 splitStateHandler = makeIfStateEquals(NO_LINE_NUMBER, token, finish, RETURN_STATE, 250 createReturnFromSplit(), splitStateHandler); 251 } 252 253 if (splitStateHandler != null) { 254 appendStatement(splitStateHandler); 255 } 256 257 return splitNode; 258 } 259 260 private static void addCase(final List<CaseNode> cases, final int i, final Block body) { 261 cases.add(new CaseNode(NO_TOKEN, NO_FINISH, intLiteral(i), body)); 262 } 263 264 private static LiteralNode<Number> intLiteral(final int i) { 265 return LiteralNode.newInstance(NO_TOKEN, NO_FINISH, i); 266 } 267 268 private static Block createReturnFromSplit() { 269 return new Block(NO_TOKEN, NO_FINISH, createReturnReturn()); 270 } 271 272 private static ReturnNode createReturnReturn() { 273 return new ReturnNode(NO_LINE_NUMBER, NO_TOKEN, NO_FINISH, createReturnIdent()); 274 } 275 276 private static IdentNode createReturnIdent() { 277 return createIdent(RETURN_NAME); 278 } 279 280 private static IdentNode createReturnParamIdent() { 281 return createIdent(RETURN_PARAM_NAME); 282 } 283 284 private static IdentNode createIdent(final String name) { 285 return new IdentNode(NO_TOKEN, NO_FINISH, name); 286 } 287 288 private Block enblockAndVisit(final JumpStatement jump) { 289 artificialBlock = true; 290 final Block block = (Block)new Block(NO_TOKEN, NO_FINISH, jump).accept(this); 291 artificialBlock = false; 292 return block; 293 } 294 295 private static IfNode makeIfStateEquals(final int lineNumber, final long token, final int finish, 296 final int value, final Block pass, final Statement fail) { 297 return new IfNode(lineNumber, token, finish, 298 new BinaryNode(Token.recast(token, TokenType.EQ_STRICT), 299 GetSplitState.INSTANCE, intLiteral(value)), 300 pass, 301 fail == null ? null : new Block(NO_TOKEN, NO_FINISH, fail)); 302 } 303 304 @Override 305 public boolean enterVarNode(final VarNode varNode) { 306 if (!inSplitNode()) { 307 return super.enterVarNode(varNode); 308 } 309 assert !varNode.isBlockScoped(); //TODO: we must handle these too, but we currently don't 310 311 final Expression init = varNode.getInit(); 312 313 // Move a declaration-only var statement to the top of the outermost function. 314 getCurrentFunctionState().varStatements.add(varNode.setInit(null)); 315 // If it had an initializer, replace it with an assignment expression statement. Note that "var" is a 316 // statement, so it doesn't contribute to :return of the programs, therefore we are _not_ adding a 317 // ":return = ..." assignment around the original assignment. 318 if (init != null) { 319 final long token = Token.recast(varNode.getToken(), TokenType.ASSIGN); 320 new ExpressionStatement(varNode.getLineNumber(), token, varNode.getFinish(), 321 new BinaryNode(token, varNode.getName(), varNode.getInit())).accept(this); 322 } 323 324 return false; 325 } 326 327 @Override 328 public Node leaveBlock(final Block block) { 329 if (!artificialBlock) { 330 if (lc.isFunctionBody()) { 331 // Prepend declaration-only var statements to the top of the statement list. 332 lc.prependStatements(getCurrentFunctionState().varStatements); 333 } else if (lc.isSplitBody()) { 334 appendSplitReturn(FALLTHROUGH_STATE, NO_LINE_NUMBER); 335 if (getCurrentFunctionState().fn.isProgram()) { 336 // If we're splitting the program, make sure every shard ends with "return :return" and 337 // begins with ":return = :return-in;". 338 lc.prependStatement(new ExpressionStatement(NO_LINE_NUMBER, NO_TOKEN, NO_FINISH, 339 new BinaryNode(Token.toDesc(TokenType.ASSIGN, 0, 0), createReturnIdent(), createReturnParamIdent()))); 340 } 341 } 342 } 343 return block; 344 } 345 346 @Override 347 public Node leaveBreakNode(final BreakNode breakNode) { 348 return leaveJumpNode(breakNode); 349 } 350 351 @Override 352 public Node leaveContinueNode(final ContinueNode continueNode) { 353 return leaveJumpNode(continueNode); 354 } 355 356 @Override 357 public Node leaveJumpToInlinedFinally(final JumpToInlinedFinally jumpToInlinedFinally) { 358 return leaveJumpNode(jumpToInlinedFinally); 359 } 360 361 private JumpStatement leaveJumpNode(final JumpStatement jump) { 362 if (inSplitNode()) { 363 final SplitState splitState = getCurrentSplitState(); 364 final SplitNode splitNode = splitState.splitNode; 365 if (lc.isExternalTarget(splitNode, jump.getTarget(lc))) { 366 appendSplitReturn(splitState.getSplitStateIndex(jump), jump.getLineNumber()); 367 return jump; 368 } 369 } 370 appendStatement(jump); 371 return jump; 372 } 373 374 private void appendSplitReturn(final int splitState, final int lineNumber) { 375 appendStatement(new SetSplitState(splitState, lineNumber)); 376 if (getCurrentFunctionState().fn.isProgram()) { 377 // If we're splitting the program, make sure every fragment passes back :return 378 appendStatement(createReturnReturn()); 379 } else { 380 appendStatement(SplitReturn.INSTANCE); 381 } 382 } 383 384 @Override 385 public Node leaveReturnNode(final ReturnNode returnNode) { 386 if(inSplitNode()) { 387 appendStatement(new SetSplitState(RETURN_STATE, returnNode.getLineNumber())); 388 getCurrentSplitState().hasReturn = true; 389 } 390 appendStatement(returnNode); 391 return returnNode; 392 } 393 394 private void appendStatement(final Statement statement) { 395 lc.appendStatement(statement); 396 } 397 398 private boolean inSplitNode() { 399 return getCurrentFunctionState().splitDepth > 0; 400 } 401 402 private FunctionState getCurrentFunctionState() { 403 return functionStates.peek(); 404 } 405 406 private SplitState getCurrentSplitState() { 407 return splitStates.peek(); 408 } 409 410 private static class FunctionState { 411 final FunctionNode fn; 412 final List<Statement> varStatements = new ArrayList<>(); 413 int splitDepth; 414 415 FunctionState(final FunctionNode fn) { 416 this.fn = fn; 417 } 418 } 419 420 private static class SplitState { 421 final SplitNode splitNode; 422 boolean hasReturn; 423 boolean hasBreak; 424 425 final List<JumpStatement> jumpStatements = new ArrayList<>(); 426 427 int getSplitStateIndex(final JumpStatement jump) { 428 if (jump instanceof BreakNode && jump.getLabelName() == null) { 429 // Unlabelled break is a special case 430 hasBreak = true; 431 return BREAK_STATE; 432 } 433 434 int i = 0; 435 for(final JumpStatement exJump: jumpStatements) { 436 if (jump.getClass() == exJump.getClass() && Objects.equals(jump.getLabelName(), exJump.getLabelName())) { 437 return i + FIRST_JUMP_STATE; 438 } 439 ++i; 440 } 441 jumpStatements.add(jump); 442 return i + FIRST_JUMP_STATE; 443 } 444 445 SplitState(final SplitNode splitNode) { 446 this.splitNode = splitNode; 447 } 448 } 449} 450