Package org.eclipse.jdt.internal.compiler.codegen

Examples of org.eclipse.jdt.internal.compiler.codegen.CodeStream


    int constantPoolOffset = constantPool.currentOffset;
    int constantPoolIndex = constantPool.currentIndex;
    classFile.generateMethodInfoHeaderForClinit();
    int codeAttributeOffset = classFile.contentsOffset;
    classFile.generateCodeAttributeHeader();
    CodeStream codeStream = classFile.codeStream;
    resolve(classScope);

    codeStream.reset(this, classFile);
    TypeDeclaration declaringType = classScope.referenceContext;

    // initialize local positions - including initializer scope.
    MethodScope staticInitializerScope = declaringType.staticInitializerScope;
    staticInitializerScope.computeLocalVariablePositions(0, codeStream);

    // 1.4 feature
    // This has to be done before any other initialization
    if (this.assertionSyntheticFieldBinding != null) {
      // generate code related to the activation of assertion for this class
      codeStream.generateClassLiteralAccessForType(
          classScope.outerMostClassScope().enclosingSourceType(),
          this.classLiteralSyntheticField);
      codeStream.invokeJavaLangClassDesiredAssertionStatus();
      BranchLabel falseLabel = new BranchLabel(codeStream);
      codeStream.ifne(falseLabel);
      codeStream.iconst_1();
      BranchLabel jumpLabel = new BranchLabel(codeStream);
      codeStream.decrStackSize(1);
      codeStream.goto_(jumpLabel);
      falseLabel.place();
      codeStream.iconst_0();
      jumpLabel.place();
      codeStream.fieldAccess(Opcodes.OPC_putstatic, this.assertionSyntheticFieldBinding, null /* default declaringClass */);
    }
    // generate static fields/initializers/enum constants
    final FieldDeclaration[] fieldDeclarations = declaringType.fields;
    int sourcePosition = -1;
    int remainingFieldCount = 0;
    if (TypeDeclaration.kind(declaringType.modifiers) == TypeDeclaration.ENUM_DECL) {
      int enumCount = declaringType.enumConstantsCounter;
      if (enumCount > ENUM_CONSTANTS_THRESHOLD) {
        // generate synthetic methods to initialize all the enum constants
        int begin = -1;
        int count = 0;
        if (fieldDeclarations != null) {
          int max = fieldDeclarations.length;
          for (int i = 0; i < max; i++) {
            FieldDeclaration fieldDecl = fieldDeclarations[i];
            if (fieldDecl.isStatic()) {
              if (fieldDecl.getKind() == AbstractVariableDeclaration.ENUM_CONSTANT) {
                if (begin == -1) {
                  begin = i;
                }
                count++;
                if (count > ENUM_CONSTANTS_THRESHOLD) {
                  SyntheticMethodBinding syntheticMethod = declaringType.binding.addSyntheticMethodForEnumInitialization(begin, i);
                  codeStream.invoke(Opcodes.OPC_invokestatic, syntheticMethod, null /* default declaringClass */);
                  begin = i;
                  count = 1;
                }
              } else {
                remainingFieldCount++;
              }
            }
          }
          if (count != 0) {
            // add last synthetic method
            SyntheticMethodBinding syntheticMethod = declaringType.binding.addSyntheticMethodForEnumInitialization(begin, max);
            codeStream.invoke(Opcodes.OPC_invokestatic, syntheticMethod, null /* default declaringClass */);
          }
        }
      } else if (fieldDeclarations != null) {
        for (int i = 0, max = fieldDeclarations.length; i < max; i++) {
          FieldDeclaration fieldDecl = fieldDeclarations[i];
          if (fieldDecl.isStatic()) {
            if (fieldDecl.getKind() == AbstractVariableDeclaration.ENUM_CONSTANT) {
              fieldDecl.generateCode(staticInitializerScope, codeStream);
            } else {
              remainingFieldCount++;
            }
          }
        }
      }
      // enum need to initialize $VALUES synthetic cache of enum constants
      // $VALUES := new <EnumType>[<enumCount>]
      codeStream.generateInlinedValue(enumCount);
      codeStream.anewarray(declaringType.binding);
      if (enumCount > 0) {
        if (fieldDeclarations != null) {
          for (int i = 0, max = fieldDeclarations.length; i < max; i++) {
            FieldDeclaration fieldDecl = fieldDeclarations[i];
            // $VALUES[i] = <enum-constant-i>
            if (fieldDecl.getKind() == AbstractVariableDeclaration.ENUM_CONSTANT) {
              codeStream.dup();
              codeStream.generateInlinedValue(fieldDecl.binding.id);
              codeStream.fieldAccess(Opcodes.OPC_getstatic, fieldDecl.binding, null /* default declaringClass */);
              codeStream.aastore();
            }
          }
        }
      }
      codeStream.fieldAccess(Opcodes.OPC_putstatic, declaringType.enumValuesSyntheticfield, null /* default declaringClass */);
      if (remainingFieldCount != 0) {
        // if fields that are not enum constants need to be generated (static initializer/static field)
        for (int i = 0, max = fieldDeclarations.length; i < max && remainingFieldCount >= 0; i++) {
          FieldDeclaration fieldDecl = fieldDeclarations[i];
          switch (fieldDecl.getKind()) {
            case AbstractVariableDeclaration.ENUM_CONSTANT :
              break;
            case AbstractVariableDeclaration.INITIALIZER :
              if (!fieldDecl.isStatic()) {
                break;
              }
              remainingFieldCount--;
              sourcePosition = ((Initializer) fieldDecl).block.sourceEnd;
              fieldDecl.generateCode(staticInitializerScope, codeStream);
              break;
            case AbstractVariableDeclaration.FIELD :
              if (!fieldDecl.binding.isStatic()) {
                break;
              }
              remainingFieldCount--;
              sourcePosition = fieldDecl.declarationEnd;
              fieldDecl.generateCode(staticInitializerScope, codeStream);
              break;
          }
        }
      }
    } else {
      if (fieldDeclarations != null) {
        for (int i = 0, max = fieldDeclarations.length; i < max; i++) {
          FieldDeclaration fieldDecl = fieldDeclarations[i];
          switch (fieldDecl.getKind()) {
            case AbstractVariableDeclaration.INITIALIZER :
              if (!fieldDecl.isStatic())
                break;
              sourcePosition = ((Initializer) fieldDecl).block.sourceEnd;
              fieldDecl.generateCode(staticInitializerScope, codeStream);
              break;
            case AbstractVariableDeclaration.FIELD :
              if (!fieldDecl.binding.isStatic())
                break;
              sourcePosition = fieldDecl.declarationEnd;
              fieldDecl.generateCode(staticInitializerScope, codeStream);
              break;
          }
        }
      }
    }

    if (codeStream.position == 0) {
      // do not need to output a Clinit if no bytecodes
      // so we reset the offset inside the byte array contents.
      classFile.contentsOffset = clinitOffset;
      // like we don't addd a method we need to undo the increment on the method count
      classFile.methodCount--;
      // reset the constant pool to its state before the clinit
      constantPool.resetForClinit(constantPoolIndex, constantPoolOffset);
    } else {
      if ((this.bits & ASTNode.NeedFreeReturn) != 0) {
        int before = codeStream.position;
        codeStream.return_();
        if (sourcePosition != -1) {
          // expand the last initializer variables to include the trailing return
          codeStream.recordPositionsFrom(before, sourcePosition);
        }
      }
      // Record the end of the clinit: point to the declaration of the class
      codeStream.recordPositionsFrom(0, declaringType.sourceStart);
      classFile.completeCodeAttributeForClinit(codeAttributeOffset);
    }
  }
View Full Code Here


    classFile.generateMethodInfoHeader(this.binding);
    int methodAttributeOffset = classFile.contentsOffset;
    int attributeNumber = classFile.generateMethodInfoAttributes(this.binding);
    int codeAttributeOffset = classFile.contentsOffset;
    classFile.generateCodeAttributeHeader();
    CodeStream codeStream = classFile.codeStream;
    codeStream.reset(this, classFile);
    // initialize local positions
    this.scope.computeLocalVariablePositions(this.outerLocalVariablesSlotSize + (this.binding.isStatic() ? 0 : 1), codeStream);
    if (this.outerLocalVariables != null) {
      for (int i = 0, max = this.outerLocalVariables.length; i < max; i++) {
        LocalVariableBinding argBinding;
        codeStream.addVisibleLocalVariable(argBinding = this.outerLocalVariables[i]);
        codeStream.record(argBinding);
        argBinding.recordInitializationStartPC(0);
      }
    }
    // arguments initialization for local variable debug attributes
    if (this.arguments != null) {
      for (int i = 0, max = this.arguments.length; i < max; i++) {
        LocalVariableBinding argBinding;
        codeStream.addVisibleLocalVariable(argBinding = this.arguments[i].binding);
        argBinding.recordInitializationStartPC(0);
      }
    }
    if (this.body instanceof Block) {
      this.body.generateCode(this.scope, codeStream);
      if ((this.bits & ASTNode.NeedFreeReturn) != 0) {
        codeStream.return_();
      }
    } else {
      Expression expression = (Expression) this.body;
      expression.generateCode(this.scope, codeStream, true);
      if (this.binding.returnType == TypeBinding.VOID) {
        codeStream.return_();
      } else {
        codeStream.generateReturnBytecode(expression);
      }
    }
    // local variable attributes
    codeStream.exitUserScope(this.scope);
    codeStream.recordPositionsFrom(0, this.sourceEnd); // WAS declarationSourceEnd.
    try {
      classFile.completeCodeAttribute(codeAttributeOffset);
    } catch(NegativeArraySizeException e) {
      throw new AbortMethod(this.scope.referenceCompilationUnit().compilationResult, null);
    }
View Full Code Here

    int constantPoolOffset = constantPool.currentOffset;
    int constantPoolIndex = constantPool.currentIndex;
    classFile.generateMethodInfoHeaderForClinit();
    int codeAttributeOffset = classFile.contentsOffset;
    classFile.generateCodeAttributeHeader();
    CodeStream codeStream = classFile.codeStream;
    resolve(classScope);

    codeStream.reset(this, classFile);
    TypeDeclaration declaringType = classScope.referenceContext;

    // initialize local positions - including initializer scope.
    MethodScope staticInitializerScope = declaringType.staticInitializerScope;
    staticInitializerScope.computeLocalVariablePositions(0, codeStream);

    // 1.4 feature
    // This has to be done before any other initialization
    if (this.assertionSyntheticFieldBinding != null) {
      // generate code related to the activation of assertion for this class
      codeStream.generateClassLiteralAccessForType(
          classScope.outerMostClassScope().enclosingSourceType(),
          this.classLiteralSyntheticField);
      codeStream.invokeJavaLangClassDesiredAssertionStatus();
      BranchLabel falseLabel = new BranchLabel(codeStream);
      codeStream.ifne(falseLabel);
      codeStream.iconst_1();
      BranchLabel jumpLabel = new BranchLabel(codeStream);
      codeStream.decrStackSize(1);
      codeStream.goto_(jumpLabel);
      falseLabel.place();
      codeStream.iconst_0();
      jumpLabel.place();
      codeStream.fieldAccess(Opcodes.OPC_putstatic, this.assertionSyntheticFieldBinding, null /* default declaringClass */);
    }
    // generate static fields/initializers/enum constants
    final FieldDeclaration[] fieldDeclarations = declaringType.fields;
    int sourcePosition = -1;
    int remainingFieldCount = 0;
    if (TypeDeclaration.kind(declaringType.modifiers) == TypeDeclaration.ENUM_DECL) {
      int enumCount = declaringType.enumConstantsCounter;
      if (enumCount > ENUM_CONSTANTS_THRESHOLD) {
        // generate synthetic methods to initialize all the enum constants
        int begin = -1;
        int count = 0;
        if (fieldDeclarations != null) {
          int max = fieldDeclarations.length;
          for (int i = 0; i < max; i++) {
            FieldDeclaration fieldDecl = fieldDeclarations[i];
            if (fieldDecl.isStatic()) {
              if (fieldDecl.getKind() == AbstractVariableDeclaration.ENUM_CONSTANT) {
                if (begin == -1) {
                  begin = i;
                }
                count++;
                if (count > ENUM_CONSTANTS_THRESHOLD) {
                  SyntheticMethodBinding syntheticMethod = declaringType.binding.addSyntheticMethodForEnumInitialization(begin, i);
                  codeStream.invoke(Opcodes.OPC_invokestatic, syntheticMethod, null /* default declaringClass */);
                  begin = i;
                  count = 1;
                }
              } else {
                remainingFieldCount++;
              }
            }
          }
          if (count != 0) {
            // add last synthetic method
            SyntheticMethodBinding syntheticMethod = declaringType.binding.addSyntheticMethodForEnumInitialization(begin, max);
            codeStream.invoke(Opcodes.OPC_invokestatic, syntheticMethod, null /* default declaringClass */);
          }
        }
      } else if (fieldDeclarations != null) {
        for (int i = 0, max = fieldDeclarations.length; i < max; i++) {
          FieldDeclaration fieldDecl = fieldDeclarations[i];
          if (fieldDecl.isStatic()) {
            if (fieldDecl.getKind() == AbstractVariableDeclaration.ENUM_CONSTANT) {
              fieldDecl.generateCode(staticInitializerScope, codeStream);
            } else {
              remainingFieldCount++;
            }
          }
        }
      }
      // enum need to initialize $VALUES synthetic cache of enum constants
      // $VALUES := new <EnumType>[<enumCount>]
      codeStream.generateInlinedValue(enumCount);
      codeStream.anewarray(declaringType.binding);
      if (enumCount > 0) {
        if (fieldDeclarations != null) {
          for (int i = 0, max = fieldDeclarations.length; i < max; i++) {
            FieldDeclaration fieldDecl = fieldDeclarations[i];
            // $VALUES[i] = <enum-constant-i>
            if (fieldDecl.getKind() == AbstractVariableDeclaration.ENUM_CONSTANT) {
              codeStream.dup();
              codeStream.generateInlinedValue(fieldDecl.binding.id);
              codeStream.fieldAccess(Opcodes.OPC_getstatic, fieldDecl.binding, null /* default declaringClass */);
              codeStream.aastore();
            }
          }
        }
      }
      codeStream.fieldAccess(Opcodes.OPC_putstatic, declaringType.enumValuesSyntheticfield, null /* default declaringClass */);
      if (remainingFieldCount != 0) {
        // if fields that are not enum constants need to be generated (static initializer/static field)
        for (int i = 0, max = fieldDeclarations.length; i < max && remainingFieldCount >= 0; i++) {
          FieldDeclaration fieldDecl = fieldDeclarations[i];
          switch (fieldDecl.getKind()) {
            case AbstractVariableDeclaration.ENUM_CONSTANT :
              break;
            case AbstractVariableDeclaration.INITIALIZER :
              if (!fieldDecl.isStatic()) {
                break;
              }
              remainingFieldCount--;
              sourcePosition = ((Initializer) fieldDecl).block.sourceEnd;
              fieldDecl.generateCode(staticInitializerScope, codeStream);
              break;
            case AbstractVariableDeclaration.FIELD :
              if (!fieldDecl.binding.isStatic()) {
                break;
              }
              remainingFieldCount--;
              sourcePosition = fieldDecl.declarationEnd;
              fieldDecl.generateCode(staticInitializerScope, codeStream);
              break;
          }
        }
      }
    } else {
      if (fieldDeclarations != null) {
        for (int i = 0, max = fieldDeclarations.length; i < max; i++) {
          FieldDeclaration fieldDecl = fieldDeclarations[i];
          switch (fieldDecl.getKind()) {
            case AbstractVariableDeclaration.INITIALIZER :
              if (!fieldDecl.isStatic())
                break;
              sourcePosition = ((Initializer) fieldDecl).block.sourceEnd;
              fieldDecl.generateCode(staticInitializerScope, codeStream);
              break;
            case AbstractVariableDeclaration.FIELD :
              if (!fieldDecl.binding.isStatic())
                break;
              sourcePosition = fieldDecl.declarationEnd;
              fieldDecl.generateCode(staticInitializerScope, codeStream);
              break;
          }
        }
      }
    }

    if (codeStream.position == 0) {
      // do not need to output a Clinit if no bytecodes
      // so we reset the offset inside the byte array contents.
      classFile.contentsOffset = clinitOffset;
      // like we don't addd a method we need to undo the increment on the method count
      classFile.methodCount--;
      // reset the constant pool to its state before the clinit
      constantPool.resetForClinit(constantPoolIndex, constantPoolOffset);
    } else {
      if ((this.bits & ASTNode.NeedFreeReturn) != 0) {
        int before = codeStream.position;
        codeStream.return_();
        if (sourcePosition != -1) {
          // expand the last initializer variables to include the trailing return
          codeStream.recordPositionsFrom(before, sourcePosition);
        }
      }
      // Record the end of the clinit: point to the declaration of the class
      codeStream.recordPositionsFrom(0, declaringType.sourceStart);
      classFile.completeCodeAttributeForClinit(codeAttributeOffset);
    }
  }
View Full Code Here

TOP

Related Classes of org.eclipse.jdt.internal.compiler.codegen.CodeStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.