StackValue.java 20.3 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 14 15

/**
 * @author yole
 */
public abstract class StackValue {
16
    public final Type type;
D
Dmitry Jemerov 已提交
17 18 19

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

    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 已提交
32 33
    }

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

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

D
Dmitry Jemerov 已提交
40
    public void dupReceiver(InstructionAdapter v, int below) {
41 42
    }

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

    public static StackValue onStack(Type type) {
63
        return type == Type.VOID_TYPE ? none() : new OnStack(type);
64 65
    }

D
Dmitry Jemerov 已提交
66 67
    public static StackValue constant(Object value, Type type) {
        return new Constant(value, type);
68 69
    }

70
    public static StackValue cmp(IElementType opToken, Type type) {
D
Dmitry Jemerov 已提交
71
        return type.getSort() == Type.OBJECT ? new ObjectCompare(opToken, type) : new NumberCompare(opToken, type);
72 73
    }

D
Dmitry Jemerov 已提交
74 75 76 77
    public static StackValue not(StackValue stackValue) {
        return new Invert(stackValue);
    }

A
Alex Tkachman 已提交
78 79
    public static StackValue arrayElement(Type type, boolean unbox) {
        return new ArrayElement(type, unbox);
80 81
    }

D
Dmitry Jemerov 已提交
82 83 84 85
    public static StackValue collectionElement(Type type, CallableMethod getter, CallableMethod setter) {
        return new CollectionElement(type, getter, setter);
    }

86 87 88
    public static StackValue field(Type type, String owner, String name, boolean isStatic) {
        return new Field(type, owner, name, isStatic);
    }
89

M
Maxim Shafirov 已提交
90 91 92 93
    public static StackValue instanceField(Type type, String owner, String name) {
        return new InstanceField(type, owner, name);
    }

94 95
    public static StackValue property(String name, String owner, Type type, boolean isStatic, boolean isInterface, Method getter, Method setter) {
        return new Property(name, owner, getter, setter, isStatic, isInterface, type);
96 97
    }

98 99 100 101
    public static StackValue expression(Type type, JetExpression expression, ExpressionCodegen generator) {
        return new Expression(type, expression, generator);
    }

102 103 104
    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 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
            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;");
        }
    }

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    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 已提交
157
    public void upcast(Type type, InstructionAdapter v) {
M
Maxim Shafirov 已提交
158 159 160 161 162
        if (type.equals(this.type)) return;

        if (type.getSort() == Type.OBJECT && this.type.getSort() == Type.OBJECT) {
            v.checkcast(type);
        }
M
Maxim Shafirov 已提交
163 164 165 166 167 168 169 170
        else {
            coerce(type, v);
        }
    }

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

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

D
Dmitry Jemerov 已提交
214 215 216
    protected void putAsBoolean(InstructionAdapter v) {
        Label ifTrue = new Label();
        Label end = new Label();
217
        condJump(ifTrue, false, v);
D
Dmitry Jemerov 已提交
218 219 220 221 222 223 224
        v.iconst(0);
        v.goTo(end);
        v.mark(ifTrue);
        v.iconst(1);
        v.mark(end);
    }

225 226 227 228 229 230 231 232 233 234 235 236
    public static StackValue none() {
        return None.INSTANCE;
    }
    
    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 已提交
237
            coerce(type, v);
238 239 240
        }
    }

241 242 243
    public static class Local extends StackValue {
        private final int index;

D
Dmitry Jemerov 已提交
244
        public Local(int index, Type type) {
D
Dmitry Jemerov 已提交
245
            super(type);
246 247 248 249 250
            this.index = index;
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
D
Dmitry Jemerov 已提交
251
            v.load(index, this.type);
D
Dmitry Jemerov 已提交
252
            coerce(type, v);
D
Dmitry Jemerov 已提交
253
            // TODO unbox
254
        }
255 256

        @Override
257
        public void store(InstructionAdapter v) {
258
            v.store(index, this.type);
259
        }
260 261 262 263
    }

    public static class OnStack extends StackValue {
        public OnStack(Type type) {
D
Dmitry Jemerov 已提交
264
            super(type);
265 266 267 268
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
269 270 271 272 273 274 275 276 277 278 279
            if (type == Type.VOID_TYPE && this.type != Type.VOID_TYPE) {
                if (this.type.getSize() == 2) {
                    v.pop2();
                }
                else {
                    v.pop();
                }
            }
            else {
                coerce(type, v);
            }
280 281 282 283 284 285
        }
    }

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

D
Dmitry Jemerov 已提交
286 287
        public Constant(Object value, Type type) {
            super(type);
288 289 290 291 292
            this.value = value;
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
A
Alex Tkachman 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305
            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 已提交
306
            coerce(type, v);
307
        }
308 309

        @Override
310
        public void condJump(Label label, boolean jumpIfFalse, InstructionAdapter v) {
D
Dmitry Jemerov 已提交
311
            if (value instanceof Boolean) {
A
Alex Tkachman 已提交
312
                boolean boolValue = (Boolean) value;
313 314 315
                if (boolValue ^ jumpIfFalse) {
                    v.goTo(label);
                }
D
Dmitry Jemerov 已提交
316 317 318 319
            }
            else {
                throw new UnsupportedOperationException("don't know how to generate this condjump");
            }
320 321 322
        }
    }

D
Float  
Dmitry Jemerov 已提交
323
    private static class NumberCompare extends StackValue {
D
Dmitry Jemerov 已提交
324
        protected final IElementType opToken;
D
Dmitry Jemerov 已提交
325
        private final Type operandType;
326

D
Dmitry Jemerov 已提交
327 328
        public NumberCompare(IElementType opToken, Type operandType) {
            super(Type.BOOLEAN_TYPE);
329
            this.opToken = opToken;
D
Dmitry Jemerov 已提交
330
            this.operandType = operandType;
331 332 333 334
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
335 336 337
            if (type == Type.VOID_TYPE) {
                return;
            }
338
            if (type != Type.BOOLEAN_TYPE) {
339
                throw new UnsupportedOperationException("don't know how to put a compare as a non-boolean type " + type);
340
            }
D
Dmitry Jemerov 已提交
341
            putAsBoolean(v);
342 343 344
        }

        @Override
345
        public void condJump(Label label, boolean jumpIfFalse, InstructionAdapter v) {
346
            int opcode;
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
            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;
            }
365 366 367
            else {
                throw new UnsupportedOperationException("don't know how to generate this condjump");
            }
D
Dmitry Jemerov 已提交
368
            if (operandType == Type.FLOAT_TYPE || operandType == Type.DOUBLE_TYPE) {
D
Float  
Dmitry Jemerov 已提交
369
                if (opToken == JetTokens.GT || opToken == JetTokens.GTEQ) {
D
Dmitry Jemerov 已提交
370
                    v.cmpg(operandType);
D
Float  
Dmitry Jemerov 已提交
371 372
                }
                else {
D
Dmitry Jemerov 已提交
373
                    v.cmpl(operandType);
D
Float  
Dmitry Jemerov 已提交
374 375
                }
            }
D
Dmitry Jemerov 已提交
376
            else if (operandType == Type.LONG_TYPE) {
D
Dmitry Jemerov 已提交
377 378
                v.lcmp();
            }
D
Float  
Dmitry Jemerov 已提交
379 380 381
            else {
                opcode += (Opcodes.IF_ICMPEQ - Opcodes.IFEQ);
            }
382
            v.visitJumpInsn(opcode, label);
383
        }
384
    }
D
Dmitry Jemerov 已提交
385

D
Dmitry Jemerov 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
    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 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
    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
424 425
        public void condJump(Label label, boolean jumpIfFalse, InstructionAdapter v) {
            myOperand.condJump(label, !jumpIfFalse, v);
D
Dmitry Jemerov 已提交
426 427
        }
    }
428 429

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

A
Alex Tkachman 已提交
432
        public ArrayElement(Type type, boolean unbox) {
433
            super(type);
A
Alex Tkachman 已提交
434
            this.boxed = unbox ? JetTypeMapper.boxType(type) : type;
435 436 437 438
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
A
Alex Tkachman 已提交
439 440
            v.aload(boxed);    // assumes array and index are on the stack
            onStack(boxed).coerce(type, v);
441 442 443
        }

        @Override
444
        public void store(InstructionAdapter v) {
A
Alex Tkachman 已提交
445 446
            onStack(type).coerce(boxed, v);
            v.astore(boxed);
447
        }
448 449

        @Override
D
Dmitry Jemerov 已提交
450 451 452 453 454 455 456
        public void dupReceiver(InstructionAdapter v, int below) {
            if (below == 1) {
                v.dup2X1();
            }
            else {
                v.dup2();   // array and index
            }
457
        }
458 459
    }

D
Dmitry Jemerov 已提交
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
    private static class CollectionElement extends StackValue {
        private final CallableMethod getter;
        private final CallableMethod setter;

        public CollectionElement(Type type, CallableMethod getter, CallableMethod setter) {
            super(type);
            this.getter = getter;
            this.setter = setter;
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
            if (getter == null) {
                throw new UnsupportedOperationException("no getter specified");
            }
            getter.invoke(v);
A
Alex Tkachman 已提交
476
            coerce(type, v);
D
Dmitry Jemerov 已提交
477 478 479 480 481 482 483 484 485 486 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
        public void dupReceiver(InstructionAdapter v, int below) {
            if (below == 1) {
                v.dup2X1();
            }
            else {
                v.dup2();   // collection and index
            }
        }
    }

M
Maxim Shafirov 已提交
498

499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
    private static class Field extends StackValue {
        private final String owner;
        private final String name;
        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
D
Dmitry Jemerov 已提交
517 518 519 520 521 522 523 524 525
        public void dupReceiver(InstructionAdapter v, int below) {
            if (!isStatic) {
                if (below == 1) {
                    v.dupX1();
                }
                else {
                    v.dup();
                }
            }
526 527 528 529
        }

        @Override
        public void store(InstructionAdapter v) {
530 531 532
            v.visitFieldInsn(isStatic ? Opcodes.PUTSTATIC : Opcodes.PUTFIELD, owner, name, this.type.getDescriptor());
        }
    }
533

M
Maxim Shafirov 已提交
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
    private static class InstanceField extends StackValue {
        private final String owner;
        private final String name;

        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
        public void dupReceiver(InstructionAdapter v, int below) {
        }

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

561 562 563 564
    private static class Property extends StackValue {
        private final String name;
        private final Method getter;
        private final Method setter;
565
        private final String owner;
566
        private final boolean isStatic;
567
        private final boolean isInterface;
568

569
        public Property(String name, String owner, Method getter, Method setter, boolean aStatic, boolean isInterface, Type type) {
570 571
            super(type);
            this.name = name;
572
            this.owner = owner;
573 574
            this.getter = getter;
            this.setter = setter;
575
            isStatic = aStatic;
576
            this.isInterface = isInterface;
577 578 579 580 581
        }

        @Override
        public void put(Type type, InstructionAdapter v) {
            if (getter == null) {
582
                v.visitFieldInsn(isStatic ? Opcodes.GETSTATIC : Opcodes.GETFIELD, owner, name, this.type.getDescriptor());
583 584
            }
            else {
585
                v.visitMethodInsn(isStatic ? Opcodes.INVOKESTATIC : isInterface ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL, owner, getter.getName(), getter.getDescriptor());
586
            }
587
            coerce(type, v);
588 589 590 591 592
        }

        @Override
        public void store(InstructionAdapter v) {
            if (setter == null) {
593
                v.visitFieldInsn(isStatic ? Opcodes.PUTSTATIC : Opcodes.PUTFIELD, owner, name, this.type.getDescriptor());
594 595
            }
            else {
596
                v.visitMethodInsn(isStatic ? Opcodes.INVOKESTATIC : isInterface ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL, owner, setter.getName(), setter.getDescriptor());
597 598
            }
        }
599 600 601 602 603 604 605 606 607 608 609 610

        @Override
        public void dupReceiver(InstructionAdapter v, int below) {
            if (!isStatic) {
                if (below == 1) {
                    v.dupX1();
                }
                else {
                    v.dup();
                }
            }
        }
611
    }
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627

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