Revise code gen for port calls

This commit is contained in:
Rob Bocchino 2025-11-17 12:58:26 -08:00
parent d0738bb807
commit 72012e49c4
4 changed files with 80 additions and 27 deletions

View File

@ -58,6 +58,15 @@ case class TopComponentCppWriter (
)
}
private def writePortNumAssertion(numPorts: String) =
lines(
s"""|FW_ASSERT(
| (0 <= portNum) && (portNum < $numPorts),
| static_cast<FwAssertArgType>(portNum),
| static_cast<FwAssertArgType>($numPorts)
|);"""
)
private def writeIsConnectedFnBody(
portName: Name.Unqualified,
componentInstanceMap: TopComponents.ComponentInstanceMap
@ -65,10 +74,10 @@ case class TopComponentCppWriter (
val portInstance = component.portMap(portName)
val numPorts = numPortsConstantName(portInstance)
List.concat(
writePortNumAssertion(numPorts),
lines(
s"""|FW_ASSERT((0 <= portNum) && (portNum < $numPorts), static_cast<FwAssertArgType>(portNum), static_cast<FwAssertArgType>($numPorts));
|bool result = false;
|const auto instance = this->getInstance();"""
"""|bool result = false;
|const auto instance = this->getInstance();"""
),
writeInstanceSwitch(
componentInstanceMap,
@ -100,26 +109,34 @@ case class TopComponentCppWriter (
val portInstance = component.portMap(portName)
val numPorts = numPortsConstantName(portInstance)
val returnType = getInvokerReturnTypeAsString(portInstance)
val returnsNonVoid = returnType != "void"
val nonVoidReturn = returnType != "void"
List.concat(
lines(
s"""|FW_ASSERT((0 <= portNum) && (portNum < $numPorts), static_cast<FwAssertArgType>(portNum), static_cast<FwAssertArgType>($numPorts));
|const auto instance = this->getInstance();"""
),
guardedList (returnsNonVoid) (lines(s"$returnType _result = {};")),
writePortNumAssertion(numPorts),
lines("const auto instance = this->getInstance();"),
guardedList (nonVoidReturn) (lines(s"$returnType _result = {};")),
writeInstanceSwitch(
componentInstanceMap,
writeOutFnCase,
lines("FW_ASSERT(0, static_cast<FwAssertArgType>(portNum));")
),
guardedList (returnsNonVoid) (lines(s"return _result;"))
guardedList (nonVoidReturn) (lines(s"return _result;"))
)
}
private def writeOutFnCase(portNum: Int, connection: Connection) = {
val toPort = connection.to.port
val fnName = CppWriter.writeQualifiedName(toPort.componentInstance.qualifiedName)
lines("// TODO")
val componentInstanceName = CppWriter.writeQualifiedName(toPort.componentInstance.qualifiedName)
val portName = toPort.portInstance.getUnqualifiedName
val handlerBaseName = inputPortHandlerBaseName(portName)
val fnName = s"$componentInstanceName.$handlerBaseName"
val returnType = getInvokerReturnTypeAsString(toPort.portInstance)
val nonVoidReturn = returnType != "void"
val addResultPrefix = addConditionalPrefix (nonVoidReturn) ("_result =")
writeFunctionCall(
addResultPrefix(fnName),
List("portNum"),
getPortParams(toPort.portInstance).map(_._1)
)
}
private def writeOutFnForPort(

View File

@ -23,7 +23,7 @@ case class TopComponents(
def getMembers: List[CppDoc.Member] =
wrapMembersInIfDirective(
"#ifdef FW_DIRECT_PORT_CALLS",
"#if FW_DIRECT_PORT_CALLS",
addMemberComment(
"Topology-dependent component implementation",
getComponentMembers,

View File

@ -160,7 +160,7 @@ namespace M {
}
#ifdef FW_DIRECT_PORT_CALLS
#if FW_DIRECT_PORT_CALLS
// ----------------------------------------------------------------------
// Topology-dependent component implementation
@ -169,7 +169,11 @@ namespace M {
namespace M {
bool PassiveComponentBase::isConnected_p_OutputPort(FwIndexType portNum) const {
FW_ASSERT((0 <= portNum) && (portNum < NUM_P_OUTPUT_PORTS), static_cast<FwAssertArgType>(portNum), static_cast<FwAssertArgType>(NUM_P_OUTPUT_PORTS));
FW_ASSERT(
(0 <= portNum) && (portNum < NUM_P_OUTPUT_PORTS),
static_cast<FwAssertArgType>(portNum),
static_cast<FwAssertArgType>(NUM_P_OUTPUT_PORTS)
);
bool result = false;
const auto instance = this->getInstance();
switch (instance) {
@ -199,13 +203,17 @@ namespace M {
}
void PassiveComponentBase::p_out(FwIndexType portNum) const {
FW_ASSERT((0 <= portNum) && (portNum < NUM_P_OUTPUT_PORTS), static_cast<FwAssertArgType>(portNum), static_cast<FwAssertArgType>(NUM_P_OUTPUT_PORTS));
FW_ASSERT(
(0 <= portNum) && (portNum < NUM_P_OUTPUT_PORTS),
static_cast<FwAssertArgType>(portNum),
static_cast<FwAssertArgType>(NUM_P_OUTPUT_PORTS)
);
const auto instance = this->getInstance();
switch (instance) {
case ::M::InstanceIds::M_passive1:
switch (portNum) {
case 0:
// TODO
active1.p_handlerBase(portNum);
break;
default:
FW_ASSERT(0, static_cast<FwAssertArgType>(portNum));
@ -215,7 +223,7 @@ namespace M {
case ::M::InstanceIds::M_passive2:
switch (portNum) {
case 0:
// TODO
M::active2.p_handlerBase(portNum);
break;
default:
FW_ASSERT(0, static_cast<FwAssertArgType>(portNum));

View File

@ -150,7 +150,7 @@ namespace M {
}
#ifdef FW_DIRECT_PORT_CALLS
#if FW_DIRECT_PORT_CALLS
// ----------------------------------------------------------------------
// Topology-dependent component implementation
@ -159,7 +159,11 @@ namespace M {
namespace M {
bool CComponentBase::isConnected_pingOut_OutputPort(FwIndexType portNum) const {
FW_ASSERT((0 <= portNum) && (portNum < NUM_PINGOUT_OUTPUT_PORTS), static_cast<FwAssertArgType>(portNum), static_cast<FwAssertArgType>(NUM_PINGOUT_OUTPUT_PORTS));
FW_ASSERT(
(0 <= portNum) && (portNum < NUM_PINGOUT_OUTPUT_PORTS),
static_cast<FwAssertArgType>(portNum),
static_cast<FwAssertArgType>(NUM_PINGOUT_OUTPUT_PORTS)
);
bool result = false;
const auto instance = this->getInstance();
switch (instance) {
@ -192,13 +196,20 @@ namespace M {
FwIndexType portNum,
U32 key
) const {
FW_ASSERT((0 <= portNum) && (portNum < NUM_PINGOUT_OUTPUT_PORTS), static_cast<FwAssertArgType>(portNum), static_cast<FwAssertArgType>(NUM_PINGOUT_OUTPUT_PORTS));
FW_ASSERT(
(0 <= portNum) && (portNum < NUM_PINGOUT_OUTPUT_PORTS),
static_cast<FwAssertArgType>(portNum),
static_cast<FwAssertArgType>(NUM_PINGOUT_OUTPUT_PORTS)
);
const auto instance = this->getInstance();
switch (instance) {
case ::M::InstanceIds::M_c1:
switch (portNum) {
case 0:
// TODO
M::health.pingIn_handlerBase(
portNum,
key
);
break;
default:
FW_ASSERT(0, static_cast<FwAssertArgType>(portNum));
@ -208,7 +219,10 @@ namespace M {
case ::M::InstanceIds::M_c2:
switch (portNum) {
case 0:
// TODO
M::health.pingIn_handlerBase(
portNum,
key
);
break;
default:
FW_ASSERT(0, static_cast<FwAssertArgType>(portNum));
@ -226,7 +240,11 @@ namespace M {
namespace Svc {
bool HealthComponentBase::isConnected_pingOut_OutputPort(FwIndexType portNum) const {
FW_ASSERT((0 <= portNum) && (portNum < NUM_PINGOUT_OUTPUT_PORTS), static_cast<FwAssertArgType>(portNum), static_cast<FwAssertArgType>(NUM_PINGOUT_OUTPUT_PORTS));
FW_ASSERT(
(0 <= portNum) && (portNum < NUM_PINGOUT_OUTPUT_PORTS),
static_cast<FwAssertArgType>(portNum),
static_cast<FwAssertArgType>(NUM_PINGOUT_OUTPUT_PORTS)
);
bool result = false;
const auto instance = this->getInstance();
switch (instance) {
@ -253,16 +271,26 @@ namespace Svc {
FwIndexType portNum,
U32 key
) const {
FW_ASSERT((0 <= portNum) && (portNum < NUM_PINGOUT_OUTPUT_PORTS), static_cast<FwAssertArgType>(portNum), static_cast<FwAssertArgType>(NUM_PINGOUT_OUTPUT_PORTS));
FW_ASSERT(
(0 <= portNum) && (portNum < NUM_PINGOUT_OUTPUT_PORTS),
static_cast<FwAssertArgType>(portNum),
static_cast<FwAssertArgType>(NUM_PINGOUT_OUTPUT_PORTS)
);
const auto instance = this->getInstance();
switch (instance) {
case ::M::InstanceIds::M_health:
switch (portNum) {
case 0:
// TODO
M::c1.pingIn_handlerBase(
portNum,
key
);
break;
case 1:
// TODO
M::c2.pingIn_handlerBase(
portNum,
key
);
break;
default:
FW_ASSERT(0, static_cast<FwAssertArgType>(portNum));