Skip to content

Commit f9ecb45

Browse files
authored
Merge pull request #716 from nasa/array-tostring-loop
Array toString() with loop
2 parents 7e64ab0 + d6d9c8d commit f9ecb45

29 files changed

+775
-437
lines changed

.github/workflows/build-native.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ jobs:
1818
output-directory: ./compiler/bin
1919
meta-package: fpp
2020
fast-hack: true
21+
trace: false
2122
secrets: inherit

.github/workflows/native-build.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ jobs:
7979
export PATH="${NATIVE_IMAGE_TOOLS_PATH}:${PATH}"
8080
${{ inputs.test }}
8181
echo -e "Trace output files:\n$( find ${{ inputs.trace-directory }} -name *.json )"
82-
- name: "Archiving Tracing"
82+
- if: ${{ inputs.trace }}
83+
name: "Archiving Tracing"
8384
uses: actions/upload-artifact@v4
8485
with:
8586
name: jar-traces

compiler/lib/src/main/scala/codegen/CppWriter/ArrayCppWriter.scala

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -406,27 +406,35 @@ case class ArrayCppWriter (
406406
)
407407

408408
private def getPublicFunctionMembers: List[CppDoc.Class.Member] = {
409-
// Write string initialization for serializable element types in toString()
410-
val initStrings =
411-
if hasPrimitiveEltType || hasStringEltType then Nil
412-
else List(
413-
lines("// Declare strings to hold any serializable toString() arguments"),
414-
List.range(0, arraySize).map(i => line(s"Fw::String str$i;")),
415-
Line.blank ::
416-
lines("// Call toString for arrays and serializable types"),
417-
List.range(0, arraySize).map(i => line(s"this->elements[$i].toString(str$i);")),
418-
List(Line.blank),
419-
).flatten
420-
// Write format arguments in toString()
421-
def getFormatArg(i: Int) =
422-
if hasPrimitiveEltType then s"this->elements[$i]"
423-
else if hasStringEltType then s"this->elements[$i].toChar()"
424-
else s"str$i.toChar()"
425-
val formatArgs = lines(
426-
List.range(0, arraySize).map(
427-
promoteF32ToF64(eltType) compose getFormatArg
428-
).mkString(",\n")
429-
)
409+
val fillTmpString =
410+
// Standard format string, just copy it in
411+
if hasStringEltType && formatStr == "%s" then s"tmp = this->elements[index];"
412+
// Non-standard format string, we need to copy the string in with .format()
413+
else if hasStringEltType then s"tmp.format(\"$formatStr\", this->elements[index].toChar());"
414+
// Primitive string format
415+
else if hasPrimitiveEltType then s"tmp.format(\"$formatStr\", ${promoteF32ToF64(eltType)("this->elements[index]")});"
416+
// Complex object type with default format string, convert the object to a string
417+
else if formatStr == "%s" then "this->elements[index].toString(tmp);"
418+
// Complex object type with non-default format string, convert the object to a string
419+
// Then re-format the string using the custom format string
420+
else s"""this->elements[index].toString(tmp);
421+
|tmp.format(\"%s\", tmp.toChar());"""
422+
423+
val formatLoop = indexIterator(lines(
424+
s"""|Fw::String tmp;
425+
|$fillTmpString
426+
|
427+
|FwSizeType size = tmp.length() + (index > 0 ? 2 : 0);
428+
|if ((size + sb.length()) <= sb.maxLength()) {
429+
| if (index > 0) {
430+
| sb += ", ";
431+
| }
432+
| sb += tmp;
433+
|} else {
434+
| break;
435+
|}
436+
|"""
437+
))
430438

431439
List(
432440
linesClassMember(
@@ -494,20 +502,24 @@ case class ArrayCppWriter (
494502
),
495503
CppDoc.Type("void"),
496504
List.concat(
497-
wrapInScope(
498-
"static const char *formatString = \"[ \"",
499-
lines(List.fill(arraySize)(s"\"$formatStr ").mkString("\"\n") + "]\";"),
500-
""
501-
),
502-
initStrings,
503-
wrapInScope(
504-
"sb.format(",
505-
{
506-
line("formatString,") ::
507-
formatArgs
508-
},
509-
");"
510-
)
505+
lines(
506+
s"""|// Clear the output string
507+
|sb = "";
508+
|
509+
|// Array prefix
510+
|if (sb.length() + 2 <= sb.maxLength()) {
511+
| sb += \"[ \";
512+
|} else {
513+
| return;
514+
|}"""),
515+
List(Line.blank),
516+
formatLoop,
517+
List(Line.blank),
518+
lines(
519+
s"""|// Array suffix
520+
|if (sb.length() + 2 <= sb.maxLength()) {
521+
| sb += \" ]\";
522+
|}"""),
511523
),
512524
CppDoc.Function.NonSV,
513525
CppDoc.Function.Const,

compiler/lib/src/main/scala/codegen/CppWriter/CppWriterUtils.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ trait CppWriterUtils extends LineUtils {
323323

324324
/** Explicitly promotes an F32 value to an F64 value */
325325
def promoteF32ToF64 (t: Type) (v: String) =
326-
if t == Type.F32
326+
if t.getUnderlyingType == Type.F32
327327
then s"static_cast<F64>($v)"
328328
else v
329329

compiler/tools/fpp-to-cpp/test/array/AArrayAc.ref.cpp

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,35 @@ Fw::SerializeStatus A ::
175175
void A ::
176176
toString(Fw::StringBase& sb) const
177177
{
178-
static const char *formatString = "[ "
179-
"%" PRIu32 " "
180-
"%" PRIu32 " "
181-
"%" PRIu32 " ]";
182-
183-
sb.format(
184-
formatString,
185-
this->elements[0],
186-
this->elements[1],
187-
this->elements[2]
188-
);
178+
// Clear the output string
179+
sb = "";
180+
181+
// Array prefix
182+
if (sb.length() + 2 <= sb.maxLength()) {
183+
sb += "[ ";
184+
} else {
185+
return;
186+
}
187+
188+
for (U32 index = 0; index < SIZE; index++) {
189+
Fw::String tmp;
190+
tmp.format("%" PRIu32 "", this->elements[index]);
191+
192+
FwSizeType size = tmp.length() + (index > 0 ? 2 : 0);
193+
if ((size + sb.length()) <= sb.maxLength()) {
194+
if (index > 0) {
195+
sb += ", ";
196+
}
197+
sb += tmp;
198+
} else {
199+
break;
200+
}
201+
}
202+
203+
// Array suffix
204+
if (sb.length() + 2 <= sb.maxLength()) {
205+
sb += " ]";
206+
}
189207
}
190208

191209
#endif

compiler/tools/fpp-to-cpp/test/array/AbsTypeArrayAc.ref.cpp

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -175,27 +175,35 @@ Fw::SerializeStatus AbsType ::
175175
void AbsType ::
176176
toString(Fw::StringBase& sb) const
177177
{
178-
static const char *formatString = "[ "
179-
"%s "
180-
"%s "
181-
"%s ]";
182-
183-
// Declare strings to hold any serializable toString() arguments
184-
Fw::String str0;
185-
Fw::String str1;
186-
Fw::String str2;
187-
188-
// Call toString for arrays and serializable types
189-
this->elements[0].toString(str0);
190-
this->elements[1].toString(str1);
191-
this->elements[2].toString(str2);
192-
193-
sb.format(
194-
formatString,
195-
str0.toChar(),
196-
str1.toChar(),
197-
str2.toChar()
198-
);
178+
// Clear the output string
179+
sb = "";
180+
181+
// Array prefix
182+
if (sb.length() + 2 <= sb.maxLength()) {
183+
sb += "[ ";
184+
} else {
185+
return;
186+
}
187+
188+
for (U32 index = 0; index < SIZE; index++) {
189+
Fw::String tmp;
190+
this->elements[index].toString(tmp);
191+
192+
FwSizeType size = tmp.length() + (index > 0 ? 2 : 0);
193+
if ((size + sb.length()) <= sb.maxLength()) {
194+
if (index > 0) {
195+
sb += ", ";
196+
}
197+
sb += tmp;
198+
} else {
199+
break;
200+
}
201+
}
202+
203+
// Array suffix
204+
if (sb.length() + 2 <= sb.maxLength()) {
205+
sb += " ]";
206+
}
199207
}
200208

201209
#endif

compiler/tools/fpp-to-cpp/test/array/AliasTypeArrayAc.ref.cpp

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,35 @@ Fw::SerializeStatus AliasType ::
175175
void AliasType ::
176176
toString(Fw::StringBase& sb) const
177177
{
178-
static const char *formatString = "[ "
179-
"%" PRIu32 " "
180-
"%" PRIu32 " "
181-
"%" PRIu32 " ]";
182-
183-
sb.format(
184-
formatString,
185-
this->elements[0],
186-
this->elements[1],
187-
this->elements[2]
188-
);
178+
// Clear the output string
179+
sb = "";
180+
181+
// Array prefix
182+
if (sb.length() + 2 <= sb.maxLength()) {
183+
sb += "[ ";
184+
} else {
185+
return;
186+
}
187+
188+
for (U32 index = 0; index < SIZE; index++) {
189+
Fw::String tmp;
190+
tmp.format("%" PRIu32 "", this->elements[index]);
191+
192+
FwSizeType size = tmp.length() + (index > 0 ? 2 : 0);
193+
if ((size + sb.length()) <= sb.maxLength()) {
194+
if (index > 0) {
195+
sb += ", ";
196+
}
197+
sb += tmp;
198+
} else {
199+
break;
200+
}
201+
}
202+
203+
// Array suffix
204+
if (sb.length() + 2 <= sb.maxLength()) {
205+
sb += " ]";
206+
}
189207
}
190208

191209
#endif

compiler/tools/fpp-to-cpp/test/array/C_AArrayAc.ref.cpp

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,35 @@ Fw::SerializeStatus C_A ::
175175
void C_A ::
176176
toString(Fw::StringBase& sb) const
177177
{
178-
static const char *formatString = "[ "
179-
"%" PRIu32 " "
180-
"%" PRIu32 " "
181-
"%" PRIu32 " ]";
182-
183-
sb.format(
184-
formatString,
185-
this->elements[0],
186-
this->elements[1],
187-
this->elements[2]
188-
);
178+
// Clear the output string
179+
sb = "";
180+
181+
// Array prefix
182+
if (sb.length() + 2 <= sb.maxLength()) {
183+
sb += "[ ";
184+
} else {
185+
return;
186+
}
187+
188+
for (U32 index = 0; index < SIZE; index++) {
189+
Fw::String tmp;
190+
tmp.format("%" PRIu32 "", this->elements[index]);
191+
192+
FwSizeType size = tmp.length() + (index > 0 ? 2 : 0);
193+
if ((size + sb.length()) <= sb.maxLength()) {
194+
if (index > 0) {
195+
sb += ", ";
196+
}
197+
sb += tmp;
198+
} else {
199+
break;
200+
}
201+
}
202+
203+
// Array suffix
204+
if (sb.length() + 2 <= sb.maxLength()) {
205+
sb += " ]";
206+
}
189207
}
190208

191209
#endif

compiler/tools/fpp-to-cpp/test/array/Enum1ArrayAc.ref.cpp

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -172,23 +172,35 @@ Fw::SerializeStatus Enum1 ::
172172
void Enum1 ::
173173
toString(Fw::StringBase& sb) const
174174
{
175-
static const char *formatString = "[ "
176-
"%s "
177-
"%s ]";
178-
179-
// Declare strings to hold any serializable toString() arguments
180-
Fw::String str0;
181-
Fw::String str1;
182-
183-
// Call toString for arrays and serializable types
184-
this->elements[0].toString(str0);
185-
this->elements[1].toString(str1);
186-
187-
sb.format(
188-
formatString,
189-
str0.toChar(),
190-
str1.toChar()
191-
);
175+
// Clear the output string
176+
sb = "";
177+
178+
// Array prefix
179+
if (sb.length() + 2 <= sb.maxLength()) {
180+
sb += "[ ";
181+
} else {
182+
return;
183+
}
184+
185+
for (U32 index = 0; index < SIZE; index++) {
186+
Fw::String tmp;
187+
this->elements[index].toString(tmp);
188+
189+
FwSizeType size = tmp.length() + (index > 0 ? 2 : 0);
190+
if ((size + sb.length()) <= sb.maxLength()) {
191+
if (index > 0) {
192+
sb += ", ";
193+
}
194+
sb += tmp;
195+
} else {
196+
break;
197+
}
198+
}
199+
200+
// Array suffix
201+
if (sb.length() + 2 <= sb.maxLength()) {
202+
sb += " ]";
203+
}
192204
}
193205

194206
#endif

0 commit comments

Comments
 (0)