public class CCodeGenerator extends ProceduralCodeGenerator
NamedObj.ContainedObjectsIterator
Modifier and Type | Field and Description |
---|---|
Parameter |
generateCpp
If true, the generated code will be C++ instead of C.
|
_compileCommandDefault, _includes, _libraries, compile, compileCommand, generateEmbeddedCode
_modifiedVariables, _newTypesUsed, _portTypeMap, _portTypeMap2, _portTypeMap3, _portTypeMaxIndex, _portTypeMaxIndex2, _portTypeMaxIndex3, _primitiveTypes, _runCommandDefault, _tokenFuncUsed, _typeFuncUsed, _variablesAsArrays, generateComment, inline, maximumLinesPerBlock, measureTime, sourceLineBinding, useMake, variablesAsArrays, verbosity
_substituteMap, run, runCommand
_codeFileName, _eol, _executeCommands, _generateInSubdirectory, _model, _sanitizedModelName, codeDirectory, generateInSubdirectory, generatorPackage, generatorPackageList, INDENT1, INDENT2, INDENT3, overwriteFiles
_changeListeners, _changeLock, _changeRequests, _debugging, _debugListeners, _deferChangeRequests, _elementName, _isPersistent, _verbose, _workspace, ATTRIBUTES, CLASSNAME, COMPLETE, CONTENTS, DEEP, FULLNAME, LINKS
Constructor and Description |
---|
CCodeGenerator(NamedObj container,
java.lang.String name)
Create a new instance of the C code generator.
|
Modifier and Type | Method and Description |
---|---|
protected void |
_addActorIncludeDirectories(NamedProgramCodeGeneratorAdapter adapter)
Add include directories specified by the actors in this model.
|
protected void |
_addActorLibraries(NamedProgramCodeGeneratorAdapter adapter)
Add libraries specified by the actors in this model.
|
protected void |
_analyzeTypeConversions()
Analyze the model to find out what connections need to be type
converted.
|
protected java.lang.StringBuffer |
_finalPassOverCode(java.lang.StringBuffer code)
Make a final pass over the generated code.
|
protected void |
_generateAndWriteActorCode(NamedObj actor,
NamedProgramCodeGeneratorAdapter directorAdapter,
CompositeEntity container,
java.lang.String directory)
Generate and write the code for an actor.
|
protected void |
_generateAndWriteCompositeActorCode(CompositeEntity container,
java.lang.String containerDirectory)
Generate and write the code for a composite actor.
|
protected java.lang.String |
_generateBodyCode()
Generate the body code that lies between variable declaration
and wrapup.
|
protected int |
_generateCode(java.lang.StringBuffer code)
Generate code and append it to the given string buffer.
|
protected java.lang.String |
_generateConstructorCode(Actor actor)
Generate the code for a constructor.
|
protected java.lang.String |
_generateConstructorCode(CompositeActor actor)
Generate the code for a constructor.
|
protected java.lang.String |
_generateDeclareSharedCode()
Generate the declarations of the code shared by actors,
including globally defined
data struct types and static methods or variables shared by multiple
instances of the same actor type.
|
protected java.lang.String |
_generateIncludeFiles(NamedProgramCodeGeneratorAdapter actorAdapter)
Generate include files.
|
protected java.lang.String |
_generatePortsAccessorsCode(Actor actor)
Generate the code to access ports for an actor.
|
protected java.lang.String |
_generatePortsAccessorsCode(CompositeActor actor)
Generate the code to access ports for a composite actor.
|
protected java.lang.String |
_generatePortsAccessorsDeclaration(Actor actor)
Generate the code to declare the accessors to ports.
|
protected java.lang.String |
_generatePortsAccessorsDeclaration(CompositeActor actor)
Generate the code to declare the accessors to ports.
|
protected java.lang.String |
_getFireFunctionArguments()
Return the prototype for fire functions.
|
protected java.lang.String |
_printExecutionTime()
Generate the code for printing the execution time since
the code generated by _recordStartTime() was called.
|
protected java.lang.String |
_recordStartTime()
Generate the code for recording the current time.
|
protected java.lang.Class<? extends TemplateParser> |
_templateParserClass()
Return the class of the templateParser class.
|
protected void |
_writeMakefile(CompositeEntity container,
java.lang.String currentDirectory)
Read in a template makefile, substitute variables and write
the resulting makefile.
|
java.lang.Object |
clone(Workspace workspace)
Clone the actor into the specified workspace.
|
java.lang.String |
generateClosingEntryCode()
Return the closing entry code, if any.
|
java.lang.String |
generateClosingExitCode()
Return the closing exit code, if any.
|
java.lang.String |
generateConstantDefinition(java.lang.String constant,
java.lang.String type,
java.lang.String value)
Generate code that defines a constant.
|
java.lang.String |
generateFireFunctionMethodName(NamedObj namedObj)
Generate the fire function method name.
|
java.lang.String |
generateFunctionTable(java.lang.String[] types,
java.lang.String[] functions)
Generate the function table.
|
java.lang.String |
generateInitializeEntryCode()
Generate the initialization procedure entry point.
|
java.lang.String |
generateInitializeExitCode()
Generate the initialization procedure exit point.
|
java.lang.String |
generateInitializeProcedureName()
Generate the initialization procedure name.
|
java.lang.String |
generateLineInfo(int lineNumber,
java.lang.String filename)
Generate line number information.
|
java.lang.String |
generateMainEntryCode()
Generate the main entry point.
|
java.lang.String |
generateMainExitCode()
Generate the main exit point.
|
java.lang.String |
generatePostfireEntryCode()
Generate the postfire procedure entry point.
|
java.lang.String |
generatePostfireExitCode()
Generate the postfire procedure exit point.
|
java.lang.String |
generatePostfireProcedureName()
Generate the postfire procedure name.
|
java.lang.String[] |
generateTypeConvertCodeCandH()
Generate type conversion code.
|
java.lang.String |
generateVariableDeclaration()
Generate variable declarations for inputs and outputs and parameters.
|
java.lang.String |
generateVariableInitialization()
Generate variable initialization for the referenced parameters.
|
java.lang.String |
generateWrapupEntryCode()
Generate the wrapup procedure entry point.
|
java.lang.String |
generateWrapupExitCode()
Generate the wrapup procedure exit point.
|
java.lang.String |
generateWrapupProcedureName()
Generate the wrapup procedure name.
|
java.lang.String |
getModelName()
Generate the model name.
|
boolean |
invokeGenerateUpdatePortOffsets()
Return true if StaticSchedulingDirector should
invoke generateUpdatePortOffsets.
|
void |
markFunctionCalled(java.lang.String name,
ProceduralTemplateParser templateParser)
Add called functions to the set of overloaded functions for
later use.
|
java.lang.String[] |
splitLongBody(int linesPerMethod,
java.lang.String prefix,
java.lang.String code)
Split a long function body into multiple functions.
|
_concatenateElements, _executeCommands, _reset, addInclude, addLibrary, addLibraryIfNecessary
_addActorIncludeDirectories, _addActorLibraries, _codeDirectoryAsFile, _formatComment, _generateIncludeFiles, _generatePreinitializeCode, _generatePreinitializeMethodBodyCode, _generatePreinitializeMethodEntryCode, _generatePreinitializeMethodExitCode, _generatePreinitializeMethodProcedureName, _generateSharedCode, _instantiateAdapter, _setupAdapter, _splitBody, _splitVariableDeclaration, _writeVariableDeclarations, addModifiedVariables, attributeChanged, codeGenType, comment, comment, containsCode, generateFireFunctionCode, generateFireFunctionCompositeEnd, generateFireFunctionCompositeStart, generateFireFunctionMethodInvocation, generateFireFunctionVariableAndMethodName, generateFireFunctionVariableDeclaration, generateInitializeCode, generatePackageStatement, generatePortName, generatePostfireCode, generatePtIOPortName, generatePtIOPortSize, generatePtTypedCompositeActorName, generatePtTypedCompositeActorSize, generateTypeConvertCode, generateVariableName, generateWrapupCode, getModifiedVariables, getTemplateExtension, isPrimitive, isPrimitive, ptolemyType, setModifiedVariables, splitVariableDeclaration, targetType, updateCommandOptions
_runCommand, _setupCommands, _updateSubstituteMap
_copyCFilesTosrc, _copyCFileTosrc, _getAdapter, _getAdapterClassFilter, _getAutoGeneratedAdapter, _getOutputFilename, _isTopLevel, _printTimeAndMemory, _resetAll, _writeCode, _writeCodeFileName, createDecoratorAttributes, decoratedObjects, generateCode, generateCode, generateCode, generateCopyright, getAdapter, getCodeFileName, getComponent, getExecuteCommands, getMethodExceptionString, getMethodVisibilityString, isGlobalDecorator, main, setCodeGenerator, setContainer, setExecuteCommands
_checkContainer, _getContainedObject, _propagateExistence, getContainer, moveDown, moveToFirst, moveToIndex, moveToLast, moveUp, setName, updateContent
_addAttribute, _adjustOverride, _attachText, _cloneFixAttributeFields, _containedDecorators, _copyChangeRequestList, _debug, _debug, _debug, _debug, _debug, _description, _executeChangeRequests, _exportMoMLContents, _getIndentPrefix, _isMoMLSuppressed, _markContentsDerived, _notifyHierarchyListenersAfterChange, _notifyHierarchyListenersBeforeChange, _propagateValue, _removeAttribute, _splitName, _stripNumericSuffix, _validateSettables, addChangeListener, addDebugListener, addHierarchyListener, attributeDeleted, attributeList, attributeList, attributeTypeChanged, clone, containedObjectsIterator, decorators, deepContains, depthInHierarchy, description, description, event, executeChangeRequests, exportMoML, exportMoML, exportMoML, exportMoML, exportMoML, exportMoMLPlain, getAttribute, getAttribute, getAttributes, getChangeListeners, getClassName, getDecoratorAttribute, getDecoratorAttributes, getDerivedLevel, getDerivedList, getDisplayName, getElementName, getFullName, getModelErrorHandler, getName, getName, getPrototypeList, getSource, handleModelError, isDeferringChangeRequests, isOverridden, isPersistent, lazyContainedObjectsIterator, message, notifyOfNameChange, propagateExistence, propagateValue, propagateValues, removeAttribute, removeChangeListener, removeDebugListener, removeHierarchyListener, requestChange, setClassName, setDeferringChangeRequests, setDerivedLevel, setDisplayName, setModelErrorHandler, setPersistent, setSource, sortContainedObjects, toplevel, toString, uniqueName, validateSettables, workspace
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
description, getContainer, getDisplayName, getFullName, getName, getName, setName
public Parameter generateCpp
public CCodeGenerator(NamedObj container, java.lang.String name) throws IllegalActionException, NameDuplicationException
container
- The container.name
- The name of the C code generator.IllegalActionException
- If the super class throws the
exception or error occurs when setting the file path.NameDuplicationException
- If the super class throws the
exception or an error occurs when setting the file path.public java.lang.Object clone(Workspace workspace) throws java.lang.CloneNotSupportedException
clone
in class ProceduralCodeGenerator
workspace
- The workspace for the new object.java.lang.CloneNotSupportedException
- If a derived class contains
an attribute that cannot be cloned.NamedObj.exportMoML(Writer, int, String)
,
NamedObj.setDeferringChangeRequests(boolean)
public java.lang.String generateConstantDefinition(java.lang.String constant, java.lang.String type, java.lang.String value)
generateConstantDefinition
in class ProgramCodeGenerator
constant
- The name of the constant to be definedtype
- A string representing the type. In C, this
parameter is ignored.value
- The value of the constant.public java.lang.String generateFireFunctionMethodName(NamedObj namedObj) throws IllegalActionException
generateFireFunctionMethodName
in class ProgramCodeGenerator
namedObj
- The named object for which the name is generated.IllegalActionException
- Not thrown in this base class.
Derived classes should throw this exception if there are problems
accessing the name or generating the name.public java.lang.String generateFunctionTable(java.lang.String[] types, java.lang.String[] functions)
types
- An array of types.functions
- An array of functions.public java.lang.String generateInitializeEntryCode() throws IllegalActionException
generateInitializeEntryCode
in class ProgramCodeGenerator
IllegalActionException
- Not thrown in this base class.public java.lang.String generateClosingEntryCode()
generateClosingEntryCode
in class ProgramCodeGenerator
public java.lang.String generateClosingExitCode()
generateClosingExitCode
in class ProgramCodeGenerator
public java.lang.String generateInitializeExitCode() throws IllegalActionException
generateInitializeExitCode
in class ProgramCodeGenerator
IllegalActionException
- Not thrown in this base class.public java.lang.String generateInitializeProcedureName() throws IllegalActionException
generateInitializeProcedureName
in class ProgramCodeGenerator
IllegalActionException
- Not thrown in this base class.public java.lang.String generateLineInfo(int lineNumber, java.lang.String filename)
#line lineNumber "filename"are generated for use by the C preprocessor.
generateLineInfo
in class ProgramCodeGenerator
lineNumber
- The line number of the source file or
file containing code blocks.filename
- The name of the source file or file containing
code blocks.public java.lang.String generateMainEntryCode() throws IllegalActionException
generateMainEntryCode
in class ProgramCodeGenerator
IllegalActionException
- Not thrown in this base class.public java.lang.String generateMainExitCode() throws IllegalActionException
generateMainExitCode
in class ProgramCodeGenerator
IllegalActionException
- Not thrown in this base class.public java.lang.String generatePostfireEntryCode() throws IllegalActionException
generatePostfireEntryCode
in class ProgramCodeGenerator
IllegalActionException
- Not thrown in this base class.public java.lang.String generatePostfireExitCode() throws IllegalActionException
generatePostfireExitCode
in class ProgramCodeGenerator
IllegalActionException
- Not thrown in this base class.public java.lang.String generatePostfireProcedureName() throws IllegalActionException
generatePostfireProcedureName
in class ProgramCodeGenerator
IllegalActionException
- Not thrown in this base class.public java.lang.String[] generateTypeConvertCodeCandH() throws IllegalActionException
IllegalActionException
- If an error occurs when generating
the type resolution code, or if the adapter class for the model
director cannot be found, or if an error occurs when the adapter
actor generates the type resolution code.public java.lang.String generateVariableDeclaration() throws IllegalActionException
generateVariableDeclaration
in class ProgramCodeGenerator
IllegalActionException
- If the adapter class for the model
director cannot be found.public java.lang.String generateVariableInitialization() throws IllegalActionException
generateVariableInitialization
in class ProgramCodeGenerator
IllegalActionException
- If the adapter class for the model
director cannot be found.public java.lang.String generateWrapupEntryCode() throws IllegalActionException
generateWrapupEntryCode
in class ProgramCodeGenerator
IllegalActionException
- Not thrown in this base class.public java.lang.String generateWrapupExitCode() throws IllegalActionException
generateWrapupExitCode
in class ProgramCodeGenerator
IllegalActionException
- Not thrown in this base class.public java.lang.String generateWrapupProcedureName() throws IllegalActionException
generateWrapupProcedureName
in class ProgramCodeGenerator
IllegalActionException
- Not thrown in this base class.public java.lang.String getModelName() throws IllegalActionException
IllegalActionException
- Not thrown in this base class.public boolean invokeGenerateUpdatePortOffsets()
invokeGenerateUpdatePortOffsets
in class ProgramCodeGenerator
public void markFunctionCalled(java.lang.String name, ProceduralTemplateParser templateParser) throws IllegalActionException
markFunctionCalled
in class ProceduralCodeGenerator
name
- The name of the function, for example "Double_equals"templateParser
- The corresponding templateParser that contains the
codeBlock.IllegalActionException
- If there is a problem adding
a function to the set of overloaded functions.public java.lang.String[] splitLongBody(int linesPerMethod, java.lang.String prefix, java.lang.String code) throws java.io.IOException
splitLongBody
in class ProgramCodeGenerator
linesPerMethod
- The number of lines that should go into
each method.prefix
- The prefix to use when naming functions that
are createdcode
- The method body to be split.java.io.IOException
- If thrown will reading the code.protected void _addActorIncludeDirectories(NamedProgramCodeGeneratorAdapter adapter) throws IllegalActionException
ProceduralCodeGenerator.addInclude(String)
is called.adapter
- The adapter that has the include directories.IllegalActionException
- If thrown when getting an actor's
include directories.NamedProgramCodeGeneratorAdapter.getIncludeDirectories()
protected void _addActorLibraries(NamedProgramCodeGeneratorAdapter adapter) throws IllegalActionException
adapter
- The adapter that has library directories.IllegalActionException
- If thrown when getting an actor's
libraries.protected void _analyzeTypeConversions() throws IllegalActionException
_analyzeTypeConversions
in class ProgramCodeGenerator
IllegalActionException
- If the adapter of the
top composite actor is unavailable.protected java.lang.StringBuffer _finalPassOverCode(java.lang.StringBuffer code) throws IllegalActionException
_finalPassOverCode
in class ProgramCodeGenerator
code
- The given code to be processed.IllegalActionException
- If #getOutputFilename() throws it.protected int _generateCode(java.lang.StringBuffer code) throws KernelException
$HOME
, the name of the
model is Foo
and the generatorPackage
is ptolemy.codegen.c
, then the file that is
written will be $HOME/Foo.c
This method is the main entry point.
We do not call the super method because it is too different from it.
For instance, there is not only one file generated, but a few.
The code generation algorithm works as followed :
We generate a file with the name of the model (+ .c) and its header
file. In this file we have the implementation of a ptolemy manager in
C (Manager
.
Also for, each Composite Actor (including the top level container)
We generate the files implementing the behavior of the director and
of all the actors. (sources files are in the src directory and header
files in includes directory)
Moreover, for each folder a makefile is generated._generateCode
in class ProgramCodeGenerator
code
- The given string buffer.KernelException
- If the target file cannot be overwritten
or write-to-file throw any exception.protected java.lang.String _generateConstructorCode(Actor actor) throws IllegalActionException
actor
- The actor for which constructor code shall be generated.IllegalActionException
- If there is a problem
generating the code.protected java.lang.String _generateConstructorCode(CompositeActor actor) throws IllegalActionException
actor
- The Composite actor for which constructor code
shall be generated.IllegalActionException
- If there is a problem
generating the code.protected java.lang.String _generatePortsAccessorsCode(Actor actor)
actor
- The actor for which code shall be generated.protected java.lang.String _generatePortsAccessorsCode(CompositeActor actor)
actor
- The actor for which code shall be generated.protected java.lang.String _generatePortsAccessorsDeclaration(Actor actor)
actor
- The actor for which code shall be generated.protected java.lang.String _generatePortsAccessorsDeclaration(CompositeActor actor)
actor
- The composite actor for which code shall be generated.protected void _generateAndWriteActorCode(NamedObj actor, NamedProgramCodeGeneratorAdapter directorAdapter, CompositeEntity container, java.lang.String directory) throws IllegalActionException
actor
- The actor that needs to be generateddirectorAdapter
- The adapter of the director. This
adapter is used to generate variable declarations for the
director.container
- Not used by this method.directory
- The directory path of the sources filesIllegalActionException
- If anything goes wrong during the generation.protected void _generateAndWriteCompositeActorCode(CompositeEntity container, java.lang.String containerDirectory) throws IllegalActionException
container
- The actor that needs to be generatedcontainerDirectory
- A string describing where to write the filesIllegalActionException
- If anything goes wrong during the generation.protected java.lang.String _generateBodyCode() throws IllegalActionException
_generateBodyCode
in class ProgramCodeGenerator
IllegalActionException
- If there is no director.protected java.lang.String _generateDeclareSharedCode() throws IllegalActionException
IllegalActionException
- If an error occurs when generating
the globally shared code, or if the adapter class for the model
director cannot be found, or if an error occurs when the adapter
actor generates the shared code.protected java.lang.String _generateIncludeFiles(NamedProgramCodeGeneratorAdapter actorAdapter) throws IllegalActionException
actorAdapter
- The adapter that has the header files.IllegalActionException
- If the adapter class for some actor
cannot be found.protected java.lang.String _getFireFunctionArguments()
_getFireFunctionArguments
in class ProgramCodeGenerator
protected java.lang.String _printExecutionTime()
_printExecutionTime
in class ProgramCodeGenerator
protected java.lang.String _recordStartTime()
_recordStartTime
in class ProgramCodeGenerator
protected java.lang.Class<? extends TemplateParser> _templateParserClass()
_templateParserClass
in class ProgramCodeGenerator
protected void _writeMakefile(CompositeEntity container, java.lang.String currentDirectory) throws IllegalActionException
If a .mk.in
file with the name of the sanitized model
name, then that file is used as a template. For example, if the
model name is Foo
and the file Foo.mk.in
exists, then the file Foo.mk.in
is used as a makefile
template.
If no .mk.in
file is found, then the makefile
template can be found by looking up a resource name
makefile.in in the package named by the
generatorPackage parameter. Thus, if the
generatorPackage has the value "ptolemy.codegen.c",
then we look for the resource "ptolemy.codegen.c.makefile.in", which
is usually found as $PTII/ptolemy/codegen/c/makefile.in
.
The makefile is written to a directory named by the codeDirectory parameter, with a file name that is a sanitized version of the model name, and a ".mk" extension. Thus, for a model named "Foo", we might generate a makefile in "$HOME/codegen/Foo.mk".
Under Java under Windows, your $HOME
variable
is set to the value of the user.home
System property,
which is usually something like
C:\Documents and Settings\yourlogin
, thus
for user mrptolemy
the makefile would be
C:\Documents and Settings\mrptolemy\codegen\Foo.mk
.
See the parent class
ProceduralCodeGenerator._writeMakefile(CompositeEntity, String)
for variable that are substituted by the parent class.
The following variables are substituted:
PTCG_CFILES
, PTCG_OFILES
,
PTCGCompiler
.
_writeMakefile
in class ProceduralCodeGenerator
container
- The composite actor for which we generate the makefilecurrentDirectory
- The director in which the makefile is to be written.IllegalActionException
- If there is a problem reading
a parameter, if there is a problem creating the codeDirectory directory
or if there is a problem writing the code to a file.