com.facebook.presto.bytecode.BytecodeBlock Maven / Gradle / Ivy
/*
* 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.facebook.presto.bytecode;
import com.facebook.presto.bytecode.debug.LineNumberNode;
import com.facebook.presto.bytecode.instruction.Constant;
import com.facebook.presto.bytecode.instruction.InvokeInstruction;
import com.facebook.presto.bytecode.instruction.JumpInstruction;
import com.facebook.presto.bytecode.instruction.LabelNode;
import com.facebook.presto.bytecode.instruction.TypeInstruction;
import com.facebook.presto.bytecode.instruction.VariableInstruction;
import com.google.common.collect.ImmutableList;
import org.objectweb.asm.MethodVisitor;
import javax.annotation.concurrent.NotThreadSafe;
import java.lang.invoke.MethodType;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import static com.facebook.presto.bytecode.Access.STATIC;
import static com.facebook.presto.bytecode.ParameterizedType.type;
import static com.facebook.presto.bytecode.instruction.Constant.loadBoolean;
import static com.facebook.presto.bytecode.instruction.Constant.loadClass;
import static com.facebook.presto.bytecode.instruction.Constant.loadDouble;
import static com.facebook.presto.bytecode.instruction.Constant.loadFloat;
import static com.facebook.presto.bytecode.instruction.Constant.loadInt;
import static com.facebook.presto.bytecode.instruction.Constant.loadLong;
import static com.facebook.presto.bytecode.instruction.Constant.loadNumber;
import static com.facebook.presto.bytecode.instruction.FieldInstruction.getFieldInstruction;
import static com.facebook.presto.bytecode.instruction.FieldInstruction.getStaticInstruction;
import static com.facebook.presto.bytecode.instruction.FieldInstruction.putFieldInstruction;
import static com.facebook.presto.bytecode.instruction.FieldInstruction.putStaticInstruction;
import static com.facebook.presto.bytecode.instruction.TypeInstruction.cast;
import static com.facebook.presto.bytecode.instruction.TypeInstruction.instanceOf;
import static com.facebook.presto.bytecode.instruction.VariableInstruction.loadVariable;
import static com.facebook.presto.bytecode.instruction.VariableInstruction.storeVariable;
import static com.google.common.base.Preconditions.checkArgument;
@SuppressWarnings("UnusedDeclaration")
@NotThreadSafe
public class BytecodeBlock
implements BytecodeNode
{
private final List nodes = new ArrayList<>();
private String description;
private int currentLineNumber = -1;
public String getDescription()
{
return description;
}
public BytecodeBlock setDescription(String description)
{
this.description = description;
return this;
}
@Override
public List getChildNodes()
{
return ImmutableList.copyOf(nodes);
}
public BytecodeBlock append(BytecodeNode node)
{
nodes.add(node);
return this;
}
public BytecodeBlock comment(String comment)
{
nodes.add(new Comment(comment));
return this;
}
public BytecodeBlock comment(String comment, Object... args)
{
nodes.add(new Comment(String.format(comment, args)));
return this;
}
public boolean isEmpty()
{
return nodes.isEmpty();
}
public BytecodeBlock visitLabel(LabelNode label)
{
nodes.add(label);
return this;
}
public BytecodeBlock gotoLabel(LabelNode label)
{
nodes.add(JumpInstruction.jump(label));
return this;
}
public BytecodeBlock ifFalseGoto(LabelNode label)
{
return ifZeroGoto(label);
}
public BytecodeBlock ifTrueGoto(LabelNode label)
{
return ifNotZeroGoto(label);
}
public BytecodeBlock ifZeroGoto(LabelNode label)
{
nodes.add(JumpInstruction.jumpIfEqualZero(label));
return this;
}
public BytecodeBlock ifNotZeroGoto(LabelNode label)
{
nodes.add(JumpInstruction.jumpIfNotEqualZero(label));
return this;
}
public BytecodeBlock ifNullGoto(LabelNode label)
{
nodes.add(JumpInstruction.jumpIfNull(label));
return this;
}
public BytecodeBlock ifNotNullGoto(LabelNode label)
{
nodes.add(JumpInstruction.jumpIfNotNull(label));
return this;
}
public BytecodeBlock intAdd()
{
nodes.add(OpCode.IADD);
return this;
}
public BytecodeBlock longAdd()
{
nodes.add(OpCode.LADD);
return this;
}
public BytecodeBlock longCompare()
{
nodes.add(OpCode.LCMP);
return this;
}
/**
* Compare two doubles. If either is NaN comparison is -1.
*/
public BytecodeBlock doubleCompareNanLess()
{
nodes.add(OpCode.DCMPL);
return this;
}
/**
* Compare two doubles. If either is NaN comparison is 1.
*/
public BytecodeBlock doubleCompareNanGreater()
{
nodes.add(OpCode.DCMPG);
return this;
}
public BytecodeBlock intLeftShift()
{
nodes.add(OpCode.ISHL);
return this;
}
public BytecodeBlock intRightShift()
{
nodes.add(OpCode.ISHR);
return this;
}
public BytecodeBlock longLeftShift()
{
nodes.add(OpCode.LSHL);
return this;
}
public BytecodeBlock longRightShift()
{
nodes.add(OpCode.LSHR);
return this;
}
public BytecodeBlock unsignedIntRightShift()
{
nodes.add(OpCode.IUSHR);
return this;
}
public BytecodeBlock unsignedLongRightShift()
{
nodes.add(OpCode.LUSHR);
return this;
}
public BytecodeBlock intBitAnd()
{
nodes.add(OpCode.IAND);
return this;
}
public BytecodeBlock intBitOr()
{
nodes.add(OpCode.IOR);
return this;
}
public BytecodeBlock intBitXor()
{
nodes.add(OpCode.IXOR);
return this;
}
public BytecodeBlock longBitAnd()
{
nodes.add(OpCode.LAND);
return this;
}
public BytecodeBlock longBitOr()
{
nodes.add(OpCode.LOR);
return this;
}
public BytecodeBlock longBitXor()
{
nodes.add(OpCode.LXOR);
return this;
}
public BytecodeBlock intNegate()
{
nodes.add(OpCode.INEG);
return this;
}
public BytecodeBlock intToLong()
{
nodes.add(OpCode.I2L);
return this;
}
public BytecodeBlock longNegate()
{
nodes.add(OpCode.LNEG);
return this;
}
public BytecodeBlock longToInt()
{
nodes.add(OpCode.L2I);
return this;
}
public BytecodeBlock isInstanceOf(Class> type)
{
nodes.add(instanceOf(type));
return this;
}
public BytecodeBlock isInstanceOf(ParameterizedType type)
{
nodes.add(instanceOf(type));
return this;
}
public BytecodeBlock checkCast(Class> type)
{
nodes.add(cast(type));
return this;
}
public BytecodeBlock checkCast(ParameterizedType type)
{
nodes.add(cast(type));
return this;
}
public BytecodeBlock invokeStatic(Method method)
{
nodes.add(InvokeInstruction.invokeStatic(method));
return this;
}
public BytecodeBlock invokeStatic(MethodDefinition method)
{
nodes.add(InvokeInstruction.invokeStatic(method));
return this;
}
public BytecodeBlock invokeStatic(Class> type, String name, Class> returnType, Class>... parameterTypes)
{
nodes.add(InvokeInstruction.invokeStatic(type, name, returnType, parameterTypes));
return this;
}
public BytecodeBlock invokeStatic(Class> type, String name, Class> returnType, Iterable> parameterTypes)
{
nodes.add(InvokeInstruction.invokeStatic(type, name, returnType, parameterTypes));
return this;
}
public BytecodeBlock invokeStatic(ParameterizedType type, String name, ParameterizedType returnType, ParameterizedType... parameterTypes)
{
nodes.add(InvokeInstruction.invokeStatic(type, name, returnType, parameterTypes));
return this;
}
public BytecodeBlock invokeStatic(ParameterizedType type, String name, ParameterizedType returnType, Iterable parameterTypes)
{
nodes.add(InvokeInstruction.invokeStatic(type, name, returnType, parameterTypes));
return this;
}
public BytecodeBlock invokeVirtual(Method method)
{
nodes.add(InvokeInstruction.invokeVirtual(method));
return this;
}
public BytecodeBlock invokeVirtual(MethodDefinition method)
{
nodes.add(InvokeInstruction.invokeVirtual(method));
return this;
}
public BytecodeBlock invokeVirtual(Class> type, String name, Class> returnType, Class>... parameterTypes)
{
nodes.add(InvokeInstruction.invokeVirtual(type, name, returnType, parameterTypes));
return this;
}
public BytecodeBlock invokeVirtual(Class> type, String name, Class> returnType, Iterable> parameterTypes)
{
nodes.add(InvokeInstruction.invokeVirtual(type, name, returnType, parameterTypes));
return this;
}
public BytecodeBlock invokeVirtual(ParameterizedType type, String name, ParameterizedType returnType, ParameterizedType... parameterTypes)
{
nodes.add(InvokeInstruction.invokeVirtual(type, name, returnType, parameterTypes));
return this;
}
public BytecodeBlock invokeVirtual(ParameterizedType type, String name, ParameterizedType returnType, Iterable parameterTypes)
{
nodes.add(InvokeInstruction.invokeVirtual(type, name, returnType, parameterTypes));
return this;
}
public BytecodeBlock invokeInterface(Method method)
{
nodes.add(InvokeInstruction.invokeInterface(method));
return this;
}
public BytecodeBlock invokeInterface(MethodDefinition method)
{
nodes.add(InvokeInstruction.invokeInterface(method));
return this;
}
public BytecodeBlock invokeInterface(Class> type, String name, Class> returnType, Class>... parameterTypes)
{
nodes.add(InvokeInstruction.invokeInterface(type, name, returnType, parameterTypes));
return this;
}
public BytecodeBlock invokeInterface(Class> type, String name, Class> returnType, Iterable> parameterTypes)
{
nodes.add(InvokeInstruction.invokeInterface(type, name, returnType, parameterTypes));
return this;
}
public BytecodeBlock invokeInterface(ParameterizedType type, String name, ParameterizedType returnType, ParameterizedType... parameterTypes)
{
nodes.add(InvokeInstruction.invokeInterface(type, name, returnType, parameterTypes));
return this;
}
public BytecodeBlock invokeInterface(ParameterizedType type, String name, ParameterizedType returnType, Iterable parameterTypes)
{
nodes.add(InvokeInstruction.invokeInterface(type, name, returnType, parameterTypes));
return this;
}
public BytecodeBlock invokeConstructor(Constructor> constructor)
{
nodes.add(InvokeInstruction.invokeConstructor(constructor));
return this;
}
public BytecodeBlock invokeConstructor(Class> type, Class>... parameterTypes)
{
nodes.add(InvokeInstruction.invokeConstructor(type, parameterTypes));
return this;
}
public BytecodeBlock invokeConstructor(Class> type, Iterable> parameterTypes)
{
nodes.add(InvokeInstruction.invokeConstructor(type, parameterTypes));
return this;
}
public BytecodeBlock invokeConstructor(ParameterizedType type, ParameterizedType... parameterTypes)
{
nodes.add(InvokeInstruction.invokeConstructor(type, parameterTypes));
return this;
}
public BytecodeBlock invokeConstructor(ParameterizedType type, Iterable parameterTypes)
{
nodes.add(InvokeInstruction.invokeConstructor(type, parameterTypes));
return this;
}
public BytecodeBlock invokeSpecial(Method method)
{
nodes.add(InvokeInstruction.invokeSpecial(method));
return this;
}
public BytecodeBlock invokeSpecial(MethodDefinition method)
{
nodes.add(InvokeInstruction.invokeSpecial(method));
return this;
}
public BytecodeBlock invokeSpecial(Class> type, String name, Class> returnType, Class>... parameterTypes)
{
nodes.add(InvokeInstruction.invokeSpecial(type, name, returnType, parameterTypes));
return this;
}
public BytecodeBlock invokeSpecial(Class> type, String name, Class> returnType, Iterable> parameterTypes)
{
nodes.add(InvokeInstruction.invokeSpecial(type, name, returnType, parameterTypes));
return this;
}
public BytecodeBlock invokeSpecial(ParameterizedType type, String name, ParameterizedType returnType, ParameterizedType... parameterTypes)
{
nodes.add(InvokeInstruction.invokeSpecial(type, name, returnType, parameterTypes));
return this;
}
public BytecodeBlock invokeSpecial(ParameterizedType type, String name, ParameterizedType returnType, Iterable parameterTypes)
{
nodes.add(InvokeInstruction.invokeSpecial(type, name, returnType, parameterTypes));
return this;
}
public BytecodeBlock invokeDynamic(String name, MethodType methodType, Method bootstrapMethod, Object... defaultBootstrapArguments)
{
nodes.add(InvokeInstruction.invokeDynamic(name, methodType, bootstrapMethod, defaultBootstrapArguments));
return this;
}
public BytecodeNode invokeDynamic(String name,
ParameterizedType returnType,
Iterable parameterTypes,
Method bootstrapMethod,
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy