From 947e364d75f89726ec8cfce373f92a2a547eea62 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Thu, 24 Jun 2021 02:13:06 -0700 Subject: [PATCH] new parsing: fix names of arrays of structs (#2849) * fix offsets inside struct arrays * style * fix index writing for arrays of structs --- .../layout/ArrayIterateScalarLayout.java | 12 +++---- .../layout/ArrayIterateStructLayout.java | 12 +++---- .../rusefi/newparse/layout/ArrayLayout.java | 4 +-- .../newparse/layout/BitGroupLayout.java | 6 ++-- .../rusefi/newparse/layout/EnumLayout.java | 4 +-- .../com/rusefi/newparse/layout/Layout.java | 6 ++-- .../rusefi/newparse/layout/ScalarLayout.java | 14 ++++---- .../rusefi/newparse/layout/StringLayout.java | 4 +-- .../rusefi/newparse/layout/StructLayout.java | 4 +-- .../newparse/layout/StructNamePrefixer.java | 32 +++++++++++++++---- .../rusefi/newparse/layout/UnionLayout.java | 4 +-- .../rusefi/newparse/layout/UnusedLayout.java | 4 +-- 12 files changed, 63 insertions(+), 43 deletions(-) diff --git a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/ArrayIterateScalarLayout.java b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/ArrayIterateScalarLayout.java index 211629d85d..4760115a50 100644 --- a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/ArrayIterateScalarLayout.java +++ b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/ArrayIterateScalarLayout.java @@ -14,17 +14,17 @@ public class ArrayIterateScalarLayout extends ArrayLayout { this.prototypeLayout.setOffset(offset + this.prototypeLayout.getSize() * idx); // Put a 1-based index on the end of the name to distinguish in TS - prefixer.setSuffix(Integer.toString(idx + 1)); - this.prototypeLayout.writeTunerstudioLayout(ps, prefixer); - prefixer.resetSuffix(); + prefixer.setIndex(idx); + this.prototypeLayout.writeTunerstudioLayout(ps, prefixer, 0); + prefixer.clearIndex(); } @Override - public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) { + protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) { // Time to iterate: emit one scalar per array element, with the name modified accordingly - for (int i = 0; i < length; i++) { - emitOne(ps, prefixer, this.offset, i); + for (int i = 0; i < this.length; i++) { + emitOne(ps, prefixer, this.offset + offsetAdd, i); } } diff --git a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/ArrayIterateStructLayout.java b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/ArrayIterateStructLayout.java index 382de558d0..a9743ae6ab 100644 --- a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/ArrayIterateStructLayout.java +++ b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/ArrayIterateStructLayout.java @@ -11,20 +11,20 @@ public class ArrayIterateStructLayout extends ArrayLayout { private void emitOne(PrintStream ps, StructNamePrefixer prefixer, int offset, int idx) { // Set element's position within the array - this.prototypeLayout.setOffset(offset + this.prototypeLayout.getSize() * idx); + int offsetAdd = offset + this.prototypeLayout.getSize() * idx; // Put a 1-based index on the end of the name to distinguish in TS - prefixer.setSuffix(Integer.toString(idx + 1)); - this.prototypeLayout.writeTunerstudioLayout(ps, prefixer); - prefixer.resetSuffix(); + prefixer.setIndex(idx); + this.prototypeLayout.writeTunerstudioLayout(ps, prefixer, offsetAdd); + prefixer.clearIndex(); } @Override - public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) { + protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) { // Time to iterate: emit one scalar per array element, with the name modified accordingly for (int i = 0; i < this.length; i++) { - emitOne(ps, prefixer, this.offset, i); + emitOne(ps, prefixer, this.offset + offsetAdd, i); } } diff --git a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/ArrayLayout.java b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/ArrayLayout.java index 9bfa810c44..b069b74d57 100644 --- a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/ArrayLayout.java +++ b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/ArrayLayout.java @@ -55,8 +55,8 @@ public class ArrayLayout extends Layout { } @Override - public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) { - this.prototypeLayout.writeTunerstudioLayout(ps, prefixer, this.length); + protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) { + this.prototypeLayout.writeTunerstudioLayout(ps, prefixer, offsetAdd, this.length); } @Override diff --git a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/BitGroupLayout.java b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/BitGroupLayout.java index 71b596488a..3f1421103a 100644 --- a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/BitGroupLayout.java +++ b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/BitGroupLayout.java @@ -46,12 +46,14 @@ public class BitGroupLayout extends Layout { } @Override - public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) { + protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) { + int actualOffset = this.offset + offsetAdd; + for (int i = 0; i < bits.size(); i++) { BitLayout bit = bits.get(i); ps.print(prefixer.get(bit.name)); ps.print(" = bits, U32, "); - ps.print(this.offset); + ps.print(actualOffset); ps.print(", ["); ps.print(i + ":" + i); diff --git a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/EnumLayout.java b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/EnumLayout.java index 4543aabf46..4dfef117cd 100644 --- a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/EnumLayout.java +++ b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/EnumLayout.java @@ -35,12 +35,12 @@ public class EnumLayout extends Layout { } @Override - public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) { + protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) { ps.print(prefixer.get(this.name)); ps.print(" = bits, "); ps.print(this.type.tsType); ps.print(", "); - ps.print(this.offset); + ps.print(this.offset + offsetAdd); ps.print(", "); ps.print("[0:"); diff --git a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/Layout.java b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/Layout.java index 48980ace38..cdbb7b70cd 100644 --- a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/Layout.java +++ b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/Layout.java @@ -26,12 +26,12 @@ public abstract class Layout { } public final void writeTunerstudioLayout(PrintStream ps) { - writeTunerstudioLayout(ps, new StructNamePrefixer()); + writeTunerstudioLayout(ps, new StructNamePrefixer(), 0); } - public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) {} + protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) {} - public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int arrayLength) { + protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd, int arrayLength) { throw new IllegalStateException("This type can't be in an array!"); } diff --git a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/ScalarLayout.java b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/ScalarLayout.java index 97605050ab..8cc7db219a 100644 --- a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/ScalarLayout.java +++ b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/ScalarLayout.java @@ -28,12 +28,12 @@ public class ScalarLayout extends Layout { return "Scalar " + type.cType + " " + super.toString(); } - private void printBeforeArrayLength(PrintStream ps, StructNamePrefixer prefixer, String fieldType) { + private void printBeforeArrayLength(PrintStream ps, StructNamePrefixer prefixer, String fieldType, int offsetAdd) { ps.print(prefixer.get(this.name)); ps.print(" = " + fieldType + ", "); ps.print(this.type.tsType); ps.print(", "); - ps.print(this.offset); + ps.print(this.offset + offsetAdd); ps.print(", "); } @@ -44,17 +44,17 @@ public class ScalarLayout extends Layout { } @Override - public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int arrayLength) { + protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd, int arrayLength) { if (arrayLength == 0) { // Skip zero length arrays, they may be used for dynamic padding but TS doesn't like them return; } else if (arrayLength == 1) { // For 1-length arrays, emit as a plain scalar instead - writeTunerstudioLayout(ps, prefixer); + writeTunerstudioLayout(ps, prefixer, offsetAdd); return; } - printBeforeArrayLength(ps, prefixer, "array"); + printBeforeArrayLength(ps, prefixer, "array", offsetAdd); ps.print("["); ps.print(arrayLength); @@ -64,8 +64,8 @@ public class ScalarLayout extends Layout { } @Override - public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) { - printBeforeArrayLength(ps, prefixer, "scalar"); + protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) { + printBeforeArrayLength(ps, prefixer, "scalar", offsetAdd); printAfterArrayLength(ps); } diff --git a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/StringLayout.java b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/StringLayout.java index 2d9f705e25..462e1ee585 100644 --- a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/StringLayout.java +++ b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/StringLayout.java @@ -31,10 +31,10 @@ public class StringLayout extends Layout { } @Override - public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) { + protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) { ps.print(prefixer.get(this.name)); ps.print(" = string, ASCII, "); - ps.print(this.offset); + ps.print(this.offset + offsetAdd); ps.print(", "); ps.print(size); diff --git a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/StructLayout.java b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/StructLayout.java index 8062576a98..f7612747ed 100644 --- a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/StructLayout.java +++ b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/StructLayout.java @@ -156,13 +156,13 @@ public class StructLayout extends Layout { @Override - public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) { + protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) { if (!this.noPrefix) { prefixer.push(this.name); } // print all children in sequence - this.children.forEach(c -> c.writeTunerstudioLayout(ps, prefixer)); + this.children.forEach(c -> c.writeTunerstudioLayout(ps, prefixer, offsetAdd)); if (!this.noPrefix) { prefixer.pop(); diff --git a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/StructNamePrefixer.java b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/StructNamePrefixer.java index c6d9bb2430..ff66293406 100644 --- a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/StructNamePrefixer.java +++ b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/StructNamePrefixer.java @@ -5,26 +5,44 @@ import java.util.stream.Collectors; public class StructNamePrefixer { private Stack stack = new Stack<>(); + private int idx = -1; public void pop() { stack.pop(); } public void push(String name) { + if (this.idx != -1) { + // If we push with an index set, it means we're inside an array of structs. + // In that case, we embed the index (of the struct in the array) within the array's name + name = name + this.idx; + this.idx = -1; + } + stack.push(name + "_"); } - private String suffix = new String(); - - public void setSuffix(String suffix) { - this.suffix = suffix; + void setIndex(int idx) { + if (idx >= 0) { + this.idx = idx + 1; + } else { + throw new RuntimeException("Invalid StructNamePrefixer index: " + idx); + } } - public void resetSuffix() { - this.suffix = new String(); + void clearIndex() { + this.idx = -1; } String get(String name) { - return stack.stream().collect(Collectors.joining()) + name + suffix; + // stack has no prefixes, just return the plain name (no join necessary) + name = stack.stream().collect(Collectors.joining()) + name; + + // Append the array index if necessary (for iterated arrays) + if (this.idx != -1) { + return name + idx; + } else { + return name; + } } } diff --git a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/UnionLayout.java b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/UnionLayout.java index 8af76cbdd1..1f0adf54a4 100644 --- a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/UnionLayout.java +++ b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/UnionLayout.java @@ -56,9 +56,9 @@ public class UnionLayout extends Layout { } @Override - public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) { + protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) { // Simply write out all children - no container necessary as fields can overlap in TS - this.children.forEach(c -> c.writeTunerstudioLayout(ps, prefixer)); + this.children.forEach(c -> c.writeTunerstudioLayout(ps, prefixer, offsetAdd)); } @Override diff --git a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/UnusedLayout.java b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/UnusedLayout.java index 90af37a9fd..8b3b3d44b7 100644 --- a/java_tools/configuration_definition/src/com/rusefi/newparse/layout/UnusedLayout.java +++ b/java_tools/configuration_definition/src/com/rusefi/newparse/layout/UnusedLayout.java @@ -34,8 +34,8 @@ public class UnusedLayout extends Layout { } @Override - public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) { - ps.println("; unused " + this.size + " bytes at offset " + this.offset); + protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) { + ps.println("; unused " + this.size + " bytes at offset " + (this.offset + offsetAdd)); } @Override