aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools
diff options
context:
space:
mode:
authorGravatar Klaas Boesche <klaasb@google.com>2015-11-06 14:01:52 +0000
committerGravatar Florian Weikert <fwe@google.com>2015-11-06 16:40:04 +0000
commita9855c7204543b64c6f1e0f3cb2a82ebd5c4947d (patch)
tree25ce4a880ca05dea7c1240ccbe388543377aeb53 /src/main/java/com/google/devtools
parentb2e8e9ec5cc72ebdefebdb5f8459e768bd927e28 (diff)
Compile int and string literals to byte code
-- MOS_MIGRATED_REVID=107227795
Diffstat (limited to 'src/main/java/com/google/devtools')
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/IntegerLiteral.java14
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/StringLiteral.java11
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/compiler/ByteCodeMethodCalls.java35
3 files changed, 60 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/IntegerLiteral.java b/src/main/java/com/google/devtools/build/lib/syntax/IntegerLiteral.java
index be9ce536e3..942f3370f7 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/IntegerLiteral.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/IntegerLiteral.java
@@ -13,6 +13,13 @@
// limitations under the License.
package com.google.devtools.build.lib.syntax;
+import com.google.devtools.build.lib.syntax.compiler.ByteCodeMethodCalls;
+import com.google.devtools.build.lib.syntax.compiler.DebugInfo;
+import com.google.devtools.build.lib.syntax.compiler.VariableScope;
+
+import net.bytebuddy.implementation.bytecode.ByteCodeAppender;
+import net.bytebuddy.implementation.bytecode.constant.IntegerConstant;
+
/**
* Syntax node for an integer literal.
*/
@@ -30,4 +37,11 @@ public final class IntegerLiteral extends Literal<Integer> {
@Override
void validate(ValidationEnvironment env) throws EvalException {
}
+
+ @Override
+ ByteCodeAppender compile(VariableScope scope, DebugInfo debugInfo) {
+ return new ByteCodeAppender.Simple(
+ IntegerConstant.forValue(value),
+ ByteCodeMethodCalls.BCInteger.valueOf);
+ }
}
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/StringLiteral.java b/src/main/java/com/google/devtools/build/lib/syntax/StringLiteral.java
index b2526a0646..cd21c1eb6f 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/StringLiteral.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/StringLiteral.java
@@ -13,6 +13,12 @@
// limitations under the License.
package com.google.devtools.build.lib.syntax;
+import com.google.devtools.build.lib.syntax.compiler.DebugInfo;
+import com.google.devtools.build.lib.syntax.compiler.VariableScope;
+
+import net.bytebuddy.implementation.bytecode.ByteCodeAppender;
+import net.bytebuddy.implementation.bytecode.constant.TextConstant;
+
/**
* Syntax node for a string literal.
*/
@@ -48,4 +54,9 @@ public final class StringLiteral extends Literal<String> {
@Override
void validate(ValidationEnvironment env) throws EvalException {
}
+
+ @Override
+ ByteCodeAppender compile(VariableScope scope, DebugInfo debugInfo) {
+ return new ByteCodeAppender.Simple(new TextConstant(value));
+ }
}
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/compiler/ByteCodeMethodCalls.java b/src/main/java/com/google/devtools/build/lib/syntax/compiler/ByteCodeMethodCalls.java
new file mode 100644
index 0000000000..b93fe3856d
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/syntax/compiler/ByteCodeMethodCalls.java
@@ -0,0 +1,35 @@
+// 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 net.bytebuddy.implementation.bytecode.StackManipulation;
+
+/**
+ * Keeps often used {@link StackManipulation}s which call often needed methods from the standard
+ * library and others.
+ *
+ * <p>Kept in a central place to reduce possible human errors in getting reflection right and
+ * improve code reuse. Inner classes are prefixed with "BC" to avoid import errors and to allow
+ * cleaner code in this class.
+ */
+public class ByteCodeMethodCalls {
+
+ /**
+ * Byte code invocations for {@link Integer}.
+ */
+ public static class BCInteger {
+ public static final StackManipulation valueOf =
+ ByteCodeUtils.invoke(Integer.class, "valueOf", int.class);
+ }
+}