File indexing completed on 2024-07-21 08:05:26

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by David Saxton                                    *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #ifndef OPTIMIZER_H
0012 #define OPTIMIZER_H
0013 
0014 #include "instruction.h"
0015 
0016 
0017 /// Used for debugging; returns the uchar as a binary string (e.g. 01101010).
0018 QString binary( uchar val );
0019 
0020 
0021 /**
0022 @author David Saxton
0023 */
0024 class Optimizer
0025 {
0026     public:
0027         Optimizer();
0028         ~Optimizer();
0029 
0030         void optimize( Code * code );
0031         
0032     protected:
0033         /**
0034          * Repeatedly generates links and states for the instructions and
0035          * refining their input states, until equilibrium in the input states
0036          * is reached.
0037          */
0038         void propagateLinksAndStates();
0039         /**
0040          * Tell the instructions about their input states.
0041          * @return whether any input states changed from the previous value
0042          * stored in the instruction (if checkChanged is true - else returns
0043          * true).
0044          */
0045         bool giveInputStates();
0046         /**
0047          * Remove instructions without any input links (and the ones that are
0048          * only linked to from a removed instruction).
0049          * @return whether any instructions were removed
0050          */
0051         bool pruneInstructions();
0052         /**
0053          * Perform optimizations (code cropping, modification, assembly, etc)
0054          * based on instruction linkage and processor states.
0055          * @return whether anything was changed
0056          */
0057         bool optimizeInstructions();
0058         /**
0059          * Redirects any GOTOs that point at the given instruction to the given
0060          * label.
0061          * @return whether any GOTOs were redirected
0062          */
0063         bool redirectGotos( Instruction * current, const QString & label );
0064         /**
0065          * Find out if the given instruction or any of its outputs overwrite
0066          * any of the bits of the given register before they are used.
0067          */
0068         uchar generateRegisterDepends( Instruction * current, const Register & reg );
0069         /**
0070          * This function should only be used from generateRegisterDepends.
0071          * Recursively looks at the output links of the given instruction, and
0072          * returns which bits are eventually used before being overwritten.
0073          */
0074         uchar registerDepends( Instruction * current, const Register & reg );
0075         /**
0076          * We often need to know whether removing an instruction will affect the
0077          * future processor state. This function looks are all possible future
0078          * dependencies of the given register, and returns true if the removal
0079          * of the instruction will have no critical effect.
0080          * @param bitMask only look at the given bits of the register
0081          */
0082         bool canRemove( Instruction * ins, const Register & reg, uchar bitMask = 0xff );
0083         
0084         Code * m_pCode;
0085 };
0086 
0087 #endif