Ejemplo n.º 1
0
 public void createBCode(CodeGeneration gen) {
   super.createBCode(gen);
   if (hasResult()) {
     TypeDecl type = null;
     BodyDecl b = enclosingBodyDecl();
     if (b instanceof MethodDecl) {
       type = ((MethodDecl) b).type();
     } else {
       throw new Error("Can not create code that returns value within non method");
     }
     getResult().createBCode(gen);
     getResult().type().emitCastTo(gen, type);
     if (!finallyList().isEmpty()) {
       type.emitStoreLocal(gen, resultSaveLocalNum());
     }
     for (Iterator iter = finallyList().iterator(); iter.hasNext(); ) {
       FinallyHost stmt = (FinallyHost) iter.next();
       gen.emitJsr(stmt.label_finally_block());
     }
     if (!finallyList().isEmpty()) {
       type.emitLoadLocal(gen, resultSaveLocalNum());
     }
     type.emitReturn(gen);
   } else {
     for (Iterator iter = finallyList().iterator(); iter.hasNext(); ) {
       FinallyHost stmt = (FinallyHost) iter.next();
       gen.emitJsr(stmt.label_finally_block());
     }
     gen.emitReturn();
   }
 }
Ejemplo n.º 2
0
  // string addition assign expression
  public void createBCode(CodeGeneration gen) {
    TypeDecl dest = getDest().type();
    TypeDecl source = getSource().type();
    if (dest.isString()) {
      getDest().createAssignLoadDest(gen);

      // new StringBuffer()
      TypeDecl stringBuffer = lookupType("java.lang", "StringBuffer");
      String classname = stringBuffer.constantPoolName();
      String desc;
      int index;
      TypeDecl argumentType;
      stringBuffer.emitNew(gen); // new StringBuffer
      gen.emitDup(); // dup
      desc = "()V";
      index = gen.constantPool().addMethodref(classname, "<init>", desc);
      gen.emit(Bytecode.INVOKESPECIAL, -1).add2(index); // invokespecial StringBuffer()

      gen.emitSwap();

      // append
      argumentType = dest.stringPromotion();
      desc = "(" + argumentType.typeDescriptor() + ")" + stringBuffer.typeDescriptor();
      index = gen.constantPool().addMethodref(classname, "append", desc);
      gen.emit(Bytecode.INVOKEVIRTUAL, -argumentType.variableSize())
          .add2(index); // StringBuffer.append

      getSource().createBCode(gen);

      // typed append
      argumentType = source.stringPromotion();
      desc = "(" + argumentType.typeDescriptor() + ")" + stringBuffer.typeDescriptor();
      index = gen.constantPool().addMethodref(classname, "append", desc);
      gen.emit(Bytecode.INVOKEVIRTUAL, -argumentType.variableSize())
          .add2(index); // StringBuffer.append

      // toString
      desc = "()" + type().typeDescriptor();
      index = gen.constantPool().addMethodref(classname, "toString", desc);
      gen.emit(Bytecode.INVOKEVIRTUAL, 0).add2(index); // StringBuffer.toString

      if (needsPush()) {
        getDest().createPushAssignmentResult(gen);
      }
      getDest().emitStore(gen);
    } else {
      super.createBCode(gen);
    }
  }
Ejemplo n.º 3
0
 public void createBCode(CodeGeneration gen) {
   super.createBCode(gen);
   int elseBranch = else_branch_label();
   int thenBranch = then_branch_label();
   int endBranch = hostType().constantPool().newLabel();
   getCondition().emitEvalBranch(gen);
   gen.addLabel(thenBranch);
   // if(getCondition().canBeTrue()) {
   getThen().createBCode(gen);
   if (getThen().canCompleteNormally() && hasElse() /*&& getCondition().canBeFalse()*/)
     gen.emitGoto(endBranch);
   // }
   gen.addLabel(elseBranch);
   if (hasElse() /*&& getCondition().canBeFalse()*/) getElse().createBCode(gen);
   gen.addLabel(endBranch);
 }