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

3
import com.intellij.psi.tree.IElementType;
4
import org.jetbrains.jet.lang.psi.JetExpression;
5 6 7
import org.jetbrains.jet.lexer.JetTokens;
import org.objectweb.asm.Label;
import org.objectweb.asm.Opcodes;
8 9
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.InstructionAdapter;
10
import org.objectweb.asm.commons.Method;
11 12 13

/**
 * @author yole
A
Alex Tkachman 已提交
14
 * @author alex.tkachman
15 16
 */
public abstract class StackValue {
17
    public final Type type;
D
Dmitry Jemerov 已提交
18 19 20

    public StackValue(Type type) {
        this.type = type;
21 22 23 24 25 26 27 28 29 30 31 32
    }

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

35 36
    public abstract void put(Type type, InstructionAdapter v);

37
    public void store(InstructionAdapter v) {
38 39 40
        throw new UnsupportedOperationException("cannot store to value " + this);
    }

41
    public void dupReceiver(InstructionAdapter v) {
42 43
    }

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    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 已提交
59
    public static StackValue local(int index, Type type) {
60 61 62
        return new Local(index, type);
    }

A
Alex Tkachman 已提交
63 64 65 66
    public static StackValue shared(int index, Type type) {
        return new Shared(index, type);
    }

67
    public static StackValue onStack(Type type) {
68
        return type == Type.VOID_TYPE ? none() : new OnStack(type);
69 70
    }

D
Dmitry Jemerov 已提交
71 72
    public static StackValue constant(Object value, Type type) {
        return new Constant(value, type);
73 74
    }

75
    public static StackValue cmp(IElementType opToken, Type type) {
D
Dmitry Jemerov 已提交
76
        return type.getSort() == Type.OBJECT ? new ObjectCompare(opToken, type) : new NumberCompare(opToken, type);
77 78
    }

D
Dmitry Jemerov 已提交
79 80 81 82
    public static StackValue not(StackValue stackValue) {
        return new Invert(stackValue);
    }

A
Alex Tkachman 已提交
83 84
    public static StackValue arrayElement(Type type, boolean unbox) {
        return new ArrayElement(type, unbox);
85 86
    }

87 88
    public static StackValue collectionElement(Type type, CallableMethod getter, CallableMethod setter, FrameMap frame) {
        return new CollectionElement(type, getter, setter, frame);
D
Dmitry Jemerov 已提交
89 90
    }

91 92 93
    public static StackValue field(Type type, String owner, String name, boolean isStatic) {
        return new Field(type, owner, name, isStatic);
    }
94

M
Maxim Shafirov 已提交
95 96 97 98
    public static StackValue instanceField(Type type, String owner, String name) {
        return new InstanceField(type, owner, name);
    }

A
Alex Tkachman 已提交
99 100
    public static StackValue property(String name, String owner, Type type, boolean isStatic, boolean isInterface, boolean isSuper, Method getter, Method setter) {
        return new Property(name, owner, getter, setter, isStatic, isInterface, isSuper, type);
101 102
    }

103 104 105 106
    public static StackValue expression(Type type, JetExpression expression, ExpressionCodegen generator) {
        return new Expression(type, expression, generator);
    }

107 108 109
    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 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
            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;");
        }
    }

135 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 161
    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 已提交
162
    public void upcast(Type type, InstructionAdapter v) {
M
Maxim Shafirov 已提交
163 164 165 166 167
        if (type.equals(this.type)) return;

        if (type.getSort() == Type.OBJECT && this.type.getSort() == Type.OBJECT) {
            v.checkcast(type);
        }
M
Maxim Shafirov 已提交
168 169 170 171 172 173 174 175
        else {
            coerce(type, v);
        }
    }

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

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

D
Dmitry Jemerov 已提交
219 220 221
    protected void putAsBoolean(InstructionAdapter v) {
        Label ifTrue = new Label();
        Label end = new Label();
222
        condJump(ifTrue, false, v);
D
Dmitry Jemerov 已提交
223 224 225 226 227 228 229
        v.iconst(0);
        v.goTo(end);
        v.mark(ifTrue);
        v.iconst(1);
        v.mark(end);
    }

230 231 232
    public static StackValue none() {
        return None.INSTANCE;
    }
A
Alex Tkachman 已提交
233 234 235 236 237

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

238 239 240 241
    public static StackValue composed(StackValue prefix, StackValue suffix) {
        return new Composed(prefix, suffix);
    }

242 243 244 245 246 247 248 249
    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 已提交
250
            coerce(type, v);
251 252 253
        }
    }

254 255 256
    public static class Local extends StackValue {
        private final int index;

D
Dmitry Jemerov 已提交
257
        public Local(int index, Type type) {
D
Dmitry Jemerov 已提交
258
            super(type);
259 260 261 262 263
            this.index = index;
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
D
Dmitry Jemerov 已提交
264
            v.load(index, this.type);
D
Dmitry Jemerov 已提交
265
            coerce(type, v);
D
Dmitry Jemerov 已提交
266
            // TODO unbox
267
        }
268 269

        @Override
270
        public void store(InstructionAdapter v) {
271
            v.store(index, this.type);
272
        }
273 274 275 276
    }

    public static class OnStack extends StackValue {
        public OnStack(Type type) {
D
Dmitry Jemerov 已提交
277
            super(type);
278 279 280 281
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
282 283 284 285 286 287 288 289 290 291 292
            if (type == Type.VOID_TYPE && this.type != Type.VOID_TYPE) {
                if (this.type.getSize() == 2) {
                    v.pop2();
                }
                else {
                    v.pop();
                }
            }
            else {
                coerce(type, v);
            }
293 294 295 296 297 298
        }
    }

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

D
Dmitry Jemerov 已提交
299 300
        public Constant(Object value, Type type) {
            super(type);
301 302 303 304 305
            this.value = value;
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
A
Alex Tkachman 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318
            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 已提交
319
            coerce(type, v);
320
        }
321 322

        @Override
323
        public void condJump(Label label, boolean jumpIfFalse, InstructionAdapter v) {
D
Dmitry Jemerov 已提交
324
            if (value instanceof Boolean) {
A
Alex Tkachman 已提交
325
                boolean boolValue = (Boolean) value;
326 327 328
                if (boolValue ^ jumpIfFalse) {
                    v.goTo(label);
                }
D
Dmitry Jemerov 已提交
329 330 331 332
            }
            else {
                throw new UnsupportedOperationException("don't know how to generate this condjump");
            }
333 334 335
        }
    }

D
Float  
Dmitry Jemerov 已提交
336
    private static class NumberCompare extends StackValue {
D
Dmitry Jemerov 已提交
337
        protected final IElementType opToken;
D
Dmitry Jemerov 已提交
338
        private final Type operandType;
339

D
Dmitry Jemerov 已提交
340 341
        public NumberCompare(IElementType opToken, Type operandType) {
            super(Type.BOOLEAN_TYPE);
342
            this.opToken = opToken;
D
Dmitry Jemerov 已提交
343
            this.operandType = operandType;
344 345 346 347
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
348 349 350
            if (type == Type.VOID_TYPE) {
                return;
            }
351
            if (type != Type.BOOLEAN_TYPE) {
352
                throw new UnsupportedOperationException("don't know how to put a compare as a non-boolean type " + type);
353
            }
D
Dmitry Jemerov 已提交
354
            putAsBoolean(v);
355 356 357
        }

        @Override
358
        public void condJump(Label label, boolean jumpIfFalse, InstructionAdapter v) {
359
            int opcode;
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
            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;
            }
378 379 380
            else {
                throw new UnsupportedOperationException("don't know how to generate this condjump");
            }
D
Dmitry Jemerov 已提交
381
            if (operandType == Type.FLOAT_TYPE || operandType == Type.DOUBLE_TYPE) {
D
Float  
Dmitry Jemerov 已提交
382
                if (opToken == JetTokens.GT || opToken == JetTokens.GTEQ) {
D
Dmitry Jemerov 已提交
383
                    v.cmpg(operandType);
D
Float  
Dmitry Jemerov 已提交
384 385
                }
                else {
D
Dmitry Jemerov 已提交
386
                    v.cmpl(operandType);
D
Float  
Dmitry Jemerov 已提交
387 388
                }
            }
D
Dmitry Jemerov 已提交
389
            else if (operandType == Type.LONG_TYPE) {
D
Dmitry Jemerov 已提交
390 391
                v.lcmp();
            }
D
Float  
Dmitry Jemerov 已提交
392 393 394
            else {
                opcode += (Opcodes.IF_ICMPEQ - Opcodes.IFEQ);
            }
395
            v.visitJumpInsn(opcode, label);
396
        }
397
    }
D
Dmitry Jemerov 已提交
398

D
Dmitry Jemerov 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    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 已提交
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
    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
437 438
        public void condJump(Label label, boolean jumpIfFalse, InstructionAdapter v) {
            myOperand.condJump(label, !jumpIfFalse, v);
D
Dmitry Jemerov 已提交
439 440
        }
    }
441 442

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

A
Alex Tkachman 已提交
445
        public ArrayElement(Type type, boolean unbox) {
446
            super(type);
A
Alex Tkachman 已提交
447
            this.boxed = unbox ? JetTypeMapper.boxType(type) : type;
448 449 450 451
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
A
Alex Tkachman 已提交
452 453
            v.aload(boxed);    // assumes array and index are on the stack
            onStack(boxed).coerce(type, v);
454 455 456
        }

        @Override
457
        public void store(InstructionAdapter v) {
A
Alex Tkachman 已提交
458 459
            onStack(type).coerce(boxed, v);
            v.astore(boxed);
460
        }
461 462

        @Override
463 464
        public void dupReceiver(InstructionAdapter v) {
            v.dup2();   // array and index
465
        }
466 467
    }

D
Dmitry Jemerov 已提交
468 469 470
    private static class CollectionElement extends StackValue {
        private final CallableMethod getter;
        private final CallableMethod setter;
471
        private final FrameMap frame;
D
Dmitry Jemerov 已提交
472

473
        public CollectionElement(Type type, CallableMethod getter, CallableMethod setter, FrameMap frame) {
D
Dmitry Jemerov 已提交
474 475 476
            super(type);
            this.getter = getter;
            this.setter = setter;
477
            this.frame = frame;
D
Dmitry Jemerov 已提交
478 479 480 481 482 483 484 485
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
            if (getter == null) {
                throw new UnsupportedOperationException("no getter specified");
            }
            getter.invoke(v);
A
Alex Tkachman 已提交
486
            coerce(type, v);
D
Dmitry Jemerov 已提交
487 488 489 490 491 492 493 494 495 496 497
        }

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

        @Override
498 499 500 501 502
        public void dupReceiver(InstructionAdapter v) {
            int size = calcSize();
            
            if(size == 2) {
                v.dup2();   // collection and index
D
Dmitry Jemerov 已提交
503 504
            }
            else {
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
                Method signature = getter.getSignature();
                Type[] argumentTypes = signature.getArgumentTypes();

                int firstIndex = frame.enterTemp();
                int lastIndex = firstIndex;
                frame.leaveTemp();
                
                for(int i = argumentTypes.length-1; i >= 0; i--) {
                    int sz = argumentTypes[i].getSize();
                    frame.enterTemp(sz);
                    lastIndex += sz;
                    v.store(lastIndex-sz, argumentTypes[i]);
                }

                if(getter.getInvokeOpcode() != Opcodes.INVOKESTATIC) {
                    frame.enterTemp();
                    lastIndex++;
                    v.store(lastIndex-1, JetTypeMapper.TYPE_OBJECT);
                }

                firstIndex = lastIndex;
                int curIndex = lastIndex;
                if(getter.getInvokeOpcode() != Opcodes.INVOKESTATIC) {
                    v.load(curIndex-1, JetTypeMapper.TYPE_OBJECT);
                    curIndex--;
                }

                for(int i = 0; i != argumentTypes.length; i++) {
                    int sz = argumentTypes[i].getSize();
                    v.load(curIndex-sz, argumentTypes[i]);
                    curIndex -= sz;
                }

                curIndex = firstIndex;
                if(getter.getInvokeOpcode() != Opcodes.INVOKESTATIC) {
                    v.load(curIndex-1, JetTypeMapper.TYPE_OBJECT);
                    curIndex--;
                }

                for(int i = 0; i != argumentTypes.length; i++) {
                    int sz = argumentTypes[i].getSize();
                    v.load(curIndex-sz, argumentTypes[i]);
                    curIndex -= sz;
                }

                frame.leaveTemp(size);
D
Dmitry Jemerov 已提交
551 552
            }
        }
553 554 555 556 557 558 559 560 561 562

        private int calcSize() {
            assert getter != null;
            int size = getter.getInvokeOpcode() == Opcodes.INVOKESTATIC ? 0 : 1;
            Method signature = getter.getSignature();
            Type[] argumentTypes = signature.getArgumentTypes();
            for(int i = 0; i != argumentTypes.length; ++i)
                size += argumentTypes[i].getSize();
            return size;
        }
D
Dmitry Jemerov 已提交
563 564
    }

M
Maxim Shafirov 已提交
565

A
Alex Tkachman 已提交
566 567 568
    static class Field extends StackValue {
        final String owner;
        final String name;
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
        private final boolean isStatic;

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

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

        @Override
584
        public void dupReceiver(InstructionAdapter v) {
D
Dmitry Jemerov 已提交
585
            if (!isStatic) {
586
                v.dup();
D
Dmitry Jemerov 已提交
587
            }
588 589 590 591
        }

        @Override
        public void store(InstructionAdapter v) {
592 593 594
            v.visitFieldInsn(isStatic ? Opcodes.PUTSTATIC : Opcodes.PUTFIELD, owner, name, this.type.getDescriptor());
        }
    }
595

A
Alex Tkachman 已提交
596 597 598
    static class InstanceField extends StackValue {
        final String owner;
        final String name;
M
Maxim Shafirov 已提交
599 600 601 602 603 604 605 606 607 608 609 610 611 612

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

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

        @Override
613
        public void dupReceiver(InstructionAdapter v) {
M
Maxim Shafirov 已提交
614 615 616 617 618 619 620 621 622
        }

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

623 624 625 626
    private static class Property extends StackValue {
        private final String name;
        private final Method getter;
        private final Method setter;
627
        private final String owner;
628
        private final boolean isStatic;
629
        private final boolean isInterface;
A
Alex Tkachman 已提交
630
        private boolean isSuper;
631

A
Alex Tkachman 已提交
632
        public Property(String name, String owner, Method getter, Method setter, boolean aStatic, boolean isInterface, boolean isSuper, Type type) {
633 634
            super(type);
            this.name = name;
635
            this.owner = owner;
636 637
            this.getter = getter;
            this.setter = setter;
638
            isStatic = aStatic;
639
            this.isInterface = isInterface;
A
Alex Tkachman 已提交
640
            this.isSuper = isSuper;
641 642 643 644
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
A
Alex Tkachman 已提交
645 646
            if(isSuper && isInterface) {
                v.visitMethodInsn(Opcodes.INVOKESTATIC, owner + "$$TImpl", getter.getName(), getter.getDescriptor().replace("(","(L" + owner + ";"));
647 648
            }
            else {
A
Alex Tkachman 已提交
649 650 651 652
            if (getter == null) {
                v.visitFieldInsn(isStatic ? Opcodes.GETSTATIC : Opcodes.GETFIELD, owner, name, this.type.getDescriptor());
            }
            else {
A
Alex Tkachman 已提交
653
                    v.visitMethodInsn(isStatic ? Opcodes.INVOKESTATIC : isSuper ? Opcodes.INVOKESPECIAL : isInterface ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL, owner, getter.getName(), getter.getDescriptor());
A
Alex Tkachman 已提交
654
            }
655
            }
656
            coerce(type, v);
657 658 659 660
        }

        @Override
        public void store(InstructionAdapter v) {
A
Alex Tkachman 已提交
661 662
            if(isSuper && isInterface) {
                v.visitMethodInsn(Opcodes.INVOKESTATIC, owner + "$$TImpl", setter.getName(), setter.getDescriptor().replace("(","(L" + owner + ";"));
663 664
            }
            else {
A
Alex Tkachman 已提交
665 666 667 668
            if (setter == null) {
                v.visitFieldInsn(isStatic ? Opcodes.PUTSTATIC : Opcodes.PUTFIELD, owner, name, this.type.getDescriptor());
            }
            else {
A
Alex Tkachman 已提交
669 670
                    v.visitMethodInsn(isStatic ? Opcodes.INVOKESTATIC : isSuper ? Opcodes.INVOKESPECIAL : isInterface ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL, owner, setter.getName(), setter.getDescriptor());
                }
671 672
            }
        }
673 674

        @Override
675
        public void dupReceiver(InstructionAdapter v) {
676
            if (!isStatic) {
677
                v.dup();
678 679
            }
        }
680
    }
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696

    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 已提交
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743

    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 已提交
744 745 746
            case Type.LONG:
                return JetTypeMapper.TYPE_SHARED_LONG;

A
Alex Tkachman 已提交
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
            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
779 780
        public void dupReceiver(InstructionAdapter v) {
            v.dup();
A
Alex Tkachman 已提交
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
        }

        @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());
        }
    }
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813

    private static class Composed extends StackValue {
        private StackValue prefix;
        private StackValue suffix;

        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);
        }
    }
814
}