/** * Generate code for the case where we actually want a boolean value (true or false) computed onto * the stack, eg for assignment to a boolean variable. * * @param output the code emitter (basically an abstraction for producing the .class file). */ public void codegen(CLEmitter output) { String elseLabel = output.createLabel(); String endIfLabel = output.createLabel(); this.codegen(output, elseLabel, false); output.addNoArgInstruction(ICONST_1); // true output.addBranchInstruction(GOTO, endIfLabel); output.addLabel(elseLabel); output.addNoArgInstruction(ICONST_0); // false output.addLabel(endIfLabel); }
/** * The semantics of j-- require that we implement short-circuiting branching in implementing the * logical AND. * * @param output the code emitter (basically an abstraction for producing the .class file). * @param targetLabel target for generated branch instruction. * @param onTrue should we branch on true? */ public void codegen(CLEmitter output, String targetLabel, boolean onTrue) { if (onTrue) { String falseLabel = output.createLabel(); lhs.codegen(output, falseLabel, false); rhs.codegen(output, targetLabel, true); output.addLabel(falseLabel); } else { lhs.codegen(output, targetLabel, false); rhs.codegen(output, targetLabel, false); } }