aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/syntax/compiler/VariableStore.java
blob: cd5af7e903ac8ce3e871dd7e6635e3035fd2ef12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
// Copyright 2015 The Bazel Authors. All rights reserved.
//
// 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.
package com.google.devtools.build.lib.syntax.compiler;

import com.google.common.base.Preconditions;
import com.google.devtools.build.lib.syntax.compiler.Variable.InternalVariable;
import com.google.devtools.build.lib.syntax.compiler.Variable.SkylarkVariable;

import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.Implementation.Context;
import net.bytebuddy.implementation.bytecode.ByteCodeAppender;
import net.bytebuddy.implementation.bytecode.StackManipulation.Size;
import net.bytebuddy.implementation.bytecode.StackSize;

import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

// TODO(klaasb) javadoc
enum VariableStore {
  INTEGER(Opcodes.ISTORE, StackSize.SINGLE),
  LONG(Opcodes.LSTORE, StackSize.DOUBLE),
  FLOAT(Opcodes.FSTORE, StackSize.SINGLE),
  DOUBLE(Opcodes.DSTORE, StackSize.DOUBLE),
  REFERENCE(Opcodes.ASTORE, StackSize.SINGLE);

  private final int opCode;
  private final Size size;

  private VariableStore(int opCode, StackSize size) {
    this.opCode = opCode;
    this.size = size.toIncreasingSize();
  }

  // TODO(klaasb) javadoc
  private VariableIndexStore into(int index) {
    return new VariableIndexStore(index);
  }

  // TODO(klaasb) javadoc
  public static VariableIndexStore into(SkylarkVariable variable) {
    return REFERENCE.into(variable.index);
  }

  // TODO(klaasb) javadoc
  public static VariableIndexStore into(InternalVariable variable) {
    return forType(variable.type).into(variable.index);
  }

  // TODO(klaasb) javadoc
  class VariableIndexStore implements ByteCodeAppender {

    private int operandIndex;

    private VariableIndexStore(int operandIndex) {
      this.operandIndex = operandIndex;
    }

    @Override
    public ByteCodeAppender.Size apply(
        MethodVisitor methodVisitor,
        Context implementationContext,
        MethodDescription instrumentedMethod) {
      methodVisitor.visitVarInsn(opCode, operandIndex);
      return new ByteCodeAppender.Size(
          size.getMaximalSize(), Math.max(instrumentedMethod.getStackSize(), operandIndex + 1));
    }

    @Override
    public String toString() {
      return "VariableStore(" + opCode + ", " + operandIndex + ")";
    }
  }

  /**
   * Selects the correct VariableStore value for the given type
   */
  public static VariableStore forType(TypeDescription type) {
    if (type.isPrimitive()) {
      if (type.represents(long.class)) {
        return LONG;
      } else if (type.represents(double.class)) {
        return DOUBLE;
      } else if (type.represents(float.class)) {
        return FLOAT;
      } else {
        Preconditions.checkArgument(
            !type.represents(void.class), "Variables can't be of void type");
        return INTEGER;
      }
    } else {
      return REFERENCE;
    }
  }
}