new parser writes offset assertions

This commit is contained in:
Matthew Kennedy 2023-07-19 14:48:09 -07:00
parent 7fd38084e0
commit bbbf46deea
6 changed files with 39 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import com.rusefi.ReaderState;
import com.rusefi.ReaderStateImpl;
import com.rusefi.RusefiParseErrorStrategy;
import com.rusefi.newparse.ParseState;
import com.rusefi.newparse.outputs.CStructWriter;
import com.rusefi.newparse.outputs.OutputChannelWriter;
import com.rusefi.newparse.parsing.Definition;
import com.rusefi.output.*;
@ -114,7 +115,8 @@ public class LiveDataProcessor {
if (extraPrepend != null)
state.addPrepend(extraPrepend);
state.addPrepend(prepend);
state.addCHeaderDestination(folder + File.separator + name + "_generated.h");
String cHeaderDestination = folder + File.separator + name + "_generated.h";
state.addCHeaderDestination(cHeaderDestination);
int baseOffset = outputsSections.getBaseOffset();
state.addDestination(new FileJavaFieldsConsumer(state, "../java_console/models/src/main/java/com/rusefi/config/generated/" + javaName, baseOffset));
@ -142,11 +144,14 @@ public class LiveDataProcessor {
RusefiParseErrorStrategy.parseDefinitionFile(parseState.getListener(), state.getDefinitionInputFile());
// CStructWriter cStructs = new CStructWriter();
// cStructs.writeCStructs(parseState, cHeaderDestination);
// if (outputNames.length == 0) {
// outputChannelWriter.writeOutputChannels(parseState, null);
// } else {
// for (int i = 0; i < outputNames.length; i++) {
// outputChannelWriter.writeOutputChannels(parseState, fragmentDialogConsumer, outputNames[i]);
// outputChannelWriter.writeOutputChannels(parseState, outputNames[i]);
// }
// }
}

View File

@ -74,6 +74,11 @@ public class ArrayLayout extends Layout {
}
}
@Override
public void writeCOffsetCheck(PrintStream ps, String parentTypeName) {
this.prototypeLayout.writeCOffsetCheck(ps, parentTypeName);
}
@Override
protected void writeOutputChannelLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd) {
this.prototypeLayout.writeOutputChannelLayout(ps, prefixer, offsetAdd, this.length);

View File

@ -65,6 +65,8 @@ public abstract class Layout {
throw new IllegalStateException("This type can't be in an array!");
}
public void writeCOffsetCheck(PrintStream ps, String parentTypeName) { }
public void writeOutputChannelLayout(PrintStream ps, String prefix, int offsetAdd) {
StructNamePrefixer prefixer = new StructNamePrefixer();

View File

@ -148,6 +148,17 @@ public class ScalarLayout extends Layout {
ps.println("\t" + cTypeName + " " + this.name + "[" + al + "];");
}
@Override
public void writeCOffsetCheck(PrintStream ps, String parentTypeName) {
ps.print("static_assert(offsetof(");
ps.print(parentTypeName);
ps.print(", ");
ps.print(this.name);
ps.print(") == ");
ps.print(this.offsetWithinStruct);
ps.println(");");
}
private void writeOutputChannelLayout(PrintStream ps, StructNamePrefixer prefixer, int offsetAdd, String name) {
ps.print(prefixer.get(name));
//ps.print(" = " + fieldType + ", ");

View File

@ -59,4 +59,15 @@ public class StringLayout extends Layout {
this.writeCOffsetHeader(ps, this.comment, null);
ps.println("\tchar " + this.name + "[" + arrayLength[0] + "][" + this.size + "];");
}
@Override
public void writeCOffsetCheck(PrintStream ps, String parentTypeName) {
ps.print("static_assert(offsetof(");
ps.print(parentTypeName);
ps.print(", ");
ps.print(this.name);
ps.print(") == ");
ps.print(this.offsetWithinStruct);
ps.println(");");
}
}

View File

@ -189,6 +189,9 @@ public class StructLayout extends Layout {
ps.println("};");
ps.println("static_assert(sizeof(" + this.typeName + ") == " + getSize() + ");");
// Emit assertions to check that the offset of each child is correct according to the C++ compiler
this.children.forEach(c -> c.writeCOffsetCheck(ps, this.typeName));
ps.println();
}