001    /*
002     * Copyright 2010-2015 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.kotlin.codegen.inline;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.kotlin.codegen.StackValue;
022    import org.jetbrains.kotlin.resolve.jvm.AsmTypes;
023    import org.jetbrains.org.objectweb.asm.Label;
024    import org.jetbrains.org.objectweb.asm.MethodVisitor;
025    import org.jetbrains.org.objectweb.asm.Opcodes;
026    import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter;
027    
028    import static org.jetbrains.kotlin.codegen.inline.LocalVarRemapper.RemapStatus.*;
029    
030    public class LocalVarRemapper {
031    
032        private final Parameters params;
033        private final int actualParamsSize;
034    
035        private final StackValue[] remapValues;
036        private final int additionalShift;
037    
038        public LocalVarRemapper(Parameters params, int additionalShift) {
039            this.additionalShift = additionalShift;
040            this.params = params;
041    
042            remapValues = new StackValue [params.getArgsSizeOnStack()];
043    
044            int realSize = 0;
045            for (ParameterInfo info : params) {
046                Integer shift = params.getDeclarationSlot(info);
047                if (!info.isSkippedOrRemapped()) {
048                    remapValues[shift] = StackValue.local(realSize, AsmTypes.OBJECT_TYPE);
049                    realSize += info.getType().getSize();
050                }
051                else {
052                    remapValues[shift] = info.isRemapped() ? info.getRemapValue() : null;
053                }
054            }
055    
056            actualParamsSize = realSize;
057        }
058    
059        public RemapInfo doRemap(int index) {
060            int remappedIndex;
061    
062            if (index < params.getArgsSizeOnStack()) {
063                ParameterInfo info = params.getParameterByDeclarationSlot(index);
064                StackValue remapped = remapValues[index];
065                if (info.isSkipped || remapped == null) {
066                    return new RemapInfo(info);
067                }
068                if (info.isRemapped()) {
069                    return new RemapInfo(remapped, info, REMAPPED);
070                } else {
071                    remappedIndex = ((StackValue.Local)remapped).index;
072                }
073            } else {
074                remappedIndex = actualParamsSize - params.getArgsSizeOnStack() + index; //captured params not used directly in this inlined method, they used in closure
075            }
076    
077            return new RemapInfo(StackValue.local(remappedIndex + additionalShift, AsmTypes.OBJECT_TYPE), null, SHIFT);
078        }
079    
080        public RemapInfo remap(int index) {
081            RemapInfo info = doRemap(index);
082            if (FAIL == info.status) {
083                assert info.parameterInfo != null : "Parameter info should be not null";
084                throw new RuntimeException("Trying to access skipped parameter: " + info.parameterInfo.type + " at " +index);
085            }
086            return info;
087        }
088    
089        public void visitIincInsn(int var, int increment, MethodVisitor mv) {
090            RemapInfo remap = remap(var);
091            assert remap.value instanceof StackValue.Local;
092            mv.visitIincInsn(((StackValue.Local) remap.value).index, increment);
093        }
094    
095        public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index, MethodVisitor mv) {
096            RemapInfo info = doRemap(index);
097            //add entries only for shifted vars
098            if (SHIFT == info.status) {
099                int newIndex = ((StackValue.Local) info.value).index;
100                if (newIndex != 0 && "this".equals(name)) {
101                    /*skip additional this for now*/
102                    return;
103                }
104                mv.visitLocalVariable(name, desc, signature, start, end, newIndex);
105            }
106        }
107    
108    
109        public void visitVarInsn(int opcode, int var, InstructionAdapter mv) {
110            RemapInfo remapInfo = remap(var);
111            StackValue value = remapInfo.value;
112            if (value instanceof StackValue.Local) {
113                if (remapInfo.parameterInfo != null) {
114                    //All remapped value parameters can't be rewritten except case of default ones.
115                    //On remapping default parameter to actual value there is only one instruction that writes to it according to mask value
116                    //but if such parameter remapped then it passed and this mask branch code never executed
117                    //TODO add assertion about parameter default value: descriptor is required
118                    opcode = value.type.getOpcode(InlineCodegenUtil.isStoreInstruction(opcode) ? Opcodes.ISTORE : Opcodes.ILOAD);
119                }
120                mv.visitVarInsn(opcode, ((StackValue.Local) value).index);
121                if (remapInfo.parameterInfo != null) {
122                    StackValue.coerce(value.type, remapInfo.parameterInfo.type, mv);
123                }
124            } else {
125                assert remapInfo.parameterInfo != null : "Non local value should have parameter info";
126                value.put(remapInfo.parameterInfo.type, mv);
127            }
128        }
129    
130        public enum RemapStatus {
131            SHIFT,
132            REMAPPED,
133            FAIL
134        }
135    
136        public static class RemapInfo {
137            public final StackValue value;
138            public final ParameterInfo parameterInfo;
139            public final RemapStatus status;
140    
141            public RemapInfo(@NotNull StackValue value, @Nullable ParameterInfo info, RemapStatus remapStatus) {
142                this.value = value;
143                parameterInfo = info;
144                this.status = remapStatus;
145            }
146    
147            public RemapInfo(@NotNull ParameterInfo info) {
148                this.value = null;
149                parameterInfo = info;
150                this.status = FAIL;
151            }
152        }
153    }