001    /*
002     *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003     *
004     *  This file is licensed to You under the Eclipse Public License (EPL);
005     *  You may not use this file except in compliance with the License. You
006     *  may obtain a copy of the License at
007     *
008     *      http://www.opensource.org/licenses/eclipse-1.0.php
009     *
010     *  See the COPYRIGHT.txt file distributed with this work for information
011     *  regarding copyright ownership.
012     */
013    package org.jikesrvm.compilers.opt.instrsched;
014    
015    import org.jikesrvm.compilers.opt.ir.BasicBlock;
016    import org.jikesrvm.compilers.opt.ir.Instruction;
017    import org.jikesrvm.compilers.opt.ir.InstructionEnumeration;
018    
019    /**
020     * Default (IR-order) instruction list
021     * Used by the scheduler to enumerate over instructions
022     *
023     * @see Priority
024     * @see Scheduler
025     */
026    class DefaultPriority extends Priority {
027      // Underlying enumeration.
028      private final BasicBlock bb;
029      private Instruction i;
030      private InstructionEnumeration instr;
031    
032      /**
033       * Creates new priority object for a given basic block
034       *
035       * @param bb basic block
036       */
037      public DefaultPriority(BasicBlock bb) {
038        this.bb = bb;
039      }
040    
041      /**
042       * Resets the enumeration to the first instruction in sequence
043       */
044      public final void reset() {
045        i = bb.firstInstruction();
046        instr = bb.forwardRealInstrEnumerator();
047      }
048    
049      /**
050       * Returns true if there are more instructions, false otherwise
051       *
052       * @return true if there are more instructions, false otherwise
053       */
054      public final boolean hasMoreElements() {
055        return i != null || instr.hasMoreElements();
056      }
057    
058      /**
059       * Returns the next instruction in sequence
060       *
061       * @return the next instruction in sequence
062       */
063      public final Instruction next() {
064        if (i != null) {
065          Instruction r = i;
066          i = null;
067          return r;
068        }
069        return instr.next();
070      }
071    }