ptolemy.copernicus.c
Class MethodCodeGenerator

java.lang.Object
  extended by ptolemy.copernicus.c.MethodCodeGenerator

public class MethodCodeGenerator
extends java.lang.Object

Class that encapsulates functionality for generating C code from a SootMethod.

Since:
Ptolemy II 2.0
Version:
$Id: MethodCodeGenerator.java,v 1.32 2007/12/06 18:28:41 cxh Exp $
Author:
Ankush Varma
Accepted Rating:
Red (ssb)
Proposed Rating:
Red ()

Constructor Summary
MethodCodeGenerator(Context context, java.util.HashMap requiredTypeMap)
          Construct a MethodCodeGenerator object.
 
Method Summary
protected  java.lang.String _comment(java.lang.String text)
          Enclose a given string of text within appropriate delimiters to form a comment in the generated code.
protected  java.lang.String _declareExceptionVariables(ExceptionTracker tracker)
          Generate the declarations for variables used in exception-tracking.
protected  java.lang.String _generateEpilogue(ExceptionTracker tracker, CSwitch visitor)
          Generate the epilogue.
protected  java.lang.String _generateExceptionMap(ExceptionTracker tracker, CSwitch visitor)
          Generate the exception map.
protected  java.lang.String _generateLocal(soot.SootMethod method, java.util.HashSet parameterAndThisLocals)
          Generate code to declare and initialize local variables.
protected  java.lang.String _generateMethodBody(soot.SootMethod method, CSwitch visitor, ExceptionTracker tracker, java.lang.String thisLocalName)
          Generate the code for the body of a method.
protected  java.lang.String _generateMethodDeclaration(soot.SootMethod method, java.util.HashSet parameterAndThisLocals, java.lang.String thisLocalName)
          Generate the method header for the code.
protected  java.lang.String _generateMethodPrologue(ExceptionTracker tracker, CSwitch visitor)
          Generate prologue code to be inserted in a method for exception-catching.
protected  java.lang.String _generateMethodUnitCode(ExceptionTracker tracker, CSwitch visitor, soot.SootMethod method, byte indentLevel)
          Generate the method unit code and code for handling exceptions.
protected  java.lang.String _indent(int level)
          Return a string that generates an indentation string (a sequence of spaces) for the given indentation level.
protected  void _initializeLabels(CSwitch visitor, ExceptionTracker tracker, soot.SootMethod method)
          Initialize the labels for branch targets in the method.
protected  void _updateRequiredTypes(soot.Type type)
          Register a type as a type that must be imported into the generated code through an #include directive.
 java.lang.String generate(soot.SootMethod method)
          Generate code for a method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MethodCodeGenerator

public MethodCodeGenerator(Context context,
                           java.util.HashMap requiredTypeMap)
Construct a MethodCodeGenerator object.

Parameters:
context - a Context object
requiredTypeMap - a HashMap object
Method Detail

generate

public java.lang.String generate(soot.SootMethod method)
Generate code for a method.

Parameters:
method - The method.
Returns:
The code.

_comment

protected final java.lang.String _comment(java.lang.String text)
Enclose a given string of text within appropriate delimiters to form a comment in the generated code.

Parameters:
text - The text to place in the generated comment.
Returns:
The generated comment.

_declareExceptionVariables

protected java.lang.String _declareExceptionVariables(ExceptionTracker tracker)
Generate the declarations for variables used in exception-tracking.

Parameters:
tracker - The ExceptionTracker for the method for which the declarations are needed.
Returns:
A string containing these declarations.

_generateEpilogue

protected java.lang.String _generateEpilogue(ExceptionTracker tracker,
                                             CSwitch visitor)
Generate the epilogue. The method below generates epilogue for the code in a method.

Parameters:
tracker - The ExceptionTracker
visitor - The visitor.
Returns:
The code.

_generateExceptionMap

protected java.lang.String _generateExceptionMap(ExceptionTracker tracker,
                                                 CSwitch visitor)
Generate the exception map. The method below generates exception map for the code.

Parameters:
tracker - The ExceptionTracker
visitor - The visitor
Returns:
The code.

_generateLocal

protected java.lang.String _generateLocal(soot.SootMethod method,
                                          java.util.HashSet parameterAndThisLocals)
Generate code to declare and initialize local variables. The method below generates local declarations for the code, and initializes them to NULL pointers if they are RefTypes. If they are not Reftypes, they are initialized to 0.

Parameters:
method - The method for which declarations are needed.
parameterAndThisLocals - The parameters of this method, and variables that are local to the instance of the class to which this method belongs. These variables do NOT need to be declared or initialized by this method.
Returns:
The code.

_generateMethodBody

protected java.lang.String _generateMethodBody(soot.SootMethod method,
                                               CSwitch visitor,
                                               ExceptionTracker tracker,
                                               java.lang.String thisLocalName)
Generate the code for the body of a method.

Parameters:
method - The method for which code is needed.
visitor - The visitor.
tracker - The ExceptionTracker.
thisLocalName - The local name.
Returns:
The code.

_generateMethodDeclaration

protected java.lang.String _generateMethodDeclaration(soot.SootMethod method,
                                                      java.util.HashSet parameterAndThisLocals,
                                                      java.lang.String thisLocalName)
Generate the method header for the code. The method below generates the method header.

Parameters:
method - The method.
parameterAndThisLocals - The set of parameters and local variables for this method.
thisLocalName - The local name.
Returns:
The code for the method's declaration(its head).

_generateMethodPrologue

protected java.lang.String _generateMethodPrologue(ExceptionTracker tracker,
                                                   CSwitch visitor)
Generate prologue code to be inserted in a method for exception-catching.

Parameters:
tracker - The ExceptionTracker that has the information for exceptions in this method.
visitor - The local CSwitch visitor object.
Returns:
The prologue code.

_generateMethodUnitCode

protected java.lang.String _generateMethodUnitCode(ExceptionTracker tracker,
                                                   CSwitch visitor,
                                                   soot.SootMethod method,
                                                   byte indentLevel)
Generate the method unit code and code for handling exceptions.

Parameters:
tracker - The ExceptionTracker object handing exceptions here.
visitor - The CSwitch visitor.
method - The method for which code is needed.
indentLevel - The level of indentation needed in each statement.
Returns:
The code.

_indent

protected java.lang.String _indent(int level)
Return a string that generates an indentation string (a sequence of spaces) for the given indentation level. Each indentation level unit is four characters wide.

Parameters:
level - The indentation level.
Returns:
The indentation string that corresponds to the given indentation level. Same as the corresponding method in CodeGenerator

_initializeLabels

protected void _initializeLabels(CSwitch visitor,
                                 ExceptionTracker tracker,
                                 soot.SootMethod method)
Initialize the labels for branch targets in the method.

Parameters:
visitor - The visitor design pattern.
tracker - The ExceptionTracker.
method - The method for which labels need to be initialized.

_updateRequiredTypes

protected void _updateRequiredTypes(soot.Type type)
Register a type as a type that must be imported into the generated code through an #include directive. The request is processed only if the argument is a RefType, or if it is an ArrayType with a RefType as the base type. All other requests are ignored. Duplicate requests are also ignored.

Parameters:
type - The type. Same as the corresponding method in CodeGenerator