mapss.dif.softsyn.c
Class DIFtoC

java.lang.Object
  extended by mapss.dif.softsyn.c.DIFtoC
Direct Known Subclasses:
DIFtoCsrw

public class DIFtoC
extends java.lang.Object

DIF-to-C code generation (software synthesis) supports: SDF, hierarchical scheduling (with flatten option), semi-circular buffering, read/write pointers with modulo operation -- SAS and MAS.

1. Buffer declaration for an edge uses edge's name as buffer variable.

2. Read and write pointers for an edge buffer have suffix "r" and "w". Note that this may cause naming ambiguity.

3. Subhierarchy is generated as a subroutine (C function). Subroutine parameter types use ports' DIFAttributeType.datatype parameters. Subroutine parameter id use ports' name.

4. Actor attribute types, INPUT, OUTPUT, and PARAMETER, are usually associated when attribute's value is Edge or Port object. However, it is not necessary to specify INPUT, OUTPUT, and PARAMETER for software synthesis (code generation). But they may be useful in exporting, importing, and porting.

5. In DIF specification, the first attribute of an actor is its computation. The following actor attributes must match the function's parameter order defined in function prototype.

6. In dif specification, each graph must have a unique name. Several supernodes based on the same subgraph functionality still have different sub-routine functions for them. The derived class may improve it. Be aware of local variable that may need to be static.

7. Datatype of edges and ports are int, float, double, short, or static int, static float, ... Do not add '*' in datatype specification in DIF. The derived class may parse the datatype String for more complicated declaration.

8. User-defined attribute datatype is preserved to specify the datatype of an edge. 9. Source and sink of an edge must produces and consumes data in the same type. There is no way in C to cast an array to another type. 10. Sub-routine (function) parameters corresponding to ports are always pointer types. This class will automatically add '*'.

11. Array and matrix must set as a parameter in DIF instead of directly used in attribute value.

12. I/O parameters of functions are assumed to have types matching the connected edges' types. In this case, it is not necessary to specify the datatype fields of actor attributes. The derived class can add methods to check the consistency of edges' datatypes and I/O parameters' datatypes.

13. String in DIF value will be directly generated as its content instead of "...".

14. Adjusting read and write pointers performs modulo operation.

15. Semi-circular buffering: Buffer size for an edge e is maxTokne(e,S) + max(prd(e), cns(e)) -1 for the worst case MAS or delay situation in wrap around access.

16. Semi-circular buffering: If at certain instance, the actor consumes or produces tokens circled around the circular buffer, a necessary amount of tokens are copied from front to the extra space for being consumed before function call or a necessary amount of tokens are copied from the extra space to front after the function call.

17. User-defined attribute: include, priorExpression, and postExpression can be used in DIF specification to specify include files, expression before the main execution, and expression after the main execution (ex: file close).

Version:
$Id: DIFtoC.java 1688 2007-05-09 02:22:30Z jerryhsu $
Author:
Chia-Jui Hsu

Field Summary
protected  boolean _ansic
           
protected  java.util.Map _buffers
           
protected  CGenerator _generator
           
protected  DIFHierarchy _hierarchy
           
protected  java.lang.String _IDX
           
protected  java.lang.String _INT
           
protected  int _level
           
protected  java.lang.String _RP
           
protected  Scheduler _scheduler
           
protected  java.util.HashMap _subSchedules
           
protected  java.lang.String _TEMPIDX
           
protected  java.lang.String _WP
           
 
Constructor Summary
DIFtoC(DIFHierarchy hierarchy, Scheduler scheduler, java.lang.String cFile)
          Constructor.
 
Method Summary
protected  void _calculateBuffers(DIFHierarchy hierarchy, mocgraph.sched.Schedule schedule)
          Calculate buffer memory requirements by SynthesisUtilities.simulateBuffer(mapss.dif.csdf.sdf.SDFGraph, mocgraph.sched.Schedule) for all eges in hierarchy.getGraph() given the schedule.
protected  void _generateBuffers(DIFHierarchy hierarchy, mocgraph.sched.Schedule schedule)
          This method declaress buffers, such as: int e1[6], for all edges according to the results computed by _calculateBuffers.
protected  void _generateFiring(DIFHierarchy hierarchy, mocgraph.sched.Firing firing)
          Generate function call for an actor firing.
protected  void _generateFunctionCall(DIFHierarchy hierarchy, mocgraph.sched.Firing firing)
          Generate function call for an actor whose computation is a library function.
protected  void _generateHierarchy(DIFHierarchy hierarchy)
          Generate C sub-routine for hierarchy.
protected  void _generateIndices(DIFHierarchy hierarchy, mocgraph.sched.Schedule schedule)
          Generate indices of for-loops for all subschedules in schedule.
protected  void _generateIOParam(DIFGraph graph, mocgraph.Edge edge, mocgraph.sched.Firing firing, java.util.List params, java.util.List priorExps, java.util.List postExps)
          Generate I/O parameter for function call.
protected  void _generateIOParam(DIFHierarchy hierarchy, Port port, mocgraph.sched.Firing firing, java.util.List params, java.util.List priorExps, java.util.List postExps)
          Generate I/O parameter for function call.
protected  void _generateMain(DIFHierarchy hierarchy, mocgraph.sched.Schedule schedule)
          Generate main().
protected  void _generateParameters(DIFHierarchy hierarchy)
          Generate C variable or array for DIFParameter contained in the graph of hierarchy.
protected  void _generatePortAccess(DIFHierarchy hierarchy)
          For all ports in hierarchy, generate read or write pointer for port according to its direction.
protected  void _generatePostExpression(DIFHierarchy hierarchy)
          Generate post expressions before the end of main or subroutine according to the user-defined attribute "postExpression".
protected  void _generatePriorExpression(DIFHierarchy hierarchy)
          Generate prior expressions before the major for loop in main or subroutine according to the user-defined attribute "priorExpression".
protected  void _generatePrologInclude()
          Generate prolog (copyright, author, version) and include according to the user-defined attribute "include".
protected  void _generateSchedule(DIFHierarchy hierarchy, mocgraph.sched.Schedule schedule)
          Generate codes for schedule or subschedule, schedule, of hierarchy.
protected  void _generateSubroutineParams(DIFHierarchy hierarchy, java.util.List types, java.util.List params)
          Generate subroutine parameters and parameters' types.
protected  void _generateSupernodeFiring(DIFHierarchy hierarchy, mocgraph.sched.Firing firing)
          Generate subroutine function call for firing whose firing element is a supernode.
protected  void _generateVariable(java.lang.String name, java.lang.String dataType, java.lang.Object value)
          Generate variable name, with datatype and value.
protected  boolean _isTypeMatch(DIFGraph graph, mocgraph.Edge edge, DIFHierarchy subHierarchy, Port subPort)
          Check whether the datatype of edge is equal to the datatype of subPort.
protected  boolean _isTypeMatch(DIFHierarchy hierarchy, Port port, DIFHierarchy subHierarchy, Port subPort)
          Check whether the datatype of port is equal to the datatype of subPort.
 void generate()
          Generate the c code implementing the input hierarchy.
 void setANSIC(boolean ansic)
          If ansic is true, all variables will be declared before any functional expression.
 void setFlatten(boolean flag)
           
 void setIteration(int iteration)
          Set the number of iteration for the whole schedule.
 void setYear(java.lang.String year)
          Set the copyright year.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_generator

protected CGenerator _generator

_hierarchy

protected DIFHierarchy _hierarchy

_subSchedules

protected java.util.HashMap _subSchedules

_scheduler

protected Scheduler _scheduler

_buffers

protected java.util.Map _buffers

_level

protected int _level

_WP

protected java.lang.String _WP

_RP

protected java.lang.String _RP

_IDX

protected java.lang.String _IDX

_INT

protected java.lang.String _INT

_TEMPIDX

protected java.lang.String _TEMPIDX

_ansic

protected boolean _ansic
Constructor Detail

DIFtoC

public DIFtoC(DIFHierarchy hierarchy,
              Scheduler scheduler,
              java.lang.String cFile)
       throws java.io.IOException
Constructor.

Parameters:
hierarchy -
scheduler - The SDFScheduler.
cFile - The c file name to be generated.
Throws:
java.io.IOException
Method Detail

generate

public void generate()
Generate the c code implementing the input hierarchy. If first schedule the hierarchy. The main() function is generated by invoking _generateMain(mapss.dif.DIFHierarchy, mocgraph.sched.Schedule). For each sub-hierarchy, _generateHierarchy(mapss.dif.DIFHierarchy) is invoked.


setANSIC

public void setANSIC(boolean ansic)
If ansic is true, all variables will be declared before any functional expression. The default is true. Set false to disable it.


setYear

public void setYear(java.lang.String year)
Set the copyright year.

Parameters:
year -

setIteration

public void setIteration(int iteration)
Set the number of iteration for the whole schedule. If iteration is larger than 0. The number of iteration is set to iteration. If iteration is not set, the default is "atoi(argv[1])".

Parameters:
iteration -

setFlatten

public void setFlatten(boolean flag)

_calculateBuffers

protected void _calculateBuffers(DIFHierarchy hierarchy,
                                 mocgraph.sched.Schedule schedule)
Calculate buffer memory requirements by SynthesisUtilities.simulateBuffer(mapss.dif.csdf.sdf.SDFGraph, mocgraph.sched.Schedule) for all eges in hierarchy.getGraph() given the schedule. The results will be set to a private Map _buffers containing all edges in the graph. The key of the Map is Edge object, the entry is Integer buffer size. The derived class can override this class to perform other buffer calculation and put their own entry into the Map.

Parameters:
hierarchy -
schedule -

_generateBuffers

protected void _generateBuffers(DIFHierarchy hierarchy,
                                mocgraph.sched.Schedule schedule)
This method declaress buffers, such as: int e1[6], for all edges according to the results computed by _calculateBuffers. The buffer size is extended for accommodating circulation for delays and MASs. It also declares read and write pointers for all buffers. A read pointer is named by edge name + _RP and a write pointer is named by edge name + _WP. All read pointers are initialized to 0. If edges have initial delays, the corresponding write pointers are set accordingly.

Parameters:
hierarchy -
schedule -

_generateFiring

protected void _generateFiring(DIFHierarchy hierarchy,
                               mocgraph.sched.Firing firing)
Generate function call for an actor firing. If the node of the firing is a supernode, it invokes _generateSupernodeFiring(mapss.dif.DIFHierarchy, mocgraph.sched.Firing). If the node of firing is an actor, it calls _generateFunctionCall(mapss.dif.DIFHierarchy, mocgraph.sched.Firing).

Parameters:
hierarchy -
firing -

_generateFunctionCall

protected void _generateFunctionCall(DIFHierarchy hierarchy,
                                     mocgraph.sched.Firing firing)
Generate function call for an actor whose computation is a library function. The for loop to iterate firing is generated first, and then the subroutine function call. I/O parameters of the function call and the adjusting expression is handled by _generateIOParam(mapss.dif.DIFGraph, mocgraph.Edge, mocgraph.sched.Firing, java.util.List, java.util.List, java.util.List).

Parameters:
graph -
firing -

_generateHierarchy

protected void _generateHierarchy(DIFHierarchy hierarchy)
Generate C sub-routine for hierarchy. Note that only the subschedule corresponding to the graph of hierarchy are generated. This method generates the prototype of the subroutine (fuinction). Subroutine name is the hierarchy name. Subroutine parameters and parameters' types are generated by _generateSubroutineParams(mapss.dif.DIFHierarchy, java.util.List, java.util.List). Port access pointers are generated by _generatePortAccess(mapss.dif.DIFHierarchy). Edge buffer information is calculate by #calculateBuffers. Which can be overrided to perform various computation such as buffer memory requirement, lifetime analysis. Edge buffers are generated by _generateBuffers(mapss.dif.DIFHierarchy, mocgraph.sched.Schedule). Parameters are generated by _generateParameters(mapss.dif.DIFHierarchy). Indices of for loops are generated by _generateIndices(mapss.dif.DIFHierarchy, mocgraph.sched.Schedule). Code implementing the schedule of this hierarchy is generated by _generateSchedule(mapss.dif.DIFHierarchy, mocgraph.sched.Schedule).


_generateIndices

protected void _generateIndices(DIFHierarchy hierarchy,
                                mocgraph.sched.Schedule schedule)
Generate indices of for-loops for all subschedules in schedule.

Parameters:
hierarchy -
schedule -

_generateIOParam

protected void _generateIOParam(DIFGraph graph,
                                mocgraph.Edge edge,
                                mocgraph.sched.Firing firing,
                                java.util.List params,
                                java.util.List priorExps,
                                java.util.List postExps)
Generate I/O parameter for function call. An I/O parameter refers to either an edge buffer or a pointer representing the port of the hierarchy. Because of using read and write pointers, the I/O parameter is generated as: edgeName + readPointer or edgeName + writePointer. It also generates expressions to adjust read and write pointers according to circular buffering.

Parameters:
graph - The graph contains edge.
edge - The edge to which the parameter refers.
firing -
params - The parameter list of the function call.
priorExps - The expresions before firing.
postExps - The expresions after firing, e.g., the pointer adjust expressions of the function call.

_generateIOParam

protected void _generateIOParam(DIFHierarchy hierarchy,
                                Port port,
                                mocgraph.sched.Firing firing,
                                java.util.List params,
                                java.util.List priorExps,
                                java.util.List postExps)
Generate I/O parameter for function call. In this method, an I/O parameter refers to a pointer representing the port of the hierarchy. Because of using read and write pointers, the I/O parameter is generated as: portName + readPointer or portName + writePointer. Since buffers passing to a subhierarchy (subroutine), hierarchy, meet the sizes required by interface actors for repeating its repetition count, port pointers do not need to be adjusted.

Parameters:
hierarchy - The hierarchy contains port.
port - The port to which the parameter refers.
firing -
params - The parameter list of the function call.
priorExps - The expresions before firing.
postExps - The expresions after firing, e.g., the pointer adjust expressions of the function call.

_generateMain

protected void _generateMain(DIFHierarchy hierarchy,
                             mocgraph.sched.Schedule schedule)
Generate main(). Prolog and include is generated by _generatePrologInclude(). Edge buffers of the top level graph are generated by _generateBuffers(mapss.dif.DIFHierarchy, mocgraph.sched.Schedule). Parameters are generated by _generateParameters(mapss.dif.DIFHierarchy). Indices of for loops are generated by _generateIndices(mapss.dif.DIFHierarchy, mocgraph.sched.Schedule). The most outer for loops to iterate schedule is generated according to setIteration(int). Code implementing the schedule of the top level hierarchy is generated by _generateSchedule(mapss.dif.DIFHierarchy, mocgraph.sched.Schedule). A fcloseall() may be generated according to #setAutoFileClose.

Parameters:
hierarchy - The top level hierarchy corresponding to main().
schedule - The schedule of hierarchy.

_generateParameters

protected void _generateParameters(DIFHierarchy hierarchy)
Generate C variable or array for DIFParameter contained in the graph of hierarchy.

Parameters:
hierarchy -

_generatePortAccess

protected void _generatePortAccess(DIFHierarchy hierarchy)
For all ports in hierarchy, generate read or write pointer for port according to its direction. A read pointer is named by port name + _RP and a write pointer is named by port name + _WP. All read and write pointers are initialized to 0.

Parameters:
hierarchy -

_generatePostExpression

protected void _generatePostExpression(DIFHierarchy hierarchy)
Generate post expressions before the end of main or subroutine according to the user-defined attribute "postExpression". attribute postExpression { = "xxx"; } or attribute postExpression { = ("xxx", ..., "xxx"); }

Parameters:
hierarchy -

_generatePriorExpression

protected void _generatePriorExpression(DIFHierarchy hierarchy)
Generate prior expressions before the major for loop in main or subroutine according to the user-defined attribute "priorExpression". attribute priorExpression { = "xxx"; } or attribute priorExpression { = ("xxx", ..., "xxx"); }

Parameters:
hierarchy -

_generatePrologInclude

protected void _generatePrologInclude()
Generate prolog (copyright, author, version) and include according to the user-defined attribute "include". attribute include { = "xxx"; } or attribute include { = ("xxx", ..., "xxx"); }


_generateSchedule

protected void _generateSchedule(DIFHierarchy hierarchy,
                                 mocgraph.sched.Schedule schedule)
Generate codes for schedule or subschedule, schedule, of hierarchy. The for loop to iterate the schedule is generated first. For each element of schedule, if it is a firing, _generateFiring(mapss.dif.DIFHierarchy, mocgraph.sched.Firing) is invoked; if it is a sub-schedule, _generateSchedule(mapss.dif.DIFHierarchy, mocgraph.sched.Schedule) is invoked recursively.

Parameters:
hierarchy -
schedule -

_generateSubroutineParams

protected void _generateSubroutineParams(DIFHierarchy hierarchy,
                                         java.util.List types,
                                         java.util.List params)
Generate subroutine parameters and parameters' types. Subroutine parameter names are port names and parameters' types are datatype attributes of ports. Subroutine parameters are passed by pointers. This method automatically adds '*' before each parameter name. Note that this method does not add '*' after type.

Parameters:
hierarchy -
types - LinkedList of String containing type
params - LinkedList of String containing parameter

_generateSupernodeFiring

protected void _generateSupernodeFiring(DIFHierarchy hierarchy,
                                        mocgraph.sched.Firing firing)
Generate subroutine function call for firing whose firing element is a supernode. The function implementing the sub-hierarchy corresponding to the supernode is generated by _generateHierarchy(mapss.dif.DIFHierarchy), which is independent of this method. The for loop to iterate firing is generated first, and then the subroutine function call. I/O parameters of the function call and the adjusting expression is handled by _generateIOParam(mapss.dif.DIFGraph, mocgraph.Edge, mocgraph.sched.Firing, java.util.List, java.util.List, java.util.List).

Parameters:
hierarchy -
firing -

_generateVariable

protected void _generateVariable(java.lang.String name,
                                 java.lang.String dataType,
                                 java.lang.Object value)
Generate variable name, with datatype and value. The supported value is int[], double[], int[][], double[][], Integer, Double, String (for expression).

Parameters:
name -
datatype -
value -

_isTypeMatch

protected boolean _isTypeMatch(DIFGraph graph,
                               mocgraph.Edge edge,
                               DIFHierarchy subHierarchy,
                               Port subPort)
Check whether the datatype of edge is equal to the datatype of subPort.

Parameters:
graph -
edge -
subHierarchy -
subPort -
Returns:
True if match, otherwise, false.

_isTypeMatch

protected boolean _isTypeMatch(DIFHierarchy hierarchy,
                               Port port,
                               DIFHierarchy subHierarchy,
                               Port subPort)
Check whether the datatype of port is equal to the datatype of subPort. This method does not compare the last '*'.

Parameters:
hierarchy -
port -
subHierarchy -
subPort -
Returns:
True if match, otherwise, false.