Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    * 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 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.google.common.base.Preconditions.checkArgument;
  import static java.lang.invoke.MethodType.methodType;
  
  public class Block
          implements ByteCodeNode
  {
      private final CompilerContext context;
      private final List<ByteCodeNodenodes = new ArrayList<>();
  
      private String description;
  
      public Block(CompilerContext context)
      {
          this. = context;
      }
  
      public String getDescription()
      {
          return ;
      }
  
      public Block setDescription(String description)
      {
          this. = description;
          return this;
      }
  
      @Override
      public List<ByteCodeNodegetChildNodes()
      {
          return ImmutableList.copyOf();
      }
  
      public Block append(ByteCodeNode node)
      {
          if (node != . && !(node instanceof Block && ((Blocknode).isEmpty())) {
              .add(node);
          }
          return this;
      }
  
      public Block comment(String comment)
      {
          .add(new Comment(comment));
          return this;
      }
  
     public Block comment(String commentObject... args)
     {
         .add(new Comment(String.format(commentargs)));
         return this;
     }
 
     public boolean isEmpty()
     {
         return .size() == 0;
     }
 
     public Block visitLabel(LabelNode label)
     {
         .add(label);
         return this;
     }
 
     public Block gotoLabel(LabelNode label)
     {
         .add(JumpInstruction.jump(label));
         return this;
     }
 
     public Block ifFalseGoto(LabelNode label)
     {
         return ifZeroGoto(label);
     }
 
     public Block ifTrueGoto(LabelNode label)
     {
         return ifNotZeroGoto(label);
     }
 
     public Block ifZeroGoto(LabelNode label)
     {
         .add(JumpInstruction.jumpIfEqualZero(label));
         return this;
     }
 
     public Block ifNotZeroGoto(LabelNode label)
     {
         .add(JumpInstruction.jumpIfNotEqualZero(label));
         return this;
     }
 
     public Block ifNullGoto(LabelNode label)
     {
         .add(JumpInstruction.jumpIfNull(label));
         return this;
     }
 
     public Block ifNotNullGoto(LabelNode label)
     {
         .add(JumpInstruction.jumpIfNotNull(label));
         return this;
     }
 
     public Block intAdd()
     {
         .add(.);
         return this;
     }
 
     public Block longAdd()
     {
         .add(.);
         return this;
     }
 
     public Block longCompare()
     {
         .add(.);
         return this;
     }

    
Compare two doubles. If either is NaN comparison is -1.
 
     public Block doubleCompareNanLess()
     {
         .add(.);
         return this;
     }

    
Compare two doubles. If either is NaN comparison is 1.
 
     public Block doubleCompareNanGreater()
     {
         .add(.);
         return this;
     }
 
     public Block intLeftShift()
     {
         .add(.);
         return this;
     }
 
     public Block intRightShift()
     {
         .add(.);
         return this;
     }
 
     public Block longLeftShift()
     {
         .add(.);
         return this;
     }
 
     public Block longRightShift()
     {
         .add(.);
         return this;
     }
 
     public Block unsignedIntRightShift()
     {
         .add(.);
         return this;
     }
 
     public Block unsignedLongRightShift()
     {
         .add(.);
         return this;
     }
 
     public Block intBitAnd()
     {
         .add(.);
         return this;
     }
 
     public Block intBitOr()
     {
         .add(.);
         return this;
     }
 
     public Block intBitXor()
     {
         .add(.);
         return this;
     }
 
     public Block longBitAnd()
     {
         .add(.);
         return this;
     }
 
     public Block longBitOr()
     {
         .add(.);
         return this;
     }
 
     public Block longBitXor()
     {
         .add(.);
         return this;
     }
 
     public Block intNegate()
     {
         .add(.);
         return this;
     }
 
     public Block intToLong()
     {
         .add(.);
         return this;
     }
 
     public Block longNegate()
     {
         .add(.);
         return this;
     }
 
     public Block longToInt()
     {
         .add(.);
         return this;
     }
 
     public Block isInstanceOf(Class<?> type)
     {
         .add(instanceOf(type));
         return this;
     }
 
     public Block isInstanceOf(ParameterizedType type)
     {
         .add(instanceOf(type));
         return this;
     }
 
     public Block checkCast(Class<?> type)
     {
         .add(cast(type));
         return this;
     }
 
     public Block checkCast(ParameterizedType type)
     {
         .add(cast(type));
         return this;
     }
 
     public Block invokeStatic(Method method)
     {
         .add(InvokeInstruction.invokeStatic(method));
         return this;
     }
 
     public Block invokeStatic(MethodDefinition method)
     {
         .add(InvokeInstruction.invokeStatic(method));
         return this;
     }
 
     public Block invokeStatic(Class<?> typeString nameClass<?> returnTypeClass<?>... parameterTypes)
     {
         .add(InvokeInstruction.invokeStatic(typenamereturnTypeparameterTypes));
         return this;
     }
 
     public Block invokeStatic(Class<?> typeString nameClass<?> returnTypeIterable<Class<?>> parameterTypes)
     {
         .add(InvokeInstruction.invokeStatic(typenamereturnTypeparameterTypes));
         return this;
     }
 
     public Block invokeStatic(ParameterizedType typeString nameParameterizedType returnTypeParameterizedType... parameterTypes)
     {
         .add(InvokeInstruction.invokeStatic(typenamereturnTypeparameterTypes));
         return this;
     }
 
     public Block invokeStatic(ParameterizedType typeString nameParameterizedType returnTypeIterable<ParameterizedTypeparameterTypes)
     {
         .add(InvokeInstruction.invokeStatic(typenamereturnTypeparameterTypes));
         return this;
     }
 
     public Block invokeVirtual(Method method)
     {
         .add(InvokeInstruction.invokeVirtual(method));
         return this;
     }
 
     public Block invokeVirtual(MethodDefinition method)
     {
         .add(InvokeInstruction.invokeVirtual(method));
         return this;
     }
 
     public Block invokeVirtual(Class<?> typeString nameClass<?> returnTypeClass<?>... parameterTypes)
     {
         .add(InvokeInstruction.invokeVirtual(typenamereturnTypeparameterTypes));
         return this;
     }
 
     public Block invokeVirtual(Class<?> typeString nameClass<?> returnTypeIterable<Class<?>> parameterTypes)
     {
         .add(InvokeInstruction.invokeVirtual(typenamereturnTypeparameterTypes));
         return this;
     }
 
     public Block invokeVirtual(ParameterizedType typeString nameParameterizedType returnTypeParameterizedType... parameterTypes)
     {
         .add(InvokeInstruction.invokeVirtual(typenamereturnTypeparameterTypes));
         return this;
     }
 
     public Block invokeVirtual(ParameterizedType typeString nameParameterizedType returnTypeIterable<ParameterizedTypeparameterTypes)
     {
         .add(InvokeInstruction.invokeVirtual(typenamereturnTypeparameterTypes));
         return this;
     }
 
     public Block invokeInterface(Method method)
     {
         .add(InvokeInstruction.invokeInterface(method));
         return this;
     }
 
     public Block invokeInterface(MethodDefinition method)
     {
         .add(InvokeInstruction.invokeInterface(method));
         return this;
     }
 
     public Block invokeInterface(Class<?> typeString nameClass<?> returnTypeClass<?>... parameterTypes)
     {
         .add(InvokeInstruction.invokeInterface(typenamereturnTypeparameterTypes));
         return this;
     }
 
     public Block invokeInterface(Class<?> typeString nameClass<?> returnTypeIterable<Class<?>> parameterTypes)
     {
         .add(InvokeInstruction.invokeInterface(typenamereturnTypeparameterTypes));
         return this;
     }
 
     public Block invokeInterface(ParameterizedType typeString nameParameterizedType returnTypeParameterizedType... parameterTypes)
     {
         .add(InvokeInstruction.invokeInterface(typenamereturnTypeparameterTypes));
         return this;
     }
 
     public Block invokeInterface(ParameterizedType typeString nameParameterizedType returnTypeIterable<ParameterizedTypeparameterTypes)
     {
         .add(InvokeInstruction.invokeInterface(typenamereturnTypeparameterTypes));
         return this;
     }
 
     public Block invokeConstructor(Constructor<?> constructor)
     {
         .add(InvokeInstruction.invokeConstructor(constructor));
         return this;
     }
 
     public Block invokeConstructor(Class<?> typeClass<?>... parameterTypes)
     {
         .add(InvokeInstruction.invokeConstructor(typeparameterTypes));
         return this;
     }
 
     public Block invokeConstructor(Class<?> typeIterable<Class<?>> parameterTypes)
     {
         .add(InvokeInstruction.invokeConstructor(typeparameterTypes));
         return this;
     }
 
     public Block invokeConstructor(ParameterizedType typeParameterizedType... parameterTypes)
     {
         .add(InvokeInstruction.invokeConstructor(typeparameterTypes));
         return this;
     }
 
     public Block invokeConstructor(ParameterizedType typeIterable<ParameterizedTypeparameterTypes)
     {
         .add(InvokeInstruction.invokeConstructor(typeparameterTypes));
         return this;
     }
 
     public Block invokeSpecial(Method method)
     {
         .add(InvokeInstruction.invokeSpecial(method));
         return this;
     }
 
     public Block invokeSpecial(MethodDefinition method)
     {
         .add(InvokeInstruction.invokeSpecial(method));
         return this;
     }
 
     public Block invokeSpecial(Class<?> typeString nameClass<?> returnTypeClass<?>... parameterTypes)
     {
         .add(InvokeInstruction.invokeSpecial(typenamereturnTypeparameterTypes));
         return this;
     }
 
     public Block invokeSpecial(Class<?> typeString nameClass<?> returnTypeIterable<Class<?>> parameterTypes)
     {
         .add(InvokeInstruction.invokeSpecial(typenamereturnTypeparameterTypes));
         return this;
     }
 
     public Block invokeSpecial(ParameterizedType typeString nameParameterizedType returnTypeParameterizedType... parameterTypes)
     {
         .add(InvokeInstruction.invokeSpecial(typenamereturnTypeparameterTypes));
         return this;
     }
 
     public Block invokeSpecial(ParameterizedType typeString nameParameterizedType returnTypeIterable<ParameterizedTypeparameterTypes)
     {
         .add(InvokeInstruction.invokeSpecial(typenamereturnTypeparameterTypes));
         return this;
     }
 
     public Block invokeDynamic(String nameClass<?> returnTypeList<Class<?>> parameterTypes)
     {
         return invokeDynamic(namemethodType(returnTypeparameterTypes));
     }
 
     public Block invokeDynamic(String nameClass<?> returnTypeClass<?>... parameterTypes)
     {
         return invokeDynamic(namemethodType(returnType, ImmutableList.copyOf(parameterTypes)));
     }
 
     public Block invokeDynamic(String nameMethodType methodType)
     {
         return invokeDynamic(namemethodType.getDefaultBootstrapMethod(), .getDefaultBootstrapArguments());
     }
 
     public Block invokeDynamic(String nameMethodType methodTypeObject... defaultBootstrapArguments)
     {
         .add(InvokeInstruction.invokeDynamic(namemethodType.getDefaultBootstrapMethod(), defaultBootstrapArguments));
         return this;
     }
 
     public Block invokeDynamic(String nameMethodType methodTypeMethod bootstrapMethodObject... defaultBootstrapArguments)
     {
         .add(InvokeInstruction.invokeDynamic(namemethodTypebootstrapMethoddefaultBootstrapArguments));
         return this;
     }
 
     public ByteCodeNode invokeDynamic(String name,
             ParameterizedType returnType,
             Iterable<ParameterizedTypeparameterTypes,
             Method bootstrapMethod,
             List<ObjectbootstrapArgs)
     {
         .add(InvokeInstruction.invokeDynamic(namereturnTypeparameterTypesbootstrapMethodbootstrapArgs));
         return this;
     }
 
     public Block ret(Class<?> type)
     {
         if (type == long.class) {
             retLong();
         }
         else if (type == boolean.class) {
             retBoolean();
         }
         else if (type == int.class || type == byte.class || type == char.class || type == short.class) {
             retInt();
         }
         else if (type == float.class) {
             retFloat();
         }
         else if (type == double.class) {
             retDouble();
         }
         else if (type == void.class) {
             ret();
         }
         else if (!type.isPrimitive()) {
             retObject();
         }
         else {
             throw new IllegalArgumentException("Unsupported type: " + type.getName());
         }
 
         return this;
     }
 
     public Block ret()
     {
         .add(.);
         return this;
     }
 
     public Block retObject()
     {
         .add(.);
         return this;
     }
 
     public Block retFloat()
     {
         .add(.);
         return this;
     }
 
     public Block retDouble()
     {
         .add(.);
         return this;
     }
 
     public Block retBoolean()
     {
         .add(.);
         return this;
     }
 
     public Block retLong()
     {
         .add(.);
         return this;
     }
 
     public Block retInt()
     {
         .add(.);
         return this;
     }
 
     public Block throwObject()
     {
         .add(.);
         return this;
     }
 
     public Block newObject(Class<?> type)
     {
         .add(TypeInstruction.newObject(type));
         return this;
     }
 
     public Block newObject(ParameterizedType type)
     {
         .add(TypeInstruction.newObject(type));
         return this;
     }
 
     public Block newArray(Class<?> type)
     {
         .add(TypeInstruction.newObjectArray(type));
         return this;
     }
 
     public Block dup()
     {
         .add(.);
         return this;
     }
 
     public Block dup(Class<?> type)
     {
         if (type == long.class || type == double.class) {
             .add(.);
         }
         else {
             .add(.);
         }
         return this;
     }
 
     public Block pop()
     {
         .add(.);
         return this;
     }
 
     public Block pop(Class<?> type)
     {
         if (type == long.class || type == double.class) {
             .add(.);
         }
         else if (type != void.class) {
             .add(.);
         }
         return this;
     }
 
     public Block pop(ParameterizedType type)
     {
         Class<?> primitiveType = type.getPrimitiveType();
         if (primitiveType == long.class || primitiveType == double.class) {
             .add(.);
         }
         else if (primitiveType != void.class) {
             .add(.);
         }
         return this;
     }
 
     public Block swap()
     {
         .add(.);
         return this;
     }
 
     //
     // Fields (non-static)
     //
 
     public Block getField(Field field)
     {
         return getField(field.getDeclaringClass(), field.getName(), field.getType());
     }
 
     public Block getField(FieldDefinition field)
     {
         getField(field.getDeclaringClass().getType(), field.getName(), field.getType());
         return this;
     }
 
     public Block getField(Class<?> targetString fieldNameClass<?> fieldType)
     {
         getField(type(target), fieldNametype(fieldType));
         return this;
     }
 
     public Block getField(ParameterizedType targetString fieldNameParameterizedType fieldType)
     {
         .add(getFieldInstruction(targetfieldNamefieldType));
         return this;
     }
 
     public Block putField(Field field)
     {
         return putField(field.getDeclaringClass(), field.getName(), field.getType());
     }
 
     public Block putField(Class<?> targetString fieldNameClass<?> fieldType)
     {
         putField(type(target), fieldNametype(fieldType));
         return this;
     }
 
     public Block putField(FieldDefinition field)
     {
         checkArgument(!field.getAccess().contains(), "Field is static: %s"field);
         putField(field.getDeclaringClass().getType(), field.getName(), field.getType());
         return this;
     }
 
     public Block putField(ParameterizedType targetString fieldNameParameterizedType fieldType)
     {
         .add(putFieldInstruction(targetfieldNamefieldType));
         return this;
     }
 
     //
     // Static fields
     //
 
     public Block getStaticField(FieldDefinition field)
     {
         getStaticField(field.getDeclaringClass().getType(), field.getName(), field.getType());
         return this;
     }
 
     public Block getStaticField(Field field)
     {
         checkArgument(Modifier.isStatic(field.getModifiers()), "Field is not static: %s"field);
         getStaticField(type(field.getDeclaringClass()), field.getName(), type(field.getType()));
         return this;
     }
 
     public Block getStaticField(Class<?> targetString fieldNameClass<?> fieldType)
     {
         .add(getStaticInstruction(targetfieldNamefieldType));
         return this;
     }
 
     public Block getStaticField(ParameterizedType targetString fieldNameParameterizedType fieldType)
     {
         .add(getStaticInstruction(targetfieldNamefieldType));
         return this;
     }
 
     public Block getStaticField(ParameterizedType targetFieldDefinition field)
     {
         .add(getStaticInstruction(targetfield.getName(), field.getType()));
         return this;
     }
 
     public Block putStaticField(FieldDefinition field)
     {
         putStaticField(field.getDeclaringClass().getType(), field.getName(), field.getType());
         return this;
     }
 
     public Block putStaticField(ParameterizedType targetFieldDefinition field)
     {
         checkArgument(field.getAccess().contains(), "Field is not static: %s"field);
         putStaticField(targetfield.getName(), field.getType());
         return this;
     }
 
     public Block putStaticField(ParameterizedType targetString fieldNameParameterizedType fieldType)
     {
         .add(putStaticInstruction(targetfieldNamefieldType));
         return this;
     }
 
     //
     // Load constants
     //
 
     public Block pushThis()
     {
         getVariable("this");
         return this;
     }
 
     public Block pushNull()
     {
         .add(.);
         return this;
     }
 
     public Block push(Class<?> type)
     {
         .add(loadClass(type));
         return this;
     }
 
     public Block push(ParameterizedType type)
     {
         .add(loadClass(type));
         return this;
     }
 
     public Block push(String value)
     {
         .add(Constant.loadString(value));
         return this;
     }
 
     public Block push(Number value)
     {
         .add(loadNumber(value));
         return this;
     }
 
     public Block push(int value)
     {
         .add(loadInt(value));
         return this;
     }
 
     public Block push(boolean value)
     {
         .add(loadBoolean(value));
         return this;
     }
 
     public Block pushJavaDefault(Class<?> type)
     {
         if (type == void.class) {
             return this;
         }
         if (type == boolean.class || type == byte.class || type == char.class || type == short.class || type == int.class) {
             return push(0);
         }
         if (type == long.class) {
             return push(0L);
         }
         if (type == float.class) {
             return push(0.0f);
         }
         if (type == double.class) {
             return push(0.0d);
         }
         return pushNull();
     }
 
     public Block getVariable(String name)
     {
         append(.getVariable(name).getByteCode());
         return this;
     }
 
     public Block getVariable(String nameParameterizedType type)
     {
         getVariable(name);
         checkCast(type);
         return this;
     }
 
     public Block initializeVariable(Variable variable)
     {
         ParameterizedType type = variable.getType();
         if (type.getType().length() == 1) {
             switch (type.getType().charAt(0)) {
                 case 'B':
                 case 'Z':
                 case 'S':
                 case 'C':
                 case 'I':
                     .add(loadInt(0));
                     break;
                 case 'F':
                     .add(loadFloat(0));
                     break;
                 case 'D':
                     .add(loadDouble(0));
                     break;
                 case 'J':
                     .add(loadLong(0));
                     break;
                 default:
                     checkArgument(false"Unknown type '%s'"variable.getType());
             }
         }
         else {
             .add(Constant.loadNull());
         }
 
         .add(VariableInstruction.storeVariable(variable));
 
         return this;
     }
 
     public Block getVariable(Variable variable)
     {
         .add(VariableInstruction.loadVariable(variable));
         return this;
     }
 
     public Block putVariable(String name)
     {
         putVariable(.getVariable(name));
         return this;
     }
 
     public Block putVariable(String nameClass<?> type)
     {
         .add(loadClass(type));
         putVariable(name);
         return this;
     }
 
     public Block putVariable(String nameParameterizedType type)
     {
         .add(loadClass(type));
         putVariable(name);
         return this;
     }
 
     public Block putVariable(String nameString value)
     {
         .add(Constant.loadString(value));
         putVariable(name);
         return this;
     }
 
     public Block putVariable(String nameNumber value)
     {
         .add(loadNumber(value));
         putVariable(name);
         return this;
     }
 
     public Block putVariable(String nameint value)
     {
         .add(loadInt(value));
         putVariable(name);
         return this;
     }
 
     public Block putVariable(String nameboolean value)
     {
         .add(loadBoolean(value));
         putVariable(name);
         return this;
     }
 
     public Block putVariable(Variable variable)
     {
         .add(VariableInstruction.storeVariable(variable));
         return this;
     }
 
     public Block putVariable(Variable variableClass<?> type)
     {
         .add(loadClass(type));
         putVariable(variable);
         return this;
     }
 
     public Block putVariable(Variable variableParameterizedType type)
     {
         .add(loadClass(type));
         putVariable(variable);
         return this;
     }
 
     public Block putVariable(Variable variableString value)
     {
         .add(Constant.loadString(value));
         putVariable(variable);
         return this;
     }
 
     public Block putVariable(Variable variableNumber value)
     {
         .add(loadNumber(value));
         putVariable(variable);
         return this;
     }
 
     public Block putVariable(Variable variableint value)
     {
         .add(loadInt(value));
         putVariable(variable);
         return this;
     }
 
     public Block putVariable(Variable variableboolean value)
     {
         .add(loadBoolean(value));
         putVariable(variable);
         return this;
     }
 
     public Block incrementVariable(Variable variablebyte increment)
     {
        String type = variable.getType().getClassName();
        Preconditions.checkArgument(ImmutableList.of("byte""short""int").contains(type), "variable must be an byte, short or int, but is %s"type);
        .add(VariableInstruction.incrementVariable(variableincrement));
        return this;
    }
    public Block getObjectArrayElement()
    {
        .add(.);
        return this;
    }
    public Block putObjectArrayElement()
    {
        .add(.);
        return this;
    }
    public Block visitLineNumber(int line)
    {
        if (line <= 0) {
            .cleanLineNumber();
        }
        else if (!.hasVisitedLine(line)) {
            .add(new LineNumberNode(line));
            .visitLine(line);
        }
        return this;
    }
    @Override
    public void