001    /*
002     * Copyright 2010-2013 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.jet.codegen.intrinsics;
018    
019    import com.intellij.psi.PsiElement;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.asm4.Type;
022    import org.jetbrains.asm4.commons.InstructionAdapter;
023    import org.jetbrains.jet.codegen.ExpressionCodegen;
024    import org.jetbrains.jet.codegen.StackValue;
025    import org.jetbrains.jet.codegen.state.GenerationState;
026    import org.jetbrains.jet.lang.psi.JetExpression;
027    import org.jetbrains.jet.lang.psi.JetParenthesizedExpression;
028    import org.jetbrains.jet.lang.psi.JetReferenceExpression;
029    
030    import java.util.List;
031    
032    import static org.jetbrains.jet.codegen.AsmUtil.genIncrement;
033    import static org.jetbrains.jet.codegen.AsmUtil.unboxType;
034    
035    public class Increment implements IntrinsicMethod {
036        private final int myDelta;
037    
038        public Increment(int delta) {
039            myDelta = delta;
040        }
041    
042        @Override
043        public StackValue generate(
044                ExpressionCodegen codegen,
045                InstructionAdapter v,
046                @NotNull Type expectedType,
047                PsiElement element,
048                List<JetExpression> arguments,
049                StackValue receiver,
050                @NotNull GenerationState state
051        ) {
052            boolean nullable = expectedType.getSort() == Type.OBJECT;
053            if (nullable) {
054                expectedType = unboxType(expectedType);
055            }
056            if (arguments.size() > 0) {
057                JetExpression operand = arguments.get(0);
058                while (operand instanceof JetParenthesizedExpression) {
059                    operand = ((JetParenthesizedExpression) operand).getExpression();
060                }
061                if (operand instanceof JetReferenceExpression && expectedType == Type.INT_TYPE) {
062                    int index = codegen.indexOfLocal((JetReferenceExpression) operand);
063                    if (index >= 0) {
064                        return StackValue.preIncrement(index, myDelta);
065                    }
066                }
067                StackValue value = codegen.genQualified(receiver, operand);
068                value.dupReceiver(v);
069                value.dupReceiver(v);
070    
071                value.put(expectedType, v);
072                genIncrement(expectedType, myDelta, v);
073                value.store(expectedType, v);
074                value.put(expectedType, v);
075            }
076            else {
077                receiver.put(expectedType, v);
078                genIncrement(expectedType, myDelta, v);
079            }
080            return StackValue.onStack(expectedType);
081        }
082    }