at.dms.util
Class Utils

java.lang.Object
  extended by at.dms.util.Utils
All Implemented Interfaces:
DeepCloneable, Serializable
Direct Known Subclasses:
BufferedStaticCommunication, CContext, CMember, CodeSequence, CStdType, CThrowableInfo, CTopLevel, CType, DirectCommunication, DirectCommunication, ExecutionCode, GraphFlattener, IRPrinter, KjcPrettyPrinter, Kopi2SIR, Layout, Layout, NumberGathering, Phylum, PopSimulator, PushSimulator, RateMatch, RawExecutionCode, RawExecutionCode, RawUtil, RemoveGlobals, RemoveGlobals, SinkUnroller, SinkUnroller, Structurer, SwitchCode, SwitchCode, TileCode, TileCode, Util, Util, Util

public abstract class Utils
extends Object
implements Serializable, DeepCloneable

This class defines severals utilities methods used in source code

See Also:
Serialized Form

Field Summary
static LinkedList<SIRStream> EMPTY_LIST
           
static boolean getForLoopCallers
          Set to true to get a stack trace of callers inserted as a comment.
 
Constructor Summary
Utils()
           
 
Method Summary
static String asPercent(double val)
          Returns
static String cellMathEquivalent(JExpression prefix, String ident)
          Return the name of a math method for vector processing using IBM's vector headers for the Cell processor (or null if none)
static String cMathEquivalent(JExpression prefix, String ident)
          Return the name of a math method for emitting C code
static String cppMathEquivalent(JExpression prefix, String ident)
          Return the name of a math method for emitting C++ code
 Object deepClone()
          Returns a deep clone of this object.
protected  void deepCloneInto(Utils other)
          Clones all fields of this into
static boolean equalArrays(int[] a1, int[] a2)
          Returns whether or not two integer arrays have the same length and entries
static void fail(String str)
          Signal a failure with given error message
static String getEnvironmentVariable(String var)
          Returns value of environment variable named
static JExpression getExpression(JStatement orig)
          Given a statement, return the expression that this statement is composed of, if not an expression statement return null.
static boolean hasPeeks(SIRCodeUnit filter)
          Asks the question "Is a peek present in this unit of code?".
static int[] initArray(int n, int val)
          Returns a new array of length n with all values set to val
static JExpression[] initArray(int n, JExpression exp)
          Returns a new array of length n with all values set to val
static JExpression[] initLiteralArray(int n, int val)
          Returns a new array of length n with all values as JIntLiterals set to val
static List<Integer> intArrayToList(int[] arr)
          Returns a list of Integers containing same elements as
static boolean isMathMethod(JExpression prefix, String ident)
          Is the passed method name (broken into prefix and identifier) a Java math method? Limited to those methods that we can emit code for...
static boolean isUniform(JExpression[] arr)
          Returns whether all elements of an array of JExpressions are JLiterals with the same value.
static void kopi_assert(boolean b)
          Check if an assertion is valid
static void kopi_assert(boolean b, String str)
          Check if an assertion is valid with a given error message
static JStatement makeCountdownForLoop(JStatement body, JExpression count, JVariableDefinition loopIndex)
          Returns a block with a loop counter declaration and a for loop that executes body for count number of times.
static String makeDotFileName(String prefix, SIRStream strName)
          Make a name for a dot file from a supplied prefix and the name of the top-level SIRStream.
static JStatement makeForLoop(JStatement body, int count)
          Returns a block with a loop counter declaration and a for loop that executes body for count number of times.
static JStatement makeForLoop(JStatement body, JExpression count)
          Returns a block with a loop counter declaration and a for loop that executes body for count number of times.
static JStatement makeForLoop(JStatement body, JExpression count, JVariableDefinition loopIndex)
          Returns a block with a loop counter declaration and a for loop that executes body for count number of times.
static JStatement makeForLoopFieldIndex(JStatement body, JVariableDefinition var, JExpression count)
          Returns a for loop that uses field var to count count times with the body of the loop being body.
static JStatement makeForLoopLocalIndex(JStatement body, JVariableDefinition local, JExpression count)
          Returns a for loop that uses local var to count count times with the body of the loop being body.
static int nextPow2(int val)
          Returns a power of 2 that is greater than or equal to
static JExpression passThruParens(JExpression orig)
          Return the first non-parentheses expressions contained in
static JStatement peelMarkers(JStatement stmt)
          If the first and last SIRMarker's in
static boolean popBeforePeek(JStatement stmt)
          Returns whether or not there are any pop expressions before peek expressions in the dynamic execution of
static StringBuffer readFile(String fileName)
          Returns the contents of
static JStatement removeUnusedPops(JStatement stmt)
          Returns a version of
static String replaceAll(String orig, String oldSubStr, String newSubStr)
          /** replaces in all occurances.
static void setupDotFileName(int _numSubGraphs)
          Record the number of subgraphs for future dot filenames.
static JExpression simplifyMathMethod(JMethodCallExpression applyMath)
          Simplify an call to a math function with literal arguments.
static String[] splitQualifiedName(String name)
          Splits a string like: "java/lang/System/out" into two strings: "java/lang/System" and "out"
static String[] splitQualifiedName(String name, char separator)
          Splits a string like: "java/lang/System/out" into two strings: "java/lang/System" and "out"
static Object[] toArray(Vector vect, Class type)
          Deprecated.  
static int[] toIntArray(Vector<Integer> vect)
          Creates a int array from a vector.
static
<T> Vector<T>
toVector(T[] array)
          Creates a vector and fills it with the elements of the specified array.
static CType voidToInt(CType type)
          If
static void writeFile(String filename, String str)
          Writes
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

getForLoopCallers

public static boolean getForLoopCallers
Set to true to get a stack trace of callers inserted as a comment. Limitation: only provides info for loops that are created as loops: i.e. those with trip count > 1.


EMPTY_LIST

public static final LinkedList<SIRStream> EMPTY_LIST
Constructor Detail

Utils

public Utils()
Method Detail

kopi_assert

public static final void kopi_assert(boolean b)
Check if an assertion is valid

Throws:
RuntimeException - the entire token reference

kopi_assert

public static final void kopi_assert(boolean b,
                                     String str)
Check if an assertion is valid with a given error message

Throws:
RuntimeException - the entire token reference

fail

public static final void fail(String str)
Signal a failure with given error message

Throws:
RuntimeException - the entire token reference

readFile

public static StringBuffer readFile(String fileName)
                             throws IOException
Returns the contents of
fileName
as a string buffer.

Throws:
IOException

writeFile

public static void writeFile(String filename,
                             String str)
                      throws IOException
Writes
str
to
filename
, overwriting it if it's already there.

Throws:
IOException

replaceAll

public static String replaceAll(String orig,
                                String oldSubStr,
                                String newSubStr)
/** replaces in all occurances. modifies: nothing.
effects: constructs a new String from orig, replacing all occurances of oldSubStr with newSubStr.

Returns:
a copy of orig with all occurances of oldSubStr replaced with newSubStr. if any of arguments are null, returns orig.

simplifyMathMethod

public static JExpression simplifyMathMethod(JMethodCallExpression applyMath)
Simplify an call to a math function with literal arguments. Even if can't simplify, pass back something semantically equivalent to the original expression.

Parameters:
applyMath - application of math function to simplify
Returns:
an expression, possibly a literal value.

isMathMethod

public static boolean isMathMethod(JExpression prefix,
                                   String ident)
Is the passed method name (broken into prefix and identifier) a Java math method? Limited to those methods that we can emit code for...

Parameters:
prefix - JExpression that is method name prefix
ident - String that is method name
Returns:
whether or not method is a math method.

cellMathEquivalent

public static String cellMathEquivalent(JExpression prefix,
                                        String ident)
Return the name of a math method for vector processing using IBM's vector headers for the Cell processor (or null if none)

Parameters:
prefix - Prefix portion of the method name
ident - ident portion of the method name.
Returns:
name or null.

cMathEquivalent

public static String cMathEquivalent(JExpression prefix,
                                     String ident)
Return the name of a math method for emitting C code

Parameters:
prefix - Prefix portion of the method name
ident - ident portion of the method name.
Returns:
name or null.

cppMathEquivalent

public static String cppMathEquivalent(JExpression prefix,
                                       String ident)
Return the name of a math method for emitting C++ code

Parameters:
prefix - Prefix portion of the method name
ident - ident portion of the method name.
Returns:
name or null.

isUniform

public static boolean isUniform(JExpression[] arr)
Returns whether all elements of an array of JExpressions are JLiterals with the same value.

Parameters:
arr -
Returns:

asPercent

public static String asPercent(double val)
Returns
val
as a percentage with maximum of 4 digits


nextPow2

public static int nextPow2(int val)
Returns a power of 2 that is greater than or equal to
val
.


intArrayToList

public static List<Integer> intArrayToList(int[] arr)
Returns a list of Integers containing same elements as
arr


toVector

public static <T> Vector<T> toVector(T[] array)
Creates a vector and fills it with the elements of the specified array.

Parameters:
array - the array of elements

toArray

public static Object[] toArray(Vector vect,
                               Class type)
Deprecated. 

Creates a typed array from a vector. Java 1.5: should use Vector.toArray

Parameters:
vect - the vector containing the elements
type - the type of the elements

toIntArray

public static int[] toIntArray(Vector<Integer> vect)
Creates a int array from a vector. different from Vector.toArray because casts Integer to int

Parameters:
vect - the vector containing the elements

initArray

public static int[] initArray(int n,
                              int val)
Returns a new array of length n with all values set to val

Parameters:
n - the desired number of elements in the array
val - the value of each element

initArray

public static JExpression[] initArray(int n,
                                      JExpression exp)
Returns a new array of length n with all values set to val

Parameters:
n - the desired number of elements in the array
exp - the value of each element

initLiteralArray

public static JExpression[] initLiteralArray(int n,
                                             int val)
Returns a new array of length n with all values as JIntLiterals set to val

Parameters:
n - the desired number of elements in the array
val - the value of each element

equalArrays

public static boolean equalArrays(int[] a1,
                                  int[] a2)
Returns whether or not two integer arrays have the same length and entries


getExpression

public static JExpression getExpression(JStatement orig)
Given a statement, return the expression that this statement is composed of, if not an expression statement return null.

Parameters:
orig - The statement
Returns:
null if
orig
does not contain an expression or the expression if it does.

passThruParens

public static JExpression passThruParens(JExpression orig)
Return the first non-parentheses expressions contained in
orig


splitQualifiedName

public static String[] splitQualifiedName(String name,
                                          char separator)
Splits a string like: "java/lang/System/out" into two strings: "java/lang/System" and "out"


splitQualifiedName

public static String[] splitQualifiedName(String name)
Splits a string like: "java/lang/System/out" into two strings: "java/lang/System" and "out"


peelMarkers

public static JStatement peelMarkers(JStatement stmt)
If the first and last SIRMarker's in
stmt
mark the beginning and end of the same segment, then move those markers to the outermost edges of
stmt
. The purpose of this routine is to lift markers of filter boundaries out of loops.


removeUnusedPops

public static JStatement removeUnusedPops(JStatement stmt)
Returns a version of
stmt
with all standalone pops (i.e., pop() as a statement rather than as an expression) removed.


popBeforePeek

public static boolean popBeforePeek(JStatement stmt)
Returns whether or not there are any pop expressions before peek expressions in the dynamic execution of
stmt
.


makeForLoop

public static JStatement makeForLoop(JStatement body,
                                     int count)
Returns a block with a loop counter declaration and a for loop that executes body for count number of times. If the count is just one, then return the body instead of a loop.


makeForLoop

public static JStatement makeForLoop(JStatement body,
                                     JExpression count)
Returns a block with a loop counter declaration and a for loop that executes body for count number of times.
Optimizes output in cases where count is a literal and == 0 or == 1.


makeForLoop

public static JStatement makeForLoop(JStatement body,
                                     JExpression count,
                                     JVariableDefinition loopIndex)
Returns a block with a loop counter declaration and a for loop that executes body for count number of times. Executes in the forward direction, counting up from 0 to count-1 with loopIndex as the loop counter.
Optimizes if loopIndex == 0 or loopIndex == 1
Note that loopIndex should not appear in a different variable decl; it will get one in this routine.


makeCountdownForLoop

public static JStatement makeCountdownForLoop(JStatement body,
                                              JExpression count,
                                              JVariableDefinition loopIndex)
Returns a block with a loop counter declaration and a for loop that executes body for count number of times. Executes in the backwards direction, counting down from count-1 to zero with loopIndex as the loop counter.
Optimizes in the cases where count is literal and == 0 or == 1.
Note that loopIndex should not appear in a different variable decl; it will get one in this routine.


makeForLoopLocalIndex

public static JStatement makeForLoopLocalIndex(JStatement body,
                                               JVariableDefinition local,
                                               JExpression count)
Returns a for loop that uses local var to count count times with the body of the loop being body. If count is non-positive, just returns empty (!not legal in the general case)

Parameters:
body - The body of the for loop. (null OK: returns empty statement)
local - The local to use as the index variable.
count - The trip count of the loop.
Returns:
The for loop.

makeForLoopFieldIndex

public static JStatement makeForLoopFieldIndex(JStatement body,
                                               JVariableDefinition var,
                                               JExpression count)
Returns a for loop that uses field var to count count times with the body of the loop being body. If count is non-positive, just returns empty (!not legal in the general case)

Parameters:
body - The body of the for loop. (null OK: returns empty statement)
var - The field to use as the index variable.
count - The trip count of the loop.
Returns:
The for loop.

voidToInt

public static CType voidToInt(CType type)
If
type
is void, then return
int
type; otherwise return
type
. This is a hack to get around the disallowance of void arrays in C--should fix this better post-asplos.


getEnvironmentVariable

public static String getEnvironmentVariable(String var)
Returns value of environment variable named
var
, or null if the variable is undefined.


setupDotFileName

public static void setupDotFileName(int _numSubGraphs)
Record the number of subgraphs for future dot filenames.


makeDotFileName

public static String makeDotFileName(String prefix,
                                     SIRStream strName)
Make a name for a dot file from a supplied prefix and the name of the top-level SIRStream.


hasPeeks

public static boolean hasPeeks(SIRCodeUnit filter)
Asks the question "Is a peek present in this unit of code?".
Kopi2SIR seets the peek rate for a filter = max(declared peek rate, declared pop rate). For some optimizations it is of interest whether the code actually contains any peeks, or if it only contains pops.

Parameters:
filter - Anything that implements SIRCodeUnit so that its methods can be scanned.
Returns:
true if a peek is found in any method, false otherwise.

deepClone

public Object deepClone()
Returns a deep clone of this object.

Specified by:
deepClone in interface DeepCloneable

deepCloneInto

protected void deepCloneInto(Utils other)
Clones all fields of this into
other