Class CCodeGenerator

    • Field Detail

      • generateCpp

        public Parameter generateCpp
        If true, the generated code will be C++ instead of C. FIXME: This is a temporary fix. In the long run, C++ should be its own target language for code generation. In the short run, this parameter will allow experimentation with C++ code generation, and should identify changes needed for correctly implemented C++ code generation.
    • Constructor Detail

      • CCodeGenerator

        public CCodeGenerator​(NamedObj container,
                              java.lang.String name)
                       throws IllegalActionException,
                              NameDuplicationException
        Create a new instance of the C code generator. This class is the core of the C Code generation For instance, the _generateCode method is key to the code generation.
        Parameters:
        container - The container.
        name - The name of the C code generator.
        Throws:
        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.
    • Method Detail

      • generateConstantDefinition

        public java.lang.String generateConstantDefinition​(java.lang.String constant,
                                                           java.lang.String type,
                                                           java.lang.String value)
        Generate code that defines a constant. In C, generate a #define
        Overrides:
        generateConstantDefinition in class ProgramCodeGenerator
        Parameters:
        constant - The name of the constant to be defined
        type - A string representing the type. In C, this parameter is ignored.
        value - The value of the constant.
        Returns:
        A #define that defines the constant.
      • generateFireFunctionMethodName

        public java.lang.String generateFireFunctionMethodName​(NamedObj namedObj)
                                                        throws IllegalActionException
        Generate the fire function method name. This method is called when the firing code of each actor is not inlined. In this class, each actor's firing code is in a function with the same name as that of the actor, with the "fire" id
        Overrides:
        generateFireFunctionMethodName in class ProgramCodeGenerator
        Parameters:
        namedObj - The named object for which the name is generated.
        Returns:
        The name of the fire function method.
        Throws:
        IllegalActionException - Not thrown in this base class. Derived classes should throw this exception if there are problems accessing the name or generating the name.
      • generateFunctionTable

        public java.lang.String generateFunctionTable​(java.lang.String[] types,
                                                      java.lang.String[] functions)
        Generate the function table. In this base class return the empty string.
        Parameters:
        types - An array of types.
        functions - An array of functions.
        Returns:
        The code that declares functions.
      • generateLineInfo

        public java.lang.String generateLineInfo​(int lineNumber,
                                                 java.lang.String filename)
        Generate line number information. In this class, lines of the format
          #line lineNumber "filename"
          
        are generated for use by the C preprocessor.
        Overrides:
        generateLineInfo in class ProgramCodeGenerator
        Parameters:
        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.
        Returns:
        text that is suitable for the C preprocessor.
      • generateTypeConvertCodeCandH

        public java.lang.String[] generateTypeConvertCodeCandH()
                                                        throws IllegalActionException
        Generate type conversion code. Determine the proper code put into the source to support dynamic type resolution. First, find out the different types used in the model. Second, find out the different polymorphic functions used. (note: types and functions are independent of each other). Third, append code blocks according to the functions used, and read from files according to the types referenced. Fourth, generate type resolution code, which consists of constants (MAX_NUM_TYPE, MAX_NUM_FUNC), the type map, the function map, function definitions read from the files, and function table. Note that this method is really different from the super one because it splits the code in a .c part and a .h part.
        Returns:
        The type resolution code.
        Throws:
        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.
      • invokeGenerateUpdatePortOffsets

        public boolean invokeGenerateUpdatePortOffsets()
        Return true if StaticSchedulingDirector should invoke generateUpdatePortOffsets. Most code generators do not override this method, CCodeGenerator overrides it to return false.
        Overrides:
        invokeGenerateUpdatePortOffsets in class ProgramCodeGenerator
        Returns:
        false, indicating that StaticSchedulingDirector should not invoke StaticSchedulingDirectory.invokeGenerateUpdatePortOffset().
      • markFunctionCalled

        public void markFunctionCalled​(java.lang.String name,
                                       ProceduralTemplateParser templateParser)
                                throws IllegalActionException
        Add called functions to the set of overloaded functions for later use. If the function starts with "Array_", add everything after the "Array_" is added to the set of token functions used.
        Overrides:
        markFunctionCalled in class ProceduralCodeGenerator
        Parameters:
        name - The name of the function, for example "Double_equals"
        templateParser - The corresponding templateParser that contains the codeBlock.
        Throws:
        IllegalActionException - If there is a problem adding a function to the set of overloaded functions.
      • splitLongBody

        public java.lang.String[] splitLongBody​(int linesPerMethod,
                                                java.lang.String prefix,
                                                java.lang.String code)
                                         throws java.io.IOException
        Split a long function body into multiple functions.
        Overrides:
        splitLongBody in class ProgramCodeGenerator
        Parameters:
        linesPerMethod - The number of lines that should go into each method.
        prefix - The prefix to use when naming functions that are created
        code - The method body to be split.
        Returns:
        An array of two Strings, where the first element is the new definitions (if any), and the second element is the new body. If the number of lines in the code parameter is less than linesPerMethod, then the first element will be the empty string and the second element will be the value of the code parameter.
        Throws:
        java.io.IOException - If thrown will reading the code.
      • _finalPassOverCode

        protected java.lang.StringBuffer _finalPassOverCode​(java.lang.StringBuffer code)
                                                     throws IllegalActionException
        Make a final pass over the generated code. Subclass may extend this method to do extra processing to format the output code. If sourceLineBinding is set to true, it will check and insert the appropriate #line macro for each line in the given code. Blank lines are discarded if #line macros are inserted.
        Overrides:
        _finalPassOverCode in class ProgramCodeGenerator
        Parameters:
        code - The given code to be processed.
        Returns:
        The processed code.
        Throws:
        IllegalActionException - If #getOutputFilename() throws it.
      • _generateCode

        protected int _generateCode​(java.lang.StringBuffer code)
                             throws KernelException
        Generate code and append it to the given string buffer. Write the code to the directory specified by the codeDirectory parameter. The file name is a sanitized version of the model name with a suffix that is based on last package name of the generatorPackage parameter. Thus if the codeDirectory is $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.
        Overrides:
        _generateCode in class ProgramCodeGenerator
        Parameters:
        code - The given string buffer.
        Returns:
        The return value of the last subprocess that was executed. or -1 if no commands were executed.
        Throws:
        KernelException - If the target file cannot be overwritten or write-to-file throw any exception.
      • _generateConstructorCode

        protected java.lang.String _generateConstructorCode​(Actor actor)
                                                     throws IllegalActionException
        Generate the code for a constructor.
        Parameters:
        actor - The actor for which constructor code shall be generated.
        Returns:
        The constructor code.
        Throws:
        IllegalActionException - If there is a problem generating the code.
      • _generateConstructorCode

        protected java.lang.String _generateConstructorCode​(CompositeActor actor)
                                                     throws IllegalActionException
        Generate the code for a constructor.
        Parameters:
        actor - The Composite actor for which constructor code shall be generated.
        Returns:
        The constructor code.
        Throws:
        IllegalActionException - If there is a problem generating the code.
      • _generatePortsAccessorsCode

        protected java.lang.String _generatePortsAccessorsCode​(Actor actor)
        Generate the code to access ports for an actor.
        Parameters:
        actor - The actor for which code shall be generated.
        Returns:
        The port accessor code.
      • _generatePortsAccessorsCode

        protected java.lang.String _generatePortsAccessorsCode​(CompositeActor actor)
        Generate the code to access ports for a composite actor.
        Parameters:
        actor - The actor for which code shall be generated.
        Returns:
        The port accessor code.
      • _generatePortsAccessorsDeclaration

        protected java.lang.String _generatePortsAccessorsDeclaration​(Actor actor)
        Generate the code to declare the accessors to ports.
        Parameters:
        actor - The actor for which code shall be generated.
        Returns:
        The port accessor declaration code.
      • _generatePortsAccessorsDeclaration

        protected java.lang.String _generatePortsAccessorsDeclaration​(CompositeActor actor)
        Generate the code to declare the accessors to ports.
        Parameters:
        actor - The composite actor for which code shall be generated.
        Returns:
        The port accessor declaration code.
      • _generateAndWriteActorCode

        protected void _generateAndWriteActorCode​(NamedObj actor,
                                                  NamedProgramCodeGeneratorAdapter directorAdapter,
                                                  CompositeEntity container,
                                                  java.lang.String directory)
                                           throws IllegalActionException
        Generate and write the code for an actor. This method is called by the container actor There are 2 phases in this method : First we generate the code for the actor Then we write the code
        Parameters:
        actor - The actor that needs to be generated
        directorAdapter - 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 files
        Throws:
        IllegalActionException - If anything goes wrong during the generation.
      • _generateAndWriteCompositeActorCode

        protected void _generateAndWriteCompositeActorCode​(CompositeEntity container,
                                                           java.lang.String containerDirectory)
                                                    throws IllegalActionException
        Generate and write the code for a composite actor. This method is called recursively, for any composite actor present in the model. There are 5 phases in this method : First we initialize the parameters for the container Then we generate and write the code for the container Then we call the generation for each contained actor Then we call the generation for the director Finally we generate the makefile corresponding
        Parameters:
        container - The actor that needs to be generated
        containerDirectory - A string describing where to write the files
        Throws:
        IllegalActionException - If anything goes wrong during the generation.
      • _generateBodyCode

        protected java.lang.String _generateBodyCode()
                                              throws IllegalActionException
        Generate the body code that lies between variable declaration and wrapup. This method delegates to the director adapter to generate a main loop. Note : We do not call the super method, because this one is too different
        Overrides:
        _generateBodyCode in class ProgramCodeGenerator
        Returns:
        The generated body code.
        Throws:
        IllegalActionException - If there is no director.
      • _generateDeclareSharedCode

        protected java.lang.String _generateDeclareSharedCode()
                                                       throws IllegalActionException
        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.
        Returns:
        The shared code of the containing composite actor.
        Throws:
        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.
      • _generateIncludeFiles

        protected java.lang.String _generateIncludeFiles​(NamedProgramCodeGeneratorAdapter actorAdapter)
                                                  throws IllegalActionException
        Generate include files. FIXME: State what is included.
        Parameters:
        actorAdapter - The adapter that has the header files.
        Returns:
        The #include statements, surrounded by #ifndef to ensure that the files are included only once.
        Throws:
        IllegalActionException - If the adapter class for some actor cannot be found.
      • _getFireFunctionArguments

        protected java.lang.String _getFireFunctionArguments()
        Return the prototype for fire functions.
        Overrides:
        _getFireFunctionArguments in class ProgramCodeGenerator
        Returns:
        The string"(void)" so as to avoid the avr-gcc 3.4.6 warning: "function declaration isn't a prototype"
      • _printExecutionTime

        protected java.lang.String _printExecutionTime()
        Generate the code for printing the execution time since the code generated by _recordStartTime() was called.
        Overrides:
        _printExecutionTime in class ProgramCodeGenerator
        Returns:
        Return the code for printing the total execution time.
      • _recordStartTime

        protected java.lang.String _recordStartTime()
        Generate the code for recording the current time. This writes current time into a timespec struct called "start".
        Overrides:
        _recordStartTime in class ProgramCodeGenerator
        Returns:
        Return the code for recording the current time.
      • _templateParserClass

        protected java.lang.Class<? extends TemplateParser> _templateParserClass()
        Return the class of the templateParser class. In cse there isn't one return null.
        Overrides:
        _templateParserClass in class ProgramCodeGenerator
        Returns:
        The base class for templateParser.
      • _writeMakefile

        protected void _writeMakefile​(CompositeEntity container,
                                      java.lang.String currentDirectory)
                               throws IllegalActionException
        Read in a template makefile, substitute variables and write the resulting makefile.

        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.homeSystem 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.

        Overrides:
        _writeMakefile in class ProceduralCodeGenerator
        Parameters:
        container - The composite actor for which we generate the makefile
        currentDirectory - The director in which the makefile is to be written.
        Throws:
        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.