/** * If caseExpression argument is null it indicates a default label. */ private void addSwitchCase(Node switchBlock, Node caseExpression, Node statements) { if (switchBlock.getType() != Token.BLOCK) throw Kit.codeBug(); Jump switchNode = (Jump)switchBlock.getFirstChild(); if (switchNode.getType() != Token.SWITCH) throw Kit.codeBug(); Node gotoTarget = Node.newTarget(); if (caseExpression != null) { Jump caseNode = new Jump(Token.CASE, caseExpression); caseNode.target = gotoTarget; switchNode.addChildToBack(caseNode); } else { switchNode.setDefault(gotoTarget); } switchBlock.addChildToBack(gotoTarget); switchBlock.addChildToBack(statements); }
private void closeSwitch(Node switchBlock) { if (switchBlock.getType() != Token.BLOCK) throw Kit.codeBug(); Jump switchNode = (Jump)switchBlock.getFirstChild(); if (switchNode.getType() != Token.SWITCH) throw Kit.codeBug(); Node switchBreakTarget = Node.newTarget(); // switchNode.target is only used by NodeTransformer // to detect switch end switchNode.target = switchBreakTarget; Node defaultTarget = switchNode.getDefault(); if (defaultTarget == null) { defaultTarget = switchBreakTarget; } switchBlock.addChildAfter(makeJump(Token.GOTO, defaultTarget), switchNode); switchBlock.addChildToBack(switchBreakTarget); }
private void enterLoop(Loop loop) { if (loopSet == null) loopSet = new ArrayList<Loop>(); loopSet.add(loop); if (loopAndSwitchSet == null) loopAndSwitchSet = new ArrayList<Jump>(); loopAndSwitchSet.add(loop); pushScope(loop); if (currentLabel != null) { currentLabel.setStatement(loop); currentLabel.getFirstLabel().setLoop(loop); // This is the only time during parsing that we set a node's parent // before parsing the children. In order for the child node offsets // to be correct, we adjust the loop's reported position back to an // absolute source offset, and restore it when we call exitLoop(). loop.setRelative(-currentLabel.getPosition()); } }
/** * Returns in the then and else blocks must be consistent with each other. * If there is no else block, then the return statement can fall through. * @return logical OR of END_* flags */ private int endCheckIf() { Node th, el; int rv = END_UNREACHED; th = next; el = ((Jump)this).target; rv = th.endCheck(); if (el != null) rv |= el.endCheck(); else rv |= END_DROPS_OFF; return rv; }
private void visitGoto(Jump node, int type, Node child) { Node target = node.target; if (type == Token.IFEQ || type == Token.IFNE) { if (child == null) throw Codegen.badTree(); int targetLabel = getTargetLabel(target); int fallThruLabel = cfw.acquireLabel(); if (type == Token.IFEQ) generateIfJump(child, node, targetLabel, fallThruLabel); else generateIfJump(child, node, fallThruLabel, targetLabel); cfw.markLabel(fallThruLabel); } else { if (type == Token.JSR) { if (isGenerator) { addGotoWithReturn(target); } else { // This assumes that JSR is only ever used for finally inlineFinally(target); } } else { addGoto(target, ByteCode.GOTO); } } }
private void visitGoto(Jump node, int type, Node child) { Node target = node.target; if (type == Token.IFEQ || type == Token.IFNE) { if (child == null) throw Codegen.badTree(); int targetLabel = getTargetLabel(target); int fallThruLabel = cfw.acquireLabel(); if (type == Token.IFEQ) generateIfJump(child, node, targetLabel, fallThruLabel); else generateIfJump(child, node, fallThruLabel, targetLabel); cfw.markLabel(fallThruLabel); } else { if (type == Token.JSR) { if (isGenerator) { addGotoWithReturn(target); } else { addGoto(target, ByteCode.JSR); } } else { addGoto(target, ByteCode.GOTO); } } }
/** * Create loop node. The code generator will later call * createWhile|createDoWhile|createFor|createForIn * to finish loop generation. */ private Scope createLoopNode(Node loopLabel, int lineno) { Scope result = createScopeNode(Token.LOOP, lineno); if (loopLabel != null) { ((Jump)loopLabel).setLoop(result); } return result; }
private Node createIf(Node cond, Node ifTrue, Node ifFalse, int lineno) { int condStatus = isAlwaysDefinedBoolean(cond); if (condStatus == ALWAYS_TRUE_BOOLEAN) { return ifTrue; } else if (condStatus == ALWAYS_FALSE_BOOLEAN) { if (ifFalse != null) { return ifFalse; } // Replace if (false) xxx by empty block return new Node(Token.BLOCK, lineno); } Node result = new Node(Token.BLOCK, lineno); Node ifNotTarget = Node.newTarget(); Jump IFNE = new Jump(Token.IFNE, cond); IFNE.target = ifNotTarget; result.addChildToBack(IFNE); result.addChildrenToBack(ifTrue); if (ifFalse != null) { Node endTarget = Node.newTarget(); result.addChildToBack(makeJump(Token.GOTO, endTarget)); result.addChildToBack(ifNotTarget); result.addChildrenToBack(ifFalse); result.addChildToBack(endTarget); } else { result.addChildToBack(ifNotTarget); } return result; }
private BreakStatement breakStatement() throws IOException { if (currentToken != Token.BREAK) codeBug(); consumeToken(); int lineno = ts.lineno, pos = ts.tokenBeg, end = ts.tokenEnd; Name breakLabel = null; if (peekTokenOrEOL() == Token.NAME) { breakLabel = createNameNode(); end = getNodeEnd(breakLabel); } // matchJumpLabelName only matches if there is one LabeledStatement labels = matchJumpLabelName(); // always use first label as target Jump breakTarget = labels == null ? null : labels.getFirstLabel(); if (breakTarget == null && breakLabel == null) { if (loopAndSwitchSet == null || loopAndSwitchSet.size() == 0) { if (breakLabel == null) { reportError("msg.bad.break", pos, end - pos); } } else { breakTarget = loopAndSwitchSet.get(loopAndSwitchSet.size() - 1); } } BreakStatement pn = new BreakStatement(pos, end - pos); pn.setBreakLabel(breakLabel); // can be null if it's a bad break in error-recovery mode if (breakTarget != null) pn.setBreakTarget(breakTarget); pn.setLineno(lineno); return pn; }
/** * Return statement in the loop body must be consistent. The default * assumption for any kind of a loop is that it will eventually terminate. * The only exception is a loop with a constant true condition. Code that * follows such a loop is examined only if one can statically determine * that there is a break out of the loop. * <pre> * for(<> ; <>; <>) {} * for(<> in <> ) {} * while(<>) { } * do { } while(<>) * </pre> * @return logical OR of END_* flags */ private int endCheckLoop() { Node n; int rv = END_UNREACHED; // To find the loop body, we look at the second to last node of the // loop node, which should be the predicate that the loop should // satisfy. // The target of the predicate is the loop-body for all 4 kinds of // loops. for (n = first; n.next != last; n = n.next) { /* skip */ } if (n.type != Token.IFEQ) return END_DROPS_OFF; // The target's next is the loop body block rv = ((Jump)n).target.next.endCheck(); // check to see if the loop condition is true if (n.first.type == Token.TRUE) rv &= ~END_DROPS_OFF; // look for effect of breaks rv |= getIntProp(CONTROL_BLOCK_PROP, END_UNREACHED); return rv; }
/** * When a break is encountered annotate the statement being broken * out of by setting its CONTROL_BLOCK_PROP property. * @return logical OR of END_* flags */ private int endCheckBreak() { Node n = ((Jump) this).getJumpStatement(); n.putIntProp(CONTROL_BLOCK_PROP, END_DROPS_OFF); return END_UNREACHED; }
/** * Push a new try block onto the exception information stack. * * @param node an exception handling node (node.getType() == * Token.TRY) */ void pushExceptionInfo(Jump node) { Node fBlock = getFinallyAtTarget(node.getFinally()); ExceptionInfo ei = new ExceptionInfo(node, fBlock); exceptionInfo.add(ei); }
ExceptionInfo(Jump node, Node finallyBlock) { this.node = node; this.finallyBlock = finallyBlock; handlerLabels = new int[EXCEPTION_MAX]; exceptionStarts = new int[EXCEPTION_MAX]; currentFinally = null; }
private void visitSwitch(Jump switchNode, Node child) { // See comments in IRFactory.createSwitch() for description // of SWITCH node generateExpression(child, switchNode); // save selector value short selector = getNewWordLocal(); cfw.addAStore(selector); for (Jump caseNode = (Jump)child.getNext(); caseNode != null; caseNode = (Jump)caseNode.getNext()) { if (caseNode.getType() != Token.CASE) throw Codegen.badTree(); Node test = caseNode.getFirstChild(); generateExpression(test, caseNode); cfw.addALoad(selector); addScriptRuntimeInvoke("shallowEq", "(Ljava/lang/Object;" +"Ljava/lang/Object;" +")Z"); addGoto(caseNode.target, ByteCode.IFNE); } releaseWordLocal(selector); }