reduce flash footprint by smarter code generation #4163

This commit is contained in:
rusefillc 2022-05-15 17:56:39 -04:00
parent aa87eb681c
commit e13a34a956
3 changed files with 101 additions and 30 deletions

Binary file not shown.

View File

@ -11,22 +11,45 @@ import java.io.IOException;
import static com.rusefi.output.ConfigStructure.ALIGNMENT_FILL_AT; import static com.rusefi.output.ConfigStructure.ALIGNMENT_FILL_AT;
import static com.rusefi.output.DataLogConsumer.UNUSED; import static com.rusefi.output.DataLogConsumer.UNUSED;
import static com.rusefi.output.JavaSensorsConsumer.quote;
@SuppressWarnings("StringConcatenationInsideStringBufferAppend") @SuppressWarnings("StringConcatenationInsideStringBufferAppend")
public class GetConfigValueConsumer implements ConfigurationConsumer { public class GetConfigValueConsumer implements ConfigurationConsumer {
private static final String CONFIG_ENGINE_CONFIGURATION = "config->engineConfiguration."; private static final String CONFIG_ENGINE_CONFIGURATION = "config->engineConfiguration.";
private static final String ENGINE_CONFIGURATION = "engineConfiguration."; private static final String ENGINE_CONFIGURATION = "engineConfiguration.";
static final String FILE_HEADER = "#include \"pch.h\"\n"; static final String FILE_HEADER = "#include \"pch.h\"\n" +
private static final String GET_METHOD_HEADER = "float getConfigValueByName(const char *name) {\n"; "#include \"value_lookup.h\"\n" +
"plain_get_float_s * findFloat(const char *name) {\n" +
"\tplain_get_float_s *currentF = &getF_plain[0];\n" +
"\twhile (currentF < getF_plain + sizeof(getF_plain)/sizeof(getF_plain[0])) {\n" +
"\t\tif (strEqualCaseInsensitive(name, currentF->token)) {\n" +
"\t\t\treturn currentF;\n" +
"\t\t}\n" +
"\t\tcurrentF++;\n" +
"\t}\n" +
"\treturn nullptr;\n" +
"}\n";
private static final String GET_METHOD_HEADER =
"float getConfigValueByName(const char *name) {\n" +
"\t{\n" +
"\t\tplain_get_float_s * known = findFloat(name);\n" +
"\t\tif (known != nullptr) {\n" +
"\t\t\treturn *(float*)hackEngineConfigurationPointer(known->value);\n" +
"\t\t}\n" +
"\t}\n"
;
static final String GET_METHOD_FOOTER = "\treturn EFI_ERROR_CODE;\n" + "}\n"; static final String GET_METHOD_FOOTER = "\treturn EFI_ERROR_CODE;\n" + "}\n";
private static final String SET_METHOD_HEADER = "void setConfigValueByName(const char *name, float value) {\n"; private static final String SET_METHOD_HEADER = "void setConfigValueByName(const char *name, float value) {\n";
private static final String SET_METHOD_FOOTER = "}\n"; private static final String SET_METHOD_FOOTER = "}\n";
private final StringBuilder getterBody = new StringBuilder(); private final StringBuilder getterBody = new StringBuilder();
private final StringBuilder setterBody = new StringBuilder(); private final StringBuilder setterBody = new StringBuilder();
private final StringBuilder allFloatAddresses = new StringBuilder(
"static plain_get_float_s getF_plain[] = {\n");
private final String outputFileName; private final String outputFileName;
public GetConfigValueConsumer(String outputFileName) { public GetConfigValueConsumer(String outputFileName) {
System.out.println("Hello " + getClass() + " " + outputFileName);
this.outputFileName = outputFileName; this.outputFileName = outputFileName;
} }
@ -70,14 +93,16 @@ public class GetConfigValueConsumer implements ConfigurationConsumer {
if (javaName.startsWith(CONFIG_ENGINE_CONFIGURATION)) if (javaName.startsWith(CONFIG_ENGINE_CONFIGURATION))
javaName = "engineConfiguration->" + javaName.substring(CONFIG_ENGINE_CONFIGURATION.length()); javaName = "engineConfiguration->" + javaName.substring(CONFIG_ENGINE_CONFIGURATION.length());
getterBody.append(getCompareName(userName));
getterBody.append("\t\treturn " + javaName + cf.getName() + ";\n");
if (TypesHelper.isFloat(cf.getType())) { if (TypesHelper.isFloat(cf.getType())) {
allFloatAddresses.append("\t{" + quote(userName) + ", &engineConfiguration->" + userName + "},\n");
setterBody.append(getCompareName(userName)); setterBody.append(getCompareName(userName));
String str = getAssignment(cf, javaName, ""); String str = getAssignment(cf, javaName, "");
setterBody.append(str); setterBody.append(str);
} else { } else {
getterBody.append(getCompareName(userName));
getterBody.append("\t\treturn " + javaName + cf.getName() + ";\n");
setterBody.append(getCompareName(userName)); setterBody.append(getCompareName(userName));
String str = getAssignment(cf, javaName, "(int)"); String str = getAssignment(cf, javaName, "(int)");
setterBody.append(str); setterBody.append(str);
@ -99,9 +124,20 @@ public class GetConfigValueConsumer implements ConfigurationConsumer {
return "\tif (strEqualCaseInsensitive(name, \"" + userName + "\"))\n"; return "\tif (strEqualCaseInsensitive(name, \"" + userName + "\"))\n";
} }
public String getGetterForUnitTest() { public String getHeaderAndGetter() {
return FILE_HEADER + return FILE_HEADER +
GET_METHOD_HEADER + getterBody + GET_METHOD_FOOTER; getFloatsSections() +
getComleteGetterBody();
}
@NotNull
public String getComleteGetterBody() {
return GET_METHOD_HEADER + getterBody + GET_METHOD_FOOTER;
}
@NotNull
public String getFloatsSections() {
return allFloatAddresses + "};\n\n";
} }
public String getSetterBody() { public String getSetterBody() {
@ -109,8 +145,7 @@ public class GetConfigValueConsumer implements ConfigurationConsumer {
} }
public String getContent() { public String getContent() {
return FILE_HEADER + return getHeaderAndGetter()
GET_METHOD_HEADER + getterBody + GET_METHOD_FOOTER
+ +
SET_METHOD_HEADER + setterBody + SET_METHOD_FOOTER SET_METHOD_HEADER + setterBody + SET_METHOD_FOOTER
; ;

View File

@ -21,12 +21,24 @@ public class GetConfigValueConsumerTest {
"\tdc_io[2 iterate] etbIn\n" + "\tdc_io[2 iterate] etbIn\n" +
"end_struct\n"; "end_struct\n";
GetConfigValueConsumer getConfigValueConsumer = new GetConfigValueConsumer(null); GetConfigValueConsumer getConfigValueConsumer = new GetConfigValueConsumer(null);
state.readBufferedReader(test, (getConfigValueConsumer)); state.readBufferedReader(test, getConfigValueConsumer);
assertEquals("#include \"pch.h\"\n" + assertEquals(
"static plain_get_float_s getF_plain[] = {\n" +
"};\n\n"
, getConfigValueConsumer.getFloatsSections());
assertEquals(
"float getConfigValueByName(const char *name) {\n" + "float getConfigValueByName(const char *name) {\n" +
"\t{\n" +
"\t\tplain_get_float_s * known = findFloat(name);\n" +
"\t\tif (known != nullptr) {\n" +
"\t\t\treturn *(float*)hackEngineConfigurationPointer(known->value);\n" +
"\t\t}\n" +
"\t}\n" +
"\treturn EFI_ERROR_CODE;\n" + "\treturn EFI_ERROR_CODE;\n" +
"}\n", getConfigValueConsumer.getGetterForUnitTest()); "}\n", getConfigValueConsumer.getComleteGetterBody());
} }
@Test @Test
@ -44,7 +56,7 @@ public class GetConfigValueConsumerTest {
"ThermistorConf iat;\n" + "ThermistorConf iat;\n" +
"end_struct\n"; "end_struct\n";
GetConfigValueConsumer getConfigValueConsumer = new GetConfigValueConsumer(null); GetConfigValueConsumer getConfigValueConsumer = new GetConfigValueConsumer(null);
state.readBufferedReader(test, (getConfigValueConsumer)); state.readBufferedReader(test, getConfigValueConsumer);
assertEquals("\tif (strEqualCaseInsensitive(name, \"iat.config.tempC_1\"))\n" + assertEquals("\tif (strEqualCaseInsensitive(name, \"iat.config.tempC_1\"))\n" +
"\t{\n" + "\t{\n" +
@ -57,14 +69,23 @@ public class GetConfigValueConsumerTest {
"\t\treturn;\n" + "\t\treturn;\n" +
"\t}\n", getConfigValueConsumer.getSetterBody()); "\t}\n", getConfigValueConsumer.getSetterBody());
assertEquals("#include \"pch.h\"\n" + assertEquals(
"float getConfigValueByName(const char *name) {\n" + "static plain_get_float_s getF_plain[] = {\n" +
"\tif (strEqualCaseInsensitive(name, \"iat.config.tempC_1\"))\n" + "\t{\"iat.config.tempC_1\", &engineConfiguration->iat.config.tempC_1},\n" +
"\t\treturn config->iat.config.tempC_1;\n" + "};\n" +
"\n", getConfigValueConsumer.getFloatsSections());
assertEquals("float getConfigValueByName(const char *name) {\n" +
"\t{\n" +
"\t\tplain_get_float_s * known = findFloat(name);\n" +
"\t\tif (known != nullptr) {\n" +
"\t\t\treturn *(float*)hackEngineConfigurationPointer(known->value);\n" +
"\t\t}\n" +
"\t}\n" +
"\tif (strEqualCaseInsensitive(name, \"iat.adcChannel\"))\n" + "\tif (strEqualCaseInsensitive(name, \"iat.adcChannel\"))\n" +
"\t\treturn config->iat.adcChannel;\n" + "\t\treturn config->iat.adcChannel;\n" +
"\treturn EFI_ERROR_CODE;\n" + "\treturn EFI_ERROR_CODE;\n" +
"}\n", getConfigValueConsumer.getGetterForUnitTest()); "}\n", getConfigValueConsumer.getComleteGetterBody());
} }
@Test @Test
@ -120,28 +141,43 @@ public class GetConfigValueConsumerTest {
GetConfigValueConsumer getConfigValueConsumer = new GetConfigValueConsumer(null); GetConfigValueConsumer getConfigValueConsumer = new GetConfigValueConsumer(null);
state.readBufferedReader(test, (getConfigValueConsumer)); state.readBufferedReader(test, getConfigValueConsumer);
assertEquals("#include \"pch.h\"\n" + assertEquals("#include \"pch.h\"\n" +
"#include \"value_lookup.h\"\n" +
"plain_get_float_s * findFloat(const char *name) {\n" +
"\tplain_get_float_s *currentF = &getF_plain[0];\n" +
"\twhile (currentF < getF_plain + sizeof(getF_plain)/sizeof(getF_plain[0])) {\n" +
"\t\tif (strEqualCaseInsensitive(name, currentF->token)) {\n" +
"\t\t\treturn currentF;\n" +
"\t\t}\n" +
"\t\tcurrentF++;\n" +
"\t}\n" +
"\treturn nullptr;\n" +
"}\n" +
"static plain_get_float_s getF_plain[] = {\n" +
"\t{\"clt.config.tempC_1\", &engineConfiguration->clt.config.tempC_1},\n" +
"\t{\"clt.config.map.sensor.highValue\", &engineConfiguration->clt.config.map.sensor.highValue},\n" +
"\t{\"clt.config.injector.flow\", &engineConfiguration->clt.config.injector.flow},\n" +
"\t{\"clt.config.bias_resistor\", &engineConfiguration->clt.config.bias_resistor},\n" +
"\t{\"afr_type\", &engineConfiguration->afr_type},\n" +
"};\n" +
"\n" +
"float getConfigValueByName(const char *name) {\n" + "float getConfigValueByName(const char *name) {\n" +
"\tif (strEqualCaseInsensitive(name, \"clt.config.tempC_1\"))\n" + "\t{\n" +
"\t\treturn config->clt.config.tempC_1;\n" + "\t\tplain_get_float_s * known = findFloat(name);\n" +
"\tif (strEqualCaseInsensitive(name, \"clt.config.map.sensor.highValue\"))\n" + "\t\tif (known != nullptr) {\n" +
"\t\treturn config->clt.config.map.sensor.highValue;\n" + "\t\t\treturn *(float*)hackEngineConfigurationPointer(known->value);\n" +
"\t\t}\n" +
"\t}\n" +
"\tif (strEqualCaseInsensitive(name, \"clt.config.map.sensor.hwChannel\"))\n" + "\tif (strEqualCaseInsensitive(name, \"clt.config.map.sensor.hwChannel\"))\n" +
"\t\treturn config->clt.config.map.sensor.hwChannel;\n" + "\t\treturn config->clt.config.map.sensor.hwChannel;\n" +
"\tif (strEqualCaseInsensitive(name, \"clt.config.injector.flow\"))\n" +
"\t\treturn config->clt.config.injector.flow;\n" +
"\tif (strEqualCaseInsensitive(name, \"clt.config.bias_resistor\"))\n" +
"\t\treturn config->clt.config.bias_resistor;\n" +
"\tif (strEqualCaseInsensitive(name, \"clt.adcChannel\"))\n" + "\tif (strEqualCaseInsensitive(name, \"clt.adcChannel\"))\n" +
"\t\treturn config->clt.adcChannel;\n" + "\t\treturn config->clt.adcChannel;\n" +
"\tif (strEqualCaseInsensitive(name, \"issue_294_31\"))\n" + "\tif (strEqualCaseInsensitive(name, \"issue_294_31\"))\n" +
"\t\treturn config->issue_294_31;\n" + "\t\treturn config->issue_294_31;\n" +
"\tif (strEqualCaseInsensitive(name, \"baseFuel\"))\n" + "\tif (strEqualCaseInsensitive(name, \"baseFuel\"))\n" +
"\t\treturn config->baseFuel;\n" + "\t\treturn config->baseFuel;\n" +
"\tif (strEqualCaseInsensitive(name, \"afr_type\"))\n" +
"\t\treturn config->afr_type;\n" +
"\tif (strEqualCaseInsensitive(name, \"speedToRpmRatio\"))\n" + "\tif (strEqualCaseInsensitive(name, \"speedToRpmRatio\"))\n" +
"\t\treturn config->speedToRpmRatio;\n" + "\t\treturn config->speedToRpmRatio;\n" +
"\tif (strEqualCaseInsensitive(name, \"afr_typet\"))\n" + "\tif (strEqualCaseInsensitive(name, \"afr_typet\"))\n" +
@ -153,6 +189,6 @@ public class GetConfigValueConsumerTest {
"\tif (strEqualCaseInsensitive(name, \"enableFan1WithAc\"))\n" + "\tif (strEqualCaseInsensitive(name, \"enableFan1WithAc\"))\n" +
"\t\treturn config->enableFan1WithAc;\n" + "\t\treturn config->enableFan1WithAc;\n" +
"\treturn EFI_ERROR_CODE;\n" + "\treturn EFI_ERROR_CODE;\n" +
"}\n", getConfigValueConsumer.getGetterForUnitTest()); "}\n", getConfigValueConsumer.getHeaderAndGetter());
} }
} }