StackValue.java 33.6 KB
Newer Older
1 2
package org.jetbrains.jet.codegen;

3
import com.intellij.psi.tree.IElementType;
4
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
5 6 7
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
8
import org.jetbrains.jet.lang.psi.JetExpression;
9 10
import org.jetbrains.jet.lang.resolve.calls.ResolvedCall;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
11 12 13
import org.jetbrains.jet.lexer.JetTokens;
import org.objectweb.asm.Label;
import org.objectweb.asm.Opcodes;
14 15
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.InstructionAdapter;
16
import org.objectweb.asm.commons.Method;
17

18 19
import java.util.List;

20 21
/**
 * @author yole
A
Alex Tkachman 已提交
22
 * @author alex.tkachman
23 24
 */
public abstract class StackValue {
25
    public final Type type;
D
Dmitry Jemerov 已提交
26 27 28

    public StackValue(Type type) {
        this.type = type;
29 30 31 32 33 34 35 36 37 38 39 40
    }

    public static void valueOf(InstructionAdapter instructionAdapter, final Type type) {
        if (type.getSort() == Type.OBJECT || type.getSort() == Type.ARRAY) {
            return;
        }
        if (type == Type.VOID_TYPE) {
            instructionAdapter.aconst(null);
        } else {
            Type boxed = JetTypeMapper.getBoxedType(type);
            instructionAdapter.invokestatic(boxed.getInternalName(), "valueOf", "(" + type.getDescriptor() + ")" + boxed.getDescriptor());
        }
D
Dmitry Jemerov 已提交
41 42
    }

43 44
    public abstract void put(Type type, InstructionAdapter v);

45
    public void store(InstructionAdapter v) {
46 47 48
        throw new UnsupportedOperationException("cannot store to value " + this);
    }

49
    public void dupReceiver(InstructionAdapter v) {
50 51
    }

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    public void condJump(Label label, boolean jumpIfFalse, InstructionAdapter v) {
        if (this.type == Type.BOOLEAN_TYPE) {
            put(Type.BOOLEAN_TYPE, v);
            if (jumpIfFalse) {
                v.ifeq(label);
            }
            else {
                v.ifne(label);
            }
        }
        else {
            throw new UnsupportedOperationException("can't generate a cond jump for a non-boolean value");
        }
    }

D
Dmitry Jemerov 已提交
67
    public static StackValue local(int index, Type type) {
68 69 70
        return new Local(index, type);
    }

A
Alex Tkachman 已提交
71 72 73 74
    public static StackValue shared(int index, Type type) {
        return new Shared(index, type);
    }

75
    public static StackValue onStack(Type type) {
76
        return type == Type.VOID_TYPE ? none() : new OnStack(type);
77 78
    }

D
Dmitry Jemerov 已提交
79 80
    public static StackValue constant(Object value, Type type) {
        return new Constant(value, type);
81 82
    }

83
    public static StackValue cmp(IElementType opToken, Type type) {
D
Dmitry Jemerov 已提交
84
        return type.getSort() == Type.OBJECT ? new ObjectCompare(opToken, type) : new NumberCompare(opToken, type);
85 86
    }

D
Dmitry Jemerov 已提交
87 88 89 90
    public static StackValue not(StackValue stackValue) {
        return new Invert(stackValue);
    }

A
Alex Tkachman 已提交
91 92
    public static StackValue arrayElement(Type type, boolean unbox) {
        return new ArrayElement(type, unbox);
93 94
    }

95 96
    public static StackValue collectionElement(Type type, ResolvedCall<FunctionDescriptor> getter, ResolvedCall<FunctionDescriptor> setter, ExpressionCodegen codegen) {
        return new CollectionElement(type, getter, setter, codegen);
D
Dmitry Jemerov 已提交
97 98
    }

99 100 101
    public static StackValue field(Type type, String owner, String name, boolean isStatic) {
        return new Field(type, owner, name, isStatic);
    }
102

M
Maxim Shafirov 已提交
103 104 105 106
    public static StackValue instanceField(Type type, String owner, String name) {
        return new InstanceField(type, owner, name);
    }

A
Alex Tkachman 已提交
107 108
    public static StackValue property(String name, String owner, Type type, boolean isStatic, boolean isInterface, boolean isSuper, Method getter, Method setter) {
        return new Property(name, owner, getter, setter, isStatic, isInterface, isSuper, type);
109 110
    }

111 112 113 114
    public static StackValue expression(Type type, JetExpression expression, ExpressionCodegen generator) {
        return new Expression(type, expression, generator);
    }

115 116 117
    private static void box(final Type type, final Type toType, InstructionAdapter v) {
        // TODO handle toType correctly
        if (type == Type.INT_TYPE || (JetTypeMapper.isIntPrimitive(type) && toType.getInternalName().equals("java/lang/Integer"))) {
D
Dmitry Jemerov 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
            v.invokestatic("java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
        }
        else if (type == Type.BOOLEAN_TYPE) {
            v.invokestatic("java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;");
        }
        else if (type == Type.CHAR_TYPE) {
            v.invokestatic("java/lang/Character", "valueOf", "(C)Ljava/lang/Character;");
        }
        else if (type == Type.SHORT_TYPE) {
            v.invokestatic("java/lang/Short", "valueOf", "(S)Ljava/lang/Short;");
        }
        else if (type == Type.LONG_TYPE) {
            v.invokestatic("java/lang/Long", "valueOf", "(J)Ljava/lang/Long;");
        }
        else if (type == Type.BYTE_TYPE) {
            v.invokestatic("java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;");
        }
        else if (type == Type.FLOAT_TYPE) {
            v.invokestatic("java/lang/Float", "valueOf", "(F)Ljava/lang/Float;");
        }
        else if (type == Type.DOUBLE_TYPE) {
            v.invokestatic("java/lang/Double", "valueOf", "(D)Ljava/lang/Double;");
        }
    }

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    private static void unbox(final Type type, InstructionAdapter v) {
        if (type == Type.INT_TYPE) {
            v.invokevirtual("java/lang/Number", "intValue", "()I");
        }
        else if (type == Type.BOOLEAN_TYPE) {
            v.invokevirtual("java/lang/Boolean", "booleanValue", "()Z");
        }
        else if (type == Type.CHAR_TYPE) {
            v.invokevirtual("java/lang/Character", "charValue", "()C");
        }
        else if (type == Type.SHORT_TYPE) {
            v.invokevirtual("java/lang/Number", "shortValue", "()S");
        }
        else if (type == Type.LONG_TYPE) {
            v.invokevirtual("java/lang/Number", "longValue", "()J");
        }
        else if (type == Type.BYTE_TYPE) {
            v.invokevirtual("java/lang/Number", "byteValue", "()B");
        }
        else if (type == Type.FLOAT_TYPE) {
            v.invokevirtual("java/lang/Number", "floatValue", "()F");
        }
        else if (type == Type.DOUBLE_TYPE) {
            v.invokevirtual("java/lang/Number", "doubleValue", "()D");
        }
    }

M
Maxim Shafirov 已提交
170
    public void upcast(Type type, InstructionAdapter v) {
M
Maxim Shafirov 已提交
171 172 173 174 175
        if (type.equals(this.type)) return;

        if (type.getSort() == Type.OBJECT && this.type.getSort() == Type.OBJECT) {
            v.checkcast(type);
        }
M
Maxim Shafirov 已提交
176 177 178 179 180 181 182 183
        else {
            coerce(type, v);
        }
    }

    protected void coerce(Type type, InstructionAdapter v) {
        if (type.equals(this.type)) return;

A
Alex Tkachman 已提交
184 185 186 187 188 189 190 191
        if (type.getSort() == Type.VOID && this.type.getSort() != Type.VOID) {
            if(this.type.getSize() == 1)
                v.pop();
            else
                v.pop2();
        }
        else if (type.getSort() != Type.VOID && this.type.getSort() == Type.VOID) {
            if(type.getSort() == Type.OBJECT)
192
                v.visitFieldInsn(Opcodes.GETSTATIC, "jet/Tuple0", "INSTANCE", "Ljet/Tuple0;");
A
Alex Tkachman 已提交
193 194 195 196 197 198 199 200 201
            else if(type == Type.LONG_TYPE)
                v.lconst(0);
            else if(type == Type.FLOAT_TYPE)
                v.fconst(0);
            else if(type == Type.DOUBLE_TYPE)
                v.dconst(0);
            else
                v.iconst(0);
        }
A
Alex Tkachman 已提交
202
        else if (type.getSort() == Type.OBJECT && this.type.equals(JetTypeMapper.TYPE_OBJECT) || type.getSort() == Type.ARRAY) {
203
                v.checkcast(type);
M
Maxim Shafirov 已提交
204
        }
M
Maxim Shafirov 已提交
205
        else if (type.getSort() == Type.OBJECT) {
206 207 208 209 210
            if(this.type.getSort() == Type.OBJECT && !type.equals(JetTypeMapper.TYPE_OBJECT)) {
                v.checkcast(type);
            }
            else
                box(this.type, type, v);
D
Dmitry Jemerov 已提交
211
        }
212
        else if (this.type.getSort() == Type.OBJECT && type.getSort() <= Type.DOUBLE) {
M
Maxim Shafirov 已提交
213
            if (this.type.equals(JetTypeMapper.TYPE_OBJECT)) {
214
                if (type.getSort() == Type.BOOLEAN) {
A
Alex Tkachman 已提交
215
                    v.checkcast(JetTypeMapper.JL_BOOLEAN_TYPE);
216
                }
A
Alex Tkachman 已提交
217 218 219
                else if (type.getSort() == Type.CHAR) {
                    v.checkcast(JetTypeMapper.JL_CHAR_TYPE);
                }
220
                else {
A
Alex Tkachman 已提交
221
                    v.checkcast(JetTypeMapper.JL_NUMBER_TYPE);
222
                }
M
Maxim Shafirov 已提交
223
            }
224 225
            unbox(type, v);
        }
M
Maxim Shafirov 已提交
226
        else {
D
Dmitry Jemerov 已提交
227 228 229 230
            v.cast(this.type, type);
        }
    }

D
Dmitry Jemerov 已提交
231 232 233
    protected void putAsBoolean(InstructionAdapter v) {
        Label ifTrue = new Label();
        Label end = new Label();
234
        condJump(ifTrue, false, v);
D
Dmitry Jemerov 已提交
235 236 237 238 239 240 241
        v.iconst(0);
        v.goTo(end);
        v.mark(ifTrue);
        v.iconst(1);
        v.mark(end);
    }

242 243 244
    public static StackValue none() {
        return None.INSTANCE;
    }
A
Alex Tkachman 已提交
245 246 247 248 249

    public static StackValue fieldForSharedVar(Type type, String name, String fieldName) {
        return new FieldForSharedVar(type, name, fieldName);
    }

250 251 252 253
    public static StackValue composed(StackValue prefix, StackValue suffix) {
        return new Composed(prefix, suffix);
    }

254 255 256 257
    public static StackValue thisOrOuter(ExpressionCodegen codegen, ClassDescriptor descriptor) {
        return new ThisOuter(codegen, descriptor);
    }

258 259 260 261 262 263 264 265
    private static class None extends StackValue {
        public static None INSTANCE = new None();
        private None() {
            super(Type.VOID_TYPE);
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
A
Alex Tkachman 已提交
266
            coerce(type, v);
267 268 269
        }
    }

270 271 272
    public static class Local extends StackValue {
        private final int index;

D
Dmitry Jemerov 已提交
273
        public Local(int index, Type type) {
D
Dmitry Jemerov 已提交
274
            super(type);
275 276 277 278 279
            this.index = index;
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
D
Dmitry Jemerov 已提交
280
            v.load(index, this.type);
D
Dmitry Jemerov 已提交
281
            coerce(type, v);
D
Dmitry Jemerov 已提交
282
            // TODO unbox
283
        }
284 285

        @Override
286
        public void store(InstructionAdapter v) {
287
            v.store(index, this.type);
288
        }
289 290 291 292
    }

    public static class OnStack extends StackValue {
        public OnStack(Type type) {
D
Dmitry Jemerov 已提交
293
            super(type);
294 295 296 297
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
298 299 300 301 302 303 304 305 306 307 308
            if (type == Type.VOID_TYPE && this.type != Type.VOID_TYPE) {
                if (this.type.getSize() == 2) {
                    v.pop2();
                }
                else {
                    v.pop();
                }
            }
            else {
                coerce(type, v);
            }
309 310 311 312 313 314
        }
    }

    public static class Constant extends StackValue {
        private final Object value;

D
Dmitry Jemerov 已提交
315 316
        public Constant(Object value, Type type) {
            super(type);
317 318 319 320 321
            this.value = value;
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
A
Alex Tkachman 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334
            if(value instanceof Integer)
                v.iconst((Integer) value);
            else
            if(value instanceof Long)
                v.lconst((Long) value);
            else
            if(value instanceof Float)
                v.fconst((Float) value);
            else
            if(value instanceof Double)
                v.dconst((Double) value);
            else
                v.aconst(value);
D
Dmitry Jemerov 已提交
335
            coerce(type, v);
336
        }
337 338

        @Override
339
        public void condJump(Label label, boolean jumpIfFalse, InstructionAdapter v) {
D
Dmitry Jemerov 已提交
340
            if (value instanceof Boolean) {
A
Alex Tkachman 已提交
341
                boolean boolValue = (Boolean) value;
342 343 344
                if (boolValue ^ jumpIfFalse) {
                    v.goTo(label);
                }
D
Dmitry Jemerov 已提交
345 346 347 348
            }
            else {
                throw new UnsupportedOperationException("don't know how to generate this condjump");
            }
349 350 351
        }
    }

D
Float  
Dmitry Jemerov 已提交
352
    private static class NumberCompare extends StackValue {
D
Dmitry Jemerov 已提交
353
        protected final IElementType opToken;
D
Dmitry Jemerov 已提交
354
        private final Type operandType;
355

D
Dmitry Jemerov 已提交
356 357
        public NumberCompare(IElementType opToken, Type operandType) {
            super(Type.BOOLEAN_TYPE);
358
            this.opToken = opToken;
D
Dmitry Jemerov 已提交
359
            this.operandType = operandType;
360 361 362 363
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
364 365 366
            if (type == Type.VOID_TYPE) {
                return;
            }
367
            if (type != Type.BOOLEAN_TYPE) {
368
                throw new UnsupportedOperationException("don't know how to put a compare as a non-boolean type " + type);
369
            }
D
Dmitry Jemerov 已提交
370
            putAsBoolean(v);
371 372 373
        }

        @Override
374
        public void condJump(Label label, boolean jumpIfFalse, InstructionAdapter v) {
375
            int opcode;
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
            if (opToken == JetTokens.EQEQ) {
                opcode = jumpIfFalse ? Opcodes.IFNE : Opcodes.IFEQ;
            }
            else if (opToken == JetTokens.EXCLEQ) {
                opcode = jumpIfFalse ? Opcodes.IFEQ : Opcodes.IFNE;
            }
            else if (opToken == JetTokens.GT) {
                opcode = jumpIfFalse ? Opcodes.IFLE : Opcodes.IFGT;
            }
            else if (opToken == JetTokens.GTEQ) {
                opcode = jumpIfFalse ? Opcodes.IFLT : Opcodes.IFGE;
            }
            else if (opToken == JetTokens.LT) {
                opcode = jumpIfFalse ? Opcodes.IFGE : Opcodes.IFLT;
            }
            else if (opToken == JetTokens.LTEQ) {
                opcode = jumpIfFalse ? Opcodes.IFGT : Opcodes.IFLE;
            }
394 395 396
            else {
                throw new UnsupportedOperationException("don't know how to generate this condjump");
            }
D
Dmitry Jemerov 已提交
397
            if (operandType == Type.FLOAT_TYPE || operandType == Type.DOUBLE_TYPE) {
D
Float  
Dmitry Jemerov 已提交
398
                if (opToken == JetTokens.GT || opToken == JetTokens.GTEQ) {
D
Dmitry Jemerov 已提交
399
                    v.cmpg(operandType);
D
Float  
Dmitry Jemerov 已提交
400 401
                }
                else {
D
Dmitry Jemerov 已提交
402
                    v.cmpl(operandType);
D
Float  
Dmitry Jemerov 已提交
403 404
                }
            }
D
Dmitry Jemerov 已提交
405
            else if (operandType == Type.LONG_TYPE) {
D
Dmitry Jemerov 已提交
406 407
                v.lcmp();
            }
D
Float  
Dmitry Jemerov 已提交
408 409 410
            else {
                opcode += (Opcodes.IF_ICMPEQ - Opcodes.IFEQ);
            }
411
            v.visitJumpInsn(opcode, label);
412
        }
413
    }
D
Dmitry Jemerov 已提交
414

D
Dmitry Jemerov 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
    private static class ObjectCompare extends NumberCompare {
        public ObjectCompare(IElementType opToken, Type operandType) {
            super(opToken, operandType);
        }

        @Override
        public void condJump(Label label, boolean jumpIfFalse, InstructionAdapter v) {
            int opcode;
            if (opToken == JetTokens.EQEQEQ) {
                opcode = jumpIfFalse ? Opcodes.IF_ACMPNE : Opcodes.IF_ACMPEQ;
            }
            else if (opToken == JetTokens.EXCLEQEQEQ) {
                opcode = jumpIfFalse ? Opcodes.IF_ACMPEQ : Opcodes.IF_ACMPNE;
            }
            else {
                throw new UnsupportedOperationException("don't know how to generate this condjump");
            }
            v.visitJumpInsn(opcode, label);
        }
    }

D
Dmitry Jemerov 已提交
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
    private static class Invert extends StackValue {
        private StackValue myOperand;

        private Invert(StackValue operand) {
            super(operand.type);
            myOperand = operand;
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
            if (type != Type.BOOLEAN_TYPE) {
                throw new UnsupportedOperationException("don't know how to put a compare as a non-boolean type");
            }
            putAsBoolean(v);
        }

        @Override
453 454
        public void condJump(Label label, boolean jumpIfFalse, InstructionAdapter v) {
            myOperand.condJump(label, !jumpIfFalse, v);
D
Dmitry Jemerov 已提交
455 456
        }
    }
457 458

    private static class ArrayElement extends StackValue {
A
Alex Tkachman 已提交
459
        private Type boxed;
460

A
Alex Tkachman 已提交
461
        public ArrayElement(Type type, boolean unbox) {
462
            super(type);
A
Alex Tkachman 已提交
463
            this.boxed = unbox ? JetTypeMapper.boxType(type) : type;
464 465 466 467
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
A
Alex Tkachman 已提交
468 469
            v.aload(boxed);    // assumes array and index are on the stack
            onStack(boxed).coerce(type, v);
470 471 472
        }

        @Override
473
        public void store(InstructionAdapter v) {
A
Alex Tkachman 已提交
474 475
            onStack(type).coerce(boxed, v);
            v.astore(boxed);
476
        }
477 478

        @Override
479 480
        public void dupReceiver(InstructionAdapter v) {
            v.dup2();   // array and index
481
        }
482 483
    }

D
Dmitry Jemerov 已提交
484 485 486
    private static class CollectionElement extends StackValue {
        private final CallableMethod getter;
        private final CallableMethod setter;
487
        private final ExpressionCodegen codegen;
488
        private final FrameMap frame;
489 490 491 492
        private final ResolvedCall<FunctionDescriptor> resolvedGetCall;
        private final ResolvedCall<FunctionDescriptor> resolvedSetCall;
        private final FunctionDescriptor setterDescriptor;
        private final FunctionDescriptor getterDescriptor;
D
Dmitry Jemerov 已提交
493

494
        public CollectionElement(Type type, ResolvedCall<FunctionDescriptor> resolvedGetCall, ResolvedCall<FunctionDescriptor> resolvedSetCall, ExpressionCodegen codegen) {
D
Dmitry Jemerov 已提交
495
            super(type);
496 497 498 499 500 501 502 503
            this.resolvedGetCall = resolvedGetCall;
            this.resolvedSetCall = resolvedSetCall;
            this.setterDescriptor = resolvedSetCall == null ? null : resolvedSetCall.getResultingDescriptor();
            this.getterDescriptor = resolvedGetCall == null ? null : resolvedGetCall.getResultingDescriptor();
            this.setter = resolvedSetCall == null ? null : codegen.typeMapper.mapToCallableMethod(setterDescriptor, false, OwnerKind.IMPLEMENTATION);
            this.getter = resolvedGetCall == null ? null : codegen.typeMapper.mapToCallableMethod(getterDescriptor, false, OwnerKind.IMPLEMENTATION);
            this.codegen = codegen;
            this.frame = codegen.myFrameMap;
D
Dmitry Jemerov 已提交
504 505 506 507 508 509 510 511
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
            if (getter == null) {
                throw new UnsupportedOperationException("no getter specified");
            }
            getter.invoke(v);
A
Alex Tkachman 已提交
512
            coerce(type, v);
D
Dmitry Jemerov 已提交
513 514 515 516 517 518 519 520 521 522 523
        }

        @Override
        public void store(InstructionAdapter v) {
            if (setter == null) {
                throw new UnsupportedOperationException("no setter specified");
            }
            setter.invoke(v);
        }

        @Override
524
        public void dupReceiver(InstructionAdapter v) {
525
            if(isStandardStack(resolvedGetCall) && isStandardStack(resolvedSetCall)) {
526
                v.dup2();   // collection and index
D
Dmitry Jemerov 已提交
527 528
            }
            else {
529 530
                int size = 0;
                int lastIndex = frame.enterTemp();
531 532
                frame.leaveTemp();
                
533 534 535 536 537 538
                // indexes
                List<ValueParameterDescriptor> valueParameters = resolvedGetCall.getResultingDescriptor().getValueParameters();
                int firstParamIndex = -1;
                for(int i = valueParameters.size()-1; i >= 0; --i) {
                    Type type = codegen.typeMapper.mapType(valueParameters.get(i).getOutType());
                    int sz = type.getSize();
539 540
                    frame.enterTemp(sz);
                    lastIndex += sz;
541 542 543 544 545 546 547 548 549 550 551 552 553
                    size += sz;
                    v.store((firstParamIndex = lastIndex)-sz, type);
                }

                List<TypeParameterDescriptor> typeParameters = resolvedGetCall.getResultingDescriptor().getTypeParameters();
                int firstTypeParamIndex = -1;
                for(int i = typeParameters.size()-1; i >= 0; --i)  {
                    if(typeParameters.get(i).isReified()) {
                        frame.enterTemp();
                        lastIndex++;
                        size++;
                        v.store(firstTypeParamIndex = lastIndex-1, JetTypeMapper.TYPE_OBJECT);
                    }
554 555
                }

556 557 558 559 560 561 562 563 564 565 566 567 568 569
                ReceiverDescriptor receiverParameter = resolvedGetCall.getReceiverArgument();
                int receiverIndex = -1;
                if(receiverParameter.exists()) {
                    Type type = codegen.typeMapper.mapType(receiverParameter.getType());
                    int sz = type.getSize();
                    frame.enterTemp(sz);
                    lastIndex += sz;
                    size += sz;
                    v.store((receiverIndex = lastIndex)-sz, type);
                }

                ReceiverDescriptor thisObject = resolvedGetCall.getThisObject();
                int thisIndex = -1;
                if(thisObject.exists()) {
570 571
                    frame.enterTemp();
                    lastIndex++;
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
                    size++;
                    v.store((thisIndex = lastIndex)-1, JetTypeMapper.TYPE_OBJECT);
                }
                
                // for setter

                int  realReceiverIndex;
                Type realReceiverType;
                if(thisIndex != -1) {
                    if(receiverIndex != -1) {
                        realReceiverIndex = receiverIndex;
                        realReceiverType =  codegen.typeMapper.mapType(receiverParameter.getType());
                    }
                    else {
                        realReceiverIndex = thisIndex;
                        realReceiverType = JetTypeMapper.TYPE_OBJECT;
                    }
                }
                else {
                    if(receiverIndex != -1) {
                        realReceiverType =  codegen.typeMapper.mapType(receiverParameter.getType());
                        realReceiverIndex = receiverIndex;
                    }
                    else {
                        throw new UnsupportedOperationException();
                    }
598 599
                }

600 601 602 603 604 605 606 607 608 609 610 611 612
                if(resolvedSetCall.getThisObject().exists()) {
                    if(resolvedSetCall.getReceiverArgument().exists()) {
                        codegen.generateFromResolvedCall(resolvedSetCall.getThisObject());
                    }
                    v.load(realReceiverIndex - realReceiverType.getSize(), realReceiverType);
                }
                else {
                    if(resolvedSetCall.getReceiverArgument().exists()) {
                        v.load(realReceiverIndex - realReceiverType.getSize(), realReceiverType);
                    }
                    else {
                        throw new UnsupportedOperationException();
                    }
613 614
                }

615 616 617 618
                for (TypeParameterDescriptor typeParameterDescriptor : setterDescriptor.getOriginal().getTypeParameters()) {
                    if(typeParameterDescriptor.isReified()) {
                        codegen.generateTypeInfo(resolvedSetCall.getTypeArguments().get(typeParameterDescriptor));
                    }
619 620
                }

621 622 623 624 625 626
                int index = firstParamIndex;
                for(int i = 0; i != valueParameters.size(); ++i) {
                    Type type = codegen.typeMapper.mapType(valueParameters.get(i).getOutType());
                    int sz = type.getSize();
                    v.load(index-sz, type);
                    index -= sz;
627 628
                }

629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
                // restoring original
                if(thisIndex != -1) {
                    v.load(thisIndex-1, JetTypeMapper.TYPE_OBJECT);
                }
                
                if(receiverIndex != -1) {
                    Type type = codegen.typeMapper.mapType(receiverParameter.getType());
                    v.load(receiverIndex-type.getSize(), type);
                }

                if(firstTypeParamIndex != -1) {
                    index = firstTypeParamIndex;
                    for(int i = 0; i != typeParameters.size(); ++i)  {
                        if(typeParameters.get(i).isReified()) {
                            v.load(index-1, JetTypeMapper.TYPE_OBJECT);
                            index--;
                        }
                    }
                }
                
                index = firstParamIndex;
                for(int i = 0; i != valueParameters.size(); ++i) {
                    Type type = codegen.typeMapper.mapType(valueParameters.get(i).getOutType());
                    int sz = type.getSize();
                    v.load(index-sz, type);
                    index -= sz;
655 656 657
                }

                frame.leaveTemp(size);
D
Dmitry Jemerov 已提交
658 659
            }
        }
660

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
        private boolean isStandardStack(ResolvedCall call) {
            for (TypeParameterDescriptor typeParameterDescriptor : call.getResultingDescriptor().getTypeParameters()) {
                if(typeParameterDescriptor.isReified())
                    return false;
            }
            
            if(call.getResultingDescriptor().getValueParameters().size() != 1)
                return false;

            if(codegen.typeMapper.mapType(call.getResultingDescriptor().getValueParameters().get(0).getOutType()).getSize() != 1)
                return false;

            if(call.getThisObject().exists()) {
                if(call.getReceiverArgument().exists())
                    return false;
            }
            else {
                if(codegen.typeMapper.mapType(call.getResultingDescriptor().getReceiverParameter().getType()).getSize() != 1)
                    return false;
            }

            return true;
683
        }
D
Dmitry Jemerov 已提交
684 685
    }

M
Maxim Shafirov 已提交
686

A
Alex Tkachman 已提交
687 688 689
    static class Field extends StackValue {
        final String owner;
        final String name;
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
        private final boolean isStatic;

        public Field(Type type, String owner, String name, boolean isStatic) {
            super(type);
            this.owner = owner;
            this.name = name;
            this.isStatic = isStatic;
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
            v.visitFieldInsn(isStatic ? Opcodes.GETSTATIC : Opcodes.GETFIELD, owner, name, this.type.getDescriptor());
        }

        @Override
705
        public void dupReceiver(InstructionAdapter v) {
D
Dmitry Jemerov 已提交
706
            if (!isStatic) {
707
                v.dup();
D
Dmitry Jemerov 已提交
708
            }
709 710 711 712
        }

        @Override
        public void store(InstructionAdapter v) {
713 714 715
            v.visitFieldInsn(isStatic ? Opcodes.PUTSTATIC : Opcodes.PUTFIELD, owner, name, this.type.getDescriptor());
        }
    }
716

A
Alex Tkachman 已提交
717 718 719
    static class InstanceField extends StackValue {
        final String owner;
        final String name;
M
Maxim Shafirov 已提交
720 721 722 723 724 725 726 727 728 729 730 731 732 733

        public InstanceField(Type type, String owner, String name) {
            super(type);
            this.owner = owner;
            this.name = name;
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
            v.load(0, JetTypeMapper.TYPE_OBJECT);
            v.getfield(owner, name, this.type.getDescriptor());
        }

        @Override
734
        public void dupReceiver(InstructionAdapter v) {
M
Maxim Shafirov 已提交
735 736 737 738 739 740 741 742 743
        }

        @Override
        public void store(InstructionAdapter v) {
            v.load(0, JetTypeMapper.TYPE_OBJECT);
            v.putfield(owner, name, this.type.getDescriptor());
        }
    }

744 745 746 747
    private static class Property extends StackValue {
        private final String name;
        private final Method getter;
        private final Method setter;
748
        private final String owner;
749
        private final boolean isStatic;
750
        private final boolean isInterface;
A
Alex Tkachman 已提交
751
        private boolean isSuper;
752

A
Alex Tkachman 已提交
753
        public Property(String name, String owner, Method getter, Method setter, boolean aStatic, boolean isInterface, boolean isSuper, Type type) {
754 755
            super(type);
            this.name = name;
756
            this.owner = owner;
757 758
            this.getter = getter;
            this.setter = setter;
759
            isStatic = aStatic;
760
            this.isInterface = isInterface;
A
Alex Tkachman 已提交
761
            this.isSuper = isSuper;
762 763 764 765
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
A
Alex Tkachman 已提交
766 767
            if(isSuper && isInterface) {
                v.visitMethodInsn(Opcodes.INVOKESTATIC, owner + "$$TImpl", getter.getName(), getter.getDescriptor().replace("(","(L" + owner + ";"));
768 769
            }
            else {
A
Alex Tkachman 已提交
770 771 772 773
            if (getter == null) {
                v.visitFieldInsn(isStatic ? Opcodes.GETSTATIC : Opcodes.GETFIELD, owner, name, this.type.getDescriptor());
            }
            else {
A
Alex Tkachman 已提交
774
                    v.visitMethodInsn(isStatic ? Opcodes.INVOKESTATIC : isSuper ? Opcodes.INVOKESPECIAL : isInterface ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL, owner, getter.getName(), getter.getDescriptor());
A
Alex Tkachman 已提交
775
            }
776
            }
777
            coerce(type, v);
778 779 780 781
        }

        @Override
        public void store(InstructionAdapter v) {
A
Alex Tkachman 已提交
782 783
            if(isSuper && isInterface) {
                v.visitMethodInsn(Opcodes.INVOKESTATIC, owner + "$$TImpl", setter.getName(), setter.getDescriptor().replace("(","(L" + owner + ";"));
784 785
            }
            else {
A
Alex Tkachman 已提交
786 787 788 789
            if (setter == null) {
                v.visitFieldInsn(isStatic ? Opcodes.PUTSTATIC : Opcodes.PUTFIELD, owner, name, this.type.getDescriptor());
            }
            else {
A
Alex Tkachman 已提交
790 791
                    v.visitMethodInsn(isStatic ? Opcodes.INVOKESTATIC : isSuper ? Opcodes.INVOKESPECIAL : isInterface ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL, owner, setter.getName(), setter.getDescriptor());
                }
792 793
            }
        }
794 795

        @Override
796
        public void dupReceiver(InstructionAdapter v) {
797
            if (!isStatic) {
798
                v.dup();
799 800
            }
        }
801
    }
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817

    private static class Expression extends StackValue {
        private final JetExpression expression;
        private final ExpressionCodegen generator;

        public Expression(Type type, JetExpression expression, ExpressionCodegen generator) {
            super(type);
            this.expression = expression;
            this.generator = generator;
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
            generator.gen(expression, type);
        }
    }
A
Alex Tkachman 已提交
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864

    public static class Shared extends StackValue {
        private final int index;

        public Shared(int index, Type type) {
            super(type);
            this.index = index;
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
            v.load(index, JetTypeMapper.TYPE_OBJECT);
            Type refType = refType(this.type);
            Type sharedType = sharedTypeForType(this.type);
            v.visitFieldInsn(Opcodes.GETFIELD, sharedType.getInternalName(), "ref", refType.getDescriptor());
            StackValue.onStack(refType).coerce(this.type, v);
            StackValue.onStack(this.type).coerce(type, v);
        }

        @Override
        public void store(InstructionAdapter v) {
            v.load(index, JetTypeMapper.TYPE_OBJECT);
            v.swap();
            Type refType = refType(this.type);
            Type sharedType = sharedTypeForType(this.type);
            v.visitFieldInsn(Opcodes.PUTFIELD, sharedType.getInternalName(), "ref", refType.getDescriptor());
        }
    }

    public static Type sharedTypeForType(Type type) {
        switch(type.getSort()) {
            case Type.OBJECT:
            case Type.ARRAY:
                return JetTypeMapper.TYPE_SHARED_VAR;

            case Type.BYTE:
                return JetTypeMapper.TYPE_SHARED_BYTE;

            case Type.SHORT:
                return JetTypeMapper.TYPE_SHARED_SHORT;

            case Type.CHAR:
                return JetTypeMapper.TYPE_SHARED_CHAR;

            case Type.INT:
                return JetTypeMapper.TYPE_SHARED_INT;

A
varargs  
Alex Tkachman 已提交
865 866 867
            case Type.LONG:
                return JetTypeMapper.TYPE_SHARED_LONG;

A
Alex Tkachman 已提交
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
            case Type.BOOLEAN:
                return JetTypeMapper.TYPE_SHARED_BOOLEAN;

            case Type.FLOAT:
                return JetTypeMapper.TYPE_SHARED_FLOAT;

            case Type.DOUBLE:
                return JetTypeMapper.TYPE_SHARED_DOUBLE;

            default:
                throw new UnsupportedOperationException();
        }
    }

    public static Type refType(Type type) {
        if(type.getSort() == Type.OBJECT || type.getSort() == Type.ARRAY)
            return JetTypeMapper.TYPE_OBJECT;

        return type;
    }

    static class FieldForSharedVar extends StackValue {
        final String owner;
        final String name;

        public FieldForSharedVar(Type type, String owner, String name) {
            super(type);
            this.owner = owner;
            this.name = name;
        }

        @Override
900 901
        public void dupReceiver(InstructionAdapter v) {
            v.dup();
A
Alex Tkachman 已提交
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
            Type sharedType = sharedTypeForType(this.type);
            Type refType = refType(this.type);
            v.visitFieldInsn(Opcodes.GETFIELD, sharedType.getInternalName(), "ref", refType.getDescriptor());
            StackValue.onStack(refType).coerce(this.type, v);
            StackValue.onStack(this.type).coerce(type, v);
        }

        @Override
        public void store(InstructionAdapter v) {
            v.visitFieldInsn(Opcodes.PUTFIELD, sharedTypeForType(type).getInternalName(), "ref", refType(type).getDescriptor());
        }
    }
918

A
Alex Tkachman 已提交
919 920 921
    public static class Composed extends StackValue {
        public final StackValue prefix;
        public final StackValue suffix;
922 923 924 925 926 927 928 929 930 931 932 933

        public Composed(StackValue prefix, StackValue suffix) {
            super(suffix.type);
            this.prefix = prefix;
            this.suffix = suffix;
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
            prefix.put(prefix.type, v);
            suffix.put(type, v);
        }
A
Alex Tkachman 已提交
934 935 936 937 938 939

        @Override
        public void store(InstructionAdapter v) {
            prefix.put(JetTypeMapper.TYPE_OBJECT, v);
            suffix.store(v);
        }
940
    }
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956

    private static class ThisOuter extends StackValue {
        private ExpressionCodegen codegen;
        private ClassDescriptor descriptor;

        public ThisOuter(ExpressionCodegen codegen, ClassDescriptor descriptor) {
            super(JetTypeMapper.TYPE_OBJECT);
            this.codegen = codegen;
            this.descriptor = descriptor;
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
            codegen.generateThisOrOuter(descriptor);
        }
    }
957
}