Code Generation for Ptolemy II

Download Report

Transcript Code Generation for Ptolemy II

Code Generation for Ptolemy II
Ye Zhou, Man-Kit Leung, Christopher Brooks, Edward Lee, Gang Zhou
University of California, Berkeley
Project Overview
1.
2.
3.
4.
The Butterfly Example
Generate code in other languages such as C and VHDL for a Ptolemy model.
Build a code generation infrastructure for component-based and heterogeneous modeling.
Based on code generation work in Ptolemy Classic.
Current work focuses on generating C code for synchronous dataflow (SDF) models.
Code Generator Architecture
Code Generator
A Ptolemy Model
invokes
Associate
Director Helper
invokes
Associate
Actor Helper
The Ptolemy Model
C Code Output
Macros and Variable Declarations
• Helper classes contain code blocks written in
pseudo language with interspersed macros.
• Macros, which always begin with a ‘$’, take the
places of ports and parameters to hold values.
• Referenced macros will be replaced with a unique
variable in the generated code.
E.g., the helper class for the Scale actor contains:
1.
2.
3.
The CodeGenerator is like a manager in the
Ptolemy model. It governs the overall code
generation.
A director’s CodeGeneratorHelper defines the order
of code generation for actors, the way of data
exchange, etc.. It also governs time advance, buffer
management, etc..
An actor’s CodeGeneratorHelper contains the basic
C code that implements the function of this actor.
/* variable declarations */
Static int actorFullName_input;
Static int actorFullName_initial = 0;
main() {
/* generate initialize code */
This basically
follows the action
methods in Ptolemy.
The CodeGenerator
invokes the director
helper, which will then
invokes the actor
helpers to generate code.
/* generate body code */
}
/* generate wrapup code */
$ref(output) refers to the output port of this Scale actor.
$val(factor) is the concrete value of the factor parameter
of this actor.
The Code Generator GUI
$ref(output) will be replaced in the C code with a unique
static variable ActorFullName_output.
Buffer Management
Code Structure Example
/*including necessary files*/
#include “stidio.h”
$ref(output) = $val(factor) * $ref(input);
Benefit: actor helper
classes are domainindependent.
• Each actor helper provides an interface to maintain the buffer size
and offset for each of its communication channels.
• The director helper determines the buffer size for each channel.
• The director helper updates the offset of each channel while
generating the code.
Handling Delays in SDF
• The offset of an input port is the
buffer position from which
tokens are read. The offset of an
output port is the buffer position
to which tokens are written.
Generated C Code
Conclusion
• We present an infrastructure for code generation in
Ptolemy II.
• We have implemented a C code generator for SDF
models.
Future Work
• Helper classes for type system.
• Code generation for other domains, such as Giotto
and Finite State Machine.
• Code generation for heterogeneous models.
• Code generation infrastructure for multiple target
languages.