StackValue.java 41.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2010-2012 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17 18
package org.jetbrains.jet.codegen;

19
import com.intellij.psi.tree.IElementType;
20
import org.jetbrains.annotations.NotNull;
21
import org.jetbrains.annotations.Nullable;
22
import org.jetbrains.jet.codegen.intrinsics.IntrinsicMethod;
23
import org.jetbrains.jet.lang.descriptors.*;
24
import org.jetbrains.jet.lang.psi.JetExpression;
25
import org.jetbrains.jet.lang.resolve.calls.ResolvedCall;
26
import org.jetbrains.jet.lang.resolve.java.JvmPrimitiveType;
27
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
28 29 30
import org.jetbrains.jet.lexer.JetTokens;
import org.objectweb.asm.Label;
import org.objectweb.asm.Opcodes;
31 32
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.InstructionAdapter;
33
import org.objectweb.asm.commons.Method;
34

35 36
import java.util.List;

37 38
/**
 * @author yole
A
Alex Tkachman 已提交
39
 * @author alex.tkachman
40 41
 */
public abstract class StackValue {
42
    public final Type type;
D
Dmitry Jemerov 已提交
43 44 45

    public StackValue(Type type) {
        this.type = type;
46 47 48 49 50 51 52 53
    }

    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);
S
Stepan Koltsov 已提交
54 55
        }
        else {
56
            Type boxed = JetTypeMapper.boxType(type);
57 58
            instructionAdapter.invokestatic(boxed.getInternalName(), "valueOf", "(" + type.getDescriptor() + ")" + boxed.getDescriptor());
        }
D
Dmitry Jemerov 已提交
59 60
    }

61 62
    public abstract void put(Type type, InstructionAdapter v);

63
    public void store(InstructionAdapter v) {
64 65 66
        throw new UnsupportedOperationException("cannot store to value " + this);
    }

67
    public void dupReceiver(InstructionAdapter v) {
68 69
    }

A
Alex Tkachman 已提交
70 71 72 73
    public int receiverSize() {
        return 0;
    }

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    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 已提交
89
    public static StackValue local(int index, Type type) {
90 91 92
        return new Local(index, type);
    }

A
Alex Tkachman 已提交
93 94 95 96
    public static StackValue shared(int index, Type type) {
        return new Shared(index, type);
    }

97
    public static StackValue onStack(Type type) {
98
        return type == Type.VOID_TYPE ? none() : new OnStack(type);
99 100
    }

D
Dmitry Jemerov 已提交
101 102
    public static StackValue constant(Object value, Type type) {
        return new Constant(value, type);
103 104
    }

105
    public static StackValue cmp(IElementType opToken, Type type) {
D
Dmitry Jemerov 已提交
106
        return type.getSort() == Type.OBJECT ? new ObjectCompare(opToken, type) : new NumberCompare(opToken, type);
107 108
    }

D
Dmitry Jemerov 已提交
109 110 111 112
    public static StackValue not(StackValue stackValue) {
        return new Invert(stackValue);
    }

A
Alex Tkachman 已提交
113 114
    public static StackValue arrayElement(Type type, boolean unbox) {
        return new ArrayElement(type, unbox);
115 116
    }

S
Stepan Koltsov 已提交
117 118
    public static StackValue collectionElement(Type type, ResolvedCall<FunctionDescriptor> getter, ResolvedCall<FunctionDescriptor> setter, ExpressionCodegen codegen, GenerationState state) {
        return new CollectionElement(type, getter, setter, codegen, state);
D
Dmitry Jemerov 已提交
119 120
    }

121 122 123
    public static StackValue field(Type type, String owner, String name, boolean isStatic) {
        return new Field(type, owner, name, isStatic);
    }
124

125 126
    public static Property property(String name, String methodOwner, String methodOwnerParam, Type type, boolean isStatic, boolean isInterface, boolean isSuper, Method getter, Method setter, int invokeOpcode) {
        return new Property(name, methodOwner, methodOwnerParam, getter, setter, isStatic, isInterface, isSuper, type, invokeOpcode);
127 128
    }

129 130 131 132
    public static StackValue expression(Type type, JetExpression expression, ExpressionCodegen generator) {
        return new Expression(type, expression, generator);
    }

133 134 135
    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 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
            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;");
        }
    }

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    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 已提交
188
    public void upcast(Type type, InstructionAdapter v) {
M
Maxim Shafirov 已提交
189 190 191 192 193
        if (type.equals(this.type)) return;

        if (type.getSort() == Type.OBJECT && this.type.getSort() == Type.OBJECT) {
            v.checkcast(type);
        }
M
Maxim Shafirov 已提交
194 195 196 197 198 199 200 201
        else {
            coerce(type, v);
        }
    }

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

A
Alex Tkachman 已提交
202 203 204 205 206 207 208 209
        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)
210
                v.visitFieldInsn(Opcodes.GETSTATIC, "jet/Tuple0", "INSTANCE", "Ljet/Tuple0;");
A
Alex Tkachman 已提交
211 212 213 214 215 216 217 218 219
            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 已提交
220
        else if (type.getSort() == Type.OBJECT && this.type.equals(JetTypeMapper.TYPE_OBJECT) || type.getSort() == Type.ARRAY) {
221
                v.checkcast(type);
M
Maxim Shafirov 已提交
222
        }
M
Maxim Shafirov 已提交
223
        else if (type.getSort() == Type.OBJECT) {
224 225 226 227 228
            if(this.type.getSort() == Type.OBJECT && !type.equals(JetTypeMapper.TYPE_OBJECT)) {
                v.checkcast(type);
            }
            else
                box(this.type, type, v);
D
Dmitry Jemerov 已提交
229
        }
230
        else if (this.type.getSort() == Type.OBJECT && type.getSort() <= Type.DOUBLE) {
M
Maxim Shafirov 已提交
231
            if (this.type.equals(JetTypeMapper.TYPE_OBJECT)) {
232
                if (type.getSort() == Type.BOOLEAN) {
233
                    v.checkcast(JvmPrimitiveType.BOOLEAN.getWrapper().getAsmType());
234
                }
A
Alex Tkachman 已提交
235
                else if (type.getSort() == Type.CHAR) {
236
                    v.checkcast(JvmPrimitiveType.CHAR.getWrapper().getAsmType());
A
Alex Tkachman 已提交
237
                }
238
                else {
A
Alex Tkachman 已提交
239
                    v.checkcast(JetTypeMapper.JL_NUMBER_TYPE);
240
                }
M
Maxim Shafirov 已提交
241
            }
242 243
            unbox(type, v);
        }
M
Maxim Shafirov 已提交
244
        else {
D
Dmitry Jemerov 已提交
245 246 247 248
            v.cast(this.type, type);
        }
    }

D
Dmitry Jemerov 已提交
249 250 251
    protected void putAsBoolean(InstructionAdapter v) {
        Label ifTrue = new Label();
        Label end = new Label();
252
        condJump(ifTrue, false, v);
D
Dmitry Jemerov 已提交
253 254 255 256 257 258 259
        v.iconst(0);
        v.goTo(end);
        v.mark(ifTrue);
        v.iconst(1);
        v.mark(end);
    }

260 261 262
    public static StackValue none() {
        return None.INSTANCE;
    }
A
Alex Tkachman 已提交
263 264 265 266 267

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

268 269 270 271
    public static StackValue composed(StackValue prefix, StackValue suffix) {
        return new Composed(prefix, suffix);
    }

272 273 274 275
    public static StackValue thisOrOuter(ExpressionCodegen codegen, ClassDescriptor descriptor) {
        return new ThisOuter(codegen, descriptor);
    }

A
Alex Tkachman 已提交
276 277 278 279 280 281 282 283
    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);
    }

S
Stepan Koltsov 已提交
284
    public static StackValue receiver(ResolvedCall<? extends CallableDescriptor> resolvedCall, StackValue receiver, ExpressionCodegen codegen, @Nullable CallableMethod callableMethod, GenerationState state) {
285
        if(resolvedCall.getThisObject().exists() || resolvedCall.getReceiverArgument().exists())
S
Stepan Koltsov 已提交
286
            return new CallReceiver(resolvedCall, receiver, codegen, state, callableMethod);
287 288 289
        return receiver;
    }

290 291 292 293 294 295 296 297
    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 已提交
298
            coerce(type, v);
299 300 301
        }
    }

302
    public static class Local extends StackValue {
303
        final int index;
304

D
Dmitry Jemerov 已提交
305
        public Local(int index, Type type) {
D
Dmitry Jemerov 已提交
306
            super(type);
307 308 309 310 311
            this.index = index;
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
D
Dmitry Jemerov 已提交
312
            v.load(index, this.type);
D
Dmitry Jemerov 已提交
313
            coerce(type, v);
D
Dmitry Jemerov 已提交
314
            // TODO unbox
315
        }
316 317

        @Override
318
        public void store(InstructionAdapter v) {
319
            v.store(index, this.type);
320
        }
321 322 323 324
    }

    public static class OnStack extends StackValue {
        public OnStack(Type type) {
D
Dmitry Jemerov 已提交
325
            super(type);
326 327 328 329
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
330 331 332 333 334 335 336 337 338 339 340
            if (type == Type.VOID_TYPE && this.type != Type.VOID_TYPE) {
                if (this.type.getSize() == 2) {
                    v.pop2();
                }
                else {
                    v.pop();
                }
            }
            else {
                coerce(type, v);
            }
341 342 343 344 345 346
        }
    }

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

D
Dmitry Jemerov 已提交
347 348
        public Constant(Object value, Type type) {
            super(type);
349 350 351 352 353
            this.value = value;
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
A
Alex Tkachman 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366
            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 已提交
367
            coerce(type, v);
368
        }
369 370

        @Override
371
        public void condJump(Label label, boolean jumpIfFalse, InstructionAdapter v) {
D
Dmitry Jemerov 已提交
372
            if (value instanceof Boolean) {
A
Alex Tkachman 已提交
373
                boolean boolValue = (Boolean) value;
374 375 376
                if (boolValue ^ jumpIfFalse) {
                    v.goTo(label);
                }
D
Dmitry Jemerov 已提交
377 378 379 380
            }
            else {
                throw new UnsupportedOperationException("don't know how to generate this condjump");
            }
381 382 383
        }
    }

D
Float  
Dmitry Jemerov 已提交
384
    private static class NumberCompare extends StackValue {
D
Dmitry Jemerov 已提交
385
        protected final IElementType opToken;
D
Dmitry Jemerov 已提交
386
        private final Type operandType;
387

D
Dmitry Jemerov 已提交
388 389
        public NumberCompare(IElementType opToken, Type operandType) {
            super(Type.BOOLEAN_TYPE);
390
            this.opToken = opToken;
D
Dmitry Jemerov 已提交
391
            this.operandType = operandType;
392 393 394 395
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
396 397 398
            if (type == Type.VOID_TYPE) {
                return;
            }
399
            if (type != Type.BOOLEAN_TYPE) {
400
                throw new UnsupportedOperationException("don't know how to put a compare as a non-boolean type " + type);
401
            }
D
Dmitry Jemerov 已提交
402
            putAsBoolean(v);
403 404 405
        }

        @Override
406
        public void condJump(Label label, boolean jumpIfFalse, InstructionAdapter v) {
407
            int opcode;
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
            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;
            }
426 427 428
            else {
                throw new UnsupportedOperationException("don't know how to generate this condjump");
            }
D
Dmitry Jemerov 已提交
429
            if (operandType == Type.FLOAT_TYPE || operandType == Type.DOUBLE_TYPE) {
D
Float  
Dmitry Jemerov 已提交
430
                if (opToken == JetTokens.GT || opToken == JetTokens.GTEQ) {
D
Dmitry Jemerov 已提交
431
                    v.cmpg(operandType);
D
Float  
Dmitry Jemerov 已提交
432 433
                }
                else {
D
Dmitry Jemerov 已提交
434
                    v.cmpl(operandType);
D
Float  
Dmitry Jemerov 已提交
435 436
                }
            }
D
Dmitry Jemerov 已提交
437
            else if (operandType == Type.LONG_TYPE) {
D
Dmitry Jemerov 已提交
438 439
                v.lcmp();
            }
D
Float  
Dmitry Jemerov 已提交
440 441 442
            else {
                opcode += (Opcodes.IF_ICMPEQ - Opcodes.IFEQ);
            }
443
            v.visitJumpInsn(opcode, label);
444
        }
445
    }
D
Dmitry Jemerov 已提交
446

D
Dmitry Jemerov 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
    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 已提交
468 469 470 471
    private static class Invert extends StackValue {
        private StackValue myOperand;

        private Invert(StackValue operand) {
A
Alex Tkachman 已提交
472
            super(Type.BOOLEAN_TYPE);
D
Dmitry Jemerov 已提交
473
            myOperand = operand;
A
Alex Tkachman 已提交
474 475
            if(myOperand.type != Type.BOOLEAN_TYPE)
                throw new UnsupportedOperationException("operand of ! must be boolean");
D
Dmitry Jemerov 已提交
476 477 478 479 480 481 482 483 484 485 486
        }

        @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
487 488
        public void condJump(Label label, boolean jumpIfFalse, InstructionAdapter v) {
            myOperand.condJump(label, !jumpIfFalse, v);
D
Dmitry Jemerov 已提交
489 490
        }
    }
491 492

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

A
Alex Tkachman 已提交
495
        public ArrayElement(Type type, boolean unbox) {
496
            super(type);
A
Alex Tkachman 已提交
497
            this.boxed = unbox ? JetTypeMapper.boxType(type) : type;
498 499 500 501
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
A
Alex Tkachman 已提交
502 503
            v.aload(boxed);    // assumes array and index are on the stack
            onStack(boxed).coerce(type, v);
504 505 506
        }

        @Override
507
        public void store(InstructionAdapter v) {
A
Alex Tkachman 已提交
508 509
            onStack(type).coerce(boxed, v);
            v.astore(boxed);
510
        }
511 512

        @Override
513 514
        public void dupReceiver(InstructionAdapter v) {
            v.dup2();   // array and index
515
        }
A
Alex Tkachman 已提交
516 517 518 519

        public int receiverSize() {
            return 2;
        }
520 521
    }

D
Dmitry Jemerov 已提交
522
    private static class CollectionElement extends StackValue {
523 524
        private final Callable getter;
        private final Callable setter;
525
        private final ExpressionCodegen codegen;
S
Stepan Koltsov 已提交
526
        private final GenerationState state;
527
        private final FrameMap frame;
528 529 530 531
        private final ResolvedCall<FunctionDescriptor> resolvedGetCall;
        private final ResolvedCall<FunctionDescriptor> resolvedSetCall;
        private final FunctionDescriptor setterDescriptor;
        private final FunctionDescriptor getterDescriptor;
D
Dmitry Jemerov 已提交
532

S
Stepan Koltsov 已提交
533
        public CollectionElement(Type type, ResolvedCall<FunctionDescriptor> resolvedGetCall, ResolvedCall<FunctionDescriptor> resolvedSetCall, ExpressionCodegen codegen, GenerationState state) {
D
Dmitry Jemerov 已提交
534
            super(type);
535 536
            this.resolvedGetCall = resolvedGetCall;
            this.resolvedSetCall = resolvedSetCall;
S
Stepan Koltsov 已提交
537
            this.state = state;
538 539
            this.setterDescriptor = resolvedSetCall == null ? null : resolvedSetCall.getResultingDescriptor();
            this.getterDescriptor = resolvedGetCall == null ? null : resolvedGetCall.getResultingDescriptor();
540 541
            this.setter = resolvedSetCall == null ? null : codegen.resolveToCallable(setterDescriptor, false);
            this.getter = resolvedGetCall == null ? null : codegen.resolveToCallable(getterDescriptor, false);
542 543
            this.codegen = codegen;
            this.frame = codegen.myFrameMap;
D
Dmitry Jemerov 已提交
544 545 546 547 548 549 550
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
            if (getter == null) {
                throw new UnsupportedOperationException("no getter specified");
            }
551 552 553
            if(getter instanceof CallableMethod)
                ((CallableMethod)getter).invoke(v);
            else
S
Stepan Koltsov 已提交
554
                ((IntrinsicMethod)getter).generate(codegen, v, null, null, null, null, state);
A
Alex Tkachman 已提交
555
            coerce(type, v);
D
Dmitry Jemerov 已提交
556 557 558 559 560 561 562
        }

        @Override
        public void store(InstructionAdapter v) {
            if (setter == null) {
                throw new UnsupportedOperationException("no setter specified");
            }
563 564 565 566 567 568 569 570 571 572 573
            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();
                }
            }
574
            else
S
Stepan Koltsov 已提交
575
                ((IntrinsicMethod) setter).generate(codegen, v, null, null, null, null, state);
D
Dmitry Jemerov 已提交
576 577
        }

A
Alex Tkachman 已提交
578
        public int receiverSize() {
579
            if(isStandardStack(resolvedGetCall, 1) && isStandardStack(resolvedSetCall, 2)) {
A
Alex Tkachman 已提交
580 581 582 583 584 585 586
                return 2;
            }
            else {
                return -1;
            }
        }

D
Dmitry Jemerov 已提交
587
        @Override
588
        public void dupReceiver(InstructionAdapter v) {
589
            if(isStandardStack(resolvedGetCall, 1) && isStandardStack(resolvedSetCall, 2)) {
590
                v.dup2();   // collection and index
D
Dmitry Jemerov 已提交
591 592
            }
            else {
593 594
                int size = 0;
                int lastIndex = frame.enterTemp();
595 596
                frame.leaveTemp();
                
597 598 599 600
                // indexes
                List<ValueParameterDescriptor> valueParameters = resolvedGetCall.getResultingDescriptor().getValueParameters();
                int firstParamIndex = -1;
                for(int i = valueParameters.size()-1; i >= 0; --i) {
601
                    Type type = codegen.typeMapper.mapType(valueParameters.get(i).getType(), MapTypeMode.VALUE);
602
                    int sz = type.getSize();
603 604
                    frame.enterTemp(sz);
                    lastIndex += sz;
605 606 607 608 609 610 611 612 613 614 615 616 617
                    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);
                    }
618 619
                }

620 621 622
                ReceiverDescriptor receiverParameter = resolvedGetCall.getReceiverArgument();
                int receiverIndex = -1;
                if(receiverParameter.exists()) {
623
                    Type type = codegen.typeMapper.mapType(receiverParameter.getType(), MapTypeMode.VALUE);
624 625 626 627 628 629 630 631 632 633
                    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()) {
634 635
                    frame.enterTemp();
                    lastIndex++;
636 637 638 639 640 641 642 643 644 645 646
                    size++;
                    v.store((thisIndex = lastIndex)-1, JetTypeMapper.TYPE_OBJECT);
                }
                
                // for setter

                int  realReceiverIndex;
                Type realReceiverType;
                if(thisIndex != -1) {
                    if(receiverIndex != -1) {
                        realReceiverIndex = receiverIndex;
647
                        realReceiverType =  codegen.typeMapper.mapType(receiverParameter.getType(), MapTypeMode.VALUE);
648 649 650 651 652 653 654 655
                    }
                    else {
                        realReceiverIndex = thisIndex;
                        realReceiverType = JetTypeMapper.TYPE_OBJECT;
                    }
                }
                else {
                    if(receiverIndex != -1) {
656
                        realReceiverType =  codegen.typeMapper.mapType(receiverParameter.getType(), MapTypeMode.VALUE);
657 658 659 660 661
                        realReceiverIndex = receiverIndex;
                    }
                    else {
                        throw new UnsupportedOperationException();
                    }
662 663
                }

664 665
                if(resolvedSetCall.getThisObject().exists()) {
                    if(resolvedSetCall.getReceiverArgument().exists()) {
666
                        codegen.generateFromResolvedCall(resolvedSetCall.getThisObject(), JetTypeMapper.TYPE_OBJECT);
667 668 669 670 671 672 673 674 675 676
                    }
                    v.load(realReceiverIndex - realReceiverType.getSize(), realReceiverType);
                }
                else {
                    if(resolvedSetCall.getReceiverArgument().exists()) {
                        v.load(realReceiverIndex - realReceiverType.getSize(), realReceiverType);
                    }
                    else {
                        throw new UnsupportedOperationException();
                    }
677 678
                }

679 680
                int index = firstParamIndex;
                for(int i = 0; i != valueParameters.size(); ++i) {
681
                    Type type = codegen.typeMapper.mapType(valueParameters.get(i).getType(), MapTypeMode.VALUE);
682 683 684
                    int sz = type.getSize();
                    v.load(index-sz, type);
                    index -= sz;
685 686
                }

687 688 689 690 691 692
                // restoring original
                if(thisIndex != -1) {
                    v.load(thisIndex-1, JetTypeMapper.TYPE_OBJECT);
                }
                
                if(receiverIndex != -1) {
693
                    Type type = codegen.typeMapper.mapType(receiverParameter.getType(), MapTypeMode.VALUE);
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
                    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) {
709
                    Type type = codegen.typeMapper.mapType(valueParameters.get(i).getType(), MapTypeMode.VALUE);
710 711 712
                    int sz = type.getSize();
                    v.load(index-sz, type);
                    index -= sz;
713 714 715
                }

                frame.leaveTemp(size);
D
Dmitry Jemerov 已提交
716 717
            }
        }
718

719
        private boolean isStandardStack(ResolvedCall call, int valueParamsSize) {
A
Alex Tkachman 已提交
720 721 722
            if(call == null)
                return true;

723 724 725 726 727
            for (TypeParameterDescriptor typeParameterDescriptor : call.getResultingDescriptor().getTypeParameters()) {
                if(typeParameterDescriptor.isReified())
                    return false;
            }

728 729
            List<ValueParameterDescriptor> valueParameters = call.getResultingDescriptor().getValueParameters();
            if(valueParameters.size() != valueParamsSize)
730 731
                return false;

732
            for (ValueParameterDescriptor valueParameter : valueParameters) {
733
                if (codegen.typeMapper.mapType(valueParameter.getType(), MapTypeMode.VALUE).getSize() != 1)
734 735 736
                    return false;
            }

737 738 739 740 741
            if(call.getThisObject().exists()) {
                if(call.getReceiverArgument().exists())
                    return false;
            }
            else {
742
                if(codegen.typeMapper.mapType(call.getResultingDescriptor().getReceiverParameter().getType(), MapTypeMode.VALUE).getSize() != 1)
743 744 745 746
                    return false;
            }

            return true;
747
        }
D
Dmitry Jemerov 已提交
748 749
    }

M
Maxim Shafirov 已提交
750

A
Alex Tkachman 已提交
751 752 753
    static class Field extends StackValue {
        final String owner;
        final String name;
754 755 756 757 758 759 760 761 762 763 764 765
        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());
766
            coerce(type, v);
767 768 769
        }

        @Override
770
        public void dupReceiver(InstructionAdapter v) {
D
Dmitry Jemerov 已提交
771
            if (!isStatic) {
772
                v.dup();
D
Dmitry Jemerov 已提交
773
            }
774 775
        }

A
Alex Tkachman 已提交
776 777 778 779
        public int receiverSize() {
            return isStatic ? 0 : 1;
        }

780 781
        @Override
        public void store(InstructionAdapter v) {
782 783 784
            v.visitFieldInsn(isStatic ? Opcodes.PUTSTATIC : Opcodes.PUTFIELD, owner, name, this.type.getDescriptor());
        }
    }
785

786
    static class Property extends StackValue {
787
        @NotNull
788
        private final String name;
789
        @Nullable
790
        private final Method getter;
791
        @Nullable
792
        private final Method setter;
793 794 795 796
        @NotNull
        public final String methodOwner;
        @NotNull
        private final String methodOwnerParam;
797
        private final boolean isStatic;
798
        private final boolean isInterface;
799 800
        private final boolean isSuper;
        private final int invokeOpcode;
801

802
        public Property(@NotNull String name, @NotNull String methodOwner, @NotNull String methodOwnerParam, Method getter, Method setter, boolean aStatic, boolean isInterface, boolean isSuper, Type type, int invokeOpcode) {
803 804
            super(type);
            this.name = name;
805 806
            this.methodOwner = methodOwner;
            this.methodOwnerParam = methodOwnerParam;
807 808
            this.getter = getter;
            this.setter = setter;
809
            isStatic = aStatic;
810
            this.isInterface = isInterface;
A
Alex Tkachman 已提交
811
            this.isSuper = isSuper;
812 813 814 815 816 817
            this.invokeOpcode = invokeOpcode;
            if (invokeOpcode == 0) {
                if (setter != null || getter != null) {
                    throw new IllegalArgumentException();
                }
            }
818 819 820 821
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
A
Alex Tkachman 已提交
822
            if(isSuper && isInterface) {
823
                v.visitMethodInsn(Opcodes.INVOKESTATIC, methodOwner, getter.getName(), getter.getDescriptor().replace("(", "(L" + methodOwnerParam + ";"));
824 825
            }
            else {
A
Alex Tkachman 已提交
826
            if (getter == null) {
827
                v.visitFieldInsn(isStatic ? Opcodes.GETSTATIC : Opcodes.GETFIELD, methodOwner, name, this.type.getDescriptor());
A
Alex Tkachman 已提交
828 829
            }
            else {
830
                v.visitMethodInsn(invokeOpcode, methodOwner, getter.getName(), getter.getDescriptor());
A
Alex Tkachman 已提交
831
            }
832
            }
833
            coerce(type, v);
834 835 836 837
        }

        @Override
        public void store(InstructionAdapter v) {
A
Alex Tkachman 已提交
838
            if(isSuper && isInterface) {
839
                v.visitMethodInsn(Opcodes.INVOKESTATIC, methodOwner, setter.getName(), setter.getDescriptor().replace("(", "(L" + methodOwnerParam + ";"));
840 841
            }
            else {
A
Alex Tkachman 已提交
842
            if (setter == null) {
843
                v.visitFieldInsn(isStatic ? Opcodes.PUTSTATIC : Opcodes.PUTFIELD, methodOwner, name, this.type.getDescriptor());
A
Alex Tkachman 已提交
844 845
            }
            else {
846
                v.visitMethodInsn(invokeOpcode, methodOwner, setter.getName(), setter.getDescriptor());
A
Alex Tkachman 已提交
847
                }
848 849
            }
        }
850 851

        @Override
852
        public void dupReceiver(InstructionAdapter v) {
853
            if (!isStatic) {
854
                v.dup();
855 856
            }
        }
A
Alex Tkachman 已提交
857 858 859 860

        public int receiverSize() {
            return isStatic ? 0 : 1;
        }
861
    }
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877

    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 已提交
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 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924

    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 已提交
925 926 927
            case Type.LONG:
                return JetTypeMapper.TYPE_SHARED_LONG;

A
Alex Tkachman 已提交
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
            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
960 961
        public void dupReceiver(InstructionAdapter v) {
            v.dup();
A
Alex Tkachman 已提交
962 963
        }

A
Alex Tkachman 已提交
964 965 966 967
        public int receiverSize() {
            return 1;
        }

A
Alex Tkachman 已提交
968 969 970 971 972 973 974 975 976 977 978 979 980 981
        @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());
        }
    }
982

A
Alex Tkachman 已提交
983 984 985
    public static class Composed extends StackValue {
        public final StackValue prefix;
        public final StackValue suffix;
986 987 988 989 990 991 992 993 994 995 996 997

        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 已提交
998 999 1000 1001 1002 1003

        @Override
        public void store(InstructionAdapter v) {
            prefix.put(JetTypeMapper.TYPE_OBJECT, v);
            suffix.store(v);
        }
1004
    }
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020

    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 已提交
1021 1022 1023 1024 1025 1026 1027 1028 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

    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);
            }
        }
    }
1061 1062 1063 1064 1065

    public static class CallReceiver extends StackValue {
        private ResolvedCall<? extends CallableDescriptor> resolvedCall;
        StackValue receiver;
        private ExpressionCodegen codegen;
S
Stepan Koltsov 已提交
1066 1067
        @NotNull
        private final GenerationState state;
1068 1069
        private CallableMethod callableMethod;

S
Stepan Koltsov 已提交
1070
        public CallReceiver(ResolvedCall<? extends CallableDescriptor> resolvedCall, StackValue receiver, ExpressionCodegen codegen, @NotNull GenerationState state, CallableMethod callableMethod) {
1071 1072 1073 1074
            super(calcType(resolvedCall, codegen, callableMethod));
            this.resolvedCall = resolvedCall;
            this.receiver = receiver;
            this.codegen = codegen;
S
Stepan Koltsov 已提交
1075
            this.state = state;
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
            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()) {
1088
                        return codegen.typeMapper.mapType(callableMethod.getReceiverClass(), MapTypeMode.VALUE);
1089 1090
                    }
                    else {
1091
                        return codegen.typeMapper.mapType(callableMethod.getThisType(), MapTypeMode.VALUE);
1092 1093 1094 1095
                    }
                }
                else {
                    if(receiverArgument.exists()) {
1096
                        return codegen.typeMapper.mapType(descriptor.getReceiverParameter().getType(), MapTypeMode.VALUE);
1097 1098
                    }
                    else {
1099
                        return codegen.typeMapper.mapType(descriptor.getExpectedThisObject().getType(), MapTypeMode.VALUE);
1100 1101 1102 1103 1104 1105
                    }
                }
            }
            else {
                if (receiverArgument.exists()) {
                    if(callableMethod != null)
1106
                        return codegen.typeMapper.mapType(callableMethod.getReceiverClass(), MapTypeMode.VALUE);
1107
                    else
1108
                        return codegen.typeMapper.mapType(descriptor.getReceiverParameter().getType(), MapTypeMode.VALUE);
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
                }
                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()) {
1124 1125 1126 1127 1128 1129
                    if (callableMethod != null) {
                        codegen.generateFromResolvedCall(thisObject, Type.getObjectType(callableMethod.getOwner()));
                    }
                    else {
                        codegen.generateFromResolvedCall(thisObject, codegen.typeMapper.mapType(descriptor.getExpectedThisObject().getType(), MapTypeMode.VALUE));
                    }
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
                    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) {
1146
                    Type receiverType = codegen.typeMapper.mapType(receiverParameter.getType(), MapTypeMode.VALUE);
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
                    codegen.generateFromResolvedCall(receiverArgument, receiverType);
                    StackValue.onStack(receiverType).put(type, v);
                }
                else {
                    codegen.generateFromResolvedCall(receiverArgument, type);
                }
            }
            else {
                receiver.put(type, v);
            }
        }
    }
1159
}