jdk.nashorn.internal.ir.Expression Java Examples
The following examples show how to use
jdk.nashorn.internal.ir.Expression.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Lower.java From nashorn with GNU General Public License v2.0 | 6 votes |
/** * Check whether a call node may be a call to eval. In that case we * clone the args in order to create the following construct in * {@link CodeGenerator} * * <pre> * if (calledFuntion == buildInEval) { * eval(cloned arg); * } else { * cloned arg; * } * </pre> * * @param callNode call node to check if it's an eval */ private CallNode checkEval(final CallNode callNode) { if (callNode.getFunction() instanceof IdentNode) { final List<Expression> args = callNode.getArgs(); final IdentNode callee = (IdentNode)callNode.getFunction(); // 'eval' call with at least one argument if (args.size() >= 1 && EVAL.symbolName().equals(callee.getName())) { final FunctionNode currentFunction = lc.getCurrentFunction(); return callNode.setEvalArgs( new CallNode.EvalArgs( (Expression)ensureUniqueNamesIn(args.get(0)).accept(this), compilerConstant(THIS), evalLocation(callee), currentFunction.isStrict())); } } return callNode; }
Example #2
Source File: OptimisticTypesCalculator.java From jdk8u_nashorn with GNU General Public License v2.0 | 6 votes |
@Override public boolean enterBinaryNode(final BinaryNode binaryNode) { if(binaryNode.isAssignment()) { final Expression lhs = binaryNode.lhs(); if(!binaryNode.isSelfModifying()) { tagNeverOptimistic(lhs); } if(lhs instanceof IdentNode) { final Symbol symbol = ((IdentNode)lhs).getSymbol(); // Assignment to internal symbols is never optimistic, except for self-assignment expressions if(symbol.isInternal() && !binaryNode.rhs().isSelfModifying()) { tagNeverOptimistic(binaryNode.rhs()); } } } else if(binaryNode.isTokenType(TokenType.INSTANCEOF)) { tagNeverOptimistic(binaryNode.lhs()); tagNeverOptimistic(binaryNode.rhs()); } return true; }
Example #3
Source File: Lower.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
/** * Check whether a call node may be a call to eval. In that case we * clone the args in order to create the following construct in * {@link CodeGenerator} * * <pre> * if (calledFuntion == buildInEval) { * eval(cloned arg); * } else { * cloned arg; * } * </pre> * * @param callNode call node to check if it's an eval */ private CallNode checkEval(final CallNode callNode) { if (callNode.getFunction() instanceof IdentNode) { final List<Expression> args = callNode.getArgs(); final IdentNode callee = (IdentNode)callNode.getFunction(); // 'eval' call with at least one argument if (args.size() >= 1 && EVAL.symbolName().equals(callee.getName())) { final List<Expression> evalArgs = new ArrayList<>(args.size()); for(final Expression arg: args) { evalArgs.add((Expression)ensureUniqueNamesIn(arg).accept(this)); } return callNode.setEvalArgs(new CallNode.EvalArgs(evalArgs, evalLocation(callee))); } } return callNode; }
Example #4
Source File: BranchOptimizer.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
private void branchOptimizer(final Expression node, final Label label, final boolean state) { if (!(node instanceof TernaryNode)) { if (node instanceof BinaryNode) { branchOptimizer((BinaryNode)node, label, state); return; } if (node instanceof UnaryNode) { branchOptimizer((UnaryNode)node, label, state); return; } } codegen.load(node, Type.BOOLEAN); if (state) { method.ifne(label); } else { method.ifeq(label); } }
Example #5
Source File: Lower.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
@Override public Node leaveExpressionStatement(final ExpressionStatement expressionStatement) { final Expression expr = expressionStatement.getExpression(); ExpressionStatement node = expressionStatement; final FunctionNode currentFunction = lc.getCurrentFunction(); if (currentFunction.isProgram()) { if (!isInternalExpression(expr) && !isEvalResultAssignment(expr)) { node = expressionStatement.setExpression( new BinaryNode( Token.recast( expressionStatement.getToken(), TokenType.ASSIGN), compilerConstant(RETURN), expr)); } } return addStatement(node); }
Example #6
Source File: CodeGenerator.java From nashorn with GNU General Public License v2.0 | 6 votes |
@Override public boolean enterReturnNode(final ReturnNode returnNode) { lineNumber(returnNode); method.registerReturn(); final Type returnType = lc.getCurrentFunction().getReturnType(); final Expression expression = returnNode.getExpression(); if (expression != null) { load(expression); } else { method.loadUndefined(returnType); } method._return(returnType); return false; }
Example #7
Source File: CodeGenerator.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
private boolean enterCmp(final Expression lhs, final Expression rhs, final Condition cond, final Type type, final Symbol symbol) { final Type lhsType = lhs.getType(); final Type rhsType = rhs.getType(); final Type widest = Type.widest(lhsType, rhsType); assert widest.isNumeric() || widest.isBoolean() : widest; loadBinaryOperands(lhs, rhs, widest); final Label trueLabel = new Label("trueLabel"); final Label afterLabel = new Label("skip"); method.conditionalJump(cond, trueLabel); method.load(Boolean.FALSE); method._goto(afterLabel); method.label(trueLabel); method.load(Boolean.TRUE); method.label(afterLabel); method.convert(type); method.store(symbol); return false; }
Example #8
Source File: Parser.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
/** * Arguments : * ( ) * ( ArgumentList ) * * ArgumentList : * AssignmentExpression * ArgumentList , AssignmentExpression * * See 11.2 * * Parse function call arguments. * @return Argument list. */ private ArrayList<Expression> argumentList() { // Prepare to accumulate list of arguments. final ArrayList<Expression> nodeList = new ArrayList<>(); // LPAREN tested in caller. next(); // Track commas. boolean first = true; while (type != RPAREN) { // Comma prior to every argument except the first. if (!first) { expect(COMMARIGHT); } else { first = false; } // Get argument expression. nodeList.add(assignmentExpression(false)); } expect(RPAREN); return nodeList; }
Example #9
Source File: RangeAnalyzer.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
private Symbol setRange(final Expression dest, final Range range) { if (range.isUnknown()) { return null; } final Symbol symbol = dest.getSymbol(); assert symbol != null : dest + " " + dest.getClass() + " has no symbol"; assert symbol.getRange() != null : symbol + " has no range"; final Range symRange = RANGE.join(symbol.getRange(), range); //anything assigned in the loop, not being the safe loop counter(s) invalidates its entire range if (lc.inLoop() && !isLoopCounter(lc.getCurrentLoop(), symbol)) { symbol.setRange(Range.createGenericRange()); return symbol; } if (!symRange.equals(symbol.getRange())) { LOG.fine("Modify range for " + dest + " " + symbol + " from " + symbol.getRange() + " to " + symRange + " (in node = " + dest + ")" ); symbol.setRange(symRange); } return null; }
Example #10
Source File: Lower.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
@Override public Node leaveForNode(final ForNode forNode) { ForNode newForNode = forNode; final Expression test = forNode.getTest(); if (!forNode.isForIn() && isAlwaysTrue(test)) { newForNode = forNode.setTest(lc, null); } newForNode = checkEscape(newForNode); if(newForNode.isForIn()) { // Wrap it in a block so its internally created iterator is restricted in scope addStatementEnclosedInBlock(newForNode); } else { addStatement(newForNode); } return newForNode; }
Example #11
Source File: Parser.java From hottub with GNU General Public License v2.0 | 6 votes |
/** * Arguments : * ( ) * ( ArgumentList ) * * ArgumentList : * AssignmentExpression * ArgumentList , AssignmentExpression * * See 11.2 * * Parse function call arguments. * @return Argument list. */ private ArrayList<Expression> argumentList() { // Prepare to accumulate list of arguments. final ArrayList<Expression> nodeList = new ArrayList<>(); // LPAREN tested in caller. next(); // Track commas. boolean first = true; while (type != RPAREN) { // Comma prior to every argument except the first. if (!first) { expect(COMMARIGHT); } else { first = false; } // Get argument expression. nodeList.add(assignmentExpression(false)); } expect(RPAREN); return nodeList; }
Example #12
Source File: CodeGenerator.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
private MethodEmitter loadBinaryOperands(final Expression lhs, final Expression rhs, final Type type, final boolean baseAlreadyOnStack) { // ECMAScript 5.1 specification (sections 11.5-11.11 and 11.13) prescribes that when evaluating a binary // expression "LEFT op RIGHT", the order of operations must be: LOAD LEFT, LOAD RIGHT, CONVERT LEFT, CONVERT // RIGHT, EXECUTE OP. Unfortunately, doing it in this order defeats potential optimizations that arise when we // can combine a LOAD with a CONVERT operation (e.g. use a dynamic getter with the conversion target type as its // return value). What we do here is reorder LOAD RIGHT and CONVERT LEFT when possible; it is possible only when // we can prove that executing CONVERT LEFT can't have a side effect that changes the value of LOAD RIGHT. // Basically, if we know that either LEFT already is a primitive value, or does not have to be converted to // a primitive value, or RIGHT is an expression that loads without side effects, then we can do the // reordering and collapse LOAD/CONVERT into a single operation; otherwise we need to do the more costly // separate operations to preserve specification semantics. if (noToPrimitiveConversion(lhs.getType(), type) || rhs.isLocal()) { // Can reorder. Combine load and convert into single operations. load(lhs, type, baseAlreadyOnStack); load(rhs, type, false); } else { // Can't reorder. Load and convert separately. load(lhs, lhs.getType(), baseAlreadyOnStack); load(rhs, rhs.getType(), false); method.swap().convert(type).swap().convert(type); } return method; }
Example #13
Source File: Parser.java From hottub with GNU General Public License v2.0 | 6 votes |
/** * IfStatement : * if ( Expression ) Statement else Statement * if ( Expression ) Statement * * See 12.5 * * Parse an IF statement. */ private void ifStatement() { // Capture IF token. final int ifLine = line; final long ifToken = token; // IF tested in caller. next(); expect(LPAREN); final Expression test = expression(); expect(RPAREN); final Block pass = getStatement(); Block fail = null; if (type == ELSE) { next(); fail = getStatement(); } appendStatement(new IfNode(ifLine, ifToken, fail != null ? fail.getFinish() : pass.getFinish(), test, pass, fail)); }
Example #14
Source File: Parser.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
/** * Arguments : * ( ) * ( ArgumentList ) * * ArgumentList : * AssignmentExpression * ArgumentList , AssignmentExpression * * See 11.2 * * Parse function call arguments. * @return Argument list. */ private ArrayList<Expression> argumentList() { // Prepare to accumulate list of arguments. final ArrayList<Expression> nodeList = new ArrayList<>(); // LPAREN tested in caller. next(); // Track commas. boolean first = true; while (type != RPAREN) { // Comma prior to every argument except the first. if (!first) { expect(COMMARIGHT); } else { first = false; } // Get argument expression. nodeList.add(assignmentExpression(false)); } expect(RPAREN); return nodeList; }
Example #15
Source File: Parser.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
/** * IfStatement : * if ( Expression ) Statement else Statement * if ( Expression ) Statement * * See 12.5 * * Parse an IF statement. */ private void ifStatement() { // Capture IF token. final int ifLine = line; final long ifToken = token; // IF tested in caller. next(); expect(LPAREN); final Expression test = expression(); expect(RPAREN); final Block pass = getStatement(); Block fail = null; if (type == ELSE) { next(); fail = getStatement(); } appendStatement(new IfNode(ifLine, ifToken, fail != null ? fail.getFinish() : pass.getFinish(), test, pass, fail)); }
Example #16
Source File: Parser.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
private RuntimeNode referenceError(final Expression lhs, final Expression rhs, final boolean earlyError) { if (earlyError) { throw error(JSErrorType.REFERENCE_ERROR, AbstractParser.message("invalid.lvalue"), lhs.getToken()); } final ArrayList<Expression> args = new ArrayList<>(); args.add(lhs); if (rhs == null) { args.add(LiteralNode.newInstance(lhs.getToken(), lhs.getFinish())); } else { args.add(rhs); } args.add(LiteralNode.newInstance(lhs.getToken(), lhs.getFinish(), lhs.toString())); return new RuntimeNode(lhs.getToken(), lhs.getFinish(), RuntimeNode.Request.REFERENCE_ERROR, args); }
Example #17
Source File: LocalVariableTypesCalculator.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Override public boolean enterUnaryNode(final UnaryNode unaryNode) { final Expression expr = unaryNode.getExpression(); final LvarType unaryType = toLvarType(unaryNode.setExpression(visitExpression(expr).typeExpression).getType()); if(unaryNode.isSelfModifying() && expr instanceof IdentNode) { onSelfAssignment((IdentNode)expr, unaryType); } typeStack.push(unaryType); return false; }
Example #18
Source File: BranchOptimizer.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private void optimizeLogicalOperand(final Expression expr, final Label label, final boolean state, final boolean isRhs) { final JoinPredecessorExpression jpexpr = (JoinPredecessorExpression)expr; if(LocalVariableConversion.hasLiveConversion(jpexpr)) { final Label after = new Label("after"); branchOptimizer(jpexpr.getExpression(), after, !state); method.beforeJoinPoint(jpexpr); method._goto(label); method.label(after); if(isRhs) { method.beforeJoinPoint(jpexpr); } } else { branchOptimizer(jpexpr.getExpression(), label, state); } }
Example #19
Source File: AssignSymbols.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private Node leaveASSIGN(final BinaryNode binaryNode) { // If we're assigning a property of the this object ("this.foo = ..."), record it. final Expression lhs = binaryNode.lhs(); if (lhs instanceof AccessNode) { final AccessNode accessNode = (AccessNode) lhs; final Expression base = accessNode.getBase(); if (base instanceof IdentNode) { final Symbol symbol = ((IdentNode)base).getSymbol(); if(symbol.isThis()) { thisProperties.peek().add(accessNode.getProperty()); } } } return binaryNode; }
Example #20
Source File: OptimisticTypesCalculator.java From jdk8u_nashorn with GNU General Public License v2.0 | 5 votes |
private Expression leaveOptimistic(final Optimistic opt) { final int pp = opt.getProgramPoint(); if(isValid(pp) && !neverOptimistic.peek().get(pp)) { return (Expression)opt.setType(compiler.getOptimisticType(opt)); } return (Expression)opt; }
Example #21
Source File: OptimisticTypesCalculator.java From jdk8u_nashorn with GNU General Public License v2.0 | 5 votes |
@Override public boolean enterExpressionStatement(final ExpressionStatement expressionStatement) { final Expression expr = expressionStatement.getExpression(); if(!expr.isSelfModifying()) { tagNeverOptimistic(expr); } return true; }
Example #22
Source File: Parser.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
/** * DebuggerStatement : * debugger ; * * See 12.15 * * Parse debugger statement. */ private void debuggerStatement() { // Capture DEBUGGER token. final int debuggerLine = line; final long debuggerToken = token; // DEBUGGER tested in caller. next(); endOfLine(); appendStatement(new ExpressionStatement(debuggerLine, debuggerToken, finish, new RuntimeNode(debuggerToken, finish, RuntimeNode.Request.DEBUGGER, new ArrayList<Expression>()))); }
Example #23
Source File: Parser.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
/** * ReturnStatement : * return Expression? ; // [no LineTerminator here] * * See 12.9 * * Parse RETURN statement. */ private void returnStatement() { // check for return outside function if (lc.getCurrentFunction().getKind() == FunctionNode.Kind.SCRIPT) { throw error(AbstractParser.message("invalid.return")); } // Capture RETURN token. final int returnLine = line; final long returnToken = token; // RETURN tested in caller. nextOrEOL(); Expression expression = null; // SEMICOLON or expression. switch (type) { case RBRACE: case SEMICOLON: case EOL: case EOF: break; default: expression = expression(); break; } endOfLine(); // Construct and add RETURN node. appendStatement(new ReturnNode(returnLine, returnToken, finish, expression)); }
Example #24
Source File: Parser.java From jdk8u_nashorn with GNU General Public License v2.0 | 5 votes |
/** * NewExpression : * MemberExpression * new NewExpression * * See 11.2 * * Parse new expression. * @return Expression node. */ private Expression newExpression() { final long newToken = token; // NEW is tested in caller. next(); // Get function base. final int callLine = line; final Expression constructor = memberExpression(); if (constructor == null) { return null; } // Get arguments. ArrayList<Expression> arguments; // Allow for missing arguments. if (type == LPAREN) { arguments = argumentList(); } else { arguments = new ArrayList<>(); } // Nashorn extension: This is to support the following interface implementation // syntax: // // var r = new java.lang.Runnable() { // run: function() { println("run"); } // }; // // The object literal following the "new Constructor()" expression // is passed as an additional (last) argument to the constructor. if (!env._no_syntax_extensions && type == LBRACE) { arguments.add(objectLiteral()); } final CallNode callNode = new CallNode(callLine, constructor.getToken(), finish, constructor, optimizeList(arguments), true); return new UnaryNode(newToken, callNode); }
Example #25
Source File: BranchOptimizer.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
private void optimizeLogicalOperand(final Expression expr, final Label label, final boolean state, final boolean isRhs) { final JoinPredecessorExpression jpexpr = (JoinPredecessorExpression)expr; if(LocalVariableConversion.hasLiveConversion(jpexpr)) { final Label after = new Label("after"); branchOptimizer(jpexpr.getExpression(), after, !state); method.beforeJoinPoint(jpexpr); method._goto(label); method.label(after); if(isRhs) { method.beforeJoinPoint(jpexpr); } } else { branchOptimizer(jpexpr.getExpression(), label, state); } }
Example #26
Source File: BranchOptimizer.java From hottub with GNU General Public License v2.0 | 5 votes |
private void loadTestAndJump(final Expression node, final Label label, final boolean state) { codegen.loadExpressionAsBoolean(node); if (state) { method.ifne(label); } else { method.ifeq(label); } }
Example #27
Source File: FinalizeTypes.java From nashorn with GNU General Public License v2.0 | 5 votes |
@Override public Node leaveReturnNode(final ReturnNode returnNode) { final Expression expr = returnNode.getExpression(); if (expr != null) { return returnNode.setExpression(convert(expr, lc.getCurrentFunction().getReturnType())); } return returnNode; }
Example #28
Source File: RangeAnalyzer.java From nashorn with GNU General Public License v2.0 | 5 votes |
/** * Check for a loop counter. This is currently quite conservative, in that it only handles * x <= counter and x < counter. * * @param node loop node to check * @return */ private static Symbol findLoopCounter(final LoopNode node) { final Expression test = node.getTest(); if (test != null && test.isComparison()) { final BinaryNode binaryNode = (BinaryNode)test; final Expression lhs = binaryNode.lhs(); final Expression rhs = binaryNode.rhs(); //detect ident cmp int_literal if (lhs instanceof IdentNode && rhs instanceof LiteralNode && ((LiteralNode<?>)rhs).getType().isInteger()) { final Symbol symbol = lhs.getSymbol(); final int margin = ((LiteralNode<?>)rhs).getInt32(); final TokenType op = test.tokenType(); switch (op) { case LT: case LE: symbol.setRange(RANGE.join(symbol.getRange(), Range.createRange(op == TokenType.LT ? margin - 1 : margin))); return symbol; case GT: case GE: //setRange(lhs, Range.createRange(op == TokenType.GT ? margin + 1 : margin)); //return symbol; default: break; } } } return null; }
Example #29
Source File: Parser.java From nashorn with GNU General Public License v2.0 | 5 votes |
/** * Verify an assignment expression. * @param op Operation token. * @param lhs Left hand side expression. * @param rhs Right hand side expression. * @return Verified expression. */ private Expression verifyAssignment(final long op, final Expression lhs, final Expression rhs) { final TokenType opType = Token.descType(op); switch (opType) { case ASSIGN: case ASSIGN_ADD: case ASSIGN_BIT_AND: case ASSIGN_BIT_OR: case ASSIGN_BIT_XOR: case ASSIGN_DIV: case ASSIGN_MOD: case ASSIGN_MUL: case ASSIGN_SAR: case ASSIGN_SHL: case ASSIGN_SHR: case ASSIGN_SUB: if (!(lhs instanceof AccessNode || lhs instanceof IndexNode || lhs instanceof IdentNode)) { return referenceError(lhs, rhs, env._early_lvalue_error); } if (lhs instanceof IdentNode) { if (!checkIdentLValue((IdentNode)lhs)) { return referenceError(lhs, rhs, false); } verifyStrictIdent((IdentNode)lhs, "assignment"); } break; default: break; } // Build up node. return new BinaryNode(op, lhs, rhs); }
Example #30
Source File: Lower.java From jdk8u_nashorn with GNU General Public License v2.0 | 5 votes |
/** * Given a function node that is a callee in a CallNode, replace it with * the appropriate marker function. This is used by {@link CodeGenerator} * for fast scope calls * * @param function function called by a CallNode * @return transformed node to marker function or identity if not ident/access/indexnode */ private static Expression markerFunction(final Expression function) { if (function instanceof IdentNode) { return ((IdentNode)function).setIsFunction(); } else if (function instanceof BaseNode) { return ((BaseNode)function).setIsFunction(); } return function; }