private void generateForeachRange(ForeachStatement node) {
            Expression range = node.getRange();
            Expression endRange = node.getEndRange();
            Statement init = node.getInitializer();
            Statement body = node.getBody();

            // Holds the value to compare against for when the index has
            // reached the end and looping should stop. Isn't used if the
            // end index has a known value.
            LocalVariable endIndexLocal = null;
            // Only valid if endIndexLocal is null.
            long endIndexValue = 0;

            // Initialize the index and end index local variables.

            final Expression indexExpr;
            Expression endIndexExpr;

            if (!node.isReverse()) {
                indexExpr = range;
                endIndexExpr = endRange;
            }
            else {
                indexExpr = endRange;
                endIndexExpr = range;
            }

            // Feed the loop variable with a value.
            VariableRef loopVarRef = node.getLoopVariable();
            Variable loopVar = loopVarRef.getVariable();
            storeToVariable(loopVar, new Runnable() {
                public void run() {
                    generate(indexExpr);
                }
            });

            boolean longRange = Type.LONG_TYPE.equals(loopVar.getType());

            if (endIndexExpr.isValueKnown()) {
                // End index is known, so don't use local variable to hold it.
                endIndexValue = ((Number)endIndexExpr.getValue()).longValue();
            }
            else {
                generate(endIndexExpr);
                if (longRange) {
                    endIndexLocal = mBuilder.createLocalVariable
                        (null, TypeDesc.LONG);
                }
                else {
                    endIndexLocal = mBuilder.createLocalVariable
                        (null, TypeDesc.INT);
                }
                mBuilder.storeLocal(endIndexLocal);
            }

            // Generate init right before the loop entry point.
            if (init != null) {
                generate(init);
            }

            Label checkLabel = mBuilder.createLabel();
            mBuilder.branch(checkLabel);

            // Loop body begins here.
            Label startLabel = mBuilder.createLabel().setLocation();
            Label continueLabel = mBuilder.createLabel();
            saveContinueLabel(continueLabel);

            if (body != null) {
                generate(body);
            }

            // entry point for continue
            continueLabel.setLocation();


            // Build index adjustment and determine choice comparison.
            String choice;
            if (!node.isReverse()) {
                incrementIntVariable(loopVar, 1);
                choice = "<=";
            }
            else {
                incrementIntVariable(loopVar, -1);
                choice = ">=";
            }

            // Build check.
            checkLabel.setLocation();
            loadFromVariable(loopVar);

            if (endIndexLocal != null) {
                mBuilder.loadLocal(endIndexLocal);
                if (longRange) {
                    mBuilder.math(Opcode.LCMP);
                    mBuilder.ifZeroComparisonBranch(startLabel, choice);
                }
                else {
                    mBuilder.ifComparisonBranch(startLabel, choice);
                }
            }
            else if (longRange) {
                mBuilder.loadConstant(endIndexValue);
                mBuilder.math(Opcode.LCMP);
                mBuilder.ifZeroComparisonBranch(startLabel, choice);
            }
            else if (endIndexValue != 0) {
                mBuilder.loadConstant((int)endIndexValue);
                mBuilder.ifComparisonBranch(startLabel, choice);
            }
            else {
                mBuilder.ifZeroComparisonBranch(startLabel, choice);
            }
        }