예제 #1
0
        /// <summary>
        ///		Optimizes the given byte code instruction list.
        /// </summary>
        /// <param name="instructionList">Instruction list to optimize.</param>
        /// <param name="symbolList">List of symbols referenced by the instruction list.</param>
        public void Optimize(ArrayList instructionList, ArrayList symbolList)
        {
            // Store and reset variables for future use.
            _instructionList = instructionList;
            _symbolList = symbolList;
            _totalOptimizationCount = 0;

            // Go through byte code instruction list in until it has been
            // optimized to the best it can be.
            while (true)
            {
                _currentInstruction = null;
                _instructionIndex = 0;
                _optimizationCount = 0;

                while (!EndOfInstructionStream())
                    OptimizeInstruction();

                _totalOptimizationCount += _optimizationCount;

                if (_optimizationCount == 0)
                    break;
            }

            // Remove all NOP's.
        }
예제 #2
0
 /// <summary>
 ///		Creates a new direct stack index operand.
 /// </summary>
 /// <param name="instruction">Instruction to add operand to.</param>
 /// <param name="stackIndex">Index of stack slot this operand should point to.</param>
 /// <param name="register">Register containing index offset.</param>
 /// <returns>New operand instance.</returns>
 public static Operand DirectStackIndexedOperand(Instruction instruction, int stackIndex, Register regiser)
 {
     Operand op = new Operand(instruction, OperandType.DirectStackIndexed);
     op.StackIndex = stackIndex;
     op.OffsetRegister = regiser;
     return op;
 }
예제 #3
0
 /// <summary>
 ///		Creates a new direct memory operand.
 /// </summary>
 /// <param name="instruction">Instruction to add operand to.</param>
 /// <param name="memoryIndex">Index of memory slot this operand should point to.</param>
 /// <returns>New operand instance.</returns>
 public static Operand DirectMemoryOperand(Instruction instruction, int memoryIndex)
 {
     Operand op = new Operand(instruction, OperandType.DirectMemory);
     op.MemoryIndex = memoryIndex;
     return op;
 }
예제 #4
0
 public Operand(Instruction instruction, Symbol symbol)
 {
     _opType = OperandType.SymbolIndexTracker;
     _symbolIndexTracker = symbol;
     instruction[instruction.OperandCount] = this;
 }
예제 #5
0
 public Operand(Instruction instruction, JumpTargetSymbol value)
 {
     _opType = OperandType.JumpTarget;
     _jumpTarget = value;
     instruction[instruction.OperandCount] = this;
 }
예제 #6
0
 /// <summary>
 ///		Returns the next instruction in the list and advances the stream.
 /// </summary>
 /// <returns>Next instruction in the list.</returns>
 private Instruction NextInstruction()
 {
     if (EndOfInstructionStream() == true) return null;
     _currentInstruction = (Instruction)_instructionList[_instructionIndex];
     _instructionIndex++;
     return _currentInstruction;
 }
예제 #7
0
 /// <summary>
 ///		Creates a new symbol index operand.
 /// </summary>
 /// <param name="instruction">Instruction to add operand to.</param>
 /// <param name="symbolIndex">Index of symbol this operand should point to.</param>
 /// <returns>New operand instance.</returns>
 public static Operand SymbolIndexOperand(Instruction instruction, int symbolIndex)
 {
     Operand op = new Operand(instruction, OperandType.SymbolIndex);
     op.SymbolIndex = symbolIndex;
     return op;
 }
예제 #8
0
 /// <summary>
 ///		Creates a new indirect stack operand.
 /// </summary>
 /// <param name="instruction">Instruction to add operand to.</param>
 /// <param name="register">Register containing stack slot this operand should point to.</param>
 /// <returns>New operand instance.</returns>
 public static Operand IndirectStackOperand(Instruction instruction, Register register)
 {
     Operand op = new Operand(instruction, OperandType.IndirectStack);
     op.Register = register;
     return op;
 }
예제 #9
0
 public Operand(Instruction instruction, double value)
 {
     _opType = OperandType.DoubleLiteral;
     _doubleLiteral = value;
     instruction[instruction.OperandCount] = this;
 }
예제 #10
0
 public Operand(Instruction instruction, int value)
 {
     _opType = OperandType.IntegerLiteral;
     _integerLiteral = value;
     instruction[instruction.OperandCount] = this;
 }
예제 #11
0
 public Operand(Instruction instruction, float value)
 {
     _opType = OperandType.FloatLiteral;
     _floatLiteral = value;
     instruction[instruction.OperandCount] = this;
 }
예제 #12
0
 public Operand(Instruction instruction, byte value)
 {
     _opType = OperandType.ByteLiteral;
     _byteLiteral = value;
     instruction[instruction.OperandCount] = this;
 }
예제 #13
0
 public Operand(Instruction instruction, bool value)
 {
     _opType = OperandType.BooleanLiteral;
     _booleanLiteral = value;
     instruction[instruction.OperandCount] = this;
 }
예제 #14
0
 /// <summary>
 ///		Initializes a new Operand instance with the given instruction as a parent
 ///		and a given operand type.
 /// </summary>
 /// <param name="instruction">Instruction this operand is associated with.</param>
 /// <param name="opType">Operand type describing what value this operand holds.</param>
 public Operand(Instruction instruction, OperandType opType)
 {
     _opType = opType;
     instruction[instruction.OperandCount] = this;
 }
예제 #15
0
 /// <summary>
 ///		Creates a new direct stack operand.
 /// </summary>
 /// <param name="instruction">Instruction to add operand to.</param>
 /// <param name="stackIndex">Index of stack slot this operand should point to.</param>
 /// <returns>New operand instance.</returns>
 public static Operand DirectStackOperand(Instruction instruction, int stackIndex)
 {
     Operand op = new Operand(instruction, OperandType.DirectStack);
     op.StackIndex = stackIndex;
     return op;
 }
예제 #16
0
 public Operand(Instruction instruction, short value)
 {
     _opType = OperandType.ShortLiteral;
     _shortLiteral = value;
     instruction[instruction.OperandCount] = this;
 }
예제 #17
0
 /// <summary>
 ///		Creates a new indirect stack indexed operand.
 /// </summary>
 /// <param name="instruction">Instruction to add operand to.</param>
 /// <param name="register">Register containing stack slot this operand should point to.</param>
 /// <param name="offsetRegister">Register containing index offset this operand should point to.</param>
 /// <returns>New operand instance.</returns>
 public static Operand IndirectStackIndexedOperand(Instruction instruction, Register register, Register offsetRegister)
 {
     Operand op = new Operand(instruction, OperandType.IndirectStackIndexed);
     op.Register = register;
     op.OffsetRegister = offsetRegister;
     return op;
 }
예제 #18
0
 public Operand(Instruction instruction, string value)
 {
     _opType = OperandType.StringLiteral;
     _stringLiteral = value;
     instruction[instruction.OperandCount] = this;
 }
예제 #19
0
 /// <summary>
 ///		Creates a new instruction index operand.
 /// </summary>
 /// <param name="instruction">Instruction to add operand to.</param>
 /// <param name="instrIndex">Index of instruction this operand should point to.</param>
 /// <returns>New operand instance.</returns>
 public static Operand InstrIndexOperand(Instruction instruction, int instrIndex)
 {
     Operand op = new Operand(instruction, OperandType.InstrIndex);
     op.InstrIndex = instrIndex;
     return op;
 }
예제 #20
0
 public Operand(Instruction instruction, Register value)
 {
     _opType = OperandType.Register;
     _register = value;
     instruction[instruction.OperandCount] = this;
 }
예제 #21
0
 /// <summary>
 ///		Adds an instruction to this symbols scope.
 /// </summary>
 /// <param name="instruction">Instruction to add to this symbols scope.</param>
 public void AddInstruction(Instruction instruction)
 {
     _instructionList.Add(instruction);
 }
예제 #22
0
        /// <summary>
        ///		Inserts the given instruction at the given index.
        /// </summary>
        /// <param name="index">Index to insert instruction at.</param>
        /// <param name="insertInstruction">Instruction to insert.</param>
        private void InsertInstruction(int index, Instruction insertInstruction)
        {
            // Update the instruction index's of the instruction index operands.
            foreach (Instruction instruction in _instructionList)
                for (int i = 0; i < instruction.OperandCount; i++)
                {
                    Operand operand = instruction[i];
                    if (operand.OpType == OperandType.InstrIndex && operand.InstrIndex > index)
                        operand.InstrIndex++;
                }

            // Update the entry points of the functions.
            foreach (Symbol symbol in _symbolList)
                if (symbol.Type == SymbolType.Function)
                {
                    FunctionSymbol functionSymbol = (FunctionSymbol)symbol;
                    if (functionSymbol.EntryPoint > index && functionSymbol.IsImport == false)
                        functionSymbol.EntryPoint++;
                }

            _instructionList.Insert(index, insertInstruction);
            _instructionIndex += 1;
        }