new parsing: fix names of arrays of structs (#2849)
* fix offsets inside struct arrays * style * fix index writing for arrays of structs
This commit is contained in:
parent
8ef5e0d1c9
commit
947e364d75
|
@ -14,17 +14,17 @@ public class ArrayIterateScalarLayout extends ArrayLayout {
|
||||||
this.prototypeLayout.setOffset(offset + this.prototypeLayout.getSize() * idx);
|
this.prototypeLayout.setOffset(offset + this.prototypeLayout.getSize() * idx);
|
||||||
|
|
||||||
// Put a 1-based index on the end of the name to distinguish in TS
|
// Put a 1-based index on the end of the name to distinguish in TS
|
||||||
prefixer.setSuffix(Integer.toString(idx + 1));
|
prefixer.setIndex(idx);
|
||||||
this.prototypeLayout.writeTunerstudioLayout(ps, prefixer);
|
this.prototypeLayout.writeTunerstudioLayout(ps, prefixer, 0);
|
||||||
prefixer.resetSuffix();
|
prefixer.clearIndex();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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
|
// Time to iterate: emit one scalar per array element, with the name modified accordingly
|
||||||
|
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < this.length; i++) {
|
||||||
emitOne(ps, prefixer, this.offset, i);
|
emitOne(ps, prefixer, this.offset + offsetAdd, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,20 +11,20 @@ public class ArrayIterateStructLayout extends ArrayLayout {
|
||||||
|
|
||||||
private void emitOne(PrintStream ps, StructNamePrefixer prefixer, int offset, int idx) {
|
private void emitOne(PrintStream ps, StructNamePrefixer prefixer, int offset, int idx) {
|
||||||
// Set element's position within the array
|
// 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
|
// Put a 1-based index on the end of the name to distinguish in TS
|
||||||
prefixer.setSuffix(Integer.toString(idx + 1));
|
prefixer.setIndex(idx);
|
||||||
this.prototypeLayout.writeTunerstudioLayout(ps, prefixer);
|
this.prototypeLayout.writeTunerstudioLayout(ps, prefixer, offsetAdd);
|
||||||
prefixer.resetSuffix();
|
prefixer.clearIndex();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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
|
// Time to iterate: emit one scalar per array element, with the name modified accordingly
|
||||||
|
|
||||||
for (int i = 0; i < this.length; i++) {
|
for (int i = 0; i < this.length; i++) {
|
||||||
emitOne(ps, prefixer, this.offset, i);
|
emitOne(ps, prefixer, this.offset + offsetAdd, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -55,8 +55,8 @@ public class ArrayLayout extends Layout {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) {
|
protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) {
|
||||||
this.prototypeLayout.writeTunerstudioLayout(ps, prefixer, this.length);
|
this.prototypeLayout.writeTunerstudioLayout(ps, prefixer, offsetAdd, this.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -46,12 +46,14 @@ public class BitGroupLayout extends Layout {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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++) {
|
for (int i = 0; i < bits.size(); i++) {
|
||||||
BitLayout bit = bits.get(i);
|
BitLayout bit = bits.get(i);
|
||||||
ps.print(prefixer.get(bit.name));
|
ps.print(prefixer.get(bit.name));
|
||||||
ps.print(" = bits, U32, ");
|
ps.print(" = bits, U32, ");
|
||||||
ps.print(this.offset);
|
ps.print(actualOffset);
|
||||||
ps.print(", [");
|
ps.print(", [");
|
||||||
ps.print(i + ":" + i);
|
ps.print(i + ":" + i);
|
||||||
|
|
||||||
|
|
|
@ -35,12 +35,12 @@ public class EnumLayout extends Layout {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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(prefixer.get(this.name));
|
||||||
ps.print(" = bits, ");
|
ps.print(" = bits, ");
|
||||||
ps.print(this.type.tsType);
|
ps.print(this.type.tsType);
|
||||||
ps.print(", ");
|
ps.print(", ");
|
||||||
ps.print(this.offset);
|
ps.print(this.offset + offsetAdd);
|
||||||
ps.print(", ");
|
ps.print(", ");
|
||||||
|
|
||||||
ps.print("[0:");
|
ps.print("[0:");
|
||||||
|
|
|
@ -26,12 +26,12 @@ public abstract class Layout {
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void writeTunerstudioLayout(PrintStream ps) {
|
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!");
|
throw new IllegalStateException("This type can't be in an array!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,12 +28,12 @@ public class ScalarLayout extends Layout {
|
||||||
return "Scalar " + type.cType + " " + super.toString();
|
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(prefixer.get(this.name));
|
||||||
ps.print(" = " + fieldType + ", ");
|
ps.print(" = " + fieldType + ", ");
|
||||||
ps.print(this.type.tsType);
|
ps.print(this.type.tsType);
|
||||||
ps.print(", ");
|
ps.print(", ");
|
||||||
ps.print(this.offset);
|
ps.print(this.offset + offsetAdd);
|
||||||
ps.print(", ");
|
ps.print(", ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,17 +44,17 @@ public class ScalarLayout extends Layout {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int arrayLength) {
|
protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd, int arrayLength) {
|
||||||
if (arrayLength == 0) {
|
if (arrayLength == 0) {
|
||||||
// Skip zero length arrays, they may be used for dynamic padding but TS doesn't like them
|
// Skip zero length arrays, they may be used for dynamic padding but TS doesn't like them
|
||||||
return;
|
return;
|
||||||
} else if (arrayLength == 1) {
|
} else if (arrayLength == 1) {
|
||||||
// For 1-length arrays, emit as a plain scalar instead
|
// For 1-length arrays, emit as a plain scalar instead
|
||||||
writeTunerstudioLayout(ps, prefixer);
|
writeTunerstudioLayout(ps, prefixer, offsetAdd);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
printBeforeArrayLength(ps, prefixer, "array");
|
printBeforeArrayLength(ps, prefixer, "array", offsetAdd);
|
||||||
|
|
||||||
ps.print("[");
|
ps.print("[");
|
||||||
ps.print(arrayLength);
|
ps.print(arrayLength);
|
||||||
|
@ -64,8 +64,8 @@ public class ScalarLayout extends Layout {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) {
|
protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) {
|
||||||
printBeforeArrayLength(ps, prefixer, "scalar");
|
printBeforeArrayLength(ps, prefixer, "scalar", offsetAdd);
|
||||||
printAfterArrayLength(ps);
|
printAfterArrayLength(ps);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,10 +31,10 @@ public class StringLayout extends Layout {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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(prefixer.get(this.name));
|
||||||
ps.print(" = string, ASCII, ");
|
ps.print(" = string, ASCII, ");
|
||||||
ps.print(this.offset);
|
ps.print(this.offset + offsetAdd);
|
||||||
ps.print(", ");
|
ps.print(", ");
|
||||||
ps.print(size);
|
ps.print(size);
|
||||||
|
|
||||||
|
|
|
@ -156,13 +156,13 @@ public class StructLayout extends Layout {
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) {
|
protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) {
|
||||||
if (!this.noPrefix) {
|
if (!this.noPrefix) {
|
||||||
prefixer.push(this.name);
|
prefixer.push(this.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
// print all children in sequence
|
// 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) {
|
if (!this.noPrefix) {
|
||||||
prefixer.pop();
|
prefixer.pop();
|
||||||
|
|
|
@ -5,26 +5,44 @@ import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class StructNamePrefixer {
|
public class StructNamePrefixer {
|
||||||
private Stack<String> stack = new Stack<>();
|
private Stack<String> stack = new Stack<>();
|
||||||
|
private int idx = -1;
|
||||||
|
|
||||||
public void pop() {
|
public void pop() {
|
||||||
stack.pop();
|
stack.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void push(String name) {
|
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 + "_");
|
stack.push(name + "_");
|
||||||
}
|
}
|
||||||
|
|
||||||
private String suffix = new String();
|
void setIndex(int idx) {
|
||||||
|
if (idx >= 0) {
|
||||||
public void setSuffix(String suffix) {
|
this.idx = idx + 1;
|
||||||
this.suffix = suffix;
|
} else {
|
||||||
|
throw new RuntimeException("Invalid StructNamePrefixer index: " + idx);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resetSuffix() {
|
void clearIndex() {
|
||||||
this.suffix = new String();
|
this.idx = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
String get(String name) {
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,9 +56,9 @@ public class UnionLayout extends Layout {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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
|
// 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
|
@Override
|
||||||
|
|
|
@ -34,8 +34,8 @@ public class UnusedLayout extends Layout {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer) {
|
protected void writeTunerstudioLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) {
|
||||||
ps.println("; unused " + this.size + " bytes at offset " + this.offset);
|
ps.println("; unused " + this.size + " bytes at offset " + (this.offset + offsetAdd));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue