Recent Changes - Search:

edit SideBar

CreatingMacros

This guide discusses how to make macros such as $get in Ptolemy II for code generation.

Macros are used in actor specific .c files by users in order for the code generator to substitute additional code needed. Typical macros that are used are $get and $put to get the data from the input ports of an actor or put data at the output ports.

List of Macros:
$get() ---> ((*(input->get))((struct IOPort*) input, 0)->payload.Boolean)
$getNoPayload() ---> (*(input->get))((struct IOPort*) input, 0)

There are four files used for macro creation in Ptolemy II: $PTII/ptolemy/cg/kernel/generic/program/TemplateParser.java $PTII/ptolemy/cg/adapter/generic/program/procedural/adapters/ptolemy/actor/IOPort.java $PTII/ptolemy/cg/kernel/generic/PortCodeGenerator.java $PTII/ptolemy/cg/adapter/generic/program/procedural/c/adapters/ptolemy/actor/IOPort.java

In $PTII/ptolemy/cg/kernel/generic/program/TemplateParser.java: Add macro name here as this is where it checks whether the macro is $get, $getNoPayload, etc..:

       if (macro.equals("get")) {
            return _replaceGetMacro(parameter);
        } else if (macro.equals("getNoPayload")) {
            return _replaceGetMacro(parameter,false);
        } else if (macro.equals("getAndFree")) {
            return _replaceGetAndFree(parameter);

Create method based on macro definition, for example the method :

      _replaceGetMacro(parameter){
                  ...
           return processCode(portAdapter.generateGetCode(channel, offset));
       }

In $PTII/ptolemy/cg/adapter/generic/program/procedural/adapters/ptolemy/actor/IOPort.java:
Based on the method for the macro definition, create the method for the replacement as this is the code generator adapter file for IOPort. For example, this is where we create the generateGetCode method as in the previous example.

$PTII/ptolemy/cg/kernel/generic/PortCodeGenerator.java:
This is the interface for the port adapters and the method (generateGetCode) should be added here.

$PTII/ptolemy/cg/adapter/generic/program/procedural/c/adapters/ptolemy/actor/IOPort.java"
This is where the code that is substituted for the macro is done. For example, the code substituted for $get is:

       public String generateGetCode(String channel, String offset)
            throws IllegalActionException {

        int channelIndex = Integer.parseInt(channel);

        TypedIOPort port = (TypedIOPort) getComponent();
        Type type = port.getType();
        String typeString = getCodeGenerator().codeGenType(type);
        if (!((ptolemy.actor.IOPort) getComponent()).isOutsideConnected()) {
            return processCode("$new(" + typeString + "(0))->payload."
                    + typeString);
        }
        String result = "(*(" + port.getName() + "->get))((struct IOPort*) "
                + port.getName() + "_X_COMA_X_ " + channelIndex + ")";
        if (type instanceof BaseType) {
            result += "->payload." + typeString;
        } else if (type instanceof RecordType) {
            result += "->payload.Record";
        }

        return result;
    }

After making these changes, users should be able to use macro in the .c files such as in $PTII/cg/adapter/generic/program/procedural/c/adapters/ptolemy/actor/lib.

Edit - History - Print - Recent Changes - Search
Page last modified on April 12, 2015, at 06:52 PM