Main /
CreatingMacrosThis 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: 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: $PTII/ptolemy/cg/kernel/generic/PortCodeGenerator.java: $PTII/ptolemy/cg/adapter/generic/program/procedural/c/adapters/ptolemy/actor/IOPort.java" 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. |