package code; import env.*; import type.*; import type.basicType.*; import text.*; public class Usage { public static final int MAXREG = 10; public static final int INTREG = 1; public static final int FLOATREG = 2; public static final int TEMPMEM = 3; private long intUsage; private long floatUsage; private long tempUsage; private Env env; public Usage( Env env ) { this.env = env; } public String toString() { return Long.toHexString( intUsage ); } /* public static Usage none( Env env ) { return new Usage( env ); } public static Usage all( Env env ) { Usage all = new Usage( env ); all.intUsage = ( 1 << MAXREG ) - 1; all.floatUsage = ( 1 << MAXREG ) - 1; return all; } */ public static int dataType( Type type ) { if ( type instanceof FloatType ) return FLOATREG; else return INTREG; } public Usage plus( Address address ) { Print.error().debugln( "Usage.plus( " + address + " )" ); if ( address instanceof IntTempReg ) { IntTempReg intTempReg = ( IntTempReg ) address; intUsage |= 1L << intTempReg.regNum(); } else if ( address instanceof FloatTempReg ) { FloatTempReg floatTempReg = ( FloatTempReg ) address; floatUsage |= 1L << floatTempReg.regNum(); } else if ( address instanceof TempMem ) { TempMem tempMem = ( TempMem ) address; tempUsage |= 1L << tempMem.memNum(); } else if ( address instanceof Displacement ) { Displacement displAddr = ( Displacement ) address; plus( displAddr.base() ); } return this; } public Usage minus( Address address ) { Print.error().debugln( "Usage.minus( " + address + " )" ); if ( address instanceof IntTempReg ) { IntTempReg intTempReg = ( IntTempReg ) address; intUsage &= ~ ( 1L << intTempReg.regNum() ); } else if ( address instanceof FloatTempReg ) { FloatTempReg floatTempReg = ( FloatTempReg ) address; floatUsage &= ~ ( 1L << floatTempReg.regNum() ); } else if ( address instanceof TempMem ) { TempMem tempMem = ( TempMem ) address; tempUsage &= ~ ( 1L << tempMem.memNum() ); } else if ( address instanceof Displacement ) { Displacement displAddr = ( Displacement ) address; minus( displAddr.base() ); } return this; } public Address getFree( int dataType ) { switch ( dataType ) { case INTREG: for ( int i = 0; i < MAXREG; i++ ) { if ( ( intUsage & ( 1 << i ) ) == 0 ) { intUsage |= 1L << i; return new IntTempReg( i ); } } break; case FLOATREG: for ( int i = 0; i < MAXREG; i++ ) { if ( ( floatUsage & ( 1 << i ) ) == 0 ) { floatUsage |= 1L << i; return new FloatTempReg( i ); } } break; case TEMPMEM: for ( int i = 0; i < 64; i++ ) { if ( ( tempUsage & ( 1 << i ) ) == 0 ) { tempUsage |= 1L << i; env.addTempUsage( tempUsage ); return new TempMem( i ); } } break; } return null; } public Address getFree( Type type ) { return getFree( dataType( type ) ); } public static int maxTemp( long tempUsage ) { Print.error().debugln( "maxTemp tempUsage = " + Long.toHexString( tempUsage ) ); int maxTemp = 0; for ( int i = 0; i < 64; i++ ) { if ( ( tempUsage & ( 1L << i ) ) != 0 ) maxTemp = i + 1; } return maxTemp; } }