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

3
import com.intellij.psi.tree.IElementType;
4
import org.jetbrains.annotations.Nullable;
5
import org.jetbrains.jet.codegen.intrinsics.IntrinsicMethod;
6
import org.jetbrains.jet.lang.descriptors.*;
7
import org.jetbrains.jet.lang.psi.JetExpression;
8
import org.jetbrains.jet.lang.resolve.calls.ResolvedCall;
9
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
10
import org.jetbrains.jet.lang.resolve.java.JvmPrimitiveType;
11
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
12
import org.jetbrains.jet.lang.types.JetType;
13 14 15
import org.jetbrains.jet.lexer.JetTokens;
import org.objectweb.asm.Label;
import org.objectweb.asm.Opcodes;
16 17
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.InstructionAdapter;
18
import org.objectweb.asm.commons.Method;
19

20 21
import java.util.List;

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

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

    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 {
40
            Type boxed = JetTypeMapper.boxType(type);
41 42
            instructionAdapter.invokestatic(boxed.getInternalName(), "valueOf", "(" + type.getDescriptor() + ")" + boxed.getDescriptor());
        }
D
Dmitry Jemerov 已提交
43 44
    }

45 46
    public abstract void put(Type type, InstructionAdapter v);

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

51
    public void dupReceiver(InstructionAdapter v) {
52 53
    }

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    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 已提交
69
    public static StackValue local(int index, Type type) {
70 71 72
        return new Local(index, type);
    }

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

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

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

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

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

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

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

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

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

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

113 114 115
    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 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
            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;");
        }
    }

141 142 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
    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 已提交
168
    public void upcast(Type type, InstructionAdapter v) {
M
Maxim Shafirov 已提交
169 170 171 172 173
        if (type.equals(this.type)) return;

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

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

A
Alex Tkachman 已提交
182 183 184 185 186 187 188 189
        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)
190
                v.visitFieldInsn(Opcodes.GETSTATIC, "jet/Tuple0", "INSTANCE", "Ljet/Tuple0;");
A
Alex Tkachman 已提交
191 192 193 194 195 196 197 198 199
            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 已提交
200
        else if (type.getSort() == Type.OBJECT && this.type.equals(JetTypeMapper.TYPE_OBJECT) || type.getSort() == Type.ARRAY) {
201
                v.checkcast(type);
M
Maxim Shafirov 已提交
202
        }
M
Maxim Shafirov 已提交
203
        else if (type.getSort() == Type.OBJECT) {
204 205 206 207 208
            if(this.type.getSort() == Type.OBJECT && !type.equals(JetTypeMapper.TYPE_OBJECT)) {
                v.checkcast(type);
            }
            else
                box(this.type, type, v);
D
Dmitry Jemerov 已提交
209
        }
210
        else if (this.type.getSort() == Type.OBJECT && type.getSort() <= Type.DOUBLE) {
M
Maxim Shafirov 已提交
211
            if (this.type.equals(JetTypeMapper.TYPE_OBJECT)) {
212
                if (type.getSort() == Type.BOOLEAN) {
213
                    v.checkcast(JvmPrimitiveType.BOOLEAN.getWrapper().getAsmType());
214
                }
A
Alex Tkachman 已提交
215
                else if (type.getSort() == Type.CHAR) {
216
                    v.checkcast(JvmPrimitiveType.CHAR.getWrapper().getAsmType());
A
Alex Tkachman 已提交
217
                }
218
                else {
A
Alex Tkachman 已提交
219
                    v.checkcast(JetTypeMapper.JL_NUMBER_TYPE);
220
                }
M
Maxim Shafirov 已提交
221
            }
222 223
            unbox(type, v);
        }
M
Maxim Shafirov 已提交
224
        else {
D
Dmitry Jemerov 已提交
225 226 227 228
            v.cast(this.type, type);
        }
    }

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

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

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

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

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

A
Alex Tkachman 已提交
256 257 258 259 260 261 262 263
    public static StackValue postIncrement(int index, int increment) {
        return new PostIncrement(index, increment);
    }

    public static StackValue preIncrement(int index, int increment) {
        return new PreIncrement(index, increment);
    }

264 265 266 267 268 269
    public static StackValue receiver(ResolvedCall<? extends CallableDescriptor> resolvedCall, StackValue receiver, ExpressionCodegen codegen, @Nullable CallableMethod callableMethod) {
        if(resolvedCall.getThisObject().exists() || resolvedCall.getReceiverArgument().exists())
            return new CallReceiver(resolvedCall, receiver, codegen, callableMethod);
        return receiver;
    }

270 271 272 273 274 275 276 277
    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 已提交
278
            coerce(type, v);
279 280 281
        }
    }

282
    public static class Local extends StackValue {
283
        final int index;
284

D
Dmitry Jemerov 已提交
285
        public Local(int index, Type type) {
D
Dmitry Jemerov 已提交
286
            super(type);
287 288 289 290 291
            this.index = index;
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
D
Dmitry Jemerov 已提交
292
            v.load(index, this.type);
D
Dmitry Jemerov 已提交
293
            coerce(type, v);
D
Dmitry Jemerov 已提交
294
            // TODO unbox
295
        }
296 297

        @Override
298
        public void store(InstructionAdapter v) {
299
            v.store(index, this.type);
300
        }
301 302 303 304
    }

    public static class OnStack extends StackValue {
        public OnStack(Type type) {
D
Dmitry Jemerov 已提交
305
            super(type);
306 307 308 309
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
310 311 312 313 314 315 316 317 318 319 320
            if (type == Type.VOID_TYPE && this.type != Type.VOID_TYPE) {
                if (this.type.getSize() == 2) {
                    v.pop2();
                }
                else {
                    v.pop();
                }
            }
            else {
                coerce(type, v);
            }
321 322 323 324 325 326
        }
    }

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

D
Dmitry Jemerov 已提交
327 328
        public Constant(Object value, Type type) {
            super(type);
329 330 331 332 333
            this.value = value;
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
A
Alex Tkachman 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346
            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 已提交
347
            coerce(type, v);
348
        }
349 350

        @Override
351
        public void condJump(Label label, boolean jumpIfFalse, InstructionAdapter v) {
D
Dmitry Jemerov 已提交
352
            if (value instanceof Boolean) {
A
Alex Tkachman 已提交
353
                boolean boolValue = (Boolean) value;
354 355 356
                if (boolValue ^ jumpIfFalse) {
                    v.goTo(label);
                }
D
Dmitry Jemerov 已提交
357 358 359 360
            }
            else {
                throw new UnsupportedOperationException("don't know how to generate this condjump");
            }
361 362 363
        }
    }

D
Float  
Dmitry Jemerov 已提交
364
    private static class NumberCompare extends StackValue {
D
Dmitry Jemerov 已提交
365
        protected final IElementType opToken;
D
Dmitry Jemerov 已提交
366
        private final Type operandType;
367

D
Dmitry Jemerov 已提交
368 369
        public NumberCompare(IElementType opToken, Type operandType) {
            super(Type.BOOLEAN_TYPE);
370
            this.opToken = opToken;
D
Dmitry Jemerov 已提交
371
            this.operandType = operandType;
372 373 374 375
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
376 377 378
            if (type == Type.VOID_TYPE) {
                return;
            }
379
            if (type != Type.BOOLEAN_TYPE) {
380
                throw new UnsupportedOperationException("don't know how to put a compare as a non-boolean type " + type);
381
            }
D
Dmitry Jemerov 已提交
382
            putAsBoolean(v);
383 384 385
        }

        @Override
386
        public void condJump(Label label, boolean jumpIfFalse, InstructionAdapter v) {
387
            int opcode;
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
            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;
            }
406 407 408
            else {
                throw new UnsupportedOperationException("don't know how to generate this condjump");
            }
D
Dmitry Jemerov 已提交
409
            if (operandType == Type.FLOAT_TYPE || operandType == Type.DOUBLE_TYPE) {
D
Float  
Dmitry Jemerov 已提交
410
                if (opToken == JetTokens.GT || opToken == JetTokens.GTEQ) {
D
Dmitry Jemerov 已提交
411
                    v.cmpg(operandType);
D
Float  
Dmitry Jemerov 已提交
412 413
                }
                else {
D
Dmitry Jemerov 已提交
414
                    v.cmpl(operandType);
D
Float  
Dmitry Jemerov 已提交
415 416
                }
            }
D
Dmitry Jemerov 已提交
417
            else if (operandType == Type.LONG_TYPE) {
D
Dmitry Jemerov 已提交
418 419
                v.lcmp();
            }
D
Float  
Dmitry Jemerov 已提交
420 421 422
            else {
                opcode += (Opcodes.IF_ICMPEQ - Opcodes.IFEQ);
            }
423
            v.visitJumpInsn(opcode, label);
424
        }
425
    }
D
Dmitry Jemerov 已提交
426

D
Dmitry Jemerov 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
    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 已提交
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
    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
465 466
        public void condJump(Label label, boolean jumpIfFalse, InstructionAdapter v) {
            myOperand.condJump(label, !jumpIfFalse, v);
D
Dmitry Jemerov 已提交
467 468
        }
    }
469 470

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

A
Alex Tkachman 已提交
473
        public ArrayElement(Type type, boolean unbox) {
474
            super(type);
A
Alex Tkachman 已提交
475
            this.boxed = unbox ? JetTypeMapper.boxType(type) : type;
476 477 478 479
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
A
Alex Tkachman 已提交
480 481
            v.aload(boxed);    // assumes array and index are on the stack
            onStack(boxed).coerce(type, v);
482 483 484
        }

        @Override
485
        public void store(InstructionAdapter v) {
A
Alex Tkachman 已提交
486 487
            onStack(type).coerce(boxed, v);
            v.astore(boxed);
488
        }
489 490

        @Override
491 492
        public void dupReceiver(InstructionAdapter v) {
            v.dup2();   // array and index
493
        }
494 495
    }

D
Dmitry Jemerov 已提交
496
    private static class CollectionElement extends StackValue {
497 498
        private final Callable getter;
        private final Callable setter;
499
        private final ExpressionCodegen codegen;
500
        private final FrameMap frame;
501 502 503 504
        private final ResolvedCall<FunctionDescriptor> resolvedGetCall;
        private final ResolvedCall<FunctionDescriptor> resolvedSetCall;
        private final FunctionDescriptor setterDescriptor;
        private final FunctionDescriptor getterDescriptor;
D
Dmitry Jemerov 已提交
505

506
        public CollectionElement(Type type, ResolvedCall<FunctionDescriptor> resolvedGetCall, ResolvedCall<FunctionDescriptor> resolvedSetCall, ExpressionCodegen codegen) {
D
Dmitry Jemerov 已提交
507
            super(type);
508 509 510 511
            this.resolvedGetCall = resolvedGetCall;
            this.resolvedSetCall = resolvedSetCall;
            this.setterDescriptor = resolvedSetCall == null ? null : resolvedSetCall.getResultingDescriptor();
            this.getterDescriptor = resolvedGetCall == null ? null : resolvedGetCall.getResultingDescriptor();
512 513
            this.setter = resolvedSetCall == null ? null : codegen.resolveToCallable(setterDescriptor, false);
            this.getter = resolvedGetCall == null ? null : codegen.resolveToCallable(getterDescriptor, false);
514 515
            this.codegen = codegen;
            this.frame = codegen.myFrameMap;
D
Dmitry Jemerov 已提交
516 517 518 519 520 521 522
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
            if (getter == null) {
                throw new UnsupportedOperationException("no getter specified");
            }
523 524 525 526
            if(getter instanceof CallableMethod)
                ((CallableMethod)getter).invoke(v);
            else
                ((IntrinsicMethod)getter).generate(codegen, v, null, null, null, null);
A
Alex Tkachman 已提交
527
            coerce(type, v);
D
Dmitry Jemerov 已提交
528 529 530 531 532 533 534
        }

        @Override
        public void store(InstructionAdapter v) {
            if (setter == null) {
                throw new UnsupportedOperationException("no setter specified");
            }
535 536 537 538 539 540 541 542 543 544 545
            if(setter instanceof CallableMethod) {
                CallableMethod method = (CallableMethod) setter;
                method.invoke(v);
                Type returnType = method.getSignature().getAsmMethod().getReturnType();
                if(returnType != Type.VOID_TYPE) {
                    if(returnType.getSize() == 2)
                        v.pop2();
                    else
                        v.pop();
                }
            }
546 547
            else
                ((IntrinsicMethod)setter).generate(codegen, v, null, null, null, null);
D
Dmitry Jemerov 已提交
548 549 550
        }

        @Override
551
        public void dupReceiver(InstructionAdapter v) {
552
            if(isStandardStack(resolvedGetCall) && isStandardStack(resolvedSetCall)) {
553
                v.dup2();   // collection and index
D
Dmitry Jemerov 已提交
554 555
            }
            else {
556 557
                int size = 0;
                int lastIndex = frame.enterTemp();
558 559
                frame.leaveTemp();
                
560 561 562 563 564 565
                // 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();
566 567
                    frame.enterTemp(sz);
                    lastIndex += sz;
568 569 570 571 572 573 574 575 576 577 578 579 580
                    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);
                    }
581 582
                }

583 584 585 586 587 588 589 590 591 592 593 594 595 596
                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()) {
597 598
                    frame.enterTemp();
                    lastIndex++;
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
                    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();
                    }
625 626
                }

627 628
                if(resolvedSetCall.getThisObject().exists()) {
                    if(resolvedSetCall.getReceiverArgument().exists()) {
629
                        codegen.generateFromResolvedCall(resolvedSetCall.getThisObject(), JetTypeMapper.TYPE_OBJECT);
630 631 632 633 634 635 636 637 638 639
                    }
                    v.load(realReceiverIndex - realReceiverType.getSize(), realReceiverType);
                }
                else {
                    if(resolvedSetCall.getReceiverArgument().exists()) {
                        v.load(realReceiverIndex - realReceiverType.getSize(), realReceiverType);
                    }
                    else {
                        throw new UnsupportedOperationException();
                    }
640 641
                }

642 643
                for (TypeParameterDescriptor typeParameterDescriptor : setterDescriptor.getOriginal().getTypeParameters()) {
                    if(typeParameterDescriptor.isReified()) {
644
                        codegen.generateTypeInfo(resolvedSetCall.getTypeArguments().get(typeParameterDescriptor), null);
645
                    }
646 647
                }

648 649 650 651 652 653
                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;
654 655
                }

656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
                // 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;
682 683 684
                }

                frame.leaveTemp(size);
D
Dmitry Jemerov 已提交
685 686
            }
        }
687

688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
        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;
710
        }
D
Dmitry Jemerov 已提交
711 712
    }

M
Maxim Shafirov 已提交
713

A
Alex Tkachman 已提交
714 715 716
    static class Field extends StackValue {
        final String owner;
        final String name;
717 718 719 720 721 722 723 724 725 726 727 728
        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());
729
            coerce(type, v);
730 731 732
        }

        @Override
733
        public void dupReceiver(InstructionAdapter v) {
D
Dmitry Jemerov 已提交
734
            if (!isStatic) {
735
                v.dup();
D
Dmitry Jemerov 已提交
736
            }
737 738 739 740
        }

        @Override
        public void store(InstructionAdapter v) {
741 742 743
            v.visitFieldInsn(isStatic ? Opcodes.PUTSTATIC : Opcodes.PUTFIELD, owner, name, this.type.getDescriptor());
        }
    }
744

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

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

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

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

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

    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 已提交
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 865 866 867

    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 已提交
868 869 870
            case Type.LONG:
                return JetTypeMapper.TYPE_SHARED_LONG;

A
Alex Tkachman 已提交
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 900 901 902
            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
903 904
        public void dupReceiver(InstructionAdapter v) {
            v.dup();
A
Alex Tkachman 已提交
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
        }

        @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());
        }
    }
921

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

        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 已提交
937 938 939 940 941 942

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

    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);
        }
    }
A
Alex Tkachman 已提交
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999

    private static class PostIncrement extends StackValue {
        private int index;
        private int increment;

        public PostIncrement(int index, int increment) {
            super(Type.INT_TYPE);
            this.index = index;
            this.increment = increment;
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
            if(!type.equals(Type.VOID_TYPE)) {
                v.load(index, Type.INT_TYPE);
                coerce(type, v);
            }
            v.iinc(index, increment);
        }
    }

    private static class PreIncrement extends StackValue {
        private int index;
        private int increment;

        public PreIncrement(int index, int increment) {
            super(Type.INT_TYPE);
            this.index = index;
            this.increment = increment;
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
            v.iinc(index, increment);
            if(!type.equals(Type.VOID_TYPE)) {
                v.load(index, Type.INT_TYPE);
                coerce(type, v);
            }
        }
    }
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026

    public static class CallReceiver extends StackValue {
        private ResolvedCall<? extends CallableDescriptor> resolvedCall;
        StackValue receiver;
        private ExpressionCodegen codegen;
        private CallableMethod callableMethod;

        public CallReceiver(ResolvedCall<? extends CallableDescriptor> resolvedCall, StackValue receiver, ExpressionCodegen codegen, CallableMethod callableMethod) {
            super(calcType(resolvedCall, codegen, callableMethod));
            this.resolvedCall = resolvedCall;
            this.receiver = receiver;
            this.codegen = codegen;
            this.callableMethod = callableMethod;
        }

        private static Type calcType(ResolvedCall<? extends CallableDescriptor> resolvedCall, ExpressionCodegen codegen, CallableMethod callableMethod) {
            ReceiverDescriptor thisObject = resolvedCall.getThisObject();
            ReceiverDescriptor receiverArgument = resolvedCall.getReceiverArgument();

            CallableDescriptor descriptor = resolvedCall.getResultingDescriptor();

            if (thisObject.exists()) {
                if(callableMethod != null) {
                    if(receiverArgument.exists()) {
                        return codegen.typeMapper.mapType(callableMethod.getReceiverClass());
                    }
                    else {
1027 1028
                        JetType thisType = callableMethod.getThisType();
                        return codegen.typeMapper.mapType(thisType);
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
                    }
                }
                else {
                    if(receiverArgument.exists()) {
                        return codegen.typeMapper.mapType(descriptor.getReceiverParameter().getType());
                    }
                    else {
                        return codegen.typeMapper.mapType(descriptor.getExpectedThisObject().getType());
                    }
                }
            }
            else {
                if (receiverArgument.exists()) {
                    if(callableMethod != null)
                        return codegen.typeMapper.mapType(callableMethod.getReceiverClass());
                    else
                        return codegen.typeMapper.mapType(descriptor.getReceiverParameter().getType());
                }
                else {
                    return Type.VOID_TYPE;
                }
            }
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
            CallableDescriptor descriptor = resolvedCall.getResultingDescriptor();

            ReceiverDescriptor thisObject = resolvedCall.getThisObject();
            ReceiverDescriptor receiverArgument = resolvedCall.getReceiverArgument();
            if (thisObject.exists()) {
                if(receiverArgument.exists()) {
                    codegen.generateFromResolvedCall(thisObject, codegen.typeMapper.mapType(descriptor.getExpectedThisObject().getType()));
                    genReceiver(v, receiverArgument, type, descriptor.getReceiverParameter());
                }
                else {
                    genReceiver(v, thisObject, type, null);
                }
            }
            else {
                if (receiverArgument.exists()) {
                    genReceiver(v, receiverArgument, type, descriptor.getReceiverParameter());
                }
            }
        }

        private void genReceiver(InstructionAdapter v, ReceiverDescriptor receiverArgument, Type type, ReceiverDescriptor receiverParameter) {
            if(receiver == StackValue.none()) {
                if(receiverParameter != null) {
                    Type receiverType = codegen.typeMapper.mapType(receiverParameter.getType());
                    codegen.generateFromResolvedCall(receiverArgument, receiverType);
                    StackValue.onStack(receiverType).put(type, v);
                }
                else {
                    codegen.generateFromResolvedCall(receiverArgument, type);
                }
            }
            else {
                receiver.put(type, v);
            }
        }
    }
1091
}