Example #1
0
  /** Fifth pass, produce Java byte code. */
  public void byte_code(InstructionList il, MethodGen method, ConstantPoolGen cp) {
    exprs[0].byte_code(il, method, cp);

    if (unop != -1) { // Apply unary operand
      if (unop == MINUS) {
        il.append(InstructionConstants.INEG);
      } else { // == NOT
        il.append(new PUSH(cp, 1));
        ASTFunDecl.push(); // Push TRUE
        il.append(InstructionConstants.IXOR);
        ASTFunDecl.pop();
      }
    } else { // Apply binary operand
      BranchHandle bh = null;

      exprs[1].byte_code(il, method, cp);

      switch (kind) {
        case PLUS:
          il.append(InstructionConstants.IADD);
          ASTFunDecl.pop();
          break;
        case MINUS:
          il.append(InstructionConstants.ISUB);
          ASTFunDecl.pop();
          break;
        case MULT:
          il.append(InstructionConstants.IMUL);
          ASTFunDecl.pop();
          break;
        case DIV:
          il.append(InstructionConstants.IDIV);
          ASTFunDecl.pop();
          break;
        case AND:
          il.append(InstructionConstants.IAND);
          ASTFunDecl.pop();
          break;
        case OR:
          il.append(InstructionConstants.IOR);
          ASTFunDecl.pop();
          break;

          /* Use negated operands */
        case EQ:
          bh = il.append(new IF_ICMPNE(null));
          ASTFunDecl.pop(2);
          break;
        case LEQ:
          bh = il.append(new IF_ICMPGT(null));
          ASTFunDecl.pop(2);
          break;
        case GEQ:
          bh = il.append(new IF_ICMPLT(null));
          ASTFunDecl.pop(2);
          break;
        case NEQ:
          bh = il.append(new IF_ICMPEQ(null));
          ASTFunDecl.pop(2);
          break;
        case LT:
          bh = il.append(new IF_ICMPGE(null));
          ASTFunDecl.pop(2);
          break;
        case GT:
          bh = il.append(new IF_ICMPLE(null));
          ASTFunDecl.pop(2);
          break;
        default:
          System.err.println("Ooops");
      }

      switch (kind) {
        case EQ:
        case LEQ:
        case GEQ:
        case NEQ:
        case LT:
        case GT:
          BranchHandle g;

          il.append(new PUSH(cp, 1));
          g = il.append(new GOTO(null));
          bh.setTarget(il.append(new PUSH(cp, 0)));
          g.setTarget(il.append(InstructionConstants.NOP)); // May be optimized away later
          ASTFunDecl.push();
          break;

        default:
          break;
      }
    }
  }
Example #2
0
  /** Fourth pass, produce Java code. */
  public void code(StringBuffer buf) {
    if (unop != -1) {
      exprs[0].code(buf);
      String top = ASTFunDecl.pop();
      if (unop == MINUS) {
        ASTFunDecl.push(buf, "-" + top);
      } else {
        ASTFunDecl.push(buf, "(" + top + " == 1)? 0 : 1)");
      }
    } else {
      exprs[0].code(buf);
      exprs[1].code(buf);
      String _body_int2 = ASTFunDecl.pop();
      String _body_int = ASTFunDecl.pop();

      switch (kind) {
        case PLUS:
          ASTFunDecl.push(buf, _body_int + " + " + _body_int2);
          break;
        case MINUS:
          ASTFunDecl.push(buf, _body_int + " - " + _body_int2);
          break;
        case MULT:
          ASTFunDecl.push(buf, _body_int + " * " + _body_int2);
          break;
        case DIV:
          ASTFunDecl.push(buf, _body_int + " / " + _body_int2);
          break;

        case AND:
          ASTFunDecl.push(buf, toInt(toBool(_body_int) + " && " + toBool(_body_int2)));
          break;
        case OR:
          ASTFunDecl.push(buf, toInt(toBool(_body_int) + " || " + toBool(_body_int2)));
          break;

        case EQ:
          ASTFunDecl.push(buf, toInt(_body_int + " == " + _body_int2));
          break;
        case LEQ:
          ASTFunDecl.push(buf, toInt(_body_int + " <= " + _body_int2));
          break;
        case GEQ:
          ASTFunDecl.push(buf, toInt(_body_int + " >= " + _body_int2));
          break;
        case NEQ:
          ASTFunDecl.push(buf, toInt(_body_int + " != " + _body_int2));
          break;
        case LT:
          ASTFunDecl.push(buf, toInt(_body_int + " < " + _body_int2));
          break;
        case GT:
          ASTFunDecl.push(buf, toInt(_body_int + " > " + _body_int2));
          break;

        default:
          System.err.println("Ooops");
      }
    }
  }