at.dms.kjc.common
Class CommonUtils

java.lang.Object
  extended by at.dms.kjc.common.CommonUtils

public class CommonUtils
extends Object

Some public static utility functions pulled out of other routines.


Constructor Summary
CommonUtils()
           
 
Method Summary
static String CTypeToString(CType s, boolean hasBoolType)
          Turn a CType into a string for inclusion in C or C++ code generation.
static String CTypeToStringA(CType s, boolean hasBoolType)
          Turn a CType into a string for inclusion in C or C++ code generation.
static String declToString(CType s, String ident, boolean hasBoolType)
          Returns a declaration for the given type with the given identifier.
static CType getBaseType(CType type)
          Return the underlying CType for a Ctype.
static CType getJoinerType(FlatNode joiner)
          Get the output type of a joiner in a Flatnode representation.
static CType getOutputType(FlatNode node)
          Get the output type of any FlatNode element (filter, splitter, joiner).
static JExpression lhsBaseExpr(JExpression expr)
          Take an expression that could occur on the lhs of an assignment and drill down to find the name of the field or local involved.
static int[] makeArrayInts(JExpression[] dims)
          Make an array of int's from an array of JExpression's for array dimensions.
static JVariableDefinition makeArrayVariableDefn(int numElements, CType elementType, String arrayName)
          Make a JVariableDefinition for a static array.
static int nextPow2(int i)
          Compute the smallest power of 2 that is >= i and >= 0.
static void println_debugging(String s)
          Print a string only if compiling with --debug
static String structToTypedef(SIRStructure strct, boolean hasBoolType)
          Factor out printing (or misprinting) of SIRStruct typedefs from various backends
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CommonUtils

public CommonUtils()
Method Detail

makeArrayInts

public static int[] makeArrayInts(JExpression[] dims)
Make an array of int's from an array of JExpression's for array dimensions.

Parameters:
dims - An array of JExpressions that shuold all be JIntLiteral's
Returns:
array of ints, or asserts that the array has a non-int dimension.

getBaseType

public static CType getBaseType(CType type)
Return the underlying CType for a Ctype. For anything except an array this is no-op. For an array, return the array's base type (which according to CArrayType is not allowed to be another array type).

Parameters:
type - a CType
Returns:
the underlying type

CTypeToString

public static String CTypeToString(CType s,
                                   boolean hasBoolType)
Turn a CType into a string for inclusion in C or C++ code generation. N-dimensional arrays have N "*" in type, this is the type needed if as a return type for a N-dimensional array if you are going to copy it element-wise. Note: SIRStructure type is not handled (and is not a CType)

Parameters:
s - a CType.
hasBoolType - if true then Java 'boolean' becomse 'bool' for C++ if false then Java 'boolean' becomse 'int' for C

CTypeToStringA

public static String CTypeToStringA(CType s,
                                    boolean hasBoolType)
Turn a CType into a string for inclusion in C or C++ code generation. (multi-dimensional arrays have a single "*" in type.) Note: SIRStructure type is not handled (and is not a CType) These types are useful for C or C++ return types you get e.g. int* rather than int[4][4]. This is the correct C++ type for returning an array pointer (actually for returning &array[0][0]). If you want the array type with dimensions, (for a argument type in C++) use declToString as declToString(type, "", tf)

Parameters:
s - a CType.
hasBoolType - if true then Java 'boolean' becomse 'bool' for C++ if false then Java 'boolean' becomse 'int' for C
Returns:
A string representation suitable for C or C++ return types.

declToString

public static String declToString(CType s,
                                  String ident,
                                  boolean hasBoolType)
Returns a declaration for the given type with the given identifier. Returns int x[10][10] for arrays.

Parameters:
s - the type to declare
ident - the identifier to declare
hasBoolType - if true then Java 'boolean' becomse 'bool' for C++ if false then Java 'boolean' becomse 'int' for C

structToTypedef

public static String structToTypedef(SIRStructure strct,
                                     boolean hasBoolType)
Factor out printing (or misprinting) of SIRStruct typedefs from various backends

Parameters:
strct - a SIRStructure to print
hasBoolType - if true then Java 'boolean' becomes 'bool' for C++ if false then Java 'boolean' becomes 'int' for C
Returns:
printable C or C++ representation, no final newline.

lhsBaseExpr

public static JExpression lhsBaseExpr(JExpression expr)
Take an expression that could occur on the lhs of an assignment and drill down to find the name of the field or local involved. These expressions have the form (Local | (This|ClassName).field | ...) ([Expression] | .StructureField)*

Parameters:
expr - An expression that could occur on the left-hand side of an assignment
Returns:
The root field (including this or classname) or local expression, or whatever else was found after peeling off all structure field modifiers and array offset modifiers. (There are assignments of the form SIRPortal = SIRPortalCreation and such like, so this is not guaranteed to return a field or local.)

getJoinerType

public static CType getJoinerType(FlatNode joiner)
Get the output type of a joiner in a Flatnode representation.
The type of a joiner is the output type of the first filter found searching back from the joiner. (Actually a bit more complicated: it searches back in the flat graph for a non-void input to the joiner, and returns that as the joiner type. Only id all inputs to the joiner have output type of void, or are null, will getJoinerType return a void type for the joiner. This complexity is to deal with split-joins that decimiate the initial portion of the data, in which case the initial input to the joiner would be void, where some other input would be non-void.) Edge cases: If you pass this a null joiner, you will get back void. If you pass this method a FlatNode that is not a joiner, the output and side effects are undefined.

Parameters:
joiner - a joiner in a FlatNode representation
Returns:
a CType

getOutputType

public static CType getOutputType(FlatNode node)
Get the output type of any FlatNode element (filter, splitter, joiner). The output type of a filter is stored in the filter. The output type of a splitter is the output type of its incoming edge: If the splitter has 0 total outgoing weight then its incoming edge, if any, should be null, so return the Void type. The output type of a joiner is that of the first filter found above the joiner.

Parameters:
node - a FlatNode (and not null)
Returns:
a CType

println_debugging

public static void println_debugging(String s)
Print a string only if compiling with --debug

Parameters:
s - the debugging string to print.

makeArrayVariableDefn

public static JVariableDefinition makeArrayVariableDefn(int numElements,
                                                        CType elementType,
                                                        String arrayName)
Make a JVariableDefinition for a static array. If you want an initializer, set one in the returned definition. The tricky part here is that the element type may itself be an array type in which case we want to make an array with one more dimension than the element type rather than making an array of arrays.

Parameters:
numElements - Number of elements
elementType - Type of elements
arrayName - Name of array
Returns:
JVariableDefinition, or null if asked for array of 0 elements or array storing void.

nextPow2

public static int nextPow2(int i)
Compute the smallest power of 2 that is >= i and >= 0.

Parameters:
i -
Returns:
the next power of 2.