ExpressionCodegen.java 9.0 KB
Newer Older
M
Maxim Shafirov 已提交
1 2 3
package org.jetbrains.jet.codegen;

import org.jetbrains.jet.lang.psi.*;
4 5
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.types.DeclarationDescriptor;
M
Maxim Shafirov 已提交
6 7
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
D
Dmitry Jemerov 已提交
8
import org.objectweb.asm.Opcodes;
M
Maxim Shafirov 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.InstructionAdapter;

import java.util.List;
import java.util.Stack;

/**
 * @author max
 */
public class ExpressionCodegen extends JetVisitor {
    private final Stack<Label> myLoopStarts = new Stack<Label>();
    private final Stack<Label> myLoopEnds = new Stack<Label>();

    private final InstructionAdapter v;
23
    private final FrameMap myMap;
24
    private final BindingContext bindingContext;
M
Maxim Shafirov 已提交
25

26 27
    public ExpressionCodegen(MethodVisitor v, BindingContext bindingContext, FrameMap myMap) {
        this.myMap = myMap;
M
Maxim Shafirov 已提交
28
        this.v = new InstructionAdapter(v);
29
        this.bindingContext = bindingContext;
M
Maxim Shafirov 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 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 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 157 158 159 160 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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    }

    private void gen(JetElement expr) {
        if (expr == null) throw new CompilationException();
        expr.accept(this);
    }

    @Override
    public void visitExpression(JetExpression expression) {
        throw new UnsupportedOperationException("Codegen for " + expression + " is not yet implemented");
    }

    @Override
    public void visitParenthesizedExpression(JetParenthesizedExpression expression) {
        gen(expression.getExpression());
    }

    @Override
    public void visitAnnotatedExpression(JetAnnotatedExpression expression) {
        gen(expression.getBaseExpression());
    }

    @Override
    public void visitIfExpression(JetIfExpression expression) {
        gen(expression.getCondition());
        unboxBoolean();

        JetExpression thenExpression = expression.getThen();
        JetExpression elseExpression = expression.getElse();

        if (thenExpression == null && elseExpression == null) {
            throw new CompilationException();
        }

        if (thenExpression == null) {
            generateSingleBranchIf(elseExpression, false);
            return;
        }

        if (elseExpression == null) {
            generateSingleBranchIf(thenExpression, true);
            return;
        }


        Label elseLabel = new Label();
        v.ifeq(elseLabel); // == 0, i.e. false

        gen(thenExpression);

        Label endLabel = new Label();
        v.goTo(endLabel);
        v.mark(elseLabel);

        gen(elseExpression);

        v.mark(endLabel);
    }

    @Override
    public void visitWhileExpression(JetWhileExpression expression) {
        Label condition = new Label();
        myLoopStarts.push(condition);
        v.mark(condition);

        Label end = new Label();
        myLoopEnds.push(end);

        gen(expression.getCondition());
        unboxBoolean();
        v.ifeq(end);

        genToUnit(expression.getBody());
        v.goTo(condition);

        v.mark(end);
        myLoopEnds.pop();
        myLoopStarts.pop();
    }

    @Override
    public void visitDoWhileExpression(JetDoWhileExpression expression) {
        Label condition = new Label();
        v.mark(condition);
        myLoopStarts.push(condition);

        Label end = new Label();
        myLoopEnds.push(end);

        genToUnit(expression.getBody());

        gen(expression.getCondition());
        unboxBoolean();
        v.ifne(condition);

        v.mark(end);

        myLoopEnds.pop();
        myLoopStarts.pop();
    }

    @Override
    public void visitBreakExpression(JetBreakExpression expression) {
        String labelName = expression.getLabelName();

        Label label = labelName == null ? myLoopEnds.peek() : null; // TODO:

        v.goTo(label);
    }

    @Override
    public void visitContinueExpression(JetContinueExpression expression) {
        String labelName = expression.getLabelName();

        Label label = labelName == null ? myLoopStarts.peek() : null; // TODO:

        v.goTo(label);
    }

    private void unboxBoolean() {
        v.invokevirtual("java/lang/Boolean", "booleanValue", "()Z");
    }

    private void generateSingleBranchIf(JetExpression expression, boolean inverse) {
        Label endLabel = new Label();

        if (inverse) {
            v.ifeq(endLabel);
        }
        else {
            v.ifne(endLabel);
        }

        genToUnit(expression);

        v.mark(endLabel);
    }

    private void genToUnit(JetExpression expression) {
        gen(expression);

        if (!isUnitType(expression)) {
            v.pop();
        }
    }

    @Override
    public void visitConstantExpression(JetConstantExpression expression) {
        Object value = expression.getValue();
        v.aconst(value);

        if (value instanceof Integer) {
            v.invokestatic("java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
        }
        else if (value instanceof Boolean) {
            v.invokestatic("java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;");
        }
        else if (value instanceof Character) {
            v.invokestatic("java/lang/Character", "valueOf", "(C)Ljava/lang/Character;");
        }
        else if (value instanceof Short) {
            v.invokestatic("java/lang/Short", "valueOf", "(S)Ljava/lang/Short;");
        }
        else if (value instanceof Long) {
            v.invokestatic("java/lang/Long", "valueOf", "(J)Ljava/lang/Long;");
        }
        else if (value instanceof Byte) {
            v.invokestatic("java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;");
        }
        else if (value instanceof Float) {
            v.invokestatic("java/lang/Float", "valueOf", "(F)Ljava/lang/Float;");
        }
        else if (value instanceof Double) {
            v.invokestatic("java/lang/Double", "valueOf", "(D)Ljava/lang/Double;");
        }
    }

    @Override
    public void visitBlockExpression(JetBlockExpression expression) {
        Label blockStart = new Label();
        v.mark(blockStart);

        List<JetElement> statements = expression.getStatements();
        for (JetElement statement : statements) {
            if (statement instanceof JetProperty) {
215
                myMap.enter(bindingContext.getPropertyDescriptor((JetProperty) statement));
M
Maxim Shafirov 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228
            }
        }

        for (JetElement statement : statements) {
            gen(statement);
        }

        Label blockEnd = new Label();
        v.mark(blockEnd);

        for (JetElement statement : statements) {
            if (statement instanceof JetProperty) {
                JetProperty var = (JetProperty) statement;
229
                int index = myMap.leave(bindingContext.getPropertyDescriptor(var));
M
Maxim Shafirov 已提交
230 231 232 233 234 235
                v.visitLocalVariable(var.getName(), getType(var).getDescriptor(), null, blockStart, blockEnd, index);
            }
        }

    }

D
Dmitry Jemerov 已提交
236 237 238 239 240 241 242 243 244 245 246 247
    @Override
    public void visitReturnExpression(JetReturnExpression expression) {
        final JetExpression returnedExpression = expression.getReturnedExpression();
        if (returnedExpression != null) {
            returnedExpression.accept(this);
            v.visitInsn(Opcodes.ARETURN);
        }
        else {
            v.visitInsn(Opcodes.RETURN);
        }
    }

248
    @Override
249
    public void visitSimpleNameExpression(JetSimpleNameExpression expression) {
A
Andrey Breslav 已提交
250
        final DeclarationDescriptor descriptor = bindingContext.resolveReferenceExpression(expression);
251 252 253
        int index = myMap.getIndex(descriptor);
        if (index >= 0) {
            v.visitVarInsn(Opcodes.ALOAD, index);
254 255 256 257 258 259
        }
        else {
            throw new UnsupportedOperationException("don't know how to generate reference " + descriptor);
        }
    }

260 261 262 263 264 265 266 267 268 269
/*
    @Override
    public void visitCallExpression(JetCallExpression expression) {
        List<JetArgument> args = expression.getValueArguments();
        for (JetArgument arg : args) {
            gen(arg.getArgumentExpression());
        }

        JetExpression callee = expression.getCalleeExpression();

270 271
        if (callee instanceof JetSimpleNameExpression) {
            DeclarationDescriptor funDescriptor = bindingContext.resolveReferenceExpression((JetSimpleNameExpression) callee);
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
            if (funDescriptor instanceof FunctionDescriptor) {
            }
            else {
                throw new CompilationException();
            }
        }
        else {
            throw new UnsupportedOperationException("Don't know how to generate a call");
        }
    }

    @Override
    public void visitBinaryExpression(JetBinaryExpression expression) {
    }
*/

M
Maxim Shafirov 已提交
288 289 290 291 292 293 294 295 296 297
    private Type getType(JetProperty var) {
        return InstructionAdapter.OBJECT_TYPE;  // TODO:
    }

    private boolean isUnitType(JetExpression e) {
        return false; // TODO:!!!
    }

    @Override
    public void visitProperty(JetProperty property) {
298
        int index = myMap.getIndex(bindingContext.getPropertyDescriptor(property));
M
Maxim Shafirov 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311

        assert index >= 0;

        JetExpression initializer = property.getInitializer();
        if (initializer != null) {
            gen(initializer);
            v.store(index, getType(property));
        }
    }

    private static class CompilationException extends RuntimeException {
    }
}