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