multi dim arrays (#2867)

This commit is contained in:
Matthew Kennedy 2021-06-26 17:16:31 -07:00 committed by GitHub
parent a498e86944
commit 20ed673915
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 64 additions and 38 deletions

View File

@ -224,14 +224,12 @@ public class ParseState extends RusefiConfigGrammarBaseListener {
@Override @Override
public void exitArrayTypedefSuffix(RusefiConfigGrammarParser.ArrayTypedefSuffixContext ctx) { public void exitArrayTypedefSuffix(RusefiConfigGrammarParser.ArrayTypedefSuffixContext ctx) {
int arrayLength = this.arrayDim;
Type datatype = Type.findByTsType(ctx.Datatype().getText()); Type datatype = Type.findByTsType(ctx.Datatype().getText());
FieldOptions options = new FieldOptions(); FieldOptions options = new FieldOptions();
handleFieldOptionsList(options, ctx.fieldOptionsList()); handleFieldOptionsList(options, ctx.fieldOptionsList());
this.typedefs.put(this.typedefName, new ArrayTypedef(this.typedefName, arrayLength, datatype, options)); this.typedefs.put(this.typedefName, new ArrayTypedef(this.typedefName, this.arrayDim, datatype, options));
} }
@Override @Override
@ -349,7 +347,7 @@ public class ParseState extends RusefiConfigGrammarBaseListener {
handleFieldOptionsList(options, ctx.fieldOptionsList()); handleFieldOptionsList(options, ctx.fieldOptionsList());
ScalarField prototype = new ScalarField(arTypedef.type, name, options); ScalarField prototype = new ScalarField(arTypedef.type, name, options);
scope.structFields.add(new ArrayField<ScalarField>(prototype, arTypedef.length, false)); scope.structFields.add(new ArrayField<>(prototype, arTypedef.length, false));
return; return;
} else if (typedef instanceof EnumTypedef) { } else if (typedef instanceof EnumTypedef) {
EnumTypedef bTypedef = (EnumTypedef) typedef; EnumTypedef bTypedef = (EnumTypedef) typedef;
@ -421,7 +419,7 @@ public class ParseState extends RusefiConfigGrammarBaseListener {
public void exitArrayField(RusefiConfigGrammarParser.ArrayFieldContext ctx) { public void exitArrayField(RusefiConfigGrammarParser.ArrayFieldContext ctx) {
String type = ctx.identifier(0).getText(); String type = ctx.identifier(0).getText();
String name = ctx.identifier(1).getText(); String name = ctx.identifier(1).getText();
int length = this.arrayDim; int[] length = this.arrayDim;
// check if the iterate token is present // check if the iterate token is present
boolean iterate = ctx.Iterate() != null; boolean iterate = ctx.Iterate() != null;
@ -453,7 +451,7 @@ public class ParseState extends RusefiConfigGrammarBaseListener {
EnumField prototype = new EnumField(bTypedef.type, type, name, bTypedef.endBit, bTypedef.values, options); EnumField prototype = new EnumField(bTypedef.type, type, name, bTypedef.endBit, bTypedef.values, options);
scope.structFields.add(new ArrayField<EnumField>(prototype, length, iterate)); scope.structFields.add(new ArrayField<>(prototype, length, iterate));
return; return;
} else if (typedef instanceof StringTypedef) { } else if (typedef instanceof StringTypedef) {
StringTypedef sTypedef = (StringTypedef) typedef; StringTypedef sTypedef = (StringTypedef) typedef;
@ -462,7 +460,7 @@ public class ParseState extends RusefiConfigGrammarBaseListener {
assert(iterate); assert(iterate);
StringField prototype = new StringField(name, sTypedef.size); StringField prototype = new StringField(name, sTypedef.size);
scope.structFields.add(new ArrayField<StringField>(prototype, length, iterate)); scope.structFields.add(new ArrayField<>(prototype, length, iterate));
return; return;
} else { } else {
throw new RuntimeException("didn't understand type " + type + " for element " + name); throw new RuntimeException("didn't understand type " + type + " for element " + name);
@ -482,17 +480,19 @@ public class ParseState extends RusefiConfigGrammarBaseListener {
ScalarField prototype = new ScalarField(Type.findByCtype(type).get(), name, options); ScalarField prototype = new ScalarField(Type.findByCtype(type).get(), name, options);
scope.structFields.add(new ArrayField(prototype, length, iterate)); scope.structFields.add(new ArrayField<>(prototype, length, iterate));
} }
private int arrayDim = 0; private int[] arrayDim = null;
@Override @Override
public void exitArrayLengthSpec(RusefiConfigGrammarParser.ArrayLengthSpecContext ctx) { public void exitArrayLengthSpec(RusefiConfigGrammarParser.ArrayLengthSpecContext ctx) {
arrayDim = evalResults.remove().intValue(); int arrayDim0 = evalResults.remove().intValue();
if (ctx.ArrayDimensionSeparator() != null) { if (ctx.ArrayDimensionSeparator() != null) {
arrayDim *= evalResults.remove().intValue(); this.arrayDim = new int[] { arrayDim0, evalResults.remove().intValue() };
} else {
this.arrayDim = new int[] { arrayDim0 };
} }
} }

View File

@ -6,7 +6,7 @@ import com.rusefi.newparse.parsing.*;
import java.io.PrintStream; import java.io.PrintStream;
public class ArrayIterateScalarLayout extends ArrayLayout { public class ArrayIterateScalarLayout extends ArrayLayout {
public ArrayIterateScalarLayout(PrototypeField prototype, int length) { public ArrayIterateScalarLayout(PrototypeField prototype, int[] length) {
super(prototype, length); super(prototype, length);
} }
@ -24,7 +24,7 @@ public class ArrayIterateScalarLayout extends ArrayLayout {
protected void writeTunerstudioLayout(PrintStream ps, TsMetadata meta, StructNamePrefixer prefixer, int offsetAdd) { protected void writeTunerstudioLayout(PrintStream ps, TsMetadata meta, 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[0]; i++) {
emitOne(ps, meta, prefixer, this.offset + offsetAdd, i); emitOne(ps, meta, prefixer, this.offset + offsetAdd, i);
} }
} }

View File

@ -6,7 +6,7 @@ import com.rusefi.newparse.parsing.*;
import java.io.PrintStream; import java.io.PrintStream;
public class ArrayIterateStructLayout extends ArrayLayout { public class ArrayIterateStructLayout extends ArrayLayout {
public ArrayIterateStructLayout(StructField prototype, int length) { public ArrayIterateStructLayout(StructField prototype, int[] length) {
super(prototype, length); super(prototype, length);
} }
@ -24,7 +24,7 @@ public class ArrayIterateStructLayout extends ArrayLayout {
protected void writeTunerstudioLayout(PrintStream ps, TsMetadata meta, StructNamePrefixer prefixer, int offsetAdd) { protected void writeTunerstudioLayout(PrintStream ps, TsMetadata meta, 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[0]; i++) {
emitOne(ps, meta, prefixer, this.offset + offsetAdd, i); emitOne(ps, meta, prefixer, this.offset + offsetAdd, i);
} }
} }

View File

@ -6,11 +6,11 @@ import com.rusefi.newparse.parsing.*;
import java.io.PrintStream; import java.io.PrintStream;
public class ArrayLayout extends Layout { public class ArrayLayout extends Layout {
protected final int length; protected final int[] length;
protected final Layout prototypeLayout; protected final Layout prototypeLayout;
public ArrayLayout(PrototypeField prototype, int length) { public ArrayLayout(PrototypeField prototype, int[] length) {
this.length = length; this.length = length;
if (prototype instanceof ScalarField) { if (prototype instanceof ScalarField) {
@ -29,7 +29,13 @@ public class ArrayLayout extends Layout {
@Override @Override
public int getSize() { public int getSize() {
return this.prototypeLayout.getSize() * this.length; int size = this.prototypeLayout.getSize();
for (int x : this.length) {
size *= x;
}
return size;
} }
@Override @Override
@ -52,7 +58,7 @@ public class ArrayLayout extends Layout {
@Override @Override
public String toString() { public String toString() {
return "Array of " + this.prototypeLayout.toString() + " length " + this.length + " " + super.toString(); return "Array of " + this.prototypeLayout.toString() + " length " + this.length[0] + " " + super.toString();
} }
@Override @Override
@ -63,7 +69,7 @@ public class ArrayLayout extends Layout {
@Override @Override
public void writeCLayout(PrintStream ps) { public void writeCLayout(PrintStream ps) {
// Skip zero length arrays, they may be used for padding // Skip zero length arrays, they may be used for padding
if (this.length > 0) { if (this.length[0] > 0) {
this.prototypeLayout.writeCLayout(ps, this.length); this.prototypeLayout.writeCLayout(ps, this.length);
} }
} }

View File

@ -75,8 +75,8 @@ public class EnumLayout extends Layout {
} }
@Override @Override
public void writeCLayout(PrintStream ps, int arrayLength) { public void writeCLayout(PrintStream ps, int[] arrayLength) {
this.writeCOffsetHeader(ps, this.options.comment, this.options.units); this.writeCOffsetHeader(ps, this.options.comment, this.options.units);
ps.println("\t" + this.enumType + " " + this.name + "[" + arrayLength + "];"); ps.println("\t" + this.enumType + " " + this.name + "[" + arrayLength[0] + "];");
} }
} }

View File

@ -33,7 +33,7 @@ public abstract class Layout {
protected void writeTunerstudioLayout(PrintStream ps, TsMetadata meta, StructNamePrefixer prefixer, int offsetAdd) {} protected void writeTunerstudioLayout(PrintStream ps, TsMetadata meta, StructNamePrefixer prefixer, int offsetAdd) {}
protected void writeTunerstudioLayout(PrintStream ps, TsMetadata meta, StructNamePrefixer prefixer, int offsetAdd, int arrayLength) { protected void writeTunerstudioLayout(PrintStream ps, TsMetadata meta, 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!");
} }
@ -64,7 +64,7 @@ public abstract class Layout {
public void writeCLayout(PrintStream ps) { } public void writeCLayout(PrintStream ps) { }
public void writeCLayout(PrintStream ps, int arrayLength) { public void writeCLayout(PrintStream ps, int[] arrayLength) {
throw new IllegalStateException("This type can't be in an array!"); throw new IllegalStateException("This type can't be in an array!");
} }
} }

View File

@ -48,11 +48,11 @@ public class ScalarLayout extends Layout {
} }
@Override @Override
protected void writeTunerstudioLayout(PrintStream ps, TsMetadata meta, StructNamePrefixer prefixer, int offsetAdd, int arrayLength) { protected void writeTunerstudioLayout(PrintStream ps, TsMetadata meta, StructNamePrefixer prefixer, int offsetAdd, int[] arrayLength) {
if (arrayLength == 0) { if (arrayLength[0] == 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[0] == 1) {
// For 1-length arrays, emit as a plain scalar instead // For 1-length arrays, emit as a plain scalar instead
writeTunerstudioLayout(ps, meta, prefixer, offsetAdd); writeTunerstudioLayout(ps, meta, prefixer, offsetAdd);
return; return;
@ -61,7 +61,17 @@ public class ScalarLayout extends Layout {
printBeforeArrayLength(ps, meta, prefixer, "array", offsetAdd); printBeforeArrayLength(ps, meta, prefixer, "array", offsetAdd);
ps.print("["); ps.print("[");
ps.print(arrayLength); ps.print(arrayLength[0]);
for (int i = 1; i < arrayLength.length; i++) {
if (arrayLength[i] == 1) {
continue;
}
ps.print('x');
ps.print(arrayLength[i]);
}
ps.print("], "); ps.print("], ");
printAfterArrayLength(ps); printAfterArrayLength(ps);
@ -86,8 +96,18 @@ public class ScalarLayout extends Layout {
} }
@Override @Override
public void writeCLayout(PrintStream ps, int arrayLength) { public void writeCLayout(PrintStream ps, int[] arrayLength) {
this.writeCOffsetHeader(ps, this.options.comment, this.options.units); this.writeCOffsetHeader(ps, this.options.comment, this.options.units);
ps.println("\t" + this.type.cType.replaceAll("^int32_t$", "int") + " " + this.name + "[" + arrayLength + "];");
StringBuilder al = new StringBuilder();
al.append(arrayLength[0]);
for (int i = 1; i < arrayLength.length; i++) {
al.append("][");
al.append(arrayLength[i]);
}
ps.println("\t" + this.type.cType.replaceAll("^int32_t$", "int") + " " + this.name + "[" + al + "];");
} }
} }

View File

@ -53,8 +53,8 @@ public class StringLayout extends Layout {
} }
@Override @Override
public void writeCLayout(PrintStream ps, int arrayLength) { public void writeCLayout(PrintStream ps, int[] arrayLength) {
this.writeCOffsetHeader(ps, null, null); this.writeCOffsetHeader(ps, null, null);
ps.println("\tchar " + this.name + "[" + arrayLength + "][" + this.size + "];"); ps.println("\tchar " + this.name + "[" + arrayLength[0] + "][" + this.size + "];");
} }
} }

View File

@ -177,9 +177,9 @@ public class StructLayout extends Layout {
} }
@Override @Override
public void writeCLayout(PrintStream ps, int arrayLength) { public void writeCLayout(PrintStream ps, int[] arrayLength) {
this.writeCOffsetHeader(ps, null, null); this.writeCOffsetHeader(ps, null, null);
ps.println("\t" + this.typeName + " " + this.name + "[" + arrayLength + "];"); ps.println("\t" + this.typeName + " " + this.name + "[" + arrayLength[0] + "];");
} }
public void writeCLayoutRoot(PrintStream ps) { public void writeCLayoutRoot(PrintStream ps) {

View File

@ -1,11 +1,11 @@
package com.rusefi.newparse.parsing; package com.rusefi.newparse.parsing;
public class ArrayField<PrototypeType extends PrototypeField> implements Field { public class ArrayField<PrototypeType extends PrototypeField> implements Field {
public final int length; public final int[] length;
public final Boolean iterate; public final Boolean iterate;
public final PrototypeType prototype; public final PrototypeType prototype;
public ArrayField(PrototypeType prototype, int length, Boolean iterate) { public ArrayField(PrototypeType prototype, int[] length, Boolean iterate) {
this.length = length; this.length = length;
this.iterate = iterate; this.iterate = iterate;
this.prototype = prototype; this.prototype = prototype;

View File

@ -3,9 +3,9 @@ package com.rusefi.newparse.parsing;
public class ArrayTypedef extends Typedef { public class ArrayTypedef extends Typedef {
public final FieldOptions options; public final FieldOptions options;
public final Type type; public final Type type;
public final int length; public final int[] length;
public ArrayTypedef(String name, int length, Type type, FieldOptions options) { public ArrayTypedef(String name, int[] length, Type type, FieldOptions options) {
super(name); super(name);
this.length = length; this.length = length;