All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tangosol.dev.compiler.java.MinusExpression Maven / Gradle / Ivy

There is a newer version: 24.03
Show newest version
/*
 * Copyright (c) 2000, 2020, Oracle and/or its affiliates.
 *
 * Licensed under the Universal Permissive License v 1.0 as shown at
 * http://oss.oracle.com/licenses/upl.
 */


package com.tangosol.dev.compiler.java;


import com.tangosol.dev.assembler.CodeAttribute;
import com.tangosol.dev.assembler.Dconst;
import com.tangosol.dev.assembler.Dneg;
import com.tangosol.dev.assembler.Fconst;
import com.tangosol.dev.assembler.Fneg;
import com.tangosol.dev.assembler.Iconst;
import com.tangosol.dev.assembler.Ineg;
import com.tangosol.dev.assembler.Lconst;
import com.tangosol.dev.assembler.Lneg;
import com.tangosol.dev.compiler.CompilerException;
import com.tangosol.dev.compiler.Context;
import com.tangosol.util.ErrorList;

import java.util.Map;


/**
* The unary minus (-) expression.
*
* @version 1.00, 10/05/98
* @author  Cameron Purdy
*/
public class MinusExpression extends UnaryExpression
    {
    // ----- construction ---------------------------------------------------

    /**
    * Construct a MinusExpression.
    *
    * @param operator  the operator token
    * @param expr      the sub-expression
    */
    public MinusExpression(Token operator, Expression expr)
        {
        super(operator, expr);
        }


    // ----- code generation ------------------------------------------------

    /**
    * Perform semantic checks, parse tree re-organization, name binding,
    * and optimizations.
    *
    * @param ctx        the compiler context
    * @param setUVars   the set of potentially unassigned variables
    * @param setFVars   the set of potentially assigned final variables
    * @param mapThrown  the set of potentially thrown checked exceptions
    * @param errlist    the error list
    *
    * @return the resulting language element (typically this)
    *
    * @exception CompilerException  thrown if an error occurs that should
    *            stop the compilation process
    */
    protected Element precompile(Context ctx, DualSet setUVars, DualSet setFVars, Map mapThrown, ErrorList errlist)
            throws CompilerException
        {
        // get the sub-expression
        Expression expr = getExpression();

        // pre-compile the sub-expression
        expr = (Expression) expr.precompile(ctx, setUVars, setFVars, mapThrown, errlist);

        // sub-expression must be numeric
        if (expr.checkNumeric(errlist))
            {
            // numeric promotion is applied to the sub-expression
            expr = expr.promoteNumeric();

            // store the sub-expression
            setExpression(expr);

            // the result type is the type of the sub-expression
            setType(expr.getType());
            }

        return this;
        }

    /**
    * Perform final optimizations and code generation.
    *
    * @param ctx       the compiler context
    * @param code      the assembler code attribute to compile to
    * @param fReached  true if this language element is reached (JLS 14.19)
    * @param errlist   the error list to log errors to
    *
    * @return true if the element can complete normally (JLS 14.1)
    *
    * @exception CompilerException  thrown if an error occurs that should
    *            stop the compilation process
    */
    protected boolean compile(Context ctx, CodeAttribute code, boolean fReached, ErrorList errlist)
            throws CompilerException
        {
        Expression expr   = getExpression();
        char       chType = expr.getType().getTypeString().charAt(0);

        if (!ctx.isDebug() && expr.isConstant())
            {
            // get the constant value of the sub-expression
            Object oVal = expr.getValue();

            // produce code to load the negated value of the sub-expression
            switch (chType)
                {
                case 'I':
                    {
                    int n = ((Number) oVal).intValue();
                    code.add(new Iconst(-n));
                    }
                    break;

                case 'J':
                    {
                    long l = ((Number) oVal).longValue();
                    code.add(new Lconst(-l));
                    }
                    break;

                case 'F':
                    {
                    float fl = ((Number) oVal).floatValue();
                    code.add(new Fconst(-fl));
                    }
                    break;

                case 'D':
                    {
                    double dfl = ((Number) oVal).doubleValue();
                    code.add(new Dconst(-dfl));
                    }
                    break;

                default:
                    // assert
                    throw new IllegalStateException("Illegal Type=" + chType);
                }
            }
        else
            {
            // compile the sub-expression
            expr.compile(ctx, code, fReached, errlist);

            // negate the result
            switch (chType)
                {
                case 'I':
                    code.add(new Ineg());
                    break;

                case 'J':
                    code.add(new Lneg());
                    break;

                case 'F':
                    code.add(new Fneg());
                    break;

                case 'D':
                    code.add(new Dneg());
                    break;

                default:
                    // assert
                    throw new IllegalStateException("Illegal Type=" + chType);
                }
            }

        // normal completion possible if reachable
        return fReached;
        }


    // ----- Expression methods ---------------------------------------------

    /**
    * Determine if the expression has a constant value.
    *
    * @return true if the expression results in a constant value
    */
    public boolean isConstant()
        {
        return getExpression().isConstant();
        }

    /**
    * Determine the constant value of the expression.
    *
    * @return the constant value of the expression
    */
    public Object getValue()
        {
        Expression expr   = getExpression();
        char       chType = expr.getType().getTypeString().charAt(0);
        Object     oVal   = expr.getValue();

        switch (chType)
            {
            case 'I':
                return Integer.valueOf(-((Number) oVal).   intValue());

            case 'J':
                return makeLong   (-((Number) oVal).  longValue());

            case 'F':
                return new Float  (-((Number) oVal). floatValue());

            case 'D':
                return new Double (-((Number) oVal).doubleValue());

            default:
                // assert
                throw new IllegalStateException("Illegal Type=" + chType);
            }
        }


    // ----- data members ---------------------------------------------------

    /**
    * The class name.
    */
    private static final String CLASS = "MinusExpression";
    }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy