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