/** * Generates a sequence of bytescodes * * @param code the code list */ public void genCode(GenerationContext context) { CodeSequence code = context.getCodeSequence(); setLineNumber(code); code.pushContext(this); if (cond.isConstant() && cond.booleanValue()) { // while (true) code.plantLabel(getContinueLabel()); // body: LocalVariableScope scope = new LocalVariableScope(); code.pushLocalVariableScope(scope); body.genCode(context); // BODY CODE code.popLocalVariableScope(scope); code.plantJumpInstruction(opc_goto, getContinueLabel()); // GOTO body } else { CodeLabel bodyLabel = new CodeLabel(); code.plantJumpInstruction(opc_goto, getContinueLabel()); // GOTO cont code.plantLabel(bodyLabel); // body: LocalVariableScope scope = new LocalVariableScope(); code.pushLocalVariableScope(scope); body.genCode(context); // BODY CODE code.popLocalVariableScope(scope); code.plantLabel(getContinueLabel()); // cont: cond.genBranch(true, context, bodyLabel); // EXPR CODE; IFNE body } code.plantLabel(getBreakLabel()); // end: ... code.popContext(this); }
/** Optimize a bi-conditional expression */ protected void genBranch( JExpression left, JExpression right, boolean cond, GenerationContext context, CodeLabel label) { CodeSequence code = context.getCodeSequence(); if (cond) { CodeLabel skip = new CodeLabel(); left.genBranch(false, context, skip); right.genBranch(true, context, label); code.plantLabel(skip); } else { left.genBranch(false, context, label); right.genBranch(false, context, label); } }